Commit 8114a72e authored by catch's avatar catch
Browse files

Issue #2505171 by Wim Leers: Follow-up for #2483433

parent 6bb1ba38
......@@ -97,7 +97,10 @@ public function getAttachments() {
* @return $this
*/
public function addAttachments(array $attachments) {
$this->attached = \Drupal::service('renderer')->mergeAttachments($this->attached, $attachments);
$other = (new BubbleableMetadata())->setAttachments($attachments);
$result = $other->merge($this);
$this->attached = $result->getAttachments();
return $this;
}
......
......@@ -638,37 +638,4 @@ public function addCacheableDependency(array &$elements, $dependency) {
$meta_a->merge($meta_b)->applyTo($elements);
}
/**
* {@inheritdoc}
*/
public function mergeAttachments(array $a, array $b) {
// If both #attached arrays contain drupalSettings, then merge them
// correctly; adding the same settings multiple times needs to behave
// idempotently.
if (!empty($a['drupalSettings']) && !empty($b['drupalSettings'])) {
$drupalSettings = NestedArray::mergeDeepArray(array($a['drupalSettings'], $b['drupalSettings']), TRUE);
// No need for re-merging them.
unset($a['drupalSettings']);
unset($b['drupalSettings']);
}
// Optimize merging of placeholders: no need for deep merging.
if (!empty($a['placeholders']) && !empty($b['placeholders'])) {
$placeholders = $a['placeholders'] + $b['placeholders'];
// No need for re-merging them.
unset($a['placeholders']);
unset($b['placeholders']);
}
// Apply the normal merge.
$a = array_merge_recursive($a, $b);
if (isset($drupalSettings)) {
// Save the custom merge for the drupalSettings.
$a['drupalSettings'] = $drupalSettings;
}
if (isset($placeholders)) {
// Save the custom merge for the placeholders.
$a['placeholders'] = $placeholders;
}
return $a;
}
}
......@@ -31,8 +31,12 @@ class BubbleableMetadataTest extends UnitTestCase {
* @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
* @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
* @see \Drupal\Tests\Core\Cache\CacheContextsTest
* @see \Drupal\system\Tests\Common\MergeAttachmentsTest
* @see \Drupal\Tests\Core\Render\RendererPlaceholdersTest
* @see testMergeAttachmentsLibraryMerging()
* @see testMergeAttachmentsFeedMerging()
* @see testMergeAttachmentsHtmlHeadMerging()
* @see testMergeAttachmentsHtmlHeadLinkMerging()
* @see testMergeAttachmentsHttpHeaderMerging()
*/
public function testMerge(BubbleableMetadata $a, CacheableMetadata $b, BubbleableMetadata $expected) {
// Verify that if the second operand is a CacheableMetadata object, not a
......@@ -97,6 +101,37 @@ public function providerTestMerge() {
];
}
/**
* @covers ::addAttachments
* @covers ::setAttachments
* @dataProvider providerTestAddAttachments
*
* This only tests at a high level, because it reuses existing logic. Detailed
* tests exist for the existing logic:
*
* @see testMergeAttachmentsLibraryMerging()
* @see testMergeAttachmentsFeedMerging()
* @see testMergeAttachmentsHtmlHeadMerging()
* @see testMergeAttachmentsHtmlHeadLinkMerging()
* @see testMergeAttachmentsHttpHeaderMerging()
*/
public function testAddAttachments(BubbleableMetadata $initial, $attachments, BubbleableMetadata $expected) {
$test = $initial;
$test->addAttachments($attachments);
$this->assertEquals($expected, $test);
}
/**
* Provides test data for testAddAttachments().
*/
public function providerTestAddAttachments() {
return [
[new BubbleableMetadata(), [], new BubbleableMetadata()],
[new BubbleableMetadata(), ['library' => ['core/foo']], (new BubbleableMetadata())->setAttachments(['library' => ['core/foo']])],
[(new BubbleableMetadata())->setAttachments(['library' => ['core/foo']]), ['library' => ['core/bar']], (new BubbleableMetadata())->setAttachments(['library' => ['core/bar', 'core/foo']])],
];
}
/**
* @covers ::applyTo
* @dataProvider providerTestApplyTo
......@@ -107,7 +142,7 @@ public function testApplyTo(BubbleableMetadata $metadata, array $render_array, a
}
/**
* Provides test data for apply().
* Provides test data for testApplyTo().
*
* @return array
*/
......@@ -494,7 +529,7 @@ public function providerTestMergeAttachmentsHtmlHeadMerging() {
*
* @dataProvider providerTestMergeAttachementsHtmlHeadLinkMerging
*/
function testMergeAttachementsHtmlHeadLinkMerging($a, $b, $expected) {
function testMergeAttachmentsHtmlHeadLinkMerging($a, $b, $expected) {
$this->assertSame($expected, BubbleableMetadata::mergeAttachments($a, $b));
}
......@@ -503,7 +538,7 @@ function testMergeAttachementsHtmlHeadLinkMerging($a, $b, $expected) {
*
* @return array
*/
public function providerTestMergeAttachementsHtmlHeadLinkMerging() {
public function providerTestMergeAttachmentsHtmlHeadLinkMerging() {
$rel = [
'rel' => 'rel',
'href' => 'http://rel.example.com',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment