You are here

function SearchAdvancedSearchFormTest::testFormRefill in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/search/src/Tests/SearchAdvancedSearchFormTest.php \Drupal\search\Tests\SearchAdvancedSearchFormTest::testFormRefill()

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

File

core/modules/search/src/Tests/SearchAdvancedSearchFormTest.php, line 75
Contains \Drupal\search\Tests\SearchAdvancedSearchFormTest.

Class

SearchAdvancedSearchFormTest
Indexes content and tests the advanced search form.

Namespace

Drupal\search\Tests

Code

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

  // 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]', array(
        ':name' => $key,
      ));
      $this
        ->assertTrue(isset($elements[0]) && $elements[0]['value'] == $value, "Field {$key} is set to {$value}");
    }
    else {
      $elements = $this
        ->xpath('//input[@name=:name]', array(
        ':name' => $key,
      ));
      $this
        ->assertTrue(isset($elements[0]) && !empty($elements[0]['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 = array(
    'keys' => 'cat dog OR gerbil -fish -snake',
  );
  $this
    ->drupalPostForm('search/node', $edit2, t('Advanced search'));
  $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]', array(
        ':name' => $key,
      ));
      $this
        ->assertFalse(isset($elements[0]) && $elements[0]['value'] == $value, "Field {$key} is not set to {$value}");
    }
  }
}