You are here

class FormStateValuesTraitTest in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Form/FormStateValuesTraitTest.php \Drupal\Tests\Core\Form\FormStateValuesTraitTest

@coversDefaultClass \Drupal\Core\Form\FormStateValuesTrait

@group Form

Hierarchy

Expanded class hierarchy of FormStateValuesTraitTest

File

core/tests/Drupal/Tests/Core/Form/FormStateValuesTraitTest.php, line 13

Namespace

Drupal\Tests\Core\Form
View source
class FormStateValuesTraitTest extends UnitTestCase {

  /**
   * Tests that setting the value for an element adds to the values.
   *
   * @covers ::setValueForElement
   */
  public function testSetValueForElement() {
    $element = [
      '#parents' => [
        'foo',
        'bar',
      ],
    ];
    $value = $this
      ->randomMachineName();
    $form_state = new FormStateValuesTraitStub();
    $form_state
      ->setValueForElement($element, $value);
    $expected = [
      'foo' => [
        'bar' => $value,
      ],
    ];
    $this
      ->assertSame($expected, $form_state
      ->getValues());
  }

  /**
   * @covers ::getValue
   *
   * @dataProvider providerGetValue
   */
  public function testGetValue($key, $expected, $default = NULL) {
    $form_state = (new FormStateValuesTraitStub())
      ->setValues([
      'foo' => 'one',
      'bar' => [
        'baz' => 'two',
      ],
    ]);
    $this
      ->assertSame($expected, $form_state
      ->getValue($key, $default));
  }

  /**
   * Provides data to self::testGetValue().
   *
   * @return array[]
   *   Items are arrays of two items:
   *   - The key for which to get the value (string)
   *   - The expected value (mixed).
   *   - The default value (mixed).
   */
  public function providerGetValue() {
    $data = [];
    $data[] = [
      'foo',
      'one',
    ];
    $data[] = [
      [
        'bar',
        'baz',
      ],
      'two',
    ];
    $data[] = [
      [
        'foo',
        'bar',
        'baz',
      ],
      NULL,
    ];
    $data[] = [
      'baz',
      'baz',
      'baz',
    ];
    $data[] = [
      NULL,
      [
        'foo' => 'one',
        'bar' => [
          'baz' => 'two',
        ],
      ],
    ];
    return $data;
  }

  /**
   * @covers ::getValue
   */
  public function testGetValueModifyReturn() {
    $initial_values = $values = [
      'foo' => 'one',
      'bar' => [
        'baz' => 'two',
      ],
    ];
    $form_state = (new FormStateValuesTraitStub())
      ->setValues($values);
    $value =& $form_state
      ->getValue(NULL);
    $this
      ->assertSame($initial_values, $value);
    $value = [
      'bing' => 'bang',
    ];
    $this
      ->assertSame([
      'bing' => 'bang',
    ], $form_state
      ->getValues());
    $this
      ->assertSame('bang', $form_state
      ->getValue('bing'));
    $this
      ->assertSame([
      'bing' => 'bang',
    ], $form_state
      ->getValue(NULL));
  }

  /**
   * @covers ::setValue
   *
   * @dataProvider providerSetValue
   */
  public function testSetValue($key, $value, $expected) {
    $form_state = (new FormStateValuesTraitStub())
      ->setValues([
      'bar' => 'wrong',
    ]);
    $form_state
      ->setValue($key, $value);
    $this
      ->assertSame($expected, $form_state
      ->getValues());
  }

