SearchConfigSettingsFormTest.php 15 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\search\Tests\SearchConfigSettingsFormTest.
6 7 8 9 10
 */

namespace Drupal\search\Tests;

/**
11 12 13
 * Verify the search config settings form.
 *
 * @group search
14 15
 */
class SearchConfigSettingsFormTest extends SearchTestBase {
16 17 18 19 20 21 22 23

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('block', 'search_extra_type');

24 25 26 27 28
  /**
   * User who can search and administer search.
   *
   * @var \Drupal\user\UserInterface
   */
29
  public $search_user;
30 31 32 33 34 35

  /**
   * Node indexed for searching.
   *
   * @var \Drupal\node\NodeInterface
   */
36 37 38
  public $search_node;

  function setUp() {
39
    parent::setUp();
40 41

    // Login as a user that can create and search content.
42
    $this->search_user = $this->drupalCreateUser(array('search content', 'administer search', 'administer nodes', 'bypass node access', 'access user profiles', 'administer users', 'administer blocks', 'access site reports'));
43 44 45 46 47 48 49
    $this->drupalLogin($this->search_user);

    // Add a single piece of content and index it.
    $node = $this->drupalCreateNode();
    $this->search_node = $node;
    // Link the node to itself to test that it's only indexed once. The content
    // also needs the word "pizza" so we can use it as the search keyword.
50
    $body_key = 'body[0][value]';
51
    $edit[$body_key] = l($node->label(), 'node/' . $node->id()) . ' pizza sandwich';
52
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
53

54
    $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
55 56 57
    search_update_totals();

    // Enable the search block.
58
    $this->drupalPlaceBlock('search_form_block');
59 60 61
  }

  /**
62
   * Verifies the search settings form.
63 64 65 66
   */
  function testSearchSettingsPage() {

    // Test that the settings form displays the correct count of items left to index.
67
    $this->drupalGet('admin/config/search/pages');
68 69 70
    $this->assertText(t('There are @count items left to index.', array('@count' => 0)));

    // Test the re-index button.
71
    $this->drupalPostForm('admin/config/search/pages', array(), t('Re-index site'));
72
    $this->assertText(t('Are you sure you want to re-index the site'));
73
    $this->drupalPostForm('admin/config/search/pages/reindex', array(), t('Re-index site'));
74
    $this->assertText(t('The index will be rebuilt'));
75
    $this->drupalGet('admin/config/search/pages');
76 77 78
    $this->assertText(t('There is 1 item left to index.'));

    // Test that the form saves with the default values.
79
    $this->drupalPostForm('admin/config/search/pages', array(), t('Save configuration'));
80 81 82 83 84 85
    $this->assertText(t('The configuration options have been saved.'), 'Form saves with the default values.');

    // Test that the form does not save with an invalid word length.
    $edit = array(
      'minimum_word_size' => $this->randomName(3),
    );
86
    $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
87
    $this->assertNoText(t('The configuration options have been saved.'), 'Form does not save with an invalid word length.');
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

    // Test logging setting. It should be off by default.
    $text = $this->randomName(5);
    $this->drupalPostForm('search/node', array('keys' => $text), t('Search'));
    $this->drupalGet('admin/reports/dblog');
    $this->assertNoLink('Searched Content for ' . $text . '.', 'Search was not logged');

    // Turn on logging.
    $edit = array('logging' => TRUE);
    $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
    $text = $this->randomName(5);
    $this->drupalPostForm('search/node', array('keys' => $text), t('Search'));
    $this->drupalGet('admin/reports/dblog');
    $this->assertLink('Searched Content for ' . $text . '.', 0, 'Search was logged');

103 104
  }

105
  /**
106
   * Verifies plugin-supplied settings form.
107 108
   */
  function testSearchModuleSettingsPage() {
109
    $this->drupalGet('admin/config/search/pages');
110
    $this->clickLink(t('Edit'), 1);
111 112 113 114 115 116 117 118

    // Ensure that the default setting was picked up from the default config
    $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');

    // Change extra type setting and also modify a common search setting.
    $edit = array(
      'extra_type_settings[boost]' => 'ii',
    );
119
    $this->drupalPostForm(NULL, $edit, t('Save search page'));
120 121

    // Ensure that the modifications took effect.
122
    $this->assertRaw(t('The %label search page has been updated.', array('%label' => 'Dummy search type')));
123
    $this->drupalGet('admin/config/search/pages/manage/dummy_search_type');
124 125 126
    $this->assertTrue($this->xpath('//select[@id="edit-extra-type-settings-boost"]//option[@value="ii" and @selected="selected"]'), 'Module specific settings can be changed');
  }

127
  /**
128
   * Verifies that you can disable individual search plugins.
129 130
   */
  function testSearchModuleDisabling() {
131 132
    // Array of search plugins to test: 'keys' are the keywords to search for,
    // and 'text' is the text to assert is on the results page.
133 134
    $plugin_info = array(
      'node_search' => array(
135
        'keys' => 'pizza',
136
        'text' => $this->search_node->label(),
137
      ),
138
      'user_search' => array(
139 140
        'keys' => $this->search_user->getUsername(),
        'text' => $this->search_user->getEmail(),
141
      ),
142
      'dummy_search_type' => array(
143 144 145 146
        'keys' => 'foo',
        'text' => 'Dummy search snippet to display',
      ),
    );
147
    $plugins = array_keys($plugin_info);
148 149 150 151 152 153
    /** @var $entities \Drupal\search\SearchPageInterface[] */
    $entities = entity_load_multiple('search_page');
    // Disable all of the search pages.
    foreach ($entities as $entity) {
      $entity->disable()->save();
    }
154

155
    // Test each plugin if it's enabled as the only search plugin.
156 157
    foreach ($entities as $entity_id => $entity) {
      // Set this as default.
158
      $this->drupalGet("admin/config/search/pages/manage/$entity_id/set-default");
159 160

      // Run a search from the correct search URL.
161
      $info = $plugin_info[$entity_id];
162
      $this->drupalGet('search/' . $entity->getPath(), array('query' => array('keys' => $info['keys'])));
163 164
      $this->assertResponse(200);
      $this->assertNoText('no results', $entity->label() . ' search found results');
165 166
      $this->assertText($info['text'], 'Correct search text found');

167
      // Verify that other plugin search tab labels are not visible.
168
      foreach ($plugins as $other) {
169 170 171
        if ($other != $entity_id) {
          $label = $entities[$other]->label();
          $this->assertNoText($label, $label . ' search tab is not shown');
172 173 174 175
        }
      }

      // Run a search from the search block on the node page. Verify you get
176
      // to this plugin's search results page.
177 178 179 180 181
      $terms = array('keys' => $info['keys']);
      $this->submitGetForm('node', $terms, t('Search'));
      $current = $this->getURL();
      $expected = \Drupal::url('search.view_' . $entity->id(), array(), array('query' => array('keys' => $info['keys']), 'absolute' => TRUE));
      $this->assertEqual( $current, $expected, 'Block redirected to right search page');
182

183
      // Try an invalid search path, which should 404.
184
      $this->drupalGet('search/not_a_plugin_path');
185 186 187
      $this->assertResponse(404);

      $entity->disable()->save();
188 189
    }

190 191
    // Test with all search plugins enabled. When you go to the search
    // page or run search, all plugins should be shown.
192 193
    foreach ($entities as $entity) {
      $entity->enable()->save();
194
    }
195
    // Set the node search as default.
196
    $this->drupalGet('admin/config/search/pages/manage/node_search/set-default');
197

198 199 200 201 202 203 204
    $paths = array(
      array('path' => 'search/node', 'options' => array('query' => array('keys' => 'pizza'))),
      array('path' => 'search/node', 'options' => array()),
    );

    foreach ($paths as $item) {
      $this->drupalGet($item['path'], $item['options']);
205 206 207
      foreach ($plugins as $entity_id) {
        $label = $entities[$entity_id]->label();
        $this->assertText($label, format_string('%label search tab is shown', array('%label' => $label)));
208 209 210
      }
    }
  }
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

