You are here

public function ProcessorIntegrationTest::testProcessorIntegration in Search API 8

Tests the admin UI for processors.

Calls the other test methods in this class, named check*Integration(), to avoid the overhead of having one test per processor.

File

tests/src/Functional/ProcessorIntegrationTest.php, line 98

Class

ProcessorIntegrationTest
Tests the admin UI for processors.

Namespace

Drupal\Tests\search_api\Functional

Code

public function testProcessorIntegration() {

  // Some processors are always enabled.
  $enabled = [
    'add_url',
    'aggregated_field',
    'language_with_fallback',
    'rendered_item',
  ];
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);

  // Ensure the hidden processors aren't displayed in the UI.
  $this
    ->loadProcessorsTab();
  $hidden = $enabled;
  foreach ($hidden as $processor_id) {
    $this
      ->assertSession()
      ->responseNotContains(Html::escape($processor_id));
  }
  $this
    ->checkLanguageWithFallbackIntegration();
  $this
    ->checkAggregatedFieldsIntegration();
  $this
    ->checkContentAccessIntegration();
  $enabled[] = 'content_access';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkEntityBundleBoostIntegration();
  $enabled[] = 'type_boost';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkHighlightIntegration();
  $enabled[] = 'highlight';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkHtmlFilterIntegration();
  $enabled[] = 'html_filter';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkIgnoreCaseIntegration();
  $enabled[] = 'ignorecase';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkIgnoreCharactersIntegration();
  $enabled[] = 'ignore_character';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkEntityStatusIntegration();
  $enabled[] = 'entity_status';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkNoUiIntegration();
  $enabled[] = 'search_api_test_no_ui';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkRoleFilterIntegration();
  $enabled[] = 'role_filter';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkRenderedItemIntegration();
  $this
    ->checkStopWordsIntegration();
  $enabled[] = 'stopwords';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkTokenizerIntegration();
  $enabled[] = 'tokenizer';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkTransliterationIntegration();
  $enabled[] = 'transliteration';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkAddHierarchyIntegration();
  $enabled[] = 'hierarchy';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkStemmerIntegration();
  $enabled[] = 'stemmer';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);
  $this
    ->checkNumberFieldBoostIntegration();
  $enabled[] = 'number_field_boost';
  sort($enabled);
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);

  // The 'add_url' processor is not available to be removed because it's
  // locked.
  $this
    ->checkUrlFieldIntegration();

  // Check the order of the displayed processors.
  $stages = [
    ProcessorInterface::STAGE_PREPROCESS_INDEX,
    ProcessorInterface::STAGE_PREPROCESS_QUERY,
    ProcessorInterface::STAGE_POSTPROCESS_QUERY,
  ];

  /** @var \Drupal\search_api\Processor\ProcessorInterface[] $processors */
  $processors = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createProcessorPlugins($this
    ->loadIndex());
  $page = $this
    ->getSession()
    ->getPage();
  foreach ($stages as $stage) {

    // Since the order of processors with the same weight is undefined, we
    // can't just use $index->getProcessorsByStage() and assertEquals().
    $previous_weight = NULL;
    $class = 'search-api-stage-wrapper-' . Html::cleanCssIdentifier($stage);

    /** @var \Behat\Mink\Element\NodeElement $element */
    foreach ($page
      ->findAll('css', ".{$class} tr.draggable") as $element) {

      // Since processors are shown right away when checked in the form, they
      // are even included in the form when disabled – just hidden. We can
      // check the "style" attribute to identify them.
      if (preg_match('/\\bsearch-api-processor-weight--([-a-z0-9]+)\\b/', $element
        ->getAttribute('class'), $m)) {
        $processor_id = str_replace('-', '_', $m[1]);
        $weight = $processors[$processor_id]
          ->getWeight($stage);
        if ($previous_weight !== NULL) {
          $this
            ->assertGreaterThanOrEqual($previous_weight, $weight, "Correct order of processor {$processor_id} in stage {$stage}");
        }
        $previous_weight = $weight;
      }
    }
  }

  // Check whether disabling processors also works correctly.
  $this
    ->loadProcessorsTab();
  $edit = [
    'status[stopwords]' => FALSE,
  ];
  $this
    ->submitForm($edit, 'Save');
  $enabled = array_values(array_diff($enabled, [
    'stopwords',
  ]));
  $actual_processors = array_keys($this
    ->loadIndex()
    ->getProcessors());
  sort($actual_processors);
  $this
    ->assertEquals($enabled, $actual_processors);

  // After disabling some datasource, all related processors should be
  // disabled also.
  $this
    ->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/edit');
  $this
    ->submitForm([
    'datasources[entity:user]' => FALSE,
  ], 'Save');
  $processors = $this
    ->loadIndex()
    ->getProcessors();
  $this
    ->assertArrayNotHasKey('role_filter', $processors);
  $this
    ->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/processors');
  $this
    ->assertSession()
    ->pageTextNotContains('Role filter');
}