You are here

protected function SearchTestBase::submitGetForm in Drupal 8

Same name in this branch
  1. 8 core/modules/search/src/Tests/SearchTestBase.php \Drupal\search\Tests\SearchTestBase::submitGetForm()
  2. 8 core/modules/search/tests/src/Functional/SearchTestBase.php \Drupal\Tests\search\Functional\SearchTestBase::submitGetForm()

Simulates submission of a form using GET instead of POST.

Forms that use the GET method cannot be submitted with WebTestBase::drupalPostForm(), which explicitly uses POST to submit the form. So this method finds the form, verifies that it has input fields and a submit button matching the inputs to this method, and then calls WebTestBase::drupalGet() to simulate the form submission to the 'action' URL of the form (if set, or the current URL if not).

See WebTestBase::drupalPostForm() for more detailed documentation of the function parameters.

Parameters

string $path: Location of the form to be submitted: either a Drupal path, absolute path, or NULL to use the current page.

array $edit: Form field data to submit. Unlike drupalPostForm(), this does not support file uploads.

string $submit: Value of the submit button to submit clicking. Unlike drupalPostForm(), this does not support AJAX.

string $form_html_id: (optional) HTML ID of the form, to disambiguate.

File

core/modules/search/src/Tests/SearchTestBase.php, line 62

Class

SearchTestBase
Defines the common search test code.

Namespace

Drupal\search\Tests

Code

protected function submitGetForm($path, $edit, $submit, $form_html_id = NULL) {
  if (isset($path)) {
    $this
      ->drupalGet($path);
  }
  if ($this
    ->parse()) {

    // Iterate over forms to find one that matches $edit and $submit.
    $edit_save = $edit;
    $xpath = '//form';
    if (!empty($form_html_id)) {
      $xpath .= "[@id='" . $form_html_id . "']";
    }
    $forms = $this
      ->xpath($xpath);
    foreach ($forms as $form) {

      // Try to set the fields of this form as specified in $edit.
      $edit = $edit_save;
      $post = [];
      $upload = [];
      $submit_matches = $this
        ->handleForm($post, $edit, $upload, $submit, $form);
      if (!$edit && $submit_matches) {

        // Everything matched, so "submit" the form.
        $action = isset($form['action']) ? $this
          ->getAbsoluteUrl((string) $form['action']) : NULL;
        $this
          ->drupalGet($action, [
          'query' => $post,
        ]);
        return;
      }
    }

    // We have not found a form which contained all fields of $edit and
    // the submit button.
    foreach ($edit as $name => $value) {
      $this
        ->fail(new FormattableMarkup('Failed to set field @name to @value', [
        '@name' => $name,
        '@value' => $value,
      ]));
    }
    $this
      ->assertTrue($submit_matches, new FormattableMarkup('Found the @submit button', [
      '@submit' => $submit,
    ]));
    $this
      ->fail(new FormattableMarkup('Found the requested form fields at @path', [
      '@path' => $path,
    ]));
  }
}