ProcessorIntegrationTest.php 27.7 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\facets\Functional;
4

5 6
use Drupal\facets\Entity\Facet;
use Drupal\facets\Processor\SortProcessorInterface;
7 8 9
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\search_api\Item\Field;
10

11 12 13 14 15
/**
 * Tests the processor functionality.
 *
 * @group facets
 */
16
class ProcessorIntegrationTest extends FacetsTestBase {
17 18 19 20 21 22 23 24

  /**
   * The url of the edit form.
   *
   * @var string
   */
  protected $editForm;

25 26 27 28
  /**
   * {@inheritdoc}
   */
  public static $modules = [
29
    'facets_custom_widget',
30 31 32
    'options',
  ];

33 34 35 36 37 38 39 40 41 42 43
  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

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

    // Set up example content types and insert 10 new content items.
    $this->setUpExampleStructure();
    $this->insertExampleContent();
44
    $this->assertEquals($this->indexItems($this->indexId), 5, '5 items were indexed.');
45
    $this->insertExampleContent();
46
    $this->assertEquals($this->indexItems($this->indexId), 5, '5 items were indexed.');
47 48 49 50 51 52 53 54 55 56 57 58 59
  }

  /**
   * Tests for the processors behavior in the backend.
   */
  public function testProcessorAdmin() {
    $facet_name = "Guanaco";
    $facet_id = "guanaco";

    $this->createFacet($facet_name, $facet_id);

    // Go to the processors form and check that the count limit processor is not
    // checked.
60
    $this->drupalGet('admin/config/search/facets/' . $facet_id . '/edit');
61
    $this->assertSession()->checkboxNotChecked('edit-facet-settings-count-limit-status');
62 63

    $form = ['facet_settings[count_limit][status]' => TRUE];
64
    $this->drupalPostForm(NULL, $form, 'Save');
65 66
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->checkboxChecked('edit-facet-settings-count-limit-status');
67 68 69 70 71 72 73

    // Enable the sort processor and change sort direction, check that the
    // change is persisted.
    $form = [
      'facet_sorting[active_widget_order][status]' => TRUE,
      'facet_sorting[active_widget_order][settings][sort]' => 'DESC',
    ];
74
    $this->drupalPostForm(NULL, $form, 'Save');
75 76 77
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->checkboxChecked('edit-facet-sorting-active-widget-order-status');
    $this->assertSession()->checkboxChecked('edit-facet-sorting-active-widget-order-settings-sort-desc');
78 79 80 81 82 83

    // Add an extra processor so we can test the weights as well.
    $form = [
      'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE,
      'facet_settings[count_limit][status]' => TRUE,
    ];
84
    $this->drupalPostForm(NULL, $form, 'Save');
85 86 87
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->checkboxChecked('edit-facet-settings-count-limit-status');
    $this->assertSession()->checkboxChecked('edit-facet-settings-hide-non-narrowing-result-processor-status');
88 89 90 91 92 93 94 95 96 97
    $this->assertOptionSelected('edit-processors-count-limit-weights-build', -10);
    $this->assertOptionSelected('edit-processors-hide-non-narrowing-result-processor-weights-build', -10);

    // Change the weight of one of the processors and test that the weight
    // change persisted.
    $form = [
      'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE,
      'facet_settings[count_limit][status]' => TRUE,
      'processors[hide_non_narrowing_result_processor][weights][build]' => 5,
    ];
98
    $this->drupalPostForm(NULL, $form, 'Save');
99 100
    $this->assertSession()->checkboxChecked('edit-facet-settings-count-limit-status');
    $this->assertSession()->checkboxChecked('edit-facet-settings-hide-non-narrowing-result-processor-status');
101 102 103 104 105 106 107 108 109 110
    $this->assertOptionSelected('edit-processors-count-limit-weights-build', -10);
    $this->assertOptionSelected('edit-processors-hide-non-narrowing-result-processor-weights-build', 5);
  }

  /**
   * Tests the for processors in the frontend with a 'keywords' facet.
   */
  public function testProcessorIntegration() {
    $facet_name = "Vicuña";
    $facet_id = "vicuna";
111
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
112 113

    $this->createFacet($facet_name, $facet_id, 'keywords');
114
    $this->drupalPostForm($this->editForm, ['facet_settings[query_operator]' => 'and'], 'Save');
115 116

    $this->drupalGet('search-api-test-fulltext');
117 118 119 120 121 122
    $this->assertSession()->pageTextContains('Displaying 10 search results');
    $this->assertSession()->pageTextContains('grape');
    $this->assertSession()->pageTextContains('orange');
    $this->assertSession()->pageTextContains('apple');
    $this->assertSession()->pageTextContains('strawberry');
    $this->assertSession()->pageTextContains('banana');
123 124 125 126 127 128 129

    $this->checkCountLimitProcessor();
    $this->checkExcludeItems();
    $this->checkHideNonNarrowingProcessor();
    $this->checkHideActiveItems();
  }

