You are here

public function NestedArrayTest::testGetValue in Drupal 9

Same name and namespace in other branches
  1. 8 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 48

Class

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

Namespace

Drupal\Tests\Component\Utility

Code

public function testGetValue() {

  // 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.');
}