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

3
namespace Drupal\Tests\facets\Functional;
4

5 6 7
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\search_api\Item\Field;
8

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

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

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

31 32 33 34 35 36 37 38 39 40 41
  /**
   * {@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();
42
    $this->assertEquals($this->indexItems($this->indexId), 5, '5 items were indexed.');
43
    $this->insertExampleContent();
44
    $this->assertEquals($this->indexItems($this->indexId), 5, '5 items were indexed.');
45 46 47 48 49 50 51 52 53 54 55 56 57
  }

  /**
   * 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.
58
    $this->drupalGet('admin/config/search/facets/' . $facet_id . '/edit');
59
    $this->assertSession()->checkboxNotChecked('edit-facet-settings-count-limit-status');
60 61

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

    // 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',
    ];
72
    $this->drupalPostForm(NULL, $form, 'Save');
73 74 75
    $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');
76 77 78 79 80 81

    // 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,
    ];
82
    $this->drupalPostForm(NULL, $form, 'Save');
83 84 85
    $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');
86 87 88 89 90 91 92 93 94 95
    $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,
    ];
96
    $this->drupalPostForm(NULL, $form, 'Save');
97 98
    $this->assertSession()->checkboxChecked('edit-facet-settings-count-limit-status');
    $this->assertSession()->checkboxChecked('edit-facet-settings-hide-non-narrowing-result-processor-status');
99 100 101 102 103 104 105 106 107 108
    $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";
109
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
110 111

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

    $this->drupalGet('search-api-test-fulltext');
115 116 117 118 119 120
    $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');
121 122 123 124 125 126 127

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

128 129 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
  /**
   * 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
174
    // Create facet.
175 176 177
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
    $this->createFacet($facet_name, $facet_id, $field_name);

borisson_'s avatar
borisson_ committed
178
    // Check values.
179 180 181 182 183 184 185 186 187
    $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');
188 189
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->checkboxChecked('edit-facet-settings-boolean-item-status');
190 191 192 193 194 195 196 197 198 199 200 201 202 203

    $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');
  }

204 205 206 207 208 209
  /**
   * Tests the for sorting processors in the frontend with a 'keywords' facet.
   */
  public function testSortingWidgets() {
    $facet_name = "Huacaya alpaca";
    $facet_id = "huacaya_alpaca";
210
    $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
211 212 213 214 215 216 217 218

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

    $this->checkSortByActive();
    $this->checkSortByCount();
    $this->checkSortByDisplay();
    $this->checkSortByRaw();
  }
219 220 221 222 223 224 225

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

    $this->createFacet($name, $id, 'keywords');
229
    $this->disableAllFacetSorts();
230 231 232

    $values = [
      'facet_sorting[display_value_widget_order][status]' => TRUE,
233
      'widget_config[show_numbers]' => TRUE,
234
    ];
235
    $this->drupalPostForm($this->editForm, $values, 'Save');
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    $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;
256
    $values['facet_sorting[count_widget_order][settings][sort]'] = 'ASC';
257
    $values['processors[count_widget_order][weights][sort]'] = 1;
258
    $values['facet_sorting[display_value_widget_order][status]'] = TRUE;
259
    $values['processors[display_value_widget_order][weights][sort]'] = 2;
260
    $this->disableAllFacetSorts();
261
    $this->drupalPostForm($this->editForm, $values, 'Save');
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

    $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;
282
    $values['facet_sorting[count_widget_order][settings][sort]'] = 'ASC';
283
    $this->drupalPostForm($this->editForm, $values, 'Save');
284 285
    $this->assertSession()->checkboxChecked('edit-facet-sorting-display-value-widget-order-status');
    $this->assertSession()->checkboxChecked('edit-facet-sorting-count-widget-order-status');
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

    $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);
      }
    }
  }
304 305 306 307

  /**
   * Tests the count limit processor.
   */
