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

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

namespace Drupal\search\Tests;

/**
 * Test config page.
 */
class SearchConfigSettingsFormTest extends SearchTestBase {
14 15 16 17 18 19 20 21

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

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

  /**
   * Node indexed for searching.
   *
   * @var \Drupal\node\NodeInterface
   */
34 35 36 37 38 39 40 41 42 43 44
  public $search_node;

  public static function getInfo() {
    return array(
      'name' => 'Config settings form',
      'description' => 'Verify the search config settings form.',
      'group' => 'Search',
    );
  }

  function setUp() {
45
    parent::setUp();
46 47 48 49 50 51 52 53 54 55

    // 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.
56
    $body_key = 'body[0][value]';
57
    $edit[$body_key] = l($node->label(), 'node/' . $node->id()) . ' pizza sandwich';
58
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
59

60
    $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
61 62 63
    search_update_totals();

    // Enable the search block.
64
    $this->drupalPlaceBlock('search_form_block');
65 66 67
  }

  /**
68
   * Verifies the search settings form.
69 70 71 72
   */
  function testSearchSettingsPage() {

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

    // Test the re-index button.
77
    $this->drupalPostForm('admin/config/search/pages', array(), t('Re-index site'));
78
    $this->assertText(t('Are you sure you want to re-index the site'));
79
    $this->drupalPostForm('admin/config/search/pages/reindex', array(), t('Re-index site'));
80
    $this->assertText(t('The index will be rebuilt'));
81
    $this->drupalGet('admin/config/search/pages');
82 83 84
    $this->assertText(t('There is 1 item left to index.'));

    // Test that the form saves with the default values.
85
    $this->drupalPostForm('admin/config/search/pages', array(), t('Save configuration'));
86 87 88 89 90 91
    $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),
    );
92
    $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
93 94 95
    $this->assertNoText(t('The configuration options have been saved.'), 'Form does not save with an invalid word length.');
  }

96
  /**
97
   * Verifies plugin-supplied settings form.
98 99
   */
  function testSearchModuleSettingsPage() {
100
    $this->drupalGet('admin/config/search/pages');
101
    $this->clickLink(t('Edit'), 1);
102 103 104 105 106 107 108 109

    // 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',
    );
110
    $this->drupalPostForm(NULL, $edit, t('Save search page'));
111 112

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

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

146
    // Test each plugin if it's enabled as the only search plugin.
147 148
    foreach ($entities as $entity_id => $entity) {
      // Set this as default.
149
      $this->drupalGet("admin/config/search/pages/manage/$entity_id/set-default");
150 151

      // Run a search from the correct search URL.
152
      $info = $plugin_info[$entity_id];
153
      $this->drupalGet('search/' . $entity->getPath(), array('query' => array('keys' => $info['keys'])));
154 155
      $this->assertResponse(200);
      $this->assertNoText('no results', $entity->label() . ' search found results');
156 157
      $this->assertText($info['text'], 'Correct search text found');

158
      // Verify that other plugin search tab labels are not visible.
159
      foreach ($plugins as $other) {
160 161 162
        if ($other != $entity_id) {
          $label = $entities[$other]->label();
          $this->assertNoText($label, $label . ' search tab is not shown');
163 164 165 166
        }
      }

      // Run a search from the search block on the node page. Verify you get
167
      // to this plugin's search results page.
168 169 170 171 172
      $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');
173

174
      // Try an invalid search path, which should 404.
175
      $this->drupalGet('search/not_a_plugin_path');
176 177 178
      $this->assertResponse(404);

      $entity->disable()->save();
179 180
    }

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

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

  /**
   * 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.');
218
    $search_storage = \Drupal::entityManager()->getStorage('search_page');
219 220 221 222 223
    $entities = $search_storage->loadMultiple();
    $search_storage->delete($entities);
    $this->assertDefaultSearch(FALSE);

    // Ensure that no search pages are configured.
224
    $this->drupalGet('admin/config/search/pages');
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 265 266 267 268 269 270
    $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,
    );
271
    $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
272 273 274 275 276 277
    $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.
278
    $this->drupalGet('admin/config/search/pages');
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 319 320 321 322 323 324
    $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) {
325
      $this->assertLinkByHref("admin/config/search/pages/manage/$id");
326 327
    }
    else {
328
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id");
329 330
    }
    if ($delete) {
331
      $this->assertLinkByHref("admin/config/search/pages/manage/$id/delete");
332 333
    }
    else {
334
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/delete");
335 336
    }
    if ($disable) {
337
      $this->assertLinkByHref("admin/config/search/pages/manage/$id/disable");
338 339
    }
    else {
340
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/disable");
341 342
    }
    if ($enable) {
343
      $this->assertLinkByHref("admin/config/search/pages/manage/$id/enable");
344 345
    }
    else {
346
      $this->assertNoLinkByHref("admin/config/search/pages/manage/$id/enable");
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    }
  }

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

366
}