annotate core/modules/search/src/Tests/SearchConfigSettingsFormTest.php @ 1:1a348b17ec81

Logo and header background
author Chris Cannam
date Thu, 30 Nov 2017 14:56:35 +0000
parents 4c8ae668cc8c
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\Core\Url;
Chris@0 6 use Drupal\search\Entity\SearchPage;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Verify the search config settings form.
Chris@0 10 *
Chris@0 11 * @group search
Chris@0 12 */
Chris@0 13 class SearchConfigSettingsFormTest extends SearchTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Modules to enable.
Chris@0 17 *
Chris@0 18 * @var array
Chris@0 19 */
Chris@0 20 public static $modules = ['block', 'search_extra_type', 'test_page_test'];
Chris@0 21
Chris@0 22 /**
Chris@0 23 * User who can search and administer search.
Chris@0 24 *
Chris@0 25 * @var \Drupal\user\UserInterface
Chris@0 26 */
Chris@0 27 protected $searchUser;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Node indexed for searching.
Chris@0 31 *
Chris@0 32 * @var \Drupal\node\NodeInterface
Chris@0 33 */
Chris@0 34 protected $searchNode;
Chris@0 35
Chris@0 36 protected function setUp() {
Chris@0 37 parent::setUp();
Chris@0 38
Chris@0 39 // Log in as a user that can create and search content.
Chris@0 40 $this->searchUser = $this->drupalCreateUser(['search content', 'administer search', 'administer nodes', 'bypass node access', 'access user profiles', 'administer users', 'administer blocks', 'access site reports']);
Chris@0 41 $this->drupalLogin($this->searchUser);
Chris@0 42
Chris@0 43 // Add a single piece of content and index it.
Chris@0 44 $node = $this->drupalCreateNode();
Chris@0 45 $this->searchNode = $node;
Chris@0 46 // Link the node to itself to test that it's only indexed once. The content
Chris@0 47 // also needs the word "pizza" so we can use it as the search keyword.
Chris@0 48 $body_key = 'body[0][value]';
Chris@0 49 $edit[$body_key] = \Drupal::l($node->label(), $node->urlInfo()) . ' pizza sandwich';
Chris@0 50 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
Chris@0 51
Chris@0 52 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
Chris@0 53 search_update_totals();
Chris@0 54
Chris@0 55 // Enable the search block.
Chris@0 56 $this->drupalPlaceBlock('search_form_block');
Chris@0 57 $this->drupalPlaceBlock('local_tasks_block');
Chris@0 58 $this->drupalPlaceBlock('page_title_block');
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * Verifies the search settings form.
Chris@0 63 */
Chris@0 64 public function testSearchSettingsPage() {
Chris@0 65
Chris@0 66 // Test that the settings form displays the correct count of items left to index.
Chris@0 67 $this->drupalGet('admin/config/search/pages');
Chris@0 68 $this->assertText(t('There are @count items left to index.', ['@count' => 0]));
Chris@0 69
Chris@0 70 // Test the re-index button.
Chris@0 71 $this->drupalPostForm('admin/config/search/pages', [], t('Re-index site'));
Chris@0 72 $this->assertText(t('Are you sure you want to re-index the site'));
Chris@0 73 $this->drupalPostForm('admin/config/search/pages/reindex', [], t('Re-index site'));
Chris@0 74 $this->assertText(t('All search indexes will be rebuilt'));
Chris@0 75 $this->drupalGet('admin/config/search/pages');
Chris@0 76 $this->assertText(t('There is 1 item left to index.'));
Chris@0 77
Chris@0 78 // Test that the form saves with the default values.
Chris@0 79 $this->drupalPostForm('admin/config/search/pages', [], t('Save configuration'));
Chris@0 80 $this->assertText(t('The configuration options have been saved.'), 'Form saves with the default values.');
Chris@0 81
Chris@0 82 // Test that the form does not save with an invalid word length.
Chris@0 83 $edit = [
Chris@0 84 'minimum_word_size' => $this->randomMachineName(3),
Chris@0 85 ];
Chris@0 86 $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
Chris@0 87 $this->assertNoText(t('The configuration options have been saved.'), 'Form does not save with an invalid word length.');
Chris@0 88
Chris@0 89 // Test logging setting. It should be off by default.
Chris@0 90 $text = $this->randomMachineName(5);
Chris@0 91 $this->drupalPostForm('search/node', ['keys' => $text], t('Search'));
Chris@0 92 $this->drupalGet('admin/reports/dblog');
Chris@0 93 $this->assertNoLink('Searched Content for ' . $text . '.', 'Search was not logged');
Chris@0 94
Chris@0 95 // Turn on logging.
Chris@0 96 $edit = ['logging' => TRUE];
Chris@0 97 $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
Chris@0 98 $text = $this->randomMachineName(5);
Chris@0 99 $this->drupalPostForm('search/node', ['keys' => $text], t('Search'));
Chris@0 100 $this->drupalGet('admin/reports/dblog');
Chris@0 101 $this->assertLink('Searched Content for ' . $text . '.', 0, 'Search was logged');
Chris@0 102
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * Verifies plugin-supplied settings form.
Chris@0 107 */
Chris@0 108 public function testSearchModuleSettingsPage() {
Chris@0 109 $this->drupalGet('admin/config/search/pages');
Chris@0 110 $this->clickLink(t('Edit'), 1);
Chris@0 111
Chris@0 112 // Ensure that the default setting was picked up from the default config
Chris@0 113 $this->assertTrue($this->xpath('//select[@id="edit-extra-type-settings-boost"]//option[@value="bi" and @selected="selected"]'), 'Module specific settings are picked up from the default config');
Chris@0 114
Chris@0 115 // Change extra type setting and also modify a common search setting.
Chris@0 116 $edit = [
Chris@0 117 'extra_type_settings[boost]' => 'ii',
Chris@0 118 ];
Chris@0 119 $this->drupalPostForm(NULL, $edit, t('Save search page'));
Chris@0 120
Chris@0 121 // Ensure that the modifications took effect.
Chris@0 122 $this->assertRaw(t('The %label search page has been updated.', ['%label' => 'Dummy search type']));
Chris@0 123 $this->drupalGet('admin/config/search/pages/manage/dummy_search_type');
Chris@0 124 $this->assertTrue($this->xpath('//select[@id="edit-extra-type-settings-boost"]//option[@value="ii" and @selected="selected"]'), 'Module specific settings can be changed');
Chris@0 125 }
Chris@0 126
Chris@0 127 /**
Chris@0 128 * Verifies that you can disable individual search plugins.
Chris@0 129 */
Chris@0 130 public function testSearchModuleDisabling() {
Chris@0 131 // Array of search plugins to test: 'keys' are the keywords to search for,
Chris@0 132 // and 'text' is the text to assert is on the results page.
Chris@0 133 $plugin_info = [
Chris@0 134 'node_search' => [
Chris@0 135 'keys' => 'pizza',
Chris@0 136 'text' => $this->searchNode->label(),
Chris@0 137 ],
Chris@0 138 'user_search' => [
Chris@0 139 'keys' => $this->searchUser->getUsername(),
Chris@0 140 'text' => $this->searchUser->getEmail(),
Chris@0 141 ],
Chris@0 142 'dummy_search_type' => [
Chris@0 143 'keys' => 'foo',
Chris@0 144 'text' => 'Dummy search snippet to display',
Chris@0 145 ],
Chris@0 146 ];
Chris@0 147 $plugins = array_keys($plugin_info);
Chris@0 148 /** @var $entities \Drupal\search\SearchPageInterface[] */
Chris@0 149 $entities = SearchPage::loadMultiple();
Chris@0 150 // Disable all of the search pages.
Chris@0 151 foreach ($entities as $entity) {
Chris@0 152 $entity->disable()->save();
Chris@0 153 }
Chris@0 154
Chris@0 155 // Test each plugin if it's enabled as the only search plugin.
Chris@0 156 foreach ($entities as $entity_id => $entity) {
Chris@0 157 $this->setDefaultThroughUi($entity_id);
Chris@0 158
Chris@0 159 // Run a search from the correct search URL.
Chris@0 160 $info = $plugin_info[$entity_id];
Chris@0 161 $this->drupalGet('search/' . $entity->getPath(), ['query' => ['keys' => $info['keys']]]);
Chris@0 162 $this->assertResponse(200);
Chris@0 163 $this->assertNoText('no results', $entity->label() . ' search found results');
Chris@0 164 $this->assertText($info['text'], 'Correct search text found');
Chris@0 165
Chris@0 166 // Verify that other plugin search tab labels are not visible.
Chris@0 167 foreach ($plugins as $other) {
Chris@0 168 if ($other != $entity_id) {
Chris@0 169 $label = $entities[$other]->label();
Chris@0 170 $this->assertNoText($label, $label . ' search tab is not shown');
Chris@0 171 }
Chris@0 172 }
Chris@0 173
Chris@0 174 // Run a search from the search block on the node page. Verify you get
Chris@0 175 // to this plugin's search results page.
Chris@0 176 $terms = ['keys' => $info['keys']];
Chris@0 177 $this->submitGetForm('node', $terms, t('Search'));
Chris@0 178 $current = $this->getURL();
Chris@0 179 $expected = \Drupal::url('search.view_' . $entity->id(), [], ['query' => ['keys' => $info['keys']], 'absolute' => TRUE]);
Chris@0 180 $this->assertEqual($current, $expected, 'Block redirected to right search page');
Chris@0 181
Chris@0 182 // Try an invalid search path, which should 404.
Chris@0 183 $this->drupalGet('search/not_a_plugin_path');
Chris@0 184 $this->assertResponse(404);
Chris@0 185
Chris@0 186 $entity->disable()->save();
Chris@0 187 }
Chris@0 188
Chris@0 189 // Set the node search as default.
Chris@0 190 $this->setDefaultThroughUi('node_search');
Chris@0 191
Chris@0 192 // Test with all search plugins enabled. When you go to the search
Chris@0 193 // page or run search, all plugins should be shown.
Chris@0 194 foreach ($entities as $entity) {
Chris@0 195 $entity->enable()->save();
Chris@0 196 }
Chris@0 197
Chris@0 198 \Drupal::service('router.builder')->rebuild();
Chris@0 199
Chris@0 200 $paths = [
Chris@0 201 ['path' => 'search/node', 'options' => ['query' => ['keys' => 'pizza']]],
Chris@0 202 ['path' => 'search/node', 'options' => []],
Chris@0 203 ];
Chris@0 204
Chris@0 205 foreach ($paths as $item) {
Chris@0 206 $this->drupalGet($item['path'], $item['options']);
Chris@0 207 foreach ($plugins as $entity_id) {
Chris@0 208 $label = $entities[$entity_id]->label();
Chris@0 209 $this->assertText($label, format_string('%label search tab is shown', ['%label' => $label]));
Chris@0 210 }
Chris@0 211 }
Chris@0 212 }
Chris@0 213
Chris@0 214 /**
Chris@0 215 * Tests the ordering of search pages on a clean install.
Chris@0 216 */
Chris@0 217 public function testDefaultSearchPageOrdering() {
Chris@0 218 $this->drupalGet('search');
Chris@0 219 $elements = $this->xpath('//*[contains(@class, :class)]//a', [':class' => 'tabs primary']);
Chris@0 220 $this->assertIdentical((string) $elements[0]['href'], \Drupal::url('search.view_node_search'));
Chris@0 221 $this->assertIdentical((string) $elements[1]['href'], \Drupal::url('search.view_dummy_search_type'));
Chris@0 222 $this->assertIdentical((string) $elements[2]['href'], \Drupal::url('search.view_user_search'));
Chris@0 223 }
Chris@0 224
Chris@0 225 /**
Chris@0 226 * Tests multiple search pages of the same type.
Chris@0 227 */
Chris@0 228 public function testMultipleSearchPages() {
Chris@0 229 $this->assertDefaultSearch('node_search', 'The default page is set to the installer default.');
Chris@0 230 $search_storage = \Drupal::entityManager()->getStorage('search_page');
Chris@0 231 $entities = $search_storage->loadMultiple();
Chris@0 232 $search_storage->delete($entities);
Chris@0 233 $this->assertDefaultSearch(FALSE);
Chris@0 234
Chris@0 235 // Ensure that no search pages are configured.
Chris@0 236 $this->drupalGet('admin/config/search/pages');
Chris@0 237 $this->assertText(t('No search pages have been configured.'));
Chris@0 238
Chris@0 239 // Add a search page.
Chris@0 240 $edit = [];
Chris@0 241 $edit['search_type'] = 'search_extra_type_search';
Chris@0 242 $this->drupalPostForm(NULL, $edit, t('Add search page'));
Chris@0 243 $this->assertTitle('Add new search page | Drupal');
Chris@0 244
Chris@0 245 $first = [];
Chris@0 246 $first['label'] = $this->randomString();
Chris@0 247 $first_id = $first['id'] = strtolower($this->randomMachineName(8));
Chris@0 248 $first['path'] = strtolower($this->randomMachineName(8));
Chris@0 249 $this->drupalPostForm(NULL, $first, t('Save'));
Chris@0 250 $this->assertDefaultSearch($first_id, 'The default page matches the only search page.');
Chris@0 251 $this->assertRaw(t('The %label search page has been added.', ['%label' => $first['label']]));
Chris@0 252
Chris@0 253 // Attempt to add a search page with an existing path.
Chris@0 254 $edit = [];
Chris@0 255 $edit['search_type'] = 'search_extra_type_search';
Chris@0 256 $this->drupalPostForm(NULL, $edit, t('Add search page'));
Chris@0 257 $edit = [];
Chris@0 258 $edit['label'] = $this->randomString();
Chris@0 259 $edit['id'] = strtolower($this->randomMachineName(8));
Chris@0 260 $edit['path'] = $first['path'];
Chris@0 261 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 262 $this->assertText(t('The search page path must be unique.'));
Chris@0 263
Chris@0 264 // Add a second search page.
Chris@0 265 $second = [];
Chris@0 266 $second['label'] = $this->randomString();
Chris@0 267 $second_id = $second['id'] = strtolower($this->randomMachineName(8));
Chris@0 268 $second['path'] = strtolower($this->randomMachineName(8));
Chris@0 269 $this->drupalPostForm(NULL, $second, t('Save'));
Chris@0 270 $this->assertDefaultSearch($first_id, 'The default page matches the only search page.');
Chris@0 271
Chris@0 272 // Ensure both search pages have their tabs displayed.
Chris@0 273 $this->drupalGet('search');
Chris@0 274 $elements = $this->xpath('//*[contains(@class, :class)]//a', [':class' => 'tabs primary']);
Chris@0 275 $this->assertIdentical((string) $elements[0]['href'], Url::fromRoute('search.view_' . $first_id)->toString());
Chris@0 276 $this->assertIdentical((string) $elements[1]['href'], Url::fromRoute('search.view_' . $second_id)->toString());
Chris@0 277
Chris@0 278 // Switch the weight of the search pages and check the order of the tabs.
Chris@0 279 $edit = [
Chris@0 280 'entities[' . $first_id . '][weight]' => 10,
Chris@0 281 'entities[' . $second_id . '][weight]' => -10,
Chris@0 282 ];
Chris@0 283 $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
Chris@0 284 $this->drupalGet('search');
Chris@0 285 $elements = $this->xpath('//*[contains(@class, :class)]//a', [':class' => 'tabs primary']);
Chris@0 286 $this->assertIdentical((string) $elements[0]['href'], Url::fromRoute('search.view_' . $second_id)->toString());
Chris@0 287 $this->assertIdentical((string) $elements[1]['href'], Url::fromRoute('search.view_' . $first_id)->toString());
Chris@0 288
Chris@0 289 // Check the initial state of the search pages.
Chris@0 290 $this->drupalGet('admin/config/search/pages');
Chris@0 291 $this->verifySearchPageOperations($first_id, TRUE, FALSE, FALSE, FALSE);
Chris@0 292 $this->verifySearchPageOperations($second_id, TRUE, TRUE, TRUE, FALSE);
Chris@0 293
Chris@0 294 // Change the default search page.
Chris@0 295 $this->clickLink(t('Set as default'));
Chris@0 296 $this->assertRaw(t('The default search page is now %label. Be sure to check the ordering of your search pages.', ['%label' => $second['label']]));
Chris@0 297 $this->verifySearchPageOperations($first_id, TRUE, TRUE, TRUE, FALSE);
Chris@0 298 $this->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);
Chris@0 299
Chris@0 300 // Disable the first search page.
Chris@0 301 $this->clickLink(t('Disable'));
Chris@0 302 $this->assertResponse(200);
Chris@0 303 $this->assertNoLink(t('Disable'));
Chris@0 304 $this->verifySearchPageOperations($first_id, TRUE, TRUE, FALSE, TRUE);
Chris@0 305 $this->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);
Chris@0 306
Chris@0 307 // Enable the first search page.
Chris@0 308 $this->clickLink(t('Enable'));
Chris@0 309 $this->assertResponse(200);
Chris@0 310 $this->verifySearchPageOperations($first_id, TRUE, TRUE, TRUE, FALSE);
Chris@0 311 $this->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);
Chris@0 312
Chris@0 313 // Test deleting.
Chris@0 314 $this->clickLink(t('Delete'));
Chris@0 315 $this->assertRaw(t('Are you sure you want to delete the search page %label?', ['%label' => $first['label']]));
Chris@0 316 $this->drupalPostForm(NULL, [], t('Delete'));
Chris@0 317 $this->assertRaw(t('The search page %label has been deleted.', ['%label' => $first['label']]));
Chris@0 318 $this->verifySearchPageOperations($first_id, FALSE, FALSE, FALSE, FALSE);
Chris@0 319 }
Chris@0 320
Chris@0 321 /**
Chris@0 322 * Tests that the enable/disable/default routes are protected from CSRF.
Chris@0 323 */
Chris@0 324 public function testRouteProtection() {
Chris@0 325 // Ensure that the enable and disable routes are protected.
Chris@0 326 $this->drupalGet('admin/config/search/pages/manage/node_search/enable');
Chris@0 327 $this->assertResponse(403);
Chris@0 328 $this->drupalGet('admin/config/search/pages/manage/node_search/disable');
Chris@0 329 $this->assertResponse(403);
Chris@0 330 $this->drupalGet('admin/config/search/pages/manage/node_search/set-default');
Chris@0 331 $this->assertResponse(403);
Chris@0 332 }
Chris@0 333
Chris@0 334 /**
Chris@0 335 * Checks that the search page operations match expectations.
Chris@0 336 *
Chris@0 337 * @param string $id
Chris@0 338 * The search page ID to check.
Chris@0 339 * @param bool $edit
Chris@0 340 * Whether the edit link is expected.
Chris@0 341 * @param bool $delete
Chris@0 342 * Whether the delete link is expected.
Chris@0 343 * @param bool $disable
Chris@0 344 * Whether the disable link is expected.
Chris@0 345 * @param bool $enable
Chris@0 346 * Whether the enable link is expected.
Chris@0 347 */
Chris@0 348 protected function verifySearchPageOperations($id, $edit, $delete, $disable, $enable) {
Chris@0 349 if ($edit) {
Chris@0 350 $this->assertLinkByHref("admin/config/search/pages/manage/$id");
Chris@0 351 }
Chris@0 352 else {
Chris@0 353 $this->assertNoLinkByHref("admin/config/search/pages/manage/$id");
Chris@0 354 }
Chris@0 355 if ($delete) {
Chris@0 356 $this->assertLinkByHref("admin/config/search/pages/manage/$id/delete");
Chris@0 357 }
Chris@0 358 else {
Chris@0 359 $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/delete");
Chris@0 360 }
Chris@0 361 if ($disable) {
Chris@0 362 $this->assertLinkByHref("admin/config/search/pages/manage/$id/disable");
Chris@0 363 }
Chris@0 364 else {
Chris@0 365 $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/disable");
Chris@0 366 }
Chris@0 367 if ($enable) {
Chris@0 368 $this->assertLinkByHref("admin/config/search/pages/manage/$id/enable");
Chris@0 369 }
Chris@0 370 else {
Chris@0 371 $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/enable");
Chris@0 372 }
Chris@0 373 }
Chris@0 374
Chris@0 375 /**
Chris@0 376 * Checks that the default search page matches expectations.
Chris@0 377 *
Chris@0 378 * @param string $expected
Chris@0 379 * The expected search page.
Chris@0 380 * @param string $message
Chris@0 381 * (optional) A message to display with the assertion.
Chris@0 382 * @param string $group
Chris@0 383 * (optional) The group this message is in.
Chris@0 384 */
Chris@0 385 protected function assertDefaultSearch($expected, $message = '', $group = 'Other') {
Chris@0 386 /** @var $search_page_repository \Drupal\search\SearchPageRepositoryInterface */
Chris@0 387 $search_page_repository = \Drupal::service('search.search_page_repository');
Chris@0 388 $this->assertIdentical($search_page_repository->getDefaultSearchPage(), $expected, $message, $group);
Chris@0 389 }
Chris@0 390
Chris@0 391 /**
Chris@0 392 * Sets a search page as the default in the UI.
Chris@0 393 *
Chris@0 394 * @param string $entity_id
Chris@0 395 * The search page entity ID to enable.
Chris@0 396 */
Chris@0 397 protected function setDefaultThroughUi($entity_id) {
Chris@0 398 $this->drupalGet('admin/config/search/pages');
Chris@0 399 preg_match('|href="([^"]+' . $entity_id . '/set-default[^"]+)"|', $this->getRawContent(), $matches);
Chris@0 400
Chris@0 401 $this->drupalGet($this->getAbsoluteUrl($matches[1]));
Chris@0 402 }
Chris@0 403
Chris@0 404 }