search_api_db.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Class for testing index and search capabilities using the Database search
  4. * module.
  5. */
  6. class SearchApiDbTest extends DrupalWebTestCase {
  7. protected $server_id;
  8. protected $index_id;
  9. protected function assertText($text, $message = '', $group = 'Other') {
  10. return parent::assertText($text, $message ? $message : $text, $group);
  11. }
  12. protected function drupalGet($path, array $options = array(), array $headers = array()) {
  13. $ret = parent::drupalGet($path, $options, $headers);
  14. $this->assertResponse(200, t('HTTP code 200 returned.'));
  15. return $ret;
  16. }
  17. protected function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
  18. $ret = parent::drupalPost($path, $edit, $submit, $options, $headers, $form_html_id, $extra_post);
  19. $this->assertResponse(200, t('HTTP code 200 returned.'));
  20. return $ret;
  21. }
  22. public static function getInfo() {
  23. return array(
  24. 'name' => 'Test "Database search" module',
  25. 'description' => 'Tests indexing and searching with the "Database search" module.',
  26. 'group' => 'Search API',
  27. );
  28. }
  29. public function setUp() {
  30. parent::setUp('entity', 'search_api', 'search_api_db', 'search_api_test');
  31. }
  32. public function testFramework() {
  33. $this->drupalLogin($this->drupalCreateUser(array('administer search_api')));
  34. $this->insertItems();
  35. $this->createServer();
  36. $this->createIndex();
  37. $this->searchNoResults();
  38. $this->indexItems();
  39. $this->searchSuccess1();
  40. $this->editServer();
  41. $this->searchSuccess2();
  42. $this->clearIndex();
  43. $this->searchNoResults();
  44. }
  45. protected function insertItems() {
  46. $this->drupalGet('search_api_test/insert');
  47. $count = db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField();
  48. $this->insertItem(array(
  49. 'id' => 1,
  50. 'title' => 'foo bar baz',
  51. 'body' => 'test test',
  52. 'type' => 'item',
  53. ));
  54. $this->insertItem(array(
  55. 'id' => 2,
  56. 'title' => 'foo test',
  57. 'body' => 'bar test',
  58. 'type' => 'item',
  59. ));
  60. $this->insertItem(array(
  61. 'id' => 3,
  62. 'title' => 'bar',
  63. 'body' => 'test foobar',
  64. 'type' => 'item',
  65. ));
  66. $this->insertItem(array(
  67. 'id' => 4,
  68. 'title' => 'foo baz',
  69. 'body' => 'test test test',
  70. 'type' => 'article',
  71. ));
  72. $this->insertItem(array(
  73. 'id' => 5,
  74. 'title' => 'bar baz',
  75. 'body' => 'foo',
  76. 'type' => 'article',
  77. ));
  78. $count = db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField() - $count;
  79. $this->assertEqual($count, 5, t('@count items inserted.', array('@count' => $count)));
  80. }
  81. protected function insertItem($values) {
  82. $this->drupalPost(NULL, $values, t('Save'));
  83. }
  84. protected function createServer() {
  85. $this->server_id = 'database_search_server';
  86. $values = array(
  87. 'name' => 'Database search server',
  88. 'machine_name' => $this->server_id,
  89. 'enabled' => 1,
  90. 'description' => 'A server used for testing.',
  91. 'class' => 'search_api_db_service',
  92. );
  93. $this->drupalPost('admin/config/search/search_api/add_server', $values, t('Create server'));
  94. $values2 = array(
  95. 'options[form][min_chars]' => 3,
  96. );
  97. $this->drupalPost(NULL, $values2, t('Create server'));
  98. $this->assertText(t('The server was successfully created.'));
  99. $found = strpos($this->getUrl(), 'admin/config/search/search_api/server/' . $this->server_id) !== FALSE;
  100. $this->assertTrue($found, t('Correct redirect.'));
  101. }
  102. protected function createIndex() {
  103. $this->index_id = $id = 'test_index';
  104. $values = array(
  105. 'name' => 'Test index',
  106. 'machine_name' => 'test_index',
  107. 'item_type' => 'search_api_test',
  108. 'enabled' => 1,
  109. 'description' => 'An index used for testing.',
  110. 'server' => $this->server_id,
  111. 'options[cron_limit]' => 5,
  112. );
  113. $this->drupalPost('admin/config/search/search_api/add_index', $values, t('Create index'));
  114. $this->assertText(t('The index was successfully created. Please set up its indexed fields now.'), t('The index was successfully created.'));
  115. $found = strpos($this->getUrl(), 'admin/config/search/search_api/index/' . $id) !== FALSE;
  116. $this->assertTrue($found, t('Correct redirect.'));
  117. $values = array(
  118. 'fields[id][type]' => 'integer',
  119. 'fields[id][boost]' => '1.0',
  120. 'fields[id][indexed]' => 1,
  121. 'fields[title][type]' => 'text',
  122. 'fields[title][boost]' => '5.0',
  123. 'fields[title][indexed]' => 1,
  124. 'fields[body][type]' => 'text',
  125. 'fields[body][boost]' => '1.0',
  126. 'fields[body][indexed]' => 1,
  127. 'fields[type][type]' => 'string',
  128. 'fields[type][boost]' => '1.0',
  129. 'fields[type][indexed]' => 1,
  130. );
  131. $this->drupalPost(NULL, $values, t('Save changes'));
  132. $this->assertText(t('The indexed fields were successfully changed. The index was cleared and will have to be re-indexed with the new settings.'), t('Field settings saved.'));
  133. $this->drupalPost(NULL, array(), t('Save configuration'));
  134. $this->assertText(t("The search index' workflow was successfully edited. All content was scheduled for re-indexing so the new settings can take effect."), t('Workflow successfully edited.'));
  135. $this->drupalGet("admin/config/search/search_api/index/$id/status");
  136. $this->assertText(t('The index is currently enabled.'), t('"Enabled" status displayed.'));
  137. $this->assertText(t('All items still need to be indexed (@total total).', array('@total' => 5)), t('!field displayed.', array('!field' => t('Correct index status'))));
  138. }
  139. protected function searchNoResults() {
  140. $this->drupalGet('search_api_test/query/' . $this->index_id . '/text');
  141. $this->assertText('result count = 0', t('No search results returned without indexing.'));
  142. $this->assertText('results = ()', t('No search results returned without indexing.'));
  143. $this->assertText('ignored = ()', t('No keys were ignored.'));
  144. $this->assertText('warnings = ()', t('No warnings were displayed.'));
  145. }
  146. protected function indexItems() {
  147. $this->drupalPost("admin/config/search/search_api/index/{$this->index_id}/status", array(), t('Index now'));
  148. $this->assertText(t('Successfully indexed @count items.', array('@count' => 5)));
  149. $this->assertNoText(t("Some items couldn't be indexed. Check the logs for details."), t("Index errors warning isn't displayed."));
  150. $this->assertNoText(t("Couldn't index items. Check the logs for details."), t("Index error isn't displayed."));
  151. $this->assertText(t('All items have been indexed (@total / @total).', array('@total' => 5)), t('!field displayed.', array('!field' => t('Correct index status'))));
  152. }
  153. protected function searchSuccess1() {
  154. $base ='search_api_test/query/' . $this->index_id;
  155. $this->drupalGet($base . '/test/1/2');
  156. $this->assertText('result count = 4', t('Search for »!keys« returned correct result.', array('!keys' => 'test')));
  157. $this->assertText('results = (2, 3)', t('Search for »!keys« returned correct result.', array('!keys' => 'test')));
  158. $this->assertText('ignored = ()', t('No keys were ignored.'));
  159. $this->assertText('warnings = ()', t('No warnings were displayed.'));
  160. $this->drupalGet($base . '/"test foo"/0/10//search_api_relevance,DESC');
  161. $this->assertText('result count = 3', t('Search for »!keys« returned correct result.', array('!keys' => '"test foo"')));
  162. $this->assertText('results = (2, 4, 1)', t('Search for »!keys« returned correct result.', array('!keys' => '"test foo"')));
  163. $this->assertText('ignored = ()', t('No keys were ignored.'));
  164. $this->assertText('warnings = ()', t('No warnings were displayed.'));
  165. $this->drupalGet($base . '/foo/0/10///type=item');
  166. $this->assertText('result count = 2', t('Search for »!keys« returned correct result.', array('!keys' => 'foo')));
  167. $this->assertText('results = (1, 2)', t('Search for »!keys« returned correct result.', array('!keys' => 'foo')));
  168. $this->assertText('ignored = ()', t('No keys were ignored.'));
  169. $this->assertText('warnings = ()', t('No warnings were displayed.'));
  170. $this->drupalGet($base . '/|COMPLEX|');
  171. $this->assertText('result count = 1', t('Complex search returned correct result.'));
  172. $this->assertText('results = (4)', t('Complex search returned correct result.'));
  173. $this->assertText('ignored = ()', t('No keys were ignored.'));
  174. $this->assertText('warnings = ()', t('No warnings were displayed.'));
  175. }
  176. protected function editServer() {
  177. $values = array(
  178. 'options[form][min_chars]' => 4,
  179. );
  180. $this->drupalPost("admin/config/search/search_api/server/{$this->server_id}/edit", $values, t('Save settings'));
  181. $this->clearIndex();
  182. $this->indexItems();
  183. }
  184. protected function searchSuccess2() {
  185. $base ='search_api_test/query/' . $this->index_id;
  186. $this->drupalGet($base . '/test/1/2');
  187. $this->assertText('result count = 4', t('Search for »!keys« returned correct result.', array('!keys' => 'test')));
  188. $this->assertText('results = (2, 3)', t('Search for »!keys« returned correct result.', array('!keys' => 'test')));
  189. $this->assertText('ignored = ()', t('No keys were ignored.'));
  190. $this->assertText('warnings = ()', t('No warnings were displayed.'));
  191. $this->drupalGet($base . '//0/10//search_api_relevance,DESC/body=test foobar');
  192. $this->assertText('result count = 1', t('Search with multi-term fulltext filter returned correct result.', array('!keys' => 'test foobar')));
  193. $this->assertText('results = (3)', t('Search with multi-term fulltext filter returned correct result.', array('!keys' => 'test foobar')));
  194. $this->assertText('ignored = ()', t('No keys were ignored.'));
  195. $this->assertText('warnings = ()', t('No warnings were displayed.'));
  196. $this->drupalGet($base . '/"test foo"/0/10//search_api_relevance,DESC');
  197. $this->assertText('result count = 4', t('Search for »!keys« returned correct result.', array('!keys' => '"test foo"')));
  198. $this->assertText('results = (2, 4, 1, 3)', t('Search for »!keys« returned correct result.', array('!keys' => '"test foo"')));
  199. $this->assertText('ignored = (foo)', t('Short key was ignored.'));
  200. $this->assertText('warnings = ()', t('No warnings were displayed.'));
  201. $this->drupalGet($base . '/foo/0/10///type=item');
  202. $this->assertText('result count = 3', t('Search for »!keys« returned correct result.', array('!keys' => 'foo')));
  203. $this->assertText('results = (1, 2, 3)', t('Search for »!keys« returned correct result.', array('!keys' => 'foo')));
  204. $this->assertText('ignored = (foo)', t('Short key was ignored.'));
  205. $this->assertText('warnings = ("' . t('No valid search keys were present in the query.') . '")', t('Warning displayed.'));
  206. $this->drupalGet($base . '/|COMPLEX|');
  207. $this->assertText('result count = 1', t('Complex search returned correct result.'));
  208. $this->assertText('results = (3)', t('Complex search returned correct result.'));
  209. $this->assertText('ignored = (baz, bar)', t('Correct keys were ignored.'));
  210. $this->assertText('warnings = ()', t('No warnings were displayed.'));
  211. }
  212. protected function clearIndex() {
  213. $this->drupalPost("admin/config/search/search_api/index/{$this->index_id}/status", array(), t('Clear index'));
  214. $this->assertText(t('The index was successfully cleared.'));
  215. $this->assertText(t('All items still need to be indexed (@total total).', array('@total' => 5)), t('!field displayed.', array('!field' => t('Correct index status'))));
  216. }
  217. }