You are here

function FormTest::testDisabledMarkup 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::testDisabledMarkup()

Verify markup for disabled form elements.

See also

_form_test_disabled_elements()

File

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

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\system\Tests\Form

Code

function testDisabledMarkup() {
  $this
    ->drupalGet('form-test/disabled-elements');
  $form = \Drupal::formBuilder()
    ->getForm('\\Drupal\\form_test\\Form\\FormTestDisabledElementsForm');
  $type_map = array(
    'textarea' => 'textarea',
    'select' => 'select',
    'weight' => 'select',
    'datetime' => 'datetime',
  );
  foreach ($form as $name => $item) {

    // Skip special #types.
    if (!isset($item['#type']) || in_array($item['#type'], array(
      'hidden',
      'text_format',
    ))) {
      continue;
    }

    // Setup XPath and CSS class depending on #type.
    if (in_array($item['#type'], array(
      'button',
      'submit',
    ))) {
      $path = "//!type[contains(@class, :div-class) and @value=:value]";
      $class = 'is-disabled';
    }
    elseif (in_array($item['#type'], array(
      'image_button',
    ))) {
      $path = "//!type[contains(@class, :div-class) and @value=:value]";
      $class = 'is-disabled';
    }
    else {

      // starts-with() required for checkboxes.
      $path = "//div[contains(@class, :div-class)]/descendant::!type[starts-with(@name, :name)]";
      $class = 'form-disabled';
    }

    // Replace DOM element name in $path according to #type.
    $type = 'input';
    if (isset($type_map[$item['#type']])) {
      $type = $type_map[$item['#type']];
    }
    $path = strtr($path, array(
      '!type' => $type,
    ));

    // Verify that the element exists.
    $element = $this
      ->xpath($path, array(
      ':name' => Html::escape($name),
      ':div-class' => $class,
      ':value' => isset($item['#value']) ? $item['#value'] : '',
    ));
    $this
      ->assertTrue(isset($element[0]), format_string('Disabled form element class found for #type %type.', array(
      '%type' => $item['#type'],
    )));
  }

  // Verify special element #type text-format.
  $element = $this
    ->xpath('//div[contains(@class, :div-class)]/descendant::textarea[@name=:name]', array(
    ':name' => 'text_format[value]',
    ':div-class' => 'form-disabled',
  ));
  $this
    ->assertTrue(isset($element[0]), format_string('Disabled form element class found for #type %type.', array(
    '%type' => 'text_format[value]',
  )));
  $element = $this
    ->xpath('//div[contains(@class, :div-class)]/descendant::select[@name=:name]', array(
    ':name' => 'text_format[format]',
    ':div-class' => 'form-disabled',
  ));
  $this
    ->assertTrue(isset($element[0]), format_string('Disabled form element class found for #type %type.', array(
    '%type' => 'text_format[format]',
  )));
}