3 namespace Drupal\Tests\Core\Render;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Render\BubbleableMetadata;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\DependencyInjection\ContainerBuilder;
12 * @coversDefaultClass \Drupal\Core\Render\BubbleableMetadata
15 class BubbleableMetadataTest extends UnitTestCase {
19 * @dataProvider providerTestMerge
21 * This only tests at a high level, because it reuses existing logic. Detailed
22 * tests exist for the existing logic:
24 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
25 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
26 * @see \Drupal\Tests\Core\Cache\CacheContextsTest
27 * @see \Drupal\Tests\Core\Render\RendererPlaceholdersTest
28 * @see testMergeAttachmentsLibraryMerging()
29 * @see testMergeAttachmentsFeedMerging()
30 * @see testMergeAttachmentsHtmlHeadMerging()
31 * @see testMergeAttachmentsHtmlHeadLinkMerging()
32 * @see testMergeAttachmentsHttpHeaderMerging()
34 public function testMerge(BubbleableMetadata $a, CacheableMetadata $b, BubbleableMetadata $expected) {
35 // Verify that if the second operand is a CacheableMetadata object, not a
36 // BubbleableMetadata object, that BubbleableMetadata::merge() doesn't
37 // attempt to merge assets.
38 if (!$b instanceof BubbleableMetadata) {
39 $renderer = $this->getMockBuilder('Drupal\Core\Render\Renderer')
40 ->disableOriginalConstructor()
41 ->setMethods(['mergeAttachments'])
43 $renderer->expects($this->never())
44 ->method('mergeAttachments');
46 // Otherwise, let the original ::mergeAttachments() method be executed.
48 $renderer = $this->getMockBuilder('Drupal\Core\Render\Renderer')
49 ->disableOriginalConstructor()
54 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
55 ->disableOriginalConstructor()
57 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
58 $container = new ContainerBuilder();
59 $container->set('cache_contexts_manager', $cache_contexts_manager);
60 $container->set('renderer', $renderer);
61 \Drupal::setContainer($container);
63 $this->assertEquals($expected, $a->merge($b));
67 * Provides test data for testMerge().
71 public function providerTestMerge() {
73 // Second operand is a BubbleableMetadata object.
75 [(new BubbleableMetadata()), (new BubbleableMetadata()), (new BubbleableMetadata())],
77 [(new BubbleableMetadata())->setCacheContexts(['foo']), (new BubbleableMetadata())->setCacheContexts(['bar']), (new BubbleableMetadata())->setCacheContexts(['bar', 'foo'])],
79 [(new BubbleableMetadata())->setCacheTags(['foo']), (new BubbleableMetadata())->setCacheTags(['bar']), (new BubbleableMetadata())->setCacheTags(['bar', 'foo'])],
81 [(new BubbleableMetadata())->setCacheMaxAge(60), (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT), (new BubbleableMetadata())->setCacheMaxAge(60)],
83 [(new BubbleableMetadata())->setAttachments(['library' => ['core/foo']]), (new BubbleableMetadata())->setAttachments(['library' => ['core/bar']]), (new BubbleableMetadata())->setAttachments(['library' => ['core/foo', 'core/bar']])],
85 [(new BubbleableMetadata())->setAttachments(['placeholders' => ['<my-placeholder>' => ['callback', ['A']]]]), (new BubbleableMetadata())->setAttachments(['placeholders' => ['<my-placeholder>' => ['callback', ['A']]]]), (new BubbleableMetadata())->setAttachments(['placeholders' => ['<my-placeholder>' => ['callback', ['A']]]])],
87 // Second operand is a CacheableMetadata object.
89 [(new BubbleableMetadata()), (new CacheableMetadata()), (new BubbleableMetadata())],
91 [(new BubbleableMetadata())->setCacheContexts(['foo']), (new CacheableMetadata())->setCacheContexts(['bar']), (new BubbleableMetadata())->setCacheContexts(['bar', 'foo'])],
93 [(new BubbleableMetadata())->setCacheTags(['foo']), (new CacheableMetadata())->setCacheTags(['bar']), (new BubbleableMetadata())->setCacheTags(['bar', 'foo'])],
95 [(new BubbleableMetadata())->setCacheMaxAge(60), (new CacheableMetadata())->setCacheMaxAge(Cache::PERMANENT), (new BubbleableMetadata())->setCacheMaxAge(60)],
100 * @covers ::addAttachments
101 * @covers ::setAttachments
102 * @dataProvider providerTestAddAttachments
104 * This only tests at a high level, because it reuses existing logic. Detailed
105 * tests exist for the existing logic:
107 * @see testMergeAttachmentsLibraryMerging()
108 * @see testMergeAttachmentsFeedMerging()
109 * @see testMergeAttachmentsHtmlHeadMerging()
110 * @see testMergeAttachmentsHtmlHeadLinkMerging()
111 * @see testMergeAttachmentsHttpHeaderMerging()
113 public function testAddAttachments(BubbleableMetadata $initial, $attachments, BubbleableMetadata $expected) {
115 $test->addAttachments($attachments);
116 $this->assertEquals($expected, $test);
120 * Provides test data for testAddAttachments().
122 public function providerTestAddAttachments() {
124 [new BubbleableMetadata(), [], new BubbleableMetadata()],
125 [new BubbleableMetadata(), ['library' => ['core/foo']], (new BubbleableMetadata())->setAttachments(['library' => ['core/foo']])],
126 [(new BubbleableMetadata())->setAttachments(['library' => ['core/foo']]), ['library' => ['core/bar']], (new BubbleableMetadata())->setAttachments(['library' => ['core/foo', 'core/bar']])],
132 * @dataProvider providerTestApplyTo
134 public function testApplyTo(BubbleableMetadata $metadata, array $render_array, array $expected) {
135 $this->assertNull($metadata->applyTo($render_array));
136 $this->assertEquals($expected, $render_array);
140 * Provides test data for testApplyTo().
144 public function providerTestApplyTo() {
147 $empty_metadata = new BubbleableMetadata();
148 $nonempty_metadata = new BubbleableMetadata();
149 $nonempty_metadata->setCacheContexts(['qux'])
150 ->setCacheTags(['foo:bar'])
151 ->setAttachments(['settings' => ['foo' => 'bar']]);
153 $empty_render_array = [];
154 $nonempty_render_array = [
156 'contexts' => ['qux'],
157 'tags' => ['llamas:are:awesome:but:kittens:too'],
158 'max-age' => Cache::PERMANENT,
167 $expected_when_empty_metadata = [
171 'max-age' => Cache::PERMANENT,
175 $data[] = [$empty_metadata, $empty_render_array, $expected_when_empty_metadata];
176 $data[] = [$empty_metadata, $nonempty_render_array, $expected_when_empty_metadata];
177 $expected_when_nonempty_metadata = [
179 'contexts' => ['qux'],
180 'tags' => ['foo:bar'],
181 'max-age' => Cache::PERMANENT,
189 $data[] = [$nonempty_metadata, $empty_render_array, $expected_when_nonempty_metadata];
190 $data[] = [$nonempty_metadata, $nonempty_render_array, $expected_when_nonempty_metadata];
196 * @covers ::createFromRenderArray
197 * @dataProvider providerTestCreateFromRenderArray
199 public function testCreateFromRenderArray(array $render_array, BubbleableMetadata $expected) {
200 $this->assertEquals($expected, BubbleableMetadata::createFromRenderArray($render_array));
204 * Provides test data for createFromRenderArray().
208 public function providerTestCreateFromRenderArray() {
211 $empty_metadata = new BubbleableMetadata();
212 $nonempty_metadata = new BubbleableMetadata();
213 $nonempty_metadata->setCacheContexts(['qux'])
214 ->setCacheTags(['foo:bar'])
215 ->setAttachments(['settings' => ['foo' => 'bar']]);
217 $empty_render_array = [];
218 $nonempty_render_array = [
220 'contexts' => ['qux'],
221 'tags' => ['foo:bar'],
222 'max-age' => Cache::PERMANENT,
231 $data[] = [$empty_render_array, $empty_metadata];
232 $data[] = [$nonempty_render_array, $nonempty_metadata];
238 * Tests library asset merging.
240 * @covers ::mergeAttachments
242 public function testMergeAttachmentsLibraryMerging() {
246 'core/drupalSettings',
248 'drupalSettings' => [
256 'drupalSettings' => [
257 'bar' => ['a', 'b', 'c'],
260 $expected['#attached'] = [
263 'core/drupalSettings',
266 'drupalSettings' => [
268 'bar' => ['a', 'b', 'c'],
271 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly.');
273 // Merging in the opposite direction yields the opposite library order.
274 $expected['#attached'] = [
278 'core/drupalSettings',
280 'drupalSettings' => [
281 'bar' => ['a', 'b', 'c'],
285 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.');
287 // Merging with duplicates: duplicates are simply retained, it's up to the
288 // rest of the system to handle duplicates.
289 $b['#attached']['library'][] = 'core/drupalSettings';
290 $expected['#attached'] = [
293 'core/drupalSettings',
295 'core/drupalSettings',
297 'drupalSettings' => [
299 'bar' => ['a', 'b', 'c'],
302 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly; duplicates are retained.');
304 // Merging with duplicates (simple case).
305 $b['#attached']['drupalSettings']['foo'] = ['a', 'b', 'c'];
306 $expected['#attached'] = [
309 'core/drupalSettings',
311 'core/drupalSettings',
313 'drupalSettings' => [
314 'foo' => ['a', 'b', 'c'],
315 'bar' => ['a', 'b', 'c'],
318 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']));
320 // Merging with duplicates (simple case) in the opposite direction yields
321 // the opposite JS setting asset order, but also opposite overriding order.
322 $expected['#attached'] = [
325 'core/drupalSettings',
327 'core/drupalSettings',
329 'drupalSettings' => [
330 'bar' => ['a', 'b', 'c'],
331 'foo' => ['d', 'b', 'c'],
334 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']));
336 // Merging with duplicates: complex case.
337 // Only the second of these two entries should appear in drupalSettings.
339 $build['a']['#attached']['drupalSettings']['commonTest'] = 'firstValue';
340 $build['b']['#attached']['drupalSettings']['commonTest'] = 'secondValue';
341 // Only the second of these entries should appear in drupalSettings.
342 $build['a']['#attached']['drupalSettings']['commonTestJsArrayLiteral'] = ['firstValue'];
343 $build['b']['#attached']['drupalSettings']['commonTestJsArrayLiteral'] = ['secondValue'];
344 // Only the second of these two entries should appear in drupalSettings.
345 $build['a']['#attached']['drupalSettings']['commonTestJsObjectLiteral'] = ['key' => 'firstValue'];
346 $build['b']['#attached']['drupalSettings']['commonTestJsObjectLiteral'] = ['key' => 'secondValue'];
347 // Real world test case: multiple elements in a render array are adding the
348 // same (or nearly the same) JavaScript settings. When merged, they should
349 // contain all settings and not duplicate some settings.
350 $settings_one = ['moduleName' => ['ui' => ['button A', 'button B'], 'magical flag' => 3.14159265359]];
351 $build['a']['#attached']['drupalSettings']['commonTestRealWorldIdentical'] = $settings_one;
352 $build['b']['#attached']['drupalSettings']['commonTestRealWorldIdentical'] = $settings_one;
353 $settings_two_a = ['moduleName' => ['ui' => ['button A', 'button B', 'button C'], 'magical flag' => 3.14159265359, 'thingiesOnPage' => ['id1' => []]]];
354 $build['a']['#attached']['drupalSettings']['commonTestRealWorldAlmostIdentical'] = $settings_two_a;
355 $settings_two_b = ['moduleName' => ['ui' => ['button D', 'button E'], 'magical flag' => 3.14, 'thingiesOnPage' => ['id2' => []]]];
356 $build['b']['#attached']['drupalSettings']['commonTestRealWorldAlmostIdentical'] = $settings_two_b;
358 $merged = BubbleableMetadata::mergeAttachments($build['a']['#attached'], $build['b']['#attached']);
360 // Test whether #attached can be used to override a previous setting.
361 $this->assertSame('secondValue', $merged['drupalSettings']['commonTest']);
363 // Test whether #attached can be used to add and override a JavaScript
364 // array literal (an indexed PHP array) values.
365 $this->assertSame('secondValue', $merged['drupalSettings']['commonTestJsArrayLiteral'][0]);
367 // Test whether #attached can be used to add and override a JavaScript
368 // object literal (an associate PHP array) values.
369 $this->assertSame('secondValue', $merged['drupalSettings']['commonTestJsObjectLiteral']['key']);
371 // Test whether the two real world cases are handled correctly: the first
372 // adds the exact same settings twice and hence tests idempotency, the
373 // second adds *almost* the same settings twice: the second time, some
374 // values are altered, and some key-value pairs are added.
375 $settings_two['moduleName']['thingiesOnPage']['id1'] = [];
376 $this->assertSame($settings_one, $merged['drupalSettings']['commonTestRealWorldIdentical']);
377 $expected_settings_two = $settings_two_a;
378 $expected_settings_two['moduleName']['ui'][0] = 'button D';
379 $expected_settings_two['moduleName']['ui'][1] = 'button E';
380 $expected_settings_two['moduleName']['ui'][2] = 'button C';
381 $expected_settings_two['moduleName']['magical flag'] = 3.14;
382 $expected_settings_two['moduleName']['thingiesOnPage']['id2'] = [];
383 $this->assertSame($expected_settings_two, $merged['drupalSettings']['commonTestRealWorldAlmostIdentical']);
387 * Tests feed asset merging.
389 * @covers ::mergeAttachments
391 * @dataProvider providerTestMergeAttachmentsFeedMerging
393 public function testMergeAttachmentsFeedMerging($a, $b, $expected) {
394 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
398 * Data provider for testMergeAttachmentsFeedMerging
402 public function providerTestMergeAttachmentsFeedMerging() {
409 'taxonomy/term/1/feed',
431 // Merging in the opposite direction yields the opposite library order.
440 [$a, $b, $expected_a],
441 [$b, $a, $expected_b],
446 * Tests html_head asset merging.
448 * @covers ::mergeAttachments
450 * @dataProvider providerTestMergeAttachmentsHtmlHeadMerging
452 public function testMergeAttachmentsHtmlHeadMerging($a, $b, $expected) {
453 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
457 * Data provider for testMergeAttachmentsHtmlHeadMerging
461 public function providerTestMergeAttachmentsHtmlHeadMerging() {
465 'charset' => 'utf-8',
471 '#type' => 'html_tag',
474 'name' => 'Generator',
475 'content' => 'Kitten 1.0 (https://www.drupal.org/project/kitten)',
482 'system_meta_content_type',
489 'system_meta_generator',
496 'system_meta_content_type',
498 'system_meta_generator',
502 // Merging in the opposite direction yields the opposite library order.
506 'system_meta_generator',
508 'system_meta_content_type',
513 [$a, $b, $expected_a],
514 [$b, $a, $expected_b],
519 * Tests html_head_link asset merging.
521 * @covers ::mergeAttachments
523 * @dataProvider providerTestMergeAttachmentsHtmlHeadLinkMerging
525 public function testMergeAttachmentsHtmlHeadLinkMerging($a, $b, $expected) {
526 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
530 * Data provider for testMergeAttachmentsHtmlHeadLinkMerging
534 public function providerTestMergeAttachmentsHtmlHeadLinkMerging() {
537 'href' => 'http://rel.example.com',
541 'rel' => 'shortlink',
542 'href' => 'http://shortlink.example.com',
546 'html_head_link' => [
553 'html_head_link' => [
560 'html_head_link' => [
568 // Merging in the opposite direction yields the opposite library order.
570 'html_head_link' => [
579 [$a, $b, $expected_a],
580 [$b, $a, $expected_b],
585 * Tests http_header asset merging.
587 * @covers ::mergeAttachments
589 * @dataProvider providerTestMergeAttachmentsHttpHeaderMerging
591 public function testMergeAttachmentsHttpHeaderMerging($a, $b, $expected) {
592 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
596 * Data provider for testMergeAttachmentsHttpHeaderMerging
600 public function providerTestMergeAttachmentsHttpHeaderMerging() {
603 'application/rss+xml; charset=utf-8',
608 'Sun, 19 Nov 1978 05:00:00 GMT',
630 // Merging in the opposite direction yields the opposite library order.
639 [$a, $b, $expected_a],
640 [$b, $a, $expected_b],
646 * @covers ::addCacheableDependency
647 * @dataProvider providerTestMerge
649 * This only tests at a high level, because it reuses existing logic. Detailed
650 * tests exist for the existing logic:
652 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
653 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
654 * @see \Drupal\Tests\Core\Cache\CacheContextsTest
656 public function testAddCacheableDependency(BubbleableMetadata $a, $b, BubbleableMetadata $expected) {
657 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
658 ->disableOriginalConstructor()
660 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
661 $container = new ContainerBuilder();
662 $container->set('cache_contexts_manager', $cache_contexts_manager);
663 \Drupal::setContainer($container);
665 $this->assertEquals($expected, $a->addCacheableDependency($b));
669 * Provides test data for testMerge().
673 public function providerTestAddCacheableDependency() {
675 // Merge in a cacheable metadata.
676 'merge-cacheable-metadata' => [
677 (new BubbleableMetadata())->setCacheContexts(['foo'])->setCacheTags(['foo'])->setCacheMaxAge(20),
678 (new CacheableMetadata())->setCacheContexts(['bar'])->setCacheTags(['bar'])->setCacheMaxAge(60),
679 (new BubbleableMetadata())->setCacheContexts(['foo', 'bar'])->setCacheTags(['foo', 'bar'])->setCacheMaxAge(20)
681 'merge-bubbleable-metadata' => [
682 (new BubbleableMetadata())->setCacheContexts(['foo'])->setCacheTags(['foo'])->setCacheMaxAge(20)->setAttachments(['foo' => []]),
683 (new BubbleableMetadata())->setCacheContexts(['bar'])->setCacheTags(['bar'])->setCacheMaxAge(60)->setAttachments(['bar' => []]),
684 (new BubbleableMetadata())->setCacheContexts(['foo', 'bar'])->setCacheTags(['foo', 'bar'])->setCacheMaxAge(20)->setAttachments(['foo' => [], 'bar' => []])
686 'merge-attachments-metadata' => [
687 (new BubbleableMetadata())->setAttachments(['foo' => []]),
688 (new BubbleableMetadata())->setAttachments(['baro' => []]),
689 (new BubbleableMetadata())->setAttachments(['foo' => [], 'bar' => []])