1 backdrop_web_test_case.php protected BackdropWebTestCase::handleForm(&$post, &$edit, &$upload, $submit, $form)

Handle form input related to backdropPost(). 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.

SimpleXmlElement $form: A SimpleXmlElement containing the form.

Return value

Submit value matches a valid submit input in the form.:

File

core/modules/simpletest/backdrop_web_test_case.php, line 2656

Class

BackdropWebTestCase
Test case for typical Backdrop 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 'color':
        case 'email':
        case 'hidden':
        case 'number':
        case 'range':
        case 'text':
        case 'tel':
        case 'date':
        case 'time':
        case 'textarea':
        case 'url':
        case 'password':
        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;
          }
          $post[$name] = $value;
          break;
        default:
          $post[$name] = $value;
      }
    }
  }
  return $submit_matches;
}