search_api.test 30 KB

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