annotate core/modules/search/src/Tests/SearchPageTextTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\search\Tests;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Html;
Chris@0 6 use Drupal\Component\Utility\Unicode;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests the search help text and search page text.
Chris@0 10 *
Chris@0 11 * @group search
Chris@0 12 */
Chris@0 13 class SearchPageTextTest extends SearchTestBase {
Chris@0 14 /**
Chris@0 15 * A user with permission to use advanced search.
Chris@0 16 *
Chris@0 17 * @var \Drupal\user\UserInterface
Chris@0 18 */
Chris@0 19 protected $searchingUser;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Modules to enable.
Chris@0 23 *
Chris@0 24 * @var string[]
Chris@0 25 */
Chris@0 26 public static $modules = ['block'];
Chris@0 27
Chris@0 28 /**
Chris@0 29 * {@inheritdoc}
Chris@0 30 */
Chris@0 31 protected function setUp() {
Chris@0 32 parent::setUp();
Chris@0 33
Chris@0 34 // Create user.
Chris@0 35 $this->searchingUser = $this->drupalCreateUser(['search content', 'access user profiles', 'use advanced search']);
Chris@0 36 $this->drupalPlaceBlock('local_tasks_block');
Chris@0 37 $this->drupalPlaceBlock('page_title_block');
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Tests for XSS in search module local task.
Chris@0 42 *
Chris@0 43 * This is a regression test for https://www.drupal.org/node/2338081
Chris@0 44 */
Chris@0 45 public function testSearchLabelXSS() {
Chris@0 46 $this->drupalLogin($this->drupalCreateUser(['administer search']));
Chris@0 47
Chris@0 48 $keys['label'] = '<script>alert("Dont Panic");</script>';
Chris@0 49 $this->drupalPostForm('admin/config/search/pages/manage/node_search', $keys, t('Save search page'));
Chris@0 50
Chris@0 51 $this->drupalLogin($this->searchingUser);
Chris@0 52 $this->drupalGet('search/node');
Chris@0 53 $this->assertEscaped($keys['label']);
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Tests the failed search text, and various other text on the search page.
Chris@0 58 */
Chris@0 59 public function testSearchText() {
Chris@0 60 $this->drupalLogin($this->searchingUser);
Chris@0 61 $this->drupalGet('search/node');
Chris@0 62 $this->assertText(t('Enter your keywords'));
Chris@0 63 $this->assertText(t('Search'));
Chris@0 64 $this->assertTitle(t('Search') . ' | Drupal', 'Search page title is correct');
Chris@0 65
Chris@0 66 $edit = [];
Chris@0 67 $search_terms = 'bike shed ' . $this->randomMachineName();
Chris@0 68 $edit['keys'] = $search_terms;
Chris@0 69 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 70 $this->assertText('search yielded no results');
Chris@0 71 $this->assertText(t('Search'));
Chris@0 72 $title_source = 'Search for @keywords | Drupal';
Chris@0 73 $this->assertTitle(t($title_source, ['@keywords' => Unicode::truncate($search_terms, 60, TRUE, TRUE)]), 'Search page title is correct');
Chris@0 74 $this->assertNoText('Node', 'Erroneous tab and breadcrumb text is not present');
Chris@0 75 $this->assertNoText(t('Node'), 'Erroneous translated tab and breadcrumb text is not present');
Chris@0 76 $this->assertText(t('Content'), 'Tab and breadcrumb text is present');
Chris@0 77
Chris@0 78 $this->clickLink('Search help');
Chris@0 79 $this->assertText('Search help', 'Correct title is on search help page');
Chris@0 80 $this->assertText('Use upper-case OR to get more results', 'Correct text is on content search help page');
Chris@0 81
Chris@0 82 // Search for a longer text, and see that it is in the title, truncated.
Chris@0 83 $edit = [];
Chris@0 84 $search_terms = 'Every word is like an unnecessary stain on silence and nothingness.';
Chris@0 85 $edit['keys'] = $search_terms;
Chris@0 86 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 87 $this->assertTitle(t($title_source, ['@keywords' => 'Every word is like an unnecessary stain on silence and…']), 'Search page title is correct');
Chris@0 88
Chris@0 89 // Search for a string with a lot of special characters.
Chris@0 90 $search_terms = 'Hear nothing > "see nothing" `feel' . " '1982.";
Chris@0 91 $edit['keys'] = $search_terms;
Chris@0 92 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 93 $actual_title = (string) current($this->xpath('//title'));
Chris@0 94 $this->assertEqual($actual_title, Html::decodeEntities(t($title_source, ['@keywords' => Unicode::truncate($search_terms, 60, TRUE, TRUE)])), 'Search page title is correct');
Chris@0 95
Chris@0 96 $edit['keys'] = $this->searchingUser->getUsername();
Chris@0 97 $this->drupalPostForm('search/user', $edit, t('Search'));
Chris@0 98 $this->assertText(t('Search'));
Chris@0 99 $this->assertTitle(t($title_source, ['@keywords' => Unicode::truncate($this->searchingUser->getUsername(), 60, TRUE, TRUE)]));
Chris@0 100
Chris@0 101 $this->clickLink('Search help');
Chris@0 102 $this->assertText('Search help', 'Correct title is on search help page');
Chris@0 103 $this->assertText('user names and partial user names', 'Correct text is on user search help page');
Chris@0 104
Chris@0 105 // Test that search keywords containing slashes are correctly loaded
Chris@0 106 // from the GET params and displayed in the search form.
Chris@0 107 $arg = $this->randomMachineName() . '/' . $this->randomMachineName();
Chris@0 108 $this->drupalGet('search/node', ['query' => ['keys' => $arg]]);
Chris@0 109 $input = $this->xpath("//input[@id='edit-keys' and @value='{$arg}']");
Chris@0 110 $this->assertFalse(empty($input), 'Search keys with a / are correctly set as the default value in the search box.');
Chris@0 111
Chris@0 112 // Test a search input exceeding the limit of AND/OR combinations to test
Chris@0 113 // the Denial-of-Service protection.
Chris@0 114 $limit = $this->config('search.settings')->get('and_or_limit');
Chris@0 115 $keys = [];
Chris@0 116 for ($i = 0; $i < $limit + 1; $i++) {
Chris@0 117 // Use a key of 4 characters to ensure we never generate 'AND' or 'OR'.
Chris@0 118 $keys[] = $this->randomMachineName(4);
Chris@0 119 if ($i % 2 == 0) {
Chris@0 120 $keys[] = 'OR';
Chris@0 121 }
Chris@0 122 }
Chris@0 123 $edit['keys'] = implode(' ', $keys);
Chris@0 124 $this->drupalPostForm('search/node', $edit, t('Search'));
Chris@0 125 $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 126
Chris@0 127 // Test that a search on Node or User with no keywords entered generates
Chris@0 128 // the "Please enter some keywords" message.
Chris@0 129 $this->drupalPostForm('search/node', [], t('Search'));
Chris@0 130 $this->assertText(t('Please enter some keywords'), 'With no keywords entered, message is displayed on node page');
Chris@0 131 $this->drupalPostForm('search/user', [], t('Search'));
Chris@0 132 $this->assertText(t('Please enter some keywords'), 'With no keywords entered, message is displayed on user page');
Chris@0 133
Chris@0 134 // Make sure the "Please enter some keywords" message is NOT displayed if
Chris@0 135 // you use "or" words or phrases in Advanced Search.
Chris@0 136 $this->drupalPostForm('search/node', ['or' => $this->randomMachineName() . ' ' . $this->randomMachineName()], t('Advanced search'));
Chris@0 137 $this->assertNoText(t('Please enter some keywords'), 'With advanced OR keywords entered, no keywords message is not displayed on node page');
Chris@0 138 $this->drupalPostForm('search/node', ['phrase' => '"' . $this->randomMachineName() . '" "' . $this->randomMachineName() . '"'], t('Advanced search'));
Chris@0 139 $this->assertNoText(t('Please enter some keywords'), 'With advanced phrase entered, no keywords message is not displayed on node page');
Chris@0 140
Chris@0 141 // Verify that if you search for a too-short keyword, you get the right
Chris@0 142 // message, and that if after that you search for a longer keyword, you
Chris@0 143 // do not still see the message.
Chris@0 144 $this->drupalPostForm('search/node', ['keys' => $this->randomMachineName(1)], t('Search'));
Chris@0 145 $this->assertText('You must include at least one keyword', 'Keyword message is displayed when searching for short word');
Chris@0 146 $this->assertNoText(t('Please enter some keywords'), 'With short word entered, no keywords message is not displayed');
Chris@0 147 $this->drupalPostForm(NULL, ['keys' => $this->randomMachineName()], t('Search'));
Chris@0 148 $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 149
Chris@0 150 // Test that if you search for a URL with .. in it, you still end up at
Chris@0 151 // the search page. See issue https://www.drupal.org/node/890058.
Chris@0 152 $this->drupalPostForm('search/node', ['keys' => '../../admin'], t('Search'));
Chris@0 153 $this->assertResponse(200, 'Searching for ../../admin with non-admin user does not lead to a 403 error');
Chris@0 154 $this->assertText('no results', 'Searching for ../../admin with non-admin user gives you a no search results page');
Chris@0 155
Chris@0 156 // Test that if you search for a URL starting with "./", you still end up
Chris@0 157 // at the search page. See issue https://www.drupal.org/node/1421560.
Chris@0 158 $this->drupalPostForm('search/node', ['keys' => '.something'], t('Search'));
Chris@0 159 $this->assertResponse(200, 'Searching for .something does not lead to a 403 error');
Chris@0 160 $this->assertText('no results', 'Searching for .something gives you a no search results page');
Chris@0 161 }
Chris@0 162
Chris@0 163 }