130 131 132 133 134 135 136 137 138 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 171 172 173 174 175
  /**
   * Tests the for processors in the frontend with a 'boolean' facet.
   */
  public function testBooleanProcessorIntegration() {
    $field_name = 'field_boolean';
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test_mulrev_changed',
      'type' => 'boolean',
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'item',
    ]);
    $field->save();

    $index = $this->getIndex();

    // Index the taxonomy and entity reference fields.
    $boolean_field = new Field($index, $field_name);
    $boolean_field->setType('integer');
    $boolean_field->setPropertyPath($field_name);
    $boolean_field->setDatasourceId('entity:entity_test_mulrev_changed');
    $boolean_field->setLabel('BooleanField');
    $index->addField($boolean_field);

    $index->save();
    $this->indexItems($this->indexId);

    $entity_test_storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test_mulrev_changed');
    $entity_test_storage->create([
      'name' => 'foo bar baz',
      'body' => 'test test',
      'type' => 'item',
      'keywords' => ['orange'],
      'category' => 'item_category',
      $field_name => TRUE,
    ])->save();

    $this->indexItems($this->indexId);

    $facet_name = "Boolean";
    $facet_id = "boolean";

borisson_'s avatar
borisson_ committed
176
    // Create facet.
177 178 179
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
    $this->createFacet($facet_name, $facet_id, $field_name);

borisson_'s avatar
borisson_ committed
180
    // Check values.
181 182 183 184 185 186 187 188 189
    $this->drupalGet('search-api-test-fulltext');
    $this->assertFacetLabel('1');

    $form = [
      'facet_settings[boolean_item][status]' => TRUE,
      'facet_settings[boolean_item][settings][on_value]' => 'Yes',
      'facet_settings[boolean_item][settings][off_value]' => 'No',
    ];
    $this->drupalPostForm($this->editForm, $form, 'Save');
190 191
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->checkboxChecked('edit-facet-settings-boolean-item-status');
192 193 194 195 196 197 198 199 200 201 202 203 204 205

    $this->drupalGet('search-api-test-fulltext');
    $this->assertFacetLabel('Yes');

    $form = [
      'facet_settings[boolean_item][status]' => TRUE,
      'facet_settings[boolean_item][settings][on_value]' => 'Øn',
    ];
    $this->drupalPostForm($this->editForm, $form, 'Save');

    $this->drupalGet('search-api-test-fulltext');
    $this->assertFacetLabel('Øn');
  }

206 207 208 209 210 211 212 213 214 215 216 217 218 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  /**
   * Tests the for configuration of granularity processor.
   */
  public function testNumericGranularity() {
    $field_name = 'field_integer';
    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test_mulrev_changed',
      'type' => 'integer',
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'item',
    ]);
    $field->save();
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'article',
    ]);
    $field->save();

    $index = $this->getIndex();

    // Index the taxonomy and entity reference fields.
    $integerField = new Field($index, $field_name);
    $integerField->setType('integer');
    $integerField->setPropertyPath($field_name);
    $integerField->setDatasourceId('entity:entity_test_mulrev_changed');
    $integerField->setLabel('IntegerField');
    $index->addField($integerField);

    $index->save();
    $this->indexItems($this->indexId);

    $entity_test_storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test_mulrev_changed');

    foreach ([30, 35, 40, 100] as $val) {
      $entity_test_storage->create([
        'name' => 'foo bar baz',
        'body' => 'test test int',
        'type' => 'item',
        'keywords' => ['orange'],
        'category' => 'item_category',
        $field_name => $val,
      ])->save();
    }

    $this->indexItems($this->indexId);

    $facet_id = "integer";

    // Create facet.
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
    $this->createFacet("Integer", $facet_id, $field_name);

    // Check values.
    $this->drupalGet('search-api-test-fulltext');
    $this->assertFacetLabel('100');
    $this->assertFacetLabel('30');
    $this->assertFacetLabel('35');
    $this->assertFacetLabel('40');

    $form = [
      'facet_settings[granularity_item][status]' => TRUE,
    ];
    $this->drupalPostForm($this->editForm, $form, 'Save');

    // Check values.
    $this->drupalGet('search-api-test-fulltext');
    $this->assertFacetLabel('30 (1)');
    $this->assertFacetLabel('35');
    $this->assertFacetLabel('40');
    $this->assertFacetLabel('100');

    $form = [
      'facet_settings[granularity_item][status]' => TRUE,
      'facet_settings[granularity_item][settings][granularity]' => 10,
    ];
    $this->drupalPostForm($this->editForm, $form, 'Save');

    // Check values.
    $this->drupalGet('search-api-test-fulltext');
    $this->assertFacetLabel('30 (2)');
    $this->assertEmpty($this->findFacetLink('35'));
    $this->assertFacetLabel('40');
    $this->assertFacetLabel('100');
  }

