SearchConfigSettingsFormTest.php 14.3 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 42 43 44 45 46 47 48 49

    // Login as a user that can create and search content.
    $this->search_user = $this->drupalCreateUser(array('search content', 'administer search', 'administer nodes', 'bypass node access', 'access user profiles', 'administer users', 'administer blocks'));
    $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 88 89
    $this->assertNoText(t('The configuration options have been saved.'), 'Form does not save with an invalid word length.');
  }

90
  /**
91
   * Verifies plugin-supplied settings form.
92 93
   */
  function testSearchModuleSettingsPage() {
94
    $this->drupalGet('admin/config/search/pages');
95
    $this->clickLink(t('Edit'), 1);
96 97 98 99 100 101 102 103

    // 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',
    );
104
    $this->drupalPostForm(NULL, $edit, t('Save search page'));
105 106

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

112
  /**
113
   * Verifies that you can disable individual search plugins.
114 115
   */
  function testSearchModuleDisabling() {
116 117
    // 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.
118 119
    $plugin_info = array(
      'node_search' => array(
120
        'keys' => 'pizza',
121
        'text' => $this->search_node->label(),
122
      ),
123
      'user_search' => array(
124 125
        'keys' => $this->search_user->getUsername(),
        'text' => $this->search_user->getEmail(),
126
      ),
127
      'dummy_search_type' => array(
128 129 130 131
        'keys' => 'foo',
        'text' => 'Dummy search snippet to display',
      ),
    );
132
    $plugins = array_keys($plugin_info);
133 134 135 136 137 138
    /** @var $entities \Drupal\search\SearchPageInterface[] */
    $entities = entity_load_multiple('search_page');
    // Disable all of the search pages.
    foreach ($entities as $entity) {
      $entity->disable()->save();
    }
139

140
    // Test each plugin if it's enabled as the only search plugin.
141 142
    foreach ($entities as $entity_id => $entity) {
      // Set this as default.
143
      $this->drupalGet("admin/config/search/pages/manage/$entity_id/set-default");
144 145

      // Run a search from the correct search URL.
146
      $info = $plugin_info[$entity_id];
147
      $this->drupalGet('search/' . $entity->getPath(), array('query' => array('keys' => $info['keys'])));
148 149
      $this->assertResponse(200);
      $this->assertNoText('no results', $entity->label() . ' search found results');
150 151
      $this->assertText($info['text'], 'Correct search text found');

152
      // Verify that other plugin search tab labels are not visible.
153
      foreach ($plugins as $other) {
154 155 156
        if ($other != $entity_id) {
          $label = $entities[$other]->label();
          $this->assertNoText($label, $label . ' search tab is not shown');
157 158 159 160
        }
      }

      // Run a search from the search block on the node page. Verify you get
161
      // to this plugin's search results page.
162 163 164 165 166
      $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');
167

168
      // Try an invalid search path, which should 404.
169
      $this->drupalGet('search/not_a_plugin_path');
170 171 172
      $this->assertResponse(404);

      $entity->disable()->save();
173 174
    }

175 176
    // Test with all search plugins enabled. When you go to the search
    // page or run search, all plugins should be shown.
177 178
    foreach ($entities as $entity) {
      $entity->enable()->save();
179
    }
180
    // Set the node search as default.
181
    $this->drupalGet('admin/config/search/pages/manage/node_search/set-default');
182

183 184 185 186 187 188 189
    $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']);
190 191 192
      foreach ($plugins as $entity_id) {
        $label = $entities[$entity_id]->label();
        $this->assertText($label, format_string('%label search tab is shown', array('%label' => $label)));
193 194 195
      }
    }
  }
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

  /**
   * 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.');
212
    $search_storage = \Drupal::entityManager()->getStorage('search_page');
213 214 215 216 217
    $entities = $search_storage->loadMultiple();
    $search_storage->delete($entities);
    $this->assertDefaultSearch(FALSE);

    // Ensure that no search pages are configured.
218
    $this->drupalGet('admin/config/search/pages');
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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
    $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,
    );
265
    $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
266 267 268 269 270 271
    $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.
272
    $this->drupalGet('admin/config/search/pages');
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 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
    $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) {
319
      $this->assertLinkByHref("admin/config/search/pages/manage/$id");
320 321
    }
    else {
322
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id");
323 324
    }
    if ($delete) {
325
      $this->assertLinkByHref("admin/config/search/pages/manage/$id/delete");
326 327
    }
    else {
328
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/delete");
329 330
    }
    if ($disable) {
331
      $this->assertLinkByHref("admin/config/search/pages/manage/$id/disable");
332 333
    }
    else {
334
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/disable");
335 336
    }
    if ($enable) {
337
      $this->assertLinkByHref("admin/config/search/pages/manage/$id/enable");
338 339
    }
    else {
340
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/enable");
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    }
  }

  /**
   * 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);
  }

360
}