.../src/Tests/BlockContentCacheTagsTest.php | 9 ++++ .../Field/FieldFormatter/ImageFormatterBase.php | 2 +- .../src/Tests/Entity/EntityCacheTagsTestBase.php | 51 ++++++++++------------ .../Entity/EntityWithUriCacheTagsTestBase.php | 6 ++- 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php b/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php index ddd117d..582d832 100644 --- a/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php +++ b/core/modules/block_content/src/Tests/BlockContentCacheTagsTest.php @@ -55,6 +55,15 @@ protected function createEntity() { /** * {@inheritdoc} * + * @see \Drupal\block_content\BlockContentAccessControlHandler::checkAccess() + */ + protected function getAccessCacheContextsForEntity(EntityInterface $entity) { + return []; + } + + /** + * {@inheritdoc} + * * Each comment must have a comment body, which always has a text format. */ protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) { diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php index bf5686d..dad8275 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php @@ -53,7 +53,7 @@ protected function getEntitiesToView(array &$build, EntityReferenceFieldItemList // @todo Use RendererInterface::addDependency() when https://www.drupal.org/node/2444231 lands BubbleableMetadata::createFromRenderArray($build) ->merge(new BubbleableMetadata([], $file->getCacheTags())) - ->applyTo($build_list); + ->applyTo($build); } } diff --git a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php index 7cbd0c9..d9c4ef3 100644 --- a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php @@ -130,6 +130,21 @@ protected static function generateStandardizedInfo($entity_type_label, $group) { abstract protected function createEntity(); /** + * Returns the access cache contexts for the tested entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to be tested, as created by createEntity(). + * + * @return string[] + * An array of the additional cache contexts. + * + * @see \Drupal\Core\Entity\EntityAccessControlHandlerInterface + */ + protected function getAccessCacheContextsForEntity(EntityInterface $entity) { + return ['user.permissions']; + } + + /** * Returns the additional (non-standard) cache contexts for the tested entity. * * @param \Drupal\Core\Entity\EntityInterface $entity @@ -370,7 +385,11 @@ public function testReferencedEntity() { // Also verify the existence of an entity render cache entry. $cache_keys = ['entity_view', 'entity_test', $this->referencing_entity->id(), 'full']; $cid = $this->createCacheId($cache_keys, $entity_cache_contexts); - $redirected_cid = $this->createRedirectedCacheId($cache_keys, $entity_cache_contexts); + $access_cache_contexts = $this->getAccessCacheContextsForEntity($this->entity); + $redirected_cid = NULL; + if (count($access_cache_contexts)) { + $redirected_cid = $this->createCacheId($cache_keys, Cache::mergeContexts($entity_cache_contexts, $this->getAdditionalCacheContextsForEntity($this->referencing_entity), $access_cache_contexts)); + } $this->verifyRenderCache($cid, $referencing_entity_cache_tags, $redirected_cid); $this->pass("Test non-referencing entity.", 'Debug'); @@ -639,32 +658,6 @@ protected function createCacheId(array $keys, array $contexts) { } /** - * Creates the redirected cache ID, if any. - * - * If a subclass overrides ::getAdditionalCacheContextsForEntity(), it can - * specify the additional cache contexts by which the given entity must be - * varied, because those are the cache contexts that are bubbled from the - * field formatters. - * - * @param string[] $keys - * A list of cache keys used for the regular (pre-bubbling) CID. - * @param string[] $contexts - * A set of cache contexts used for the regular (pre-bubbling) CID. - * - * @return string|null - * The redirected (post-bubbling) CID, if any. - */ - protected function createRedirectedCacheId(array $keys, array $contexts) { - $additional_cache_contexts = $this->getAdditionalCacheContextsForEntity($this->referencing_entity); - if (count($additional_cache_contexts)) { - return $this->createCacheId($keys, Cache::mergeContexts($contexts, $additional_cache_contexts)); - } - else { - return NULL; - } - } - - /** * Verify that a given render cache entry exists, with the correct cache tags. * * @param string $cid @@ -692,6 +685,10 @@ protected function verifyRenderCache($cid, array $tags, $redirected_cid = NULL) else { // Verify that $cid contains a cache redirect. $this->assertTrue($is_redirecting_cache_item, 'Render cache entry is a redirect.'); + // If this is not a redirecting cache item unlike we expected, log it. + if (!$is_redirecting_cache_item) { + debug($cache_entry->data); + } // 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 50ec12b..0b6f430 100644 --- a/core/modules/system/src/Tests/Entity/EntityWithUriCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityWithUriCacheTagsTestBase.php @@ -51,7 +51,11 @@ public function testEntityUri() { if (\Drupal::entityManager()->getDefinition($entity_type)->isRenderCacheable()) { $cache_keys = ['entity_view', $entity_type, $this->entity->id(), $view_mode]; $cid = $this->createCacheId($cache_keys, $entity_cache_contexts); - $redirected_cid = $this->createRedirectedCacheId($cache_keys, $entity_cache_contexts); + $redirected_cid = NULL; + $additional_cache_contexts = $this->getAdditionalCacheContextsForEntity($this->entity); + if (count($additional_cache_contexts)) { + $redirected_cid = $this->createCacheId($cache_keys, Cache::mergeContexts($entity_cache_contexts, $additional_cache_contexts)); + } $expected_cache_tags = Cache::mergeTags($cache_tag, $view_cache_tag, $this->getAdditionalCacheTagsForEntity($this->entity), array($render_cache_tag)); $this->verifyRenderCache($cid, $expected_cache_tags, $redirected_cid); }