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,
166 $expected_when_empty_metadata = [
170 'max-age' => Cache::PERMANENT,
174 $data[] = [$empty_metadata, $empty_render_array, $expected_when_empty_metadata];
175 $data[] = [$empty_metadata, $nonempty_render_array, $expected_when_empty_metadata];
176 $expected_when_nonempty_metadata = [
178 'contexts' => ['qux'],
179 'tags' => ['foo:bar'],
180 'max-age' => Cache::PERMANENT,
188 $data[] = [$nonempty_metadata, $empty_render_array, $expected_when_nonempty_metadata];
189 $data[] = [$nonempty_metadata, $nonempty_render_array, $expected_when_nonempty_metadata];
195 * @covers ::createFromRenderArray
196 * @dataProvider providerTestCreateFromRenderArray
198 public function testCreateFromRenderArray(array $render_array, BubbleableMetadata $expected) {
199 $this->assertEquals($expected, BubbleableMetadata::createFromRenderArray($render_array));
203 * Provides test data for createFromRenderArray().
207 public function providerTestCreateFromRenderArray() {
210 $empty_metadata = new BubbleableMetadata();
211 $nonempty_metadata = new BubbleableMetadata();
212 $nonempty_metadata->setCacheContexts(['qux'])
213 ->setCacheTags(['foo:bar'])
214 ->setAttachments(['settings' => ['foo' => 'bar']]);
216 $empty_render_array = [];
217 $nonempty_render_array = [
219 'contexts' => ['qux'],
220 'tags' => ['foo:bar'],
221 'max-age' => Cache::PERMANENT,
230 $data[] = [$empty_render_array, $empty_metadata];
231 $data[] = [$nonempty_render_array, $nonempty_metadata];
237 * Tests library asset merging.
239 * @covers ::mergeAttachments
241 public function testMergeAttachmentsLibraryMerging() {
245 'core/drupalSettings',
247 'drupalSettings' => [
255 'drupalSettings' => [
256 'bar' => ['a', 'b', 'c'],
259 $expected['#attached'] = [
262 'core/drupalSettings',
265 'drupalSettings' => [
267 'bar' => ['a', 'b', 'c'],
270 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly.');
272 // Merging in the opposite direction yields the opposite library order.
273 $expected['#attached'] = [
277 'core/drupalSettings',
279 'drupalSettings' => [
280 'bar' => ['a', 'b', 'c'],
284 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.');
286 // Merging with duplicates: duplicates are simply retained, it's up to the
287 // rest of the system to handle duplicates.
288 $b['#attached']['library'][] = 'core/drupalSettings';
289 $expected['#attached'] = [
292 'core/drupalSettings',
294 'core/drupalSettings',
296 'drupalSettings' => [
298 'bar' => ['a', 'b', 'c'],
301 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly; duplicates are retained.');
303 // Merging with duplicates (simple case).
304 $b['#attached']['drupalSettings']['foo'] = ['a', 'b', 'c'];
305 $expected['#attached'] = [
308 'core/drupalSettings',
310 'core/drupalSettings',
312 'drupalSettings' => [
313 'foo' => ['a', 'b', 'c'],
314 'bar' => ['a', 'b', 'c'],
317 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']));
319 // Merging with duplicates (simple case) in the opposite direction yields
320 // the opposite JS setting asset order, but also opposite overriding order.
321 $expected['#attached'] = [
324 'core/drupalSettings',
326 'core/drupalSettings',
328 'drupalSettings' => [
329 'bar' => ['a', 'b', 'c'],
330 'foo' => ['d', 'b', 'c'],
333 $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']));
335 // Merging with duplicates: complex case.
336 // Only the second of these two entries should appear in drupalSettings.
338 $build['a']['#attached']['drupalSettings']['commonTest'] = 'firstValue';
339 $build['b']['#attached']['drupalSettings']['commonTest'] = 'secondValue';
340 // Only the second of these entries should appear in drupalSettings.
341 $build['a']['#attached']['drupalSettings']['commonTestJsArrayLiteral'] = ['firstValue'];
342 $build['b']['#attached']['drupalSettings']['commonTestJsArrayLiteral'] = ['secondValue'];
343 // Only the second of these two entries should appear in drupalSettings.
344 $build['a']['#attached']['drupalSettings']['commonTestJsObjectLiteral'] = ['key' => 'firstValue'];
345 $build['b']['#attached']['drupalSettings']['commonTestJsObjectLiteral'] = ['key' => 'secondValue'];
346 // Real world test case: multiple elements in a render array are adding the
347 // same (or nearly the same) JavaScript settings. When merged, they should
348 // contain all settings and not duplicate some settings.
349 $settings_one = ['moduleName' => ['ui' => ['button A', 'button B'], 'magical flag' => 3.14159265359]];
350 $build['a']['#attached']['drupalSettings']['commonTestRealWorldIdentical'] = $settings_one;
351 $build['b']['#attached']['drupalSettings']['commonTestRealWorldIdentical'] = $settings_one;
352 $settings_two_a = ['moduleName' => ['ui' => ['button A', 'button B', 'button C'], 'magical flag' => 3.14159265359, 'thingiesOnPage' => ['id1' => []]]];
353 $build['a']['#attached']['drupalSettings']['commonTestRealWorldAlmostIdentical'] = $settings_two_a;
354 $settings_two_b = ['moduleName' => ['ui' => ['button D', 'button E'], 'magical flag' => 3.14, 'thingiesOnPage' => ['id2' => []]]];
355 $build['b']['#attached']['drupalSettings']['commonTestRealWorldAlmostIdentical'] = $settings_two_b;
357 $merged = BubbleableMetadata::mergeAttachments($build['a']['#attached'], $build['b']['#attached']);
359 // Test whether #attached can be used to override a previous setting.
360 $this->assertSame('secondValue', $merged['drupalSettings']['commonTest']);
362 // Test whether #attached can be used to add and override a JavaScript
363 // array literal (an indexed PHP array) values.
364 $this->assertSame('secondValue', $merged['drupalSettings']['commonTestJsArrayLiteral'][0]);
366 // Test whether #attached can be used to add and override a JavaScript
367 // object literal (an associate PHP array) values.
368 $this->assertSame('secondValue', $merged['drupalSettings']['commonTestJsObjectLiteral']['key']);
370 // Test whether the two real world cases are handled correctly: the first
371 // adds the exact same settings twice and hence tests idempotency, the
372 // second adds *almost* the same settings twice: the second time, some
373 // values are altered, and some key-value pairs are added.
374 $settings_two['moduleName']['thingiesOnPage']['id1'] = [];
375 $this->assertSame($settings_one, $merged['drupalSettings']['commonTestRealWorldIdentical']);
376 $expected_settings_two = $settings_two_a;
377 $expected_settings_two['moduleName']['ui'][0] = 'button D';
378 $expected_settings_two['moduleName']['ui'][1] = 'button E';
379 $expected_settings_two['moduleName']['ui'][2] = 'button C';
380 $expected_settings_two['moduleName']['magical flag'] = 3.14;
381 $expected_settings_two['moduleName']['thingiesOnPage']['id2'] = [];
382 $this->assertSame($expected_settings_two, $merged['drupalSettings']['commonTestRealWorldAlmostIdentical']);
386 * Tests feed asset merging.
388 * @covers ::mergeAttachments
390 * @dataProvider providerTestMergeAttachmentsFeedMerging
392 public function testMergeAttachmentsFeedMerging($a, $b, $expected) {
393 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
397 * Data provider for testMergeAttachmentsFeedMerging
401 public function providerTestMergeAttachmentsFeedMerging() {
408 'taxonomy/term/1/feed',
430 // Merging in the opposite direction yields the opposite library order.
439 [$a, $b, $expected_a],
440 [$b, $a, $expected_b],
445 * Tests html_head asset merging.
447 * @covers ::mergeAttachments
449 * @dataProvider providerTestMergeAttachmentsHtmlHeadMerging
451 public function testMergeAttachmentsHtmlHeadMerging($a, $b, $expected) {
452 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
456 * Data provider for testMergeAttachmentsHtmlHeadMerging
460 public function providerTestMergeAttachmentsHtmlHeadMerging() {
464 'charset' => 'utf-8',
470 '#type' => 'html_tag',
473 'name' => 'Generator',
474 'content' => 'Kitten 1.0 (https://www.drupal.org/project/kitten)',
481 'system_meta_content_type',
488 'system_meta_generator',
495 'system_meta_content_type',
497 'system_meta_generator',
501 // Merging in the opposite direction yields the opposite library order.
505 'system_meta_generator',
507 'system_meta_content_type',
512 [$a, $b, $expected_a],
513 [$b, $a, $expected_b],
518 * Tests html_head_link asset merging.
520 * @covers ::mergeAttachments
522 * @dataProvider providerTestMergeAttachmentsHtmlHeadLinkMerging
524 public function testMergeAttachmentsHtmlHeadLinkMerging($a, $b, $expected) {
525 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
529 * Data provider for testMergeAttachmentsHtmlHeadLinkMerging
533 public function providerTestMergeAttachmentsHtmlHeadLinkMerging() {
536 'href' => 'http://rel.example.com',
540 'rel' => 'shortlink',
541 'href' => 'http://shortlink.example.com',
545 'html_head_link' => [
552 'html_head_link' => [
559 'html_head_link' => [
567 // Merging in the opposite direction yields the opposite library order.
569 'html_head_link' => [
578 [$a, $b, $expected_a],
579 [$b, $a, $expected_b],
584 * Tests http_header asset merging.
586 * @covers ::mergeAttachments
588 * @dataProvider providerTestMergeAttachmentsHttpHeaderMerging
590 public function testMergeAttachmentsHttpHeaderMerging($a, $b, $expected) {
591 $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
595 * Data provider for testMergeAttachmentsHttpHeaderMerging
599 public function providerTestMergeAttachmentsHttpHeaderMerging() {
602 'application/rss+xml; charset=utf-8',
607 'Sun, 19 Nov 1978 05:00:00 GMT',
629 // Merging in the opposite direction yields the opposite library order.
638 [$a, $b, $expected_a],
639 [$b, $a, $expected_b],
645 * @covers ::addCacheableDependency
646 * @dataProvider providerTestMerge
648 * This only tests at a high level, because it reuses existing logic. Detailed
649 * tests exist for the existing logic:
651 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
652 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
653 * @see \Drupal\Tests\Core\Cache\CacheContextsTest
655 public function testAddCacheableDependency(BubbleableMetadata $a, $b, BubbleableMetadata $expected) {
656 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
657 ->disableOriginalConstructor()
659 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
660 $container = new ContainerBuilder();
661 $container->set('cache_contexts_manager', $cache_contexts_manager);
662 \Drupal::setContainer($container);
664 $this->assertEquals($expected, $a->addCacheableDependency($b));
668 * Provides test data for testMerge().
672 public function providerTestAddCacheableDependency() {
674 // Merge in a cacheable metadata.
675 'merge-cacheable-metadata' => [
676 (new BubbleableMetadata())->setCacheContexts(['foo'])->setCacheTags(['foo'])->setCacheMaxAge(20),
677 (new CacheableMetadata())->setCacheContexts(['bar'])->setCacheTags(['bar'])->setCacheMaxAge(60),
678 (new BubbleableMetadata())->setCacheContexts(['foo', 'bar'])->setCacheTags(['foo', 'bar'])->setCacheMaxAge(20)
680 'merge-bubbleable-metadata' => [
681 (new BubbleableMetadata())->setCacheContexts(['foo'])->setCacheTags(['foo'])->setCacheMaxAge(20)->setAttachments(['foo' => []]),
682 (new BubbleableMetadata())->setCacheContexts(['bar'])->setCacheTags(['bar'])->setCacheMaxAge(60)->setAttachments(['bar' => []]),
683 (new BubbleableMetadata())->setCacheContexts(['foo', 'bar'])->setCacheTags(['foo', 'bar'])->setCacheMaxAge(20)->setAttachments(['foo' => [], 'bar' => []])
685 'merge-attachments-metadata' => [
686 (new BubbleableMetadata())->setAttachments(['foo' => []]),
687 (new BubbleableMetadata())->setAttachments(['baro' => []]),
688 (new BubbleableMetadata())->setAttachments(['foo' => [], 'bar' => []])