IntegrationTest.php 16.5 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\core_search_facets\Tests;

5
use Drupal\facets\Tests\ExampleContentTrait;
6
use Drupal\node\Entity\Node;
7

8 9 10 11 12
/**
 * Tests the admin UI with the core search facet source.
 *
 * @group core_search_facets
 */
borisson_'s avatar
borisson_ committed
13
class IntegrationTest extends WebTestBase {
14

15 16
  use ExampleContentTrait;

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
  /**
   * The block entities used by this test.
   *
   * @var \Drupal\block\BlockInterface[]
   */
  protected $blocks;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->drupalLogin($this->adminUser);

    // Index the content.
    \Drupal::service('plugin.manager.search')->createInstance('node_search')->updateIndex();
    search_update_totals();

36 37
    // Make absolutely sure the ::$blocks variable doesn't pass information
    // along between tests.
38
    $this->blocks = [];
39 40 41 42 43 44
  }

  /**
   * Tests various operations via the Facets' admin UI.
   */
  public function testFramework() {
45

46
    $facet_id = 'test_facet_name';
47
    $facet_name = 'Test Facet Name';
48

borisson_'s avatar
borisson_ committed
49
    // Check if the overview is empty.
50 51 52
    $this->checkEmptyOverview();

    // Add a new facet and edit it.
53
    $this->addFacet($facet_id, $facet_name, 'type');
54 55 56 57 58 59 60 61 62 63 64 65

    // Create and place a block for "Test Facet name" facet.
    $this->createFacetBlock($facet_id);

    // Verify that the facet results are correct.
    $this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
    $this->assertLink('page');
    $this->assertLink('article');

    // Verify that facet blocks appear as expected.
    $this->assertFacetBlocksAppear();

66
    $this->setShowAmountOfResults($facet_id, TRUE);
67 68 69

    // Verify that the number of results per item.
    $this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
70
    $this->assertLink('page (19)');
71 72
    $this->assertLink('article (10)');

73
    // Verify that the label is correct for a clicked link.
74 75
    $this->clickLink('page (19)');
    $this->assertLink('(-) page (19)');
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    // Do not show the block on empty behaviors.
    // Truncate the search_index table because, for the moment, we don't have
    // the possibility to clear the index from the API.
    // See https://www.drupal.org/node/326062
    \Drupal::database()->truncate('search_index')->execute();

    // Verify that no facet blocks appear. Empty behavior "None" is selected by
    // default.
    $this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
    $this->assertNoFacetBlocksAppear();

    // Verify that the "empty_text" appears as expected.
    $this->setEmptyBehaviorFacetText($facet_name);
    $this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
    $this->assertRaw('block-test-facet-name');
    $this->assertRaw('No results found for this block!');

    // Delete the block.
    $this->deleteBlock($facet_id);

    // Delete the facet and make sure the overview is empty again.
98
    $this->deleteUnusedFacet($facet_id, $facet_name);
99
    $this->checkEmptyOverview();
100 101 102 103

  }

  /**
104
   * Tests the "Post date" facets.
105
   */
