function NestedArrayTest::testGetValue

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

Tests getting nested array values.

@covers ::getValue

File

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

Class

NestedArrayTest
@coversDefaultClass \Drupal\Component\Utility\NestedArray @group Utility

Namespace

Drupal\Tests\Component\Utility

Code

public function testGetValue() : void {
    // Verify getting a value of a nested element.
    $value = NestedArray::getValue($this->form, $this->parents);
    $this->assertSame('Nested element', $value['#value'], 'Nested element value found.');
    // Verify changing a value of a nested element by reference.
    $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.');
    // Verify that an existing key is reported back.
    $key_exists = NULL;
    NestedArray::getValue($this->form, $this->parents, $key_exists);
    $this->assertTrue($key_exists, 'Existing key found.');
    // Verify that a non-existing key is reported back and throws no errors.
    $key_exists = NULL;
    $parents = $this->parents;
    $parents[] = 'foo';
    NestedArray::getValue($this->form, $parents, $key_exists);
    $this->assertFalse($key_exists, 'Non-existing key not found.');
}

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