core/modules/aggregator/src/Tests/ItemCacheTagsTest.php | 10 ++++++++++ core/modules/comment/src/Tests/CommentCacheTagsTest.php | 10 ++++++++++ core/modules/shortcut/src/Tests/ShortcutCacheTagsTest.php | 11 +++++++++++ .../system/src/Tests/Entity/EntityCacheTagsTestBase.php | 11 ++++++++--- .../src/Tests/Entity/EntityWithUriCacheTagsTestBase.php | 2 +- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/core/modules/aggregator/src/Tests/ItemCacheTagsTest.php b/core/modules/aggregator/src/Tests/ItemCacheTagsTest.php index 3154e44..49dff1c 100644 --- a/core/modules/aggregator/src/Tests/ItemCacheTagsTest.php +++ b/core/modules/aggregator/src/Tests/ItemCacheTagsTest.php @@ -9,6 +9,7 @@ use Drupal\aggregator\Entity\Feed; use Drupal\aggregator\Entity\Item; +use Drupal\Core\Entity\EntityInterface; use Drupal\system\Tests\Entity\EntityCacheTagsTestBase; use Drupal\user\Entity\Role; @@ -83,4 +84,13 @@ public function testEntityCreation() { $this->assertFalse(\Drupal::cache('render')->get('foo'), 'Creating a new feed item invalidates the cache tag of the feed.'); } + /** + * {@inheritdoc} + */ + protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) { + return [ + 'user.roles', + ]; + } + } diff --git a/core/modules/comment/src/Tests/CommentCacheTagsTest.php b/core/modules/comment/src/Tests/CommentCacheTagsTest.php index 3874ba6..b6b9a37 100644 --- a/core/modules/comment/src/Tests/CommentCacheTagsTest.php +++ b/core/modules/comment/src/Tests/CommentCacheTagsTest.php @@ -80,6 +80,16 @@ protected function createEntity() { return $comment; } + + /** + * {@inheritdoc} + */ + protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) { + return [ + 'user.roles', + ]; + } + /** * {@inheritdoc} * diff --git a/core/modules/shortcut/src/Tests/ShortcutCacheTagsTest.php b/core/modules/shortcut/src/Tests/ShortcutCacheTagsTest.php index c67e0d0..a13040b 100644 --- a/core/modules/shortcut/src/Tests/ShortcutCacheTagsTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutCacheTagsTest.php @@ -7,6 +7,7 @@ namespace Drupal\shortcut\Tests; +use Drupal\Core\Entity\EntityInterface; use Drupal\shortcut\Entity\Shortcut; use Drupal\system\Tests\Entity\EntityCacheTagsTestBase; use Drupal\user\Entity\Role; @@ -71,4 +72,14 @@ public function testEntityCreation() { $this->assertFalse(\Drupal::cache('render')->get('foo'), 'Creating a new shortcut invalidates the cache tag of the shortcut set.'); } + + /** + * {@inheritdoc} + */ + protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) { + return [ + 'user.roles', + ]; + } + } diff --git a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php index a27b39c..8999167 100644 --- a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php @@ -304,7 +304,7 @@ public function testReferencedEntity() { $nonempty_entity_listing_url = Url::fromRoute('entity.entity_test.collection_labels_alphabetically', ['entity_type_id' => $entity_type]); // The default cache contexts for rendered entities. - $entity_cache_contexts = ['theme', 'user.roles']; + $entity_cache_contexts = ['theme']; // Cache tags present on every rendered page. $page_cache_tags = Cache::mergeTags( @@ -662,12 +662,17 @@ protected function verifyRenderCache($cid, array $tags, $redirected_cid = NULL) sort($cache_entry->tags); sort($tags); $this->assertIdentical($cache_entry->tags, $tags); + $is_redirecting_cache_item = isset($cache_entry->data['#cache_redirect']); if ($redirected_cid === NULL) { - $this->assertTrue(!isset($cache_entry->data['#cache_redirect']), 'Render cache entry is not a redirect.'); + $this->assertFalse($is_redirecting_cache_item, 'Render cache entry is not a redirect.'); + // If this is a redirecting cache item unlike we expected, log it. + if ($is_redirecting_cache_item) { + debug($cache_entry->data); + } } else { // Verify that $cid contains a cache redirect. - $this->assertTrue(isset($cache_entry->data['#cache_redirect']), 'Render cache entry is a redirect.'); + $this->assertTrue($is_redirecting_cache_item, 'Render cache entry is a redirect.'); // Verify that the cache redirect points to the expected CID. $redirect_cache_metadata = $cache_entry->data['#cache']; $actual_redirection_cid = $this->createCacheId( diff --git a/core/modules/system/src/Tests/Entity/EntityWithUriCacheTagsTestBase.php b/core/modules/system/src/Tests/Entity/EntityWithUriCacheTagsTestBase.php index c72d7d0..c046374 100644 --- a/core/modules/system/src/Tests/Entity/EntityWithUriCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityWithUriCacheTagsTestBase.php @@ -31,7 +31,7 @@ public function testEntityUri() { $view_mode = $this->selectViewMode($entity_type); // The default cache contexts for rendered entities. - $entity_cache_contexts = ['theme', 'user.roles']; + $entity_cache_contexts = ['theme']; // Generate the standardized entity cache tags. $cache_tag = $this->entity->getCacheTags();