class SubformStateTest in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Form/SubformStateTest.php \Drupal\Tests\Core\Form\SubformStateTest
@coversDefaultClass \Drupal\Core\Form\SubformState
@group Form
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\Form\SubformStateTest
Expanded class hierarchy of SubformStateTest
File
- core/
tests/ Drupal/ Tests/ Core/ Form/ SubformStateTest.php, line 17
Namespace
Drupal\Tests\Core\FormView source
class SubformStateTest extends UnitTestCase {
/**
* The form state's values test fixture.
*
* @var mixed[]
*/
protected $formStateValues = [
'foo' => 'bar',
'dog' => [
'breed' => 'Pit bull',
'name' => 'Dodger',
],
];
/**
* The parent form.
*
* @var mixed[]
*/
protected $parentForm = [
'#parents' => [],
'foo' => [
'#parents' => [
'foo',
],
'#array_parents' => [
'foo',
],
],
'dog' => [
'#parents' => [
'dog',
],
'#array_parents' => [
'dog',
],
'breed' => [
'#parents' => [
'dog',
'breed',
],
'#array_parents' => [
'dog',
'breed',
],
],
'name' => [
'#parents' => [
'dog',
'name',
],
'#array_parents' => [
'dog',
'name',
],
],
],
];
/**
* @covers ::getValues
* @covers ::getParents
*
* @dataProvider providerGetValues
*
* @param string[] $parents
* @param string $expected
*/
public function testGetValues(array $parents, $expected) {
$parent_form_state = new FormState();
$parent_form_state
->setValues($this->formStateValues);
$subform = NestedArray::getValue($this->parentForm, $parents);
$subform_state = SubformState::createForSubform($subform, $this->parentForm, $parent_form_state);
$subform_state_values =& $subform_state
->getValues();
$this
->assertSame($expected, $subform_state_values);
// Modify the retrieved values and confirm they are modified by reference in
// the parent form state.
$subform_state_values['fish'] = 'Jim';
$this
->assertSame($subform_state_values, $subform_state
->getValues());
}
/**
* Provides data to self::testGetValues().
*/
public function providerGetValues() {
$data = [];
$data['exist'] = [
[
'dog',
],
$this->formStateValues['dog'],
];
return $data;
}
/**
* @covers ::getValues
* @covers ::getParents
*
* @dataProvider providerGetValuesBroken
*
* @param string[] $parents
* @param string $expected
*/
public function testGetValuesBroken(array $parents, $expected) {
$this
->expectException(\UnexpectedValueException::class);
$this
->testGetValues($parents, $expected);
}
/**
* Provides data to self::testGetValuesBroken().
*/
public function providerGetValuesBroken() {
$data = [];
$data['exist'] = [
[
'foo',
],
$this->formStateValues['foo'],
];
$data['nested'] = [
[
'dog',
'name',
],
'Dodger',
];
return $data;
}
/**
* @covers ::getValue
*
* @dataProvider providerTestGetValue
*/
public function testGetValue($parents, $key, $expected, $default = NULL) {
$parent_form_state = new FormState();
$parent_form_state
->setValues($this->formStateValues);
$subform = NestedArray::getValue($this->parentForm, $parents);
$subform_state = SubformState::createForSubform($subform, $this->parentForm, $parent_form_state);
$subform_state_value =& $subform_state
->getValue($key, $default);
$this
->assertSame($expected, $subform_state_value);
// Modify the retrieved values and confirm they are modified by reference in
// the parent form state.
$subform_state_value = 'Jim';
$this
->assertSame($subform_state_value, $subform_state
->getValue($key));
}
/**
* Provides data to self::testGetValue().
*/
public function providerTestGetValue() {
$data = [];
$data['exist'] = [
[
'dog',
],
'name',
'Dodger',
];
return $data;
}
/**
* @covers ::getValue
*
* @dataProvider providerTestGetValueBroken
*/
public function testGetValueBroken(array $parents, $key, $expected, $default = NULL) {
$this
->expectException(\UnexpectedValueException::class);
$this
->testGetValue($parents, $key, $expected, $default);
}
/**
* Provides data to self::testGetValueBroken().
*/
public function providerTestGetValueBroken() {
$data = [];
$data['nested'] = [
[
'dog',
'name',
],
NULL,
'Dodger',
];
return $data;
}
/**
* @covers ::setValues
*
* @dataProvider providerTestSetValues
*/
public function testSetValues($parents, $new_values, $expected) {
$parent_form_state = new FormState();
$parent_form_state
->setValues($this->formStateValues);
$subform = NestedArray::getValue($this->parentForm, $parents);
$subform_state = SubformState::createForSubform($subform, $this->parentForm, $parent_form_state);
$this
->assertSame($subform_state, $subform_state
->setValues($new_values));
$this
->assertSame($expected, $parent_form_state
->getValues());
}
/**
* Provides data to self::testSetValues().
*/
public function providerTestSetValues() {
$data = [];
$data['exist'] = [
[
'dog',
],
[],
[
'foo' => 'bar',
'dog' => [],
],
];
return $data;
}
/**
* @covers ::setValues
*
* @dataProvider providerTestSetValuesBroken
*/
public function testSetValuesBroken($parents, $new_values, $expected) {
$this
->expectException(\UnexpectedValueException::class);
$this
->testSetValues($parents, $new_values, $expected);
}
/**
* Provides data to self::testSetValuesBroken().
*/
public function providerTestSetValuesBroken() {
$data = [];
$data['exist'] = [
[
'foo',
],
[],
[
'foo' => [],
'dog' => $this->formStateValues['dog'],
],
];
return $data;
}
/**
* @covers ::getCompleteFormState
*/
public function testGetCompleteFormStateWithParentCompleteForm() {
$parent_form_state = $this
->prophesize(FormStateInterface::class);
$subform_state = SubformState::createForSubform($this->parentForm['dog'], $this->parentForm, $parent_form_state
->reveal());
$this
->assertSame($parent_form_state
->reveal(), $subform_state
->getCompleteFormState());
}
/**
* @covers ::getCompleteFormState
*/
public function testGetCompleteFormStateWithParentSubform() {
$complete_form_state = $this
->prophesize(FormStateInterface::class);
$parent_form_state = $this
->prophesize(SubformStateInterface::class);
$parent_form_state
->getCompleteFormState()
->willReturn($complete_form_state
->reveal())
->shouldBeCalled();
$subform_state = SubformState::createForSubform($this->parentForm['dog'], $this->parentForm, $parent_form_state
->reveal());
$this
->assertSame($complete_form_state
->reveal(), $subform_state
->getCompleteFormState());
}
/**
* @covers ::setLimitValidationErrors
*/
public function testSetLimitValidationErrors() {
$parent_limit_validation_errors = [
'dog',
'name',
];
$limit_validation_errors = [
'name',
];
$parent_form_state = $this
->prophesize(FormStateInterface::class);
$parent_form_state
->setLimitValidationErrors($parent_limit_validation_errors)
->shouldBeCalled();
$subform_state = SubformState::createForSubform($this->parentForm['dog'], $this->parentForm, $parent_form_state
->reveal());
$this
->assertSame($subform_state, $subform_state
->setLimitValidationErrors($limit_validation_errors));
}
/**
* @covers ::getLimitValidationErrors
*/
public function testGetLimitValidationErrors() {
$parent_limit_validation_errors = [
'dog',
'name',
];
$limit_validation_errors = [
'name',
];
$parent_form_state = $this
->prophesize(FormStateInterface::class);
$parent_form_state
->getLimitValidationErrors()
->willReturn($parent_limit_validation_errors)
->shouldBeCalled();
$subform_state = SubformState::createForSubform($this->parentForm['dog'], $this->parentForm, $parent_form_state
->reveal());
$this
->assertSame($limit_validation_errors, $subform_state
->getLimitValidationErrors());
}
/**
* @covers ::setErrorByName
*/
public function testSetErrorByName() {
$parent_form_error_name = 'dog][name';
$subform_error_name = 'name';
// cSpell:disable-next-line
$message = 'De kat krabt de krullen van de trap.';
$parent_form_state = $this
->prophesize(FormStateInterface::class);
$parent_form_state
->setErrorByName($parent_form_error_name, $message)
->shouldBeCalled();
$subform_state = SubformState::createForSubform($this->parentForm['dog'], $this->parentForm, $parent_form_state
->reveal());
$this
->assertSame($subform_state, $subform_state
->setErrorByName($subform_error_name, $message));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
SubformStateTest:: |
protected | property | The form state's values test fixture. | |
SubformStateTest:: |
protected | property | The parent form. | |
SubformStateTest:: |
public | function | Provides data to self::testGetValues(). | |
SubformStateTest:: |
public | function | Provides data to self::testGetValuesBroken(). | |
SubformStateTest:: |
public | function | Provides data to self::testGetValue(). | |
SubformStateTest:: |
public | function | Provides data to self::testGetValueBroken(). | |
SubformStateTest:: |
public | function | Provides data to self::testSetValues(). | |
SubformStateTest:: |
public | function | Provides data to self::testSetValuesBroken(). | |
SubformStateTest:: |
public | function | @covers ::getCompleteFormState | |
SubformStateTest:: |
public | function | @covers ::getCompleteFormState | |
SubformStateTest:: |
public | function | @covers ::getLimitValidationErrors | |
SubformStateTest:: |
public | function | @covers ::getValue | |
SubformStateTest:: |
public | function | @covers ::getValue | |
SubformStateTest:: |
public | function | @covers ::getValues @covers ::getParents | |
SubformStateTest:: |
public | function | @covers ::getValues @covers ::getParents | |
SubformStateTest:: |
public | function | @covers ::setErrorByName | |
SubformStateTest:: |
public | function | @covers ::setLimitValidationErrors | |
SubformStateTest:: |
public | function | @covers ::setValues | |
SubformStateTest:: |
public | function | @covers ::setValues | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 308 | |
UnitTestCase:: |
public static | function |