View source
<?php
namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\NestedArray;
use Drupal\Tests\UnitTestCase;
class NestedArrayTest extends UnitTestCase {
protected $form;
protected $parents;
protected function setUp() {
parent::setUp();
$this->form['details']['element'] = array(
'#value' => 'Nested element',
);
$this->parents = array(
'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 = array(
'#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 = array(
'one',
);
$this->form['details']['non-array-parent'] = 'string';
$parents = array(
'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 = array(
'fragment' => 'x',
'attributes' => array(
'title' => 'X',
'class' => array(
'a',
'b',
),
),
'language' => 'en',
);
$link_options_2 = array(
'fragment' => 'y',
'attributes' => array(
'title' => 'Y',
'class' => array(
'c',
'd',
),
),
'absolute' => TRUE,
);
$expected = array(
'fragment' => 'y',
'attributes' => array(
'title' => 'Y',
'class' => array(
'a',
'b',
'c',
'd',
),
),
'language' => 'en',
'absolute' => TRUE,
);
$this
->assertSame($expected, NestedArray::mergeDeepArray(array(
$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 = array(
'subkey' => array(
'X',
'Y',
),
);
$b = array(
'subkey' => array(
'X',
),
);
$expected = array(
'subkey' => array(
'X',
'Y',
'X',
),
);
$actual = NestedArray::mergeDeepArray(array(
$a,
$b,
));
$this
->assertSame($expected, $actual, 'drupal_array_merge_deep() creates new numeric keys in the implicit sequence.');
}
public function testMergeExplicitKeys() {
$a = array(
'subkey' => array(
0 => 'A',
1 => 'B',
),
);
$b = array(
'subkey' => array(
0 => 'C',
1 => 'D',
),
);
$expected = array(
'subkey' => array(
0 => 'A',
1 => 'B',
2 => 'C',
3 => 'D',
),
);
$actual = NestedArray::mergeDeepArray(array(
$a,
$b,
));
$this
->assertSame($expected, $actual, 'drupal_array_merge_deep() creates new numeric keys in the explicit sequence.');
}
public function testMergeOutOfSequenceKeys() {
$a = array(
'subkey' => array(
10 => 'A',
30 => 'B',
),
);
$b = array(
'subkey' => array(
20 => 'C',
0 => 'D',
),
);
$expected = array(
'subkey' => array(
0 => 'A',
1 => 'B',
2 => 'C',
3 => 'D',
),
);
$actual = NestedArray::mergeDeepArray(array(
$a,
$b,
));
$this
->assertSame($expected, $actual, 'drupal_array_merge_deep() ignores numeric key order when merging.');
}
}