  /**
   * Tests the ordering of search pages on a clean install.
   */
  public function testDefaultSearchPageOrdering() {
    $this->drupalGet('search');
    $elements = $this->xpath('//*[contains(@class, :class)]//a', array(':class' => 'tabs primary'));
    $this->assertIdentical((string) $elements[0]['href'], url('search/node'));
    $this->assertIdentical((string) $elements[1]['href'], url('search/user'));
  }

  /**
   * Tests multiple search pages of the same type.
   */
  public function testMultipleSearchPages() {
    $this->assertDefaultSearch('node_search', 'The default page is set to the installer default.');
227
    $search_storage = \Drupal::entityManager()->getStorage('search_page');
228 229 230 231 232
    $entities = $search_storage->loadMultiple();
    $search_storage->delete($entities);
    $this->assertDefaultSearch(FALSE);

    // Ensure that no search pages are configured.
233
    $this->drupalGet('admin/config/search/pages');
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    $this->assertText(t('No search pages have been configured.'));

    // Add a search page.
    $edit = array();
    $edit['search_type'] = 'search_extra_type_search';
    $this->drupalPostForm(NULL, $edit, t('Add new page'));
    $this->assertTitle('Add new search page | Drupal');

    $first = array();
    $first['label'] = $this->randomString();
    $first_id = $first['id'] = strtolower($this->randomName(8));
    $first['path'] = strtolower($this->randomName(8));
    $this->drupalPostForm(NULL, $first, t('Add search page'));
    $this->assertDefaultSearch($first_id, 'The default page matches the only search page.');
    $this->assertRaw(t('The %label search page has been added.', array('%label' => $first['label'])));

    // Attempt to add a search page with an existing path.
    $edit = array();
    $edit['search_type'] = 'search_extra_type_search';
    $this->drupalPostForm(NULL, $edit, t('Add new page'));
    $edit = array();
    $edit['label'] = $this->randomString();
    $edit['id'] = strtolower($this->randomName(8));
    $edit['path'] = $first['path'];
    $this->drupalPostForm(NULL, $edit, t('Add search page'));
    $this->assertText(t('The search page path must be unique.'));

    // Add a second search page.
    $second = array();
    $second['label'] = $this->randomString();
    $second_id = $second['id'] = strtolower($this->randomName(8));
    $second['path'] = strtolower($this->randomName(8));
    $this->drupalPostForm(NULL, $second, t('Add search page'));
    $this->assertDefaultSearch($first_id, 'The default page matches the only search page.');

    // Ensure both search pages have their tabs displayed.
    $this->drupalGet('search');
    $elements = $this->xpath('//*[contains(@class, :class)]//a', array(':class' => 'tabs primary'));
    $this->assertIdentical((string) $elements[0]['href'], url('search/' . $first['path']));
    $this->assertIdentical((string) $elements[1]['href'], url('search/' . $second['path']));

    // Switch the weight of the search pages and check the order of the tabs.
    $edit = array(
      'entities[' . $first_id . '][weight]' => 10,
      'entities[' . $second_id . '][weight]' => -10,
    );
280
    $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
281 282 283 284 285 286
    $this->drupalGet('search');
    $elements = $this->xpath('//*[contains(@class, :class)]//a', array(':class' => 'tabs primary'));
    $this->assertIdentical((string) $elements[0]['href'], url('search/' . $second['path']));
    $this->assertIdentical((string) $elements[1]['href'], url('search/' . $first['path']));

    // Check the initial state of the search pages.
287
    $this->drupalGet('admin/config/search/pages');
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    $this->verifySearchPageOperations($first_id, TRUE, FALSE, FALSE, FALSE);
    $this->verifySearchPageOperations($second_id, TRUE, TRUE, TRUE, FALSE);

    // Change the default search page.
    $this->clickLink(t('Set as default'));
    $this->assertRaw(t('The default search page is now %label. Be sure to check the ordering of your search pages.', array('%label' => $second['label'])));
    $this->verifySearchPageOperations($first_id, TRUE, TRUE, TRUE, FALSE);
    $this->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);

