You are here

public function ServerTaskTest::testDeleteAllIndexItems in Search API 8

Tests task system integration for the deleteAllIndexItems() method.

File

tests/src/Kernel/Server/ServerTaskTest.php, line 290

Class

ServerTaskTest
Tests whether the server task system works correctly.

Namespace

Drupal\Tests\search_api\Kernel\Server

Code

public function testDeleteAllIndexItems() {

  // Set exception for deleteAllIndexItems().
  $this
    ->setError('backend', 'deleteAllIndexItems');

  // Try to update the index.
  $this->server
    ->deleteAllIndexItems($this->index);
  $this
    ->assertEquals([], $this
    ->getCalledMethods('backend'), 'deleteAllIndexItems correctly threw an exception.');
  $tasks = $this
    ->getServerTasks();
  if (count($tasks) == 1) {
    $task_created = $tasks[0]->type === 'deleteAllIndexItems';
  }
  $this
    ->assertTrue(!empty($task_created), 'The deleteAllIndexItems task was successfully added.');
  if ($tasks) {
    $this
      ->assertEquals($this->index
      ->id(), $tasks[0]->index_id, 'The right index ID was used for the deleteAllIndexItems task.');
  }

  // Check whether other task-system-integrated methods now fail, too.
  $this->server
    ->updateIndex($this->index);
  $this
    ->assertEquals([], $this
    ->getCalledMethods('backend'), 'updateIndex was not executed.');
  $tasks = $this
    ->getServerTasks();
  if (count($tasks) == 2) {
    $this
      ->assertTrue(TRUE, "Second task ('updateIndex') was added.");
    $this
      ->assertEquals('deleteAllIndexItems', $tasks[0]->type, 'First task stayed the same.');
    $this
      ->assertEquals('updateIndex', $tasks[1]->type, 'New task was queued as last.');
  }
  else {
    $this
      ->fail("Second task (updateIndex) was not added.");
  }

  // Let deleteAllIndexItems() succeed again, then trigger the task execution
  // with a call to indexItems().
  $this
    ->setError('backend', 'deleteAllIndexItems', FALSE);
  $this->server
    ->indexItems($this->index, []);
  $expected_methods = [
    'deleteAllIndexItems',
    'updateIndex',
    'indexItems',
  ];
  $this
    ->assertEquals([], $this
    ->getServerTasks(), 'Server tasks were correctly executed.');
  $this
    ->assertEquals($expected_methods, $this
    ->getCalledMethods('backend'), 'Right methods were called during task execution.');
}