296 297 298 299 300 301
  /**
   * Tests the for sorting processors in the frontend with a 'keywords' facet.
   */
  public function testSortingWidgets() {
    $facet_name = "Huacaya alpaca";
    $facet_id = "huacaya_alpaca";
302
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
303 304 305 306 307 308 309 310

    $this->createFacet($facet_name, $facet_id, 'keywords');

    $this->checkSortByActive();
    $this->checkSortByCount();
    $this->checkSortByDisplay();
    $this->checkSortByRaw();
  }
311 312 313 314 315 316 317

  /**
   * Tests sorting of results.
   */
  public function testResultSorting() {
    $id = 'burrowing_owl';
    $name = 'Burrowing owl';
318
    $this->editForm = 'admin/config/search/facets/' . $id . '/edit';
319 320

    $this->createFacet($name, $id, 'keywords');
321
    $this->disableAllFacetSorts();
322 323 324

    $values = [
      'facet_sorting[display_value_widget_order][status]' => TRUE,
325
      'widget_config[show_numbers]' => TRUE,
326
    ];
327
    $this->drupalPostForm($this->editForm, $values, 'Save');
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

    $expected_results = [
      'apple',
      'banana',
      'grape',
      'orange',
      'strawberry',
    ];

    $this->drupalGet('search-api-test-fulltext');
    foreach ($expected_results as $k => $link) {
      if ($k > 0) {
        $x = $expected_results[($k - 1)];
        $y = $expected_results[$k];
        $this->assertStringPosition($x, $y);
      }
    }

    // Sort by count, then by display value.
    $values['facet_sorting[count_widget_order][status]'] = TRUE;
348
    $values['facet_sorting[count_widget_order][settings][sort]'] = 'ASC';
349
    $values['processors[count_widget_order][weights][sort]'] = 1;
350
    $values['facet_sorting[display_value_widget_order][status]'] = TRUE;
351
    $values['processors[display_value_widget_order][weights][sort]'] = 2;
352
    $this->disableAllFacetSorts();
353
    $this->drupalPostForm($this->editForm, $values, 'Save');
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

    $expected_results = [
      'banana',
      'apple',
      'strawberry',
      'grape',
      'orange',
    ];

    $this->drupalGet('search-api-test-fulltext');
    foreach ($expected_results as $k => $link) {
      if ($k > 0) {
        $x = $expected_results[($k - 1)];
        $y = $expected_results[$k];
        $this->assertStringPosition($x, $y);
      }
    }

    $values['facet_sorting[display_value_widget_order][status]'] = TRUE;
    $values['facet_sorting[count_widget_order][status]'] = TRUE;
374
    $values['facet_sorting[count_widget_order][settings][sort]'] = 'ASC';
375
    $this->drupalPostForm($this->editForm, $values, 'Save');
376 377
    $this->assertSession()->checkboxChecked('edit-facet-sorting-display-value-widget-order-status');
    $this->assertSession()->checkboxChecked('edit-facet-sorting-count-widget-order-status');
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

    $expected_results = [
      'banana',
      'apple',
      'strawberry',
      'grape',
      'orange',
    ];

    $this->drupalGet('search-api-test-fulltext');
    foreach ($expected_results as $k => $link) {
      if ($k > 0) {
        $x = $expected_results[($k - 1)];
        $y = $expected_results[$k];
        $this->assertStringPosition($x, $y);
      }
    }
  }
