You are here

protected function DrupalWebTestCase::handleForm in Drupal 7

Handle form input related to drupalPost(). Ensure that the specified fields exist and attempt to create POST data in the correct manner for the particular field type.

Parameters

$post: Reference to array of post values.

$edit: Reference to array of edit values to be checked against the form.

$submit: Form submit button value.

$form: Array of form elements.

Return value

Submit value matches a valid submit input in the form.

1 call to DrupalWebTestCase::handleForm()
DrupalWebTestCase::drupalPost in modules/simpletest/drupal_web_test_case.php
Execute a POST request on a Drupal page. It will be done as usual POST request with SimpleBrowser.

File

modules/simpletest/drupal_web_test_case.php, line 2663

Class

DrupalWebTestCase
Test case for typical Drupal tests.

Code

protected function handleForm(&$post, &$edit, &$upload, $submit, $form) {

  // Retrieve the form elements.
  $elements = $form
    ->xpath('.//input[not(@disabled)]|.//textarea[not(@disabled)]|.//select[not(@disabled)]');
  $submit_matches = FALSE;
  foreach ($elements as $element) {

    // SimpleXML objects need string casting all the time.
    $name = (string) $element['name'];

    // This can either be the type of <input> or the name of the tag itself
    // for <select> or <textarea>.
    $type = isset($element['type']) ? (string) $element['type'] : $element
      ->getName();
    $value = isset($element['value']) ? (string) $element['value'] : '';
    $done = FALSE;
    if (isset($edit[$name])) {
      switch ($type) {
        case 'text':
        case 'tel':
        case 'textarea':
        case 'url':
        case 'number':
        case 'range':
        case 'color':
        case 'hidden':
        case 'password':
        case 'email':
        case 'search':
          $post[$name] = $edit[$name];
          unset($edit[$name]);
          break;
        case 'radio':
          if ($edit[$name] == $value) {
            $post[$name] = $edit[$name];
            unset($edit[$name]);
          }
          break;
        case 'checkbox':

          // To prevent checkbox from being checked.pass in a FALSE,
          // otherwise the checkbox will be set to its value regardless
          // of $edit.
          if ($edit[$name] === FALSE) {
            unset($edit[$name]);
            continue 2;
          }
          else {
            unset($edit[$name]);
            $post[$name] = $value;
          }
          break;
        case 'select':
          $new_value = $edit[$name];
          $options = $this
            ->getAllOptions($element);
          if (is_array($new_value)) {

            // Multiple select box.
            if (!empty($new_value)) {
              $index = 0;
              $key = preg_replace('/\\[\\]$/', '', $name);
              foreach ($options as $option) {
                $option_value = (string) $option['value'];
                if (in_array($option_value, $new_value)) {
                  $post[$key . '[' . $index++ . ']'] = $option_value;
                  $done = TRUE;
                  unset($edit[$name]);
                }
              }
            }
            else {

              // No options selected: do not include any POST data for the
              // element.
              $done = TRUE;
              unset($edit[$name]);
            }
          }
          else {

            // Single select box.
            foreach ($options as $option) {
              if ($new_value == $option['value']) {
                $post[$name] = $new_value;
                unset($edit[$name]);
                $done = TRUE;
                break;
              }
            }
          }
          break;
        case 'file':
          $upload[$name] = $edit[$name];
          unset($edit[$name]);
          break;
      }
    }
    if (!isset($post[$name]) && !$done) {
      switch ($type) {
        case 'textarea':
          $post[$name] = (string) $element;
          break;
        case 'select':
          $single = empty($element['multiple']);
          $first = TRUE;
          $index = 0;
          $key = preg_replace('/\\[\\]$/', '', $name);
          $options = $this
            ->getAllOptions($element);
          foreach ($options as $option) {

            // For single select, we load the first option, if there is a
            // selected option that will overwrite it later.
            if ($option['selected'] || $first && $single) {
              $first = FALSE;
              if ($single) {
                $post[$name] = (string) $option['value'];
              }
              else {
                $post[$key . '[' . $index++ . ']'] = (string) $option['value'];
              }
            }
          }
          break;
        case 'file':
          break;
        case 'submit':
        case 'image':
          if (isset($submit) && $submit == $value) {
            $post[$name] = $value;
            $submit_matches = TRUE;
          }
          break;
        case 'radio':
        case 'checkbox':
          if (!isset($element['checked'])) {
            break;
          }

        // Deliberate no break.
        default:
          $post[$name] = $value;
      }
    }
  }
  return $submit_matches;
}