You are here

protected function WebTestBase::handleForm in Drupal 8

Handles form input related to drupalPostForm().

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.

2 calls to WebTestBase::handleForm()
SearchTestBase::submitGetForm in core/modules/search/src/Tests/SearchTestBase.php
Simulates submission of a form using GET instead of POST.
WebTestBase::drupalPostForm in core/modules/simpletest/src/WebTestBase.php
Executes a form submission.

File

core/modules/simpletest/src/WebTestBase.php, line 1535

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

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':
        case 'date':
        case 'time':
        case 'datetime':
        case 'datetime-local':
          $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;
      }
    }
  }

  // An empty name means the value is not sent.
  unset($post['']);
  return $submit_matches;
}