protected function SearchApiWebTest::checkServerTasks in Search API 7
Tests whether the server tasks system works correctly.
Uses the "search_api_test_error_state" variable to trigger exceptions in the test service class and asserts that the Search API reacts correctly and re-attempts the operation on the next cron run.
File
- ./
search_api.test, line 601 - Contains the SearchApiWebTest and the SearchApiUnitTest classes.
Class
- SearchApiWebTest
- Class for testing Search API functionality via the UI.
Code
protected function checkServerTasks() {
// Make sure none of the previous operations added any tasks.
$task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')
->fetchField();
$this
->assertEqual($task_count, 0, 'No server tasks were previously saved.');
// Set error state for test service, so all operations will fail.
variable_set('search_api_test_error_state', TRUE);
// Delete some items.
$this
->drupalGet('search_api_test/delete/8');
$this
->drupalGet('search_api_test/delete/12');
// Assert that the indexed items haven't changed yet.
$results = $this
->doSearch();
$this
->assertEqual(array_keys($results['results']), array(
2,
5,
8,
11,
12,
13,
14,
), 'During error state, no indexed items were deleted.');
// Check that tasks were correctly inserted.
$task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')
->fetchField();
$this
->assertEqual($task_count, 2, 'Server tasks for deleted items were saved.');
// Now reset the error state variable and run cron to delete the items.
variable_set('search_api_test_error_state', FALSE);
$this
->cronRun();
// Assert that the indexed items were indeed deleted from the server.
$results = $this
->doSearch();
$this
->assertEqual(array_keys($results['results']), array(
2,
5,
11,
13,
14,
), 'Pending "delete item" server tasks were correctly executed during the cron run.');
// Check that the tasks were correctly deleted.
$task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')
->fetchField();
$this
->assertEqual($task_count, 0, 'Server tasks were correctly deleted after being executed.');
// Now we first delete more items, then disable the server (thereby removing
// the index from it) – all while in error state.
variable_set('search_api_test_error_state', TRUE);
$this
->drupalGet('search_api_test/delete/14');
$this
->drupalGet('search_api_test/delete/2');
$settings['enabled'] = 0;
$this
->drupalPost("admin/config/search/search_api/server/{$this->server_id}/edit", $settings, t('Save settings'));
// Check whether the index was correctly removed from the server.
$this
->assertEqual($this
->index()
->server(), NULL, 'The index was successfully set to have no server.');
$exception = FALSE;
try {
$this
->doSearch();
} catch (SearchApiException $e) {
$exception = TRUE;
}
$this
->assertTrue($exception, 'Searching on the index failed with an exception.');
// Check that only one task – to remove the index from the server – is now
// present in the tasks table.
$task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')
->fetchField();
$this
->assertEqual($task_count, 1, 'Only the "remove index" task is present in the server tasks.');
// Reset the error state variable, re-enable the server.
variable_set('search_api_test_error_state', FALSE);
$settings['enabled'] = 1;
$this
->drupalPost("admin/config/search/search_api/server/{$this->server_id}/edit", $settings, t('Save settings'));
// Check whether the index was really removed from the server now.
$server = $this
->server();
$this
->assertTrue(empty($server->options['indexes'][$this->index_id]), 'The index was removed from the server after cron ran.');
$task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')
->fetchField();
$this
->assertEqual($task_count, 0, 'Server tasks were correctly deleted after being executed.');
// Put the index back on the server and index some items for the next tests.
$settings = array(
'server' => $this->server_id,
);
$this
->drupalPost("admin/config/search/search_api/index/{$this->index_id}/edit", $settings, t('Save settings'));
$this
->cronRun();
}