    // Disable the first search page.
    $this->clickLink(t('Disable'));
    $this->assertResponse(200);
    $this->assertNoLink(t('Disable'));
    $this->verifySearchPageOperations($first_id, TRUE, TRUE, FALSE, TRUE);
    $this->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);

    // Enable the first search page.
    $this->clickLink(t('Enable'));
    $this->assertResponse(200);
    $this->verifySearchPageOperations($first_id, TRUE, TRUE, TRUE, FALSE);
    $this->verifySearchPageOperations($second_id, TRUE, FALSE, FALSE, FALSE);

    // Test deleting.
    $this->clickLink(t('Delete'));
    $this->assertRaw(t('Are you sure you want to delete the %label search page?', array('%label' => $first['label'])));
    $this->drupalPostForm(NULL, array(), t('Delete'));
    $this->assertRaw(t('The %label search page has been deleted.', array('%label' => $first['label'])));
    $this->verifySearchPageOperations($first_id, FALSE, FALSE, FALSE, FALSE);
  }

  /**
   * Checks that the search page operations match expectations.
   *
   * @param string $id
   *   The search page ID to check.
   * @param bool $edit
   *   Whether the edit link is expected.
   * @param bool $delete
   *   Whether the delete link is expected.
   * @param bool $disable
   *   Whether the disable link is expected.
   * @param bool $enable
   *   Whether the enable link is expected.
   */
  protected function verifySearchPageOperations($id, $edit, $delete, $disable, $enable) {
    if ($edit) {
334
      $this->assertLinkByHref("admin/config/search/pages/manage/$id");
335 336
    }
    else {
337
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id");
338 339
    }
    if ($delete) {
340
      $this->assertLinkByHref("admin/config/search/pages/manage/$id/delete");
341 342
    }
    else {
343
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/delete");
344 345
    }
    if ($disable) {
346
      $this->assertLinkByHref("admin/config/search/pages/manage/$id/disable");
347 348
    }
    else {
349
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/disable");
350 351
    }
    if ($enable) {
352
      $this->assertLinkByHref("admin/config/search/pages/manage/$id/enable");
353 354
    }
    else {
355
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/enable");
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    }
  }

  /**
   * Checks that the default search page matches expectations.
   *
   * @param string $expected
   *   The expected search page.
   * @param string $message
   *   (optional) A message to display with the assertion.
   * @param string $group
   *   (optional) The group this message is in.
   */
  protected function assertDefaultSearch($expected, $message = '', $group = 'Other') {
    /** @var $search_page_repository \Drupal\search\SearchPageRepositoryInterface */
    $search_page_repository = \Drupal::service('search.search_page_repository');
    $this->assertIdentical($search_page_repository->getDefaultSearchPage(), $expected, $message, $group);
  }

375
}