You are here

function FormTest::testRequiredAttribute in Zircon Profile 8.0

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

Tests required attribute.

File

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

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\system\Tests\Form

Code

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

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

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