You are here

function FormsTestCase::testDisabledMarkup in Drupal 7

Verify markup for disabled form elements.

See also

_form_test_disabled_elements()

File

modules/simpletest/tests/form.test, line 407
Unit tests for the Drupal Form API.

Class

FormsTestCase
@file Unit tests for the Drupal Form API.

Code

function testDisabledMarkup() {
  $this
    ->drupalGet('form-test/disabled-elements');
  $form_state = array();
  $form = _form_test_disabled_elements(array(), $form_state);
  $type_map = array(
    'textarea' => 'textarea',
    'select' => 'select',
    'weight' => 'select',
    'date' => 'select',
  );
  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(
      'image_button',
      'button',
      'submit',
    ))) {
      $path = "//!type[contains(@class, :div-class) and @value=:value]";
      $class = 'form-button-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' => check_plain($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]',
  )));
}