class FormErrorHandlerTest

Same name in this branch
  1. 11.x core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php \Drupal\Tests\Core\Form\FormErrorHandlerTest
Same name in other branches
  1. 9 core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest
  2. 9 core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php \Drupal\Tests\Core\Form\FormErrorHandlerTest
  3. 8.9.x core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest
  4. 8.9.x core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php \Drupal\Tests\Core\Form\FormErrorHandlerTest
  5. 10 core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest
  6. 10 core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php \Drupal\Tests\Core\Form\FormErrorHandlerTest

@coversDefaultClass \Drupal\inline_form_errors\FormErrorHandler @group InlineFormErrors

Hierarchy

Expanded class hierarchy of FormErrorHandlerTest

File

core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php, line 17

Namespace

Drupal\Tests\inline_form_errors\Unit
View source
class FormErrorHandlerTest extends UnitTestCase {
    
    /**
     * The form error handler.
     *
     * @var \Drupal\inline_form_errors\FormErrorHandler
     */
    protected $formErrorHandler;
    
    /**
     * The messenger.
     *
     * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $messenger;
    
    /**
     * The renderer.
     *
     * @var \Drupal\Core\Render\RendererInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $renderer;
    
    /**
     * The link generator.
     *
     * @var \Drupal\Core\Utility\LinkGeneratorInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $linkGenerator;
    
    /**
     * Form for testing.
     *
     * @var array
     */
    protected $testForm;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->renderer = $this->createMock(RendererInterface::class);
        $this->messenger = $this->createMock(MessengerInterface::class);
        $this->formErrorHandler = new FormErrorHandler($this->getStringTranslationStub(), $this->renderer, $this->messenger);
        $this->testForm = [
            '#parents' => [],
            '#form_id' => 'test_form',
            '#array_parents' => [],
        ];
        $this->testForm['test1'] = [
            '#type' => 'textfield',
            '#title' => 'Test 1',
            '#parents' => [
                'test1',
            ],
            '#array_parents' => [
                'test1',
            ],
            '#id' => 'edit-test1',
        ];
        $this->testForm['test2'] = [
            '#type' => 'textfield',
            '#title' => 'Test 2 & a half',
            '#parents' => [
                'test2',
            ],
            '#array_parents' => [
                'test2',
            ],
            '#id' => 'edit-test2',
        ];
        $this->testForm['fieldset'] = [
            '#parents' => [
                'fieldset',
            ],
            '#array_parents' => [
                'fieldset',
            ],
            'test3' => [
                '#type' => 'textfield',
                '#title' => 'Test 3',
                '#parents' => [
                    'fieldset',
                    'test3',
                ],
                '#array_parents' => [
                    'fieldset',
                    'test3',
                ],
                '#id' => 'edit-test3',
            ],
        ];
        $this->testForm['test4'] = [
            '#type' => 'textfield',
            '#title' => 'Test 4',
            '#parents' => [
                'test4',
            ],
            '#array_parents' => [
                'test4',
            ],
            '#id' => 'edit-test4',
            '#error_no_message' => TRUE,
        ];
        $this->testForm['test5'] = [
            '#type' => 'textfield',
            '#parents' => [
                'test5',
            ],
            '#array_parents' => [
                'test5',
            ],
            '#id' => 'edit-test5',
        ];
        $this->testForm['test6'] = [
            '#type' => 'value',
            '#title' => 'Test 6',
            '#parents' => [
                'test6',
            ],
            '#array_parents' => [
                'test6',
            ],
            '#id' => 'edit-test6',
        ];
    }
    
    /**
     * @covers ::handleFormErrors
     * @covers ::displayErrorMessages
     * @covers ::setElementErrorsFromFormState
     */
    public function testErrorMessagesInline() : void {
        $messages = [
            'no title given',
            'element is invisible',
            'this missing element is invalid',
            '3 errors have been found: <ul-comma-list-mock><li-mock>Test 1</li-mock><li-mock>Test 2 &amp; a half</li-mock><li-mock>Test 3</li-mock></ul-comma-list-mock>',
        ];
        $this->messenger
            ->expects($this->exactly(count($messages)))
            ->method('addError')
            ->with($this->callback(function (string $message) use (&$messages) : bool {
            return array_shift($messages) === $message;
        }), FALSE);
        $this->renderer
            ->expects($this->once())
            ->method('renderInIsolation')
            ->willReturnCallback(function ($render_array) {
            $links = [];
            foreach ($render_array[1]['#items'] as $item) {
                $links[] = htmlspecialchars($item['#title']);
            }
            return $render_array[0]['#markup'] . '<ul-comma-list-mock><li-mock>' . implode('</li-mock><li-mock>', $links) . '</li-mock></ul-comma-list-mock>';
        });
        $form_state = new FormState();
        $form_state->setErrorByName('test1', 'invalid');
        $form_state->setErrorByName('test2', 'invalid');
        $form_state->setErrorByName('fieldset][test3', 'invalid');
        $form_state->setErrorByName('test4', 'no error message');
        $form_state->setErrorByName('test5', 'no title given');
        $form_state->setErrorByName('test6', 'element is invisible');
        $form_state->setErrorByName('missing_element', 'this missing element is invalid');
        $this->formErrorHandler
            ->handleFormErrors($this->testForm, $form_state);
        // Assert the #errors is populated for proper input.
        $this->assertSame('invalid', $this->testForm['test1']['#errors']);
        $this->assertSame('invalid', $this->testForm['test2']['#errors']);
        $this->assertSame('invalid', $this->testForm['fieldset']['test3']['#errors']);
        $this->assertSame('no error message', $this->testForm['test4']['#errors']);
        $this->assertSame('no title given', $this->testForm['test5']['#errors']);
        $this->assertSame('element is invisible', $this->testForm['test6']['#errors']);
    }
    
    /**
     * Tests that opting out of Inline Form Errors works.
     */
    public function testErrorMessagesNotInline() : void {
        // Asserts all messages are summarized.
        $messages = [
            'invalid',
            'invalid',
            'invalid',
            'no error message',
            'no title given',
            'element is invisible',
            'this missing element is invalid',
        ];
        $this->messenger
            ->expects($this->exactly(count($messages)))
            ->method('addMessage')
            ->with($this->callback(function (string $message) use (&$messages) : bool {
            return array_shift($messages) === $message;
        }), 'error', FALSE);
        $this->renderer
            ->expects($this->never())
            ->method('renderInIsolation');
        $this->testForm['#disable_inline_form_errors'] = TRUE;
        $form_state = new FormState();
        $form_state->setErrorByName('test1', 'invalid');
        $form_state->setErrorByName('test2', 'invalid');
        $form_state->setErrorByName('fieldset][test3', 'invalid');
        $form_state->setErrorByName('test4', 'no error message');
        $form_state->setErrorByName('test5', 'no title given');
        $form_state->setErrorByName('test6', 'element is invisible');
        $form_state->setErrorByName('missing_element', 'this missing element is invalid');
        $this->formErrorHandler
            ->handleFormErrors($this->testForm, $form_state);
        // Assert the #errors is populated for proper input.
        $this->assertSame('invalid', $this->testForm['test1']['#errors']);
        $this->assertSame('invalid', $this->testForm['test2']['#errors']);
        $this->assertSame('invalid', $this->testForm['fieldset']['test3']['#errors']);
        $this->assertSame('no error message', $this->testForm['test4']['#errors']);
        $this->assertSame('no title given', $this->testForm['test5']['#errors']);
        $this->assertSame('element is invisible', $this->testForm['test6']['#errors']);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
FormErrorHandlerTest::$formErrorHandler protected property The form error handler.
FormErrorHandlerTest::$linkGenerator protected property The link generator.
FormErrorHandlerTest::$messenger protected property The messenger.
FormErrorHandlerTest::$renderer protected property The renderer.
FormErrorHandlerTest::$testForm protected property Form for testing.
FormErrorHandlerTest::setUp protected function Overrides UnitTestCase::setUp
FormErrorHandlerTest::testErrorMessagesInline public function @covers ::handleFormErrors
@covers ::displayErrorMessages
@covers ::setElementErrorsFromFormState
FormErrorHandlerTest::testErrorMessagesNotInline public function Tests that opting out of Inline Form Errors works.
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.
UnitTestCase::$root protected property The app root.
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::setUpBeforeClass public static function

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