106
  public function testPostDate() {
borisson_'s avatar
borisson_ committed
107
    $facet_name = 'Tardigrade';
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    $facet_id = 'tardigrade';

    $this->addFacet($facet_id, $facet_name, 'created');
    $this->createFacetBlock($facet_id);
    $this->setShowAmountOfResults($facet_id, TRUE);

    // Assert date facets.
    $this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
    $this->assertLink('February 2016 (9)');
    $this->assertLink('March 2016 (10)');
    $this->assertLink('April 2016 (10)');
    $this->assertResponse(200);

    $this->clickLink('March 2016 (10)');
    $this->assertResponse(200);
    $this->assertLink('March 8, 2016 (1)');
    $this->assertLink('March 9, 2016 (2)');

    $this->clickLink('March 9, 2016 (2)');
    $this->assertResponse(200);
    $this->assertLink('10 AM (1)');
    $this->assertLink('12 PM (1)');

    $this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
    $this->assertLink('April 2016 (10)');
    $this->clickLink('April 2016 (10)');
    $this->assertResponse(200);
    $this->assertLink('April 1, 2016 (1)');
    $this->assertLink('April 2, 2016 (1)');
  }

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  /**
   * Tests the "Updated date" facets.
   */
  public function testUpdatedDate() {
    $facet_name = 'Tardigrade';
    $facet_id = 'tardigrade';

    $this->addFacet($facet_id, $facet_name, 'changed');
    $this->createFacetBlock($facet_id);
    $this->setShowAmountOfResults($facet_id, TRUE);

    // Update the changed date. The nodes were created on February/March 2016
    // and the changed date is December 2016.
    $node = Node::load(1);
    $changed_date = new \DateTime('December 3 2016 1PM');
    $node->changed = $changed_date->format('U');
    $node->save();

    // Index the content.
    \Drupal::service('plugin.manager.search')->createInstance('node_search')->updateIndex();
    search_update_totals();

    $this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
    $this->assertLink('December 2016 (1)');
    $this->clickLink('December 2016 (1)');
    $this->assertResponse(200);
    $this->assertLink('December 3, 2016 (1)');
    $this->clickLink('December 3, 2016 (1)');
    $this->assertResponse(200);

  }

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  /**
   * Tests for CRUD operations.
   */
  public function testCrudFacet() {
    $facet_name = "Test Facet name";
    $facet_id = 'test_facet_name';

    // Check if the overview is empty.
    $this->checkEmptyOverview();

    // Add a new facet and edit it.
    $this->addFacetCheck($facet_id, $facet_name, 'type');
    $this->editFacetCheck($facet_id, $facet_name);

    // Create and place a block.
    $this->createFacetBlock($facet_id);

    // Delete the block.
    $this->deleteBlock($facet_id);

    // Delete the facet.
    $this->deleteUnusedFacet($facet_id, $facet_name);
  }

  /**
   * Creates a new facet.
   *
borisson_'s avatar
borisson_ committed
198
   * @param string $id
199
   *   The facet's id.
borisson_'s avatar
borisson_ committed
200
   * @param string $name
201
   *   The facet's name.
borisson_'s avatar
borisson_ committed
202
   * @param string $type
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
   *   The field type.
   */
  public function addFacet($id, $name, $type) {
    $this->drupalGet('admin/config/search/facets/add-facet');
    $form_values = [
      'id' => $id,
      'status' => 1,
      'url_alias' => $id,
      'name' => $name,
      'weight' => 2,
      'facet_source_id' => 'core_node_search:node_search',
      'facet_source_configs[core_node_search:node_search][field_identifier]' => $type,
    ];
    $this->drupalPostForm(NULL, ['facet_source_id' => 'core_node_search:node_search'], $this->t('Configure facet source'));
    $this->drupalPostForm(NULL, $form_values, $this->t('Save'));
218 219 220 221 222
  }

  /**
   * Configures the possibility to show the amount of results for facet blocks.
   *
223 224
   * @param string $facet_id
   *   The id of the facet.
225 226 227
   * @param bool|TRUE $show
   *   Boolean to determine if we want to show the amount of results.
   */
