function NestedArrayTest::testMergeOutOfSequenceKeys

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php \Drupal\Tests\Component\Utility\NestedArrayTest::testMergeOutOfSequenceKeys()
  2. 10 core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php \Drupal\Tests\Component\Utility\NestedArrayTest::testMergeOutOfSequenceKeys()
  3. 11.x 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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.