3 namespace Drupal\Tests\search\Functional;
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\comment\Tests\CommentTestTrait;
8 use Drupal\filter\Entity\FilterFormat;
9 use Drupal\search\Entity\SearchPage;
10 use Drupal\Tests\BrowserTestBase;
11 use Drupal\Tests\Traits\Core\CronRunTrait;
14 * Indexes content and tests ranking factors.
18 class SearchRankingTest extends BrowserTestBase {
24 * The node search page.
26 * @var \Drupal\search\SearchPageInterface
28 protected $nodeSearch;
33 protected static $modules = ['node', 'search', 'statistics', 'comment'];
35 protected function setUp() {
38 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
40 // Create a plugin instance.
41 $this->nodeSearch = SearchPage::load('node_search');
43 // Log in with sufficient privileges.
44 $this->drupalLogin($this->drupalCreateUser(['post comments', 'skip comment approval', 'create page content', 'administer search']));
47 public function testRankings() {
48 // Add a comment field.
49 $this->addDefaultCommentField('node', 'page');
51 // Build a list of the rankings to test.
52 $node_ranks = ['sticky', 'promote', 'relevance', 'recent', 'comments', 'views'];
54 // Create nodes for testing.
56 foreach ($node_ranks as $node_rank) {
60 ['status' => CommentItemInterface::HIDDEN],
62 'title' => 'Drupal rocks',
63 'body' => [['value' => "Drupal's search rocks"]],
64 // Node is one day old.
65 'created' => REQUEST_TIME - 24 * 3600,
69 foreach ([0, 1] as $num) {
74 $settings[$node_rank] = 1;
77 $settings['body'][0]['value'] .= " really rocks";
80 // Node is 1 hour hold.
81 $settings['created'] = REQUEST_TIME - 3600;
84 $settings['comment'][0]['status'] = CommentItemInterface::OPEN;
88 $nodes[$node_rank][$num] = $this->drupalCreateNode($settings);
92 // Add a comment to one of the nodes.
94 $edit['subject[0][value]'] = 'my comment title';
95 $edit['comment_body[0][value]'] = 'some random comment';
96 $this->drupalGet('comment/reply/node/' . $nodes['comments'][1]->id() . '/comment');
97 $this->drupalPostForm(NULL, $edit, t('Preview'));
98 $this->drupalPostForm(NULL, $edit, t('Save'));
100 // Enable counting of statistics.
101 $this->config('statistics.settings')->set('count_content_views', 1)->save();
103 // Simulating content views is kind of difficult in the test. Leave that
104 // to the Statistics module. So instead go ahead and manually update the
105 // counter for this node.
106 $nid = $nodes['views'][1]->id();
107 db_insert('node_counter')
108 ->fields(['totalcount' => 5, 'daycount' => 5, 'timestamp' => REQUEST_TIME, 'nid' => $nid])
111 // Run cron to update the search index and comment/statistics totals.
114 // Test that the settings form displays the content ranking section.
115 $this->drupalGet('admin/config/search/pages/manage/node_search');
116 $this->assertText(t('Content ranking'));
118 // Check that all rankings are visible and set to 0.
119 foreach ($node_ranks as $node_rank) {
120 $this->assertTrue($this->xpath('//select[@id="edit-rankings-' . $node_rank . '-value"]//option[@value="0"]'), 'Select list to prioritize ' . $node_rank . ' for node ranks is visible and set to 0.');
123 // Test each of the possible rankings.
125 foreach ($node_ranks as $node_rank) {
126 // Enable the ranking we are testing.
127 $edit['rankings[' . $node_rank . '][value]'] = 10;
128 $this->drupalPostForm('admin/config/search/pages/manage/node_search', $edit, t('Save search page'));
129 $this->drupalGet('admin/config/search/pages/manage/node_search');
130 $this->assertTrue($this->xpath('//select[@id="edit-rankings-' . $node_rank . '-value"]//option[@value="10"]'), 'Select list to prioritize ' . $node_rank . ' for node ranks is visible and set to 10.');
132 // Reload the plugin to get the up-to-date values.
133 $this->nodeSearch = SearchPage::load('node_search');
134 // Do the search and assert the results.
135 $this->nodeSearch->getPlugin()->setSearch('rocks', [], []);
136 $set = $this->nodeSearch->getPlugin()->execute();
137 $this->assertEqual($set[0]['node']->id(), $nodes[$node_rank][1]->id(), 'Search ranking "' . $node_rank . '" order.');
139 // Clear this ranking for the next test.
140 $edit['rankings[' . $node_rank . '][value]'] = 0;
143 // Save the final node_rank change then check that all rankings are visible
144 // and have been set back to 0.
145 $this->drupalPostForm('admin/config/search/pages/manage/node_search', $edit, t('Save search page'));
146 $this->drupalGet('admin/config/search/pages/manage/node_search');
147 foreach ($node_ranks as $node_rank) {
148 $this->assertTrue($this->xpath('//select[@id="edit-rankings-' . $node_rank . '-value"]//option[@value="0"]'), 'Select list to prioritize ' . $node_rank . ' for node ranks is visible and set to 0.');
151 // Try with sticky, then promoted. This is a test for issue
152 // https://www.drupal.org/node/771596.
161 $configuration = $this->nodeSearch->getPlugin()->getConfiguration();
162 foreach ($node_ranks as $var => $value) {
163 $configuration['rankings'][$var] = $value;
165 $this->nodeSearch->getPlugin()->setConfiguration($configuration);
166 $this->nodeSearch->save();
168 // Do the search and assert the results. The sticky node should show up
169 // first, then the promoted node, then all the rest.
170 $this->nodeSearch->getPlugin()->setSearch('rocks', [], []);
171 $set = $this->nodeSearch->getPlugin()->execute();
172 $this->assertEqual($set[0]['node']->id(), $nodes['sticky'][1]->id(), 'Search ranking for sticky first worked.');
173 $this->assertEqual($set[1]['node']->id(), $nodes['promote'][1]->id(), 'Search ranking for promoted second worked.');
175 // Try with recent, then comments. This is a test for issues
176 // https://www.drupal.org/node/771596 and
177 // https://www.drupal.org/node/303574.
186 $configuration = $this->nodeSearch->getPlugin()->getConfiguration();
187 foreach ($node_ranks as $var => $value) {
188 $configuration['rankings'][$var] = $value;
190 $this->nodeSearch->getPlugin()->setConfiguration($configuration);
191 $this->nodeSearch->save();
193 // Do the search and assert the results. The recent node should show up
194 // first, then the commented node, then all the rest.
195 $this->nodeSearch->getPlugin()->setSearch('rocks', [], []);
196 $set = $this->nodeSearch->getPlugin()->execute();
197 $this->assertEqual($set[0]['node']->id(), $nodes['recent'][1]->id(), 'Search ranking for recent first worked.');
198 $this->assertEqual($set[1]['node']->id(), $nodes['comments'][1]->id(), 'Search ranking for comments second worked.');
203 * Test rankings of HTML tags.
205 public function testHTMLRankings() {
206 $full_html_format = FilterFormat::create([
207 'format' => 'full_html',
208 'name' => 'Full HTML',
210 $full_html_format->save();
212 // Test HTML tags with different weights.
213 $sorted_tags = ['h1', 'h2', 'h3', 'h4', 'a', 'h5', 'h6', 'notag'];
214 $shuffled_tags = $sorted_tags;
216 // Shuffle tags to ensure HTML tags are ranked properly.
217 shuffle($shuffled_tags);
220 'title' => 'Simple node',
223 foreach ($shuffled_tags as $tag) {
226 $settings['body'] = [['value' => \Drupal::l('Drupal Rocks', new Url('<front>')), 'format' => 'full_html']];
229 $settings['body'] = [['value' => 'Drupal Rocks']];
232 $settings['body'] = [['value' => "<$tag>Drupal Rocks</$tag>", 'format' => 'full_html']];
235 $nodes[$tag] = $this->drupalCreateNode($settings);
238 // Update the search index.
239 $this->nodeSearch->getPlugin()->updateIndex();
240 search_update_totals();
242 $this->nodeSearch->getPlugin()->setSearch('rocks', [], []);
243 // Do the search and assert the results.
244 $set = $this->nodeSearch->getPlugin()->execute();
246 // Test the ranking of each tag.
247 foreach ($sorted_tags as $tag_rank => $tag) {
248 // Assert the results.
249 if ($tag == 'notag') {
250 $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for plain text order.');
253 $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for "<' . $sorted_tags[$tag_rank] . '>" order.');
257 // Test tags with the same weight against the sorted tags.
258 $unsorted_tags = ['u', 'b', 'i', 'strong', 'em'];
259 foreach ($unsorted_tags as $tag) {
260 $settings['body'] = [['value' => "<$tag>Drupal Rocks</$tag>", 'format' => 'full_html']];
261 $node = $this->drupalCreateNode($settings);
263 // Update the search index.
264 $this->nodeSearch->getPlugin()->updateIndex();
265 search_update_totals();
267 $this->nodeSearch->getPlugin()->setSearch('rocks', [], []);
268 // Do the search and assert the results.
269 $set = $this->nodeSearch->getPlugin()->execute();
271 // Ranking should always be second to last.
272 $set = array_slice($set, -2, 1);
274 // Assert the results.
275 $this->assertEqual($set[0]['node']->id(), $node->id(), 'Search tag ranking for "<' . $tag . '>" order.');
277 // Delete node so it doesn't show up in subsequent search results.