You are here

public function QueryTest::testProcessingLevel in Search API 8

Tests that processing levels are working correctly.

@dataProvider testProcessingLevelDataProvider

Parameters

int $level: The processing level to test.

bool $hooks_and_processors_invoked: (optional) Whether hooks and processors should be invoked with this processing level.

File

tests/src/Kernel/System/QueryTest.php, line 93

Class

QueryTest
Tests query functionality.

Namespace

Drupal\Tests\search_api\Kernel\System

Code

public function testProcessingLevel($level, $hooks_and_processors_invoked = TRUE) {

  /** @var \Drupal\search_api\Processor\ProcessorInterface $processor */
  $processor = $this->container
    ->get('plugin.manager.search_api.processor')
    ->createInstance('search_api_test', [
    '#index' => $this->index,
  ]);
  $this->index
    ->addProcessor($processor)
    ->save();
  $query = $this->index
    ->query();
  if ($level != QueryInterface::PROCESSING_FULL) {
    $query
      ->setProcessingLevel($level);
  }
  $this
    ->assertEquals($level, $query
    ->getProcessingLevel());
  $query
    ->addTag('andrew_hill')
    ->addTag('views_search_api_test_view');
  \Drupal::messenger()
    ->deleteAll();
  $query
    ->execute();
  $messages = \Drupal::messenger()
    ->all();
  \Drupal::messenger()
    ->deleteAll();
  $methods = $this
    ->getCalledMethods('processor');
  if ($hooks_and_processors_invoked) {
    $expected = [
      MessengerInterface::TYPE_STATUS => [
        'Funky blue note',
        'Search id: ',
        'Freeland',
        'Stepping into tomorrow',
        'Llama',
      ],
    ];
    $this
      ->assertEquals($expected, $messages);
    $this
      ->assertNotEmpty($query
      ->getOption('tag query alter hook'));
    $this
      ->assertContains('preprocessSearchQuery', $methods);
    $this
      ->assertContains('postprocessSearchResults', $methods);
  }
  else {
    $this
      ->assertEmpty($messages);
    $this
      ->assertNull($query
      ->getOption('tag query alter hook'));
    $this
      ->assertNotContains('preprocessSearchQuery', $methods);
    $this
      ->assertNotContains('postprocessSearchResults', $methods);
  }
}