  /**
   * Provides data to self::testSetValue().
   *
   * @return array[]
   *   Items are arrays of two items:
   *   - The key for which to set a new value (string)
   *   - The new value to set (mixed).
   *   - The expected form state values after setting the new value (mixed[]).
   */
  public function providerSetValue() {
    $data = [];
    $data[] = [
      'foo',
      'one',
      [
        'bar' => 'wrong',
        'foo' => 'one',
      ],
    ];
    $data[] = [
      [
        'bar',
        'baz',
      ],
      'two',
      [
        'bar' => [
          'baz' => 'two',
        ],
      ],
    ];
    $data[] = [
      [
        'foo',
        'bar',
        'baz',
      ],
      NULL,
      [
        'bar' => 'wrong',
        'foo' => [
          'bar' => [
            'baz' => NULL,
          ],
        ],
      ],
    ];
    return $data;
  }

  /**
   * @covers ::hasValue
   *
   * @dataProvider providerHasValue
   */
  public function testHasValue($key, $expected) {
    $form_state = (new FormStateValuesTraitStub())
      ->setValues([
      'foo' => 'one',
      'bar' => [
        'baz' => 'two',
      ],
      'true' => TRUE,
      'false' => FALSE,
      'null' => NULL,
    ]);
    $this
      ->assertSame($expected, $form_state
      ->hasValue($key));
  }

  /**
   * Provides data to self::testHasValue().
   *
   * @return array[]
   *   Items are arrays of two items:
   *   - The key to check for in the form state (string)
   *   - Whether the form state has an item with that key (bool).
   */
  public function providerHasValue() {
    $data = [];
    $data[] = [
      'foo',
      TRUE,
    ];
    $data[] = [
      [
        'bar',
        'baz',
      ],
      TRUE,
    ];
    $data[] = [
      [
        'foo',
        'bar',
        'baz',
      ],
      FALSE,
    ];
    $data[] = [
      'true',
      TRUE,
    ];
    $data[] = [
      'false',
      TRUE,
    ];
    $data[] = [
      'null',
      FALSE,
    ];
    return $data;
  }

  /**
   * @covers ::isValueEmpty
   *
   * @dataProvider providerIsValueEmpty
   */
  public function testIsValueEmpty($key, $expected) {
    $form_state = (new FormStateValuesTraitStub())
      ->setValues([
      'foo' => 'one',
      'bar' => [
        'baz' => 'two',
      ],
      'true' => TRUE,
      'false' => FALSE,
      'null' => NULL,
    ]);
    $this
      ->assertSame($expected, $form_state
      ->isValueEmpty($key));
  }

  /**
   * Provides data to self::testIsValueEmpty().
   *
   * @return array[]
   *   Items are arrays of two items:
   *   - The key to check for in the form state (string)
   *   - Whether the value is empty or not (bool).
   */
  public function providerIsValueEmpty() {
    $data = [];
    $data[] = [
      'foo',
      FALSE,
    ];
    $data[] = [
      [
        'bar',
        'baz',
      ],
      FALSE,
    ];
    $data[] = [
      [
        'foo',
        'bar',
        'baz',
      ],
      TRUE,
    ];
    $data[] = [
      'true',
      FALSE,
    ];
    $data[] = [
      'false',
      TRUE,
    ];
    $data[] = [
      'null',
      TRUE,
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormStateValuesTraitTest::providerGetValue public function Provides data to self::testGetValue().
FormStateValuesTraitTest::providerHasValue public function Provides data to self::testHasValue().
FormStateValuesTraitTest::providerIsValueEmpty public function Provides data to self::testIsValueEmpty().
FormStateValuesTraitTest::providerSetValue public function Provides data to self::testSetValue().
FormStateValuesTraitTest::testGetValue public function @covers ::getValue
FormStateValuesTraitTest::testGetValueModifyReturn public function @covers ::getValue
FormStateValuesTraitTest::testHasValue public function @covers ::hasValue
FormStateValuesTraitTest::testIsValueEmpty public function @covers ::isValueEmpty
FormStateValuesTraitTest::testSetValue public function @covers ::setValue
FormStateValuesTraitTest::testSetValueForElement public function Tests that setting the value for an element adds to the values.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed 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.
UnitTestCase::setUp protected function 308
UnitTestCase::setUpBeforeClass public static function