396 397 398 399

  /**
   * Tests the count limit processor.
   */
400
  protected function checkCountLimitProcessor() {
401 402 403
    $this->drupalGet($this->editForm);

    $form = [
404
      'widget_config[show_numbers]' => TRUE,
405 406
      'facet_settings[count_limit][status]' => TRUE,
    ];
407
    $this->drupalPostForm($this->editForm, $form, 'Save');
408 409
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->checkboxChecked('edit-facet-settings-count-limit-status');
410
    $form = [
411
      'widget_config[show_numbers]' => TRUE,
412 413
      'facet_settings[count_limit][status]' => TRUE,
    ];
414
    $this->drupalPostForm($this->editForm, $form, 'Save');
415 416

    $form = [
417
      'widget_config[show_numbers]' => TRUE,
418 419 420
      'facet_settings[count_limit][status]' => TRUE,
      'facet_settings[count_limit][settings][minimum_items]' => 5,
    ];
421
    $this->drupalPostForm($this->editForm, $form, 'Save');
422 423

    $this->drupalGet('search-api-test-fulltext');
424
    $this->assertSession()->pageTextContains('Displaying 10 search results');
425
    $this->assertFacetLabel('grape (6)');
426
    $this->assertSession()->pageTextNotContains('apple');
427 428

    $form = [
429
      'widget_config[show_numbers]' => TRUE,
430 431 432 433
      'facet_settings[count_limit][status]' => TRUE,
      'facet_settings[count_limit][settings][minimum_items]' => 1,
      'facet_settings[count_limit][settings][maximum_items]' => 5,
    ];
434
    $this->drupalPostForm($this->editForm, $form, 'Save');
435 436

    $this->drupalGet('search-api-test-fulltext');
437 438
    $this->assertSession()->pageTextContains('Displaying 10 search results');
    $this->assertSession()->pageTextNotContains('grape');
439
    $this->assertFacetLabel('apple (4)');
440 441

    $form = [
442
      'widget_config[show_numbers]' => FALSE,
443 444
      'facet_settings[count_limit][status]' => FALSE,
    ];
445
    $this->drupalPostForm($this->editForm, $form, 'Save');
446 447 448 449 450
  }

  /**
   * Tests the exclude items.
   */
451
  protected function checkExcludeItems() {
452 453 454
    $form = [
      'facet_settings[exclude_specified_items][status]' => TRUE,
    ];
455
    $this->drupalPostForm($this->editForm, $form, 'Save');
456 457 458 459 460

    $form = [
      'facet_settings[exclude_specified_items][status]' => TRUE,
      'facet_settings[exclude_specified_items][settings][exclude]' => 'banana',
    ];
461
    $this->drupalPostForm($this->editForm, $form, 'Save');
462 463

    $this->drupalGet('search-api-test-fulltext');
464 465 466
    $this->assertSession()->pageTextContains('Displaying 10 search results');
    $this->assertSession()->pageTextContains('grape');
    $this->assertSession()->pageTextNotContains('banana');
467 468 469 470 471 472

    $form = [
      'facet_settings[exclude_specified_items][status]' => TRUE,
      'facet_settings[exclude_specified_items][settings][exclude]' => '(.*)berry',
      'facet_settings[exclude_specified_items][settings][regex]' => TRUE,
    ];
473
    $this->drupalPostForm($this->editForm, $form, 'Save');
474 475

    $this->drupalGet('search-api-test-fulltext');
476 477 478
    $this->assertSession()->pageTextContains('Displaying 10 search results');
    $this->assertSession()->pageTextNotContains('strawberry');
    $this->assertSession()->pageTextContains('grape');
479 480 481 482

    $form = [
      'facet_settings[exclude_specified_items][status]' => FALSE,
    ];
483
    $this->drupalPostForm($this->editForm, $form, 'Save');
484 485 486 487 488
  }

  /**
   * Tests hiding non-narrowing results.
   */
