search_api.test 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. <?php
  2. /**
  3. * @file
  4. * Contains the SearchApiWebTest and the SearchApiUnitTest classes.
  5. */
  6. /**
  7. * Class for testing Search API functionality via the UI.
  8. */
  9. class SearchApiWebTest extends DrupalWebTestCase {
  10. /**
  11. * The machine name of the created test server.
  12. *
  13. * @var string
  14. */
  15. protected $server_id;
  16. /**
  17. * The machine name of the created test index.
  18. *
  19. * @var string
  20. */
  21. protected $index_id;
  22. /**
  23. * Overrides DrupalWebTestCase::assertText().
  24. *
  25. * Changes the default message to be just the text checked for.
  26. */
  27. protected function assertText($text, $message = '', $group = 'Other') {
  28. return parent::assertText($text, $message ? $message : $text, $group);
  29. }
  30. /**
  31. * Overrides DrupalWebTestCase::drupalGet().
  32. *
  33. * Additionally asserts that the HTTP request returned a 200 status code.
  34. */
  35. protected function drupalGet($path, array $options = array(), array $headers = array()) {
  36. $ret = parent::drupalGet($path, $options, $headers);
  37. $this->assertResponse(200, 'HTTP code 200 returned.');
  38. return $ret;
  39. }
  40. /**
  41. * Overrides DrupalWebTestCase::drupalPost().
  42. *
  43. * Additionally asserts that the HTTP request returned a 200 status code.
  44. */
  45. protected function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
  46. $ret = parent::drupalPost($path, $edit, $submit, $options, $headers, $form_html_id, $extra_post);
  47. $this->assertResponse(200, 'HTTP code 200 returned.');
  48. return $ret;
  49. }
  50. /**
  51. * Returns information about this test case.
  52. *
  53. * @return array
  54. * An array with information about this test case.
  55. */
  56. public static function getInfo() {
  57. return array(
  58. 'name' => 'Test search API framework',
  59. 'description' => 'Tests basic functions of the Search API, like creating, editing and deleting servers and indexes.',
  60. 'group' => 'Search API',
  61. );
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function setUp() {
  67. parent::setUp('entity', 'search_api', 'search_api_test');
  68. }
  69. /**
  70. * Tests correct admin UI, indexing and search behavior.
  71. *
  72. * We only use a single test method to avoid wasting ressources on setting up
  73. * the test environment multiple times. This will be the only method called
  74. * by the Simpletest framework (since the method name starts with "test"). It
  75. * in turn calls other methdos that set up the environment in a certain way
  76. * and then run tests on it.
  77. */
  78. public function testFramework() {
  79. $this->drupalLogin($this->drupalCreateUser(array('administer search_api')));
  80. $this->insertItems();
  81. $this->createIndex();
  82. $this->insertItems();
  83. $this->createServer();
  84. $this->checkOverview();
  85. $this->enableIndex();
  86. $this->searchNoResults();
  87. $this->indexItems();
  88. $this->searchSuccess();
  89. $this->checkIndexingOrder();
  90. $this->editServer();
  91. $this->clearIndex();
  92. $this->searchNoResults();
  93. $this->deleteServer();
  94. $this->disableModules();
  95. }
  96. /**
  97. * Returns the test server in use by this test case.
  98. *
  99. * @return SearchApiServer
  100. * The test server.
  101. */
  102. protected function server() {
  103. return search_api_server_load($this->server_id, TRUE);
  104. }
  105. /**
  106. * Returns the test index in use by this test case.
  107. *
  108. * @return SearchApiIndex
  109. * The test index.
  110. */
  111. protected function index() {
  112. return search_api_index_load($this->index_id, TRUE);
  113. }
  114. /**
  115. * Inserts some test items into the database, via the test module.
  116. *
  117. * @param int $number
  118. * The number of items to insert.
  119. *
  120. * @see insertItem()
  121. */
  122. protected function insertItems($number = 5) {
  123. $count = db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField();
  124. for ($i = 1; $i <= $number; ++$i) {
  125. $id = $count + $i;
  126. $this->insertItem(array(
  127. 'id' => $id,
  128. 'title' => "Title $id",
  129. 'body' => "Body text $id.",
  130. 'type' => 'Item',
  131. ));
  132. }
  133. $count = db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField() - $count;
  134. $this->assertEqual($count, $number, "$number items successfully inserted.");
  135. }
  136. /**
  137. * Helper function for inserting a single test item.
  138. *
  139. * @param array $values
  140. * The property values of the test item.
  141. *
  142. * @see search_api_test_insert_item()
  143. */
  144. protected function insertItem(array $values) {
  145. $this->drupalPost('search_api_test/insert', $values, t('Save'));
  146. }
  147. /**
  148. * Creates a test index via the UI and tests whether this works correctly.
  149. */
  150. protected function createIndex() {
  151. $values = array(
  152. 'name' => '',
  153. 'item_type' => '',
  154. 'enabled' => 1,
  155. 'description' => 'An index used for testing.',
  156. 'server' => '',
  157. 'options[cron_limit]' => 5,
  158. );
  159. $this->drupalPost('admin/config/search/search_api/add_index', $values, t('Create index'));
  160. $this->assertText(t('!name field is required.', array('!name' => t('Index name'))));
  161. $this->assertText(t('!name field is required.', array('!name' => t('Item type'))));
  162. $this->index_id = $id = 'test_index';
  163. $values = array(
  164. 'name' => 'Search API test index',
  165. 'machine_name' => $id,
  166. 'item_type' => 'search_api_test',
  167. 'enabled' => 1,
  168. 'description' => 'An index used for testing.',
  169. 'server' => '',
  170. 'options[cron_limit]' => 1,
  171. );
  172. $this->drupalPost(NULL, $values, t('Create index'));
  173. $this->assertText(t('The index was successfully created. Please set up its indexed fields now.'), 'The index was successfully created.');
  174. $found = strpos($this->getUrl(), 'admin/config/search/search_api/index/' . $id) !== FALSE;
  175. $this->assertTrue($found, 'Correct redirect.');
  176. $index = $this->index();
  177. $this->assertEqual($index->name, $values['name'], 'Name correctly inserted.');
  178. $this->assertEqual($index->item_type, $values['item_type'], 'Index item type correctly inserted.');
  179. $this->assertFalse($index->enabled, 'Status correctly inserted.');
  180. $this->assertEqual($index->description, $values['description'], 'Description correctly inserted.');
  181. $this->assertNull($index->server, 'Index server correctly inserted.');
  182. $this->assertEqual($index->options['cron_limit'], $values['options[cron_limit]'], 'Cron batch size correctly inserted.');
  183. $values = array(
  184. 'additional[field]' => 'parent',
  185. );
  186. $this->drupalPost("admin/config/search/search_api/index/$id/fields", $values, t('Add fields'));
  187. $this->assertText(t('The available fields were successfully changed.'), 'Successfully added fields.');
  188. $this->assertText('Parent » ID', 'Added fields are displayed.');
  189. $values = array(
  190. 'fields[id][type]' => 'integer',
  191. 'fields[id][boost]' => '1.0',
  192. 'fields[id][indexed]' => 1,
  193. 'fields[title][type]' => 'text',
  194. 'fields[title][boost]' => '5.0',
  195. 'fields[title][indexed]' => 1,
  196. 'fields[body][type]' => 'text',
  197. 'fields[body][boost]' => '1.0',
  198. 'fields[body][indexed]' => 1,
  199. 'fields[type][type]' => 'string',
  200. 'fields[type][boost]' => '1.0',
  201. 'fields[type][indexed]' => 1,
  202. 'fields[parent:id][type]' => 'integer',
  203. 'fields[parent:id][boost]' => '1.0',
  204. 'fields[parent:id][indexed]' => 1,
  205. 'fields[parent:title][type]' => 'text',
  206. 'fields[parent:title][boost]' => '5.0',
  207. 'fields[parent:title][indexed]' => 1,
  208. 'fields[parent:body][type]' => 'text',
  209. 'fields[parent:body][boost]' => '1.0',
  210. 'fields[parent:body][indexed]' => 1,
  211. 'fields[parent:type][type]' => 'string',
  212. 'fields[parent:type][boost]' => '1.0',
  213. 'fields[parent:type][indexed]' => 1,
  214. );
  215. $this->drupalPost(NULL, $values, t('Save changes'));
  216. $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.');
  217. $values = array(
  218. 'callbacks[search_api_alter_add_url][status]' => 1,
  219. 'callbacks[search_api_alter_add_url][weight]' => 0,
  220. 'callbacks[search_api_alter_add_aggregation][status]' => 1,
  221. 'callbacks[search_api_alter_add_aggregation][weight]' => 10,
  222. 'processors[search_api_case_ignore][status]' => 1,
  223. 'processors[search_api_case_ignore][weight]' => 0,
  224. 'processors[search_api_case_ignore][settings][fields][title]' => 1,
  225. 'processors[search_api_case_ignore][settings][fields][body]' => 1,
  226. 'processors[search_api_case_ignore][settings][fields][parent:title]' => 1,
  227. 'processors[search_api_case_ignore][settings][fields][parent:body]' => 1,
  228. 'processors[search_api_tokenizer][status]' => 1,
  229. 'processors[search_api_tokenizer][weight]' => 20,
  230. 'processors[search_api_tokenizer][settings][spaces]' => '[^\p{L}\p{N}]',
  231. 'processors[search_api_tokenizer][settings][ignorable]' => '[-]',
  232. 'processors[search_api_tokenizer][settings][fields][title]' => 1,
  233. 'processors[search_api_tokenizer][settings][fields][body]' => 1,
  234. 'processors[search_api_tokenizer][settings][fields][parent:title]' => 1,
  235. 'processors[search_api_tokenizer][settings][fields][parent:body]' => 1,
  236. );
  237. $this->drupalPost(NULL, $values, t('Add new field'));
  238. $values = array(
  239. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][name]' => 'Test fulltext field',
  240. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][type]' => 'fulltext',
  241. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][fields][title]' => 1,
  242. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][fields][body]' => 1,
  243. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][fields][parent:title]' => 1,
  244. 'callbacks[search_api_alter_add_aggregation][settings][fields][search_api_aggregation_1][fields][parent:body]' => 1,
  245. );
  246. $this->drupalPost(NULL, $values, t('Save configuration'));
  247. $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.');
  248. $this->drupalGet("admin/config/search/search_api/index/$id");
  249. $this->assertTitle('Search API test index | Drupal', 'Correct title when viewing index.');
  250. $this->assertText('An index used for testing.', 'Description displayed.');
  251. $this->assertText('Search API test entity', 'Item type displayed.');
  252. $this->assertText(t('disabled'), '"Disabled" status displayed.');
  253. }
  254. /**
  255. * Creates a test server via the UI and tests whether this works correctly.
  256. */
  257. protected function createServer() {
  258. $values = array(
  259. 'name' => '',
  260. 'enabled' => 1,
  261. 'description' => 'A server used for testing.',
  262. 'class' => '',
  263. );
  264. $this->drupalPost('admin/config/search/search_api/add_server', $values, t('Create server'));
  265. $this->assertText(t('!name field is required.', array('!name' => t('Server name'))));
  266. $this->assertText(t('!name field is required.', array('!name' => t('Service class'))));
  267. $this->server_id = $id = 'test_server';
  268. $values = array(
  269. 'name' => 'Search API test server',
  270. 'machine_name' => $id,
  271. 'enabled' => 1,
  272. 'description' => 'A server used for testing.',
  273. 'class' => 'search_api_test_service',
  274. );
  275. $this->drupalPost(NULL, $values, t('Create server'));
  276. $values2 = array(
  277. 'options[form][test]' => 'search_api_test foo bar',
  278. );
  279. $this->drupalPost(NULL, $values2, t('Create server'));
  280. $this->assertText(t('The server was successfully created.'));
  281. $found = strpos($this->getUrl(), 'admin/config/search/search_api/server/' . $id) !== FALSE;
  282. $this->assertTrue($found, 'Correct redirect.');
  283. $server = $this->server();
  284. $this->assertEqual($server->name, $values['name'], 'Name correctly inserted.');
  285. $this->assertTrue($server->enabled, 'Status correctly inserted.');
  286. $this->assertEqual($server->description, $values['description'], 'Description correctly inserted.');
  287. $this->assertEqual($server->class, $values['class'], 'Service class correctly inserted.');
  288. $this->assertEqual($server->options['test'], $values2['options[form][test]'], 'Service options correctly inserted.');
  289. $this->assertTitle('Search API test server | Drupal', 'Correct title when viewing server.');
  290. $this->assertText('A server used for testing.', 'Description displayed.');
  291. $this->assertText('search_api_test_service', 'Service name displayed.');
  292. $this->assertText('search_api_test foo bar', 'Service options displayed.');
  293. }
  294. /**
  295. * Checks whether the server and index are correctly listed in the overview.
  296. */
  297. protected function checkOverview() {
  298. $this->drupalGet('admin/config/search/search_api');
  299. $this->assertText('Search API test server', 'Server displayed.');
  300. $this->assertText('Search API test index', 'Index displayed.');
  301. $this->assertNoText(t('There are no search servers or indexes defined yet.'), '"No servers" message not displayed.');
  302. }
  303. /**
  304. * Moves the index onto the server and enables it.
  305. */
  306. protected function enableIndex() {
  307. $values = array(
  308. 'server' => $this->server_id,
  309. );
  310. $this->drupalPost("admin/config/search/search_api/index/{$this->index_id}/edit", $values, t('Save settings'));
  311. $this->assertText(t('The search index was successfully edited.'));
  312. $this->assertText('Search API test server', 'Server displayed.');
  313. $this->clickLink(t('enable'));
  314. $this->assertText(t('The index was successfully enabled.'));
  315. }
  316. /**
  317. * Asserts that a search on the index works but yields no results.
  318. *
  319. * This is the case since no items should have been indexed yet.
  320. */
  321. protected function searchNoResults() {
  322. $results = $this->doSearch();
  323. $this->assertEqual($results['result count'], 0, 'No search results returned without indexing.');
  324. $this->assertEqual(array_keys($results['results']), array(), 'No search results returned without indexing.');
  325. }
  326. /**
  327. * Executes a search on the test index.
  328. *
  329. * Helper method used for testing search results.
  330. *
  331. * @param int|null $offset
  332. * (optional) The offset for the returned results.
  333. * @param int|null $limit
  334. * (optional) The limit for the returned results.
  335. *
  336. * @return array
  337. * Search results as specified by SearchApiQueryInterface::execute().
  338. */
  339. protected function doSearch($offset = NULL, $limit = NULL) {
  340. // Since we change server and index settings via the UI (and, therefore, in
  341. // different page requests), the static cache in this page request
  342. // (executing the tests) will get stale. Therefore, we clear it before
  343. // executing the search.
  344. $this->index();
  345. $this->server();
  346. $query = search_api_query($this->index_id);
  347. if ($offset || $limit) {
  348. $query->range($offset, $limit);
  349. }
  350. return $query->execute();
  351. }
  352. /**
  353. * Tests indexing via the UI "Index now" functionality.
  354. *
  355. * Asserts that errors during indexing are handled properly and that the
  356. * status readings work.
  357. */
  358. protected function indexItems() {
  359. $this->checkIndexStatus();
  360. // Here we test the indexing + the warning message when some items
  361. // cannot be indexed.
  362. // The server refuses (for test purpose) to index the item that has the same
  363. // ID as the "search_api_test_indexing_break" variable (default: 8).
  364. // Therefore, if we try to index 8 items, only the first seven will be
  365. // successfully indexed and a warning should be displayed.
  366. $values = array(
  367. 'limit' => 8,
  368. );
  369. $this->drupalPost(NULL, $values, t('Index now'));
  370. $this->assertText(t('Successfully indexed @count items.', array('@count' => 7)));
  371. $this->assertText(t('1 item could not be indexed. Check the logs for details.'), 'Index errors warning is displayed.');
  372. $this->assertNoText(t("Couldn't index items. Check the logs for details."), "Index error isn't displayed.");
  373. $this->checkIndexStatus(7);
  374. // Here we're testing the error message when no item could be indexed.
  375. // The item with ID 8 is still not indexed, but it will be the first to be
  376. // indexed now. Therefore, if we try to index a single items, only item 8
  377. // will be passed to the server, which will reject it and no items will be
  378. // indexed. Since normally this signifies a more serious error than when
  379. // only some items couldn't be indexed, this is handled differently.
  380. $values = array(
  381. 'limit' => 1,
  382. );
  383. $this->drupalPost(NULL, $values, t('Index now'));
  384. $this->assertNoPattern('/' . str_replace('144', '-?\d*', t('Successfully indexed @count items.', array('@count' => 144))) . '/', 'No items could be indexed.');
  385. $this->assertNoText(t('1 item could not be indexed. Check the logs for details.'), "Index errors warning isn't displayed.");
  386. $this->assertText(t("Couldn't index items. Check the logs for details."), 'Index error is displayed.');
  387. // No we set the "search_api_test_indexing_break" variable to 0, so all
  388. // items will be indexed. The remaining items (8, 9, 10) should therefore
  389. // be successfully indexed and no warning should show.
  390. variable_set('search_api_test_indexing_break', 0);
  391. $values = array(
  392. 'limit' => -1,
  393. );
  394. $this->drupalPost(NULL, $values, t('Index now'));
  395. $this->assertText(t('Successfully indexed @count items.', array('@count' => 3)));
  396. $this->assertNoText(t("Some items couldn't be indexed. Check the logs for details."), "Index errors warning isn't displayed.");
  397. $this->assertNoText(t("Couldn't index items. Check the logs for details."), "Index error isn't displayed.");
  398. $this->checkIndexStatus(10);
  399. // Reset the static cache for the server.
  400. $this->server();
  401. }
  402. /**
  403. * Checks whether the index's "Status" tab shows the correct values.
  404. *
  405. * Helper method used by indexItems() and others.
  406. *
  407. * The internal browser will point to the index's "Status" tab after this
  408. * method is called.
  409. *
  410. * @param int $indexed
  411. * (optional) The number of items that should be indexed at the moment.
  412. * Defaults to 0.
  413. * @param int $total
  414. * (optional) The (correct) total number of items. Defaults to 10.
  415. * @param bool $check_buttons
  416. * (optional) Whether to check for the correct presence/absence of buttons.
  417. * Defaults to TRUE.
  418. * @param int|null $on_server
  419. * (optional) The number of items actually on the server. Defaults to
  420. * $indexed.
  421. */
  422. protected function checkIndexStatus($indexed = 0, $total = 10, $check_buttons = TRUE, $on_server = NULL) {
  423. $url = "admin/config/search/search_api/index/{$this->index_id}";
  424. if (strpos($this->url, $url) === FALSE) {
  425. $this->drupalGet($url);
  426. }
  427. $index_status = t('@indexed/@total indexed', array('@indexed' => $indexed, '@total' => $total));
  428. $this->assertText($index_status, 'Correct index status displayed.');
  429. if (!isset($on_server)) {
  430. $on_server = $indexed;
  431. }
  432. $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.');
  433. $this->assertText(t('Server index status'), 'Server index status displayed.');
  434. $this->assertText($info, 'Correct server index status displayed.');
  435. if (!$check_buttons) {
  436. return;
  437. }
  438. $this->assertText(t('enabled'), '"Enabled" status displayed.');
  439. if ($indexed == $total) {
  440. $this->assertRaw('disabled="disabled"', '"Index now" form disabled.');
  441. }
  442. else {
  443. $this->assertNoRaw('disabled="disabled"', '"Index now" form enabled.');
  444. }
  445. }
  446. /**
  447. * Tests whether searches yield the right results after indexing.
  448. *
  449. * The test server only implements range functionality, no kind of fulltext
  450. * search capabilities, so we can only test for that.
  451. */
  452. protected function searchSuccess() {
  453. $results = $this->doSearch();
  454. $this->assertEqual($results['result count'], 10, 'Correct search result count returned after indexing.');
  455. $this->assertEqual(array_keys($results['results']), array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 'Correct search results returned after indexing.');
  456. $results = $this->doSearch(2, 4);
  457. $this->assertEqual($results['result count'], 10, 'Correct search result count with ranged query.');
  458. $this->assertEqual(array_keys($results['results']), array(3, 4, 5, 6), 'Correct search results with ranged query.');
  459. }
  460. /**
  461. * Tests whether items are indexed in the right order.
  462. *
  463. * The indexing order should always be that new items are indexed before
  464. * changed ones, and only then the changed items in the order of their change.
  465. *
  466. * This method also assures that this behavior is even observed when indexing
  467. * temporarily fails.
  468. *
  469. * @see https://drupal.org/node/2115127
  470. */
  471. protected function checkIndexingOrder() {
  472. // Set cron batch size to 1 so not all items will get indexed right away.
  473. // This also ensures that later, when indexing of a single item will be
  474. // rejected by using the "search_api_test_indexing_break" variable, this
  475. // will have the effect of rejecting "all" items of a batch (since that
  476. // batch only consists of a single item).
  477. $values = array(
  478. 'options[cron_limit]' => 1,
  479. );
  480. $this->drupalPost("admin/config/search/search_api/index/{$this->index_id}/edit", $values, t('Save settings'));
  481. $this->assertText(t('The search index was successfully edited.'));
  482. // Manually clear the server's item storage – that way, the items will still
  483. // count as indexed for the Search API, but won't be returned in searches.
  484. // We do this so we have finer-grained control over the order in which items
  485. // are indexed.
  486. $this->server()->deleteItems();
  487. $results = $this->doSearch();
  488. $this->assertEqual($results['result count'], 0, 'Indexed items were successfully deleted from the server.');
  489. $this->assertEqual(array_keys($results['results']), array(), 'Indexed items were successfully deleted from the server.');
  490. // Now insert some new items, and mark others as changed. Make sure that
  491. // each action has a unique timestamp, so the order will be correct.
  492. $this->drupalGet('search_api_test/touch/8');
  493. $this->insertItems(1);// item 11
  494. sleep(1);
  495. $this->drupalGet('search_api_test/touch/2');
  496. $this->insertItems(1);// item 12
  497. sleep(1);
  498. $this->drupalGet('search_api_test/touch/5');
  499. $this->insertItems(1);// item 13
  500. sleep(1);
  501. $this->drupalGet('search_api_test/touch/8');
  502. $this->insertItems(1); // item 14
  503. // Check whether the status display is right.
  504. $this->checkIndexStatus(7, 14, FALSE, 0);
  505. // Indexing order should now be: 11, 12, 13, 14, 8, 2, 4. Let's try it out!
  506. // First manually index one item, and see if it's 11.
  507. $values = array(
  508. 'limit' => 1,
  509. );
  510. $this->drupalPost(NULL, $values, t('Index now'));
  511. $this->assertText(t('Successfully indexed @count item.', array('@count' => 1)));
  512. $this->assertNoText(t("Some items couldn't be indexed. Check the logs for details."), "Index errors warning isn't displayed.");
  513. $this->assertNoText(t("Couldn't index items. Check the logs for details."), "Index error isn't displayed.");
  514. $this->checkIndexStatus(8, 14, FALSE, 1);
  515. $results = $this->doSearch();
  516. $this->assertEqual($results['result count'], 1, 'Indexing order test 1: correct result count.');
  517. $this->assertEqual(array_keys($results['results']), array(11), 'Indexing order test 1: correct results.');
  518. // Now index with a cron run, but stop at item 8.
  519. variable_set('search_api_test_indexing_break', 8);
  520. $this->cronRun();
  521. // Now just the four new items should have been indexed.
  522. $results = $this->doSearch();
  523. $this->assertEqual($results['result count'], 4, 'Indexing order test 2: correct result count.');
  524. $this->assertEqual(array_keys($results['results']), array(11, 12, 13, 14), 'Indexing order test 2: correct results.');
  525. // This time stop at item 5 (should be the last one).
  526. variable_set('search_api_test_indexing_break', 5);
  527. $this->cronRun();
  528. // Now all new and changed items should have been indexed, except item 5.
  529. $results = $this->doSearch();
  530. $this->assertEqual($results['result count'], 6, 'Indexing order test 3: correct result count.');
  531. $this->assertEqual(array_keys($results['results']), array(2, 8, 11, 12, 13, 14), 'Indexing order test 3: correct results.');
  532. // Index the remaining item.
  533. variable_set('search_api_test_indexing_break', 0);
  534. $this->cronRun();
  535. // Now all new and changed items should have been indexed.
  536. $results = $this->doSearch();
  537. $this->assertEqual($results['result count'], 7, 'Indexing order test 4: correct result count.');
  538. $this->assertEqual(array_keys($results['results']), array(2, 5, 8, 11, 12, 13, 14), 'Indexing order test 4: correct results.');
  539. }
  540. /**
  541. * Tests whether the server tasks system works correctly.
  542. *
  543. * Uses the "search_api_test_error_state" variable to trigger exceptions in
  544. * the test service class and asserts that the Search API reacts correctly and
  545. * re-attempts the operation on the next cron run.
  546. */
  547. protected function checkServerTasks() {
  548. // Make sure none of the previous operations added any tasks.
  549. $task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')->fetchField();
  550. $this->assertEqual($task_count, 0, 'No server tasks were previously saved.');
  551. // Set error state for test service, so all operations will fail.
  552. variable_set('search_api_test_error_state', TRUE);
  553. // Delete some items.
  554. $this->drupalGet('search_api_test/delete/8');
  555. $this->drupalGet('search_api_test/delete/12');
  556. // Assert that the indexed items haven't changed yet.
  557. $results = $this->doSearch();
  558. $this->assertEqual(array_keys($results['results']), array(2, 5, 8, 11, 12, 13, 14), 'During error state, no indexed items were deleted.');
  559. // Check that tasks were correctly inserted.
  560. $task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')->fetchField();
  561. $this->assertEqual($task_count, 2, 'Server tasks for deleted items were saved.');
  562. // Now reset the error state variable and run cron to delete the items.
  563. variable_set('search_api_test_error_state', FALSE);
  564. $this->cronRun();
  565. // Assert that the indexed items were indeed deleted from the server.
  566. $results = $this->doSearch();
  567. $this->assertEqual(array_keys($results['results']), array(2, 5, 11, 13, 14), 'Pending "delete item" server tasks were correctly executed during the cron run.');
  568. // Check that the tasks were correctly deleted.
  569. $task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')->fetchField();
  570. $this->assertEqual($task_count, 0, 'Server tasks were correctly deleted after being executed.');
  571. // Now we first delete more items, then disable the server (thereby removing
  572. // the index from it) – all while in error state.
  573. variable_set('search_api_test_error_state', TRUE);
  574. $this->drupalGet('search_api_test/delete/14');
  575. $this->drupalGet('search_api_test/delete/2');
  576. $settings['enabled'] = 0;
  577. $this->drupalPost("admin/config/search/search_api/server/{$this->server_id}/edit", $settings, t('Save settings'));
  578. // Check whether the index was correctly removed from the server.
  579. $this->assertEqual($this->index()->server(), NULL, 'The index was successfully set to have no server.');
  580. $exception = FALSE;
  581. try {
  582. $this->doSearch();
  583. }
  584. catch (SearchApiException $e) {
  585. $exception = TRUE;
  586. }
  587. $this->assertTrue($exception, 'Searching on the index failed with an exception.');
  588. // Check that only one task – to remove the index from the server – is now
  589. // present in the tasks table.
  590. $task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')->fetchField();
  591. $this->assertEqual($task_count, 1, 'Only the "remove index" task is present in the server tasks.');
  592. // Reset the error state variable, re-enable the server.
  593. variable_set('search_api_test_error_state', FALSE);
  594. $settings['enabled'] = 1;
  595. $this->drupalPost("admin/config/search/search_api/server/{$this->server_id}/edit", $settings, t('Save settings'));
  596. // Check whether the index was really removed from the server now.
  597. $server = $this->server();
  598. $this->assertTrue(empty($server->options['indexes'][$this->index_id]), 'The index was removed from the server after cron ran.');
  599. $task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')->fetchField();
  600. $this->assertEqual($task_count, 0, 'Server tasks were correctly deleted after being executed.');
  601. // Put the index back on the server and index some items for the next tests.
  602. $settings = array('server' => $this->server_id);
  603. $this->drupalPost("admin/config/search/search_api/index/{$this->index_id}/edit", $settings, t('Save settings'));
  604. $this->cronRun();
  605. }
  606. /**
  607. * Tests whether editing the server works correctly.
  608. */
  609. protected function editServer() {
  610. $values = array(
  611. 'name' => 'test-name-foo',
  612. 'description' => 'test-description-bar',
  613. 'options[form][test]' => 'test-test-baz',
  614. );
  615. $this->drupalPost("admin/config/search/search_api/server/{$this->server_id}/edit", $values, t('Save settings'));
  616. $this->assertText(t('The search server was successfully edited.'));
  617. $this->assertText('test-name-foo', 'Name changed.');
  618. $this->assertText('test-description-bar', 'Description changed.');
  619. $this->assertText('test-test-baz', 'Service options changed.');
  620. }
  621. /**
  622. * Tests whether clearing the index works correctly.
  623. */
  624. protected function clearIndex() {
  625. $this->drupalPost("admin/config/search/search_api/index/{$this->index_id}", array(), t('Clear all indexed data'));
  626. $this->drupalPost(NULL, array(), t('Confirm'));
  627. $this->assertText(t('The index was successfully cleared.'));
  628. $this->assertText(t('@indexed/@total indexed', array('@indexed' => 0, '@total' => 14)), 'Correct index status displayed.');
  629. }
  630. /**
  631. * Tests whether deleting the server works correctly.
  632. *
  633. * The index still lying on the server should be disabled and removed from it.
  634. * Also, any tasks with that server's ID should be deleted.
  635. */
  636. protected function deleteServer() {
  637. // Insert some dummy tasks to check for.
  638. $server = $this->server();
  639. search_api_server_tasks_add($server, 'foo');
  640. search_api_server_tasks_add($server, 'bar', $this->index());
  641. $task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')->fetchField();
  642. $this->assertEqual($task_count, 2, 'Dummy tasks were added.');
  643. // Delete the server.
  644. $this->drupalPost("admin/config/search/search_api/server/{$this->server_id}/delete", array(), t('Confirm'));
  645. $this->assertNoText('test-name-foo', 'Server no longer listed.');
  646. $this->drupalGet("admin/config/search/search_api/index/{$this->index_id}");
  647. $this->assertNoText(t('Server'), 'The index was removed from the server.');
  648. $this->assertText(t('disabled'), 'The index was disabled.');
  649. // Check whether the tasks were correctly deleted.
  650. $task_count = db_query('SELECT COUNT(id) FROM {search_api_task}')->fetchField();
  651. $this->assertEqual($task_count, 0, 'Remaining server tasks were correctly deleted.');
  652. }
  653. /**
  654. * Tests whether disabling and uninstalling the modules works correctly.
  655. *
  656. * This will disable and uninstall both the test module and the Search API. It
  657. * asserts that this works correctly (since the server has been deleted in
  658. * deleteServer()) and that all associated tables and variables are removed.
  659. */
  660. protected function disableModules() {
  661. module_disable(array('search_api_test'), FALSE);
  662. $this->assertFalse(module_exists('search_api_test'), 'Test module was successfully disabled.');
  663. module_disable(array('search_api'), FALSE);
  664. $this->assertFalse(module_exists('search_api'), 'Search API module was successfully disabled.');
  665. drupal_uninstall_modules(array('search_api_test'), FALSE);
  666. $this->assertEqual(drupal_get_installed_schema_version('search_api_test', TRUE), SCHEMA_UNINSTALLED, 'Test module was successfully uninstalled.');
  667. $this->assertFalse(db_table_exists('search_api_test'), 'Test module table was successfully removed.');
  668. drupal_uninstall_modules(array('search_api'), FALSE);
  669. $this->assertEqual(drupal_get_installed_schema_version('search_api', TRUE), SCHEMA_UNINSTALLED, 'Search API module was successfully uninstalled.');
  670. $this->assertFalse(db_table_exists('search_api_server'), 'Search server table was successfully removed.');
  671. $this->assertFalse(db_table_exists('search_api_index'), 'Search index table was successfully removed.');
  672. $this->assertFalse(db_table_exists('search_api_item'), 'Index items table was successfully removed.');
  673. $this->assertFalse(db_table_exists('search_api_task'), 'Server tasks table was successfully removed.');
  674. $this->assertNull(variable_get('search_api_index_worker_callback_runtime'), 'Worker runtime variable was correctly removed.');
  675. }
  676. }
  677. /**
  678. * Class with unit tests testing small fragments of the Search API.
  679. *
  680. * Due to severe limitations for "real" unit tests, this still has to be a
  681. * subclass of DrupalWebTestCase.
  682. */
  683. class SearchApiUnitTest extends DrupalWebTestCase {
  684. /**
  685. * The index used by these tests.
  686. *
  687. * @var SearchApIindex
  688. */
  689. protected $index;
  690. /**
  691. * Overrides DrupalTestCase::assertEqual().
  692. *
  693. * For arrays, checks whether all array keys are mapped the same in both
  694. * arrays recursively, while ignoring their order.
  695. */
  696. protected function assertEqual($first, $second, $message = '', $group = 'Other') {
  697. if (is_array($first) && is_array($second)) {
  698. return $this->assertTrue($this->deepEquals($first, $second), $message, $group);
  699. }
  700. else {
  701. return parent::assertEqual($first, $second, $message, $group);
  702. }
  703. }
  704. /**
  705. * Tests whether two values are equal.
  706. *
  707. * For arrays, this is done by comparing the key/value pairs recursively
  708. * instead of checking for simple equality.
  709. *
  710. * @param mixed $first
  711. * The first value.
  712. * @param mixed $second
  713. * The second value.
  714. *
  715. * @return bool
  716. * TRUE if the two values are equal, FALSE otherwise.
  717. */
  718. protected function deepEquals($first, $second) {
  719. if (!is_array($first) || !is_array($second)) {
  720. return $first == $second;
  721. }
  722. $first = array_merge($first);
  723. $second = array_merge($second);
  724. foreach ($first as $key => $value) {
  725. if (!array_key_exists($key, $second) || !$this->deepEquals($value, $second[$key])) {
  726. return FALSE;
  727. }
  728. unset($second[$key]);
  729. }
  730. return empty($second);
  731. }
  732. /**
  733. * Returns information about this test case.
  734. *
  735. * @return array
  736. * An array with information about this test case.
  737. */
  738. public static function getInfo() {
  739. return array(
  740. 'name' => 'Test search API components',
  741. 'description' => 'Tests some independent components of the Search API, like the processors.',
  742. 'group' => 'Search API',
  743. );
  744. }
  745. /**
  746. * {@inheritdoc}
  747. */
  748. public function setUp() {
  749. parent::setUp('entity', 'search_api');
  750. $this->index = entity_create('search_api_index', array(
  751. 'id' => 1,
  752. 'name' => 'test',
  753. 'machine_name' => 'test',
  754. 'enabled' => 1,
  755. 'item_type' => 'user',
  756. 'options' => array(
  757. 'fields' => array(
  758. 'name' => array(
  759. 'type' => 'text',
  760. ),
  761. 'mail' => array(
  762. 'type' => 'string',
  763. ),
  764. 'search_api_language' => array(
  765. 'type' => 'string',
  766. ),
  767. ),
  768. ),
  769. ));
  770. }
  771. /**
  772. * Tests the functionality of several components of the module.
  773. *
  774. * This is the single test method called by the Simpletest framework. It in
  775. * turn calls other helper methods to test specific functionality.
  776. */
  777. public function testUnits() {
  778. $this->checkQueryParseKeys();
  779. $this->checkIgnoreCaseProcessor();
  780. $this->checkTokenizer();
  781. $this->checkHtmlFilter();
  782. $this->checkEntityDatasource();
  783. }
  784. /**
  785. * Checks whether the keys are parsed correctly by the query class.
  786. */
  787. protected function checkQueryParseKeys() {
  788. $options['parse mode'] = 'direct';
  789. $mode = &$options['parse mode'];
  790. $query = new SearchApiQuery($this->index, $options);
  791. $query->keys('foo');
  792. $this->assertEqual($query->getKeys(), 'foo', '"Direct query" parse mode, test 1.');
  793. $query->keys('foo bar');
  794. $this->assertEqual($query->getKeys(), 'foo bar', '"Direct query" parse mode, test 2.');
  795. $query->keys('(foo bar) OR "bar baz"');
  796. $this->assertEqual($query->getKeys(), '(foo bar) OR "bar baz"', '"Direct query" parse mode, test 3.');
  797. $mode = 'single';
  798. $query = new SearchApiQuery($this->index, $options);
  799. $query->keys('foo');
  800. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'foo'), '"Single term" parse mode, test 1.');
  801. $query->keys('foo bar');
  802. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'foo bar'), '"Single term" parse mode, test 2.');
  803. $query->keys('(foo bar) OR "bar baz"');
  804. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', '(foo bar) OR "bar baz"'), '"Single term" parse mode, test 3.');
  805. $mode = 'terms';
  806. $query = new SearchApiQuery($this->index, $options);
  807. $query->keys('foo');
  808. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'foo'), '"Multiple terms" parse mode, test 1.');
  809. $query->keys('foo bar');
  810. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'foo', 'bar'), '"Multiple terms" parse mode, test 2.');
  811. $query->keys('(foo bar) OR "bar baz"');
  812. $this->assertEqual($query->getKeys(), array('(foo', 'bar)', 'OR', 'bar baz', '#conjunction' => 'AND'), '"Multiple terms" parse mode, test 3.');
  813. // http://drupal.org/node/1468678
  814. $query->keys('"Münster"');
  815. $this->assertEqual($query->getKeys(), array('#conjunction' => 'AND', 'Münster'), '"Multiple terms" parse mode, test 4.');
  816. }
  817. /**
  818. * Tests the functionality of the "Ignore case" processor.
  819. */
  820. protected function checkIgnoreCaseProcessor() {
  821. $orig = 'Foo bar BaZ, ÄÖÜÀÁ<>»«.';
  822. $processed = drupal_strtolower($orig);
  823. $items = array(
  824. 1 => array(
  825. 'name' => array(
  826. 'type' => 'text',
  827. 'original_type' => 'text',
  828. 'value' => $orig,
  829. ),
  830. 'mail' => array(
  831. 'type' => 'string',
  832. 'original_type' => 'text',
  833. 'value' => $orig,
  834. ),
  835. 'search_api_language' => array(
  836. 'type' => 'string',
  837. 'original_type' => 'string',
  838. 'value' => LANGUAGE_NONE,
  839. ),
  840. ),
  841. );
  842. $keys1 = $keys2 = array(
  843. 'foo',
  844. 'bar baz',
  845. 'foobar1',
  846. '#conjunction' => 'AND',
  847. );
  848. $filters1 = array(
  849. array('name', 'foo', '='),
  850. array('mail', 'BAR', '='),
  851. );
  852. $filters2 = array(
  853. array('name', 'foo', '='),
  854. array('mail', 'bar', '='),
  855. );
  856. $processor = new SearchApiIgnoreCase($this->index, array('fields' => array('name' => 'name')));
  857. $tmp = $items;
  858. $processor->preprocessIndexItems($tmp);
  859. $this->assertEqual($tmp[1]['name']['value'], $processed, 'Name field was processed.');
  860. $this->assertEqual($tmp[1]['mail']['value'], $orig, "Mail field wasn't processed.");
  861. $query = new SearchApiQuery($this->index);
  862. $query->keys('Foo "baR BaZ" fOObAr1');
  863. $query->condition('name', 'FOO');
  864. $query->condition('mail', 'BAR');
  865. $processor->preprocessSearchQuery($query);
  866. $this->assertEqual($query->getKeys(), $keys1, 'Search keys were processed correctly.');
  867. $this->assertEqual($query->getFilter()->getFilters(), $filters1, 'Filters were processed correctly.');
  868. $processor = new SearchApiIgnoreCase($this->index, array('fields' => array('name' => 'name', 'mail' => 'mail')));
  869. $tmp = $items;
  870. $processor->preprocessIndexItems($tmp);
  871. $this->assertEqual($tmp[1]['name']['value'], $processed, 'Name field was processed.');
  872. $this->assertEqual($tmp[1]['mail']['value'], $processed, 'Mail field was processed.');
  873. $query = new SearchApiQuery($this->index);
  874. $query->keys('Foo "baR BaZ" fOObAr1');
  875. $query->condition('name', 'FOO');
  876. $query->condition('mail', 'BAR');
  877. $processor->preprocessSearchQuery($query);
  878. $this->assertEqual($query->getKeys(), $keys2, 'Search keys were processed correctly.');
  879. $this->assertEqual($query->getFilter()->getFilters(), $filters2, 'Filters were processed correctly.');
  880. }
  881. /**
  882. * Tests the functionality of the "Tokenizer" processor.
  883. */
  884. protected function checkTokenizer() {
  885. $orig = 'Foo bar1 BaZ, La-la-la.';
  886. $processed1 = array(
  887. array(
  888. 'value' => 'Foo',
  889. 'score' => 1,
  890. ),
  891. array(
  892. 'value' => 'bar1',
  893. 'score' => 1,
  894. ),
  895. array(
  896. 'value' => 'BaZ',
  897. 'score' => 1,
  898. ),
  899. array(
  900. 'value' => 'Lalala',
  901. 'score' => 1,
  902. ),
  903. );
  904. $processed2 = array(
  905. array(
  906. 'value' => 'Foob',
  907. 'score' => 1,
  908. ),
  909. array(
  910. 'value' => 'r1B',
  911. 'score' => 1,
  912. ),
  913. array(
  914. 'value' => 'Z,L',
  915. 'score' => 1,
  916. ),
  917. array(
  918. 'value' => 'l',
  919. 'score' => 1,
  920. ),
  921. array(
  922. 'value' => 'l',
  923. 'score' => 1,
  924. ),
  925. array(
  926. 'value' => '.',
  927. 'score' => 1,
  928. ),
  929. );
  930. $items = array(
  931. 1 => array(
  932. 'name' => array(
  933. 'type' => 'text',
  934. 'original_type' => 'text',
  935. 'value' => $orig,
  936. ),
  937. 'search_api_language' => array(
  938. 'type' => 'string',
  939. 'original_type' => 'string',
  940. 'value' => LANGUAGE_NONE,
  941. ),
  942. ),
  943. );
  944. $processor = new SearchApiTokenizer($this->index, array('fields' => array('name' => 'name'), 'spaces' => '[^\p{L}\p{N}]', 'ignorable' => '[-]'));
  945. $tmp = $items;
  946. $processor->preprocessIndexItems($tmp);
  947. $this->assertEqual($tmp[1]['name']['value'], $processed1, 'Value was correctly tokenized with default settings.');
  948. $query = new SearchApiQuery($this->index, array('parse mode' => 'direct'));
  949. $query->keys("foo \"bar-baz\" \n\t foobar1");
  950. $processor->preprocessSearchQuery($query);
  951. $this->assertEqual($query->getKeys(), 'foo barbaz foobar1', 'Search keys were processed correctly.');
  952. $processor = new SearchApiTokenizer($this->index, array('fields' => array('name' => 'name'), 'spaces' => '[-a]', 'ignorable' => '\s'));
  953. $tmp = $items;
  954. $processor->preprocessIndexItems($tmp);
  955. $this->assertEqual($tmp[1]['name']['value'], $processed2, 'Value was correctly tokenized with custom settings.');
  956. $query = new SearchApiQuery($this->index, array('parse mode' => 'direct'));
  957. $query->keys("foo \"bar-baz\" \n\t foobar1");
  958. $processor->preprocessSearchQuery($query);
  959. $this->assertEqual($query->getKeys(), 'foo"b r b z"foob r1', 'Search keys were processed correctly.');
  960. }
  961. /**
  962. * Tests the functionality of the "HTML filter" processor.
  963. */
  964. protected function checkHtmlFilter() {
  965. $orig = <<<END
  966. This is <em lang="en" title =
  967. "something">a test</em>.
  968. How to write <strong>links to <em>other sites</em></strong>: &lt;a href="URL" title="MOUSEOVER TEXT"&gt;TEXT&lt;/a&gt;.
  969. &lt; signs can be <A HREF="http://example.com/topic/html-escapes" TITLE = 'HTML &quot;escapes&quot;'
  970. TARGET = '_blank'>escaped</A> with "&amp;lt;".
  971. <img src = "foo.png" alt = "someone's image" />
  972. END;
  973. $tags = <<<END
  974. em = 1.5
  975. strong = 2
  976. END;
  977. $processed1 = array(
  978. array('value' => 'This', 'score' => 1),
  979. array('value' => 'is', 'score' => 1),
  980. array('value' => 'something', 'score' => 1.5),
  981. array('value' => 'a', 'score' => 1.5),
  982. array('value' => 'test', 'score' => 1.5),
  983. array('value' => 'How', 'score' => 1),
  984. array('value' => 'to', 'score' => 1),
  985. array('value' => 'write', 'score' => 1),
  986. array('value' => 'links', 'score' => 2),
  987. array('value' => 'to', 'score' => 2),
  988. array('value' => 'other', 'score' => 3),
  989. array('value' => 'sites', 'score' => 3),
  990. array('value' => '<a', 'score' => 1),
  991. array('value' => 'href="URL"', 'score' => 1),
  992. array('value' => 'title="MOUSEOVER', 'score' => 1),
  993. array('value' => 'TEXT">TEXT</a>', 'score' => 1),
  994. array('value' => '<', 'score' => 1),
  995. array('value' => 'signs', 'score' => 1),
  996. array('value' => 'can', 'score' => 1),
  997. array('value' => 'be', 'score' => 1),
  998. array('value' => 'HTML', 'score' => 1),
  999. array('value' => '"escapes"', 'score' => 1),
  1000. array('value' => 'escaped', 'score' => 1),
  1001. array('value' => 'with', 'score' => 1),
  1002. array('value' => '"&lt;"', 'score' => 1),
  1003. array('value' => 'someone\'s', 'score' => 1),
  1004. array('value' => 'image', 'score' => 1),
  1005. );
  1006. $items = array(
  1007. 1 => array(
  1008. 'name' => array(
  1009. 'type' => 'text',
  1010. 'original_type' => 'text',
  1011. 'value' => $orig,
  1012. ),
  1013. 'search_api_language' => array(
  1014. 'type' => 'string',
  1015. 'original_type' => 'string',
  1016. 'value' => LANGUAGE_NONE,
  1017. ),
  1018. ),
  1019. );
  1020. $tmp = $items;
  1021. $processor = new SearchApiHtmlFilter($this->index, array('fields' => array('name' => 'name'), 'title' => TRUE, 'alt' => TRUE, 'tags' => $tags));
  1022. $processor->preprocessIndexItems($tmp);
  1023. $processor = new SearchApiTokenizer($this->index, array('fields' => array('name' => 'name'), 'spaces' => '[\s.:]', 'ignorable' => ''));
  1024. $processor->preprocessIndexItems($tmp);
  1025. $this->assertEqual($tmp[1]['name']['value'], $processed1, 'Text was correctly processed.');
  1026. }
  1027. /**
  1028. * Tests the entity datasource controller and its bundle setting.
  1029. */
  1030. protected function checkEntityDatasource() {
  1031. // First, create the necessary content types.
  1032. $type = (object) array(
  1033. 'type' => 'article',
  1034. 'base' => 'article',
  1035. );
  1036. node_type_save($type);
  1037. $type->type = $type->base = 'page';
  1038. node_type_save($type);
  1039. // Now, create some nodes.
  1040. $node = (object) array(
  1041. 'title' => 'Foo',
  1042. 'type' => 'article',
  1043. );
  1044. node_save($node);
  1045. $nid1 = $node->nid;
  1046. $node = (object) array(
  1047. 'title' => 'Bar',
  1048. 'type' => 'article',
  1049. );
  1050. node_save($node);
  1051. $node = (object) array(
  1052. 'title' => 'Baz',
  1053. 'type' => 'page',
  1054. );
  1055. node_save($node);
  1056. // We can't use $this->index here, since users don't have bundles.
  1057. $index = entity_create('search_api_index', array(
  1058. 'id' => 2,
  1059. 'name' => 'test2',
  1060. 'machine_name' => 'test2',
  1061. 'enabled' => 1,
  1062. 'item_type' => 'node',
  1063. 'options' => array(
  1064. 'fields' => array(
  1065. 'nid' => array(
  1066. 'type' => 'integer',
  1067. ),
  1068. ),
  1069. ),
  1070. ));
  1071. // Now start tracking and check whether the index status is correct.
  1072. $datasource = search_api_get_datasource_controller('node');
  1073. $datasource->startTracking(array($index));
  1074. $status = $datasource->getIndexStatus($index);
  1075. $this->assertEqual($status['total'], 3, 'Correct number of items marked for indexing on not bundle-specific index.');
  1076. $datasource->stopTracking(array($index));
  1077. // Once again, but with only indexing articles.
  1078. $index->options['datasource']['bundles'] = array('article');
  1079. drupal_static_reset('search_api_get_datasource_controller');
  1080. $datasource = search_api_get_datasource_controller('node');
  1081. $datasource->startTracking(array($index));
  1082. $status = $datasource->getIndexStatus($index);
  1083. $this->assertEqual($status['total'], 2, 'Correct number of items marked for indexing on bundle-specific index.');
  1084. $datasource->stopTracking(array($index));
  1085. // Now test that bundle renaming works.
  1086. $index->save();
  1087. field_attach_rename_bundle('node', 'article', 'foo');
  1088. $index = search_api_index_load('test2', TRUE);
  1089. $this->assertEqual($index->options['datasource']['bundles'], array('foo'), 'Bundle was correctly renamed in index settings.');
  1090. $index->delete();
  1091. }
  1092. }