function NestedArrayUnitTest::testGetValue

Tests getting nested array values.

File

drupal/core/tests/Drupal/Tests/Core/NestedArrayUnitTest.php, line 53
Contains \Drupal\Core\NestedArrayUnitTest.

Class

NestedArrayUnitTest
Tests the NestedArray helper class.

Namespace

Drupal\Tests\Core

Code

function testGetValue() {

  // Verify getting a value of a nested element.
  $value = NestedArray::getValue($this->form, $this->parents);
  $this
    ->assertEquals($value['#value'], 'Nested element', '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
    ->assertEquals($value['#value'], 'New value', 'Nested element value was changed by reference.');
  $this
    ->assertEquals($this->form['details']['element']['#value'], 'New 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
    ->assertSame($key_exists, TRUE, '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
    ->assertSame($key_exists, FALSE, 'Non-existing key not found.');
}