diff --git a/tests/src/Kernel/Views/ViewsCacheInvalidationTest.php b/tests/src/Kernel/Views/ViewsCacheInvalidationTest.php index e24889da..84800ff9 100644 --- a/tests/src/Kernel/Views/ViewsCacheInvalidationTest.php +++ b/tests/src/Kernel/Views/ViewsCacheInvalidationTest.php @@ -106,7 +106,7 @@ class ViewsCacheInvalidationTest extends KernelTestBase { /** * {@inheritdoc} */ - public static $modules = [ + protected static $modules = [ 'field', 'node', 'rest', @@ -239,17 +239,38 @@ class ViewsCacheInvalidationTest extends KernelTestBase { $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'); @@ -257,17 +278,26 @@ class ViewsCacheInvalidationTest extends KernelTestBase { // 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 @@ -280,7 +310,7 @@ class ViewsCacheInvalidationTest extends KernelTestBase { $this->assertNotCached('has-access'); // Check that the expected results are still returned and are cacheable. - $this->assertViewsResult('no-access', ['Angua', 'Cheery', 'Detritus']); + $this->assertViewsResult('no-access', ['Cheery']); $this->assertViewsResult('has-access', ['Angua', 'Cheery', 'Detritus']); $this->assertCached('no-access'); $this->assertCached('has-access'); @@ -294,7 +324,7 @@ class ViewsCacheInvalidationTest extends KernelTestBase { $this->assertNotCached('has-access'); // Check that the expected results are still returned and are cacheable. - $this->assertViewsResult('no-access', ['Angua', 'Cheery', 'Detritus']); + $this->assertViewsResult('no-access', ['Cheery']); $this->assertViewsResult('has-access', ['Angua', 'Cheery', 'Detritus']); $this->assertCached('no-access'); $this->assertCached('has-access'); @@ -307,7 +337,7 @@ class ViewsCacheInvalidationTest extends KernelTestBase { $this->assertNotCached('has-access'); // Check that the updated results are shown and are cacheable. - $this->assertViewsResult('no-access', ['Angua', 'Cheery', 'Detritus']); + $this->assertViewsResult('no-access', ['Cheery']); $this->assertViewsResult('has-access', ['Angua', 'Cheery', 'Detritus']); $this->assertCached('no-access'); $this->assertCached('has-access');