PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 268 lines | 187 code | 29 blank | 52 comment | 4 complexity | c1ba9ebfe1a7eaae80d61d6e4bfde302 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\search\Tests\SearchRankingTest.
  5. */
  6. namespace Drupal\search\Tests;
  7. /**
  8. * Indexes content and tests ranking factors.
  9. */
  10. class SearchRankingTest extends SearchTestBase {
  11. /**
  12. * The node search page.
  13. *
  14. * @var \Drupal\search\SearchPageInterface
  15. */
  16. protected $nodeSearch;
  17. /**
  18. * Modules to enable.
  19. *
  20. * @var array
  21. */
  22. public static $modules = array('statistics', 'comment');
  23. public static function getInfo() {
  24. return array(
  25. 'name' => 'Search engine ranking',
  26. 'description' => 'Indexes content and tests ranking factors.',
  27. 'group' => 'Search',
  28. );
  29. }
  30. public function setUp() {
  31. parent::setUp();
  32. // Create a plugin instance.
  33. $this->nodeSearch = entity_load('search_page', 'node_search');
  34. }
  35. public function testRankings() {
  36. // Login with sufficient privileges.
  37. $this->drupalLogin($this->drupalCreateUser(array('post comments', 'skip comment approval', 'create page content', 'administer search')));
  38. // Add a comment field.
  39. $this->container->get('comment.manager')->addDefaultField('node', 'page');
  40. // Build a list of the rankings to test.
  41. $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views');
  42. // Create nodes for testing.
  43. $nodes = array();
  44. foreach ($node_ranks as $node_rank) {
  45. $settings = array(
  46. 'type' => 'page',
  47. 'comment' => array(array(
  48. 'status' => COMMENT_HIDDEN,
  49. )),
  50. 'title' => 'Drupal rocks',
  51. 'body' => array(array('value' => "Drupal's search rocks")),
  52. );
  53. foreach (array(0, 1) as $num) {
  54. if ($num == 1) {
  55. switch ($node_rank) {
  56. case 'sticky':
  57. case 'promote':
  58. $settings[$node_rank] = 1;
  59. break;
  60. case 'relevance':
  61. $settings['body'][0]['value'] .= " really rocks";
  62. break;
  63. case 'recent':
  64. $settings['created'] = REQUEST_TIME + 3600;
  65. break;
  66. case 'comments':
  67. $settings['comment'][0]['status'] = COMMENT_OPEN;
  68. break;
  69. }
  70. }
  71. $nodes[$node_rank][$num] = $this->drupalCreateNode($settings);
  72. }
  73. }
  74. // Add a comment to one of the nodes.
  75. $edit = array();
  76. $edit['subject'] = 'my comment title';
  77. $edit['comment_body[0][value]'] = 'some random comment';
  78. $this->drupalGet('comment/reply/node/' . $nodes['comments'][1]->id() . '/comment');
  79. $this->drupalPostForm(NULL, $edit, t('Preview'));
  80. $this->drupalPostForm(NULL, $edit, t('Save'));
  81. // Enable counting of statistics.
  82. \Drupal::config('statistics.settings')->set('count_content_views', 1)->save();
  83. // Simulating content views is kind of difficult in the test. Leave that
  84. // to the Statistics module. So instead go ahead and manually update the
  85. // counter for this node.
  86. $nid = $nodes['views'][1]->id();
  87. db_insert('node_counter')
  88. ->fields(array('totalcount' => 5, 'daycount' => 5, 'timestamp' => REQUEST_TIME, 'nid' => $nid))
  89. ->execute();
  90. // Run cron to update the search index and comment/statistics totals.
  91. $this->cronRun();
  92. // Test that the settings form displays the context ranking section.
  93. $this->drupalGet('admin/config/search/settings/manage/node_search');
  94. $this->assertText(t('Content ranking'));
  95. // Check that all rankings are visible and set to 0.
  96. foreach ($node_ranks as $node_rank) {
  97. $this->assertTrue($this->xpath('//select[@id="edit-rankings-' . $node_rank . '"]//option[@value="0"]'), 'Select list to prioritize ' . $node_rank . ' for node ranks is visible and set to 0.');
  98. }
  99. // Test each of the possible rankings.
  100. $edit = array();
  101. foreach ($node_ranks as $node_rank) {
  102. // Enable the ranking we are testing.
  103. $edit['rankings_' . $node_rank] = 10;
  104. $this->drupalPostForm('admin/config/search/settings/manage/node_search', $edit, t('Save search page'));
  105. $this->drupalGet('admin/config/search/settings/manage/node_search');
  106. $this->assertTrue($this->xpath('//select[@id="edit-rankings-' . $node_rank . '"]//option[@value="10"]'), 'Select list to prioritize ' . $node_rank . ' for node ranks is visible and set to 10.');
  107. // Reload the plugin to get the up-to-date values.
  108. $this->nodeSearch = entity_load('search_page', 'node_search');
  109. // Do the search and assert the results.
  110. $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array());
  111. $set = $this->nodeSearch->getPlugin()->execute();
  112. $this->assertEqual($set[0]['node']->id(), $nodes[$node_rank][1]->id(), 'Search ranking "' . $node_rank . '" order.');
  113. // Clear this ranking for the next test.
  114. $edit['rankings_' . $node_rank] = 0;
  115. }
  116. // Save the final node_rank change then check that all rankings are visible
  117. // and have been set back to 0.
  118. $this->drupalPostForm('admin/config/search/settings/manage/node_search', $edit, t('Save search page'));
  119. $this->drupalGet('admin/config/search/settings/manage/node_search');
  120. foreach ($node_ranks as $node_rank) {
  121. $this->assertTrue($this->xpath('//select[@id="edit-rankings-' . $node_rank . '"]//option[@value="0"]'), 'Select list to prioritize ' . $node_rank . ' for node ranks is visible and set to 0.');
  122. }
  123. }
  124. /**
  125. * Test rankings of HTML tags.
  126. */
  127. public function testHTMLRankings() {
  128. $full_html_format = entity_create('filter_format', array(
  129. 'format' => 'full_html',
  130. 'name' => 'Full HTML',
  131. ));
  132. $full_html_format->save();
  133. // Login with sufficient privileges.
  134. $this->drupalLogin($this->drupalCreateUser(array('create page content')));
  135. // Test HTML tags with different weights.
  136. $sorted_tags = array('h1', 'h2', 'h3', 'h4', 'a', 'h5', 'h6', 'notag');
  137. $shuffled_tags = $sorted_tags;
  138. // Shuffle tags to ensure HTML tags are ranked properly.
  139. shuffle($shuffled_tags);
  140. $settings = array(
  141. 'type' => 'page',
  142. 'title' => 'Simple node',
  143. );
  144. $nodes = array();
  145. foreach ($shuffled_tags as $tag) {
  146. switch ($tag) {
  147. case 'a':
  148. $settings['body'] = array(array('value' => l('Drupal Rocks', 'node'), 'format' => 'full_html'));
  149. break;
  150. case 'notag':
  151. $settings['body'] = array(array('value' => 'Drupal Rocks'));
  152. break;
  153. default:
  154. $settings['body'] = array(array('value' => "<$tag>Drupal Rocks</$tag>", 'format' => 'full_html'));
  155. break;
  156. }
  157. $nodes[$tag] = $this->drupalCreateNode($settings);
  158. }
  159. // Update the search index.
  160. $this->nodeSearch->getPlugin()->updateIndex();
  161. search_update_totals();
  162. $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array());
  163. // Do the search and assert the results.
  164. $set = $this->nodeSearch->getPlugin()->execute();
  165. // Test the ranking of each tag.
  166. foreach ($sorted_tags as $tag_rank => $tag) {
  167. // Assert the results.
  168. if ($tag == 'notag') {
  169. $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for plain text order.');
  170. } else {
  171. $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for "&lt;' . $sorted_tags[$tag_rank] . '&gt;" order.');
  172. }
  173. }
  174. // Test tags with the same weight against the sorted tags.
  175. $unsorted_tags = array('u', 'b', 'i', 'strong', 'em');
  176. foreach ($unsorted_tags as $tag) {
  177. $settings['body'] = array(array('value' => "<$tag>Drupal Rocks</$tag>", 'format' => 'full_html'));
  178. $node = $this->drupalCreateNode($settings);
  179. // Update the search index.
  180. $this->nodeSearch->getPlugin()->updateIndex();
  181. search_update_totals();
  182. $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array());
  183. // Do the search and assert the results.
  184. $set = $this->nodeSearch->getPlugin()->execute();
  185. // Ranking should always be second to last.
  186. $set = array_slice($set, -2, 1);
  187. // Assert the results.
  188. $this->assertEqual($set[0]['node']->id(), $node->id(), 'Search tag ranking for "&lt;' . $tag . '&gt;" order.');
  189. // Delete node so it doesn't show up in subsequent search results.
  190. $node->delete();
  191. }
  192. }
  193. /**
  194. * Verifies that if we combine two rankings, search still works.
  195. *
  196. * See issue http://drupal.org/node/771596
  197. */
  198. function testDoubleRankings() {
  199. // Login with sufficient privileges.
  200. $this->drupalLogin($this->drupalCreateUser(array('skip comment approval', 'create page content')));
  201. // Create two nodes that will match the search, one that is sticky.
  202. $settings = array(
  203. 'type' => 'page',
  204. 'title' => 'Drupal rocks',
  205. 'body' => array(array('value' => "Drupal's search rocks")),
  206. );
  207. $this->drupalCreateNode($settings);
  208. $settings['sticky'] = 1;
  209. $node = $this->drupalCreateNode($settings);
  210. // Update the search index.
  211. $this->nodeSearch->getPlugin()->updateIndex();
  212. search_update_totals();
  213. // Set up for ranking sticky and lots of comments; make sure others are
  214. // disabled.
  215. $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views');
  216. $configuration = $this->nodeSearch->getPlugin()->getConfiguration();
  217. foreach ($node_ranks as $var) {
  218. $value = ($var == 'sticky' || $var == 'comments') ? 10 : 0;
  219. $configuration['rankings'][$var] = $value;
  220. }
  221. $this->nodeSearch->getPlugin()->setConfiguration($configuration);
  222. $this->nodeSearch->save();
  223. // Do the search and assert the results.
  224. $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array());
  225. // Do the search and assert the results.
  226. $set = $this->nodeSearch->getPlugin()->execute();
  227. $this->assertEqual($set[0]['node']->id(), $node->id(), 'Search double ranking order.');
  228. }
  229. }