comparison core/modules/search/tests/src/Functional/SearchPageTextTest.php @ 17:129ea1e6d783

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