308
  protected function checkCountLimitProcessor() {
309 310 311
    $this->drupalGet($this->editForm);

    $form = [
312
      'widget_config[show_numbers]' => TRUE,
313 314
      'facet_settings[count_limit][status]' => TRUE,
    ];
315
    $this->drupalPostForm($this->editForm, $form, 'Save');
316 317
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->checkboxChecked('edit-facet-settings-count-limit-status');
318
    $form = [
319
      'widget_config[show_numbers]' => TRUE,
320 321
      'facet_settings[count_limit][status]' => TRUE,
    ];
322
    $this->drupalPostForm($this->editForm, $form, 'Save');
323 324

    $form = [
325
      'widget_config[show_numbers]' => TRUE,
326 327 328
      'facet_settings[count_limit][status]' => TRUE,
      'facet_settings[count_limit][settings][minimum_items]' => 5,
    ];
329
    $this->drupalPostForm($this->editForm, $form, 'Save');
330 331

    $this->drupalGet('search-api-test-fulltext');
332
    $this->assertSession()->pageTextContains('Displaying 10 search results');
333
    $this->assertFacetLabel('grape (6)');
334
    $this->assertSession()->pageTextNotContains('apple');
335 336

    $form = [
337
      'widget_config[show_numbers]' => TRUE,
338 339 340 341
      'facet_settings[count_limit][status]' => TRUE,
      'facet_settings[count_limit][settings][minimum_items]' => 1,
      'facet_settings[count_limit][settings][maximum_items]' => 5,
    ];
342
    $this->drupalPostForm($this->editForm, $form, 'Save');
343 344

    $this->drupalGet('search-api-test-fulltext');
345 346
    $this->assertSession()->pageTextContains('Displaying 10 search results');
    $this->assertSession()->pageTextNotContains('grape');
347
    $this->assertFacetLabel('apple (4)');
348 349

    $form = [
350
      'widget_config[show_numbers]' => FALSE,
351 352
      'facet_settings[count_limit][status]' => FALSE,
    ];
353
    $this->drupalPostForm($this->editForm, $form, 'Save');
354 355 356 357 358
  }

  /**
   * Tests the exclude items.
   */
359
  protected function checkExcludeItems() {
360 361 362
    $form = [
      'facet_settings[exclude_specified_items][status]' => TRUE,
    ];
363
    $this->drupalPostForm($this->editForm, $form, 'Save');
364 365 366 367 368

    $form = [
      'facet_settings[exclude_specified_items][status]' => TRUE,
      'facet_settings[exclude_specified_items][settings][exclude]' => 'banana',
    ];
369
    $this->drupalPostForm($this->editForm, $form, 'Save');
370 371

    $this->drupalGet('search-api-test-fulltext');
372 373 374
    $this->assertSession()->pageTextContains('Displaying 10 search results');
    $this->assertSession()->pageTextContains('grape');
    $this->assertSession()->pageTextNotContains('banana');
375 376 377 378 379 380

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

    $this->drupalGet('search-api-test-fulltext');
384 385 386
    $this->assertSession()->pageTextContains('Displaying 10 search results');
    $this->assertSession()->pageTextNotContains('strawberry');
    $this->assertSession()->pageTextContains('grape');
387 388 389 390

    $form = [
      'facet_settings[exclude_specified_items][status]' => FALSE,
    ];
391
    $this->drupalPostForm($this->editForm, $form, 'Save');
392 393 394 395 396
  }

  /**
   * Tests hiding non-narrowing results.
   */
397
  protected function checkHideNonNarrowingProcessor() {
398
    $this->drupalGet('search-api-test-fulltext');
399
    $this->assertSession()->pageTextContains('Displaying 10 search results');
400
    $this->assertFacetLabel('apple');
401

402
    $this->clickLink('apple');
403
    $this->assertSession()->pageTextContains('Displaying 4 search results');
404
    $this->assertFacetLabel('grape');
405 406 407 408

    $form = [
      'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE,
    ];
409
    $this->drupalPostForm($this->editForm, $form, 'Save');
410 411

    $this->drupalGet('search-api-test-fulltext');
412
    $this->assertSession()->pageTextContains('Displaying 10 search results');
413
    $this->assertFacetLabel('apple');
414

415
    $this->clickLink('apple');
416 417
    $this->assertSession()->pageTextContains('Displaying 4 search results');
    $this->assertSession()->linkNotExists('grape');
418 419 420 421

    $form = [
      'facet_settings[hide_non_narrowing_result_processor][status]' => FALSE,
    ];
422
    $this->drupalPostForm($this->editForm, $form, 'Save');
423 424 425 426 427
  }

  /**
   * Tests hiding active results.
   */
