search_api.test 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <?php
  2. /**
  3. * Class for testing Search API web functionality.
  4. */
  5. class SearchApiWebTest extends DrupalWebTestCase {
  6. protected $server_id;
  7. protected $index_id;
  8. protected function assertText($text, $message = '', $group = 'Other') {
  9. return parent::assertText($text, $message ? $message : $text, $group);
  10. }
  11. protected function drupalGet($path, array $options = array(), array $headers = array()) {
  12. $ret = parent::drupalGet($path, $options, $headers);
  13. $this->assertResponse(200, 'HTTP code 200 returned.');
  14. return $ret;
  15. }
  16. protected function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
  17. $ret = parent::drupalPost($path, $edit, $submit, $options, $headers, $form_html_id, $extra_post);
  18. $this->assertResponse(200, 'HTTP code 200 returned.');
  19. return $ret;
  20. }
  21. public static function getInfo() {
  22. return array(
  23. 'name' => 'Test search API framework',
  24. 'description' => 'Tests basic functions of the Search API, like creating, editing and deleting servers and indexes.',
  25. 'group' => 'Search API',
  26. );
  27. }
  28. public function setUp() {
  29. parent::setUp('entity', 'search_api', 'search_api_test');
  30. }
  31. public function testFramework() {
  32. $this->drupalLogin($this->drupalCreateUser(array('administer search_api')));
  33. // @todo Why is there no default index?
  34. //$this->deleteDefaultIndex();
  35. $this->insertItems();
  36. $this->checkOverview1();
  37. $this->createIndex();
  38. $this->insertItems(5);
  39. $this->createServer();
  40. $this->checkOverview2();
  41. $this->enableIndex();
  42. $this->searchNoResults();
  43. $this->indexItems();
  44. $this->searchSuccess();
  45. $this->editServer();
  46. $this->clearIndex();
  47. $this->searchNoResults();
  48. $this->deleteServer();
  49. $this->disableModules();
  50. }
  51. protected function deleteDefaultIndex() {
  52. $this->drupalPost('admin/config/search/search_api/index/default_node_index/delete', array(), t('Confirm'));
  53. }
  54. protected function insertItems($offset = 0) {
  55. $count = db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField();
  56. $this->insertItem(array(
  57. 'id' => $offset + 1,
  58. 'title' => 'Title 1',
  59. 'body' => 'Body text 1.',
  60. 'type' => 'Item',
  61. ));
  62. $this->insertItem(array(
  63. 'id' => $offset + 2,
  64. 'title' => 'Title 2',
  65. 'body' => 'Body text 2.',
  66. 'type' => 'Item',
  67. ));
  68. $this->insertItem(array(
  69. 'id' => $offset + 3,
  70. 'title' => 'Title 3',
  71. 'body' => 'Body text 3.',
  72. 'type' => 'Item',
  73. ));
  74. $this->insertItem(array(
  75. 'id' => $offset + 4,
  76. 'title' => 'Title 4',
  77. 'body' => 'Body text 4.',
  78. 'type' => 'Page',
  79. ));
  80. $this->insertItem(array(
  81. 'id' => $offset + 5,
  82. 'title' => 'Title 5',
  83. 'body' => 'Body text 5.',
  84. 'type' => 'Page',
  85. ));
  86. $count = db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField() - $count;
  87. $this->assertEqual($count, 5, '5 items successfully inserted.');
  88. }
  89. protected function insertItem($values) {
  90. $this->drupalPost('search_api_test/insert', $values, t('Save'));
  91. }
  92. protected function checkOverview1() {
  93. // This test fails for no apparent reason for drupal.org test bots.
  94. // Commenting them out for now.
  95. //$this->drupalGet('admin/config/search/search_api');
  96. //$this->assertText(t('There are no search servers or indexes defined yet.'), '"No servers" message is displayed.');
  97. }
  98. protected function createIndex() {
  99. $values = array(
  100. 'name' => '',
  101. 'item_type' => '',
  102. 'enabled' => 1,
  103. 'description' => 'An index used for testing.',
  104. 'server' => '',
  105. 'options[cron_limit]' => 5,
  106. );
  107. $this->drupalPost('admin/config/search/search_api/add_index', $values, t('Create index'));
  108. $this->assertText(t('!name field is required.', array('!name' => t('Index name'))));
  109. $this->assertText(t('!name field is required.', array('!name' => t('Item type'))));
  110. $this->index_id = $id = 'test_index';
  111. $values = array(
  112. 'name' => 'Search API test index',
  113. 'machine_name' => $id,
  114. 'item_type' => 'search_api_test',
  115. 'enabled' => 1,
  116. 'description' => 'An index used for testing.',
  117. 'server' => '',
  118. 'options[cron_limit]' => 1,
  119. );
  120. $this->drupalPost(NULL, $values, t('Create index'));
  121. $this->assertText(t('The index was successfully created. Please set up its indexed fields now.'), 'The index was successfully created.');
  122. $found = strpos($this->getUrl(), 'admin/config/search/search_api/index/' . $id) !== FALSE;
  123. $this->assertTrue($found, 'Correct redirect.');
  124. $index = search_api_index_load($id, TRUE);
  125. $this->assertEqual($index->name, $values['name'], 'Name correctly inserted.');
  126. $this->assertEqual($index->item_type, $values['item_type'], 'Index item type correctly inserted.');
  127. $this->assertFalse($index->enabled, 'Status correctly inserted.');
  128. $this->assertEqual($index->description, $values['description'], 'Description correctly inserted.');
  129. $this->assertNull($index->server, 'Index server correctly inserted.');
  130. $this->assertEqual($index->options['cron_limit'], $values['options[cron_limit]'], 'Cron batch size correctly inserted.');
  131. $values = array(
  132. 'additional[field]' => 'parent',
  133. );
  134. $this->drupalPost("admin/config/search/search_api/index/$id/fields", $values, t('Add fields'));
  135. $this->assertText(t('The available fields were successfully changed.'), 'Successfully added fields.');
  136. $this->assertText('Parent » ID', 'Added fields are displayed.');
  137. $values = array(
  138. 'fields[id][type]' => 'integer',
  139. 'fields[id][boost]' => '1.0',
  140. 'fields[id][indexed]' => 1,
  141. 'fields[title][type]' => 'text',
  142. 'fields[title][boost]' => '5.0',
  143. 'fields[title][indexed]' => 1,
  144. 'fields[body][type]' => 'text',
  145. 'fields[body][boost]' => '1.0',
  146. 'fields[body][indexed]' => 1,
  147. 'fields[type][type]' => 'string',
  148. 'fields[type][boost]' => '1.0',
  149. 'fields[type][indexed]' => 1,
  150. 'fields[parent:id][type]' => 'integer',
  151. 'fields[parent:id][boost]' => '1.0',
  152. 'fields[parent:id][indexed]' => 1,
  153. 'fields[parent:title][type]' => 'text',
  154. 'fields[parent:title][boost]' => '5.0',
  155. 'fields[parent:title][indexed]' => 1,
  156. 'fields[parent:body][type]' => 'text',
  157. 'fields[parent:body][boost]' => '1.0',
  158. 'fields[parent:body][indexed]' => 1,
  159. 'fields[parent:type][type]' => 'string',
  160. 'fields[parent:type][boost]' => '1.0',
  161. 'fields[parent:type][indexed]' => 1,
  162. );
  163. $this->drupalPost(NULL, $values, t('Save changes'));
  164. $this->assertText(t('The indexed fields were successfully changed. The index was cleared and will have to be re-indexed with the new settings.'), 'Field settings saved.');
  165. $values = array(
  166. 'callbacks[search_api_alter_add_url][status]' => 1,
  167. 'callbacks[search_api_alter_add_url][weight]' => 0,
  168. 'callbacks[search_api_alter_add_aggregation][status]' => 1,
  169. 'callbacks[search_api_alter_add_aggregation][weight]' => 10,
  170. 'processors[search_api_case_ignore][status]' => 1,
  171. 'processors[search_api_case_ignore][weight]' => 0,
  172. 'processors[search_api_case_ignore][settings][fields][title]' => 1,
  173. 'processors[search_api_case_ignore][settings][fields][body]' => 1,
  174. 'processors[search_api_case_ignore][settings][fields][parent:title]' => 1,
  175. 'processors[search_api_case_ignore][settings][fields][parent:body]' => 1,
  176. 'processors[search_api_tokenizer][status]' => 1,
  177. 'processors[search_api_tokenizer][weight]' => 20,
  178. 'processors[search_api_tokenizer][settings][spaces]' => '[^\p{L}\p{N}]',
  179. 'processors[search_api_tokenizer][settings][ignorable]' => '[-]',
  180. 'processors[search_api_tokenizer][settings][fields][title]' => 1,
  181. 'processors[search_api_tokenizer][settings][fields][body]' => 1,
  182. 'processors[search_api_tokenizer][settings][fields][parent:title]' => 1,
  183. 'processors[search_api_tokenizer][settings][fields][parent:body]' => 1,
  184. );
  185. $this->drupalPost(NULL, $values, t('Add new field'));
  186. $values = array(
  187. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][name]' => 'Test fulltext field',
  188. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][type]' => 'fulltext',
  189. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][fields][title]' => 1,
  190. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][fields][body]' => 1,
  191. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][fields][parent:title]' => 1,
  192. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][fields][parent:body]' => 1,
  193. );
  194. $this->drupalPost(NULL, $values, t('Save configuration'));
  195. $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.');
  196. $this->drupalGet("admin/config/search/search_api/index/$id");
  197. $this->assertTitle('Search API test index | Drupal', 'Correct title when viewing index.');
  198. $this->assertText('An index used for testing.', 'Description displayed.');
  199. $this->assertText('Search API test entity', 'Item type displayed.');
  200. $this->assertText(format_plural(1, '1 item per cron batch.', '@count items per cron batch.'), 'Cron batch size displayed.');
  201. $this->drupalGet("admin/config/search/search_api/index/$id/status");
  202. $this->assertText(t('The index is currently disabled.'), '"Disabled" status displayed.');
  203. }
  204. protected function createServer() {
  205. $values = array(
  206. 'name' => '',
  207. 'enabled' => 1,
  208. 'description' => 'A server used for testing.',
  209. 'class' => '',
  210. );
  211. $this->drupalPost('admin/config/search/search_api/add_server', $values, t('Create server'));
  212. $this->assertText(t('!name field is required.', array('!name' => t('Server name'))));
  213. $this->assertText(t('!name field is required.', array('!name' => t('Service class'))));
  214. $this->server_id = $id = 'test_server';
  215. $values = array(
  216. 'name' => 'Search API test server',
  217. 'machine_name' => $id,
  218. 'enabled' => 1,
  219. 'description' => 'A server used for testing.',
  220. 'class' => 'search_api_test_service',
  221. );
  222. $this->drupalPost(NULL, $values, t('Create server'));
  223. $values2 = array(
  224. 'options[form][test]' => 'search_api_test foo bar',
  225. );
  226. $this->drupalPost(NULL, $values2, t('Create server'));
  227. $this->assertText(t('The server was successfully created.'));
  228. $found = strpos($this->getUrl(), 'admin/config/search/search_api/server/' . $id) !== FALSE;
  229. $this->assertTrue($found, 'Correct redirect.');
  230. $server = search_api_server_load($id, TRUE);
  231. $this->assertEqual($server->name, $values['name'], 'Name correctly inserted.');
  232. $this->assertTrue($server->enabled, 'Status correctly inserted.');
  233. $this->assertEqual($server->description, $values['description'], 'Description correctly inserted.');
  234. $this->assertEqual($server->class, $values['class'], 'Service class correctly inserted.');
  235. $this->assertEqual($server->options['test'], $values2['options[form][test]'], 'Service options correctly inserted.');
  236. $this->assertTitle('Search API test server | Drupal', 'Correct title when viewing server.');
  237. $this->assertText('A server used for testing.', 'Description displayed.');
  238. $this->assertText('search_api_test_service', 'Service name displayed.');
  239. $this->assertText('search_api_test_service description', 'Service description displayed.');
  240. $this->assertText('search_api_test foo bar', 'Service options displayed.');
  241. }
  242. protected function checkOverview2() {
  243. $this->drupalGet('admin/config/search/search_api');
  244. $this->assertText('Search API test server', 'Server displayed.');
  245. $this->assertText('Search API test index', 'Index displayed.');
  246. $this->assertNoText(t('There are no search servers or indexes defined yet.'), '"No servers" message not displayed.');
  247. }
  248. protected function enableIndex() {
  249. $values = array(
  250. 'server' => $this->server_id,
  251. );
  252. $this->drupalPost("admin/config/search/search_api/index/{$this->index_id}/edit", $values, t('Save settings'));
  253. $this->assertText(t('The search index was successfully edited.'));
  254. $this->assertText('Search API test server', 'Server displayed.');
  255. $this->clickLink(t('enable'));
  256. $this->assertText(t('The index was successfully enabled.'));
  257. }
  258. protected function searchNoResults() {
  259. $this->drupalGet('search_api_test/query/' . $this->index_id);
  260. $this->assertText('result count = 0', 'No search results returned without indexing.');
  261. $this->assertText('results = ()', 'No search results returned without indexing.');
  262. }
  263. protected function indexItems() {
  264. $this->drupalGet("admin/config/search/search_api/index/{$this->index_id}/status");
  265. $this->assertText(t('The index is currently enabled.'), '"Enabled" status displayed.');
  266. $this->assertText(t('All items still need to be indexed (@total total).', array('@total' => 10)), 'Correct index status displayed.');
  267. $this->assertText(t('Index now'), '"Index now" button found.');
  268. $this->assertText(t('Clear index'), '"Clear index" button found.');
  269. $this->assertNoText(t('Re-index content'), '"Re-index" button not found.');
  270. // Here we test the indexing + the warning message when some items
  271. // can not be indexed.
  272. // The server refuses (for test purpose) to index items with IDs that are
  273. // multiples of 8 unless the "search_api_test_index_all" variable is set.
  274. $values = array(
  275. 'limit' => 8,
  276. );
  277. $this->drupalPost(NULL, $values, t('Index now'));
  278. $this->assertText(t('Successfully indexed @count items.', array('@count' => 7)));
  279. $this->assertText(t('1 item could not be indexed. Check the logs for details.'), 'Index errors warning is displayed.');
  280. $this->assertNoText(t("Couldn't index items. Check the logs for details."), "Index error isn't displayed.");
  281. $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.');
  282. $this->assertText(t('Re-indexing'), '"Re-index" button found.');
  283. // Here we're testing the error message when no item could be indexed.
  284. // The item with ID 8 is still not indexed.
  285. $values = array(
  286. 'limit' => 1,
  287. );
  288. $this->drupalPost(NULL, $values, t('Index now'));
  289. $this->assertNoPattern('/' . str_replace('144', '-?\d*', t('Successfully indexed @count items.', array('@count' => 144))) . '/', 'No items could be indexed.');
  290. $this->assertNoText(t('1 item could not be indexed. Check the logs for details.'), "Index errors warning isn't displayed.");
  291. $this->assertText(t("Couldn't index items. Check the logs for details."), 'Index error is displayed.');
  292. // Here we test the indexing of all the remaining items.
  293. variable_set('search_api_test_index_all', TRUE);
  294. $values = array(
  295. 'limit' => -1,
  296. );
  297. $this->drupalPost(NULL, $values, t('Index now'));
  298. $this->assertText(t('Successfully indexed @count items.', array('@count' => 3)));
  299. $this->assertNoText(t("Some items couldn't be indexed. Check the logs for details."), "Index errors warning isn't displayed.");
  300. $this->assertNoText(t("Couldn't index items. Check the logs for details."), "Index error isn't displayed.");
  301. $this->assertText(t('All items have been indexed (@indexed / @total).', array('@indexed' => 10, '@total' => 10)), 'Correct index status displayed.');
  302. $this->assertNoText(t('Index now'), '"Index now" button no longer displayed.');
  303. }
  304. protected function searchSuccess() {
  305. $this->drupalGet('search_api_test/query/' . $this->index_id);
  306. $this->assertText('result count = 10', 'Correct search result count returned after indexing.');
  307. $this->assertText('results = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)', 'Correct search results returned after indexing.');
  308. $this->drupalGet('search_api_test/query/' . $this->index_id . '/foo/2/4');
  309. $this->assertText('result count = 10', 'Correct search result count with ranged query.');
  310. $this->assertText('results = (3, 4, 5, 6)', 'Correct search results with ranged query.');
  311. }
  312. protected function editServer() {
  313. $values = array(
  314. 'name' => 'test-name-foo',
  315. 'description' => 'test-description-bar',
  316. 'options[form][test]' => 'test-test-baz',
  317. );
  318. $this->drupalPost("admin/config/search/search_api/server/{$this->server_id}/edit", $values, t('Save settings'));
  319. $this->assertText(t('The search server was successfully edited.'));
  320. $this->assertText('test-name-foo', 'Name changed.');
  321. $this->assertText('test-description-bar', 'Description changed.');
  322. $this->assertText('test-test-baz', 'Service options changed.');
  323. }
  324. protected function clearIndex() {
  325. $this->drupalPost("admin/config/search/search_api/index/{$this->index_id}/status", array(), t('Clear index'));
  326. $this->assertText(t('The index was successfully cleared.'));
  327. $this->assertText(t('All items still need to be indexed (@total total).', array('@total' => 10)), 'Correct index status displayed.');
  328. }
  329. protected function deleteServer() {
  330. $this->drupalPost("admin/config/search/search_api/server/{$this->server_id}/delete", array(), t('Confirm'));
  331. $this->assertNoText('test-name-foo', 'Server no longer listed.');
  332. $this->drupalGet("admin/config/search/search_api/index/{$this->index_id}/status");
  333. $this->assertText(t('The index is currently disabled.'), 'The index was disabled and removed from the server.');
  334. }
  335. protected function disableModules() {
  336. module_disable(array('search_api_test'), FALSE);
  337. $this->assertFalse(module_exists('search_api_test'), 'Test module was successfully disabled.');
  338. module_disable(array('search_api'), FALSE);
  339. $this->assertFalse(module_exists('search_api'), 'Search API module was successfully disabled.');
  340. drupal_uninstall_modules(array('search_api_test'), FALSE);
  341. $this->assertEqual(drupal_get_installed_schema_version('search_api_test', TRUE), SCHEMA_UNINSTALLED, 'Test module was successfully uninstalled.');
  342. $this->assertFalse(db_table_exists('search_api_test'), 'Test module table was successfully removed.');
  343. drupal_uninstall_modules(array('search_api'), FALSE);
  344. $this->assertEqual(drupal_get_installed_schema_version('search_api', TRUE), SCHEMA_UNINSTALLED, 'Search API module was successfully uninstalled.');
  345. $this->assertFalse(db_table_exists('search_api_server'), 'Search server table was successfully removed.');
  346. $this->assertFalse(db_table_exists('search_api_index'), 'Search index table was successfully removed.');
  347. $this->assertFalse(db_table_exists('search_api_item'), 'Index items table was successfully removed.');
  348. $this->assertNull(variable_get('search_api_tasks'), 'Tasks variable was correctly removed.');
  349. $this->assertNull(variable_get('search_api_index_worker_callback_runtime'), 'Worker runtime variable was correctly removed.');
  350. }
  351. }
  352. /**
  353. * Class with unit tests testing small fragments of the Search API.
  354. *
  355. * Due to severe limitations for "real" unit tests, this still has to be a
  356. * subclass of DrupalWebTestCase.
  357. */
  358. class SearchApiUnitTest extends DrupalWebTestCase {
  359. protected $index;
  360. protected function assertEqual($first, $second, $message = '', $group = 'Other') {
  361. if (is_array($first) && is_array($second)) {
  362. return $this->assertTrue($this->deepEquals($first, $second), $message, $group);
  363. }
  364. else {
  365. return parent::assertEqual($first, $second, $message, $group);
  366. }
  367. }
  368. protected function deepEquals($first, $second) {
  369. if (!is_array($first) || !is_array($second)) {
  370. return $first == $second;
  371. }
  372. $first = array_merge($first);
  373. $second = array_merge($second);
  374. foreach ($first as $key => $value) {
  375. if (!array_key_exists($key, $second) || !$this->deepEquals($value, $second[$key])) {
  376. return FALSE;
  377. }
  378. unset($second[$key]);
  379. }
  380. return empty($second);
  381. }
  382. public static function getInfo() {
  383. return array(
  384. 'name' => 'Test search API components',
  385. 'description' => 'Tests some independent components of the Search API, like the processors.',
  386. 'group' => 'Search API',
  387. );
  388. }
  389. public function setUp() {
  390. parent::setUp('entity', 'search_api');
  391. $this->index = entity_create('search_api_index', array(
  392. 'id' => 1,
  393. 'name' => 'test',
  394. 'enabled' => 1,
  395. 'item_type' => 'user',
  396. 'options' => array(
  397. 'fields' => array(
  398. 'name' => array(
  399. 'type' => 'text',
  400. ),
  401. 'mail' => array(
  402. 'type' => 'string',
  403. ),
  404. 'search_api_language' => array(
  405. 'type' => 'string',
  406. ),
  407. ),
  408. ),
  409. ));
  410. }
  411. public function testUnits() {
  412. $this->checkQueryParseKeys();
  413. $this->checkIgnoreCaseProcessor();
  414. $this->checkTokenizer();
  415. $this->checkHtmlFilter();
  416. }
  417. public function checkQueryParseKeys() {
  418. $options['parse mode'] = 'direct';
  419. $mode = &$options['parse mode'];
  420. $query = new SearchApiQuery($this->index, $options);
  421. $modes = $query->parseModes();
  422. $query->keys('foo');
  423. $this->assertEqual($query->getKeys(), 'foo', '"Direct query" parse mode, test 1.');
  424. $query->keys('foo bar');
  425. $this->assertEqual($query->getKeys(), 'foo bar', '"Direct query" parse mode, test 2.');
  426. $query->keys('(foo bar) OR "bar baz"');
  427. $this->assertEqual($query->getKeys(), '(foo bar) OR "bar baz"', '"Direct query" parse mode, test 3.');
  428. $mode = 'single';
  429. $query = new SearchApiQuery($this->index, $options);
  430. $query->keys('foo');
  431. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'foo'), '"Single term" parse mode, test 1.');
  432. $query->keys('foo bar');
  433. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'foo bar'), '"Single term" parse mode, test 2.');
  434. $query->keys('(foo bar) OR "bar baz"');
  435. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', '(foo bar) OR "bar baz"'), '"Single term" parse mode, test 3.');
  436. $mode = 'terms';
  437. $query = new SearchApiQuery($this->index, $options);
  438. $query->keys('foo');
  439. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'foo'), '"Multiple terms" parse mode, test 1.');
  440. $query->keys('foo bar');
  441. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'foo', 'bar'), '"Multiple terms" parse mode, test 2.');
  442. $query->keys('(foo bar) OR "bar baz"');
  443. $this->assertEqual($query->getKeys(), array('(foo', 'bar)', 'OR', 'bar baz', '#conjunction' => 'AND'), '"Multiple terms" parse mode, test 3.');
  444. // http://drupal.org/node/1468678
  445. $query->keys('"Münster"');
  446. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'Münster'), '"Multiple terms" parse mode, test 4.');
  447. }
  448. public function checkIgnoreCaseProcessor() {
  449. $types = search_api_field_types();
  450. $orig = 'Foo bar BaZ, ÄÖÜÀÁ<>»«.';
  451. $processed = drupal_strtolower($orig);
  452. $items = array(
  453. 1 => array(
  454. 'name' => array(
  455. 'type' => 'text',
  456. 'original_type' => 'text',
  457. 'value' => $orig,
  458. ),
  459. 'mail' => array(
  460. 'type' => 'string',
  461. 'original_type' => 'text',
  462. 'value' => $orig,
  463. ),
  464. 'search_api_language' => array(
  465. 'type' => 'string',
  466. 'original_type' => 'string',
  467. 'value' => LANGUAGE_NONE,
  468. ),
  469. ),
  470. );
  471. $keys1 = $keys2 = array(
  472. 'foo',
  473. 'bar baz',
  474. 'foobar1',
  475. '#conjunction' => 'AND',
  476. );
  477. $filters1 = array(
  478. array('name', 'foo', '='),
  479. array('mail', 'BAR', '='),
  480. );
  481. $filters2 = array(
  482. array('name', 'foo', '='),
  483. array('mail', 'bar', '='),
  484. );
  485. $processor = new SearchApiIgnoreCase($this->index, array('fields' => array('name' => 'name')));
  486. $tmp = $items;
  487. $processor->preprocessIndexItems($tmp);
  488. $this->assertEqual($tmp[1]['name']['value'], $processed, 'Name field was processed.');
  489. $this->assertEqual($tmp[1]['mail']['value'], $orig, "Mail field wasn't processed.");
  490. $query = new SearchApiQuery($this->index);
  491. $query->keys('Foo "baR BaZ" fOObAr1');
  492. $query->condition('name', 'FOO');
  493. $query->condition('mail', 'BAR');
  494. $processor->preprocessSearchQuery($query);
  495. $this->assertEqual($query->getKeys(), $keys1, 'Search keys were processed correctly.');
  496. $this->assertEqual($query->getFilter()->getFilters(), $filters1, 'Filters were processed correctly.');
  497. $processor = new SearchApiIgnoreCase($this->index, array('fields' => array('name' => 'name', 'mail' => 'mail')));
  498. $tmp = $items;
  499. $processor->preprocessIndexItems($tmp);
  500. $this->assertEqual($tmp[1]['name']['value'], $processed, 'Name field was processed.');
  501. $this->assertEqual($tmp[1]['mail']['value'], $processed, 'Mail field was processed.');
  502. $query = new SearchApiQuery($this->index);
  503. $query->keys('Foo "baR BaZ" fOObAr1');
  504. $query->condition('name', 'FOO');
  505. $query->condition('mail', 'BAR');
  506. $processor->preprocessSearchQuery($query);
  507. $this->assertEqual($query->getKeys(), $keys2, 'Search keys were processed correctly.');
  508. $this->assertEqual($query->getFilter()->getFilters(), $filters2, 'Filters were processed correctly.');
  509. }
  510. public function checkTokenizer() {
  511. $orig = 'Foo bar1 BaZ, La-la-la.';
  512. $processed1 = array(
  513. array(
  514. 'value' => 'Foo',
  515. 'score' => 1,
  516. ),
  517. array(
  518. 'value' => 'bar1',
  519. 'score' => 1,
  520. ),
  521. array(
  522. 'value' => 'BaZ',
  523. 'score' => 1,
  524. ),
  525. array(
  526. 'value' => 'Lalala',
  527. 'score' => 1,
  528. ),
  529. );
  530. $processed2 = array(
  531. array(
  532. 'value' => 'Foob',
  533. 'score' => 1,
  534. ),
  535. array(
  536. 'value' => 'r1B',
  537. 'score' => 1,
  538. ),
  539. array(
  540. 'value' => 'Z,L',
  541. 'score' => 1,
  542. ),
  543. array(
  544. 'value' => 'l',
  545. 'score' => 1,
  546. ),
  547. array(
  548. 'value' => 'l',
  549. 'score' => 1,
  550. ),
  551. array(
  552. 'value' => '.',
  553. 'score' => 1,
  554. ),
  555. );
  556. $items = array(
  557. 1 => array(
  558. 'name' => array(
  559. 'type' => 'text',
  560. 'original_type' => 'text',
  561. 'value' => $orig,
  562. ),
  563. 'search_api_language' => array(
  564. 'type' => 'string',
  565. 'original_type' => 'string',
  566. 'value' => LANGUAGE_NONE,
  567. ),
  568. ),
  569. );
  570. $processor = new SearchApiTokenizer($this->index, array('fields' => array('name' => 'name'), 'spaces' => '[^\p{L}\p{N}]', 'ignorable' => '[-]'));
  571. $tmp = $items;
  572. $processor->preprocessIndexItems($tmp);
  573. $this->assertEqual($tmp[1]['name']['value'], $processed1, 'Value was correctly tokenized with default settings.');
  574. $query = new SearchApiQuery($this->index, array('parse mode' => 'direct'));
  575. $query->keys("foo \"bar-baz\" \n\t foobar1");
  576. $processor->preprocessSearchQuery($query);
  577. $this->assertEqual($query->getKeys(), 'foo barbaz foobar1', 'Search keys were processed correctly.');
  578. $processor = new SearchApiTokenizer($this->index, array('fields' => array('name' => 'name'), 'spaces' => '[-a]', 'ignorable' => '\s'));
  579. $tmp = $items;
  580. $processor->preprocessIndexItems($tmp);
  581. $this->assertEqual($tmp[1]['name']['value'], $processed2, 'Value was correctly tokenized with custom settings.');
  582. $query = new SearchApiQuery($this->index, array('parse mode' => 'direct'));
  583. $query->keys("foo \"bar-baz\" \n\t foobar1");
  584. $processor->preprocessSearchQuery($query);
  585. $this->assertEqual($query->getKeys(), 'foo"b r b z"foob r1', 'Search keys were processed correctly.');
  586. }
  587. public function checkHtmlFilter() {
  588. $orig = <<<END
  589. This is <em lang="en" title =
  590. "something">a test</em>.
  591. How to write <strong>links to <em>other sites</em></strong>: &lt;a href="URL" title="MOUSEOVER TEXT"&gt;TEXT&lt;/a&gt;.
  592. &lt; signs can be <A HREF="http://example.com/topic/html-escapes" TITLE = 'HTML &quot;escapes&quot;'
  593. TARGET = '_blank'>escaped</A> with "&amp;lt;".
  594. <img src = "foo.png" alt = "someone's image" />
  595. END;
  596. $tags = <<<END
  597. em = 1.5
  598. strong = 2
  599. END;
  600. $processed1 = array(
  601. array('value' => 'This', 'score' => 1),
  602. array('value' => 'is', 'score' => 1),
  603. array('value' => 'something', 'score' => 1.5),
  604. array('value' => 'a', 'score' => 1.5),
  605. array('value' => 'test', 'score' => 1.5),
  606. array('value' => 'How', 'score' => 1),
  607. array('value' => 'to', 'score' => 1),
  608. array('value' => 'write', 'score' => 1),
  609. array('value' => 'links', 'score' => 2),
  610. array('value' => 'to', 'score' => 2),
  611. array('value' => 'other', 'score' => 3),
  612. array('value' => 'sites', 'score' => 3),
  613. array('value' => '<a', 'score' => 1),
  614. array('value' => 'href="URL"', 'score' => 1),
  615. array('value' => 'title="MOUSEOVER', 'score' => 1),
  616. array('value' => 'TEXT">TEXT</a>', 'score' => 1),
  617. array('value' => '<', 'score' => 1),
  618. array('value' => 'signs', 'score' => 1),
  619. array('value' => 'can', 'score' => 1),
  620. array('value' => 'be', 'score' => 1),
  621. array('value' => 'HTML', 'score' => 1),
  622. array('value' => '"escapes"', 'score' => 1),
  623. array('value' => 'escaped', 'score' => 1),
  624. array('value' => 'with', 'score' => 1),
  625. array('value' => '"&lt;"', 'score' => 1),
  626. array('value' => 'someone\'s', 'score' => 1),
  627. array('value' => 'image', 'score' => 1),
  628. );
  629. $items = array(
  630. 1 => array(
  631. 'name' => array(
  632. 'type' => 'text',
  633. 'original_type' => 'text',
  634. 'value' => $orig,
  635. ),
  636. 'search_api_language' => array(
  637. 'type' => 'string',
  638. 'original_type' => 'string',
  639. 'value' => LANGUAGE_NONE,
  640. ),
  641. ),
  642. );
  643. $tmp = $items;
  644. $processor = new SearchApiHtmlFilter($this->index, array('fields' => array('name' => 'name'), 'title' => TRUE, 'alt' => TRUE, 'tags' => $tags));
  645. $processor->preprocessIndexItems($tmp);
  646. $processor = new SearchApiTokenizer($this->index, array('fields' => array('name' => 'name'), 'spaces' => '[\s.:]', 'ignorable' => ''));
  647. $processor->preprocessIndexItems($tmp);
  648. $this->assertEqual($tmp[1]['name']['value'], $processed1, 'Text was correctly processed.');
  649. }
  650. }