updated to 7.x-1.11

This commit is contained in:
Bachir Soussi Chiadmi
2014-02-07 10:01:18 +01:00
parent a30917d1d2
commit cf03e9ca52
69 changed files with 4629 additions and 1557 deletions

View File

@@ -1,29 +1,66 @@
<?php
/**
* Class for testing Search API web functionality.
* @file
* Contains the SearchApiWebTest and the SearchApiUnitTest classes.
*/
/**
* Class for testing Search API functionality via the UI.
*/
class SearchApiWebTest extends DrupalWebTestCase {
/**
* The machine name of the created test server.
*
* @var string
*/
protected $server_id;
/**
* The machine name of the created test index.
*
* @var string
*/
protected $index_id;
/**
* Overrides DrupalWebTestCase::assertText().
*
* Changes the default message to be just the text checked for.
*/
protected function assertText($text, $message = '', $group = 'Other') {
return parent::assertText($text, $message ? $message : $text, $group);
}
/**
* Overrides DrupalWebTestCase::drupalGet().
*
* Additionally asserts that the HTTP request returned a 200 status code.
*/
protected function drupalGet($path, array $options = array(), array $headers = array()) {
$ret = parent::drupalGet($path, $options, $headers);
$this->assertResponse(200, 'HTTP code 200 returned.');
return $ret;
}
/**
* Overrides DrupalWebTestCase::drupalPost().
*
* Additionally asserts that the HTTP request returned a 200 status code.
*/
protected function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
$ret = parent::drupalPost($path, $edit, $submit, $options, $headers, $form_html_id, $extra_post);
$this->assertResponse(200, 'HTTP code 200 returned.');
return $ret;
}
/**
* Returns information about this test case.
*
* @return array
* An array with information about this test case.
*/
public static function getInfo() {
return array(
'name' => 'Test search API framework',
@@ -32,24 +69,34 @@ class SearchApiWebTest extends DrupalWebTestCase {
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp('entity', 'search_api', 'search_api_test');
}
/**
* Tests correct admin UI, indexing and search behavior.
*
* We only use a single test method to avoid wasting ressources on setting up
* the test environment multiple times. This will be the only method called
* by the Simpletest framework (since the method name starts with "test"). It
* in turn calls other methdos that set up the environment in a certain way
* and then run tests on it.
*/
public function testFramework() {
$this->drupalLogin($this->drupalCreateUser(array('administer search_api')));
// @todo Why is there no default index?
//$this->deleteDefaultIndex();
$this->insertItems();
$this->checkOverview1();
$this->createIndex();
$this->insertItems(5);
$this->insertItems();
$this->createServer();
$this->checkOverview2();
$this->checkOverview();
$this->enableIndex();
$this->searchNoResults();
$this->indexItems();
$this->searchSuccess();
$this->checkIndexingOrder();
$this->editServer();
$this->clearIndex();
$this->searchNoResults();
@@ -57,57 +104,64 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->disableModules();
}
protected function deleteDefaultIndex() {
$this->drupalPost('admin/config/search/search_api/index/default_node_index/delete', array(), t('Confirm'));
/**
* Returns the test server in use by this test case.
*
* @return SearchApiServer
* The test server.
*/
protected function server() {
return search_api_server_load($this->server_id, TRUE);
}
protected function insertItems($offset = 0) {
/**
* Returns the test index in use by this test case.
*
* @return SearchApiIndex
* The test index.
*/
protected function index() {
return search_api_index_load($this->index_id, TRUE);
}
/**
* Inserts some test items into the database, via the test module.
*
* @param int $number
* The number of items to insert.
*
* @see insertItem()
*/
protected function insertItems($number = 5) {
$count = db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField();
$this->insertItem(array(
'id' => $offset + 1,
'title' => 'Title 1',
'body' => 'Body text 1.',
'type' => 'Item',
));
$this->insertItem(array(
'id' => $offset + 2,
'title' => 'Title 2',
'body' => 'Body text 2.',
'type' => 'Item',
));
$this->insertItem(array(
'id' => $offset + 3,
'title' => 'Title 3',
'body' => 'Body text 3.',
'type' => 'Item',
));
$this->insertItem(array(
'id' => $offset + 4,
'title' => 'Title 4',
'body' => 'Body text 4.',
'type' => 'Page',
));
$this->insertItem(array(
'id' => $offset + 5,
'title' => 'Title 5',
'body' => 'Body text 5.',
'type' => 'Page',
));
for ($i = 1; $i <= $number; ++$i) {
$id = $count + $i;
$this->insertItem(array(
'id' => $id,
'title' => "Title $id",
'body' => "Body text $id.",
'type' => 'Item',
));
}
$count = db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField() - $count;
$this->assertEqual($count, 5, '5 items successfully inserted.');
$this->assertEqual($count, $number, "$number items successfully inserted.");
}
protected function insertItem($values) {
/**
* Helper function for inserting a single test item.
*
* @param array $values
* The property values of the test item.
*
* @see search_api_test_insert_item()
*/
protected function insertItem(array $values) {
$this->drupalPost('search_api_test/insert', $values, t('Save'));
}
protected function checkOverview1() {
// This test fails for no apparent reason for drupal.org test bots.
// Commenting them out for now.
//$this->drupalGet('admin/config/search/search_api');
//$this->assertText(t('There are no search servers or indexes defined yet.'), '"No servers" message is displayed.');
}
/**
* Creates a test index via the UI and tests whether this works correctly.
*/
protected function createIndex() {
$values = array(
'name' => '',
@@ -136,7 +190,7 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->assertText(t('The index was successfully created. Please set up its indexed fields now.'), 'The index was successfully created.');
$found = strpos($this->getUrl(), 'admin/config/search/search_api/index/' . $id) !== FALSE;
$this->assertTrue($found, 'Correct redirect.');
$index = search_api_index_load($id, TRUE);
$index = $this->index();
$this->assertEqual($index->name, $values['name'], 'Name correctly inserted.');
$this->assertEqual($index->item_type, $values['item_type'], 'Index item type correctly inserted.');
$this->assertFalse($index->enabled, 'Status correctly inserted.');
@@ -210,18 +264,18 @@ class SearchApiWebTest extends DrupalWebTestCase {
'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][fields][parent:body]' => 1,
);
$this->drupalPost(NULL, $values, t('Save configuration'));
$this->assertText(t("The search index' workflow was successfully edited. All content was scheduled for re-indexing so the new settings can take effect."), 'Workflow successfully edited.');
$this->assertText(t("The indexing workflow was successfully edited. All content was scheduled for re-indexing so the new settings can take effect."), 'Workflow successfully edited.');
$this->drupalGet("admin/config/search/search_api/index/$id");
$this->assertTitle('Search API test index | Drupal', 'Correct title when viewing index.');
$this->assertText('An index used for testing.', 'Description displayed.');
$this->assertText('Search API test entity', 'Item type displayed.');
$this->assertText(format_plural(1, '1 item per cron batch.', '@count items per cron batch.'), 'Cron batch size displayed.');
$this->drupalGet("admin/config/search/search_api/index/$id/status");
$this->assertText(t('The index is currently disabled.'), '"Disabled" status displayed.');
$this->assertText(t('disabled'), '"Disabled" status displayed.');
}
/**
* Creates a test server via the UI and tests whether this works correctly.
*/
protected function createServer() {
$values = array(
'name' => '',
@@ -251,7 +305,7 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->assertText(t('The server was successfully created.'));
$found = strpos($this->getUrl(), 'admin/config/search/search_api/server/' . $id) !== FALSE;
$this->assertTrue($found, 'Correct redirect.');
$server = search_api_server_load($id, TRUE);
$server = $this->server();
$this->assertEqual($server->name, $values['name'], 'Name correctly inserted.');
$this->assertTrue($server->enabled, 'Status correctly inserted.');
$this->assertEqual($server->description, $values['description'], 'Description correctly inserted.');
@@ -260,17 +314,22 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->assertTitle('Search API test server | Drupal', 'Correct title when viewing server.');
$this->assertText('A server used for testing.', 'Description displayed.');
$this->assertText('search_api_test_service', 'Service name displayed.');
$this->assertText('search_api_test_service description', 'Service description displayed.');
$this->assertText('search_api_test foo bar', 'Service options displayed.');
}
protected function checkOverview2() {
/**
* Checks whether the server and index are correctly listed in the overview.
*/
protected function checkOverview() {
$this->drupalGet('admin/config/search/search_api');
$this->assertText('Search API test server', 'Server displayed.');
$this->assertText('Search API test index', 'Index displayed.');
$this->assertNoText(t('There are no search servers or indexes defined yet.'), '"No servers" message not displayed.');
}
/**
* Moves the index onto the server and enables it.
*/
protected function enableIndex() {
$values = array(
'server' => $this->server_id,
@@ -283,24 +342,60 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->assertText(t('The index was successfully enabled.'));
}
/**
* Asserts that a search on the index works but yields no results.
*
* This is the case since no items should have been indexed yet.
*/
protected function searchNoResults() {
$this->drupalGet('search_api_test/query/' . $this->index_id);
$this->assertText('result count = 0', 'No search results returned without indexing.');
$this->assertText('results = ()', 'No search results returned without indexing.');
$results = $this->doSearch();
$this->assertEqual($results['result count'], 0, 'No search results returned without indexing.');
$this->assertEqual(array_keys($results['results']), array(), 'No search results returned without indexing.');
}
/**
* Executes a search on the test index.
*
* Helper method used for testing search results.
*
* @param int|null $offset
* (optional) The offset for the returned results.
* @param int|null $limit
* (optional) The limit for the returned results.
*
* @return array
* Search results as specified by SearchApiQueryInterface::execute().
*/
protected function doSearch($offset = NULL, $limit = NULL) {
// Since we change server and index settings via the UI (and, therefore, in
// different page requests), the static cache in this page request
// (executing the tests) will get stale. Therefore, we clear it before
// executing the search.
$this->index();
$this->server();
$query = search_api_query($this->index_id);
if ($offset || $limit) {
$query->range($offset, $limit);
}
return $query->execute();
}
/**
* Tests indexing via the UI "Index now" functionality.
*
* Asserts that errors during indexing are handled properly and that the
* status readings work.
*/
protected function indexItems() {
$this->drupalGet("admin/config/search/search_api/index/{$this->index_id}/status");
$this->assertText(t('The index is currently enabled.'), '"Enabled" status displayed.');
$this->assertText(t('All items still need to be indexed (@total total).', array('@total' => 10)), 'Correct index status displayed.');
$this->assertText(t('Index now'), '"Index now" button found.');
$this->assertText(t('Clear index'), '"Clear index" button found.');
$this->assertNoText(t('Re-index content'), '"Re-index" button not found.');
$this->checkIndexStatus();
// Here we test the indexing + the warning message when some items
// can not be indexed.
// The server refuses (for test purpose) to index items with IDs that are
// multiples of 8 unless the "search_api_test_index_all" variable is set.
// cannot be indexed.
// The server refuses (for test purpose) to index the item that has the same
// ID as the "search_api_test_indexing_break" variable (default: 8).
// Therefore, if we try to index 8 items, only the first seven will be
// successfully indexed and a warning should be displayed.
$values = array(
'limit' => 8,
);
@@ -308,11 +403,14 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->assertText(t('Successfully indexed @count items.', array('@count' => 7)));
$this->assertText(t('1 item could not be indexed. Check the logs for details.'), 'Index errors warning is displayed.');
$this->assertNoText(t("Couldn't index items. Check the logs for details."), "Index error isn't displayed.");
$this->assertText(t('About @percentage% of all items have been indexed in their latest version (@indexed / @total).', array('@indexed' => 7, '@total' => 10, '@percentage' => 70)), 'Correct index status displayed.');
$this->assertText(t('Re-indexing'), '"Re-index" button found.');
$this->checkIndexStatus(7);
// Here we're testing the error message when no item could be indexed.
// The item with ID 8 is still not indexed.
// The item with ID 8 is still not indexed, but it will be the first to be
// indexed now. Therefore, if we try to index a single items, only item 8
// will be passed to the server, which will reject it and no items will be
// indexed. Since normally this signifies a more serious error than when
// only some items couldn't be indexed, this is handled differently.
$values = array(
'limit' => 1,
);
@@ -321,8 +419,10 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->assertNoText(t('1 item could not be indexed. Check the logs for details.'), "Index errors warning isn't displayed.");
$this->assertText(t("Couldn't index items. Check the logs for details."), 'Index error is displayed.');
// Here we test the indexing of all the remaining items.
variable_set('search_api_test_index_all', TRUE);
// No we set the "search_api_test_indexing_break" variable to 0, so all
// items will be indexed. The remaining items (8, 9, 10) should therefore
// be successfully indexed and no warning should show.
variable_set('search_api_test_indexing_break', 0);
$values = array(
'limit' => -1,
);
@@ -330,20 +430,249 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->assertText(t('Successfully indexed @count items.', array('@count' => 3)));
$this->assertNoText(t("Some items couldn't be indexed. Check the logs for details."), "Index errors warning isn't displayed.");
$this->assertNoText(t("Couldn't index items. Check the logs for details."), "Index error isn't displayed.");
$this->assertText(t('All items have been indexed (@indexed / @total).', array('@indexed' => 10, '@total' => 10)), 'Correct index status displayed.');
$this->assertNoText(t('Index now'), '"Index now" button no longer displayed.');
$this->checkIndexStatus(10);
// Reset the static cache for the server.
$this->server();
}
/**
* Checks whether the index's "Status" tab shows the correct values.
*
* Helper method used by indexItems() and others.
*
* The internal browser will point to the index's "Status" tab after this
* method is called.
*
* @param int $indexed
* (optional) The number of items that should be indexed at the moment.
* Defaults to 0.
* @param int $total
* (optional) The (correct) total number of items. Defaults to 10.
* @param bool $check_buttons
* (optional) Whether to check for the correct presence/absence of buttons.
* Defaults to TRUE.
* @param int|null $on_server
* (optional) The number of items actually on the server. Defaults to
* $indexed.
*/
protected function checkIndexStatus($indexed = 0, $total = 10, $check_buttons = TRUE, $on_server = NULL) {
$url = "admin/config/search/search_api/index/{$this->index_id}";
if (strpos($this->url, $url) === FALSE) {
$this->drupalGet($url);
}
$index_status = t('@indexed/@total indexed', array('@indexed' => $indexed, '@total' => $total));
$this->assertText($index_status, 'Correct index status displayed.');
if (!isset($on_server)) {
$on_server = $indexed;
}
$info = format_plural($on_server, 'There is 1 item indexed on the server for this index.', 'There are @count items indexed on the server for this index.');
$this->assertText(t('Server index status'), 'Server index status displayed.');
$this->assertText($info, 'Correct server index status displayed.');
if (!$check_buttons) {
return;
}
$this->assertText(t('enabled'), '"Enabled" status displayed.');
if ($indexed == $total) {
$this->assertRaw('disabled="disabled"', '"Index now" form disabled.');
}
else {
$this->assertNoRaw('disabled="disabled"', '"Index now" form enabled.');
}
}
/**
* Tests whether searches yield the right results after indexing.
*
* The test server only implements range functionality, no kind of fulltext
* search capabilities, so we can only test for that.
*/
protected function searchSuccess() {
$this->drupalGet('search_api_test/query/' . $this->index_id);
$this->assertText('result count = 10', 'Correct search result count returned after indexing.');
$this->assertText('results = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)', 'Correct search results returned after indexing.');
$results = $this->doSearch();
$this->assertEqual($results['result count'], 10, 'Correct search result count returned after indexing.');
$this->assertEqual(array_keys($results['results']), array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 'Correct search results returned after indexing.');
$this->drupalGet('search_api_test/query/' . $this->index_id . '/foo/2/4');
$this->assertText('result count = 10', 'Correct search result count with ranged query.');
$this->assertText('results = (3, 4, 5, 6)', 'Correct search results with ranged query.');
$results = $this->doSearch(2, 4);
$this->assertEqual($results['result count'], 10, 'Correct search result count with ranged query.');
$this->assertEqual(array_keys($results['results']), array(3, 4, 5, 6), 'Correct search results with ranged query.');
}
/**
* Tests whether items are indexed in the right order.
*
* The indexing order should always be that new items are indexed before
* changed ones, and only then the changed items in the order of their change.
*
* This method also assures that this behavior is even observed when indexing
* temporarily fails.
*
* @see https://drupal.org/node/2115127
*/
protected function checkIndexingOrder() {
// Set cron batch size to 1 so not all items will get indexed right away.
// This also ensures that later, when indexing of a single item will be
// rejected by using the "search_api_test_indexing_break" variable, this
// will have the effect of rejecting "all" items of a batch (since that
// batch only consists of a single item).
$values = array(
'options[cron_limit]' => 1,
);
$this->drupalPost("admin/config/search/search_api/index/{$this->index_id}/edit", $values, t('Save settings'));
$this->assertText(t('The search index was successfully edited.'));
// Manually clear the server's item storage that way, the items will still
// count as indexed for the Search API, but won't be returned in searches.
// We do this so we have finer-grained control over the order in which items
// are indexed.
$this->server()->deleteItems();
$results = $this->doSearch();
$this->assertEqual($results['result count'], 0, 'Indexed items were successfully deleted from the server.');
$this->assertEqual(array_keys($results['results']), array(), 'Indexed items were successfully deleted from the server.');
// Now insert some new items, and mark others as changed. Make sure that
// each action has a unique timestamp, so the order will be correct.
$this->drupalGet('search_api_test/touch/8');
$this->insertItems(1);// item 11
sleep(1);
$this->drupalGet('search_api_test/touch/2');
$this->insertItems(1);// item 12
sleep(1);
$this->drupalGet('search_api_test/touch/5');
$this->insertItems(1);// item 13
sleep(1);
$this->drupalGet('search_api_test/touch/8');
$this->insertItems(1); // item 14
// Check whether the status display is right.
$this->checkIndexStatus(7, 14, FALSE, 0);
// Indexing order should now be: 11, 12, 13, 14, 8, 2, 4. Let's try it out!
// First manually index one item, and see if it's 11.
$values = array(
'limit' => 1,
);
$this->drupalPost(NULL, $values, t('Index now'));
$this->assertText(t('Successfully indexed @count item.', array('@count' => 1)));
$this->assertNoText(t("Some items couldn't be indexed. Check the logs for details."), "Index errors warning isn't displayed.");
$this->assertNoText(t("Couldn't index items. Check the logs for details."), "Index error isn't displayed.");
$this->checkIndexStatus(8, 14, FALSE, 1);
$results = $this->doSearch();
$this->assertEqual($results['result count'], 1, 'Indexing order test 1: correct result count.');
$this->assertEqual(array_keys($results['results']), array(11), 'Indexing order test 1: correct results.');
// Now index with a cron run, but stop at item 8.
variable_set('search_api_test_indexing_break', 8);
$this->cronRun();
// Now just the four new items should have been indexed.
$results = $this->doSearch();
$this->assertEqual($results['result count'], 4, 'Indexing order test 2: correct result count.');
$this->assertEqual(array_keys($results['results']), array(11, 12, 13, 14), 'Indexing order test 2: correct results.');
// This time stop at item 5 (should be the last one).
variable_set('search_api_test_indexing_break', 5);
$this->cronRun();
// Now all new and changed items should have been indexed, except item 5.
$results = $this->doSearch();
$this->assertEqual($results['result count'], 6, 'Indexing order test 3: correct result count.');
$this->assertEqual(array_keys($results['results']), array(2, 8, 11, 12, 13, 14), 'Indexing order test 3: correct results.');
// Index the remaining item.
variable_set('search_api_test_indexing_break', 0);
$this->cronRun();
// Now all new and changed items should have been indexed.
$results = $this->doSearch();
$this->assertEqual($results['result count'], 7, 'Indexing order test 4: correct result count.');
$this->assertEqual(array_keys($results['results']), array(2, 5, 8, 11, 12, 13, 14), 'Indexing order test 4: correct results.');
}
/**
* 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.
*/
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();
}
/**
* Tests whether editing the server works correctly.
*/
protected function editServer() {
$values = array(
'name' => 'test-name-foo',
@@ -357,19 +686,49 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->assertText('test-test-baz', 'Service options changed.');
}
/**
* Tests whether clearing the index works correctly.
*/
protected function clearIndex() {
$this->drupalPost("admin/config/search/search_api/index/{$this->index_id}/status", array(), t('Clear index'));
$this->drupalPost("admin/config/search/search_api/index/{$this->index_id}", array(), t('Clear all indexed data'));
$this->drupalPost(NULL, array(), t('Confirm'));
$this->assertText(t('The index was successfully cleared.'));
$this->assertText(t('All items still need to be indexed (@total total).', array('@total' => 10)), 'Correct index status displayed.');
$this->assertText(t('@indexed/@total indexed', array('@indexed' => 0, '@total' => 14)), 'Correct index status displayed.');
}
/**
* Tests whether deleting the server works correctly.
*
* The index still lying on the server should be disabled and removed from it.
* Also, any tasks with that server's ID should be deleted.
*/
protected function deleteServer() {
// Insert some dummy tasks to check for.
$server = $this->server();
search_api_server_tasks_add($server, 'foo');
search_api_server_tasks_add($server, 'bar', $this->index());
$task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')->fetchField();
$this->assertEqual($task_count, 2, 'Dummy tasks were added.');
// Delete the server.
$this->drupalPost("admin/config/search/search_api/server/{$this->server_id}/delete", array(), t('Confirm'));
$this->assertNoText('test-name-foo', 'Server no longer listed.');
$this->drupalGet("admin/config/search/search_api/index/{$this->index_id}/status");
$this->assertText(t('The index is currently disabled.'), 'The index was disabled and removed from the server.');
$this->drupalGet("admin/config/search/search_api/index/{$this->index_id}");
$this->assertNoText(t('Server'), 'The index was removed from the server.');
$this->assertText(t('disabled'), 'The index was disabled.');
// Check whether the tasks were correctly deleted.
$task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')->fetchField();
$this->assertEqual($task_count, 0, 'Remaining server tasks were correctly deleted.');
}
/**
* Tests whether disabling and uninstalling the modules works correctly.
*
* This will disable and uninstall both the test module and the Search API. It
* asserts that this works correctly (since the server has been deleted in
* deleteServer()) and that all associated tables and variables are removed.
*/
protected function disableModules() {
module_disable(array('search_api_test'), FALSE);
$this->assertFalse(module_exists('search_api_test'), 'Test module was successfully disabled.');
@@ -384,7 +743,7 @@ class SearchApiWebTest extends DrupalWebTestCase {
$this->assertFalse(db_table_exists('search_api_server'), 'Search server table was successfully removed.');
$this->assertFalse(db_table_exists('search_api_index'), 'Search index table was successfully removed.');
$this->assertFalse(db_table_exists('search_api_item'), 'Index items table was successfully removed.');
$this->assertNull(variable_get('search_api_tasks'), 'Tasks variable was correctly removed.');
$this->assertFalse(db_table_exists('search_api_task'), 'Server tasks table was successfully removed.');
$this->assertNull(variable_get('search_api_index_worker_callback_runtime'), 'Worker runtime variable was correctly removed.');
}
@@ -398,8 +757,19 @@ class SearchApiWebTest extends DrupalWebTestCase {
*/
class SearchApiUnitTest extends DrupalWebTestCase {
/**
* The index used by these tests.
*
* @var SearchApIindex
*/
protected $index;
/**
* Overrides DrupalTestCase::assertEqual().
*
* For arrays, checks whether all array keys are mapped the same in both
* arrays recursively, while ignoring their order.
*/
protected function assertEqual($first, $second, $message = '', $group = 'Other') {
if (is_array($first) && is_array($second)) {
return $this->assertTrue($this->deepEquals($first, $second), $message, $group);
@@ -409,6 +779,20 @@ class SearchApiUnitTest extends DrupalWebTestCase {
}
}
/**
* Tests whether two values are equal.
*
* For arrays, this is done by comparing the key/value pairs recursively
* instead of checking for simple equality.
*
* @param mixed $first
* The first value.
* @param mixed $second
* The second value.
*
* @return bool
* TRUE if the two values are equal, FALSE otherwise.
*/
protected function deepEquals($first, $second) {
if (!is_array($first) || !is_array($second)) {
return $first == $second;
@@ -424,6 +808,12 @@ class SearchApiUnitTest extends DrupalWebTestCase {
return empty($second);
}
/**
* Returns information about this test case.
*
* @return array
* An array with information about this test case.
*/
public static function getInfo() {
return array(
'name' => 'Test search API components',
@@ -432,6 +822,9 @@ class SearchApiUnitTest extends DrupalWebTestCase {
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp('entity', 'search_api');
$this->index = entity_create('search_api_index', array(
@@ -455,6 +848,12 @@ class SearchApiUnitTest extends DrupalWebTestCase {
));
}
/**
* Tests the functionality of several components of the module.
*
* This is the single test method called by the Simpletest framework. It in
* turn calls other helper methods to test specific functionality.
*/
public function testUnits() {
$this->checkQueryParseKeys();
$this->checkIgnoreCaseProcessor();
@@ -462,11 +861,13 @@ class SearchApiUnitTest extends DrupalWebTestCase {
$this->checkHtmlFilter();
}
public function checkQueryParseKeys() {
/**
* Checks whether the keys are parsed correctly by the query class.
*/
protected function checkQueryParseKeys() {
$options['parse mode'] = 'direct';
$mode = &$options['parse mode'];
$query = new SearchApiQuery($this->index, $options);
$modes = $query->parseModes();
$query->keys('foo');
$this->assertEqual($query->getKeys(), 'foo', '"Direct query" parse mode, test 1.');
@@ -499,8 +900,10 @@ class SearchApiUnitTest extends DrupalWebTestCase {
$this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'Münster'), '"Multiple terms" parse mode, test 4.');
}
public function checkIgnoreCaseProcessor() {
$types = search_api_field_types();
/**
* Tests the functionality of the "Ignore case" processor.
*/
protected function checkIgnoreCaseProcessor() {
$orig = 'Foo bar BaZ, ÄÖÜÀÁ<>»«.';
$processed = drupal_strtolower($orig);
$items = array(
@@ -566,7 +969,10 @@ class SearchApiUnitTest extends DrupalWebTestCase {
$this->assertEqual($query->getFilter()->getFilters(), $filters2, 'Filters were processed correctly.');
}
public function checkTokenizer() {
/**
* Tests the functionality of the "Tokenizer" processor.
*/
protected function checkTokenizer() {
$orig = 'Foo bar1 BaZ, La-la-la.';
$processed1 = array(
array(
@@ -648,7 +1054,10 @@ class SearchApiUnitTest extends DrupalWebTestCase {
$this->assertEqual($query->getKeys(), 'foo"b r b z"foob r1', 'Search keys were processed correctly.');
}
public function checkHtmlFilter() {
/**
* Tests the functionality of the "HTML filter" processor.
*/
protected function checkHtmlFilter() {
$orig = <<<END
This is <em lang="en" title =
"something">a test</em>.