489
  protected function checkHideNonNarrowingProcessor() {
490
    $this->drupalGet('search-api-test-fulltext');
491
    $this->assertSession()->pageTextContains('Displaying 10 search results');
492
    $this->assertFacetLabel('apple');
493

494
    $this->clickLink('apple');
495
    $this->assertSession()->pageTextContains('Displaying 4 search results');
496
    $this->assertFacetLabel('grape');
497 498 499 500

    $form = [
      'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE,
    ];
501
    $this->drupalPostForm($this->editForm, $form, 'Save');
502 503

    $this->drupalGet('search-api-test-fulltext');
504
    $this->assertSession()->pageTextContains('Displaying 10 search results');
505
    $this->assertFacetLabel('apple');
506

507
    $this->clickLink('apple');
508 509
    $this->assertSession()->pageTextContains('Displaying 4 search results');
    $this->assertSession()->linkNotExists('grape');
510 511 512 513

    $form = [
      'facet_settings[hide_non_narrowing_result_processor][status]' => FALSE,
    ];
514
    $this->drupalPostForm($this->editForm, $form, 'Save');
515 516 517 518 519
  }

  /**
   * Tests hiding active results.
   */
520
  protected function checkHideActiveItems() {
521 522 523
    $form = [
      'facet_settings[hide_active_items_processor][status]' => TRUE,
    ];
524
    $this->drupalPostForm($this->editForm, $form, 'Save');
525 526

    $this->drupalGet('search-api-test-fulltext');
527
    $this->assertSession()->pageTextContains('Displaying 10 search results');
528 529
    $this->assertFacetLabel('grape');
    $this->assertFacetLabel('banana');
530 531

    $this->clickLink('grape');
532 533
    $this->assertSession()->pageTextContains('Displaying 6 search results');
    $this->assertSession()->linkNotExists('grape');
534
    $this->assertFacetLabel('banana');
535 536 537 538

    $form = [
      'facet_settings[hide_active_items_processor][status]' => FALSE,
    ];
539
    $this->drupalPostForm($this->editForm, $form, 'Save');
540 541 542 543 544 545
  }

  /**
   * Tests the active widget order.
   */
  protected function checkSortByActive() {
546
    $this->disableAllFacetSorts();
547 548
    $form = [
      'facet_sorting[active_widget_order][status]' => TRUE,
549
      'facet_sorting[active_widget_order][settings][sort]' => 'ASC',
550
    ];
551
    $this->drupalPostForm($this->editForm, $form, 'Save');
552 553 554

    $this->drupalGet('search-api-test-fulltext');
    $this->clickLink('strawberry');
555
    $this->assertStringPosition('strawberry', 'grape');
556 557 558 559 560

    $form = [
      'facet_sorting[active_widget_order][status]' => TRUE,
      'facet_sorting[active_widget_order][settings][sort]' => 'DESC',
    ];
561
    $this->drupalPostForm($this->editForm, $form, 'Save');
562 563 564

    $this->drupalGet('search-api-test-fulltext');
    $this->clickLink('strawberry');
565
    $this->assertStringPosition('grape', 'strawberry');
566 567 568 569

    $form = [
      'facet_sorting[active_widget_order][status]' => FALSE,
    ];
570
    $this->drupalPostForm($this->editForm, $form, 'Save');
571 572 573 574 575 576
  }

  /**
   * Tests the active widget order.
   */
  protected function checkSortByCount() {
577
    $this->disableAllFacetSorts();
578
    $form = [
579
      'widget_config[show_numbers]' => TRUE,
580
      'facet_sorting[count_widget_order][status]' => TRUE,
581
      'facet_sorting[count_widget_order][settings][sort]' => 'ASC',
582
    ];
583
    $this->drupalPostForm($this->editForm, $form, 'Save');
584 585 586 587 588 589 590 591 592 593

    $this->drupalGet('search-api-test-fulltext');
    $this->assertStringPosition('banana', 'apple');
    $this->assertStringPosition('banana', 'strawberry');
    $this->assertStringPosition('apple', 'orange');

    $form = [
      'facet_sorting[count_widget_order][status]' => TRUE,
      'facet_sorting[count_widget_order][settings][sort]' => 'DESC',
    ];
594
    $this->drupalPostForm($this->editForm, $form, 'Save');
595 596 597 598 599 600 601

    $this->drupalGet('search-api-test-fulltext');
    $this->assertStringPosition('apple', 'banana');
    $this->assertStringPosition('strawberry', 'banana');
    $this->assertStringPosition('orange', 'apple');

    $form = [
602
      'widget_config[show_numbers]' => FALSE,
603 604
      'facet_sorting[count_widget_order][status]' => FALSE,
    ];
605
    $this->drupalPostForm($this->editForm, $form, 'Save');
606 607 608 609 610
  }

  /**
   * Tests the display order.
   */
