annotate core/modules/search/tests/src/Functional/SearchPageTextTest.php @ 18:af1871eacc83

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