You are here

class NestedArrayTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php \Drupal\Tests\Component\Utility\NestedArrayTest

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

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase

Expanded class hierarchy of NestedArrayTest

File

core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php, line 17
Contains \Drupal\Tests\Component\Utility\NestedArrayTest.

Namespace

Drupal\Tests\Component\Utility
View source
class NestedArrayTest extends UnitTestCase {

  /**
   * Form array to check.
   *
   * @var array
   */
  protected $form;

  /**
   * Array of parents for the nested element.
   *
   * @var array
   */
  protected $parents;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Create a form structure with a nested element.
    $this->form['details']['element'] = array(
      '#value' => 'Nested element',
    );

    // Set up parent array.
    $this->parents = array(
      'details',
      'element',
    );
  }

  /**
   * Tests getting nested array values.
   *
   * @covers ::getValue
   */
  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.');
  }

  /**
   * Tests setting nested array values.
   *
   * @covers ::setValue
   */
  public function testSetValue() {
    $new_value = array(
      '#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.');
  }

  /**
   * Tests force-setting values.
   *
   * @covers ::setValue
   */
  public function testSetValueForce() {
    $new_value = array(
      'one',
    );
    $this->form['details']['non-array-parent'] = 'string';
    $parents = array(
      'details',
      'non-array-parent',
      'child',
    );
    NestedArray::setValue($this->form, $parents, $new_value, TRUE);
    $this
      ->assertSame($new_value, $this->form['details']['non-array-parent']['child'], 'The nested element was not forced to the new value.');
  }

  /**
   * Tests unsetting nested array values.
   *
   * @covers ::unsetValue
   */
  public function testUnsetValue() {

    // Verify unsetting a non-existing nested element throws no errors and the
    // non-existing key is properly reported.
    $key_existed = NULL;
    $parents = $this->parents;
    $parents[] = 'foo';
    NestedArray::unsetValue($this->form, $parents, $key_existed);
    $this
      ->assertTrue(isset($this->form['details']['element']['#value']), 'Outermost nested element key still exists.');
    $this
      ->assertFalse($key_existed, 'Non-existing key not found.');

    // Verify unsetting a nested element.
    $key_existed = NULL;
    NestedArray::unsetValue($this->form, $this->parents, $key_existed);
    $this
      ->assertFalse(isset($this->form['details']['element']), 'Removed nested element not found.');
    $this
      ->assertTrue($key_existed, 'Existing key was found.');
  }

  /**
   * Tests existence of array key.
   */
  public function testKeyExists() {

    // Verify that existing key is found.
    $this
      ->assertTrue(NestedArray::keyExists($this->form, $this->parents), 'Nested key found.');

    // Verify that non-existing keys are not found.
    $parents = $this->parents;
    $parents[] = 'foo';
    $this
      ->assertFalse(NestedArray::keyExists($this->form, $parents), 'Non-existing nested key not found.');
  }

  /**
   * Tests NestedArray::mergeDeepArray().
   *
   * @covers ::mergeDeep
   * @covers ::mergeDeepArray
   */
  public function testMergeDeepArray() {
    $link_options_1 = array(
      'fragment' => 'x',
      'attributes' => array(
        'title' => 'X',
        'class' => array(
          'a',
          'b',
        ),
      ),
      'language' => 'en',
    );
    $link_options_2 = array(
      'fragment' => 'y',
      'attributes' => array(
        'title' => 'Y',
        'class' => array(
          'c',
          'd',
        ),
      ),
      'absolute' => TRUE,
    );
    $expected = array(
      'fragment' => 'y',
      'attributes' => array(
        'title' => 'Y',
        'class' => array(
          'a',
          'b',
          'c',
          'd',
        ),
      ),
      'language' => 'en',
      'absolute' => TRUE,
    );
    $this
      ->assertSame($expected, NestedArray::mergeDeepArray(array(
      $link_options_1,
      $link_options_2,
    )), 'NestedArray::mergeDeepArray() returned a properly merged array.');

    // Test wrapper function, NestedArray::mergeDeep().
    $this
      ->assertSame($expected, NestedArray::mergeDeep($link_options_1, $link_options_2), 'NestedArray::mergeDeep() returned a properly merged array.');
  }

  /**
   * Tests that arrays with implicit keys are appended, not merged.
   *
   * @covers ::mergeDeepArray
   */
  public function testMergeImplicitKeys() {
    $a = array(
      'subkey' => array(
        'X',
        'Y',
      ),
    );
    $b = array(
      'subkey' => array(
        'X',
      ),
    );

    // Drupal core behavior.
    $expected = array(
      'subkey' => array(
        'X',
        'Y',
        'X',
      ),
    );
    $actual = NestedArray::mergeDeepArray(array(
      $a,
      $b,
    ));
    $this
      ->assertSame($expected, $actual, 'drupal_array_merge_deep() creates new numeric keys in the implicit sequence.');
  }

  /**
   * Tests that even with explicit keys, values are appended, not merged.
   *
   * @covers ::mergeDeepArray
   */
  public function testMergeExplicitKeys() {
    $a = array(
      'subkey' => array(
        0 => 'A',
        1 => 'B',
      ),
    );
    $b = array(
      'subkey' => array(
        0 => 'C',
        1 => 'D',
      ),
    );

    // Drupal core behavior.
    $expected = array(
      'subkey' => array(
        0 => 'A',
        1 => 'B',
        2 => 'C',
        3 => 'D',
      ),
    );
    $actual = NestedArray::mergeDeepArray(array(
      $a,
      $b,
    ));
    $this
      ->assertSame($expected, $actual, 'drupal_array_merge_deep() creates new numeric keys in the explicit sequence.');
  }

  /**
   * Tests that array keys values on the first array are ignored when merging.
   *
   * Even if the initial ordering would place the data from the second array
   * before those in the first one, they are still appended, and the keys on
   * the first array are deleted and regenerated.
   *
   * @covers ::mergeDeepArray
   */
  public function testMergeOutOfSequenceKeys() {
    $a = array(
      'subkey' => array(
        10 => 'A',
        30 => 'B',
      ),
    );
    $b = array(
      'subkey' => array(
        20 => 'C',
        0 => 'D',
      ),
    );

    // Drupal core behavior.
    $expected = array(
      'subkey' => array(
        0 => 'A',
        1 => 'B',
        2 => 'C',
        3 => 'D',
      ),
    );
    $actual = NestedArray::mergeDeepArray(array(
      $a,
      $b,
    ));
    $this
      ->assertSame($expected, $actual, 'drupal_array_merge_deep() ignores numeric key order when merging.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NestedArrayTest::$form protected property Form array to check.
NestedArrayTest::$parents protected property Array of parents for the nested element.
NestedArrayTest::setUp protected function Overrides UnitTestCase::setUp
NestedArrayTest::testGetValue public function Tests getting nested array values.
NestedArrayTest::testKeyExists public function Tests existence of array key.
NestedArrayTest::testMergeDeepArray public function Tests NestedArray::mergeDeepArray().
NestedArrayTest::testMergeExplicitKeys public function Tests that even with explicit keys, values are appended, not merged.
NestedArrayTest::testMergeImplicitKeys public function Tests that arrays with implicit keys are appended, not merged.
NestedArrayTest::testMergeOutOfSequenceKeys public function Tests that array keys values on the first array are ignored when merging.
NestedArrayTest::testSetValue public function Tests setting nested array values.
NestedArrayTest::testSetValueForce public function Tests force-setting values.
NestedArrayTest::testUnsetValue public function Tests unsetting nested array values.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.