search_api.test 48 KB

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