You are here

public function ServerTaskTest::testAutomaticTaskRemoval in Search API 8

Tests the correct automatic removal of tasks upon certain operations.

File

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

Class

ServerTaskTest
Tests whether the server task system works correctly.

Namespace

Drupal\Tests\search_api\Kernel\Server

Code

public function testAutomaticTaskRemoval() {

  // Create a second server and index and add tasks for them.
  $server2 = Server::create([
    'name' => 'Test Server 2',
    'id' => 'test_server_2',
    'status' => 1,
    'backend' => 'search_api_test',
  ]);
  $server2
    ->save();
  $this->taskManager
    ->addTask('removeIndex', $server2, $this->index);
  $index_values = $this->index
    ->toArray();
  unset($index_values['uuid']);
  $index_values['id'] = 'test_index_2';
  $index2 = Index::create($index_values);
  $index2
    ->save();

  // Reset the called backend methods.
  $this
    ->getCalledMethods('backend');

  // Verify that adding an index ignores all tasks related to that index.
  $this
    ->addTasks($index2);
  $this->server
    ->addIndex($this->index);
  $this
    ->assertEquals([
    'addIndex',
    'addIndex',
  ], $this
    ->getCalledMethods('backend'), 'Re-adding an index ignored all its tasks.');
  $this
    ->assertEquals(0, $this->serverTaskManager
    ->getCount($this->server), 'No pending tasks for server.');
  $this
    ->assertEquals(1, $this->serverTaskManager
    ->getCount(), 'The tasks of other servers were not touched.');

  // Verify that removing an index ignores all tasks related to that index.
  $this
    ->addTasks($index2);
  $this->server
    ->removeIndex($this->index);
  $this
    ->assertEquals([
    'addIndex',
    'removeIndex',
  ], $this
    ->getCalledMethods('backend'), 'Removing an index ignored all its tasks.');
  $this
    ->assertEquals(0, $this->serverTaskManager
    ->getCount($this->server), 'No pending tasks for server.');
  $this
    ->assertEquals(1, $this->serverTaskManager
    ->getCount(), 'The tasks of other servers were not touched.');

  // Verify that deleting all of an index's items ignores all other deletion
  // tasks related to that index.
  $this
    ->addTasks($index2);
  $this->server
    ->deleteAllIndexItems($this->index);
  $called_methods = [
    'addIndex',
    'removeIndex',
    'addIndex',
    'updateIndex',
    'deleteAllIndexItems',
  ];
  $this
    ->assertEquals($called_methods, $this
    ->getCalledMethods('backend'), 'Deleting all items of an index ignored all its deletion tasks.');
  $this
    ->assertEquals(0, $this->serverTaskManager
    ->getCount($this->server), 'No pending tasks for server.');
  $this
    ->assertEquals(1, $this->serverTaskManager
    ->getCount(), 'The tasks of other servers were not touched.');

  // Verify that removing all items from the server automatically removes all
  // item deletion tasks as well.
  $this
    ->addTasks($index2);
  $this->server
    ->deleteAllItems();

  // deleteAllIndexItems() is called twice – once for each index.
  $this
    ->assertEquals([
    'deleteAllIndexItems',
    'deleteAllIndexItems',
  ], $this
    ->getCalledMethods('backend'), "Deleting all items from a server didn't execute any tasks.");
  $this
    ->assertEquals(4, $this->serverTaskManager
    ->getCount($this->server), 'Deleting all items from a server removed all its item deletion tasks.');
  $this
    ->assertEquals(5, $this->serverTaskManager
    ->getCount(), 'The tasks of other servers were not touched.');

  // Verify that deleting a server also deletes all of its tasks.
  $this
    ->addTasks($index2);
  $this
    ->setError('backend', 'addIndex');
  $this
    ->setError('backend', 'updateIndex');
  $this
    ->setError('backend', 'removeIndex');
  $this
    ->setError('backend', 'deleteItems');
  $this
    ->setError('backend', 'deleteAllIndexItems');
  $this->server
    ->delete();
  $this
    ->assertEquals(0, $this->serverTaskManager
    ->getCount($this->server), 'Upon server deletion, all of its tasks were deleted, too.');
  $this
    ->assertEquals(1, $this->serverTaskManager
    ->getCount(), 'The tasks of other servers were not touched.');
}