You are here

public function SearchAdvancedSearchFormTest::testFormRefill in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/search/tests/src/Functional/SearchAdvancedSearchFormTest.php \Drupal\Tests\search\Functional\SearchAdvancedSearchFormTest::testFormRefill()
  2. 10 core/modules/search/tests/src/Functional/SearchAdvancedSearchFormTest.php \Drupal\Tests\search\Functional\SearchAdvancedSearchFormTest::testFormRefill()

Tests that after submitting the advanced search form, the form is refilled.

File

core/modules/search/tests/src/Functional/SearchAdvancedSearchFormTest.php, line 86

Class

SearchAdvancedSearchFormTest
Indexes content and tests the advanced search form.

Namespace

Drupal\Tests\search\Functional

Code

public function testFormRefill() {
  $edit = [
    'keys' => 'cat',
    'or' => 'dog gerbil',
    'phrase' => 'pets are nice',
    'negative' => 'fish snake',
    'type[page]' => 'page',
  ];
  $this
    ->drupalPostForm('search/node', $edit, 'edit-submit--2');

  // Test that the encoded query appears in the page title. Only test the
  // part not including the quote, because assertText() cannot seem to find
  // the quote marks successfully.
  $this
    ->assertText('Search for cat dog OR gerbil -fish -snake');

  // Verify that all of the form fields are filled out.
  foreach ($edit as $key => $value) {
    if ($key != 'type[page]') {
      $elements = $this
        ->xpath('//input[@name=:name]', [
        ':name' => $key,
      ]);
      $this
        ->assertTrue(isset($elements[0]) && $elements[0]
        ->getValue() == $value, "Field {$key} is set to {$value}");
    }
    else {
      $elements = $this
        ->xpath('//input[@name=:name]', [
        ':name' => $key,
      ]);
      $this
        ->assertTrue(isset($elements[0]) && !empty($elements[0]
        ->getAttribute('checked')), "Field {$key} is checked");
    }
  }

  // Now test by submitting the or/not part of the query in the main
  // search box, and verify that the advanced form is not filled out.
  // (It shouldn't be filled out unless you submit values in those fields.)
  $edit2 = [
    'keys' => 'cat dog OR gerbil -fish -snake',
  ];
  $this
    ->drupalPostForm('search/node', $edit2, 'edit-submit--2');
  $this
    ->assertText('Search for cat dog OR gerbil -fish -snake');
  foreach ($edit as $key => $value) {
    if ($key != 'type[page]') {
      $elements = $this
        ->xpath('//input[@name=:name]', [
        ':name' => $key,
      ]);
      $this
        ->assertFalse(isset($elements[0]) && $elements[0]
        ->getValue() == $value, "Field {$key} is not set to {$value}");
    }
  }
}