3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\NestedArray;
6 use PHPUnit\Framework\TestCase;
9 * @coversDefaultClass \Drupal\Component\Utility\NestedArray
12 class NestedArrayTest extends TestCase {
15 * Form array to check.
22 * Array of parents for the nested element.
31 protected function setUp() {
34 // Create a form structure with a nested element.
35 $this->form['details']['element'] = [
36 '#value' => 'Nested element',
39 // Set up parent array.
40 $this->parents = ['details', 'element'];
44 * Tests getting nested array values.
48 public function testGetValue() {
49 // Verify getting a value of a nested element.
50 $value = NestedArray::getValue($this->form, $this->parents);
51 $this->assertSame('Nested element', $value['#value'], 'Nested element value found.');
53 // Verify changing a value of a nested element by reference.
54 $value = &NestedArray::getValue($this->form, $this->parents);
55 $value['#value'] = 'New value';
56 $value = NestedArray::getValue($this->form, $this->parents);
57 $this->assertSame('New value', $value['#value'], 'Nested element value was changed by reference.');
58 $this->assertSame('New value', $this->form['details']['element']['#value'], 'Nested element value was changed by reference.');
60 // Verify that an existing key is reported back.
62 NestedArray::getValue($this->form, $this->parents, $key_exists);
63 $this->assertTrue($key_exists, 'Existing key found.');
65 // Verify that a non-existing key is reported back and throws no errors.
67 $parents = $this->parents;
69 NestedArray::getValue($this->form, $parents, $key_exists);
70 $this->assertFalse($key_exists, 'Non-existing key not found.');
74 * Tests setting nested array values.
78 public function testSetValue() {
80 '#value' => 'New value',
84 // Verify setting the value of a nested element.
85 NestedArray::setValue($this->form, $this->parents, $new_value);
86 $this->assertSame('New value', $this->form['details']['element']['#value'], 'Changed nested element value found.');
87 $this->assertTrue($this->form['details']['element']['#required'], 'New nested element value found.');
91 * Tests force-setting values.
95 public function testSetValueForce() {
99 $this->form['details']['non-array-parent'] = 'string';
100 $parents = ['details', 'non-array-parent', 'child'];
101 NestedArray::setValue($this->form, $parents, $new_value, TRUE);
102 $this->assertSame($new_value, $this->form['details']['non-array-parent']['child'], 'The nested element was not forced to the new value.');
106 * Tests unsetting nested array values.
108 * @covers ::unsetValue
110 public function testUnsetValue() {
111 // Verify unsetting a non-existing nested element throws no errors and the
112 // non-existing key is properly reported.
114 $parents = $this->parents;
116 NestedArray::unsetValue($this->form, $parents, $key_existed);
117 $this->assertTrue(isset($this->form['details']['element']['#value']), 'Outermost nested element key still exists.');
118 $this->assertFalse($key_existed, 'Non-existing key not found.');
120 // Verify unsetting a nested element.
122 NestedArray::unsetValue($this->form, $this->parents, $key_existed);
123 $this->assertFalse(isset($this->form['details']['element']), 'Removed nested element not found.');
124 $this->assertTrue($key_existed, 'Existing key was found.');
128 * Tests existence of array key.
130 public function testKeyExists() {
131 // Verify that existing key is found.
132 $this->assertTrue(NestedArray::keyExists($this->form, $this->parents), 'Nested key found.');
134 // Verify that non-existing keys are not found.
135 $parents = $this->parents;
137 $this->assertFalse(NestedArray::keyExists($this->form, $parents), 'Non-existing nested key not found.');
141 * Tests NestedArray::mergeDeepArray().
143 * @covers ::mergeDeep
144 * @covers ::mergeDeepArray
146 public function testMergeDeepArray() {
149 'attributes' => ['title' => 'X', 'class' => ['a', 'b']],
154 'attributes' => ['title' => 'Y', 'class' => ['c', 'd']],
159 'attributes' => ['title' => 'Y', 'class' => ['a', 'b', 'c', 'd']],
163 $this->assertSame($expected, NestedArray::mergeDeepArray([$link_options_1, $link_options_2]), 'NestedArray::mergeDeepArray() returned a properly merged array.');
164 // Test wrapper function, NestedArray::mergeDeep().
165 $this->assertSame($expected, NestedArray::mergeDeep($link_options_1, $link_options_2), 'NestedArray::mergeDeep() returned a properly merged array.');
169 * Tests that arrays with implicit keys are appended, not merged.
171 * @covers ::mergeDeepArray
173 public function testMergeImplicitKeys() {
175 'subkey' => ['X', 'Y'],
181 // Drupal core behavior.
183 'subkey' => ['X', 'Y', 'X'],
185 $actual = NestedArray::mergeDeepArray([$a, $b]);
186 $this->assertSame($expected, $actual, 'drupal_array_merge_deep() creates new numeric keys in the implicit sequence.');
190 * Tests that even with explicit keys, values are appended, not merged.
192 * @covers ::mergeDeepArray
194 public function testMergeExplicitKeys() {
208 // Drupal core behavior.
217 $actual = NestedArray::mergeDeepArray([$a, $b]);
218 $this->assertSame($expected, $actual, 'drupal_array_merge_deep() creates new numeric keys in the explicit sequence.');
222 * Tests that array keys values on the first array are ignored when merging.
224 * Even if the initial ordering would place the data from the second array
225 * before those in the first one, they are still appended, and the keys on
226 * the first array are deleted and regenerated.
228 * @covers ::mergeDeepArray
230 public function testMergeOutOfSequenceKeys() {
244 // Drupal core behavior.
253 $actual = NestedArray::mergeDeepArray([$a, $b]);
254 $this->assertSame($expected, $actual, 'drupal_array_merge_deep() ignores numeric key order when merging.');
259 * @dataProvider providerTestFilter
261 public function testFilter($array, $callable, $expected) {
262 $this->assertEquals($expected, NestedArray::filter($array, $callable));
265 public function providerTestFilter() {
267 $data['1d-array'] = [
268 [0, 1, '', TRUE], NULL, [1 => 1, 3 => TRUE],
270 $data['1d-array-callable'] = [
272 function ($element) {
273 return $element === '';
277 $data['2d-array'] = [
278 [[0, 1, '', TRUE], [0, 1, 2, 3]], NULL, [0 => [1 => 1, 3 => TRUE], 1 => [1 => 1, 2 => 2, 3 => 3]],
280 $data['2d-array-callable'] = [
281 [[0, 1, '', TRUE], [0, 1, 2, 3]],
282 function ($element) {
283 return is_array($element) || $element === 3;
285 [0 => [], 1 => [3 => 3]],