public function ServerTaskTest::testAddIndex in Search API 8
Tests task system integration for the server's addIndex() method.
File
- tests/
src/ Kernel/ Server/ ServerTaskTest.php, line 113
Class
- ServerTaskTest
- Tests whether the server task system works correctly.
Namespace
Drupal\Tests\search_api\Kernel\ServerCode
public function testAddIndex() {
// Since we want to add the index, we should first remove it (even though it
// shouldn't matter – just for logic consistency).
$this->index
->setServer(NULL);
$this->index
->save();
// Set exception for addIndex() and reset the list of successful backend
// method calls.
$this
->setError('backend', 'addIndex');
$this
->getCalledMethods('backend');
// Try to add the index.
$this->server
->addIndex($this->index);
$this
->assertEquals([], $this
->getCalledMethods('backend'), 'addIndex correctly threw an exception.');
$tasks = $this
->getServerTasks();
if (count($tasks) == 1) {
$task_created = $tasks[0]->type === 'addIndex';
}
$this
->assertTrue(!empty($task_created), 'The addIndex task was successfully added.');
if ($tasks) {
$this
->assertEquals($this->index
->id(), $tasks[0]->index_id, 'The right index ID was used for the addIndex 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('addIndex', $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 addIndex() succeed again, then trigger the task execution with a cron
// run.
$this
->setError('backend', 'addIndex', FALSE);
search_api_cron();
$this
->assertEquals([], $this
->getServerTasks(), 'Server tasks were correctly executed.');
$this
->assertEquals([
'addIndex',
'updateIndex',
], $this
->getCalledMethods('backend'), 'Right methods were called during task execution.');
}