apachesolr_base.test 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. abstract class DrupalSolrOfflineWebTestCase extends DrupalWebTestCase {
  3. function _nestedCompare($a1, $a2) {
  4. if (count($a1) != count($a2)) {
  5. //$extra1 = array_diff_key($a1, $a2);
  6. //$extra2 = array_diff_key($a2, $a1);
  7. //$extra = '';
  8. //if ($extra1) {
  9. // $extra .= ' Extra keys in $a1: ' . implode(', ', array_keys($extra1));
  10. //}
  11. //if ($extra2) {
  12. // $extra .= ' Extra keys in $a2: ' . implode(', ', array_keys($extra2));
  13. //}
  14. //debug('count($a1) != count($a2) :' . $extra);
  15. return FALSE;
  16. }
  17. foreach ($a1 as $k => $v) {
  18. if (!isset($a2[$k])) {
  19. //debug("\$a2[$k] is not set");
  20. return FALSE;
  21. }
  22. if (is_array($a1[$k]) && is_array($a2[$k])) {
  23. if (!$this->_nestedCompare($a1[$k], $a2[$k])) {
  24. //debug("_nestedCompare(\$a1[$k], \$a2[$k]) is false");
  25. return FALSE;
  26. }
  27. }
  28. elseif ($a1[$k] !== $a2[$k]) {
  29. //debug("\$a1[$k] !== \$a2[$k] : " . var_export($a1[$k], TRUE) . " " . var_export($a2[$k], TRUE));
  30. return FALSE;
  31. }
  32. }
  33. return TRUE;
  34. }
  35. }
  36. /**
  37. * @file
  38. * Unit test class that provides tests for base functionality of the Apachesolr
  39. * Module without having the need of a Solr Server
  40. */
  41. class DrupalSolrOfflineEnvironmentWebTestCase extends DrupalSolrOfflineWebTestCase {
  42. /**
  43. * A global basic user who can search.
  44. */
  45. var $basic_user;
  46. /**
  47. * A global administrative user who can administer search.
  48. */
  49. var $admin_user;
  50. public static function getInfo() {
  51. return array(
  52. 'name' => 'Solr Search Environments',
  53. 'description' => 'Tests search environments functionality of the Solr module',
  54. 'group' => 'ApacheSolr',
  55. );
  56. }
  57. /**
  58. * Implementation of setUp().
  59. */
  60. function setUp() {
  61. parent::setUp('apachesolr', 'search', 'apachesolr_search', 'apachesolr_test');
  62. // Create a basic user, which is subject to moderation.
  63. $permissions = array(
  64. 'access content',
  65. 'search content',
  66. );
  67. $basic_user = $this->drupalCreateUser($permissions);
  68. // Create an admin user that can bypass revision moderation.
  69. $permissions = array(
  70. 'access content',
  71. 'search content',
  72. 'administer nodes',
  73. 'administer search',
  74. );
  75. $admin_user = $this->drupalCreateUser($permissions);
  76. // Assign users to their test suite-wide properties.
  77. $this->basic_user = $basic_user;
  78. $this->admin_user = $admin_user;
  79. }
  80. /**
  81. * Asserts that the module was installed and that a notice appears that the server is offline
  82. */
  83. function testServerOffline() {
  84. // Load the default server.
  85. $env_id = apachesolr_default_environment();
  86. $environment = apachesolr_environment_load($env_id);
  87. $environment['url'] = 'http://localhost/solr/core_that_should_not_exist';
  88. apachesolr_environment_save($environment);
  89. $status = apachesolr_server_status($environment['url']);
  90. $this->assertFalse($status, t('A false URL could not be loaded and is offline'));
  91. $this->drupalLogin($this->admin_user);
  92. $this->drupalGet('admin/config/search/apachesolr');
  93. $text = t('The server seems to be unavailable. Please verify the server settings');
  94. $this->assertText($text, t('When checking the status of the server it gives the correct message to inform the user that the server is not reachable'));
  95. }
  96. /**
  97. * Asserts that the module was installed and that a notice appears that the server is offline
  98. */
  99. function testIndexFileIncluded() {
  100. $env_id = apachesolr_default_environment();
  101. $environment = apachesolr_environment_load($env_id);
  102. $environment['url'] = 'http://localhost/solr/core_that_should_not_exist';
  103. apachesolr_environment_save($environment);
  104. $paths = array(
  105. 'user',
  106. 'node',
  107. 'admin/config/search/apachesolr',
  108. 'admin/config/search/apachesolr/search-pages',
  109. 'admin/config/search/apachesolr/search-pages/core_search/edit',
  110. 'admin/structure/block/manage/apachesolr_search/mlt-001/configure',
  111. 'admin/config/search/apachesolr/settings/solr/bias',
  112. 'admin/config/search/apachesolr/settings/solr/index',
  113. 'admin/config/search/apachesolr/settings/solr/edit',
  114. 'admin/reports/apachesolr',
  115. 'admin/reports/apachesolr/conf',
  116. 'search/site',
  117. );
  118. $this->drupalLogin($this->admin_user);
  119. foreach ($paths as $path) {
  120. $this->drupalGet($path);
  121. $text = 'apachesolr.index.inc was included';
  122. $this->assertNoText($text, t('Apachesolr.index.inc was not included'));
  123. }
  124. }
  125. /**
  126. * Asserts that we can edit a search environment
  127. */
  128. function testEditSearchEnvironment() {
  129. $this->drupalLogin($this->admin_user);
  130. $this->drupalGet('admin/config/search/apachesolr/settings');
  131. $this->clickLink(t('Edit'));
  132. $this->assertText(t('Example: http://localhost:8983/solr'), t('Edit page was successfully loaded'));
  133. $edit = array('name' => 'new description foo bar', 'url' => 'http://localhost:8983/solr/core_does_not_exists');
  134. $this->drupalPost($this->getUrl(), $edit, t('Save'));
  135. $this->assertResponse(200);
  136. $this->drupalGet('admin/config/search/apachesolr/settings');
  137. $this->assertText(t('new description foo bar'), t('Search environment description was successfully edited'));
  138. $this->assertText('http://localhost:8983/solr/core_does_not_exists', t('Search environment url was successfully edited'));
  139. }
  140. /**
  141. * Asserts that we can use various url forms for the search environment
  142. */
  143. function testEditSearchEnvironmentURLs() {
  144. // Set the various url schemes that will be tested
  145. $urls = array(
  146. 'http://user@localhost:8983/solr/core_does_not_exists',
  147. 'http://user:pass@localhost:8983/solr/core_does_not_exists',
  148. 'http://user:pass@localhost/solr/core_does_not_exists',
  149. 'https://localhost:8983/solr/core_does_not_exists'
  150. );
  151. $this->drupalLogin($this->admin_user);
  152. foreach ($urls as $url) {
  153. $this->drupalGet('admin/config/search/apachesolr/settings');
  154. $this->clickLink(t('Edit'));
  155. $this->assertText(t('Example: http://localhost:8983/solr'), t('Edit page was successfully loaded'));
  156. $edit = array('url' => $url);
  157. $this->drupalPost($this->getUrl(), $edit, t('Save'));
  158. $this->assertResponse(200);
  159. $this->drupalGet('admin/config/search/apachesolr/settings');
  160. $this->assertText($url, t('Search environment url was successfully set to !url', array('!url' => $url)));
  161. }
  162. }
  163. /**
  164. * Asserts that we can clone a search environment
  165. */
  166. function testCloneSearchEnvironment() {
  167. $this->drupalLogin($this->admin_user);
  168. $this->drupalGet('admin/config/search/apachesolr/settings');
  169. $this->assertText(t('Clone'), t('Clone button is available'));
  170. $this->drupalGet('admin/config/search/apachesolr/settings/solr/clone');
  171. $this->assertText(t('Are you sure you want to clone search environment localhost server'), t('Clone confirmation page was successfully loaded'));
  172. $this->drupalPost($this->getUrl(), array(), t('Clone'));
  173. $this->assertResponse(200);
  174. $this->drupalGet('admin/config/search/apachesolr/settings');
  175. $this->assertText(t('localhost server [cloned]'), t('Search Environment was successfully cloned'));
  176. // Check if the bundles and configurations are exactly the same
  177. // after we clear the caches.
  178. apachesolr_environments_clear_cache();
  179. $envs = apachesolr_load_all_environments();
  180. $this->assertEqual(count($envs), 2, 'Now we have 2 environments');
  181. $orig_env = $envs['solr'];
  182. unset($envs['solr']);
  183. $cloned_env = array_pop($envs);
  184. $this->assertTrue($this->_nestedCompare($orig_env['index_bundles'], $cloned_env['index_bundles']));
  185. $this->assertTrue($this->_nestedCompare($orig_env['conf'], $cloned_env['conf']));
  186. }
  187. /**
  188. * Asserts that we can edit a search environment
  189. */
  190. function testCreateNewSearchEnvironment() {
  191. // Create a new environment
  192. $this->drupalLogin($this->admin_user);
  193. $this->drupalGet('admin/config/search/apachesolr/settings');
  194. $this->assertText(t('Add search environment'), t('Create new environment link is available'));
  195. $this->clickLink(t('Add search environment'));
  196. $this->assertText(t('Make this Solr search environment the default'), t('Environment creation page successfully added'));
  197. $edit = array('url' => 'http://localhost:8983/solr', 'name' => 'my test description', 'env_id' => 'solr_test');
  198. $this->drupalPost($this->getUrl(), $edit, t('Save'));
  199. $this->assertResponse(200);
  200. $this->drupalGet('admin/config/search/apachesolr/settings');
  201. $this->assertText(t('my test description'), t('Search Environment was successfully created'));
  202. // Make this new search environment the default
  203. $this->drupalGet('admin/config/search/apachesolr/settings');
  204. // Click on the second environment edit link
  205. $this->clickLink(t('Edit'), 1);
  206. $this->assertText(t('Example: http://localhost:8983/solr'), t('Edit page was successfully loaded'));
  207. $edit = array('make_default' => 1, 'conf[apachesolr_read_only]' => APACHESOLR_READ_ONLY);
  208. $this->drupalPost($this->getUrl(), $edit, t('Save'));
  209. $this->assertResponse(200);
  210. $this->drupalGet('admin/config/search/apachesolr/settings');
  211. $this->assertText(t('my test description (Default)'), t('New Search environment was successfully changed to default environment'));
  212. // Clear our cache.
  213. apachesolr_environments_clear_cache();
  214. $mode = apachesolr_environment_variable_get('solr_test', 'apachesolr_read_only', APACHESOLR_READ_WRITE);
  215. $this->assertEqual($mode, APACHESOLR_READ_ONLY, 'Environment successfully changed to read only');
  216. }
  217. }
  218. /**
  219. * @file
  220. * Unit test class that provides tests for base functionality of the Apachesolr
  221. * Module without having the need of a Solr Server
  222. */
  223. class DrupalSolrOfflineSearchPagesWebTestCase extends DrupalSolrOfflineWebTestCase {
  224. /**
  225. * A global basic user who can search.
  226. */
  227. var $basic_user;
  228. /**
  229. * A global administrative user who can administer search.
  230. */
  231. var $admin_user;
  232. public static function getInfo() {
  233. return array(
  234. 'name' => 'Solr Search Pages',
  235. 'description' => 'Tests search pages functionality of the Solr module',
  236. 'group' => 'ApacheSolr',
  237. );
  238. }
  239. /**
  240. * Implementation of setUp().
  241. */
  242. function setUp() {
  243. parent::setUp('apachesolr', 'apachesolr_search', 'search', 'apachesolr_test');
  244. // Create a basic user, which is subject to moderation.
  245. $permissions = array(
  246. 'access content',
  247. 'search content',
  248. );
  249. $basic_user = $this->drupalCreateUser($permissions);
  250. // Create an admin user that can bypass revision moderation.
  251. $permissions = array(
  252. 'access content',
  253. 'search content',
  254. 'administer nodes',
  255. 'administer search',
  256. );
  257. $admin_user = $this->drupalCreateUser($permissions);
  258. // Assign users to their test suite-wide properties.
  259. $this->basic_user = $basic_user;
  260. $this->admin_user = $admin_user;
  261. // Make sure our environment does not exists
  262. $env_id = apachesolr_default_environment(NULL, TRUE);
  263. $environment = apachesolr_environment_load($env_id, TRUE);
  264. $environment['url'] = 'http://localhost/solr/core_that_should_not_exist';
  265. apachesolr_environment_save($environment);
  266. // Reset all caches
  267. apachesolr_load_all_environments(TRUE);
  268. }
  269. /**
  270. * Asserts that we can edit a search environment
  271. */
  272. function testCheckCoreSearchPage() {
  273. // Create a new environment
  274. $this->drupalLogin($this->admin_user);
  275. $this->drupalGet('admin/config/search/apachesolr/search-pages');
  276. $this->assertText(t('Core Search'), t('Core Search page is available'));
  277. }
  278. /**
  279. * Asserts that we can edit a search environment
  280. */
  281. function testEditSearchPage() {
  282. $this->drupalLogin($this->admin_user);
  283. $this->drupalGet('admin/config/search/apachesolr/search-pages');
  284. $this->clickLink(t('Edit'));
  285. $this->assertText(t('The human-readable name of the search page configuration'), t('Edit page was successfully loaded'));
  286. $edit = array(
  287. 'label' => 'Test Search Page',
  288. 'description' => 'Test Description',
  289. 'page_title' => 'Test Title',
  290. 'search_path' => 'search/searchdifferentpath',
  291. );
  292. $this->drupalPost($this->getUrl(), $edit, t('Save'));
  293. $this->assertResponse(200);
  294. // Make sure the menu is recognized
  295. drupal_static_reset('apachesolr_search_load_all_search_pages');
  296. menu_cache_clear_all();
  297. menu_rebuild();
  298. $this->drupalGet('admin/config/search/apachesolr/search-pages');
  299. $this->assertText(t('Test Search Page'), t('Search page was successfully edited'));
  300. $this->assertText('search/searchdifferentpath', t('Search path was updated'));
  301. $this->drupalGet('search/searchdifferentpath');
  302. $this->assertText(t('Search is temporarily unavailable. If the problem persists, please contact the site administrator.'), t('Search path was successfully created and is accessible'));
  303. }
  304. /**
  305. * Asserts that we can clone a search page
  306. */
  307. function testCloneSearchPage() {
  308. $this->drupalLogin($this->admin_user);
  309. $this->drupalGet('admin/config/search/apachesolr/search-pages');
  310. $this->assertText(t('Clone'), t('Clone button is available'));
  311. $this->drupalGet('admin/config/search/apachesolr/search-pages/core_search/clone');
  312. $this->assertText(t('Are you sure you want to clone search page Core Search?'), t('Clone confirmation page was successfully loaded'));
  313. $this->drupalPost($this->getUrl(), array(), t('Clone'));
  314. $this->assertResponse(200);
  315. drupal_static_reset('apachesolr_search_load_all_search_pages');
  316. $this->drupalGet('admin/config/search/apachesolr/search-pages');
  317. $this->assertText(t('Core Search [cloned]'), 'Search page was successfully cloned');
  318. }
  319. /**
  320. * Asserts that we can edit a search environment
  321. */
  322. function testNewAndRemoveSearchPage() {
  323. // Create a new search page
  324. $this->drupalLogin($this->admin_user);
  325. $this->drupalGet('admin/config/search/apachesolr/search-pages');
  326. $this->assertText(t('Add search page'), t('Create new search page link is available'));
  327. $this->clickLink(t('Add search page'));
  328. $this->assertText(t('The human-readable name of the search page configuration.'), t('Search page creation page successfully added'));
  329. $edit = array(
  330. 'page_id' => 'solr_testingsuite',
  331. 'env_id' => 'solr',
  332. 'label' => 'Test Search Page',
  333. 'description' => 'Test Description',
  334. 'page_title' => 'Test Title',
  335. 'search_path' => 'search/searchdifferentpath',
  336. );
  337. $this->drupalPost($this->getUrl(), $edit, t('Save'));
  338. $this->assertResponse(200);
  339. // Make sure the menu is recognized
  340. drupal_static_reset('apachesolr_search_load_all_search_pages');
  341. menu_cache_clear_all();
  342. menu_rebuild();
  343. $this->drupalGet('admin/config/search/apachesolr/search-pages');
  344. $this->assertText(t('Test Search Page'), t('Search Page was successfully created'));
  345. // Remove the same environment
  346. $this->clickLink(t('Delete'));
  347. $this->assertText(t('search page configuration will be deleted.This action cannot be undone.'), t('Delete confirmation page was successfully loaded'));
  348. $this->drupalPost($this->getUrl(), array(), t('Delete'));
  349. $this->assertResponse(200);
  350. drupal_static_reset('apachesolr_search_load_all_search_pages');
  351. $this->drupalGet('admin/config/search/apachesolr/search-pages');
  352. $this->assertNoText(t('Test Search Page'), t('Search Environment was successfully deleted'));
  353. apachesolr_environment_save(array('env_id' => 'DummySolr', 'service_class' => 'DummySolr', 'name' => 'dummy server', 'url' => 'http://localhost:8983/solr'));
  354. $solr = new DummySolr($url = NULL, $env_id = 'DummySolr');
  355. $params = array(
  356. 'rows' => 5,
  357. );
  358. $results = apachesolr_search_run('apachesolr_test', $params, '', '', 0, $solr);
  359. $query = apachesolr_current_query('DummySolr');
  360. $this->assertEqual($query->getParam('rows'), 5, 'Passed in rows param overrode default');
  361. }
  362. }
  363. class DrupalSolrNodeTestCase extends DrupalWebTestCase {
  364. public static function getInfo() {
  365. return array(
  366. 'name' => 'Solr Node add, deletion, indexing and document building tests',
  367. 'description' => 'Tests if we can successfully add and delete nodes',
  368. 'group' => 'ApacheSolr',
  369. );
  370. }
  371. /**
  372. * Implementation of setUp().
  373. */
  374. function setUp() {
  375. parent::setUp('apachesolr', 'apachesolr_search', 'search', 'apachesolr_test');
  376. }
  377. function testApacheSolrNodeAddDelete() {
  378. // Login as basic user to perform initial content creation.
  379. // Create an admin user that can bypass revision moderation.
  380. $permissions = array(
  381. 'access content',
  382. 'search content',
  383. 'administer nodes',
  384. 'administer search',
  385. 'access content overview',
  386. 'bypass node access',
  387. );
  388. $admin_user = $this->drupalCreateUser($permissions);
  389. $this->drupalLogin($admin_user);
  390. // enable our bundles to be indexed, and clear caches
  391. apachesolr_index_set_bundles('solr', 'node', array('page', 'article'));
  392. entity_info_cache_clear();
  393. apachesolr_environments_clear_cache();
  394. // Define types of node bundles that we want to index
  395. $types = array('page', 'article');
  396. foreach ($types as $type) {
  397. $edit = array();
  398. // Create a node of the type $type.
  399. $edit['uid'] = $admin_user->uid;
  400. $edit['type'] = $type;
  401. $edit['title'] = $this->randomName(16);
  402. $node = $this->drupalCreateNode($edit);
  403. $this->assertTrue(is_object($node) && isset($node->nid), t('Article type @type has been created.', array('@type' => $type)));
  404. // Check that the node has been created.
  405. $node = $this->drupalGetNodeByTitle($edit['title']);
  406. $this->assertTrue($node, t('Created article @type found in database.', array('@type' => $type)));
  407. // Check that the node has status 1
  408. $indexer_table = apachesolr_get_indexer_table('node');
  409. $query = db_select($indexer_table, 'aien')
  410. ->condition('entity_id', $node->nid)
  411. ->fields('aien', array('entity_id', 'status'));
  412. $db_node = $query->execute()->fetchObject();
  413. $this->assertEqual($db_node->status, 1, t('Node @entity_id has status 1', array('@entity_id' => $db_node->entity_id)));
  414. // Delete the node
  415. $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
  416. // check if the entity delete does its work. It should have set the
  417. // status to 0 so it will be deleted when solr comes online
  418. $indexer_table = apachesolr_get_indexer_table('node');
  419. $nodes_in_tracking_table = db_select($indexer_table, 'aien')
  420. ->condition('entity_id', $node->nid)
  421. ->countQuery()
  422. ->execute()
  423. ->fetchField();
  424. if ($nodes_in_tracking_table > 0) {
  425. $db_node = db_select($indexer_table, 'aien')
  426. ->condition('entity_id', $node->nid)
  427. ->fields('aien', array('entity_id', 'status'))
  428. ->execute()
  429. ->fetchObject();
  430. $this->assertEqual($db_node->status, 0, t('Node @entity_id has status 0', array('@entity_id' => $db_node->entity_id)));
  431. }
  432. else {
  433. // Check that all of the nodes (should only have 1) have status 0, it
  434. // is set as 0 because it is pending to be deleted
  435. $this->assertEqual($nodes_in_tracking_table, 0, t('No more nodes in the tracking table'));
  436. }
  437. // Check that all the nodes have been deleted.
  438. $count = db_select('node', 'n')
  439. ->condition('n.nid', $node->nid)
  440. ->countQuery()
  441. ->execute()
  442. ->fetchField();
  443. $this->assertEqual($count, 0, t('No more nodes left in the node table.'));
  444. }
  445. }
  446. function testApacheSolrNodeReindex() {
  447. // Login as basic user to perform initial content creation.
  448. // Create an admin user that can bypass revision moderation.
  449. $permissions = array(
  450. 'access content',
  451. 'search content',
  452. 'administer nodes',
  453. 'administer search',
  454. 'access content overview',
  455. 'bypass node access',
  456. );
  457. $admin_user = $this->drupalCreateUser($permissions);
  458. $this->drupalLogin($admin_user);
  459. // enable our bundles to be indexed, and clear caches
  460. apachesolr_index_set_bundles('solr', 'node', array('page', 'article'));
  461. entity_info_cache_clear();
  462. apachesolr_environments_clear_cache();
  463. // Define types of node bundles that we want to index
  464. $types = array('page', 'article');
  465. // Create 20 nodes (10 times 2)
  466. foreach ($types as $type) {
  467. for ($i = 0; $i < 5; $i++) {
  468. $edit = array();
  469. // Create a node of the type $type.
  470. $edit['uid'] = $admin_user->uid;
  471. $edit['type'] = $type;
  472. $edit['title'] = $this->randomName(16);
  473. $node = $this->drupalCreateNode($edit);
  474. }
  475. }
  476. // Set a static timestamp
  477. $timestamp = 1382019301;
  478. $env_id = apachesolr_default_environment();
  479. // Clear the last timestamp so we know that our nodes have to be indexed.
  480. apachesolr_environment_variable_set($env_id, 'apachesolr_index_last', array());
  481. // Set apachesolr_index_entities_node.changed to the same value
  482. // (REQUEST_TIME).
  483. $table = apachesolr_get_indexer_table('node');
  484. db_update($table)
  485. ->fields(array('changed' => $timestamp, 'status' => 1))
  486. ->execute();
  487. // Fake that we actually indexed before
  488. apachesolr_set_last_index_position($env_id, 'node', $timestamp + 1, 10);
  489. // Set the changed date to after our previous index cycle
  490. db_update($table)
  491. ->fields(array('changed' => $timestamp + 10, 'status' => 1))
  492. ->condition('entity_id', 9, '<=')
  493. ->execute();
  494. // Get the next 5 entities to index.
  495. $set = apachesolr_index_get_entities_to_index($env_id, 'node', 5);
  496. $count = count($set);
  497. $this->assertEqual($count, 5, t('We found 5 entities to index.'));
  498. // Mark the last item from that 5 as last indexed.
  499. $last_row = end($set);
  500. apachesolr_set_last_index_position($env_id, 'node', $last_row->changed, $last_row->entity_id);
  501. // Get the next batch of 5 and this should be 4 items.
  502. $set = apachesolr_index_get_entities_to_index($env_id, 'node', 4);
  503. $count = count($set);
  504. $this->assertEqual($count, 4, t('We found 4 entities to index.'));
  505. // Mark the last item from that 4 as last indexed.
  506. $last_row = end($set);
  507. apachesolr_set_last_index_position($env_id, 'node', $last_row->changed, $last_row->entity_id);
  508. // Get the next batch of 5 and this should be 0 items
  509. $set = apachesolr_index_get_entities_to_index($env_id, 'node', 5);
  510. $count = count($set);
  511. $this->assertEqual($count, 0, t('We found 0 entities to index.'));
  512. }
  513. function testNodeToDocument() {
  514. // enable our bundles to be indexed, and clear caches
  515. apachesolr_index_set_bundles('solr', 'node', array('article'));
  516. entity_info_cache_clear();
  517. apachesolr_environments_clear_cache();
  518. $edit = array();
  519. // Create a node of the type article.
  520. $type = 'article';
  521. $edit['uid'] = 1;
  522. $edit['type'] = $type;
  523. $edit['title'] = $this->randomName(16);
  524. $edit['body'][LANGUAGE_NONE][0]['value'] = 'some other ORDINARY_TEXT ';
  525. // Make sure the format allows all tags.
  526. $edit['body'][LANGUAGE_NONE][0]['format'] = 'full_html';
  527. $tags_to_index = _apachesolr_tags_to_index();
  528. // Tags that are not boosted normally.
  529. $other_tags = array('div' => 'tags_inline', 'span' => 'tags_inline');
  530. $all_tags = $tags_to_index + $other_tags;
  531. $tag_content = array();
  532. foreach ($all_tags as $tag_name => $field_name) {
  533. $tag_content[$tag_name] = strtoupper($tag_name) . '_TAG_CONTENT';
  534. if ($tag_name == 'a') {
  535. $edit['body'][LANGUAGE_NONE][0]['value'] .= "<{$tag_name} href=\"http://example.com\">{$tag_content[$tag_name]}</{$tag_name}> other filler ";
  536. }
  537. else {
  538. $edit['body'][LANGUAGE_NONE][0]['value'] .= "<{$tag_name}>{$tag_content[$tag_name]}</{$tag_name}> dummy text ";
  539. }
  540. }
  541. $node = $this->drupalCreateNode($edit);
  542. $this->assertTrue(is_object($node) && isset($node->nid), t('Article type @type has been created.', array('@type' => $type)));
  543. $item = new stdClass();
  544. $item->entity_id = $node->nid;
  545. $item->entity_type = 'node';
  546. $item->bundle = $node->type;
  547. $env_id = apachesolr_default_environment();
  548. $docs = apachesolr_index_entity_to_documents($item, $env_id);
  549. $this->assertEqual(count($docs), 1, 'Only one document from one node');
  550. $document = end($docs);
  551. $this->assertTrue(strpos($document->content,'ORDINARY_TEXT') !== FALSE, "Found in content field expected: ORDINARY_TEXT");
  552. foreach ($tags_to_index as $tag_name => $field_name) {
  553. $this->assertTrue(strpos($document->content, $tag_content[$tag_name]) !== FALSE, "Found in content field expected: {$tag_content[$tag_name]}");
  554. $this->assertTrue(!empty($document->{$field_name}) && strpos($document->{$field_name}, $tag_content[$tag_name]) !== FALSE, "Found in {$field_name} field expected: {$tag_content[$tag_name]}");
  555. $this->assertTrue(empty($document->{$field_name}) || strpos($document->{$field_name},'ORDINARY_TEXT') === FALSE, "NOT Found in {$field_name}: ORDINARY_TEXT");
  556. }
  557. foreach ($other_tags as $tag_name => $field_name) {
  558. $this->assertTrue(strpos($document->content, $tag_content[$tag_name]) !== FALSE, "Found in content field expected: {$tag_content[$tag_name]}");
  559. $this->assertTrue(empty($document->{$field_name}) || strpos($document->{$field_name}, $tag_content[$tag_name]) === FALSE, "NOT found in {$field_name}: {$tag_content[$tag_name]}");
  560. }
  561. }
  562. }
  563. class DrupalSolrOfflineUnitTestCase extends DrupalUnitTestCase {
  564. public static function getInfo() {
  565. return array(
  566. 'name' => 'Solr Base Framework Tests Unit Test',
  567. 'description' => 'Unit test functionality of the Solr module',
  568. 'group' => 'ApacheSolr',
  569. );
  570. }
  571. protected function setUp() {
  572. parent::setUp();
  573. require_once dirname(dirname(realpath(__FILE__))) . '/apachesolr.module';
  574. $this->script_content = <<<EOF
  575. <p>GOOD_CONTENT</p>
  576. <script type="text/javascript" >
  577. $(document).ready(function(){
  578. $('.accordion_teachers').accordion({ collapsible:true, autoHeight:false });
  579. });
  580. </script>
  581. EOF;
  582. $this->embed_content = <<<EOF
  583. <p>GOOD_CONTENT</p>
  584. <object width="425" height="349"><param name="movie" value="http://www.youtube.com/v/8Vmnq5dBF7Y?version=3&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/8Vmnq5dBF7Y?version=3&amp;hl=en_US" type="application/x-shockwave-flash" width="425" height="349" allowscriptaccess="always" allowfullscreen="true"></embed></object>
  585. OTHER_CONTENT
  586. EOF;
  587. $this->iframe_content = <<<EOF
  588. <iframe width="425" height="349" src="http://www.youtube.com/embed/8Vmnq5dBF7Y" frameborder="0" allowfullscreen></iframe>
  589. <p><a href="#">GOOD_CONTENT</a></p><iframe></iframe>
  590. EOF;
  591. $this->comment_content = <<<EOF
  592. <p><em>GOOD_CONTENT</em></p><!-- COMMENT -->
  593. OTHER_CONTENT
  594. EOF;
  595. }
  596. /**
  597. * Test ordering of parsed filter positions.
  598. *
  599. * Regression test for http://drupal.org/node/891962
  600. */
  601. function testContentFilters() {
  602. $cleaned = apachesolr_clean_text($this->script_content);
  603. $this->assertFalse(strpos($cleaned, 'script'), 'Script tags removed');
  604. $this->assertFalse(strpos($cleaned, 'accordion_teachers'), 'Script tags conent removed');
  605. $this->assertTrue(strpos(trim($cleaned), 'GOOD_CONTENT') === 0, 'Real content retained');
  606. $cleaned = apachesolr_clean_text($this->embed_content);
  607. $this->assertFalse(strpos($cleaned, 'object'), 'object tags removed');
  608. $this->assertFalse(strpos($cleaned, 'embed'), 'embed tags removed');
  609. $this->assertFalse(strpos($cleaned, '8Vmnq5dBF7Y'), 'object tags conent removed');
  610. $this->assertFalse(strpos($cleaned, 'shockwave-flash'), 'embed tags conent removed');
  611. $this->assertTrue(strpos(trim($cleaned), 'GOOD_CONTENT') === 0, 'Real content retained');
  612. $this->assertTrue(strpos($cleaned, 'OTHER_CONTENT') > 0, 'Other content retained');
  613. $cleaned = apachesolr_clean_text($this->iframe_content);
  614. $this->assertFalse(strpos($cleaned, 'iframe'), 'iframe tags removed');
  615. $this->assertFalse(strpos($cleaned, '8Vmnq5dBF7Y'), 'iframe tags conent removed');
  616. $this->assertTrue(strpos(trim($cleaned), 'GOOD_CONTENT') === 0, 'Real content retained');
  617. $cleaned = apachesolr_clean_text($this->comment_content);
  618. $this->assertFalse(strpos($cleaned, 'COMMENT'), 'html comment content removed ');
  619. $this->assertTrue(strpos(trim($cleaned), 'GOOD_CONTENT') === 0, 'Real content retained');
  620. }
  621. }