search_api.test 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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>.<h3>Header</h3>
  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. h3 = 3
  977. END;
  978. $processed1 = array(
  979. array('value' => 'This', 'score' => 1),
  980. array('value' => 'is', 'score' => 1),
  981. array('value' => 'something', 'score' => 1.5),
  982. array('value' => 'a', 'score' => 1.5),
  983. array('value' => 'test', 'score' => 1.5),
  984. array('value' => 'Header', 'score' => 3),
  985. array('value' => 'How', 'score' => 1),
  986. array('value' => 'to', 'score' => 1),
  987. array('value' => 'write', 'score' => 1),
  988. array('value' => 'links', 'score' => 2),
  989. array('value' => 'to', 'score' => 2),
  990. array('value' => 'other', 'score' => 3),
  991. array('value' => 'sites', 'score' => 3),
  992. array('value' => '<a', 'score' => 1),
  993. array('value' => 'href="URL"', 'score' => 1),
  994. array('value' => 'title="MOUSEOVER', 'score' => 1),
  995. array('value' => 'TEXT">TEXT</a>', 'score' => 1),
  996. array('value' => '<', 'score' => 1),
  997. array('value' => 'signs', 'score' => 1),
  998. array('value' => 'can', 'score' => 1),
  999. array('value' => 'be', 'score' => 1),
  1000. array('value' => 'HTML', 'score' => 1),
  1001. array('value' => '"escapes"', 'score' => 1),
  1002. array('value' => 'escaped', 'score' => 1),
  1003. array('value' => 'with', 'score' => 1),
  1004. array('value' => '"&lt;"', 'score' => 1),
  1005. array('value' => 'someone\'s', 'score' => 1),
  1006. array('value' => 'image', 'score' => 1),
  1007. );
  1008. $items = array(
  1009. 1 => array(
  1010. 'name' => array(
  1011. 'type' => 'text',
  1012. 'original_type' => 'text',
  1013. 'value' => $orig,
  1014. ),
  1015. 'search_api_language' => array(
  1016. 'type' => 'string',
  1017. 'original_type' => 'string',
  1018. 'value' => LANGUAGE_NONE,
  1019. ),
  1020. ),
  1021. );
  1022. $tmp = $items;
  1023. $processor = new SearchApiHtmlFilter($this->index, array('fields' => array('name' => 'name'), 'title' => TRUE, 'alt' => TRUE, 'tags' => $tags));
  1024. $processor->preprocessIndexItems($tmp);
  1025. $processor = new SearchApiTokenizer($this->index, array('fields' => array('name' => 'name'), 'spaces' => '[\s.:]', 'ignorable' => ''));
  1026. $processor->preprocessIndexItems($tmp);
  1027. $this->assertEqual($tmp[1]['name']['value'], $processed1, 'Text was correctly processed.');
  1028. }
  1029. /**
  1030. * Tests the entity datasource controller and its bundle setting.
  1031. */
  1032. protected function checkEntityDatasource() {
  1033. // First, create the necessary content types.
  1034. $type = (object) array(
  1035. 'type' => 'article',
  1036. 'base' => 'article',
  1037. );
  1038. node_type_save($type);
  1039. $type->type = $type->base = 'page';
  1040. node_type_save($type);
  1041. // Now, create some nodes.
  1042. $node = (object) array(
  1043. 'title' => 'Foo',
  1044. 'type' => 'article',
  1045. );
  1046. node_save($node);
  1047. $nid1 = $node->nid;
  1048. $node = (object) array(
  1049. 'title' => 'Bar',
  1050. 'type' => 'article',
  1051. );
  1052. node_save($node);
  1053. $node = (object) array(
  1054. 'title' => 'Baz',
  1055. 'type' => 'page',
  1056. );
  1057. node_save($node);
  1058. // We can't use $this->index here, since users don't have bundles.
  1059. $index = entity_create('search_api_index', array(
  1060. 'id' => 2,
  1061. 'name' => 'test2',
  1062. 'machine_name' => 'test2',
  1063. 'enabled' => 1,
  1064. 'item_type' => 'node',
  1065. 'options' => array(
  1066. 'fields' => array(
  1067. 'nid' => array(
  1068. 'type' => 'integer',
  1069. ),
  1070. ),
  1071. ),
  1072. ));
  1073. // Now start tracking and check whether the index status is correct.
  1074. $datasource = search_api_get_datasource_controller('node');
  1075. $datasource->startTracking(array($index));
  1076. $status = $datasource->getIndexStatus($index);
  1077. $this->assertEqual($status['total'], 3, 'Correct number of items marked for indexing on not bundle-specific index.');
  1078. $datasource->stopTracking(array($index));
  1079. // Once again, but with only indexing articles.
  1080. $index->options['datasource']['bundles'] = array('article');
  1081. drupal_static_reset('search_api_get_datasource_controller');
  1082. $datasource = search_api_get_datasource_controller('node');
  1083. $datasource->startTracking(array($index));
  1084. $status = $datasource->getIndexStatus($index);
  1085. $this->assertEqual($status['total'], 2, 'Correct number of items marked for indexing on bundle-specific index.');
  1086. $datasource->stopTracking(array($index));
  1087. // Now test that bundle renaming works.
  1088. $index->save();
  1089. field_attach_rename_bundle('node', 'article', 'foo');
  1090. $index = search_api_index_load('test2', TRUE);
  1091. $this->assertEqual($index->options['datasource']['bundles'], array('foo'), 'Bundle was correctly renamed in index settings.');
  1092. $index->delete();
  1093. }
  1094. }