diff --git a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php index c5942a8..51ff506 100644 --- a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php +++ b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php @@ -359,241 +359,258 @@ function testMergeAttachmentsLibraryMerging() { * Tests feed asset merging. * * @covers ::mergeAttachments + * + * @dataProvider providerTestMergeAttachmentsFeedMerging */ - function testMergeAttachmentsFeedMerging() { - $a['#attached'] = array( - 'feed' => array( - array( - 'aggregator/rss', - 'Feed title', - ), - ), - ); - $b['#attached'] = array( - 'feed' => array( - array( - 'taxonomy/term/1/feed', - 'RSS - foo', - ), - ), - ); - $expected['#attached'] = array( - 'feed' => array( - array( - 'aggregator/rss', - 'Feed title', - ), - array( - 'taxonomy/term/1/feed', - 'RSS - foo', - ), - ), - ); - $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly.'); + function testMergeAttachmentsFeedMerging($a, $b, $expected) { + $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b)); + } + + /** + * Data provider for testMergeAttachmentsFeedMerging + * + * @return array + */ + public function providerTestMergeAttachmentsFeedMerging() { + $feed_a = [ + 'aggregator/rss', + 'Feed title', + ]; + + $feed_b = [ + 'taxonomy/term/1/feed', + 'RSS - foo', + ]; + + $a = [ + 'feed' => [ + $feed_a, + ], + ]; + $b = [ + 'feed' => [ + $feed_b, + ], + ]; + + $expected_a = [ + 'feed' => [ + $feed_a, + $feed_b, + ], + ]; // Merging in the opposite direction yields the opposite library order. - $expected['#attached'] = array( - 'feed' => array( - array( - 'taxonomy/term/1/feed', - 'RSS - foo', - ), - array( - 'aggregator/rss', - 'Feed title', - ), - ), - ); - $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.'); + $expected_b = [ + 'feed' => [ + $feed_b, + $feed_a, + ], + ]; + + return [ + [$a, $b, $expected_a], + [$b, $a, $expected_b], + ]; } /** * Tests html_head asset merging. * * @covers ::mergeAttachments + * + * @dataProvider providerTestMergeAttachmentsHtmlHeadMerging */ - function testMergeAttachmentsHtmlHeadMerging() { - $a['#attached'] = array( - 'html_head' => array( - array( - '#tag' => 'meta', - '#attributes' => array( - 'charset' => 'utf-8', - ), - '#weight' => -1000, - ), + function testMergeAttachmentsHtmlHeadMerging($a, $b, $expected) { + $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b)); + } + + /** + * Data provider for testMergeAttachmentsHtmlHeadMerging + * + * @return array + */ + public function providerTestMergeAttachmentsHtmlHeadMerging() { + $meta = [ + '#tag' => 'meta', + '#attributes' => [ + 'charset' => 'utf-8', + ], + '#weight' => -1000, + ]; + + $html_tag = [ + '#type' => 'html_tag', + '#tag' => 'meta', + '#attributes' => [ + 'name' => 'Generator', + 'content' => 'Kitten 1.0 (https://www.drupal.org/project/kitten)', + ], + ]; + + $a = [ + 'html_head' => [ + $meta, 'system_meta_content_type', - ), - ); - $b['#attached'] = array( - 'html_head' => array( - array( - '#type' => 'html_tag', - '#tag' => 'meta', - '#attributes' => array( - 'name' => 'Generator', - 'content' => 'Kitten 1.0 (https://www.drupal.org/project/kitten)', - ), - ), + ], + ]; + + $b = [ + 'html_head' => [ + $html_tag, 'system_meta_generator', - ), - ); - $expected['#attached'] = array( - 'html_head' => array( - array( - '#tag' => 'meta', - '#attributes' => array( - 'charset' => 'utf-8', - ), - '#weight' => -1000, - ), + ], + ]; + + $expected_a = [ + 'html_head' => [ + $meta, 'system_meta_content_type', - array( - '#type' => 'html_tag', - '#tag' => 'meta', - '#attributes' => array( - 'name' => 'Generator', - 'content' => 'Kitten 1.0 (https://www.drupal.org/project/kitten)', - ), - ), + $html_tag, 'system_meta_generator', - ), - ); - $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly.'); + ], + ]; // Merging in the opposite direction yields the opposite library order. - $expected['#attached'] = array( - 'html_head' => array( - array( - '#type' => 'html_tag', - '#tag' => 'meta', - '#attributes' => array( - 'name' => 'Generator', - 'content' => 'Kitten 1.0 (https://www.drupal.org/project/kitten)', - ), - ), + $expected_b = [ + 'html_head' => [ + $html_tag, 'system_meta_generator', - array( - '#tag' => 'meta', - '#attributes' => array( - 'charset' => 'utf-8', - ), - '#weight' => -1000, - ), + $meta, 'system_meta_content_type', - ), - ); - $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.'); + ], + ]; + + return [ + [$a, $b, $expected_a], + [$b, $a, $expected_b], + ]; } /** * Tests html_head_link asset merging. * * @covers ::mergeAttachments + * + * @dataProvider providerTestMergeAttachementsHtmlHeadLinkMerging */ - function testMergeAttachementsHtmlHeadLinkMerging() { - $a['#attached'] = array( - 'html_head_link' => array( - array( - 'rel' => 'rel', - 'href' => 'http://rel.example.com', - ), + function testMergeAttachementsHtmlHeadLinkMerging($a, $b, $expected) { + $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b)); + } + + /** + * Data provider for testMergeAttachementsHtmlHeadLinkMerging + * + * @return array + */ + public function providerTestMergeAttachementsHtmlHeadLinkMerging() { + $rel = [ + 'rel' => 'rel', + 'href' => 'http://rel.example.com', + ]; + + $shortlink = [ + 'rel' => 'shortlink', + 'href' => 'http://shortlink.example.com', + ]; + + $a = [ + 'html_head_link' => [ + $rel, TRUE, - ), - ); - $b['#attached'] = array( - 'html_head_link' => array( - array( - 'rel' => 'shortlink', - 'href' => 'http://shortlink.example.com', - ), + ], + ]; + + $b = [ + 'html_head_link' => [ + $shortlink, FALSE, - ), - ); - $expected['#attached'] = array( - 'html_head_link' => array( - array( - 'rel' => 'rel', - 'href' => 'http://rel.example.com', - ), + ], + ]; + + $expected_a = [ + 'html_head_link' => [ + $rel, TRUE, - array( - 'rel' => 'shortlink', - 'href' => 'http://shortlink.example.com', - ), + $shortlink, FALSE, - ), - ); - $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly.'); + ], + ]; // Merging in the opposite direction yields the opposite library order. - $expected['#attached'] = array( - 'html_head_link' => array( - array( - 'rel' => 'shortlink', - 'href' => 'http://shortlink.example.com', - ), + $expected_b = [ + 'html_head_link' => [ + $shortlink, FALSE, - array( - 'rel' => 'rel', - 'href' => 'http://rel.example.com', - ), + $rel, TRUE, - ), - ); - $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.'); + ], + ]; + + return [ + [$a, $b, $expected_a], + [$b, $a, $expected_b], + ]; } /** * Tests http_header asset merging. * * @covers ::mergeAttachments + * + * @dataProvider providerTestMergeAttachmentsHttpHeaderMerging */ - function testMergeAttachmentsHttpHeaderMerging() { - $a['#attached'] = array( - 'http_header' => array( - array( - 'Content-Type', - 'application/rss+xml; charset=utf-8', - ), - ), - ); - $b['#attached'] = array( - 'http_header' => array( - array( - 'Expires', - 'Sun, 19 Nov 1978 05:00:00 GMT', - ), - ), - ); - $expected['#attached'] = array( - 'http_header' => array( - array( - 'Content-Type', - 'application/rss+xml; charset=utf-8', - ), - array( - 'Expires', - 'Sun, 19 Nov 1978 05:00:00 GMT', - ), - ), - ); - $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached'], $b['#attached']), 'Attachments merged correctly.'); + function testMergeAttachmentsHttpHeaderMerging($a, $b, $expected) { + $this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b)); + } + + /** + * Data provider for testMergeAttachmentsHttpHeaderMerging + * + * @return array + */ + public function providerTestMergeAttachmentsHttpHeaderMerging() { + $content_type = [ + 'Content-Type', + 'application/rss+xml; charset=utf-8', + ]; + + $expires = [ + 'Expires', + 'Sun, 19 Nov 1978 05:00:00 GMT', + ]; + + $a = [ + 'http_header' => [ + $content_type, + ], + ]; + + $b = [ + 'http_header' => [ + $expires, + ], + ]; + + $expected_a = [ + 'http_header' => [ + $content_type, + $expires, + ], + ]; // Merging in the opposite direction yields the opposite library order. - $expected['#attached'] = array( - 'http_header' => array( - array( - 'Expires', - 'Sun, 19 Nov 1978 05:00:00 GMT', - ), - array( - 'Content-Type', - 'application/rss+xml; charset=utf-8', - ), - ), - ); - $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.'); + $expected_b = [ + 'http_header' => [ + $expires, + $content_type, + ], + ]; + + return [ + [$a, $b, $expected_a], + [$b, $a, $expected_b], + ]; } }