You are here

function FormTest::testRequiredTextfieldNoTitle in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Form/FormTest.php \Drupal\system\Tests\Form\FormTest::testRequiredTextfieldNoTitle()

Tests validation for required textfield element without title.

Submits a test form containing a textfield form element without title. The form is submitted twice, first without value for the required field and then with value. Each submission is checked for relevant error messages.

See also

\Drupal\form_test\Form\FormTestValidateRequiredNoTitleForm

File

core/modules/system/src/Tests/Form/FormTest.php, line 319
Contains \Drupal\system\Tests\Form\FormTest.

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\system\Tests\Form

Code

function testRequiredTextfieldNoTitle() {

  // Attempt to submit the form with no required field set.
  $edit = array();
  $this
    ->drupalPostForm('form-test/validate-required-no-title', $edit, 'Submit');
  $this
    ->assertNoRaw("The form_test_validate_required_form_no_title form was submitted successfully.", 'Validation form submitted successfully.');

  // Check the page for the error class on the textfield.
  $this
    ->assertFieldByXPath('//input[contains(@class, "error")]', FALSE, 'Error input form element class found.');

  // Check the page for the aria-invalid attribute on the textfield.
  $this
    ->assertFieldByXPath('//input[contains(@aria-invalid, "true")]', FALSE, 'Aria invalid attribute found.');

  // Submit again with required fields set and verify that there are no
  // error messages.
  $edit = array(
    'textfield' => $this
      ->randomString(),
  );
  $this
    ->drupalPostForm(NULL, $edit, 'Submit');
  $this
    ->assertNoFieldByXpath('//input[contains(@class, "error")]', FALSE, 'No error input form element class found.');
  $this
    ->assertRaw("The form_test_validate_required_form_no_title form was submitted successfully.", 'Validation form submitted successfully.');
}