function NestedArrayTest::testMergeOutOfSequenceKeys

Same name and namespace 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 <a href="/api/drupal/core%21lib%21Drupal%21Component%21Utility%21NestedArray.php/class/NestedArray/9" title="Provides helpers to perform operations on nested arrays and array keys of variable depth." class="local">\Drupal\Component\Utility\NestedArray</a> @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.