You are here

public function IntegrationTest::testSearchApiPage in Search API Pages 8

Tests Search API Pages.

File

tests/src/Functional/IntegrationTest.php, line 17

Class

IntegrationTest
Provides web tests for Search API Pages.

Namespace

Drupal\Tests\search_api_page\Functional

Code

public function testSearchApiPage() {
  $this
    ->drupalLogin($this->adminUser);
  $assert_session = $this
    ->assertSession();

  // Setup search api server and index.
  $this
    ->setupSearchApi();
  $this
    ->drupalGet('admin/config/search/search-api-pages');
  $assert_session
    ->statusCodeEquals(200);
  $step1 = [
    'label' => 'Search',
    'id' => 'search',
    'index' => $this->index
      ->id(),
  ];
  $this
    ->drupalPostForm('admin/config/search/search-api-pages/add', $step1, 'Next');

  // Test whether a leading slash leads to a form error.
  $step2 = [
    'path' => '/search',
  ];
  $this
    ->drupalPostForm(NULL, $step2, 'Save');
  $assert_session
    ->responseContains('The path should not contain leading or trailing slashes.');

  // Test whether a trailing slash leads to a form error.
  $step2 = [
    'path' => 'search/',
  ];
  $this
    ->drupalPostForm(NULL, $step2, 'Save');
  $assert_session
    ->responseContains('The path should not contain leading or trailing slashes.');

  // Test whether both a leading slash and a trailing slash leads to a form error.
  $step2 = [
    'path' => '/search/',
  ];
  $this
    ->drupalPostForm(NULL, $step2, 'Save');
  $assert_session
    ->responseContains('The path should not contain leading or trailing slashes.');
  $step2 = [
    'path' => 'search',
  ];
  $this
    ->drupalPostForm(NULL, $step2, 'Save');
  $assert_session
    ->responseNotContains('The path should not contain leading or trailing slashes.');
  $this
    ->drupalGet('search');
  $assert_session
    ->responseContains('Enter the terms you wish to search for.');
  $assert_session
    ->pageTextNotContains('Your search yielded no results.');
  $assert_session
    ->statusCodeEquals(200);
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->unauthorizedUser);
  $this
    ->drupalGet('search');
  $assert_session
    ->statusCodeEquals(403);
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->anonymousUser);
  $this
    ->drupalGet('search');
  $assert_session
    ->statusCodeEquals(200);
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('search/nothing-found');
  $assert_session
    ->responseContains('Enter the terms you wish to search for.');
  $assert_session
    ->pageTextContains('Your search yielded no results.');
  $this
    ->drupalGet('search');
  $assert_session
    ->pageTextNotContains('Your search yielded no results.');
  $this
    ->drupalPostForm('admin/config/search/search-api-pages/search', [
    'show_all_when_no_keys' => TRUE,
    'show_search_form' => FALSE,
  ], 'Save');
  $this
    ->drupalGet('search');
  $assert_session
    ->pageTextNotContains('Your search yielded no results.');
  $assert_session
    ->responseNotContains('Enter the terms you wish to search for.');
  $assert_session
    ->pageTextContains('49 results found');
  $this
    ->drupalGet('search/number10');
  $assert_session
    ->pageTextContains('1 result found');
  $this
    ->drupalPostForm('admin/config/search/search-api-pages/search', [
    'show_search_form' => TRUE,
  ], 'Save');
  $this
    ->drupalGet('search/number11');
  $assert_session
    ->pageTextContains('1 result found');
  $assert_session
    ->responseContains('name="keys" value="number11"');

  // Cache should be cleared after the save.
  // @todo Make this work.
  // $this->drupalGet('search/number10');
  // $assert_session->pageTextContains('1 result found');
  // $assert_session->responseContains('name="keys" value="number10"');.
}