You are here

public function FormTest::testRequiredAttribute in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testRequiredAttribute()

Tests required attribute.

File

core/modules/system/tests/src/Functional/Form/FormTest.php, line 888

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testRequiredAttribute() {
  $this
    ->drupalGet('form-test/required-attribute');
  $expected = 'required';

  // Test to make sure the elements have the proper required attribute.
  foreach ([
    'textfield',
    'password',
  ] as $type) {
    $element = $this
      ->xpath('//input[@id=:id and @required=:expected]', [
      ':id' => 'edit-' . $type,
      ':expected' => $expected,
    ]);
    $this
      ->assertTrue(!empty($element), new FormattableMarkup('The @type has the proper required attribute.', [
      '@type' => $type,
    ]));
  }

  // Test to make sure textarea has the proper required attribute.
  $element = $this
    ->xpath('//textarea[@id=:id and @required=:expected]', [
    ':id' => 'edit-textarea',
    ':expected' => $expected,
  ]);
  $this
    ->assertTrue(!empty($element), 'The textarea has the proper required attribute.');
}