View source
<?php
namespace Drupal\Tests\search_api_page\Functional;
use Drupal\Core\Language\LanguageInterface;
class IntegrationTest extends FunctionalTestBase {
public function testSearchApiPage() {
$this
->drupalLogin($this->adminUser);
$assert_session = $this
->assertSession();
$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');
$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
->responseContains('The path should not contain leading or trailing slashes.');
$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"');
}
public function testFramework() {
$this
->drupalLogin($this->adminUser);
$this
->setupSearchAPI();
$this
->drupalGet('admin/config/search/search-api-pages');
$step1 = [
'label' => 'Search',
'id' => 'search',
'index' => $this->index
->id(),
];
$this
->drupalPostForm('admin/config/search/search-api-pages/add', $step1, 'Next');
$step2 = [
'path' => 'search',
];
$this
->drupalPostForm(NULL, $step2, 'Save');
$this
->drupalGet('/search');
$this
->assertSession()
->statusCodeEquals(200);
$this
->checkMultipleWordSearch();
$this
->checkSpacesinSearch();
$this
->checkSlashSearch();
$this
->checkUndefinedLanguageItemsAreFound();
}
protected function checkMultipleWordSearch() {
$assert_session = $this
->assertSession();
$this
->drupalGet('/search');
$assert_session
->statusCodeEquals(200);
$this
->drupalPostForm(NULL, [
'keys' => 'Owls',
], 'Search');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('9 results found');
$this
->drupalPostForm(NULL, [
'keys' => 'birds of prey',
], 'Search');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('9 results found');
$this
->drupalPostForm(NULL, [
'keys' => 'prey birds',
], 'Search');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('9 results found');
}
protected function checkSpacesInSearch() {
$assert_session = $this
->assertSession();
$this
->drupalGet('/search');
$assert_session
->statusCodeEquals(200);
$this
->drupalPostForm(NULL, [
'keys' => 'Owls ',
], 'Search');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('9 results found');
$this
->drupalPostForm(NULL, [
'keys' => ' Owls',
], 'Search');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('9 results found');
}
protected function checkSlashSearch() {
$this
->drupalCreateNode([
'title' => 'Another article',
'type' => 'article',
'body' => [
[
'value' => 'foo/bar/qux fubar',
],
],
]);
$this
->indexItems($this->index
->id());
$assert_session = $this
->assertSession();
$this
->drupalGet('/search');
$assert_session
->statusCodeEquals(200);
$this
->drupalPostForm(NULL, [
'keys' => 'foo/bar',
], 'Search');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('1 result found');
}
protected function checkUndefinedLanguageItemsAreFound() {
$this
->drupalCreateNode([
'title' => 'Another article',
'type' => 'article',
'body' => [
[
'value' => 'Undefined language',
],
],
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this
->indexItems($this->index
->id());
$assert_session = $this
->assertSession();
$this
->drupalGet('/search');
$assert_session
->statusCodeEquals(200);
$this
->drupalPostForm(NULL, [
'keys' => 'Undefined',
], 'Search');
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('1 result found');
}
}