View source
<?php
namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\NestedArray;
use PHPUnit\Framework\TestCase;
class NestedArrayTest extends TestCase {
protected $form;
protected $parents;
protected function setUp() : void {
parent::setUp();
$this->form['details']['element'] = [
'#value' => 'Nested element',
];
$this->parents = [
'details',
'element',
];
}
public function testGetValue() {
$value = NestedArray::getValue($this->form, $this->parents);
$this
->assertSame('Nested element', $value['#value'], 'Nested element value found.');
$value =& NestedArray::getValue($this->form, $this->parents);
$value['#value'] = 'New value';
$value = NestedArray::getValue($this->form, $this->parents);
$this
->assertSame('New value', $value['#value'], 'Nested element value was changed by reference.');
$this
->assertSame('New value', $this->form['details']['element']['#value'], 'Nested element value was changed by reference.');
$key_exists = NULL;
NestedArray::getValue($this->form, $this->parents, $key_exists);
$this
->assertTrue($key_exists, 'Existing key found.');
$key_exists = NULL;
$parents = $this->parents;
$parents[] = 'foo';
NestedArray::getValue($this->form, $parents, $key_exists);
$this
->assertFalse($key_exists, 'Non-existing key not found.');
}
public function testSetValue() {
$new_value = [
'#value' => 'New value',
'#required' => TRUE,
];
NestedArray::setValue($this->form, $this->parents, $new_value);
$this
->assertSame('New value', $this->form['details']['element']['#value'], 'Changed nested element value found.');
$this
->assertTrue($this->form['details']['element']['#required'], 'New nested element value found.');
}
public function testSetValueForce() {
$new_value = [
'one',
];
$this->form['details']['non-array-parent'] = 'string';
$parents = [
'details',
'non-array-parent',
'child',
];
NestedArray::setValue($this->form, $parents, $new_value, TRUE);
$this
->assertSame($new_value, $this->form['details']['non-array-parent']['child'], 'The nested element was not forced to the new value.');
}
public function testUnsetValue() {
$key_existed = NULL;
$parents = $this->parents;
$parents[] = 'foo';
NestedArray::unsetValue($this->form, $parents, $key_existed);
$this
->assertTrue(isset($this->form['details']['element']['#value']), 'Outermost nested element key still exists.');
$this
->assertFalse($key_existed, 'Non-existing key not found.');
$key_existed = NULL;
NestedArray::unsetValue($this->form, $this->parents, $key_existed);
$this
->assertFalse(isset($this->form['details']['element']), 'Removed nested element not found.');
$this
->assertTrue($key_existed, 'Existing key was found.');
}
public function testKeyExists() {
$this
->assertTrue(NestedArray::keyExists($this->form, $this->parents), 'Nested key found.');
$parents = $this->parents;
$parents[] = 'foo';
$this
->assertFalse(NestedArray::keyExists($this->form, $parents), 'Non-existing nested key not found.');
}
public function testMergeDeepArray() {
$link_options_1 = [
'fragment' => 'x',
'attributes' => [
'title' => 'X',
'class' => [
'a',
'b',
],
],
'language' => 'en',
];
$link_options_2 = [
'fragment' => 'y',
'attributes' => [
'title' => 'Y',
'class' => [
'c',
'd',
],
],
'absolute' => TRUE,
];
$expected = [
'fragment' => 'y',
'attributes' => [
'title' => 'Y',
'class' => [
'a',
'b',
'c',
'd',
],
],
'language' => 'en',
'absolute' => TRUE,
];
$this
->assertSame($expected, NestedArray::mergeDeepArray([
$link_options_1,
$link_options_2,
]), 'NestedArray::mergeDeepArray() returned a properly merged array.');
$this
->assertSame($expected, NestedArray::mergeDeep($link_options_1, $link_options_2), 'NestedArray::mergeDeep() returned a properly merged array.');
}
public function testMergeImplicitKeys() {
$a = [
'subkey' => [
'X',
'Y',
],
];
$b = [
'subkey' => [
'X',
],
];
$expected = [
'subkey' => [
'X',
'Y',
'X',
],
];
$actual = NestedArray::mergeDeepArray([
$a,
$b,
]);
$this
->assertSame($expected, $actual, 'drupal_array_merge_deep() creates new numeric keys in the implicit sequence.');
}
public function testMergeExplicitKeys() {
$a = [
'subkey' => [
0 => 'A',
1 => 'B',
],
];
$b = [
'subkey' => [
0 => 'C',
1 => 'D',
],
];
$expected = [
'subkey' => [
0 => 'A',
1 => 'B',
2 => 'C',
3 => 'D',
],
];
$actual = NestedArray::mergeDeepArray([
$a,
$b,
]);
$this
->assertSame($expected, $actual, 'drupal_array_merge_deep() creates new numeric keys in the explicit sequence.');
}
public function testMergeOutOfSequenceKeys() {
$a = [
'subkey' => [
10 => 'A',
30 => 'B',
],
];
$b = [
'subkey' => [
20 => 'C',
0 => 'D',
],
];
$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.');
}
public function testFilter($array, $callable, $expected) {
$this
->assertEquals($expected, NestedArray::filter($array, $callable));
}
public function providerTestFilter() {
$data = [];
$data['1d-array'] = [
[
0,
1,
'',
TRUE,
],
NULL,
[
1 => 1,
3 => TRUE,
],
];
$data['1d-array-callable'] = [
[
0,
1,
'',
TRUE,
],
function ($element) {
return $element === '';
},
[
2 => '',
],
];
$data['2d-array'] = [
[
[
0,
1,
'',
TRUE,
],
[
0,
1,
2,
3,
],
],
NULL,
[
0 => [
1 => 1,
3 => TRUE,
],
1 => [
1 => 1,
2 => 2,
3 => 3,
],
],
];
$data['2d-array-callable'] = [
[
[
0,
1,
'',
TRUE,
],
[
0,
1,
2,
3,
],
],
function ($element) {
return is_array($element) || $element === 3;
},
[
0 => [],
1 => [
3 => 3,
],
],
];
return $data;
}
}