You are here

protected function SearchTestBase::submitGetForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/search/src/Tests/SearchTestBase.php \Drupal\search\Tests\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.

2 calls to SearchTestBase::submitGetForm()
SearchBlockTest::testSearchFormBlock in core/modules/search/src/Tests/SearchBlockTest.php
Test that the search form block can be placed and works.
SearchConfigSettingsFormTest::testSearchModuleDisabling in core/modules/search/src/Tests/SearchConfigSettingsFormTest.php
Verifies that you can disable individual search plugins.

File

core/modules/search/src/Tests/SearchTestBase.php, line 60
Contains \Drupal\search\Tests\SearchTestBase.

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 = array();
      $upload = array();
      $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, array(
          '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(SafeMarkup::format('Failed to set field @name to @value', array(
        '@name' => $name,
        '@value' => $value,
      )));
    }
    $this
      ->assertTrue($submit_matches, format_string('Found the @submit button', array(
      '@submit' => $submit,
    )));
    $this
      ->fail(format_string('Found the requested form fields at @path', array(
      '@path' => $path,
    )));
  }
}