You are here

public function NestedArrayTest::testMergeOutOfSequenceKeys in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php \Drupal\Tests\Component\Utility\NestedArrayTest::testMergeOutOfSequenceKeys()
  2. 9 core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php \Drupal\Tests\Component\Utility\NestedArrayTest::testMergeOutOfSequenceKeys()

Tests that array keys values on the first array are ignored when merging.

Even if the initial ordering would place the data from the second array before those in the first one, they are still appended, and the keys on the first array are deleted and regenerated.

@covers ::mergeDeepArray

File

core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php, line 230

Class

NestedArrayTest
@coversDefaultClass \Drupal\Component\Utility\NestedArray @group Utility

Namespace

Drupal\Tests\Component\Utility

Code

public function testMergeOutOfSequenceKeys() {
  $a = [
    'subkey' => [
      10 => 'A',
      30 => 'B',
    ],
  ];
  $b = [
    'subkey' => [
      20 => 'C',
      0 => 'D',
    ],
  ];

  // Drupal core behavior.
  $expected = [
    'subkey' => [
      0 => 'A',
      1 => 'B',
      2 => 'C',
      3 => 'D',
    ],
  ];
  $actual = NestedArray::mergeDeepArray([
    $a,
    $b,
  ]);
  $this
    ->assertSame($expected, $actual, 'drupal_array_merge_deep() ignores numeric key order when merging.');
}