diff --git a/core/lib/Drupal/Core/Cache/Cache.php b/core/lib/Drupal/Core/Cache/Cache.php index 82bb6a0..5ad9500 100644 --- a/core/lib/Drupal/Core/Cache/Cache.php +++ b/core/lib/Drupal/Core/Cache/Cache.php @@ -25,9 +25,9 @@ class Cache { * Merges arrays of cache contexts and removes duplicates. * * @param array $a - * Cache context array to merge. + * Cache contexts array to merge. * @param array $b - * Cache context array to merge. + * Cache contexts array to merge. * * @return string[] * The merged array of cache contexts. @@ -51,9 +51,9 @@ public static function mergeContexts(array $a = [], array $b = []) { * they're constituted from. * * @param array $a - * Cache tag array to merge. + * Cache tags array to merge. * @param array $b - * Cache tag array to merge. + * Cache tags array to merge. * * @return string[] * The merged array of cache tags. @@ -71,9 +71,9 @@ public static function mergeTags(array $a = [], array $b = []) { * Ensures infinite max-age (Cache::PERMANENT) is taken into account. * * @param int $a - * Max age value to find. + * Max age value to merge. * @param int $b - * Max age value to find. + * Max age value to merge. * * @return int * The minimum max-age value. diff --git a/core/modules/block/src/BlockViewBuilder.php b/core/modules/block/src/BlockViewBuilder.php index 4266fd4..d7161d7 100644 --- a/core/modules/block/src/BlockViewBuilder.php +++ b/core/modules/block/src/BlockViewBuilder.php @@ -48,14 +48,8 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la $derivative_id = $plugin->getDerivativeId(); $configuration = $plugin->getConfiguration(); - $cache_tags = Cache::mergeTags( - $this->getCacheTags(), - $entity->getCacheTags() - ); - $cache_tags = Cache::mergeTags( - $cache_tags, - $plugin->getCacheTags() - ); + $cache_tags = Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags()); + $cache_tags = Cache::mergeTags($cache_tags, $plugin->getCacheTags()); // Create the render array for the block as a whole. // @see template_preprocess_block(). diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php index d4068f8..3c7b9d6 100644 --- a/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php +++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php @@ -200,14 +200,8 @@ public function testEntityFormatter() { '; $renderer->renderRoot($build[0]); $this->assertEqual($build[0]['#markup'], 'default | ' . $this->referencedEntity->label() . $expected_rendered_name_field_1 . $expected_rendered_body_field_1, sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter)); - $expected_cache_tags = Cache::mergeTags( - \Drupal::entityManager()->getViewBuilder($this->entityType)->getCacheTags(), - $this->referencedEntity->getCacheTags() - ); - $expected_cache_tags = Cache::mergeTags( - $expected_cache_tags, - FilterFormat::load('full_html')->getCacheTags() - ); + $expected_cache_tags = Cache::mergeTags(\Drupal::entityManager()->getViewBuilder($this->entityType)->getCacheTags(), $this->referencedEntity->getCacheTags()); + $expected_cache_tags = Cache::mergeTags($expected_cache_tags, FilterFormat::load('full_html')->getCacheTags()); $this->assertEqual($build[0]['#cache']['tags'], $expected_cache_tags, format_string('The @formatter formatter has the expected cache tags.', array('@formatter' => $formatter))); // Test the second field item. diff --git a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php index 326103d..879ee7f 100644 --- a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php @@ -339,16 +339,13 @@ public function testReferencedEntity() { $entity_cache_contexts = $default_cache_contexts; // Cache tags present on every rendered page. - $page_cache_tags = Cache::mergeTags( - ['rendered'], - // 'user.permissions' is a required cache context, and responses that vary - // by this cache context when requested by anonymous users automatically - // also get this cache tag, to ensure correct invalidation. - ['config:user.role.anonymous'], - // If the block module is used, the Block page display variant is used, - // which adds the block config entity type's list cache tags. - \Drupal::moduleHandler()->moduleExists('block') ? ['config:block_list']: [] - ); + // 'user.permissions' is a required cache context, and responses that vary + // by this cache context when requested by anonymous users automatically + // also get this cache tag, to ensure correct invalidation. + // If the block module is used, the Block page display variant is used, + // which adds the block config entity type's list cache tags. + $page_cache_tags = Cache::mergeTags(['rendered', 'config:user.role.anonymous'], \Drupal::moduleHandler()->moduleExists('block') ? ['config:block_list']: []); + $page_cache_tags_referencing_entity = in_array('user.permissions', $this->getAccessCacheContextsForEntity($this->referencingEntity)) ? ['config:user.role.anonymous'] : []; $view_cache_tag = array(); @@ -361,58 +358,25 @@ public function testReferencedEntity() { $cache_context_tags = $context_metadata->getCacheTags(); // Generate the cache tags for the (non) referencing entities. - $referencing_entity_cache_tags = Cache::mergeTags( - $this->referencingEntity->getCacheTags(), - \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTags() - ); - $referencing_entity_cache_tags = Cache::mergeTags( - $referencing_entity_cache_tags, - // Includes the main entity's cache tags, since this entity references it. - $this->entity->getCacheTags() - ); - $referencing_entity_cache_tags = Cache::mergeTags( - $referencing_entity_cache_tags, - $this->getAdditionalCacheTagsForEntity($this->entity) - ); - $referencing_entity_cache_tags = Cache::mergeTags( - $referencing_entity_cache_tags, - $view_cache_tag - ); - $referencing_entity_cache_tags = Cache::mergeTags( - $referencing_entity_cache_tags, - $cache_context_tags, - ['rendered'] - ); + $referencing_entity_cache_tags = Cache::mergeTags($this->referencingEntity->getCacheTags(), \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTags()); + // Includes the main entity's cache tags, since this entity references it. + $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags, $this->entity->getCacheTags()); + $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags, $this->getAdditionalCacheTagsForEntity($this->entity)); + $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags, $view_cache_tag); + $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags, $cache_context_tags); + $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags, ['rendered']); - $non_referencing_entity_cache_tags = Cache::mergeTags( - $this->nonReferencingEntity->getCacheTags(), - \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTags() - ); - $non_referencing_entity_cache_tags = Cache::mergeTags( - $non_referencing_entity_cache_tags, - ['rendered'] - ); + $non_referencing_entity_cache_tags = Cache::mergeTags($this->nonReferencingEntity->getCacheTags(), \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTags()); + $non_referencing_entity_cache_tags = Cache::mergeTags($non_referencing_entity_cache_tags, ['rendered']); // Generate the cache tags for all two possible entity listing paths. // 1. list cache tag only (listing query has no match) // 2. list cache tag plus entity cache tag (listing query has a match) - $empty_entity_listing_cache_tags = Cache::mergeTags( - $this->entity->getEntityType()->getListCacheTags(), - $page_cache_tags - ); + $empty_entity_listing_cache_tags = Cache::mergeTags($this->entity->getEntityType()->getListCacheTags(), $page_cache_tags); - $nonempty_entity_listing_cache_tags = Cache::mergeTags( - $this->entity->getEntityType()->getListCacheTags(), - $this->entity->getCacheTags() - ); - $nonempty_entity_listing_cache_tags = Cache::mergeTags( - $nonempty_entity_listing_cache_tags, - $this->getAdditionalCacheTagsForEntityListing($this->entity) - ); - $nonempty_entity_listing_cache_tags = Cache::mergeTags( - $nonempty_entity_listing_cache_tags, - $page_cache_tags - ); + $nonempty_entity_listing_cache_tags = Cache::mergeTags($this->entity->getEntityType()->getListCacheTags(), $this->entity->getCacheTags()); + $nonempty_entity_listing_cache_tags = Cache::mergeTags($nonempty_entity_listing_cache_tags, $this->getAdditionalCacheTagsForEntityListing($this->entity)); + $nonempty_entity_listing_cache_tags = Cache::mergeTags($nonempty_entity_listing_cache_tags, $page_cache_tags); $this->pass("Test referencing entity.", 'Debug'); $this->verifyPageCache($referencing_entity_url, 'MISS'); @@ -673,23 +637,11 @@ public function testReferencedEntity() { $this->verifyPageCache($non_referencing_entity_url, 'HIT'); // Verify cache hits. - $referencing_entity_cache_tags = Cache::mergeTags( - $this->referencingEntity->getCacheTags(), - \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTags() - ); - $referencing_entity_cache_tags = Cache::mergeTags( - $referencing_entity_cache_tags, - ['rendered'] - ); + $referencing_entity_cache_tags = Cache::mergeTags($this->referencingEntity->getCacheTags(), \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTags()); + $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags, ['rendered']); - $nonempty_entity_listing_cache_tags = Cache::mergeTags( - $this->entity->getEntityType()->getListCacheTags(), - $this->getAdditionalCacheTagsForEntityListing() - ); - $nonempty_entity_listing_cache_tags = Cache::mergeTags( - $nonempty_entity_listing_cache_tags, - $page_cache_tags - ); + $nonempty_entity_listing_cache_tags = Cache::mergeTags($this->entity->getEntityType()->getListCacheTags(), $this->getAdditionalCacheTagsForEntityListing()); + $nonempty_entity_listing_cache_tags = Cache::mergeTags($nonempty_entity_listing_cache_tags, $page_cache_tags); $this->verifyPageCache($referencing_entity_url, 'HIT', Cache::mergeTags($referencing_entity_cache_tags, $page_cache_tags)); $this->verifyPageCache($listing_url, 'HIT', $page_cache_tags);