228
  protected function setShowAmountOfResults($facet_id, $show = TRUE) {
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 271 272 273 274 275 276 277 278 279 280

    $facet_display_page = '/admin/config/search/facets/' . $facet_id . '/display';

    // Go to the facet edit page and make sure "edit facet %facet" is present.
    $this->drupalGet($facet_display_page);
    $this->assertResponse(200);

    // Configure the text for empty results behavior.
    $edit = [
      'widget_configs[show_numbers]' => $show,
    ];
    $this->drupalPostForm(NULL, $edit, $this->t('Save'));
  }

  /**
   * Deletes a facet block by id.
   *
   * @param string $id
   *   The id of the block.
   */
  protected function deleteBlock($id) {
    $this->drupalGet('admin/structure/block/manage/' . $this->blocks[$id]->id(), array('query' => array('destination' => 'admin')));
    $this->clickLink(t('Delete'));
    $this->drupalPostForm(NULL, array(), t('Delete'));
    $this->assertRaw(t('The block %name has been deleted.', array('%name' => $this->blocks[$id]->label())));
  }

  /**
   * Asserts that a facet block does not appear.
   */
  protected function assertNoFacetBlocksAppear() {
    foreach ($this->blocks as $block) {
      $this->assertNoBlockAppears($block);
    }
  }

  /**
   * Asserts that a facet block appears.
   */
  protected function assertFacetBlocksAppear() {
    foreach ($this->blocks as $block) {
      $this->assertBlockAppears($block);
    }
  }

  /**
   * Creates a facet block by id.
   *
   * @param string $id
   *   The id of the block.
   */
  protected function createFacetBlock($id) {
281 282
    $block_values = ['region' => 'footer', 'id' => str_replace('_', '-', $id)];
    $this->blocks[$id] = $this->drupalPlaceBlock('facet_block:' . $id, $block_values);
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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  }

  /**
   * Configures empty behavior option to show a text on empty results.
   *
   * @param string $facet_name
   *   The name of the facet.
   */
  protected function setEmptyBehaviorFacetText($facet_name) {
    $facet_id = $this->convertNameToMachineName($facet_name);

    $facet_display_page = '/admin/config/search/facets/' . $facet_id . '/display';

    // Go to the facet edit page and make sure "edit facet %facet" is present.
    $this->drupalGet($facet_display_page);
    $this->assertResponse(200);

    // Configure the text for empty results behavior.
    $edit = [
      'facet_settings[empty_behavior]' => 'text',
      'facet_settings[empty_behavior_container][empty_behavior_text][value]' => 'No results found for this block!',
    ];
    $this->drupalPostForm(NULL, $edit, $this->t('Save'));
  }

  /**
   * Configures a facet to only be visible when accessing to the facet source.
   *
   * @param string $facet_name
   *   The name of the facet.
   */
  protected function setOptionShowOnlyWhenFacetSourceVisible($facet_name) {
    $facet_id = $this->convertNameToMachineName($facet_name);

    $facet_display_page = '/admin/config/search/facets/' . $facet_id . '/display';
    $this->drupalGet($facet_display_page);
    $this->assertResponse(200);

    $edit = [
      'facet_settings[only_visible_when_facet_source_is_visible]' => TRUE,
      'widget' => 'links',
      'widget_configs[show_numbers]' => '0',
    ];
    $this->drupalPostForm(NULL, $edit, $this->t('Save'));
  }

  /**
   * Get the facet overview page and make sure the overview is empty.
   */
  protected function checkEmptyOverview() {
    $facet_overview = '/admin/config/search/facets';
    $this->drupalGet($facet_overview);
    $this->assertResponse(200);

    // The list overview has Field: field_name as description. This tests on the
    // absence of that.
    $this->assertNoText('Field:');
  }

  /**
   * Tests adding a facet trough the interface.
   *
345 346
   * @param string $facet_id
   *   The id of the facet.
347 348
   * @param string $facet_name
   *   The name of the facet.
349 350
   * @param string $type
   *   The field type.
351
   */
352
  protected function addFacetCheck($facet_id, $facet_name, $type) {
353 354 355 356 357 358 359 360 361 362
    // Go to the Add facet page and make sure that returns a 200.
    $facet_add_page = '/admin/config/search/facets/add-facet';
    $this->drupalGet($facet_add_page);
    $this->assertResponse(200);

    $form_values = [
      'name' => '',
      'id' => $facet_id,
      'status' => 1,
      'url_alias' => $facet_id,
363
      'weight' => 4,
364 365 366 367 368 369 370 371 372 373 374 375 376
    ];

    // Try filling out the form, but without having filled in a name for the
    // facet to test for form errors.
    $this->drupalPostForm($facet_add_page, $form_values, $this->t('Save'));
    $this->assertText($this->t('Facet name field is required.'));
    $this->assertText($this->t('Facet source field is required.'));

    // Make sure that when filling out the name, the form error disappears.
    $form_values['name'] = $facet_name;
    $this->drupalPostForm(NULL, $form_values, $this->t('Save'));
    $this->assertNoText($this->t('Facet name field is required.'));

377
    // Configure the facet source by selecting one of the Search API views.
378 379 380 381 382 383 384 385 386 387
    $this->drupalGet($facet_add_page);
    $this->drupalPostForm(NULL, ['facet_source_id' => 'core_node_search:node_search'], $this->t('Configure facet source'));

    // The facet field is still required.
    $this->drupalPostForm(NULL, $form_values, $this->t('Save'));
    $this->assertText($this->t('Facet field field is required.'));

    // Fill in all fields and make sure the 'field is required' message is no
    // longer shown.
    $facet_source_form = [
388
      'facet_source_configs[core_node_search:node_search][field_identifier]' => $type,
389 390 391 392 393 394 395 396 397 398 399 400 401 402
    ];
    $this->drupalPostForm(NULL, $form_values + $facet_source_form, $this->t('Save'));
    $this->assertNoText('field is required.');

    // Make sure that the redirection to the display page is correct.
    $this->assertRaw(t('Facet %name has been created.', ['%name' => $facet_name]));
    $this->assertUrl('admin/config/search/facets/' . $facet_id . '/display');

    $this->drupalGet('admin/config/search/facets');
  }

  /**
   * Tests editing of a facet through the UI.
   *
403 404
   * @param string $facet_id
   *   The id of the facet.
405 406 407
   * @param string $facet_name
   *   The name of the facet.
   */
