MergeAttachmentsTest.php 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\Common\MergeAttachmentsTest.
 */

namespace Drupal\system\Tests\Common;

10
use Drupal\simpletest\KernelTestBase;
11 12 13

/**
 * Tests the merging of attachments.
14
 *
15 16
 * @see drupal_merge_attached()
 *
17
 * @group Common
18
 */
19
class MergeAttachmentsTest extends KernelTestBase {
20 21 22 23 24 25 26

  /**
   * Tests justs library asset merging.
   */
  function testLibraryMerging() {
    $a['#attached'] = array(
      'library' => array(
27 28
        'core/drupal',
        'core/drupalSettings',
29 30 31 32
      ),
    );
    $b['#attached'] = array(
      'library' => array(
33
        'core/jquery',
34 35 36 37
      ),
    );
    $expected['#attached'] = array(
      'library' => array(
38 39 40
        'core/drupal',
        'core/drupalSettings',
        'core/jquery',
41 42 43 44 45 46 47
      ),
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($a['#attached'], $b['#attached']), 'Attachments merged correctly.');

    // Merging in the opposite direction yields the opposite library order.
    $expected['#attached'] = array(
      'library' => array(
48 49 50
        'core/jquery',
        'core/drupal',
        'core/drupalSettings',
51 52 53 54 55 56
      ),
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.');

    // Merging with duplicates: duplicates are simply retained, it's up to the
    // rest of the system to handle duplicates.
57
    $b['#attached']['library'][] = 'core/drupalSettings';
58 59
    $expected['#attached'] = array(
      'library' => array(
60 61 62 63
        'core/drupal',
        'core/drupalSettings',
        'core/jquery',
        'core/drupalSettings',
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
      ),
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($a['#attached'], $b['#attached']), 'Attachments merged correctly; duplicates are retained.');
  }

  /**
   * Tests justs CSS asset merging.
   */
  function testCssMerging() {
    $a['#attached'] = array(
      'css' => array(
        'foo.css' => array(),
        'bar.css' => array(),
      ),
    );
    $b['#attached'] = array(
      'css' => array(
        'baz.css' => array(),
      ),
    );
    $expected['#attached'] = array(
      'css' => array(
        'foo.css' => array(),
        'bar.css' => array(),
        'baz.css' => array(),
      ),
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($a['#attached'], $b['#attached']), 'Attachments merged correctly.');

    // Merging in the opposite direction yields the opposite CSS asset order.
    $expected['#attached'] = array(
      'css' => array(
        'baz.css' => array(),
        'foo.css' => array(),
        'bar.css' => array(),
      ),
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.');

    // Merging with duplicates: duplicates are automatically removed because the
    // values have unique keys.
    $b['#attached']['css']['bar.css'] = array();
    $expected['#attached'] = array(
      'css' => array(
        'foo.css' => array(),
        'bar.css' => array(),
        'baz.css' => array(),
      ),
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($a['#attached'], $b['#attached']), 'Attachments merged correctly; CSS asset duplicates removed.');
  }

  /**
   * Tests justs JavaScript asset merging.
   */
  function testJsMerging() {
    $a['#attached'] = array(
      'js' => array(
        'foo.js' => array(),
        'bar.js' => array(),
      ),
    );
    $b['#attached'] = array(
      'js' => array(
        'baz.js' => array(),
      ),
    );
    $expected['#attached'] = array(
      'js' => array(
        'foo.js' => array(),
        'bar.js' => array(),
        'baz.js' => array(),
      ),
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($a['#attached'], $b['#attached']), 'Attachments merged correctly.');

    // Merging in the opposite direction yields the opposite JS asset order.
    $expected['#attached'] = array(
      'js' => array(
        'baz.js' => array(),
        'foo.js' => array(),
        'bar.js' => array(),
      ),
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.');

    // Merging with duplicates: duplicates are automatically removed because the
    // values have unique keys.
    $b['#attached']['js']['bar.js'] = array();
    $expected['#attached'] = array(
      'js' => array(
        'foo.js' => array(),
        'bar.js' => array(),
        'baz.js' => array(),
      ),
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($a['#attached'], $b['#attached']), 'Attachments merged correctly; JS asset duplicates removed.');
  }

  /**
   * Tests justs JavaScript and JavaScript setting asset merging.
   */
  function testJsSettingMerging() {
    $a['#attached'] = array(
      'js' => array(
        'foo.js' => array(),
        'bar.js' => array(),
      ),
172 173 174
      'drupalSettings' => [
        'foo' => ['d'],
      ],
175 176 177 178 179
    );
    $b['#attached'] = array(
      'js' => array(
        'baz.js' => array(),
      ),
180 181 182
      'drupalSettings' => [
        'bar' => ['a', 'b', 'c'],
      ],
183 184 185 186 187 188 189
    );
    $expected['#attached'] = array(
      'js' => array(
        'foo.js' => array(),
        'bar.js' => array(),
        'baz.js' => array(),
      ),
190 191 192 193
      'drupalSettings' => [
        'foo' => ['d'],
        'bar' => ['a', 'b', 'c'],
      ],
194 195 196 197 198 199 200 201 202 203 204
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($a['#attached'], $b['#attached']), 'Attachments merged correctly.');

    // Merging in the opposite direction yields the opposite JS setting asset
    // order.
    $expected['#attached'] = array(
      'js' => array(
        'baz.js' => array(),
        'foo.js' => array(),
        'bar.js' => array(),
      ),
205 206 207 208
      'drupalSettings' => [
        'bar' => ['a', 'b', 'c'],
        'foo' => ['d'],
      ],
209 210 211
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($b['#attached'], $a['#attached']), 'Attachments merged correctly; opposite merging yields opposite order.');

212 213
    // Merging with duplicates (simple case).
    $b['#attached']['drupalSettings']['foo'] = ['a', 'b', 'c'];
214 215 216 217 218 219
    $expected['#attached'] = array(
      'js' => array(
        'foo.js' => array(),
        'bar.js' => array(),
        'baz.js' => array(),
      ),
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      'drupalSettings' => [
        'foo' => ['a', 'b', 'c'],
        'bar' => ['a', 'b', 'c'],
      ],
    );
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($a['#attached'], $b['#attached']));

    // Merging with duplicates (simple case) in the opposite direction yields
    // the opposite JS setting asset order, but also opposite overriding order.
    $expected['#attached'] = array(
      'js' => array(
        'baz.js' => array(),
        'foo.js' => array(),
        'bar.js' => array(),
      ),
      'drupalSettings' => [
        'bar' => ['a', 'b', 'c'],
        'foo' => ['d', 'b', 'c'],
      ],
239
    );
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    $this->assertIdentical($expected['#attached'], drupal_merge_attached($b['#attached'], $a['#attached']));

    // Merging with duplicates: complex case.
    // Only the second of these two entries should appear in drupalSettings.
    $build = array();
    $build['a']['#attached']['drupalSettings']['commonTest'] = 'firstValue';
    $build['b']['#attached']['drupalSettings']['commonTest'] = 'secondValue';
    // Only the second of these entries should appear in drupalSettings.
    $build['a']['#attached']['drupalSettings']['commonTestJsArrayLiteral'] = ['firstValue'];
    $build['b']['#attached']['drupalSettings']['commonTestJsArrayLiteral'] = ['secondValue'];
    // Only the second of these two entries should appear in drupalSettings.
    $build['a']['#attached']['drupalSettings']['commonTestJsObjectLiteral'] = ['key' => 'firstValue'];
    $build['b']['#attached']['drupalSettings']['commonTestJsObjectLiteral'] = ['key' => 'secondValue'];
    // Real world test case: multiple elements in a render array are adding the
    // same (or nearly the same) JavaScript settings. When merged, they should
    // contain all settings and not duplicate some settings.
    $settings_one = array('moduleName' => array('ui' => array('button A', 'button B'), 'magical flag' => 3.14159265359));
    $build['a']['#attached']['drupalSettings']['commonTestRealWorldIdentical'] = $settings_one;
    $build['b']['#attached']['drupalSettings']['commonTestRealWorldIdentical'] = $settings_one;
    $settings_two_a = array('moduleName' => array('ui' => array('button A', 'button B', 'button C'), 'magical flag' => 3.14159265359, 'thingiesOnPage' => array('id1' => array())));
    $build['a']['#attached']['drupalSettings']['commonTestRealWorldAlmostIdentical'] = $settings_two_a;
    $settings_two_b = array('moduleName' => array('ui' => array('button D', 'button E'), 'magical flag' => 3.14, 'thingiesOnPage' => array('id2' => array())));
    $build['b']['#attached']['drupalSettings']['commonTestRealWorldAlmostIdentical'] = $settings_two_b;

    $merged = drupal_merge_attached($build['a']['#attached'], $build['b']['#attached']);

    // Test whether #attached can be used to override a previous setting.
    $this->assertIdentical('secondValue', $merged['drupalSettings']['commonTest']);

    // Test whether #attached can be used to add and override a JavaScript
    // array literal (an indexed PHP array) values.
    $this->assertIdentical('secondValue', $merged['drupalSettings']['commonTestJsArrayLiteral'][0]);

    // Test whether #attached can be used to add and override a JavaScript
    // object literal (an associate PHP array) values.
    $this->assertIdentical('secondValue', $merged['drupalSettings']['commonTestJsObjectLiteral']['key']);

    // Test whether the two real world cases are handled correctly: the first
    // adds the exact same settings twice and hence tests idempotency, the
    // second adds *almost* the same settings twice: the second time, some
    // values are altered, and some key-value pairs are added.
    $settings_two['moduleName']['thingiesOnPage']['id1'] = array();
    $this->assertIdentical($settings_one, $merged['drupalSettings']['commonTestRealWorldIdentical']);
    $expected_settings_two = $settings_two_a;
    $expected_settings_two['moduleName']['ui'][0] = 'button D';
    $expected_settings_two['moduleName']['ui'][1] = 'button E';
    $expected_settings_two['moduleName']['ui'][2] = 'button C';
    $expected_settings_two['moduleName']['magical flag'] = 3.14;
    $expected_settings_two['moduleName']['thingiesOnPage']['id2'] = [];
    $this->assertIdentical($expected_settings_two, $merged['drupalSettings']['commonTestRealWorldAlmostIdentical']);
290 291 292
  }

}