You are here

public function ServerTaskTest::testDeleteItems in Search API 8

Tests task system integration for the server's deleteItems() method.

File

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

Class

ServerTaskTest
Tests whether the server task system works correctly.

Namespace

Drupal\Tests\search_api\Kernel\Server

Code

public function testDeleteItems() {

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

  // Try to update the index.
  $this->server
    ->deleteItems($this->index, []);
  $this
    ->assertEquals([], $this
    ->getCalledMethods('backend'), 'deleteItems correctly threw an exception.');
  $tasks = $this
    ->getServerTasks();
  if (count($tasks) == 1) {
    $task_created = $tasks[0]->type === 'deleteItems';
  }
  $this
    ->assertTrue(!empty($task_created), 'The deleteItems task was successfully added.');
  if ($tasks) {
    $this
      ->assertEquals($this->index
      ->id(), $tasks[0]->index_id, 'The right index ID was used for the deleteItems 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('deleteItems', $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 deleteItems() succeed again, then trigger the task execution
  // with a cron run.
  $this
    ->setError('backend', 'deleteItems', FALSE);
  search_api_cron();
  $this
    ->assertEquals([], $this
    ->getServerTasks(), 'Server tasks were correctly executed.');
  $this
    ->assertEquals([
    'deleteItems',
    'updateIndex',
  ], $this
    ->getCalledMethods('backend'), 'Right methods were called during task execution.');
}