View source
<?php
namespace Drupal\Tests\search\Functional;
use Drupal\Component\Utility\Unicode;
use Drupal\Tests\BrowserTestBase;
class SearchPageTextTest extends BrowserTestBase {
protected static $modules = [
'block',
'node',
'search',
];
protected $defaultTheme = 'stark';
protected $searchingUser;
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->searchingUser = $this
->drupalCreateUser([
'search content',
'access user profiles',
'use advanced search',
]);
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
}
public function testSearchLabelXSS() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer search',
]));
$keys['label'] = '<script>alert("Don\'t Panic");</script>';
$this
->drupalGet('admin/config/search/pages/manage/node_search');
$this
->submitForm($keys, 'Save search page');
$this
->drupalLogin($this->searchingUser);
$this
->drupalGet('search/node');
$this
->assertSession()
->assertEscaped($keys['label']);
}
public function testSearchText() {
$this
->drupalLogin($this->searchingUser);
$this
->drupalGet('search/node');
$this
->assertSession()
->pageTextContains('Enter your keywords');
$this
->assertSession()
->pageTextContains('Search');
$this
->assertSession()
->titleEquals('Search | Drupal');
$edit = [];
$search_terms = 'bike shed ' . $this
->randomMachineName();
$edit['keys'] = $search_terms;
$this
->drupalGet('search/node');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('search yielded no results');
$this
->assertSession()
->pageTextContains('Search');
$this
->assertSession()
->titleEquals('Search for ' . Unicode::truncate($search_terms, 60, TRUE, TRUE) . ' | Drupal');
$this
->assertSession()
->pageTextNotContains('Node');
$this
->assertSession()
->pageTextNotContains('Node');
$this
->assertSession()
->pageTextContains('Content');
$this
->clickLink('About searching');
$this
->assertSession()
->pageTextContains('About searching');
$this
->assertSession()
->pageTextContains('Use upper-case OR to get more results');
$edit = [];
$search_terms = 'Every word is like an unnecessary stain on silence and nothingness.';
$edit['keys'] = $search_terms;
$this
->drupalGet('search/node');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->titleEquals('Search for Every word is like an unnecessary stain on silence and… | Drupal');
$search_terms = 'Hear nothing > "see nothing" `feel' . " '1982.";
$edit['keys'] = $search_terms;
$this
->drupalGet('search/node');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->titleEquals('Search for ' . Unicode::truncate($search_terms, 60, TRUE, TRUE) . ' | Drupal');
$edit['keys'] = $this->searchingUser
->getAccountName();
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('Search');
$this
->assertSession()
->titleEquals('Search for ' . Unicode::truncate($this->searchingUser
->getAccountName(), 60, TRUE, TRUE) . ' | Drupal');
$this
->clickLink('About searching');
$this
->assertSession()
->pageTextContains('About searching');
$this
->assertSession()
->pageTextContains('user names and partial user names');
$arg = $this
->randomMachineName() . '/' . $this
->randomMachineName();
$this
->drupalGet('search/node', [
'query' => [
'keys' => $arg,
],
]);
$this
->assertSession()
->elementExists('xpath', "//input[@id='edit-keys' and @value='{$arg}']");
$limit = $this
->config('search.settings')
->get('and_or_limit');
$keys = [];
for ($i = 0; $i < $limit + 1; $i++) {
$keys[] = $this
->randomMachineName(4);
if ($i % 2 == 0) {
$keys[] = 'OR';
}
}
$edit['keys'] = implode(' ', $keys);
$this
->drupalGet('search/node');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->statusMessageContains("Your search used too many AND/OR expressions. Only the first {$limit} terms were included in this search.", 'warning');
$this
->drupalGet('search/node');
$this
->submitForm([], 'Search');
$this
->assertSession()
->statusMessageContains('Please enter some keywords', 'error');
$this
->drupalGet('search/user');
$this
->submitForm([], 'Search');
$this
->assertSession()
->statusMessageContains('Please enter some keywords', 'error');
$this
->drupalGet('search/node');
$this
->submitForm([
'or' => $this
->randomMachineName() . ' ' . $this
->randomMachineName(),
], 'edit-submit--2');
$this
->assertSession()
->statusMessageNotContains('Please enter some keywords');
$this
->drupalGet('search/node');
$this
->submitForm([
'phrase' => '"' . $this
->randomMachineName() . '" "' . $this
->randomMachineName() . '"',
], 'edit-submit--2');
$this
->assertSession()
->statusMessageNotContains('Please enter some keywords');
$this
->drupalGet('search/node');
$this
->submitForm([
'keys' => $this
->randomMachineName(1),
], 'Search');
$this
->assertSession()
->statusMessageContains('You must include at least one keyword', 'warning');
$this
->assertSession()
->statusMessageNotContains('Please enter some keywords');
$this
->submitForm([
'keys' => $this
->randomMachineName(),
], 'Search');
$this
->assertSession()
->statusMessageNotContains('You must include at least one keyword');
$this
->drupalGet('search/node');
$this
->submitForm([
'keys' => '../../admin',
], 'Search');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('no results');
$this
->drupalGet('search/node');
$this
->submitForm([
'keys' => '.something',
], 'Search');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('no results');
}
}