function NestedArrayTest::testSetValue

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php \Drupal\Tests\Component\Utility\NestedArrayTest::testSetValue()
  2. 8.9.x core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php \Drupal\Tests\Component\Utility\NestedArrayTest::testSetValue()
  3. 10 core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php \Drupal\Tests\Component\Utility\NestedArrayTest::testSetValue()

Tests setting nested array values.

@covers ::setValue

File

core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php, line 80

Class

NestedArrayTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Component%21Utility%21NestedArray.php/class/NestedArray/11.x" 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 testSetValue() : void {
    $new_value = [
        '#value' => 'New value',
        '#required' => TRUE,
    ];
    // Verify setting the value of a nested element.
    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.');
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Cannot create key "child" on non-array value.');
    NestedArray::setValue($this->form, [
        'details',
        'element',
        '#value',
        'child',
    ], $new_value);
}

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