comparison core/modules/search/tests/src/Functional/SearchRankingTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php
2
3 namespace Drupal\Tests\search\Functional;
4
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\comment\Tests\CommentTestTrait;
7 use Drupal\Core\Url;
8 use Drupal\filter\Entity\FilterFormat;
9 use Drupal\search\Entity\SearchPage;
10 use Drupal\Tests\BrowserTestBase;
11 use Drupal\Tests\Traits\Core\CronRunTrait;
12
13 /**
14 * Indexes content and tests ranking factors.
15 *
16 * @group search
17 */
18 class SearchRankingTest extends BrowserTestBase {
19
20 use CommentTestTrait;
21 use CronRunTrait;
22
23 /**
24 * The node search page.
25 *
26 * @var \Drupal\search\SearchPageInterface
27 */
28 protected $nodeSearch;
29
30 /**
31 * {@inheritdoc}
32 */
33 protected static $modules = ['node', 'search', 'statistics', 'comment'];
34
35 protected function setUp() {
36 parent::setUp();
37
38 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
39
40 // Create a plugin instance.
41 $this->nodeSearch = SearchPage::load('node_search');
42
43 // Log in with sufficient privileges.
44 $this->drupalLogin($this->drupalCreateUser(['post comments', 'skip comment approval', 'create page content', 'administer search']));
45 }
46
47 public function testRankings() {
48 // Add a comment field.
49 $this->addDefaultCommentField('node', 'page');
50
51 // Build a list of the rankings to test.
52 $node_ranks = ['sticky', 'promote', 'relevance', 'recent', 'comments', 'views'];
53
54 // Create nodes for testing.
55 $nodes = [];
56 foreach ($node_ranks as $node_rank) {
57 $settings = [
58 'type' => 'page',
59 'comment' => [
60 ['status' => CommentItemInterface::HIDDEN],
61 ],
62 'title' => 'Drupal rocks',
63 'body' => [['value' => "Drupal's search rocks"]],
64 // Node is one day old.
65 'created' => REQUEST_TIME - 24 * 3600,
66 'sticky' => 0,
67 'promote' => 0,
68 ];
69 foreach ([0, 1] as $num) {
70 if ($num == 1) {
71 switch ($node_rank) {
72 case 'sticky':
73 case 'promote':
74 $settings[$node_rank] = 1;
75 break;
76 case 'relevance':
77 $settings['body'][0]['value'] .= " really rocks";
78 break;
79 case 'recent':
80 // Node is 1 hour hold.
81 $settings['created'] = REQUEST_TIME - 3600;
82 break;
83 case 'comments':
84 $settings['comment'][0]['status'] = CommentItemInterface::OPEN;
85 break;
86 }
87 }
88 $nodes[$node_rank][$num] = $this->drupalCreateNode($settings);
89 }
90 }
91
92 // Add a comment to one of the nodes.
93 $edit = [];
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'));
99
100 // Enable counting of statistics.
101 $this->config('statistics.settings')->set('count_content_views', 1)->save();
102
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])
109 ->execute();
110
111 // Run cron to update the search index and comment/statistics totals.
112 $this->cronRun();
113
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'));
117
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.');
121 }
122
123 // Test each of the possible rankings.
124 $edit = [];
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.');
131
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.');
138
139 // Clear this ranking for the next test.
140 $edit['rankings[' . $node_rank . '][value]'] = 0;
141 }
142
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.');
149 }
150
151 // Try with sticky, then promoted. This is a test for issue
152 // https://www.drupal.org/node/771596.
153 $node_ranks = [
154 'sticky' => 10,
155 'promote' => 1,
156 'relevance' => 0,
157 'recent' => 0,
158 'comments' => 0,
159 'views' => 0,
160 ];
161 $configuration = $this->nodeSearch->getPlugin()->getConfiguration();
162 foreach ($node_ranks as $var => $value) {
163 $configuration['rankings'][$var] = $value;
164 }
165 $this->nodeSearch->getPlugin()->setConfiguration($configuration);
166 $this->nodeSearch->save();
167
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.');
174
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.
178 $node_ranks = [
179 'sticky' => 0,
180 'promote' => 0,
181 'relevance' => 0,
182 'recent' => 10,
183 'comments' => 1,
184 'views' => 0,
185 ];
186 $configuration = $this->nodeSearch->getPlugin()->getConfiguration();
187 foreach ($node_ranks as $var => $value) {
188 $configuration['rankings'][$var] = $value;
189 }
190 $this->nodeSearch->getPlugin()->setConfiguration($configuration);
191 $this->nodeSearch->save();
192
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.');
199
200 }
201
202 /**
203 * Test rankings of HTML tags.
204 */
205 public function testHTMLRankings() {
206 $full_html_format = FilterFormat::create([
207 'format' => 'full_html',
208 'name' => 'Full HTML',
209 ]);
210 $full_html_format->save();
211
212 // Test HTML tags with different weights.
213 $sorted_tags = ['h1', 'h2', 'h3', 'h4', 'a', 'h5', 'h6', 'notag'];
214 $shuffled_tags = $sorted_tags;
215
216 // Shuffle tags to ensure HTML tags are ranked properly.
217 shuffle($shuffled_tags);
218 $settings = [
219 'type' => 'page',
220 'title' => 'Simple node',
221 ];
222 $nodes = [];
223 foreach ($shuffled_tags as $tag) {
224 switch ($tag) {
225 case 'a':
226 $settings['body'] = [['value' => \Drupal::l('Drupal Rocks', new Url('<front>')), 'format' => 'full_html']];
227 break;
228 case 'notag':
229 $settings['body'] = [['value' => 'Drupal Rocks']];
230 break;
231 default:
232 $settings['body'] = [['value' => "<$tag>Drupal Rocks</$tag>", 'format' => 'full_html']];
233 break;
234 }
235 $nodes[$tag] = $this->drupalCreateNode($settings);
236 }
237
238 // Update the search index.
239 $this->nodeSearch->getPlugin()->updateIndex();
240 search_update_totals();
241
242 $this->nodeSearch->getPlugin()->setSearch('rocks', [], []);
243 // Do the search and assert the results.
244 $set = $this->nodeSearch->getPlugin()->execute();
245
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.');
251 }
252 else {
253 $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for "&lt;' . $sorted_tags[$tag_rank] . '&gt;" order.');
254 }
255 }
256
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);
262
263 // Update the search index.
264 $this->nodeSearch->getPlugin()->updateIndex();
265 search_update_totals();
266
267 $this->nodeSearch->getPlugin()->setSearch('rocks', [], []);
268 // Do the search and assert the results.
269 $set = $this->nodeSearch->getPlugin()->execute();
270
271 // Ranking should always be second to last.
272 $set = array_slice($set, -2, 1);
273
274 // Assert the results.
275 $this->assertEqual($set[0]['node']->id(), $node->id(), 'Search tag ranking for "&lt;' . $tag . '&gt;" order.');
276
277 // Delete node so it doesn't show up in subsequent search results.
278 $node->delete();
279 }
280 }
281
282 }