Chris@0: searchingUser = $this->drupalCreateUser(['search content', 'access user profiles', 'use advanced search']); Chris@0: $this->drupalPlaceBlock('local_tasks_block'); Chris@0: $this->drupalPlaceBlock('page_title_block'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests for XSS in search module local task. Chris@0: * Chris@0: * This is a regression test for https://www.drupal.org/node/2338081 Chris@0: */ Chris@0: public function testSearchLabelXSS() { Chris@0: $this->drupalLogin($this->drupalCreateUser(['administer search'])); Chris@0: Chris@0: $keys['label'] = ''; Chris@0: $this->drupalPostForm('admin/config/search/pages/manage/node_search', $keys, t('Save search page')); Chris@0: Chris@0: $this->drupalLogin($this->searchingUser); Chris@0: $this->drupalGet('search/node'); Chris@0: $this->assertEscaped($keys['label']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the failed search text, and various other text on the search page. Chris@0: */ Chris@0: public function testSearchText() { Chris@0: $this->drupalLogin($this->searchingUser); Chris@0: $this->drupalGet('search/node'); Chris@0: $this->assertText(t('Enter your keywords')); Chris@0: $this->assertText(t('Search')); Chris@0: $this->assertTitle(t('Search') . ' | Drupal', 'Search page title is correct'); Chris@0: Chris@0: $edit = []; Chris@0: $search_terms = 'bike shed ' . $this->randomMachineName(); Chris@0: $edit['keys'] = $search_terms; Chris@0: $this->drupalPostForm('search/node', $edit, t('Search')); Chris@0: $this->assertText('search yielded no results'); Chris@0: $this->assertText(t('Search')); Chris@0: $title_source = 'Search for @keywords | Drupal'; Chris@0: $this->assertTitle(t($title_source, ['@keywords' => Unicode::truncate($search_terms, 60, TRUE, TRUE)]), 'Search page title is correct'); Chris@0: $this->assertNoText('Node', 'Erroneous tab and breadcrumb text is not present'); Chris@0: $this->assertNoText(t('Node'), 'Erroneous translated tab and breadcrumb text is not present'); Chris@0: $this->assertText(t('Content'), 'Tab and breadcrumb text is present'); Chris@0: Chris@0: $this->clickLink('Search help'); Chris@0: $this->assertText('Search help', 'Correct title is on search help page'); Chris@0: $this->assertText('Use upper-case OR to get more results', 'Correct text is on content search help page'); Chris@0: Chris@0: // Search for a longer text, and see that it is in the title, truncated. Chris@0: $edit = []; Chris@0: $search_terms = 'Every word is like an unnecessary stain on silence and nothingness.'; Chris@0: $edit['keys'] = $search_terms; Chris@0: $this->drupalPostForm('search/node', $edit, t('Search')); Chris@0: $this->assertTitle(t($title_source, ['@keywords' => 'Every word is like an unnecessary stain on silence and…']), 'Search page title is correct'); Chris@0: Chris@0: // Search for a string with a lot of special characters. Chris@0: $search_terms = 'Hear nothing > "see nothing" `feel' . " '1982."; Chris@0: $edit['keys'] = $search_terms; Chris@0: $this->drupalPostForm('search/node', $edit, t('Search')); Chris@0: $actual_title = (string) current($this->xpath('//title')); Chris@0: $this->assertEqual($actual_title, Html::decodeEntities(t($title_source, ['@keywords' => Unicode::truncate($search_terms, 60, TRUE, TRUE)])), 'Search page title is correct'); Chris@0: Chris@0: $edit['keys'] = $this->searchingUser->getUsername(); Chris@0: $this->drupalPostForm('search/user', $edit, t('Search')); Chris@0: $this->assertText(t('Search')); Chris@0: $this->assertTitle(t($title_source, ['@keywords' => Unicode::truncate($this->searchingUser->getUsername(), 60, TRUE, TRUE)])); Chris@0: Chris@0: $this->clickLink('Search help'); Chris@0: $this->assertText('Search help', 'Correct title is on search help page'); Chris@0: $this->assertText('user names and partial user names', 'Correct text is on user search help page'); Chris@0: Chris@0: // Test that search keywords containing slashes are correctly loaded Chris@0: // from the GET params and displayed in the search form. Chris@0: $arg = $this->randomMachineName() . '/' . $this->randomMachineName(); Chris@0: $this->drupalGet('search/node', ['query' => ['keys' => $arg]]); Chris@0: $input = $this->xpath("//input[@id='edit-keys' and @value='{$arg}']"); Chris@0: $this->assertFalse(empty($input), 'Search keys with a / are correctly set as the default value in the search box.'); Chris@0: Chris@0: // Test a search input exceeding the limit of AND/OR combinations to test Chris@0: // the Denial-of-Service protection. Chris@0: $limit = $this->config('search.settings')->get('and_or_limit'); Chris@0: $keys = []; Chris@0: for ($i = 0; $i < $limit + 1; $i++) { Chris@0: // Use a key of 4 characters to ensure we never generate 'AND' or 'OR'. Chris@0: $keys[] = $this->randomMachineName(4); Chris@0: if ($i % 2 == 0) { Chris@0: $keys[] = 'OR'; Chris@0: } Chris@0: } Chris@0: $edit['keys'] = implode(' ', $keys); Chris@0: $this->drupalPostForm('search/node', $edit, t('Search')); Chris@0: $this->assertRaw(t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', ['@count' => $limit])); Chris@0: Chris@0: // Test that a search on Node or User with no keywords entered generates Chris@0: // the "Please enter some keywords" message. Chris@0: $this->drupalPostForm('search/node', [], t('Search')); Chris@0: $this->assertText(t('Please enter some keywords'), 'With no keywords entered, message is displayed on node page'); Chris@0: $this->drupalPostForm('search/user', [], t('Search')); Chris@0: $this->assertText(t('Please enter some keywords'), 'With no keywords entered, message is displayed on user page'); Chris@0: Chris@0: // Make sure the "Please enter some keywords" message is NOT displayed if Chris@0: // you use "or" words or phrases in Advanced Search. Chris@0: $this->drupalPostForm('search/node', ['or' => $this->randomMachineName() . ' ' . $this->randomMachineName()], t('Advanced search')); Chris@0: $this->assertNoText(t('Please enter some keywords'), 'With advanced OR keywords entered, no keywords message is not displayed on node page'); Chris@0: $this->drupalPostForm('search/node', ['phrase' => '"' . $this->randomMachineName() . '" "' . $this->randomMachineName() . '"'], t('Advanced search')); Chris@0: $this->assertNoText(t('Please enter some keywords'), 'With advanced phrase entered, no keywords message is not displayed on node page'); Chris@0: Chris@0: // Verify that if you search for a too-short keyword, you get the right Chris@0: // message, and that if after that you search for a longer keyword, you Chris@0: // do not still see the message. Chris@0: $this->drupalPostForm('search/node', ['keys' => $this->randomMachineName(1)], t('Search')); Chris@0: $this->assertText('You must include at least one keyword', 'Keyword message is displayed when searching for short word'); Chris@0: $this->assertNoText(t('Please enter some keywords'), 'With short word entered, no keywords message is not displayed'); Chris@0: $this->drupalPostForm(NULL, ['keys' => $this->randomMachineName()], t('Search')); Chris@0: $this->assertNoText('You must include at least one keyword', 'Keyword message is not displayed when searching for long word after short word search'); Chris@0: Chris@0: // Test that if you search for a URL with .. in it, you still end up at Chris@0: // the search page. See issue https://www.drupal.org/node/890058. Chris@0: $this->drupalPostForm('search/node', ['keys' => '../../admin'], t('Search')); Chris@0: $this->assertResponse(200, 'Searching for ../../admin with non-admin user does not lead to a 403 error'); Chris@0: $this->assertText('no results', 'Searching for ../../admin with non-admin user gives you a no search results page'); Chris@0: Chris@0: // Test that if you search for a URL starting with "./", you still end up Chris@0: // at the search page. See issue https://www.drupal.org/node/1421560. Chris@0: $this->drupalPostForm('search/node', ['keys' => '.something'], t('Search')); Chris@0: $this->assertResponse(200, 'Searching for .something does not lead to a 403 error'); Chris@0: $this->assertText('no results', 'Searching for .something gives you a no search results page'); Chris@0: } Chris@0: Chris@0: }