class SubformStateTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Form/SubformStateTest.php \Drupal\Tests\Core\Form\SubformStateTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Form/SubformStateTest.php \Drupal\Tests\Core\Form\SubformStateTest
  3. 11.x core/tests/Drupal/Tests/Core/Form/SubformStateTest.php \Drupal\Tests\Core\Form\SubformStateTest

@coversDefaultClass \Drupal\Core\Form\SubformState

@group Form

Hierarchy

Expanded class hierarchy of SubformStateTest

File

core/tests/Drupal/Tests/Core/Form/SubformStateTest.php, line 20

Namespace

Drupal\Tests\Core\Form
View source
class SubformStateTest extends UnitTestCase {
    
    /**
     * The form state's values test fixture.
     *
     * @var mixed[]
     */
    protected static $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
     *   The parents.
     * @param string $expected
     *   The expected state values.
     */
    public function testGetValues(array $parents, $expected) : void {
        $parent_form_state = new FormState();
        $parent_form_state->setValues(static::$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 static function providerGetValues() : array {
        $data = [];
        $data['exist'] = [
            [
                'dog',
            ],
            static::$formStateValues['dog'],
        ];
        return $data;
    }
    
    /**
     * @covers ::getValues
     * @covers ::getParents
     *
     * @dataProvider providerGetValuesBroken
     *
     * @param string[] $parents
     *   The parents.
     * @param string $expected
     *   The expected state values.
     */
    public function testGetValuesBroken(array $parents, $expected) : void {
        $this->expectException(\UnexpectedValueException::class);
        $this->testGetValues($parents, $expected);
    }
    
    /**
     * Provides data to self::testGetValuesBroken().
     */
    public static function providerGetValuesBroken() : array {
        $data = [];
        $data['exist'] = [
            [
                'foo',
            ],
            static::$formStateValues['foo'],
        ];
        $data['nested'] = [
            [
                'dog',
                'name',
            ],
            'Dodger',
        ];
        return $data;
    }
    
    /**
     * @covers ::getValue
     *
     * @dataProvider providerTestGetValue
     */
    public function testGetValue($parents, $key, $expected, $default = NULL) : void {
        $parent_form_state = new FormState();
        $parent_form_state->setValues(static::$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 static function providerTestGetValue() {
        $data = [];
        $data['exist'] = [
            [
                'dog',
            ],
            'name',
            'Dodger',
        ];
        return $data;
    }
    
    /**
     * @covers ::getValue
     *
     * @dataProvider providerTestGetValueBroken
     */
    public function testGetValueBroken(array $parents, $key, $expected, $default = NULL) : void {
        $this->expectException(\UnexpectedValueException::class);
        $this->testGetValue($parents, $key, $expected, $default);
    }
    
    /**
     * Provides data to self::testGetValueBroken().
     */
    public static function providerTestGetValueBroken() {
        $data = [];
        $data['nested'] = [
            [
                'dog',
                'name',
            ],
            NULL,
            'Dodger',
        ];
        return $data;
    }
    
    /**
     * @covers ::setValues
     *
     * @dataProvider providerTestSetValues
     */
    public function testSetValues($parents, $new_values, $expected) : void {
        $parent_form_state = new FormState();
        $parent_form_state->setValues(static::$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 static function providerTestSetValues() {
        $data = [];
        $data['exist'] = [
            [
                'dog',
            ],
            [],
            [
                'foo' => 'bar',
                'dog' => [],
            ],
        ];
        return $data;
    }
    
    /**
     * @covers ::setValues
     *
     * @dataProvider providerTestSetValuesBroken
     */
    public function testSetValuesBroken($parents, $new_values, $expected) : void {
        $this->expectException(\UnexpectedValueException::class);
        $this->testSetValues($parents, $new_values, $expected);
    }
    
    /**
     * Provides data to self::testSetValuesBroken().
     */
    public static function providerTestSetValuesBroken() : array {
        $data = [];
        $data['exist'] = [
            [
                'foo',
            ],
            [],
            [
                'foo' => [],
                'dog' => static::$formStateValues['dog'],
            ],
        ];
        return $data;
    }
    
    /**
     * @covers ::getCompleteFormState
     */
    public function testGetCompleteFormStateWithParentCompleteForm() : void {
        $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() : void {
        $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() : void {
        $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() : void {
        $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() : void {
        $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));
    }
    
    /**
     * @covers ::getFormObject
     */
    public function testFormObject() : void {
        $parent_form_state = $this->prophesize(FormStateInterface::class);
        $parent_form_object = $this->prophesize(FormInterface::class)
            ->reveal();
        $parent_form_state->getFormObject()
            ->willReturn($parent_form_object)
            ->shouldBeCalledOnce();
        $subform_form_object = $this->prophesize(FormInterface::class)
            ->reveal();
        $subform_state = SubformState::createForSubform($this->parentForm['dog'], $this->parentForm, $parent_form_state->reveal(), $subform_form_object);
        $this->assertSame($subform_form_object, $subform_state->getFormObject());
        $this->assertSame($parent_form_object, $subform_state->getCompleteFormState()
            ->getFormObject());
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
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.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
SubformStateTest::$formStateValues protected static property The form state's values test fixture.
SubformStateTest::$parentForm protected property The parent form.
SubformStateTest::providerGetValues public static function Provides data to self::testGetValues().
SubformStateTest::providerGetValuesBroken public static function Provides data to self::testGetValuesBroken().
SubformStateTest::providerTestGetValue public static function Provides data to self::testGetValue().
SubformStateTest::providerTestGetValueBroken public static function Provides data to self::testGetValueBroken().
SubformStateTest::providerTestSetValues public static function Provides data to self::testSetValues().
SubformStateTest::providerTestSetValuesBroken public static function Provides data to self::testSetValuesBroken().
SubformStateTest::testFormObject public function @covers ::getFormObject
SubformStateTest::testGetCompleteFormStateWithParentCompleteForm public function @covers ::getCompleteFormState
SubformStateTest::testGetCompleteFormStateWithParentSubform public function @covers ::getCompleteFormState
SubformStateTest::testGetLimitValidationErrors public function @covers ::getLimitValidationErrors
SubformStateTest::testGetValue public function @covers ::getValue
SubformStateTest::testGetValueBroken public function @covers ::getValue
SubformStateTest::testGetValues public function @covers ::getValues
@covers ::getParents
SubformStateTest::testGetValuesBroken public function @covers ::getValues
@covers ::getParents
SubformStateTest::testSetErrorByName public function @covers ::setErrorByName
SubformStateTest::testSetLimitValidationErrors public function @covers ::setLimitValidationErrors
SubformStateTest::testSetValues public function @covers ::setValues
SubformStateTest::testSetValuesBroken public function @covers ::setValues
UnitTestCase::$root protected property The app root. 1
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 358
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.