annotate core/modules/search/tests/src/Functional/SearchPageTextTest.php @ 4:a9cd425dd02b

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