search.test 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. <?php
  2. /**
  3. * @file
  4. * Tests for search.module.
  5. */
  6. // The search index can contain different types of content. Typically the type is 'node'.
  7. // Here we test with _test_ and _test2_ as the type.
  8. define('SEARCH_TYPE', '_test_');
  9. define('SEARCH_TYPE_2', '_test2_');
  10. define('SEARCH_TYPE_JPN', '_test3_');
  11. /**
  12. * Indexes content and queries it.
  13. */
  14. class SearchMatchTestCase extends DrupalWebTestCase {
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'Search engine queries',
  18. 'description' => 'Indexes content and queries it.',
  19. 'group' => 'Search',
  20. );
  21. }
  22. /**
  23. * Implementation setUp().
  24. */
  25. function setUp() {
  26. parent::setUp('search');
  27. }
  28. /**
  29. * Test search indexing.
  30. */
  31. function testMatching() {
  32. $this->_setup();
  33. $this->_testQueries();
  34. }
  35. /**
  36. * Set up a small index of items to test against.
  37. */
  38. function _setup() {
  39. variable_set('minimum_word_size', 3);
  40. for ($i = 1; $i <= 7; ++$i) {
  41. search_index($i, SEARCH_TYPE, $this->getText($i));
  42. }
  43. for ($i = 1; $i <= 5; ++$i) {
  44. search_index($i + 7, SEARCH_TYPE_2, $this->getText2($i));
  45. }
  46. // No getText builder function for Japanese text; just a simple array.
  47. foreach (array(
  48. 13 => '以呂波耳・ほへとち。リヌルヲ。',
  49. 14 => 'ドルーパルが大好きよ!',
  50. 15 => 'コーヒーとケーキ',
  51. ) as $i => $jpn) {
  52. search_index($i, SEARCH_TYPE_JPN, $jpn);
  53. }
  54. search_update_totals();
  55. }
  56. /**
  57. * _test_: Helper method for generating snippets of content.
  58. *
  59. * Generated items to test against:
  60. * 1 ipsum
  61. * 2 dolore sit
  62. * 3 sit am ut
  63. * 4 am ut enim am
  64. * 5 ut enim am minim veniam
  65. * 6 enim am minim veniam es cillum
  66. * 7 am minim veniam es cillum dolore eu
  67. */
  68. function getText($n) {
  69. $words = explode(' ', "Ipsum dolore sit am. Ut enim am minim veniam. Es cillum dolore eu.");
  70. return implode(' ', array_slice($words, $n - 1, $n));
  71. }
  72. /**
  73. * _test2_: Helper method for generating snippets of content.
  74. *
  75. * Generated items to test against:
  76. * 8 dear
  77. * 9 king philip
  78. * 10 philip came over
  79. * 11 came over from germany
  80. * 12 over from germany swimming
  81. */
  82. function getText2($n) {
  83. $words = explode(' ', "Dear King Philip came over from Germany swimming.");
  84. return implode(' ', array_slice($words, $n - 1, $n));
  85. }
  86. /**
  87. * Run predefine queries looking for indexed terms.
  88. */
  89. function _testQueries() {
  90. /*
  91. Note: OR queries that include short words in OR groups are only accepted
  92. if the ORed terms are ANDed with at least one long word in the rest of the query.
  93. e.g. enim dolore OR ut = enim (dolore OR ut) = (enim dolor) OR (enim ut) -> good
  94. e.g. dolore OR ut = (dolore) OR (ut) -> bad
  95. This is a design limitation to avoid full table scans.
  96. */
  97. $queries = array(
  98. // Simple AND queries.
  99. 'ipsum' => array(1),
  100. 'enim' => array(4, 5, 6),
  101. 'xxxxx' => array(),
  102. 'enim minim' => array(5, 6),
  103. 'enim xxxxx' => array(),
  104. 'dolore eu' => array(7),
  105. 'dolore xx' => array(),
  106. 'ut minim' => array(5),
  107. 'xx minim' => array(),
  108. 'enim veniam am minim ut' => array(5),
  109. // Simple OR queries.
  110. 'dolore OR ipsum' => array(1, 2, 7),
  111. 'dolore OR xxxxx' => array(2, 7),
  112. 'dolore OR ipsum OR enim' => array(1, 2, 4, 5, 6, 7),
  113. 'ipsum OR dolore sit OR cillum' => array(2, 7),
  114. 'minim dolore OR ipsum' => array(7),
  115. 'dolore OR ipsum veniam' => array(7),
  116. 'minim dolore OR ipsum OR enim' => array(5, 6, 7),
  117. 'dolore xx OR yy' => array(),
  118. 'xxxxx dolore OR ipsum' => array(),
  119. // Negative queries.
  120. 'dolore -sit' => array(7),
  121. 'dolore -eu' => array(2),
  122. 'dolore -xxxxx' => array(2, 7),
  123. 'dolore -xx' => array(2, 7),
  124. // Phrase queries.
  125. '"dolore sit"' => array(2),
  126. '"sit dolore"' => array(),
  127. '"am minim veniam es"' => array(6, 7),
  128. '"minim am veniam es"' => array(),
  129. // Mixed queries.
  130. '"am minim veniam es" OR dolore' => array(2, 6, 7),
  131. '"minim am veniam es" OR "dolore sit"' => array(2),
  132. '"minim am veniam es" OR "sit dolore"' => array(),
  133. '"am minim veniam es" -eu' => array(6),
  134. '"am minim veniam" -"cillum dolore"' => array(5, 6),
  135. '"am minim veniam" -"dolore cillum"' => array(5, 6, 7),
  136. 'xxxxx "minim am veniam es" OR dolore' => array(),
  137. 'xx "minim am veniam es" OR dolore' => array()
  138. );
  139. foreach ($queries as $query => $results) {
  140. $result = db_select('search_index', 'i')
  141. ->extend('SearchQuery')
  142. ->searchExpression($query, SEARCH_TYPE)
  143. ->execute();
  144. $set = $result ? $result->fetchAll() : array();
  145. $this->_testQueryMatching($query, $set, $results);
  146. $this->_testQueryScores($query, $set, $results);
  147. }
  148. // These queries are run against the second index type, SEARCH_TYPE_2.
  149. $queries = array(
  150. // Simple AND queries.
  151. 'ipsum' => array(),
  152. 'enim' => array(),
  153. 'enim minim' => array(),
  154. 'dear' => array(8),
  155. 'germany' => array(11, 12),
  156. );
  157. foreach ($queries as $query => $results) {
  158. $result = db_select('search_index', 'i')
  159. ->extend('SearchQuery')
  160. ->searchExpression($query, SEARCH_TYPE_2)
  161. ->execute();
  162. $set = $result ? $result->fetchAll() : array();
  163. $this->_testQueryMatching($query, $set, $results);
  164. $this->_testQueryScores($query, $set, $results);
  165. }
  166. // These queries are run against the third index type, SEARCH_TYPE_JPN.
  167. $queries = array(
  168. // Simple AND queries.
  169. '呂波耳' => array(13),
  170. '以呂波耳' => array(13),
  171. 'ほへと ヌルヲ' => array(13),
  172. 'とちリ' => array(),
  173. 'ドルーパル' => array(14),
  174. 'パルが大' => array(14),
  175. 'コーヒー' => array(15),
  176. 'ヒーキ' => array(),
  177. );
  178. foreach ($queries as $query => $results) {
  179. $result = db_select('search_index', 'i')
  180. ->extend('SearchQuery')
  181. ->searchExpression($query, SEARCH_TYPE_JPN)
  182. ->execute();
  183. $set = $result ? $result->fetchAll() : array();
  184. $this->_testQueryMatching($query, $set, $results);
  185. $this->_testQueryScores($query, $set, $results);
  186. }
  187. }
  188. /**
  189. * Test the matching abilities of the engine.
  190. *
  191. * Verify if a query produces the correct results.
  192. */
  193. function _testQueryMatching($query, $set, $results) {
  194. // Get result IDs.
  195. $found = array();
  196. foreach ($set as $item) {
  197. $found[] = $item->sid;
  198. }
  199. // Compare $results and $found.
  200. sort($found);
  201. sort($results);
  202. $this->assertEqual($found, $results, "Query matching '$query'");
  203. }
  204. /**
  205. * Test the scoring abilities of the engine.
  206. *
  207. * Verify if a query produces normalized, monotonous scores.
  208. */
  209. function _testQueryScores($query, $set, $results) {
  210. // Get result scores.
  211. $scores = array();
  212. foreach ($set as $item) {
  213. $scores[] = $item->calculated_score;
  214. }
  215. // Check order.
  216. $sorted = $scores;
  217. sort($sorted);
  218. $this->assertEqual($scores, array_reverse($sorted), "Query order '$query'");
  219. // Check range.
  220. $this->assertEqual(!count($scores) || (min($scores) > 0.0 && max($scores) <= 1.0001), TRUE, "Query scoring '$query'");
  221. }
  222. }
  223. /**
  224. * Tests the bike shed text on no results page, and text on the search page.
  225. */
  226. class SearchPageText extends DrupalWebTestCase {
  227. protected $searching_user;
  228. public static function getInfo() {
  229. return array(
  230. 'name' => 'Search page text',
  231. 'description' => 'Tests the bike shed text on the no results page, and various other text on search pages.',
  232. 'group' => 'Search'
  233. );
  234. }
  235. function setUp() {
  236. parent::setUp('search');
  237. // Create user.
  238. $this->searching_user = $this->drupalCreateUser(array('search content', 'access user profiles'));
  239. }
  240. /**
  241. * Tests the failed search text, and various other text on the search page.
  242. */
  243. function testSearchText() {
  244. $this->drupalLogin($this->searching_user);
  245. $this->drupalGet('search/node');
  246. $this->assertText(t('Enter your keywords'));
  247. $this->assertText(t('Search'));
  248. $title = t('Search') . ' | Drupal';
  249. $this->assertTitle($title, 'Search page title is correct');
  250. $edit = array();
  251. $edit['keys'] = 'bike shed ' . $this->randomName();
  252. $this->drupalPost('search/node', $edit, t('Search'));
  253. $this->assertText(t('Consider loosening your query with OR. bike OR shed will often show more results than bike shed.'), 'Help text is displayed when search returns no results.');
  254. $this->assertText(t('Search'));
  255. $this->assertTitle($title, 'Search page title is correct');
  256. $edit['keys'] = $this->searching_user->name;
  257. $this->drupalPost('search/user', $edit, t('Search'));
  258. $this->assertText(t('Search'));
  259. $this->assertTitle($title, 'Search page title is correct');
  260. // Test that search keywords containing slashes are correctly loaded
  261. // from the path and displayed in the search form.
  262. $arg = $this->randomName() . '/' . $this->randomName();
  263. $this->drupalGet('search/node/' . $arg);
  264. $input = $this->xpath("//input[@id='edit-keys' and @value='{$arg}']");
  265. $this->assertFalse(empty($input), 'Search keys with a / are correctly set as the default value in the search box.');
  266. // Test a search input exceeding the limit of AND/OR combinations to test
  267. // the Denial-of-Service protection.
  268. $limit = variable_get('search_and_or_limit', 7);
  269. $keys = array();
  270. for ($i = 0; $i < $limit + 1; $i++) {
  271. $keys[] = $this->randomName(3);
  272. if ($i % 2 == 0) {
  273. $keys[] = 'OR';
  274. }
  275. }
  276. $edit['keys'] = implode(' ', $keys);
  277. $this->drupalPost('search/node', $edit, t('Search'));
  278. $this->assertRaw(t('Your search used too many AND/OR expressions. Only the first @count terms were included in this search.', array('@count' => $limit)));
  279. }
  280. }
  281. /**
  282. * Indexes content and tests the advanced search form.
  283. */
  284. class SearchAdvancedSearchForm extends DrupalWebTestCase {
  285. protected $node;
  286. public static function getInfo() {
  287. return array(
  288. 'name' => 'Advanced search form',
  289. 'description' => 'Indexes content and tests the advanced search form.',
  290. 'group' => 'Search',
  291. );
  292. }
  293. function setUp() {
  294. parent::setUp('search');
  295. // Create and login user.
  296. $test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes'));
  297. $this->drupalLogin($test_user);
  298. // Create initial node.
  299. $node = $this->drupalCreateNode();
  300. $this->node = $this->drupalCreateNode();
  301. // First update the index. This does the initial processing.
  302. node_update_index();
  303. // Then, run the shutdown function. Testing is a unique case where indexing
  304. // and searching has to happen in the same request, so running the shutdown
  305. // function manually is needed to finish the indexing process.
  306. search_update_totals();
  307. }
  308. /**
  309. * Test using the search form with GET and POST queries.
  310. * Test using the advanced search form to limit search to nodes of type "Basic page".
  311. */
  312. function testNodeType() {
  313. $this->assertTrue($this->node->type == 'page', 'Node type is Basic page.');
  314. // Assert that the dummy title doesn't equal the real title.
  315. $dummy_title = 'Lorem ipsum';
  316. $this->assertNotEqual($dummy_title, $this->node->title, "Dummy title doesn't equal node title");
  317. // Search for the dummy title with a GET query.
  318. $this->drupalGet('search/node/' . $dummy_title);
  319. $this->assertNoText($this->node->title, 'Basic page node is not found with dummy title.');
  320. // Search for the title of the node with a GET query.
  321. $this->drupalGet('search/node/' . $this->node->title);
  322. $this->assertText($this->node->title, 'Basic page node is found with GET query.');
  323. // Search for the title of the node with a POST query.
  324. $edit = array('or' => $this->node->title);
  325. $this->drupalPost('search/node', $edit, t('Advanced search'));
  326. $this->assertText($this->node->title, 'Basic page node is found with POST query.');
  327. // Advanced search type option.
  328. $this->drupalPost('search/node', array_merge($edit, array('type[page]' => 'page')), t('Advanced search'));
  329. $this->assertText($this->node->title, 'Basic page node is found with POST query and type:page.');
  330. $this->drupalPost('search/node', array_merge($edit, array('type[article]' => 'article')), t('Advanced search'));
  331. $this->assertText('bike shed', 'Article node is not found with POST query and type:article.');
  332. }
  333. }
  334. /**
  335. * Indexes content and tests ranking factors.
  336. */
  337. class SearchRankingTestCase extends DrupalWebTestCase {
  338. public static function getInfo() {
  339. return array(
  340. 'name' => 'Search engine ranking',
  341. 'description' => 'Indexes content and tests ranking factors.',
  342. 'group' => 'Search',
  343. );
  344. }
  345. /**
  346. * Implementation setUp().
  347. */
  348. function setUp() {
  349. parent::setUp('search', 'statistics', 'comment');
  350. }
  351. function testRankings() {
  352. // Login with sufficient privileges.
  353. $this->drupalLogin($this->drupalCreateUser(array('skip comment approval', 'create page content')));
  354. // Build a list of the rankings to test.
  355. $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views');
  356. // Create nodes for testing.
  357. foreach ($node_ranks as $node_rank) {
  358. $settings = array(
  359. 'type' => 'page',
  360. 'title' => 'Drupal rocks',
  361. 'body' => array(LANGUAGE_NONE => array(array('value' => "Drupal's search rocks"))),
  362. );
  363. foreach (array(0, 1) as $num) {
  364. if ($num == 1) {
  365. switch ($node_rank) {
  366. case 'sticky':
  367. case 'promote':
  368. $settings[$node_rank] = 1;
  369. break;
  370. case 'relevance':
  371. $settings['body'][LANGUAGE_NONE][0]['value'] .= " really rocks";
  372. break;
  373. case 'recent':
  374. $settings['created'] = REQUEST_TIME + 3600;
  375. break;
  376. case 'comments':
  377. $settings['comment'] = 2;
  378. break;
  379. }
  380. }
  381. $nodes[$node_rank][$num] = $this->drupalCreateNode($settings);
  382. }
  383. }
  384. // Update the search index.
  385. module_invoke_all('update_index');
  386. search_update_totals();
  387. // Refresh variables after the treatment.
  388. $this->refreshVariables();
  389. // Add a comment to one of the nodes.
  390. $edit = array();
  391. $edit['subject'] = 'my comment title';
  392. $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = 'some random comment';
  393. $this->drupalGet('comment/reply/' . $nodes['comments'][1]->nid);
  394. $this->drupalPost(NULL, $edit, t('Preview'));
  395. $this->drupalPost(NULL, $edit, t('Save'));
  396. // Enable counting of statistics.
  397. variable_set('statistics_count_content_views', 1);
  398. // Then View one of the nodes a bunch of times.
  399. for ($i = 0; $i < 5; $i ++) {
  400. $this->drupalGet('node/' . $nodes['views'][1]->nid);
  401. }
  402. // Test each of the possible rankings.
  403. foreach ($node_ranks as $node_rank) {
  404. // Disable all relevancy rankings except the one we are testing.
  405. foreach ($node_ranks as $var) {
  406. variable_set('node_rank_' . $var, $var == $node_rank ? 10 : 0);
  407. }
  408. // Do the search and assert the results.
  409. $set = node_search_execute('rocks');
  410. $this->assertEqual($set[0]['node']->nid, $nodes[$node_rank][1]->nid, 'Search ranking "' . $node_rank . '" order.');
  411. }
  412. }
  413. /**
  414. * Test rankings of HTML tags.
  415. */
  416. function testHTMLRankings() {
  417. // Login with sufficient privileges.
  418. $this->drupalLogin($this->drupalCreateUser(array('create page content')));
  419. // Test HTML tags with different weights.
  420. $sorted_tags = array('h1', 'h2', 'h3', 'h4', 'a', 'h5', 'h6', 'notag');
  421. $shuffled_tags = $sorted_tags;
  422. // Shuffle tags to ensure HTML tags are ranked properly.
  423. shuffle($shuffled_tags);
  424. $settings = array(
  425. 'type' => 'page',
  426. 'title' => 'Simple node',
  427. );
  428. foreach ($shuffled_tags as $tag) {
  429. switch ($tag) {
  430. case 'a':
  431. $settings['body'] = array(LANGUAGE_NONE => array(array('value' => l('Drupal Rocks', 'node'), 'format' => 'full_html')));
  432. break;
  433. case 'notag':
  434. $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'Drupal Rocks')));
  435. break;
  436. default:
  437. $settings['body'] = array(LANGUAGE_NONE => array(array('value' => "<$tag>Drupal Rocks</$tag>", 'format' => 'full_html')));
  438. break;
  439. }
  440. $nodes[$tag] = $this->drupalCreateNode($settings);
  441. }
  442. // Update the search index.
  443. module_invoke_all('update_index');
  444. search_update_totals();
  445. // Refresh variables after the treatment.
  446. $this->refreshVariables();
  447. // Disable all other rankings.
  448. $node_ranks = array('sticky', 'promote', 'recent', 'comments', 'views');
  449. foreach ($node_ranks as $node_rank) {
  450. variable_set('node_rank_' . $node_rank, 0);
  451. }
  452. $set = node_search_execute('rocks');
  453. // Test the ranking of each tag.
  454. foreach ($sorted_tags as $tag_rank => $tag) {
  455. // Assert the results.
  456. if ($tag == 'notag') {
  457. $this->assertEqual($set[$tag_rank]['node']->nid, $nodes[$tag]->nid, 'Search tag ranking for plain text order.');
  458. } else {
  459. $this->assertEqual($set[$tag_rank]['node']->nid, $nodes[$tag]->nid, 'Search tag ranking for "&lt;' . $sorted_tags[$tag_rank] . '&gt;" order.');
  460. }
  461. }
  462. // Test tags with the same weight against the sorted tags.
  463. $unsorted_tags = array('u', 'b', 'i', 'strong', 'em');
  464. foreach ($unsorted_tags as $tag) {
  465. $settings['body'] = array(LANGUAGE_NONE => array(array('value' => "<$tag>Drupal Rocks</$tag>", 'format' => 'full_html')));
  466. $node = $this->drupalCreateNode($settings);
  467. // Update the search index.
  468. module_invoke_all('update_index');
  469. search_update_totals();
  470. // Refresh variables after the treatment.
  471. $this->refreshVariables();
  472. $set = node_search_execute('rocks');
  473. // Ranking should always be second to last.
  474. $set = array_slice($set, -2, 1);
  475. // Assert the results.
  476. $this->assertEqual($set[0]['node']->nid, $node->nid, 'Search tag ranking for "&lt;' . $tag . '&gt;" order.');
  477. // Delete node so it doesn't show up in subsequent search results.
  478. node_delete($node->nid);
  479. }
  480. }
  481. /**
  482. * Verifies that if we combine two rankings, search still works.
  483. *
  484. * See issue http://drupal.org/node/771596
  485. */
  486. function testDoubleRankings() {
  487. // Login with sufficient privileges.
  488. $this->drupalLogin($this->drupalCreateUser(array('skip comment approval', 'create page content')));
  489. // See testRankings() above - build a node that will rank high for sticky.
  490. $settings = array(
  491. 'type' => 'page',
  492. 'title' => 'Drupal rocks',
  493. 'body' => array(LANGUAGE_NONE => array(array('value' => "Drupal's search rocks"))),
  494. 'sticky' => 1,
  495. );
  496. $node = $this->drupalCreateNode($settings);
  497. // Update the search index.
  498. module_invoke_all('update_index');
  499. search_update_totals();
  500. // Refresh variables after the treatment.
  501. $this->refreshVariables();
  502. // Set up for ranking sticky and lots of comments; make sure others are
  503. // disabled.
  504. $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views');
  505. foreach ($node_ranks as $var) {
  506. $value = ($var == 'sticky' || $var == 'comments') ? 10 : 0;
  507. variable_set('node_rank_' . $var, $value);
  508. }
  509. // Do the search and assert the results.
  510. $set = node_search_execute('rocks');
  511. $this->assertEqual($set[0]['node']->nid, $node->nid, 'Search double ranking order.');
  512. }
  513. }
  514. /**
  515. * Tests the rendering of the search block.
  516. */
  517. class SearchBlockTestCase extends DrupalWebTestCase {
  518. public static function getInfo() {
  519. return array(
  520. 'name' => 'Block availability',
  521. 'description' => 'Check if the search form block is available.',
  522. 'group' => 'Search',
  523. );
  524. }
  525. function setUp() {
  526. parent::setUp('search');
  527. // Create and login user
  528. $admin_user = $this->drupalCreateUser(array('administer blocks', 'search content'));
  529. $this->drupalLogin($admin_user);
  530. }
  531. function testSearchFormBlock() {
  532. // Set block title to confirm that the interface is available.
  533. $this->drupalPost('admin/structure/block/manage/search/form/configure', array('title' => $this->randomName(8)), t('Save block'));
  534. $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
  535. // Set the block to a region to confirm block is available.
  536. $edit = array();
  537. $edit['blocks[search_form][region]'] = 'footer';
  538. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  539. $this->assertText(t('The block settings have been updated.'), 'Block successfully move to footer region.');
  540. }
  541. /**
  542. * Test that the search block form works correctly.
  543. */
  544. function testBlock() {
  545. // Enable the block, and place it in the 'content' region so that it isn't
  546. // hidden on 404 pages.
  547. $edit = array('blocks[search_form][region]' => 'content');
  548. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  549. // Test a normal search via the block form, from the front page.
  550. $terms = array('search_block_form' => 'test');
  551. $this->drupalPost('node', $terms, t('Search'));
  552. $this->assertText('Your search yielded no results');
  553. // Test a search from the block on a 404 page.
  554. $this->drupalGet('foo');
  555. $this->assertResponse(404);
  556. $this->drupalPost(NULL, $terms, t('Search'));
  557. $this->assertResponse(200);
  558. $this->assertText('Your search yielded no results');
  559. // Test a search from the block when it doesn't appear on the search page.
  560. $edit = array('pages' => 'search');
  561. $this->drupalPost('admin/structure/block/manage/search/form/configure', $edit, t('Save block'));
  562. $this->drupalPost('node', $terms, t('Search'));
  563. $this->assertText('Your search yielded no results');
  564. // Confirm that the user is redirected to the search page.
  565. $this->assertEqual(
  566. $this->getUrl(),
  567. url('search/node/' . $terms['search_block_form'], array('absolute' => TRUE)),
  568. 'Redirected to correct url.'
  569. );
  570. // Test an empty search via the block form, from the front page.
  571. $terms = array('search_block_form' => '');
  572. $this->drupalPost('node', $terms, t('Search'));
  573. $this->assertText('Please enter some keywords');
  574. // Confirm that the user is redirected to the search page, when form is submitted empty.
  575. $this->assertEqual(
  576. $this->getUrl(),
  577. url('search/node/', array('absolute' => TRUE)),
  578. 'Redirected to correct url.'
  579. );
  580. }
  581. }
  582. /**
  583. * Tests that searching for a phrase gets the correct page count.
  584. */
  585. class SearchExactTestCase extends DrupalWebTestCase {
  586. public static function getInfo() {
  587. return array(
  588. 'name' => 'Search engine phrase queries',
  589. 'description' => 'Tests that searching for a phrase gets the correct page count.',
  590. 'group' => 'Search',
  591. );
  592. }
  593. function setUp() {
  594. parent::setUp('search');
  595. }
  596. /**
  597. * Tests that the correct number of pager links are found for both keywords and phrases.
  598. */
  599. function testExactQuery() {
  600. // Login with sufficient privileges.
  601. $this->drupalLogin($this->drupalCreateUser(array('create page content', 'search content')));
  602. $settings = array(
  603. 'type' => 'page',
  604. 'title' => 'Simple Node',
  605. );
  606. // Create nodes with exact phrase.
  607. for ($i = 0; $i <= 17; $i++) {
  608. $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'love pizza')));
  609. $this->drupalCreateNode($settings);
  610. }
  611. // Create nodes containing keywords.
  612. for ($i = 0; $i <= 17; $i++) {
  613. $settings['body'] = array(LANGUAGE_NONE => array(array('value' => 'love cheesy pizza')));
  614. $this->drupalCreateNode($settings);
  615. }
  616. // Update the search index.
  617. module_invoke_all('update_index');
  618. search_update_totals();
  619. // Refresh variables after the treatment.
  620. $this->refreshVariables();
  621. // Test that the correct number of pager links are found for keyword search.
  622. $edit = array('keys' => 'love pizza');
  623. $this->drupalPost('search/node', $edit, t('Search'));
  624. $this->assertLinkByHref('page=1', 0, '2nd page link is found for keyword search.');
  625. $this->assertLinkByHref('page=2', 0, '3rd page link is found for keyword search.');
  626. $this->assertLinkByHref('page=3', 0, '4th page link is found for keyword search.');
  627. $this->assertNoLinkByHref('page=4', '5th page link is not found for keyword search.');
  628. // Test that the correct number of pager links are found for exact phrase search.
  629. $edit = array('keys' => '"love pizza"');
  630. $this->drupalPost('search/node', $edit, t('Search'));
  631. $this->assertLinkByHref('page=1', 0, '2nd page link is found for exact phrase search.');
  632. $this->assertNoLinkByHref('page=2', '3rd page link is not found for exact phrase search.');
  633. }
  634. }
  635. /**
  636. * Test integration searching comments.
  637. */
  638. class SearchCommentTestCase extends DrupalWebTestCase {
  639. protected $admin_user;
  640. public static function getInfo() {
  641. return array(
  642. 'name' => 'Comment Search tests',
  643. 'description' => 'Test integration searching comments.',
  644. 'group' => 'Search',
  645. );
  646. }
  647. function setUp() {
  648. parent::setUp('comment', 'search');
  649. // Create and log in an administrative user having access to the Full HTML
  650. // text format.
  651. $full_html_format = filter_format_load('full_html');
  652. $permissions = array(
  653. 'administer filters',
  654. filter_permission_name($full_html_format),
  655. 'administer permissions',
  656. 'create page content',
  657. 'skip comment approval',
  658. 'access comments',
  659. );
  660. $this->admin_user = $this->drupalCreateUser($permissions);
  661. $this->drupalLogin($this->admin_user);
  662. }
  663. /**
  664. * Verify that comments are rendered using proper format in search results.
  665. */
  666. function testSearchResultsComment() {
  667. $comment_body = 'Test comment body';
  668. variable_set('comment_preview_article', DRUPAL_OPTIONAL);
  669. // Enable check_plain() for 'Filtered HTML' text format.
  670. $filtered_html_format_id = 'filtered_html';
  671. $edit = array(
  672. 'filters[filter_html_escape][status]' => TRUE,
  673. );
  674. $this->drupalPost('admin/config/content/formats/' . $filtered_html_format_id, $edit, t('Save configuration'));
  675. // Allow anonymous users to search content.
  676. $edit = array(
  677. DRUPAL_ANONYMOUS_RID . '[search content]' => 1,
  678. DRUPAL_ANONYMOUS_RID . '[access comments]' => 1,
  679. DRUPAL_ANONYMOUS_RID . '[post comments]' => 1,
  680. );
  681. $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
  682. // Create a node.
  683. $node = $this->drupalCreateNode(array('type' => 'article'));
  684. // Post a comment using 'Full HTML' text format.
  685. $edit_comment = array();
  686. $edit_comment['subject'] = 'Test comment subject';
  687. $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = '<h1>' . $comment_body . '</h1>';
  688. $full_html_format_id = 'full_html';
  689. $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][format]'] = $full_html_format_id;
  690. $this->drupalPost('comment/reply/' . $node->nid, $edit_comment, t('Save'));
  691. // Invoke search index update.
  692. $this->drupalLogout();
  693. $this->cronRun();
  694. // Search for the comment subject.
  695. $edit = array(
  696. 'search_block_form' => "'" . $edit_comment['subject'] . "'",
  697. );
  698. $this->drupalPost('', $edit, t('Search'));
  699. $this->assertText($node->title, 'Node found in search results.');
  700. $this->assertText($edit_comment['subject'], 'Comment subject found in search results.');
  701. // Search for the comment body.
  702. $edit = array(
  703. 'search_block_form' => "'" . $comment_body . "'",
  704. );
  705. $this->drupalPost('', $edit, t('Search'));
  706. $this->assertText($node->title, 'Node found in search results.');
  707. // Verify that comment is rendered using proper format.
  708. $this->assertText($comment_body, 'Comment body text found in search results.');
  709. $this->assertNoRaw(t('n/a'), 'HTML in comment body is not hidden.');
  710. $this->assertNoRaw(check_plain($edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]']), 'HTML in comment body is not escaped.');
  711. // Hide comments.
  712. $this->drupalLogin($this->admin_user);
  713. $node->comment = 0;
  714. node_save($node);
  715. // Invoke search index update.
  716. $this->drupalLogout();
  717. $this->cronRun();
  718. // Search for $title.
  719. $this->drupalPost('', $edit, t('Search'));
  720. $this->assertNoText($comment_body, 'Comment body text not found in search results.');
  721. }
  722. /**
  723. * Verify access rules for comment indexing with different permissions.
  724. */
  725. function testSearchResultsCommentAccess() {
  726. $comment_body = 'Test comment body';
  727. $this->comment_subject = 'Test comment subject';
  728. $this->admin_role = $this->admin_user->roles;
  729. unset($this->admin_role[DRUPAL_AUTHENTICATED_RID]);
  730. $this->admin_role = key($this->admin_role);
  731. // Create a node.
  732. variable_set('comment_preview_article', DRUPAL_OPTIONAL);
  733. $this->node = $this->drupalCreateNode(array('type' => 'article'));
  734. // Post a comment using 'Full HTML' text format.
  735. $edit_comment = array();
  736. $edit_comment['subject'] = $this->comment_subject;
  737. $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = '<h1>' . $comment_body . '</h1>';
  738. $this->drupalPost('comment/reply/' . $this->node->nid, $edit_comment, t('Save'));
  739. $this->drupalLogout();
  740. $this->setRolePermissions(DRUPAL_ANONYMOUS_RID);
  741. $this->checkCommentAccess('Anon user has search permission but no access comments permission, comments should not be indexed');
  742. $this->setRolePermissions(DRUPAL_ANONYMOUS_RID, TRUE);
  743. $this->checkCommentAccess('Anon user has search permission and access comments permission, comments should be indexed', TRUE);
  744. $this->drupalLogin($this->admin_user);
  745. $this->drupalGet('admin/people/permissions');
  746. // Disable search access for authenticated user to test admin user.
  747. $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, FALSE, FALSE);
  748. $this->setRolePermissions($this->admin_role);
  749. $this->checkCommentAccess('Admin user has search permission but no access comments permission, comments should not be indexed');
  750. $this->setRolePermissions($this->admin_role, TRUE);
  751. $this->checkCommentAccess('Admin user has search permission and access comments permission, comments should be indexed', TRUE);
  752. $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID);
  753. $this->checkCommentAccess('Authenticated user has search permission but no access comments permission, comments should not be indexed');
  754. $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, TRUE);
  755. $this->checkCommentAccess('Authenticated user has search permission and access comments permission, comments should be indexed', TRUE);
  756. // Verify that access comments permission is inherited from the
  757. // authenticated role.
  758. $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, TRUE, FALSE);
  759. $this->setRolePermissions($this->admin_role);
  760. $this->checkCommentAccess('Admin user has search permission and no access comments permission, but comments should be indexed because admin user inherits authenticated user\'s permission to access comments', TRUE);
  761. // Verify that search content permission is inherited from the authenticated
  762. // role.
  763. $this->setRolePermissions(DRUPAL_AUTHENTICATED_RID, TRUE, TRUE);
  764. $this->setRolePermissions($this->admin_role, TRUE, FALSE);
  765. $this->checkCommentAccess('Admin user has access comments permission and no search permission, but comments should be indexed because admin user inherits authenticated user\'s permission to search', TRUE);
  766. }
  767. /**
  768. * Set permissions for role.
  769. */
  770. function setRolePermissions($rid, $access_comments = FALSE, $search_content = TRUE) {
  771. $permissions = array(
  772. 'access comments' => $access_comments,
  773. 'search content' => $search_content,
  774. );
  775. user_role_change_permissions($rid, $permissions);
  776. }
  777. /**
  778. * Update search index and search for comment.
  779. */
  780. function checkCommentAccess($message, $assume_access = FALSE) {
  781. // Invoke search index update.
  782. search_touch_node($this->node->nid);
  783. $this->cronRun();
  784. // Search for the comment subject.
  785. $edit = array(
  786. 'search_block_form' => "'" . $this->comment_subject . "'",
  787. );
  788. $this->drupalPost('', $edit, t('Search'));
  789. $method = $assume_access ? 'assertText' : 'assertNoText';
  790. $verb = $assume_access ? 'found' : 'not found';
  791. $this->{$method}($this->node->title, "Node $verb in search results: " . $message);
  792. $this->{$method}($this->comment_subject, "Comment subject $verb in search results: " . $message);
  793. }
  794. /**
  795. * Verify that 'add new comment' does not appear in search results or index.
  796. */
  797. function testAddNewComment() {
  798. // Create a node with a short body.
  799. $settings = array(
  800. 'type' => 'article',
  801. 'title' => 'short title',
  802. 'body' => array(LANGUAGE_NONE => array(array('value' => 'short body text'))),
  803. );
  804. $user = $this->drupalCreateUser(array('search content', 'create article content', 'access content'));
  805. $this->drupalLogin($user);
  806. $node = $this->drupalCreateNode($settings);
  807. // Verify that if you view the node on its own page, 'add new comment'
  808. // is there.
  809. $this->drupalGet('node/' . $node->nid);
  810. $this->assertText(t('Add new comment'), 'Add new comment appears on node page');
  811. // Run cron to index this page.
  812. $this->drupalLogout();
  813. $this->cronRun();
  814. // Search for 'comment'. Should be no results.
  815. $this->drupalLogin($user);
  816. $this->drupalPost('search/node', array('keys' => 'comment'), t('Search'));
  817. $this->assertText(t('Your search yielded no results'), 'No results searching for the word comment');
  818. // Search for the node title. Should be found, and 'Add new comment' should
  819. // not be part of the search snippet.
  820. $this->drupalPost('search/node', array('keys' => 'short'), t('Search'));
  821. $this->assertText($node->title, 'Search for keyword worked');
  822. $this->assertNoText(t('Add new comment'), 'Add new comment does not appear on search results page');
  823. }
  824. }
  825. /**
  826. * Tests search_expression_insert() and search_expression_extract().
  827. *
  828. * @see http://drupal.org/node/419388 (issue)
  829. */
  830. class SearchExpressionInsertExtractTestCase extends DrupalUnitTestCase {
  831. public static function getInfo() {
  832. return array(
  833. 'name' => 'Search expression insert/extract',
  834. 'description' => 'Tests the functions search_expression_insert() and search_expression_extract()',
  835. 'group' => 'Search',
  836. );
  837. }
  838. function setUp() {
  839. drupal_load('module', 'search');
  840. parent::setUp();
  841. }
  842. /**
  843. * Tests search_expression_insert() and search_expression_extract().
  844. */
  845. function testInsertExtract() {
  846. $base_expression = "mykeyword";
  847. // Build an array of option, value, what should be in the expression, what
  848. // should be retrieved from expression.
  849. $cases = array(
  850. array('foo', 'bar', 'foo:bar', 'bar'), // Normal case.
  851. array('foo', NULL, '', NULL), // Empty value: shouldn't insert.
  852. array('foo', ' ', 'foo:', ''), // Space as value: should insert but retrieve empty string.
  853. array('foo', '', 'foo:', ''), // Empty string as value: should insert but retrieve empty string.
  854. array('foo', '0', 'foo:0', '0'), // String zero as value: should insert.
  855. array('foo', 0, 'foo:0', '0'), // Numeric zero as value: should insert.
  856. );
  857. foreach ($cases as $index => $case) {
  858. $after_insert = search_expression_insert($base_expression, $case[0], $case[1]);
  859. if (empty($case[2])) {
  860. $this->assertEqual($after_insert, $base_expression, "Empty insert does not change expression in case $index");
  861. }
  862. else {
  863. $this->assertEqual($after_insert, $base_expression . ' ' . $case[2], "Insert added correct expression for case $index");
  864. }
  865. $retrieved = search_expression_extract($after_insert, $case[0]);
  866. if (!isset($case[3])) {
  867. $this->assertFalse(isset($retrieved), "Empty retrieval results in unset value in case $index");
  868. }
  869. else {
  870. $this->assertEqual($retrieved, $case[3], "Value is retrieved for case $index");
  871. }
  872. $after_clear = search_expression_insert($after_insert, $case[0]);
  873. $this->assertEqual(trim($after_clear), $base_expression, "After clearing, base expression is restored for case $index");
  874. $cleared = search_expression_extract($after_clear, $case[0]);
  875. $this->assertFalse(isset($cleared), "After clearing, value could not be retrieved for case $index");
  876. }
  877. }
  878. }
  879. /**
  880. * Tests that comment count display toggles properly on comment status of node
  881. *
  882. * Issue 537278
  883. *
  884. * - Nodes with comment status set to Open should always how comment counts
  885. * - Nodes with comment status set to Closed should show comment counts
  886. * only when there are comments
  887. * - Nodes with comment status set to Hidden should never show comment counts
  888. */
  889. class SearchCommentCountToggleTestCase extends DrupalWebTestCase {
  890. protected $searching_user;
  891. protected $searchable_nodes;
  892. public static function getInfo() {
  893. return array(
  894. 'name' => 'Comment count toggle',
  895. 'description' => 'Verify that comment count display toggles properly on comment status of node.',
  896. 'group' => 'Search',
  897. );
  898. }
  899. function setUp() {
  900. parent::setUp('search');
  901. // Create searching user.
  902. $this->searching_user = $this->drupalCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));
  903. // Create initial nodes.
  904. $node_params = array('type' => 'article', 'body' => array(LANGUAGE_NONE => array(array('value' => 'SearchCommentToggleTestCase'))));
  905. $this->searchable_nodes['1 comment'] = $this->drupalCreateNode($node_params);
  906. $this->searchable_nodes['0 comments'] = $this->drupalCreateNode($node_params);
  907. // Login with sufficient privileges.
  908. $this->drupalLogin($this->searching_user);
  909. // Create a comment array
  910. $edit_comment = array();
  911. $edit_comment['subject'] = $this->randomName();
  912. $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName();
  913. $filtered_html_format_id = 'filtered_html';
  914. $edit_comment['comment_body[' . LANGUAGE_NONE . '][0][format]'] = $filtered_html_format_id;
  915. // Post comment to the test node with comment
  916. $this->drupalPost('comment/reply/' . $this->searchable_nodes['1 comment']->nid, $edit_comment, t('Save'));
  917. // First update the index. This does the initial processing.
  918. node_update_index();
  919. // Then, run the shutdown function. Testing is a unique case where indexing
  920. // and searching has to happen in the same request, so running the shutdown
  921. // function manually is needed to finish the indexing process.
  922. search_update_totals();
  923. }
  924. /**
  925. * Verify that comment count display toggles properly on comment status of node
  926. */
  927. function testSearchCommentCountToggle() {
  928. // Search for the nodes by string in the node body.
  929. $edit = array(
  930. 'search_block_form' => "'SearchCommentToggleTestCase'",
  931. );
  932. // Test comment count display for nodes with comment status set to Open
  933. $this->drupalPost('', $edit, t('Search'));
  934. $this->assertText(t('0 comments'), 'Empty comment count displays for nodes with comment status set to Open');
  935. $this->assertText(t('1 comment'), 'Non-empty comment count displays for nodes with comment status set to Open');
  936. // Test comment count display for nodes with comment status set to Closed
  937. $this->searchable_nodes['0 comments']->comment = COMMENT_NODE_CLOSED;
  938. node_save($this->searchable_nodes['0 comments']);
  939. $this->searchable_nodes['1 comment']->comment = COMMENT_NODE_CLOSED;
  940. node_save($this->searchable_nodes['1 comment']);
  941. $this->drupalPost('', $edit, t('Search'));
  942. $this->assertNoText(t('0 comments'), 'Empty comment count does not display for nodes with comment status set to Closed');
  943. $this->assertText(t('1 comment'), 'Non-empty comment count displays for nodes with comment status set to Closed');
  944. // Test comment count display for nodes with comment status set to Hidden
  945. $this->searchable_nodes['0 comments']->comment = COMMENT_NODE_HIDDEN;
  946. node_save($this->searchable_nodes['0 comments']);
  947. $this->searchable_nodes['1 comment']->comment = COMMENT_NODE_HIDDEN;
  948. node_save($this->searchable_nodes['1 comment']);
  949. $this->drupalPost('', $edit, t('Search'));
  950. $this->assertNoText(t('0 comments'), 'Empty comment count does not display for nodes with comment status set to Hidden');
  951. $this->assertNoText(t('1 comment'), 'Non-empty comment count does not display for nodes with comment status set to Hidden');
  952. }
  953. }
  954. /**
  955. * Test search_simplify() on every Unicode character, and some other cases.
  956. */
  957. class SearchSimplifyTestCase extends DrupalWebTestCase {
  958. public static function getInfo() {
  959. return array(
  960. 'name' => 'Search simplify',
  961. 'description' => 'Check that the search_simply() function works as intended.',
  962. 'group' => 'Search',
  963. );
  964. }
  965. /**
  966. * Tests that all Unicode characters simplify correctly.
  967. */
  968. function testSearchSimplifyUnicode() {
  969. // This test uses a file that was constructed so that the even lines are
  970. // boundary characters, and the odd lines are valid word characters. (It
  971. // was generated as a sequence of all the Unicode characters, and then the
  972. // boundary chararacters (punctuation, spaces, etc.) were split off into
  973. // their own lines). So the even-numbered lines should simplify to nothing,
  974. // and the odd-numbered lines we need to split into shorter chunks and
  975. // verify that simplification doesn't lose any characters.
  976. $input = file_get_contents(DRUPAL_ROOT . '/modules/search/tests/UnicodeTest.txt');
  977. $basestrings = explode(chr(10), $input);
  978. $strings = array();
  979. foreach ($basestrings as $key => $string) {
  980. if ($key %2) {
  981. // Even line - should simplify down to a space.
  982. $simplified = search_simplify($string);
  983. $this->assertIdentical($simplified, ' ', "Line $key is excluded from the index");
  984. }
  985. else {
  986. // Odd line, should be word characters.
  987. // Split this into 30-character chunks, so we don't run into limits
  988. // of truncation in search_simplify().
  989. $start = 0;
  990. while ($start < drupal_strlen($string)) {
  991. $newstr = drupal_substr($string, $start, 30);
  992. // Special case: leading zeros are removed from numeric strings,
  993. // and there's one string in this file that is numbers starting with
  994. // zero, so prepend a 1 on that string.
  995. if (preg_match('/^[0-9]+$/', $newstr)) {
  996. $newstr = '1' . $newstr;
  997. }
  998. $strings[] = $newstr;
  999. $start += 30;
  1000. }
  1001. }
  1002. }
  1003. foreach ($strings as $key => $string) {
  1004. $simplified = search_simplify($string);
  1005. $this->assertTrue(drupal_strlen($simplified) >= drupal_strlen($string), "Nothing is removed from string $key.");
  1006. }
  1007. // Test the low-numbered ASCII control characters separately. They are not
  1008. // in the text file because they are problematic for diff, especially \0.
  1009. $string = '';
  1010. for ($i = 0; $i < 32; $i++) {
  1011. $string .= chr($i);
  1012. }
  1013. $this->assertIdentical(' ', search_simplify($string), 'Search simplify works for ASCII control characters.');
  1014. }
  1015. /**
  1016. * Tests that search_simplify() does the right thing with punctuation.
  1017. */
  1018. function testSearchSimplifyPunctuation() {
  1019. $cases = array(
  1020. array('20.03/94-28,876', '20039428876', 'Punctuation removed from numbers'),
  1021. array('great...drupal--module', 'great drupal module', 'Multiple dot and dashes are word boundaries'),
  1022. array('very_great-drupal.module', 'verygreatdrupalmodule', 'Single dot, dash, underscore are removed'),
  1023. array('regular,punctuation;word', 'regular punctuation word', 'Punctuation is a word boundary'),
  1024. );
  1025. foreach ($cases as $case) {
  1026. $out = trim(search_simplify($case[0]));
  1027. $this->assertEqual($out, $case[1], $case[2]);
  1028. }
  1029. }
  1030. }
  1031. /**
  1032. * Tests keywords and conditions.
  1033. */
  1034. class SearchKeywordsConditions extends DrupalWebTestCase {
  1035. public static function getInfo() {
  1036. return array(
  1037. 'name' => 'Keywords and conditions',
  1038. 'description' => 'Verify the search pulls in keywords and extra conditions.',
  1039. 'group' => 'Search',
  1040. );
  1041. }
  1042. function setUp() {
  1043. parent::setUp('search', 'search_extra_type');
  1044. // Create searching user.
  1045. $this->searching_user = $this->drupalCreateUser(array('search content', 'access content', 'access comments', 'skip comment approval'));
  1046. // Login with sufficient privileges.
  1047. $this->drupalLogin($this->searching_user);
  1048. // Test with all search modules enabled.
  1049. variable_set('search_active_modules', array('node' => 'node', 'user' => 'user', 'search_extra_type' => 'search_extra_type'));
  1050. menu_rebuild();
  1051. }
  1052. /**
  1053. * Verify the kewords are captured and conditions respected.
  1054. */
  1055. function testSearchKeyswordsConditions() {
  1056. // No keys, not conditions - no results.
  1057. $this->drupalGet('search/dummy_path');
  1058. $this->assertNoText('Dummy search snippet to display');
  1059. // With keys - get results.
  1060. $keys = 'bike shed ' . $this->randomName();
  1061. $this->drupalGet("search/dummy_path/{$keys}");
  1062. $this->assertText("Dummy search snippet to display. Keywords: {$keys}");
  1063. $keys = 'blue drop ' . $this->randomName();
  1064. $this->drupalGet("search/dummy_path", array('query' => array('keys' => $keys)));
  1065. $this->assertText("Dummy search snippet to display. Keywords: {$keys}");
  1066. // Add some conditions and keys.
  1067. $keys = 'moving drop ' . $this->randomName();
  1068. $this->drupalGet("search/dummy_path/bike", array('query' => array('search_conditions' => $keys)));
  1069. $this->assertText("Dummy search snippet to display.");
  1070. $this->assertRaw(print_r(array('search_conditions' => $keys), TRUE));
  1071. // Add some conditions and no keys.
  1072. $keys = 'drop kick ' . $this->randomName();
  1073. $this->drupalGet("search/dummy_path", array('query' => array('search_conditions' => $keys)));
  1074. $this->assertText("Dummy search snippet to display.");
  1075. $this->assertRaw(print_r(array('search_conditions' => $keys), TRUE));
  1076. }
  1077. }
  1078. /**
  1079. * Tests that numbers can be searched.
  1080. */
  1081. class SearchNumbersTestCase extends DrupalWebTestCase {
  1082. protected $test_user;
  1083. protected $numbers;
  1084. protected $nodes;
  1085. public static function getInfo() {
  1086. return array(
  1087. 'name' => 'Search numbers',
  1088. 'description' => 'Check that numbers can be searched',
  1089. 'group' => 'Search',
  1090. );
  1091. }
  1092. function setUp() {
  1093. parent::setUp('search');
  1094. $this->test_user = $this->drupalCreateUser(array('search content', 'access content', 'administer nodes', 'access site reports'));
  1095. $this->drupalLogin($this->test_user);
  1096. // Create content with various numbers in it.
  1097. // Note: 50 characters is the current limit of the search index's word
  1098. // field.
  1099. $this->numbers = array(
  1100. 'ISBN' => '978-0446365383',
  1101. 'UPC' => '036000 291452',
  1102. 'EAN bar code' => '5901234123457',
  1103. 'negative' => '-123456.7890',
  1104. 'quoted negative' => '"-123456.7890"',
  1105. 'leading zero' => '0777777777',
  1106. 'tiny' => '111',
  1107. 'small' => '22222222222222',
  1108. 'medium' => '333333333333333333333333333',
  1109. 'large' => '444444444444444444444444444444444444444',
  1110. 'gigantic' => '5555555555555555555555555555555555555555555555555',
  1111. 'over fifty characters' => '666666666666666666666666666666666666666666666666666666666666',
  1112. 'date', '01/02/2009',
  1113. 'commas', '987,654,321',
  1114. );
  1115. foreach ($this->numbers as $doc => $num) {
  1116. $info = array(
  1117. 'body' => array(LANGUAGE_NONE => array(array('value' => $num))),
  1118. 'type' => 'page',
  1119. 'language' => LANGUAGE_NONE,
  1120. 'title' => $doc . ' number',
  1121. );
  1122. $this->nodes[$doc] = $this->drupalCreateNode($info);
  1123. }
  1124. // Run cron to ensure the content is indexed.
  1125. $this->cronRun();
  1126. $this->drupalGet('admin/reports/dblog');
  1127. $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
  1128. }
  1129. /**
  1130. * Tests that all the numbers can be searched.
  1131. */
  1132. function testNumberSearching() {
  1133. $types = array_keys($this->numbers);
  1134. foreach ($types as $type) {
  1135. $number = $this->numbers[$type];
  1136. // If the number is negative, remove the - sign, because - indicates
  1137. // "not keyword" when searching.
  1138. $number = ltrim($number, '-');
  1139. $node = $this->nodes[$type];
  1140. // Verify that the node title does not appear on the search page
  1141. // with a dummy search.
  1142. $this->drupalPost('search/node',
  1143. array('keys' => 'foo'),
  1144. t('Search'));
  1145. $this->assertNoText($node->title, $type . ': node title not shown in dummy search');
  1146. // Verify that the node title does appear as a link on the search page
  1147. // when searching for the number.
  1148. $this->drupalPost('search/node',
  1149. array('keys' => $number),
  1150. t('Search'));
  1151. $this->assertText($node->title, format_string('%type: node title shown (search found the node) in search for number %number.', array('%type' => $type, '%number' => $number)));
  1152. }
  1153. }
  1154. }
  1155. /**
  1156. * Tests that numbers can be searched, with more complex matching.
  1157. */
  1158. class SearchNumberMatchingTestCase extends DrupalWebTestCase {
  1159. protected $test_user;
  1160. protected $numbers;
  1161. protected $nodes;
  1162. public static function getInfo() {
  1163. return array(
  1164. 'name' => 'Search number matching',
  1165. 'description' => 'Check that numbers can be searched with more complex matching',
  1166. 'group' => 'Search',
  1167. );
  1168. }
  1169. function setUp() {
  1170. parent::setUp('search');
  1171. $this->test_user = $this->drupalCreateUser(array('search content', 'access content', 'administer nodes', 'access site reports'));
  1172. $this->drupalLogin($this->test_user);
  1173. // Define a group of numbers that should all match each other --
  1174. // numbers with internal punctuation should match each other, as well
  1175. // as numbers with and without leading zeros and leading/trailing
  1176. // . and -.
  1177. $this->numbers = array(
  1178. '123456789',
  1179. '12/34/56789',
  1180. '12.3456789',
  1181. '12-34-56789',
  1182. '123,456,789',
  1183. '-123456789',
  1184. '0123456789',
  1185. );
  1186. foreach ($this->numbers as $num) {
  1187. $info = array(
  1188. 'body' => array(LANGUAGE_NONE => array(array('value' => $num))),
  1189. 'type' => 'page',
  1190. 'language' => LANGUAGE_NONE,
  1191. );
  1192. $this->nodes[] = $this->drupalCreateNode($info);
  1193. }
  1194. // Run cron to ensure the content is indexed.
  1195. $this->cronRun();
  1196. $this->drupalGet('admin/reports/dblog');
  1197. $this->assertText(t('Cron run completed'), 'Log shows cron run completed');
  1198. }
  1199. /**
  1200. * Tests that all the numbers can be searched.
  1201. */
  1202. function testNumberSearching() {
  1203. for ($i = 0; $i < count($this->numbers); $i++) {
  1204. $node = $this->nodes[$i];
  1205. // Verify that the node title does not appear on the search page
  1206. // with a dummy search.
  1207. $this->drupalPost('search/node',
  1208. array('keys' => 'foo'),
  1209. t('Search'));
  1210. $this->assertNoText($node->title, format_string('%number: node title not shown in dummy search', array('%number' => $i)));
  1211. // Now verify that we can find node i by searching for any of the
  1212. // numbers.
  1213. for ($j = 0; $j < count($this->numbers); $j++) {
  1214. $number = $this->numbers[$j];
  1215. // If the number is negative, remove the - sign, because - indicates
  1216. // "not keyword" when searching.
  1217. $number = ltrim($number, '-');
  1218. $this->drupalPost('search/node',
  1219. array('keys' => $number),
  1220. t('Search'));
  1221. $this->assertText($node->title, format_string('%i: node title shown (search found the node) in search for number %number', array('%i' => $i, '%number' => $number)));
  1222. }
  1223. }
  1224. }
  1225. }
  1226. /**
  1227. * Test config page.
  1228. */
  1229. class SearchConfigSettingsForm extends DrupalWebTestCase {
  1230. public $search_user;
  1231. public $search_node;
  1232. public static function getInfo() {
  1233. return array(
  1234. 'name' => 'Config settings form',
  1235. 'description' => 'Verify the search config settings form.',
  1236. 'group' => 'Search',
  1237. );
  1238. }
  1239. function setUp() {
  1240. parent::setUp('search', 'search_extra_type');
  1241. // Login as a user that can create and search content.
  1242. $this->search_user = $this->drupalCreateUser(array('search content', 'administer search', 'administer nodes', 'bypass node access', 'access user profiles', 'administer users', 'administer blocks'));
  1243. $this->drupalLogin($this->search_user);
  1244. // Add a single piece of content and index it.
  1245. $node = $this->drupalCreateNode();
  1246. $this->search_node = $node;
  1247. // Link the node to itself to test that it's only indexed once. The content
  1248. // also needs the word "pizza" so we can use it as the search keyword.
  1249. $langcode = LANGUAGE_NONE;
  1250. $body_key = "body[$langcode][0][value]";
  1251. $edit[$body_key] = l($node->title, 'node/' . $node->nid) . ' pizza sandwich';
  1252. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  1253. node_update_index();
  1254. search_update_totals();
  1255. // Enable the search block.
  1256. $edit = array();
  1257. $edit['blocks[search_form][region]'] = 'content';
  1258. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  1259. }
  1260. /**
  1261. * Verify the search settings form.
  1262. */
  1263. function testSearchSettingsPage() {
  1264. // Test that the settings form displays the correct count of items left to index.
  1265. $this->drupalGet('admin/config/search/settings');
  1266. $this->assertText(t('There are @count items left to index.', array('@count' => 0)));
  1267. // Test the re-index button.
  1268. $this->drupalPost('admin/config/search/settings', array(), t('Re-index site'));
  1269. $this->assertText(t('Are you sure you want to re-index the site'));
  1270. $this->drupalPost('admin/config/search/settings/reindex', array(), t('Re-index site'));
  1271. $this->assertText(t('The index will be rebuilt'));
  1272. $this->drupalGet('admin/config/search/settings');
  1273. $this->assertText(t('There is 1 item left to index.'));
  1274. // Test that the form saves with the default values.
  1275. $this->drupalPost('admin/config/search/settings', array(), t('Save configuration'));
  1276. $this->assertText(t('The configuration options have been saved.'), 'Form saves with the default values.');
  1277. // Test that the form does not save with an invalid word length.
  1278. $edit = array(
  1279. 'minimum_word_size' => $this->randomName(3),
  1280. );
  1281. $this->drupalPost('admin/config/search/settings', $edit, t('Save configuration'));
  1282. $this->assertNoText(t('The configuration options have been saved.'), 'Form does not save with an invalid word length.');
  1283. }
  1284. /**
  1285. * Verify that you can disable individual search modules.
  1286. */
  1287. function testSearchModuleDisabling() {
  1288. // Array of search modules to test: 'path' is the search path, 'title' is
  1289. // the tab title, 'keys' are the keywords to search for, and 'text' is
  1290. // the text to assert is on the results page.
  1291. $module_info = array(
  1292. 'node' => array(
  1293. 'path' => 'node',
  1294. 'title' => 'Content',
  1295. 'keys' => 'pizza',
  1296. 'text' => $this->search_node->title,
  1297. ),
  1298. 'user' => array(
  1299. 'path' => 'user',
  1300. 'title' => 'User',
  1301. 'keys' => $this->search_user->name,
  1302. 'text' => $this->search_user->mail,
  1303. ),
  1304. 'search_extra_type' => array(
  1305. 'path' => 'dummy_path',
  1306. 'title' => 'Dummy search type',
  1307. 'keys' => 'foo',
  1308. 'text' => 'Dummy search snippet to display',
  1309. ),
  1310. );
  1311. $modules = array_keys($module_info);
  1312. // Test each module if it's enabled as the only search module.
  1313. foreach ($modules as $module) {
  1314. // Enable the one module and disable other ones.
  1315. $info = $module_info[$module];
  1316. $edit = array();
  1317. foreach ($modules as $other) {
  1318. $edit['search_active_modules[' . $other . ']'] = (($other == $module) ? $module : FALSE);
  1319. }
  1320. $edit['search_default_module'] = $module;
  1321. $this->drupalPost('admin/config/search/settings', $edit, t('Save configuration'));
  1322. // Run a search from the correct search URL.
  1323. $this->drupalGet('search/' . $info['path'] . '/' . $info['keys']);
  1324. $this->assertNoText('no results', $info['title'] . ' search found results');
  1325. $this->assertText($info['text'], 'Correct search text found');
  1326. // Verify that other module search tab titles are not visible.
  1327. foreach ($modules as $other) {
  1328. if ($other != $module) {
  1329. $title = $module_info[$other]['title'];
  1330. $this->assertNoText($title, $title . ' search tab is not shown');
  1331. }
  1332. }
  1333. // Run a search from the search block on the node page. Verify you get
  1334. // to this module's search results page.
  1335. $terms = array('search_block_form' => $info['keys']);
  1336. $this->drupalPost('node', $terms, t('Search'));
  1337. $this->assertEqual(
  1338. $this->getURL(),
  1339. url('search/' . $info['path'] . '/' . $info['keys'], array('absolute' => TRUE)),
  1340. 'Block redirected to right search page');
  1341. // Try an invalid search path. Should redirect to our active module.
  1342. $this->drupalGet('search/not_a_module_path');
  1343. $this->assertEqual(
  1344. $this->getURL(),
  1345. url('search/' . $info['path'], array('absolute' => TRUE)),
  1346. 'Invalid search path redirected to default search page');
  1347. }
  1348. // Test with all search modules enabled. When you go to the search
  1349. // page or run search, all modules should be shown.
  1350. $edit = array();
  1351. foreach ($modules as $module) {
  1352. $edit['search_active_modules[' . $module . ']'] = $module;
  1353. }
  1354. $edit['search_default_module'] = 'node';
  1355. $this->drupalPost('admin/config/search/settings', $edit, t('Save configuration'));
  1356. foreach (array('search/node/pizza', 'search/node') as $path) {
  1357. $this->drupalGet($path);
  1358. foreach ($modules as $module) {
  1359. $title = $module_info[$module]['title'];
  1360. $this->assertText($title, format_string('%title search tab is shown', array('%title' => $title)));
  1361. }
  1362. }
  1363. }
  1364. }
  1365. /**
  1366. * Tests the search_excerpt() function.
  1367. */
  1368. class SearchExcerptTestCase extends DrupalWebTestCase {
  1369. public static function getInfo() {
  1370. return array(
  1371. 'name' => 'Search excerpt extraction',
  1372. 'description' => 'Tests that the search_excerpt() function works.',
  1373. 'group' => 'Search',
  1374. );
  1375. }
  1376. function setUp() {
  1377. parent::setUp('search');
  1378. }
  1379. /**
  1380. * Tests search_excerpt() with several simulated search keywords.
  1381. *
  1382. * Passes keywords and a sample marked up string, "The quick
  1383. * brown fox jumps over the lazy dog", and compares it to the
  1384. * correctly marked up string. The correctly marked up string
  1385. * contains either highlighted keywords or the original marked
  1386. * up string if no keywords matched the string.
  1387. */
  1388. function testSearchExcerpt() {
  1389. // Make some text with entities and tags.
  1390. $text = 'The <strong>quick</strong> <a href="#">brown</a> fox &amp; jumps <h2>over</h2> the lazy dog';
  1391. // Note: The search_excerpt() function adds some extra spaces -- not
  1392. // important for HTML formatting. Remove these for comparison.
  1393. $expected = 'The quick brown fox &amp; jumps over the lazy dog';
  1394. $result = preg_replace('| +|', ' ', search_excerpt('nothing', $text));
  1395. $this->assertEqual(preg_replace('| +|', ' ', $result), $expected, 'Entire string is returned when keyword is not found in short string');
  1396. $result = preg_replace('| +|', ' ', search_excerpt('fox', $text));
  1397. $this->assertEqual($result, 'The quick brown <strong>fox</strong> &amp; jumps over the lazy dog ...', 'Found keyword is highlighted');
  1398. $longtext = str_repeat($text . ' ', 10);
  1399. $result = preg_replace('| +|', ' ', search_excerpt('nothing', $longtext));
  1400. $this->assertTrue(strpos($result, $expected) === 0, 'When keyword is not found in long string, return value starts as expected');
  1401. $entities = str_repeat('k&eacute;sz&iacute;t&eacute;se ', 20);
  1402. $result = preg_replace('| +|', ' ', search_excerpt('nothing', $entities));
  1403. $this->assertFalse(strpos($result, '&'), 'Entities are not present in excerpt');
  1404. $this->assertTrue(strpos($result, 'í') > 0, 'Entities are converted in excerpt');
  1405. // The node body that will produce this rendered $text is:
  1406. // 123456789 HTMLTest +123456789+&lsquo; +&lsquo; +&lsquo; +&lsquo; +12345678 &nbsp;&nbsp; +&lsquo; +&lsquo; +&lsquo; &lsquo;
  1407. $text = "<div class=\"field field-name-body field-type-text-with-summary field-label-hidden\"><div class=\"field-items\"><div class=\"field-item even\" property=\"content:encoded\"><p>123456789 HTMLTest +123456789+‘ +‘ +‘ +‘ +12345678    +‘ +‘ +‘ ‘</p>\n</div></div></div> ";
  1408. $result = search_excerpt('HTMLTest', $text);
  1409. $this->assertFalse(empty($result), 'Rendered Multi-byte HTML encodings are not corrupted in search excerpts');
  1410. }
  1411. /**
  1412. * Tests search_excerpt() with search keywords matching simplified words.
  1413. *
  1414. * Excerpting should handle keywords that are matched only after going through
  1415. * search_simplify(). This test passes keywords that match simplified words
  1416. * and compares them with strings that contain the original unsimplified word.
  1417. */
  1418. function testSearchExcerptSimplified() {
  1419. $lorem1 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae arcu at leo cursus laoreet. Curabitur dui tortor, adipiscing malesuada tempor in, bibendum ac diam. Cras non tellus a libero pellentesque condimentum. What is a Drupalism? Suspendisse ac lacus libero. Ut non est vel nisl faucibus interdum nec sed leo. Pellentesque sem risus, vulputate eu semper eget, auctor in libero.';
  1420. $lorem2 = 'Ut fermentum est vitae metus convallis scelerisque. Phasellus pellentesque rhoncus tellus, eu dignissim purus posuere id. Quisque eu fringilla ligula. Morbi ullamcorper, lorem et mattis egestas, tortor neque pretium velit, eget eleifend odio turpis eu purus. Donec vitae metus quis leo pretium tincidunt a pulvinar sem. Morbi adipiscing laoreet mauris vel placerat. Nullam elementum, nisl sit amet scelerisque malesuada, dolor nunc hendrerit quam, eu ultrices erat est in orci.';
  1421. // Make some text with some keywords that will get simplified.
  1422. $text = $lorem1 . ' Number: 123456.7890 Hyphenated: one-two abc,def ' . $lorem2;
  1423. // Note: The search_excerpt() function adds some extra spaces -- not
  1424. // important for HTML formatting. Remove these for comparison.
  1425. $result = preg_replace('| +|', ' ', search_excerpt('123456.7890', $text));
  1426. $this->assertTrue(strpos($result, 'Number: <strong>123456.7890</strong>') !== FALSE, 'Numeric keyword is highlighted with exact match');
  1427. $result = preg_replace('| +|', ' ', search_excerpt('1234567890', $text));
  1428. $this->assertTrue(strpos($result, 'Number: <strong>123456.7890</strong>') !== FALSE, 'Numeric keyword is highlighted with simplified match');
  1429. $result = preg_replace('| +|', ' ', search_excerpt('Number 1234567890', $text));
  1430. $this->assertTrue(strpos($result, '<strong>Number</strong>: <strong>123456.7890</strong>') !== FALSE, 'Punctuated and numeric keyword is highlighted with simplified match');
  1431. $result = preg_replace('| +|', ' ', search_excerpt('"Number 1234567890"', $text));
  1432. $this->assertTrue(strpos($result, '<strong>Number: 123456.7890</strong>') !== FALSE, 'Phrase with punctuated and numeric keyword is highlighted with simplified match');
  1433. $result = preg_replace('| +|', ' ', search_excerpt('"Hyphenated onetwo"', $text));
  1434. $this->assertTrue(strpos($result, '<strong>Hyphenated: one-two</strong>') !== FALSE, 'Phrase with punctuated and hyphenated keyword is highlighted with simplified match');
  1435. $result = preg_replace('| +|', ' ', search_excerpt('"abc def"', $text));
  1436. $this->assertTrue(strpos($result, '<strong>abc,def</strong>') !== FALSE, 'Phrase with keyword simplified into two separate words is highlighted with simplified match');
  1437. // Test phrases with characters which are being truncated.
  1438. $result = preg_replace('| +|', ' ', search_excerpt('"ipsum _"', $text));
  1439. $this->assertTrue(strpos($result, '<strong>ipsum </strong>') !== FALSE, 'Only valid part of the phrase is highlighted and invalid part containing "_" is ignored.');
  1440. $result = preg_replace('| +|', ' ', search_excerpt('"ipsum 0000"', $text));
  1441. $this->assertTrue(strpos($result, '<strong>ipsum </strong>') !== FALSE, 'Only valid part of the phrase is highlighted and invalid part "0000" is ignored.');
  1442. // Test combination of the valid keyword and keyword containing only
  1443. // characters which are being truncated during simplification.
  1444. $result = preg_replace('| +|', ' ', search_excerpt('ipsum _', $text));
  1445. $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid keyword is highlighted and invalid keyword "_" is ignored.');
  1446. $result = preg_replace('| +|', ' ', search_excerpt('ipsum 0000', $text));
  1447. $this->assertTrue(strpos($result, '<strong>ipsum</strong>') !== FALSE, 'Only valid keyword is highlighted and invalid keyword "0000" is ignored.');
  1448. }
  1449. }
  1450. /**
  1451. * Test the CJK tokenizer.
  1452. */
  1453. class SearchTokenizerTestCase extends DrupalWebTestCase {
  1454. public static function getInfo() {
  1455. return array(
  1456. 'name' => 'CJK tokenizer',
  1457. 'description' => 'Check that CJK tokenizer works as intended.',
  1458. 'group' => 'Search',
  1459. );
  1460. }
  1461. function setUp() {
  1462. parent::setUp('search');
  1463. }
  1464. /**
  1465. * Verifies that strings of CJK characters are tokenized.
  1466. *
  1467. * The search_simplify() function does special things with numbers, symbols,
  1468. * and punctuation. So we only test that CJK characters that are not in these
  1469. * character classes are tokenized properly. See PREG_CLASS_CKJ for more
  1470. * information.
  1471. */
  1472. function testTokenizer() {
  1473. // Set the minimum word size to 1 (to split all CJK characters) and make
  1474. // sure CJK tokenizing is turned on.
  1475. variable_set('minimum_word_size', 1);
  1476. variable_set('overlap_cjk', TRUE);
  1477. $this->refreshVariables();
  1478. // Create a string of CJK characters from various character ranges in
  1479. // the Unicode tables.
  1480. // Beginnings of the character ranges.
  1481. $starts = array(
  1482. 'CJK unified' => 0x4e00,
  1483. 'CJK Ext A' => 0x3400,
  1484. 'CJK Compat' => 0xf900,
  1485. 'Hangul Jamo' => 0x1100,
  1486. 'Hangul Ext A' => 0xa960,
  1487. 'Hangul Ext B' => 0xd7b0,
  1488. 'Hangul Compat' => 0x3131,
  1489. 'Half non-punct 1' => 0xff21,
  1490. 'Half non-punct 2' => 0xff41,
  1491. 'Half non-punct 3' => 0xff66,
  1492. 'Hangul Syllables' => 0xac00,
  1493. 'Hiragana' => 0x3040,
  1494. 'Katakana' => 0x30a1,
  1495. 'Katakana Ext' => 0x31f0,
  1496. 'CJK Reserve 1' => 0x20000,
  1497. 'CJK Reserve 2' => 0x30000,
  1498. 'Bomofo' => 0x3100,
  1499. 'Bomofo Ext' => 0x31a0,
  1500. 'Lisu' => 0xa4d0,
  1501. 'Yi' => 0xa000,
  1502. );
  1503. // Ends of the character ranges.
  1504. $ends = array(
  1505. 'CJK unified' => 0x9fcf,
  1506. 'CJK Ext A' => 0x4dbf,
  1507. 'CJK Compat' => 0xfaff,
  1508. 'Hangul Jamo' => 0x11ff,
  1509. 'Hangul Ext A' => 0xa97f,
  1510. 'Hangul Ext B' => 0xd7ff,
  1511. 'Hangul Compat' => 0x318e,
  1512. 'Half non-punct 1' => 0xff3a,
  1513. 'Half non-punct 2' => 0xff5a,
  1514. 'Half non-punct 3' => 0xffdc,
  1515. 'Hangul Syllables' => 0xd7af,
  1516. 'Hiragana' => 0x309f,
  1517. 'Katakana' => 0x30ff,
  1518. 'Katakana Ext' => 0x31ff,
  1519. 'CJK Reserve 1' => 0x2fffd,
  1520. 'CJK Reserve 2' => 0x3fffd,
  1521. 'Bomofo' => 0x312f,
  1522. 'Bomofo Ext' => 0x31b7,
  1523. 'Lisu' => 0xa4fd,
  1524. 'Yi' => 0xa48f,
  1525. );
  1526. // Generate characters consisting of starts, midpoints, and ends.
  1527. $chars = array();
  1528. $charcodes = array();
  1529. foreach ($starts as $key => $value) {
  1530. $charcodes[] = $starts[$key];
  1531. $chars[] = $this->code2utf($starts[$key]);
  1532. $mid = round(0.5 * ($starts[$key] + $ends[$key]));
  1533. $charcodes[] = $mid;
  1534. $chars[] = $this->code2utf($mid);
  1535. $charcodes[] = $ends[$key];
  1536. $chars[] = $this->code2utf($ends[$key]);
  1537. }
  1538. // Merge into a string and tokenize.
  1539. $string = implode('', $chars);
  1540. $out = trim(search_simplify($string));
  1541. $expected = drupal_strtolower(implode(' ', $chars));
  1542. // Verify that the output matches what we expect.
  1543. $this->assertEqual($out, $expected, 'CJK tokenizer worked on all supplied CJK characters');
  1544. }
  1545. /**
  1546. * Verifies that strings of non-CJK characters are not tokenized.
  1547. *
  1548. * This is just a sanity check - it verifies that strings of letters are
  1549. * not tokenized.
  1550. */
  1551. function testNoTokenizer() {
  1552. // Set the minimum word size to 1 (to split all CJK characters) and make
  1553. // sure CJK tokenizing is turned on.
  1554. variable_set('minimum_word_size', 1);
  1555. variable_set('overlap_cjk', TRUE);
  1556. $this->refreshVariables();
  1557. $letters = 'abcdefghijklmnopqrstuvwxyz';
  1558. $out = trim(search_simplify($letters));
  1559. $this->assertEqual($letters, $out, 'Letters are not CJK tokenized');
  1560. }
  1561. /**
  1562. * Like PHP chr() function, but for unicode characters.
  1563. *
  1564. * chr() only works for ASCII characters up to character 255. This function
  1565. * converts a number to the corresponding unicode character. Adapted from
  1566. * functions supplied in comments on several functions on php.net.
  1567. */
  1568. function code2utf($num) {
  1569. if ($num < 128) {
  1570. return chr($num);
  1571. }
  1572. if ($num < 2048) {
  1573. return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
  1574. }
  1575. if ($num < 65536) {
  1576. return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  1577. }
  1578. if ($num < 2097152) {
  1579. return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
  1580. }
  1581. return '';
  1582. }
  1583. }
  1584. /**
  1585. * Tests that we can embed a form in search results and submit it.
  1586. */
  1587. class SearchEmbedForm extends DrupalWebTestCase {
  1588. /**
  1589. * Node used for testing.
  1590. */
  1591. public $node;
  1592. /**
  1593. * Count of how many times the form has been submitted.
  1594. */
  1595. public $submit_count = 0;
  1596. public static function getInfo() {
  1597. return array(
  1598. 'name' => 'Embedded forms',
  1599. 'description' => 'Verifies that a form embedded in search results works',
  1600. 'group' => 'Search',
  1601. );
  1602. }
  1603. function setUp() {
  1604. parent::setUp('search', 'search_embedded_form');
  1605. // Create a user and a node, and update the search index.
  1606. $test_user = $this->drupalCreateUser(array('access content', 'search content', 'administer nodes'));
  1607. $this->drupalLogin($test_user);
  1608. $this->node = $this->drupalCreateNode();
  1609. node_update_index();
  1610. search_update_totals();
  1611. // Set up a dummy initial count of times the form has been submitted.
  1612. $this->submit_count = 12;
  1613. variable_set('search_embedded_form_submitted', $this->submit_count);
  1614. $this->refreshVariables();
  1615. }
  1616. /**
  1617. * Tests that the embedded form appears and can be submitted.
  1618. */
  1619. function testEmbeddedForm() {
  1620. // First verify we can submit the form from the module's page.
  1621. $this->drupalPost('search_embedded_form',
  1622. array('name' => 'John'),
  1623. t('Send away'));
  1624. $this->assertText(t('Test form was submitted'), 'Form message appears');
  1625. $count = variable_get('search_embedded_form_submitted', 0);
  1626. $this->assertEqual($this->submit_count + 1, $count, 'Form submission count is correct');
  1627. $this->submit_count = $count;
  1628. // Now verify that we can see and submit the form from the search results.
  1629. $this->drupalGet('search/node/' . $this->node->title);
  1630. $this->assertText(t('Your name'), 'Form is visible');
  1631. $this->drupalPost('search/node/' . $this->node->title,
  1632. array('name' => 'John'),
  1633. t('Send away'));
  1634. $this->assertText(t('Test form was submitted'), 'Form message appears');
  1635. $count = variable_get('search_embedded_form_submitted', 0);
  1636. $this->assertEqual($this->submit_count + 1, $count, 'Form submission count is correct');
  1637. $this->submit_count = $count;
  1638. // Now verify that if we submit the search form, it doesn't count as
  1639. // our form being submitted.
  1640. $this->drupalPost('search',
  1641. array('keys' => 'foo'),
  1642. t('Search'));
  1643. $this->assertNoText(t('Test form was submitted'), 'Form message does not appear');
  1644. $count = variable_get('search_embedded_form_submitted', 0);
  1645. $this->assertEqual($this->submit_count, $count, 'Form submission count is correct');
  1646. $this->submit_count = $count;
  1647. }
  1648. }
  1649. /**
  1650. * Tests that hook_search_page runs.
  1651. */
  1652. class SearchPageOverride extends DrupalWebTestCase {
  1653. public $search_user;
  1654. public static function getInfo() {
  1655. return array(
  1656. 'name' => 'Search page override',
  1657. 'description' => 'Verify that hook_search_page can override search page display.',
  1658. 'group' => 'Search',
  1659. );
  1660. }
  1661. function setUp() {
  1662. parent::setUp('search', 'search_extra_type');
  1663. // Login as a user that can create and search content.
  1664. $this->search_user = $this->drupalCreateUser(array('search content', 'administer search'));
  1665. $this->drupalLogin($this->search_user);
  1666. // Enable the extra type module for searching.
  1667. variable_set('search_active_modules', array('node' => 'node', 'user' => 'user', 'search_extra_type' => 'search_extra_type'));
  1668. menu_rebuild();
  1669. }
  1670. function testSearchPageHook() {
  1671. $keys = 'bike shed ' . $this->randomName();
  1672. $this->drupalGet("search/dummy_path/{$keys}");
  1673. $this->assertText('Dummy search snippet', 'Dummy search snippet is shown');
  1674. $this->assertText('Test page text is here', 'Page override is working');
  1675. }
  1676. }
  1677. /**
  1678. * Test node search with multiple languages.
  1679. */
  1680. class SearchLanguageTestCase extends DrupalWebTestCase {
  1681. public static function getInfo() {
  1682. return array(
  1683. 'name' => 'Search language selection',
  1684. 'description' => 'Tests advanced search with different languages enabled.',
  1685. 'group' => 'Search',
  1686. );
  1687. }
  1688. /**
  1689. * Implementation setUp().
  1690. */
  1691. function setUp() {
  1692. parent::setUp('search', 'locale');
  1693. // Create and login user.
  1694. $test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search', 'administer nodes', 'administer languages', 'access administration pages'));
  1695. $this->drupalLogin($test_user);
  1696. }
  1697. function testLanguages() {
  1698. // Check that there are initially no languages displayed.
  1699. $this->drupalGet('search/node');
  1700. $this->assertNoText(t('Languages'), 'No languages to choose from.');
  1701. // Add predefined language.
  1702. $edit = array('langcode' => 'fr');
  1703. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  1704. $this->assertText('fr', 'Language added successfully.');
  1705. // Now we should have languages displayed.
  1706. $this->drupalGet('search/node');
  1707. $this->assertText(t('Languages'), 'Languages displayed to choose from.');
  1708. $this->assertText(t('English'), 'English is a possible choice.');
  1709. $this->assertText(t('French'), 'French is a possible choice.');
  1710. // Ensure selecting no language does not make the query different.
  1711. $this->drupalPost('search/node', array(), t('Advanced search'));
  1712. $this->assertEqual($this->getUrl(), url('search/node/', array('absolute' => TRUE)), 'Correct page redirection, no language filtering.');
  1713. // Pick French and ensure it is selected.
  1714. $edit = array('language[fr]' => TRUE);
  1715. $this->drupalPost('search/node', $edit, t('Advanced search'));
  1716. $this->assertFieldByXPath('//input[@name="keys"]', 'language:fr', 'Language filter added to query.');
  1717. // Change the default language and disable English.
  1718. $path = 'admin/config/regional/language';
  1719. $this->drupalGet($path);
  1720. $this->assertFieldChecked('edit-site-default-en', 'English is the default language.');
  1721. $edit = array('site_default' => 'fr');
  1722. $this->drupalPost(NULL, $edit, t('Save configuration'));
  1723. $this->assertNoFieldChecked('edit-site-default-en', 'Default language updated.');
  1724. $edit = array('enabled[en]' => FALSE);
  1725. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  1726. $this->assertNoFieldChecked('edit-enabled-en', 'Language disabled.');
  1727. // Check that there are again no languages displayed.
  1728. $this->drupalGet('search/node');
  1729. $this->assertNoText(t('Languages'), 'No languages to choose from.');
  1730. }
  1731. }
  1732. /**
  1733. * Tests node search with node access control.
  1734. */
  1735. class SearchNodeAccessTest extends DrupalWebTestCase {
  1736. public $test_user;
  1737. public static function getInfo() {
  1738. return array(
  1739. 'name' => 'Search and node access',
  1740. 'description' => 'Tests search functionality with node access control.',
  1741. 'group' => 'Search',
  1742. );
  1743. }
  1744. function setUp() {
  1745. parent::setUp('search', 'node_access_test');
  1746. node_access_rebuild();
  1747. // Create a test user and log in.
  1748. $this->test_user = $this->drupalCreateUser(array('access content', 'search content', 'use advanced search'));
  1749. $this->drupalLogin($this->test_user);
  1750. }
  1751. /**
  1752. * Tests that search returns results with punctuation in the search phrase.
  1753. */
  1754. function testPhraseSearchPunctuation() {
  1755. $node = $this->drupalCreateNode(array('body' => array(LANGUAGE_NONE => array(array('value' => "The bunny's ears were fuzzy.")))));
  1756. // Update the search index.
  1757. module_invoke_all('update_index');
  1758. search_update_totals();
  1759. // Refresh variables after the treatment.
  1760. $this->refreshVariables();
  1761. // Submit a phrase wrapped in double quotes to include the punctuation.
  1762. $edit = array('keys' => '"bunny\'s"');
  1763. $this->drupalPost('search/node', $edit, t('Search'));
  1764. $this->assertText($node->title);
  1765. }
  1766. }
  1767. /**
  1768. * Tests searching with locale values set.
  1769. */
  1770. class SearchSetLocaleTest extends DrupalWebTestCase {
  1771. public static function getInfo() {
  1772. return array(
  1773. 'name' => 'Search with numeric locale set',
  1774. 'description' => 'Check that search works with numeric locale settings',
  1775. 'group' => 'Search',
  1776. );
  1777. }
  1778. function setUp() {
  1779. parent::setUp('search');
  1780. // Create a simple node so something will be put in the index.
  1781. $info = array(
  1782. 'body' => array(LANGUAGE_NONE => array(array('value' => 'Tapir'))),
  1783. );
  1784. $this->drupalCreateNode($info);
  1785. // Run cron to index.
  1786. $this->cronRun();
  1787. }
  1788. /**
  1789. * Verify that search works with a numeric locale set.
  1790. */
  1791. public function testSearchWithNumericLocale() {
  1792. // French decimal point is comma.
  1793. setlocale(LC_NUMERIC, 'fr_FR');
  1794. // An exception will be thrown if a float in the wrong format occurs in the
  1795. // query to the database, so an assertion is not necessary here.
  1796. db_select('search_index', 'i')
  1797. ->extend('searchquery')
  1798. ->searchexpression('tapir', 'node')
  1799. ->execute();
  1800. }
  1801. }