611
  protected function checkSortByDisplay() {
612
    $this->disableAllFacetSorts();
613
    $form = ['facet_sorting[display_value_widget_order][status]' => TRUE];
614
    $this->drupalPostForm($this->editForm, $form, 'Save');
615 616 617 618 619 620 621 622 623

    $this->drupalGet('search-api-test-fulltext');
    $this->assertStringPosition('grape', 'strawberry');
    $this->assertStringPosition('apple', 'banana');

    $form = [
      'facet_sorting[display_value_widget_order][status]' => TRUE,
      'facet_sorting[display_value_widget_order][settings][sort]' => 'DESC',
    ];
624
    $this->drupalPostForm($this->editForm, $form, 'Save');
625 626 627 628 629

    $this->drupalGet('search-api-test-fulltext');
    $this->assertStringPosition('strawberry', 'grape');
    $this->assertStringPosition('banana', 'apple');

630
    $form = ['facet_sorting[display_value_widget_order][status]' => FALSE];
631
    $this->drupalPostForm($this->editForm, $form, 'Save');
632 633 634 635 636
  }

  /**
   * Tests the display order.
   */
637
  protected function checkSortByRaw() {
638
    $this->disableAllFacetSorts();
639 640 641
    $form = [
      'facet_sorting[raw_value_widget_order][status]' => TRUE,
    ];
642
    $this->drupalPostForm($this->editForm, $form, 'Save');
643 644 645 646 647 648 649 650 651

    $this->drupalGet('search-api-test-fulltext');
    $this->assertStringPosition('grape', 'strawberry');
    $this->assertStringPosition('apple', 'banana');

    $form = [
      'facet_sorting[raw_value_widget_order][status]' => TRUE,
      'facet_sorting[raw_value_widget_order][settings][sort]' => 'DESC',
    ];
652
    $this->drupalPostForm($this->editForm, $form, 'Save');
653 654 655 656 657 658 659 660

    $this->drupalGet('search-api-test-fulltext');
    $this->assertStringPosition('strawberry', 'grape');
    $this->assertStringPosition('banana', 'apple');

    $form = [
      'facet_sorting[raw_value_widget_order][status]' => FALSE,
    ];
661
    $this->drupalPostForm($this->editForm, $form, 'Save');
662 663
  }