borisson_'s avatar
borisson_ committed
408
  public function editFacetCheck($facet_id, $facet_name) {
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    $facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';

    // Go to the facet edit page and make sure "edit facet %facet" is present.
    $this->drupalGet($facet_edit_page);
    $this->assertResponse(200);
    $this->assertRaw($this->t('Edit facet @facet', ['@facet' => $facet_name]));

    // Change the facet name to add in "-2" to test editing of a facet works.
    $form_values = ['name' => $facet_name . ' - 2'];
    $this->drupalPostForm($facet_edit_page, $form_values, $this->t('Save'));

    // Make sure that the redirection back to the overview was successful and
    // the edited facet is shown on the overview page.
    $this->assertRaw(t('Facet %name has been updated.', ['%name' => $facet_name . ' - 2']));

    // Make sure the "-2" suffix is still on the facet when editing a facet.
    $this->drupalGet($facet_edit_page);
    $this->assertRaw($this->t('Edit facet @facet', ['@facet' => $facet_name . ' - 2']));

    // Edit the form and change the facet's name back to the initial name.
    $form_values = ['name' => $facet_name];
    $this->drupalPostForm($facet_edit_page, $form_values, $this->t('Save'));

    // Make sure that the redirection back to the overview was successful and
    // the edited facet is shown on the overview page.
    $this->assertRaw(t('Facet %name has been updated.', ['%name' => $facet_name]));
  }

  /**
   * This deletes an unused facet through the UI.
   *
   * @param string $facet_name
   *   The name of the facet.
   */
  protected function deleteUsedFacet($facet_name) {
    $facet_id = $this->convertNameToMachineName($facet_name);

    $facet_delete_page = '/admin/config/search/facets/' . $facet_id . '/delete';

    // Go to the facet delete page and make the warning is shown.
    $this->drupalGet($facet_delete_page);
    $this->assertResponse(200);

    // Check that the facet by testing for the message and the absence of the
    // facet name on the overview.
borisson_'s avatar
borisson_ committed
454
    $this->assertRaw($this->t("The facet is currently used in a block and thus can't be removed. Remove the block first."));
455 456 457 458 459
  }

  /**
   * This deletes a facet through the UI.
   *
460 461
   * @param string $facet_id
   *   The id of the facet.
462 463 464
   * @param string $facet_name
   *   The name of the facet.
   */
465
  protected function deleteUnusedFacet($facet_id, $facet_name) {
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    $facet_delete_page = '/admin/config/search/facets/' . $facet_id . '/delete';

    // Go to the facet delete page and make the warning is shown.
    $this->drupalGet($facet_delete_page);
    $this->assertResponse(200);
    $this->assertText($this->t('This action cannot be undone'));
    // Actually submit the confirmation form.
    $this->drupalPostForm(NULL, [], $this->t('Delete'));

    // Check that the facet by testing for the message and the absence of the
    // facet name on the overview.
    $this->assertRaw($this->t('The facet %facet has been deleted.', ['%facet' => $facet_name]));

    // Refresh the page because on the previous page the $facet_name is still
    // visible (in the message).
    $facet_overview = '/admin/config/search/facets';
    $this->drupalGet($facet_overview);
    $this->assertResponse(200);
    $this->assertNoText($facet_name);
  }

  /**
   * Convert facet name to machine name.
   *
   * @param string $facet_name
   *   The name of the facet.
   *
   * @return string
   *   The facet name changed to a machine name.
   */
  protected function convertNameToMachineName($facet_name) {
    return preg_replace('@[^a-zA-Z0-9_]+@', '_', strtolower($facet_name));
  }

  /**
   * Go to the Delete Facet Page using the facet name.
   *
   * @param string $facet_name
   *   The name of the facet.
   */
  protected function goToDeleteFacetPage($facet_name) {
    $facet_id = $this->convertNameToMachineName($facet_name);

    $facet_delete_page = '/admin/config/search/facets/' . $facet_id . '/delete';

    // Go to the facet delete page and make the warning is shown.
    $this->drupalGet($facet_delete_page);
    $this->assertResponse(200);
  }

}