You are here

public function ServerTaskTest::testUpdateIndex in Search API 8

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

File

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

Class

ServerTaskTest
Tests whether the server task system works correctly.

Namespace

Drupal\Tests\search_api\Kernel\Server

Code

public function testUpdateIndex() {

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

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

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

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