664 665 666 667 668 669 670 671 672 673 674 675 676
  /**
   * Disables all sorting processors for a clean testing base.
   */
  protected function disableAllFacetSorts($path = FALSE) {
    $settings = [
      'facet_sorting[raw_value_widget_order][status]' => FALSE,
      'facet_sorting[display_value_widget_order][status]' => FALSE,
      'facet_sorting[count_widget_order][status]' => FALSE,
      'facet_sorting[active_widget_order][status]' => FALSE,
    ];
    if (!$path) {
      $path = $this->editForm;
    }
677
    $this->drupalPostForm($path, $settings, 'Save');
678 679
  }

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
  /**
   * Checks if the list processor changes machine name to the display label.
   */
  public function testListProcessor() {
    entity_test_create_bundle('basic', "Basic page", 'entity_test_mulrev_changed');
    $entity_test_storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test_mulrev_changed');

    // Add an entity with basic page content type.
    $entity_test_storage->create([
      'name' => 'AC0871108',
      'body' => 'Eamus Catuli',
      'type' => 'basic',
    ])->save();
    $this->indexItems($this->indexId);

    $facet_name = "Eamus Catuli";
    $facet_id = "eamus_catuli";
    $editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
    $this->createFacet($facet_name, $facet_id);

    // Go to the overview and check that the machine names are used as facets.
    $this->drupalGet('search-api-test-fulltext');
703
    $this->assertSession()->pageTextContains('Displaying 11 search results');
704 705 706 707 708 709 710
    $this->assertFacetLabel('basic');

    // Edit the facet to use the list_item processor.
    $edit = [
      'facet_settings[list_item][status]' => TRUE,
    ];
    $this->drupalPostForm($editForm, $edit, 'Save');
711 712
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->checkboxChecked('edit-facet-settings-list-item-status');
713 714 715 716

    // Go back to the overview and check that now the label is being used
    // instead.
    $this->drupalGet('search-api-test-fulltext');
717
    $this->assertSession()->pageTextContains('Displaying 11 search results');
718 719 720
    $this->assertFacetLabel('Basic page');
  }

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
  /**
   * Test pre query processor.
   */
  public function testPreQueryProcessor() {
    $facet_name = "Eamus Catuli";
    $facet_id = "eamus_catuli";
    $editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
    $this->createFacet($facet_name, $facet_id);

    $edit = [
      'facet_settings[test_pre_query][status]' => TRUE,
      'facet_settings[test_pre_query][settings][test_value]' => 'Llama',
    ];
    $this->drupalPostForm($editForm, $edit, 'Save');

    $this->drupalGet('search-api-test-fulltext');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Llama');
  }

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
  /**
   * Tests the facet support for a widget.
   */
  public function testSupportsFacet() {
    $id = 'masked_owl';
    $this->createFacet('Australian masked owl', $id);

    // Go the the facet edit page and check to see if the custom processor shows
    // up.
    $this->drupalGet('admin/config/search/facets/' . $id . '/edit');
    $this->assertSession()->pageTextContains('test pre query');

    // Make the ::supportsFacet method on the custom processor return false.
    \Drupal::state()->set('facets_test_supports_facet', FALSE);

    // Go to the facet edit page and check to see if the custom processor is
    // now hidden.
    $this->drupalGet('admin/config/search/facets/' . $id . '/edit');
    $this->assertSession()->pageTextNotContains('test pre query');
  }

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
  /**
   * Test HideOnlyOneItemProcessor.
   *
   * Test if after clicking an item that has only one item, the facet block no
   * longer shows.
   */
  public function testHideOnlyOneItemProcessor() {
    $entity_test_storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test_mulrev_changed');
    $entity_test_storage->create([
      'name' => 'baz baz',
      'body' => 'foo test',
      'type' => 'article',
      'keywords' => ['kiwi'],
      'category' => 'article_category',
    ])->save();

    $this->indexItems($this->indexId);

    $facet_name = 'Drupalcon Vienna';
    $facet_id = 'drupalcon_vienna';
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
    $this->createFacet($facet_name, $facet_id, 'keywords');

    $form = [
      'facet_settings[hide_1_result_facet][status]' => TRUE,
      'facet_settings[query_operator]' => 'and',
    ];
    $this->drupalPostForm($this->editForm, $form, 'Save');
    $this->drupalGet('search-api-test-fulltext');

    $this->assertFacetBlocksAppear();
    $this->clickLink('kiwi');
    $this->assertNoFacetBlocksAppear();
  }

798 799 800 801 802 803 804 805 806
  /**
   * Tests that processors are hidden when the correct fields aren't there.
   */
  public function testHiddenProcessors() {
    $facet_id = 'alpaca';
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
    $this->createFacet('Alpaca', $facet_id);
    $this->drupalGet($this->editForm);
    $this->assertSession()->pageTextNotContains('Boolean item label');
807
    $this->assertSession()->pageTextNotContains('Transform UID to user name');
808
    $this->assertSession()->pageTextNotContains('Transform entity ID to label');
809 810
  }

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
  /**
   * Tests the configuration of the processors.
   */
  public function testProcessorConfig() {
    $this->createFacet('Llama', 'llama');

    $facet_id = 'alpaca';
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
    $this->createFacet('Alpaca', $facet_id);
    $this->drupalGet($this->editForm);

    $facet = Facet::load($facet_id);

    /** @var \Drupal\facets\Processor\ProcessorInterface $processor */
    foreach ($facet->getProcessors(FALSE) as $processor) {
      // Sort processors have a different form key, so don't bother for now.
      if ($processor instanceof SortProcessorInterface) {
        continue;
      }
      // These processors are hidden by default, see also
      // ::testHiddenProcessors.
      if (in_array($processor->getPluginId(), ['boolean_item', 'translate_entity', 'uid_to_username_callback'])) {
        continue;
      }

      $this->drupalPostForm(NULL, ["facet_settings[{$processor->getPluginId()}][status]" => '1'], 'Save');
      $this->assertSession()->statusCodeEquals(200);
    }
  }

841
}