You are here

public function TaskTest::testMultipleTasks in Search API 8

Tests that multiple pending tasks are treated correctly.

File

tests/src/Kernel/System/TaskTest.php, line 205

Class

TaskTest
Tests whether the Search API task system works correctly.

Namespace

Drupal\Tests\search_api\Kernel\System

Code

public function testMultipleTasks() {

  // Add some tasks to the system. We use explicit indexes since we want to
  // verify that the tasks are executed in a different order than the one they
  // were added, if appropriate $conditions parameters are given.
  $tasks = [];
  $tasks[0] = $this
    ->addTask('success', $this->server, $this->index, [
    'foo' => 1,
    'bar',
  ]);
  $tasks[6] = $this
    ->addTask('fail');
  $tasks[1] = $this
    ->addTask('success', $this->server, NULL, TRUE);
  $tasks[4] = $this
    ->addTask('success', NULL, NULL, 1);
  $tasks[2] = $this
    ->addTask('fail', $this->server, $this->index);
  $tasks[5] = $this
    ->addTask('success');
  $tasks[3] = $this
    ->addTask('success', NULL, $this->index);
  $num = count($tasks);
  $this
    ->assertEquals($num, $this->taskManager
    ->getTasksCount());
  $this->taskManager
    ->executeSingleTask();
  $this
    ->assertEquals(--$num, $this->taskManager
    ->getTasksCount());
  $this->taskManager
    ->executeSingleTask([
    'server_id' => $this->server
      ->id(),
  ]);
  $this
    ->assertEquals(--$num, $this->taskManager
    ->getTasksCount());
  try {
    $this->taskManager
      ->executeAllTasks([
      'server_id' => $this->server
        ->id(),
    ]);
    $this
      ->fail('Exception expected');
  } catch (SearchApiException $e) {
    $this
      ->assertEquals('fail', $e
      ->getMessage());
  }
  $this
    ->assertEquals($num, $this->taskManager
    ->getTasksCount());
  $tasks[2]
    ->delete();
  $this
    ->assertEquals(--$num, $this->taskManager
    ->getTasksCount());
  $this->taskManager
    ->executeSingleTask([
    'index_id' => $this->index
      ->id(),
  ]);
  $this
    ->assertEquals(--$num, $this->taskManager
    ->getTasksCount());
  $this->taskManager
    ->executeAllTasks([
    'type' => [
      'search_api_test_tasks.success',
      'foobar',
    ],
  ]);
  $this
    ->assertEquals($num -= 2, $this->taskManager
    ->getTasksCount());

  // Need to include some data so the new task won't count as a duplicate.
  $tasks[7] = $this
    ->addTask('success', NULL, NULL, 1);
  $tasks[8] = $this
    ->addTask('success', NULL, NULL, 2);
  $tasks[9] = $this
    ->addTask('fail', NULL, NULL, 3);
  $tasks[10] = $this
    ->addTask('success', NULL, NULL, 4);
  $num += 4;
  try {
    $this->taskManager
      ->executeAllTasks();
    $this
      ->fail('Exception expected');
  } catch (SearchApiException $e) {
    $this
      ->assertEquals('fail', $e
      ->getMessage());
  }
  $this
    ->assertEquals($num, $this->taskManager
    ->getTasksCount());
  $tasks[6]
    ->delete();
  $this
    ->assertEquals(--$num, $this->taskManager
    ->getTasksCount());
  try {
    $this->taskManager
      ->executeAllTasks();
    $this
      ->fail('Exception expected');
  } catch (SearchApiException $e) {
    $this
      ->assertEquals('fail', $e
      ->getMessage());
  }
  $this
    ->assertEquals($num -= 2, $this->taskManager
    ->getTasksCount());
  $tasks[9]
    ->delete();
  $this
    ->assertEquals(--$num, $this->taskManager
    ->getTasksCount());
  $this->taskManager
    ->executeAllTasks();
  $this
    ->assertEquals(0, $this->taskManager
    ->getTasksCount());
  $to_array = function (TaskInterface $task) {
    return $task
      ->toArray();
  };
  $tasks = array_map($to_array, $tasks);
  $this
    ->assertEquals($tasks, $this->taskWorker
    ->getEventLog());
}