function FormErrorHandlerTest::testErrorMessagesNotInline

Same name and namespace in other branches
  1. 9 core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest::testErrorMessagesNotInline()
  2. 10 core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest::testErrorMessagesNotInline()
  3. 11.x core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest::testErrorMessagesNotInline()

Tests that opting out of Inline Form Errors works.

File

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

Class

FormErrorHandlerTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21inline_form_errors%21src%21FormErrorHandler.php/class/FormErrorHandler/8.9.x" title="Produces inline form errors." class="local">\Drupal\inline_form_errors\FormErrorHandler</a> @group InlineFormErrors

Namespace

Drupal\Tests\inline_form_errors\Unit

Code

public function testErrorMessagesNotInline() {
    $this->messenger
        ->expects($this->exactly(7))
        ->method('addMessage');
    // Asserts all messages are summarized.
    $this->messenger
        ->expects($this->at(0))
        ->method('addMessage')
        ->with('invalid', 'error', FALSE);
    $this->messenger
        ->expects($this->at(1))
        ->method('addMessage')
        ->with('invalid', 'error', FALSE);
    $this->messenger
        ->expects($this->at(2))
        ->method('addMessage')
        ->with('invalid', 'error', FALSE);
    $this->messenger
        ->expects($this->at(3))
        ->method('addMessage')
        ->with('no error message', 'error', FALSE);
    $this->messenger
        ->expects($this->at(4))
        ->method('addMessage')
        ->with('no title given', 'error', FALSE);
    $this->messenger
        ->expects($this->at(5))
        ->method('addMessage')
        ->with('element is invisible', 'error', FALSE);
    $this->messenger
        ->expects($this->at(6))
        ->method('addMessage')
        ->with('this missing element is invalid', 'error', FALSE);
    $this->renderer
        ->expects($this->never())
        ->method('renderPlain');
    $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']);
}

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