428
  protected function checkHideActiveItems() {
429 430 431
    $form = [
      'facet_settings[hide_active_items_processor][status]' => TRUE,
    ];
432
    $this->drupalPostForm($this->editForm, $form, 'Save');
433 434

    $this->drupalGet('search-api-test-fulltext');
435
    $this->assertSession()->pageTextContains('Displaying 10 search results');
436 437
    $this->assertFacetLabel('grape');
    $this->assertFacetLabel('banana');
438 439

    $this->clickLink('grape');
440 441
    $this->assertSession()->pageTextContains('Displaying 6 search results');
    $this->assertSession()->linkNotExists('grape');
442
    $this->assertFacetLabel('banana');
443 444 445 446

    $form = [
      'facet_settings[hide_active_items_processor][status]' => FALSE,
    ];
447
    $this->drupalPostForm($this->editForm, $form, 'Save');
448 449 450 451 452 453
  }

  /**
   * Tests the active widget order.
   */
  protected function checkSortByActive() {
454
    $this->disableAllFacetSorts();
455 456
    $form = [
      'facet_sorting[active_widget_order][status]' => TRUE,
457
      'facet_sorting[active_widget_order][settings][sort]' => 'ASC',
458
    ];
459
    $this->drupalPostForm($this->editForm, $form, 'Save');
460 461 462

    $this->drupalGet('search-api-test-fulltext');
    $this->clickLink('strawberry');
463
    $this->assertStringPosition('strawberry', 'grape');
464 465 466 467 468

    $form = [
      'facet_sorting[active_widget_order][status]' => TRUE,
      'facet_sorting[active_widget_order][settings][sort]' => 'DESC',
    ];
469
    $this->drupalPostForm($this->editForm, $form, 'Save');
470 471 472

    $this->drupalGet('search-api-test-fulltext');
    $this->clickLink('strawberry');
473
    $this->assertStringPosition('grape', 'strawberry');
474 475 476 477

    $form = [
      'facet_sorting[active_widget_order][status]' => FALSE,
    ];
478
    $this->drupalPostForm($this->editForm, $form, 'Save');
479 480 481 482 483 484
  }

  /**
   * Tests the active widget order.
   */
  protected function checkSortByCount() {
485
    $this->disableAllFacetSorts();
486
    $form = [
487
      'widget_config[show_numbers]' => TRUE,
488
      'facet_sorting[count_widget_order][status]' => TRUE,
489
      'facet_sorting[count_widget_order][settings][sort]' => 'ASC',
490
    ];
491
    $this->drupalPostForm($this->editForm, $form, 'Save');
492 493 494 495 496 497 498 499 500 501

    $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',
    ];
502
    $this->drupalPostForm($this->editForm, $form, 'Save');
503 504 505 506 507 508 509

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

    $form = [
510
      'widget_config[show_numbers]' => FALSE,
511 512
      'facet_sorting[count_widget_order][status]' => FALSE,
    ];
513
    $this->drupalPostForm($this->editForm, $form, 'Save');
514 515 516 517 518
  }

  /**
   * Tests the display order.
   */
519
  protected function checkSortByDisplay() {
520
    $this->disableAllFacetSorts();
521
    $form = ['facet_sorting[display_value_widget_order][status]' => TRUE];
522
    $this->drupalPostForm($this->editForm, $form, 'Save');
523 524 525 526 527 528 529 530 531

    $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',
    ];
532
    $this->drupalPostForm($this->editForm, $form, 'Save');
533 534 535 536 537

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

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

  /**
   * Tests the display order.
   */
545
  protected function checkSortByRaw() {
546
    $this->disableAllFacetSorts();
547 548 549
    $form = [
      'facet_sorting[raw_value_widget_order][status]' => TRUE,
    ];
550
    $this->drupalPostForm($this->editForm, $form, 'Save');
551 552 553 554 555 556 557 558 559

    $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',
    ];
560
    $this->drupalPostForm($this->editForm, $form, 'Save');
561 562 563 564 565 566 567 568

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

    $form = [
      'facet_sorting[raw_value_widget_order][status]' => FALSE,
    ];
569
    $this->drupalPostForm($this->editForm, $form, 'Save');
570 571
  }

572 573 574 575 576 577 578 579 580 581 582 583 584
  /**
   * 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;
    }
585
    $this->drupalPostForm($path, $settings, 'Save');
586 587
  }

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
  /**
   * 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');
611
    $this->assertSession()->pageTextContains('Displaying 11 search results');
612 613 614 615 616 617 618
    $this->assertFacetLabel('basic');

    // Edit the facet to use the list_item processor.
    $edit = [
      'facet_settings[list_item][status]' => TRUE,
    ];
    $this->drupalPostForm($editForm, $edit, 'Save');
619 620
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->checkboxChecked('edit-facet-settings-list-item-status');
621 622 623 624

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

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
  /**
   * 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');
  }

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
  /**
   * 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');
  }

670 671 672 673 674 675 676 677 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 703 704 705
  /**
   * 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();
  }

706 707 708 709 710 711 712 713 714
  /**
   * 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');
715
    $this->assertSession()->pageTextNotContains('Transform UID to user name');
716
    $this->assertSession()->pageTextNotContains('Transform entity ID to label');
717 718
  }

719
}