public function ViewsCacheInvalidationTest::testQueryCacheInvalidation in Search API 8
Tests that a cached views query result is invalidated at the right moments.
File
- tests/
src/ Kernel/ Views/ ViewsCacheInvalidationTest.php, line 174
Class
- ViewsCacheInvalidationTest
- Tests that cached Search API views get invalidated at the right occasions.
Namespace
Drupal\Tests\search_api\Kernel\ViewsCode
public function testQueryCacheInvalidation() {
// We are testing two variants of the view, one for users that have
// permission to view unpublished entities, and one for users that do not.
// Initially both variants should be uncached.
$this
->assertNotCached('no-access');
$this
->assertNotCached('has-access');
// Check that the user with the "bypass node access" permission can see all
// 3 items.
$this
->assertViewsResult('has-access', [
'Cheery',
'Carrot',
'Detritus',
]);
// The result should now be cached for the privileged user.
$this
->assertNotCached('no-access');
$this
->assertCached('has-access');
// Check that the user without the "bypass node access" permission can only
// see the published items.
$this
->assertViewsResult('no-access', [
'Cheery',
'Carrot',
]);
// Both results should now be cached.
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// Add another unpublished item.
$this
->createNode('Angua', FALSE);
// Our search index is not configured to automatically index items, so just
// creating a node should not invalidate the caches.
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// Index the item, this should invalidate the caches.
$this->index
->indexItems();
$this
->assertNotCached('no-access');
$this
->assertNotCached('has-access');
// Check that the user without the "bypass node access" permission can still
// only see the published items.
$this
->assertViewsResult('no-access', [
'Cheery',
'Carrot',
]);
$this
->assertCached('no-access');
$this
->assertNotCached('has-access');
// Check that the user with the "bypass node access" permission can see all
// 4 items.
$this
->assertViewsResult('has-access', [
'Angua',
'Cheery',
'Carrot',
'Detritus',
]);
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// Grant the permission to "bypass node access" to the unprivileged user.
$privileged_role = $this->users['has-access']
->getRoles()[1];
$this->users['no-access']
->addRole($privileged_role);
$this->users['no-access']
->save();
// Changing the roles of a user should not affect the cached results. The
// user will now have a new cache context, but the old context should still
// be present for all other users that still have the same combination of
// roles that our "no-access" user had before they were changed.
// In fact, since our user now has the same set of roles as the "has-access"
// user, the user will immediately benefit from the cached results that
// already exist for the cache contexts of the "has-access" user.
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// The user should now be able to see all 4 items.
$this
->assertViewsResult('no-access', [
'Angua',
'Cheery',
'Carrot',
'Detritus',
]);
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// Remove the role again from the unprivileged user. This also should not
// affect cached results. The "no-access" user now switches back to only
// being able to see the published items, and everything is still happily
// cached.
$this->users['no-access']
->removeRole($privileged_role);
$this->users['no-access']
->save();
$this
->assertCached('no-access');
$this
->assertCached('has-access');
$this
->assertViewsResult('no-access', [
'Cheery',
'Carrot',
]);
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// Edit one of the test content entities. This should not affect the cached
// view until the search index is updated.
$this->nodes['Cheery']
->set('title', 'Cheery Littlebottom')
->save();
$this
->assertCached('no-access');
$this
->assertCached('has-access');
$this->index
->indexItems();
$this
->assertNotCached('no-access');
$this
->assertNotCached('has-access');
// The view should show the updated title when displayed, and the result
// should be cached.
$this
->assertViewsResult('has-access', [
'Angua',
'Cheery',
'Carrot',
'Detritus',
]);
$this
->assertNotCached('no-access');
$this
->assertCached('has-access');
$this
->assertViewsResult('no-access', [
'Cheery',
'Carrot',
]);
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// Delete one of the test content entities. This takes effect immediately,
// there is no need to wait until the search index is updated.
// @see search_api_entity_delete()
$this->nodes['Carrot']
->delete();
$this
->assertNotCached('no-access');
$this
->assertNotCached('has-access');
// The view should no longer include the deleted content now, and the result
// should be cached after the view has been displayed.
$this
->assertViewsResult('no-access', [
'Cheery',
]);
$this
->assertCached('no-access');
$this
->assertNotCached('has-access');
$this
->assertViewsResult('has-access', [
'Angua',
'Cheery',
'Detritus',
]);
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// Update the search index configuration so it will index items immediately
// when they are created or updated.
$this->index
->setOption('index_directly', TRUE)
->save();
// Changing the configuration of the index should invalidate all views that
// show its data.
$this
->assertNotCached('no-access');
$this
->assertNotCached('has-access');
// Check that the expected results are still returned and are cacheable.
$this
->assertViewsResult('no-access', [
'Cheery',
]);
$this
->assertViewsResult('has-access', [
'Angua',
'Cheery',
'Detritus',
]);
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// Change the configuration of the view. This should also invalidate all
// displays of the view.
$view = $this
->getView();
$view
->setItemsPerPage(20);
$view
->save();
$this
->assertNotCached('no-access');
$this
->assertNotCached('has-access');
// Check that the expected results are still returned and are cacheable.
$this
->assertViewsResult('no-access', [
'Cheery',
]);
$this
->assertViewsResult('has-access', [
'Angua',
'Cheery',
'Detritus',
]);
$this
->assertCached('no-access');
$this
->assertCached('has-access');
// Edit one of the test content entities. Because the search index is being
// updated immediately, the cached views should be cleared without having to
// perform a manual indexing step.
$this->nodes['Angua']
->set('title', 'Angua von Überwald')
->save();
$this
->assertNotCached('no-access');
$this
->assertNotCached('has-access');
// Check that the updated results are shown and are cacheable.
$this
->assertViewsResult('no-access', [
'Cheery',
]);
$this
->assertViewsResult('has-access', [
'Angua',
'Cheery',
'Detritus',
]);
$this
->assertCached('no-access');
$this
->assertCached('has-access');
}