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()
42 $renderer->expects($this->never())
43 ->method('mergeAttachments');
45 // Otherwise, let the original ::mergeAttachments() method be executed.
47 $renderer = $this->getMockBuilder('Drupal\Core\Render\Renderer')
48 ->disableOriginalConstructor()
53 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
54 ->disableOriginalConstructor()
56 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
57 $container = new ContainerBuilder();
58 $container->set('cache_contexts_manager', $cache_contexts_manager);
59 $container->set('renderer', $renderer);
60 \Drupal::setContainer($container);
62 $this->assertEquals($expected, $a->merge($b));
66 * Provides test data for testMerge().
70 public function providerTestMerge() {
72 // Second operand is a BubbleableMetadata object.
74 [(new BubbleableMetadata()), (new BubbleableMetadata()), (new BubbleableMetadata())],
76 [(new BubbleableMetadata())->setCacheContexts(['foo']), (new BubbleableMetadata())->setCacheContexts(['bar']), (new BubbleableMetadata())->setCacheContexts(['bar', 'foo'])],
78 [(new BubbleableMetadata())->setCacheTags(['foo']), (new BubbleableMetadata())->setCacheTags(['bar']), (new BubbleableMetadata())->setCacheTags(['bar', 'foo'])],
80 [(new BubbleableMetadata())->setCacheMaxAge(60), (new BubbleableMetadata())->setCacheMaxAge(Cache::PERMANENT), (new BubbleableMetadata())->setCacheMaxAge(60)],
82 [(new BubbleableMetadata())->setAttachments(['library' => ['core/foo']]), (new BubbleableMetadata())->setAttachments(['library' => ['core/bar']]), (new BubbleableMetadata())->setAttachments(['library' => ['core/foo', 'core/bar']])],
84 [(new BubbleableMetadata())->setAttachments(['placeholders' => ['<my-placeholder>' => ['callback', ['A']]]]), (new BubbleableMetadata())->setAttachments(['placeholders' => ['<my-placeholder>' => ['callback', ['A']]]]), (new BubbleableMetadata())->setAttachments(['placeholders' => ['<my-placeholder>' => ['callback', ['A']]]])],
86 // Second operand is a CacheableMetadata object.
88 [(new BubbleableMetadata()), (new CacheableMetadata()), (new BubbleableMetadata())],
90 [(new BubbleableMetadata())->setCacheContexts(['foo']), (new CacheableMetadata())->setCacheContexts(['bar']), (new BubbleableMetadata())->setCacheContexts(['bar', 'foo'])],
92 [(new BubbleableMetadata())->setCacheTags(['foo']), (new CacheableMetadata())->setCacheTags(['bar']), (new BubbleableMetadata())->setCacheTags(['bar', 'foo'])],
94 [(new BubbleableMetadata())->setCacheMaxAge(60), (new CacheableMetadata())->setCacheMaxAge(Cache::PERMANENT), (new BubbleableMetadata())->setCacheMaxAge(60)],
99 * @covers ::addAttachments
100 * @covers ::setAttachments
101 * @dataProvider providerTestAddAttachments
103 * This only tests at a high level, because it reuses existing logic. Detailed
104 * tests exist for the existing logic:
106 * @see testMergeAttachmentsLibraryMerging()
107 * @see testMergeAttachmentsFeedMerging()
108 * @see testMergeAttachmentsHtmlHeadMerging()
109 * @see testMergeAttachmentsHtmlHeadLinkMerging()
110 * @see testMergeAttachmentsHttpHeaderMerging()
112 public function testAddAttachments(BubbleableMetadata $initial, $attachments, BubbleableMetadata $expected) {
114 $test->addAttachments($attachments);
115 $this->assertEquals($expected, $test);
119 * Provides test data for testAddAttachments().
121 public function providerTestAddAttachments() {
123 [new BubbleableMetadata(), [], new BubbleableMetadata()],
124 [new BubbleableMetadata(), ['library' => ['core/foo']], (new BubbleableMetadata())->setAttachments(['library' => ['core/foo']])],
125 [(new BubbleableMetadata())->setAttachments(['library' => ['core/foo']]), ['library' => ['core/bar']], (new BubbleableMetadata())->setAttachments(['library' => ['core/foo', 'core/bar']])],
131 * @dataProvider providerTestApplyTo
133 public function testApplyTo(BubbleableMetadata $metadata, array $render_array, array $expected) {
134 $this->assertNull($metadata->applyTo($render_array));
135 $this->assertEquals($expected, $render_array);
139 * Provides test data for testApplyTo().
143 public function providerTestApplyTo() {
146 $empty_metadata = new BubbleableMetadata();
147 $nonempty_metadata = new BubbleableMetadata();
148 $nonempty_metadata->setCacheContexts(['qux'])
149 ->setCacheTags(['foo:bar'])
150 ->setAttachments(['settings' => ['foo' => 'bar']]);
152 $empty_render_array = [];
153 $nonempty_render_array = [
155 'contexts' => ['qux'],
156 'tags' => ['llamas:are:awesome:but:kittens:too'],
157 '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,
232 $data[] = [$empty_render_array, $empty_metadata];
233 $data[] = [$nonempty_render_array, $nonempty_metadata];
239 * Tests library asset merging.
241 * @covers ::mergeAttachments
243 public function testMergeAttachmentsLibraryMerging() {
247 'core/drupalSettings',
249 'drupalSettings' => [
257 'drupalSettings' => [
258 'bar' => ['a', 'b', 'c'],
261 $expected['#attached'] = [
264 'core/drupalSettings',
267 'drupalSettings' => [
269 'bar' => ['a', 'b', 'c'],
272 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly.');
274 // Merging in the opposite direction yields the opposite library order.
275 $expected['#attached'] = [
279 'core/drupalSettings',
281 'drupalSettings' => [
282 'bar' => ['a', 'b', 'c'],
286 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.');
288 // Merging with duplicates: duplicates are simply retained, it's up to the
289 // rest of the system to handle duplicates.
290 $b['#attached']['library'][] = 'core/drupalSettings';
291 $expected['#attached'] = [
294 'core/drupalSettings',
296 'core/drupalSettings',
298 'drupalSettings' => [
300 'bar' => ['a', 'b', 'c'],
303 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly; duplicates are retained.');
305 // Merging with duplicates (simple case).
306 $b['#attached']['drupalSettings']['foo'] = ['a', 'b', 'c'];
307 $expected['#attached'] = [
310 'core/drupalSettings',
312 'core/drupalSettings',
314 'drupalSettings' => [
315 'foo' => ['a', 'b', 'c'],
316 'bar' => ['a', 'b', 'c'],
319 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']));
321 // Merging with duplicates (simple case) in the opposite direction yields
322 // the opposite JS setting asset order, but also opposite overriding order.
323 $expected['#attached'] = [
326 'core/drupalSettings',
328 'core/drupalSettings',
330 'drupalSettings' => [
331 'bar' => ['a', 'b', 'c'],
332 'foo' => ['d', 'b', 'c'],
335 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']));
337 // Merging with duplicates: complex case.
338 // Only the second of these two entries should appear in drupalSettings.
340 $build['a']['#attached']['drupalSettings']['commonTest'] = 'firstValue';
341 $build['b']['#attached']['drupalSettings']['commonTest'] = 'secondValue';
342 // Only the second of these entries should appear in drupalSettings.
343 $build['a']['#attached']['drupalSettings']['commonTestJsArrayLiteral'] = ['firstValue'];
344 $build['b']['#attached']['drupalSettings']['commonTestJsArrayLiteral'] = ['secondValue'];
345 // Only the second of these two entries should appear in drupalSettings.
346 $build['a']['#attached']['drupalSettings']['commonTestJsObjectLiteral'] = ['key' => 'firstValue'];
347 $build['b']['#attached']['drupalSettings']['commonTestJsObjectLiteral'] = ['key' => 'secondValue'];
348 // Real world test case: multiple elements in a render array are adding the
349 // same (or nearly the same) JavaScript settings. When merged, they should
350 // contain all settings and not duplicate some settings.
351 $settings_one = ['moduleName' => ['ui' => ['button A', 'button B'], 'magical flag' => 3.14159265359]];
352 $build['a']['#attached']['drupalSettings']['commonTestRealWorldIdentical'] = $settings_one;
353 $build['b']['#attached']['drupalSettings']['commonTestRealWorldIdentical'] = $settings_one;
354 $settings_two_a = ['moduleName' => ['ui' => ['button A', 'button B', 'button C'], 'magical flag' => 3.14159265359, 'thingiesOnPage' => ['id1' => []]]];
355 $build['a']['#attached']['drupalSettings']['commonTestRealWorldAlmostIdentical'] = $settings_two_a;
356 $settings_two_b = ['moduleName' => ['ui' => ['button D', 'button E'], 'magical flag' => 3.14, 'thingiesOnPage' => ['id2' => []]]];
357 $build['b']['#attached']['drupalSettings']['commonTestRealWorldAlmostIdentical'] = $settings_two_b;
359 $merged = BubbleableMetadata::mergeAttachments($build['a']['#attached'], $build['b']['#attached']);
361 // Test whether #attached can be used to override a previous setting.
362 $this->assertSame('secondValue', $merged['drupalSettings']['commonTest']);
364 // Test whether #attached can be used to add and override a JavaScript
365 // array literal (an indexed PHP array) values.
366 $this->assertSame('secondValue', $merged['drupalSettings']['commonTestJsArrayLiteral'][0]);
368 // Test whether #attached can be used to add and override a JavaScript
369 // object literal (an associate PHP array) values.
370 $this->assertSame('secondValue', $merged['drupalSettings']['commonTestJsObjectLiteral']['key']);
372 // Test whether the two real world cases are handled correctly: the first
373 // adds the exact same settings twice and hence tests idempotency, the
374 // second adds *almost* the same settings twice: the second time, some
375 // values are altered, and some key-value pairs are added.
376 $settings_two['moduleName']['thingiesOnPage']['id1'] = [];
377 $this->assertSame($settings_one, $merged['drupalSettings']['commonTestRealWorldIdentical']);
378 $expected_settings_two = $settings_two_a;
379 $expected_settings_two['moduleName']['ui'][0] = 'button D';
380 $expected_settings_two['moduleName']['ui'][1] = 'button E';
381 $expected_settings_two['moduleName']['ui'][2] = 'button C';
382 $expected_settings_two['moduleName']['magical flag'] = 3.14;
383 $expected_settings_two['moduleName']['thingiesOnPage']['id2'] = [];
384 $this->assertSame($expected_settings_two, $merged['drupalSettings']['commonTestRealWorldAlmostIdentical']);
388 * Tests feed asset merging.
390 * @covers ::mergeAttachments
392 * @dataProvider providerTestMergeAttachmentsFeedMerging
394 public function testMergeAttachmentsFeedMerging($a, $b, $expected) {
395 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
399 * Data provider for testMergeAttachmentsFeedMerging
403 public function providerTestMergeAttachmentsFeedMerging() {
410 'taxonomy/term/1/feed',
432 // Merging in the opposite direction yields the opposite library order.
441 [$a, $b, $expected_a],
442 [$b, $a, $expected_b],
447 * Tests html_head asset merging.
449 * @covers ::mergeAttachments
451 * @dataProvider providerTestMergeAttachmentsHtmlHeadMerging
453 public function testMergeAttachmentsHtmlHeadMerging($a, $b, $expected) {
454 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
458 * Data provider for testMergeAttachmentsHtmlHeadMerging
462 public function providerTestMergeAttachmentsHtmlHeadMerging() {
466 'charset' => 'utf-8',
472 '#type' => 'html_tag',
475 'name' => 'Generator',
476 'content' => 'Kitten 1.0 (https://www.drupal.org/project/kitten)',
483 'system_meta_content_type',
490 'system_meta_generator',
497 'system_meta_content_type',
499 'system_meta_generator',
503 // Merging in the opposite direction yields the opposite library order.
507 'system_meta_generator',
509 'system_meta_content_type',
514 [$a, $b, $expected_a],
515 [$b, $a, $expected_b],
520 * Tests html_head_link asset merging.
522 * @covers ::mergeAttachments
524 * @dataProvider providerTestMergeAttachmentsHtmlHeadLinkMerging
526 public function testMergeAttachmentsHtmlHeadLinkMerging($a, $b, $expected) {
527 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
531 * Data provider for testMergeAttachmentsHtmlHeadLinkMerging
535 public function providerTestMergeAttachmentsHtmlHeadLinkMerging() {
538 'href' => 'http://rel.example.com',
542 'rel' => 'shortlink',
543 'href' => 'http://shortlink.example.com',
547 'html_head_link' => [
554 'html_head_link' => [
561 'html_head_link' => [
569 // Merging in the opposite direction yields the opposite library order.
571 'html_head_link' => [
580 [$a, $b, $expected_a],
581 [$b, $a, $expected_b],
586 * Tests http_header asset merging.
588 * @covers ::mergeAttachments
590 * @dataProvider providerTestMergeAttachmentsHttpHeaderMerging
592 public function testMergeAttachmentsHttpHeaderMerging($a, $b, $expected) {
593 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
597 * Data provider for testMergeAttachmentsHttpHeaderMerging
601 public function providerTestMergeAttachmentsHttpHeaderMerging() {
604 'application/rss+xml; charset=utf-8',
609 'Sun, 19 Nov 1978 05:00:00 GMT',
631 // Merging in the opposite direction yields the opposite library order.
640 [$a, $b, $expected_a],
641 [$b, $a, $expected_b],
647 * @covers ::addCacheableDependency
648 * @dataProvider providerTestMerge
650 * This only tests at a high level, because it reuses existing logic. Detailed
651 * tests exist for the existing logic:
653 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
654 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
655 * @see \Drupal\Tests\Core\Cache\CacheContextsTest
657 public function testAddCacheableDependency(BubbleableMetadata $a, $b, BubbleableMetadata $expected) {
658 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
659 ->disableOriginalConstructor()
661 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
662 $container = new ContainerBuilder();
663 $container->set('cache_contexts_manager', $cache_contexts_manager);
664 \Drupal::setContainer($container);
666 $this->assertEquals($expected, $a->addCacheableDependency($b));
670 * Provides test data for testMerge().
674 public function providerTestAddCachableDependency() {
676 // Merge in a cacheable metadata.
677 'merge-cacheable-metadata' => [
678 (new BubbleableMetadata())->setCacheContexts(['foo'])->setCacheTags(['foo'])->setCacheMaxAge(20),
679 (new CacheableMetadata())->setCacheContexts(['bar'])->setCacheTags(['bar'])->setCacheMaxAge(60),
680 (new BubbleableMetadata())->setCacheContexts(['foo', 'bar'])->setCacheTags(['foo', 'bar'])->setCacheMaxAge(20)
682 'merge-bubbleable-metadata' => [
683 (new BubbleableMetadata())->setCacheContexts(['foo'])->setCacheTags(['foo'])->setCacheMaxAge(20)->setAttachments(['foo' => []]),
684 (new BubbleableMetadata())->setCacheContexts(['bar'])->setCacheTags(['bar'])->setCacheMaxAge(60)->setAttachments(['bar' => []]),
685 (new BubbleableMetadata())->setCacheContexts(['foo', 'bar'])->setCacheTags(['foo', 'bar'])->setCacheMaxAge(20)->setAttachments(['foo' => [], 'bar' => []])
687 'merge-attachments-metadata' => [
688 (new BubbleableMetadata())->setAttachments(['foo' => []]),
689 (new BubbleableMetadata())->setAttachments(['baro' => []]),
690 (new BubbleableMetadata())->setAttachments(['foo' => [], 'bar' => []])