core/lib/Drupal/Core/Render/BubbleableMetadata.php | 22 +++++++++++----------- core/modules/filter/src/FilterProcessResult.php | 12 ++++++------ .../Tests/Core/Render/BubbleableMetadataTest.php | 12 ++++++------ .../Tests/Core/Render/RendererBubblingTest.php | 11 ++++++----- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/core/lib/Drupal/Core/Render/BubbleableMetadata.php b/core/lib/Drupal/Core/Render/BubbleableMetadata.php index 7c94f43..fa2edf0 100644 --- a/core/lib/Drupal/Core/Render/BubbleableMetadata.php +++ b/core/lib/Drupal/Core/Render/BubbleableMetadata.php @@ -18,18 +18,18 @@ class BubbleableMetadata { /** - * Cache tags. + * Cache contexts. * * @var string[] */ - protected $tags; + protected $contexts; /** - * Cache contexts. + * Cache tags. * * @var string[] */ - protected $contexts; + protected $tags; /** * Attached assets. @@ -48,18 +48,18 @@ class BubbleableMetadata { /** * Constructs a BubbleableMetadata value object. * - * @param string[] $tags - * An array of cache tags. * @param string[] $contexts * An array of cache contexts. + * @param string[] $tags + * An array of cache tags. * @param array $attached * An array of attached assets. * @param array $post_render_cache * An array of #post_render_cache metadata. */ - public function __construct(array $tags = [], array $contexts = [], array $attached = [], array $post_render_cache = []) { - $this->tags = $tags; + public function __construct(array $contexts = [], array $tags = [], array $attached = [], array $post_render_cache = []) { $this->contexts = $contexts; + $this->tags = $tags; $this->attached = $attached; $this->postRenderCache = $post_render_cache; } @@ -79,8 +79,8 @@ public function __construct(array $tags = [], array $contexts = [], array $attac */ public function merge(BubbleableMetadata $other) { $result = new BubbleableMetadata(); + $result->contexts = Cache::mergeContexts($this->contexts, $other->contexts); $result->tags = Cache::mergeTags($this->tags, $other->tags); - $result->contexts = array_unique(array_merge($this->contexts, $other->contexts)); $result->attached = Renderer::mergeAttachments($this->attached, $other->attached); $result->postRenderCache = NestedArray::mergeDeep($this->postRenderCache, $other->postRenderCache); return $result; @@ -93,8 +93,8 @@ public function merge(BubbleableMetadata $other) { * A render array. */ public function applyTo(array &$build) { - $build['#cache']['tags'] = $this->tags; $build['#cache']['contexts'] = $this->contexts; + $build['#cache']['tags'] = $this->tags; $build['#attached'] = $this->attached; $build['#post_render_cache'] = $this->postRenderCache; } @@ -109,8 +109,8 @@ public function applyTo(array &$build) { */ public static function createFromRenderArray(array $build) { $meta = new static(); - $meta->tags = (isset($build['#cache']['tags'])) ? $build['#cache']['tags'] : []; $meta->contexts = (isset($build['#cache']['contexts'])) ? $build['#cache']['contexts'] : []; + $meta->tags = (isset($build['#cache']['tags'])) ? $build['#cache']['tags'] : []; $meta->attached = (isset($build['#attached'])) ? $build['#attached'] : []; $meta->postRenderCache = (isset($build['#post_render_cache'])) ? $build['#post_render_cache'] : []; return $meta; diff --git a/core/modules/filter/src/FilterProcessResult.php b/core/modules/filter/src/FilterProcessResult.php index cbed27e..b0d4fd2 100644 --- a/core/modules/filter/src/FilterProcessResult.php +++ b/core/modules/filter/src/FilterProcessResult.php @@ -88,7 +88,7 @@ class FilterProcessResult { /** * The associated cache contexts. * - * @var array + * @var string[] */ protected $cacheContexts; @@ -185,7 +185,7 @@ public function setCacheTags(array $cache_tags) { /** * Gets cache contexts associated with the processed text. * - * @return array + * @return string[] */ public function getCacheContexts() { return $this->cacheContexts; @@ -194,26 +194,26 @@ public function getCacheContexts() { /** * Adds cache contexts associated with the processed text. * - * @param array $cache_contexts + * @param string[] $cache_contexts * The cache contexts to be added. * * @return $this */ public function addCacheContexts(array $cache_contexts) { - $this->cacheContexts = array_unique(array_merge($this->cacheTags, $cache_contexts)); + $this->cacheContexts = Cache::mergeContexts($this->cacheTags, $cache_contexts); return $this; } /** * Sets cache contexts associated with the processed text. * - * @param array $cache_contexts + * @param string[] $cache_contexts * The cache contexts to be associated. * * @return $this */ public function setCacheContexts(array $cache_contexts) { - $this->cacheContexts= $cache_contexts; + $this->cacheContexts = $cache_contexts; return $this; } diff --git a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php index 88999e9..ea336da 100644 --- a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php +++ b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php @@ -35,13 +35,13 @@ public function providerTestApplyTo() { $data = []; $empty_metadata = new BubbleableMetadata(); - $nonempty_metadata = new BubbleableMetadata(['foo:bar'], ['qux'], ['settings' => ['foo' => 'bar']]); + $nonempty_metadata = new BubbleableMetadata(['qux'], ['foo:bar'], ['settings' => ['foo' => 'bar']]); $empty_render_array = []; $nonempty_render_array = [ '#cache' => [ - 'tags' => ['llamas:are:awesome:but:kittens:too'], 'contexts' => ['qux'], + 'tags' => ['llamas:are:awesome:but:kittens:too'], ], '#attached' => [ 'library' => [ @@ -54,8 +54,8 @@ public function providerTestApplyTo() { $expected_when_empty_metadata = [ '#cache' => [ - 'tags' => [], 'contexts' => [], + 'tags' => [], ], '#attached' => [], '#post_render_cache' => [], @@ -64,8 +64,8 @@ public function providerTestApplyTo() { $data[] = [$empty_metadata, $nonempty_render_array, $expected_when_empty_metadata]; $expected_when_nonempty_metadata = [ '#cache' => [ - 'tags' => ['foo:bar'], 'contexts' => ['qux'], + 'tags' => ['foo:bar'], ], '#attached' => [ 'settings' => [ @@ -97,13 +97,13 @@ public function providerTestCreateFromRenderArray() { $data = []; $empty_metadata = new BubbleableMetadata(); - $nonempty_metadata = new BubbleableMetadata(['foo:bar'], ['qux'], ['settings' => ['foo' => 'bar']]); + $nonempty_metadata = new BubbleableMetadata(['qux'], ['foo:bar'], ['settings' => ['foo' => 'bar']]); $empty_render_array = []; $nonempty_render_array = [ '#cache' => [ - 'tags' => ['foo:bar'], 'contexts' => ['qux'], + 'tags' => ['foo:bar'], ], '#attached' => [ 'settings' => [ diff --git a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php index 3b9f82d..336a22d 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php @@ -180,6 +180,7 @@ public function providerTestContextBubblingEdgeCases() { ]; foreach ($context_orders as $context_order) { $test_element['#cache']['contexts'] = $context_order; + sort($context_order); $expected_cache_items['set_test:bar:baz:foo']['#cache']['contexts'] = $context_order; $data[] = [$test_element, $context_order, $expected_cache_items]; } @@ -202,14 +203,14 @@ public function providerTestContextBubblingEdgeCases() { 'parent:bar:baz:foo' => [ '#attached' => [], '#cache' => [ - 'contexts' => ['foo', 'bar', 'baz'], + 'contexts' => ['bar', 'baz', 'foo'], 'tags' => ['rendered'], ], '#post_render_cache' => [], '#markup' => 'parent', ], ]; - $data[] = [$test_element, ['foo', 'bar', 'baz'], $expected_cache_items]; + $data[] = [$test_element, ['bar', 'baz', 'foo'], $expected_cache_items]; // A parent with a certain set of cache contexts that is a subset of the // cache contexts of a child gets a redirecting cache item for the cache ID @@ -251,14 +252,14 @@ public function providerTestContextBubblingEdgeCases() { 'parent:bar:foo' => [ '#attached' => [], '#cache' => [ - 'contexts' => ['foo', 'bar'], + 'contexts' => ['bar', 'foo'], 'tags' => ['dee', 'fiddle', 'har', 'yar', 'rendered'], ], '#post_render_cache' => [], '#markup' => 'parent', ], ]; - $data[] = [$test_element, ['foo', 'bar'], $expected_cache_items]; + $data[] = [$test_element, ['bar', 'foo'], $expected_cache_items]; return $data; } @@ -344,7 +345,7 @@ public function testConditionalCacheContextBubblingSelfHealing() { $this->assertRenderCacheItem('parent:bar:foo.1', [ '#attached' => [], '#cache' => [ - 'contexts' => ['foo', 'bar'], + 'contexts' => ['bar', 'foo'], 'tags' => ['a', 'b', 'c', 'rendered'], ], '#post_render_cache' => [],