public function SearchRankingTest::testRankings in Drupal 8
Same name and namespace in other branches
- 9 core/modules/search/tests/src/Functional/SearchRankingTest.php \Drupal\Tests\search\Functional\SearchRankingTest::testRankings()
File
- core/modules/search/tests/src/Functional/SearchRankingTest.php, line 60
Class
- SearchRankingTest
- Indexes content and tests ranking factors.
Namespace
Drupal\Tests\search\Functional
Code
public function testRankings() {
$this
->addDefaultCommentField('node', 'page');
$node_ranks = [
'sticky',
'promote',
'relevance',
'recent',
'comments',
'views',
];
$nodes = [];
foreach ($node_ranks as $node_rank) {
$settings = [
'type' => 'page',
'comment' => [
[
'status' => CommentItemInterface::HIDDEN,
],
],
'title' => 'Drupal rocks',
'body' => [
[
'value' => "Drupal's search rocks",
],
],
'created' => REQUEST_TIME - 24 * 3600,
'sticky' => 0,
'promote' => 0,
];
foreach ([
0,
1,
] as $num) {
if ($num == 1) {
switch ($node_rank) {
case 'sticky':
case 'promote':
$settings[$node_rank] = 1;
break;
case 'relevance':
$settings['body'][0]['value'] .= " really rocks";
break;
case 'recent':
$settings['created'] = REQUEST_TIME - 3600;
break;
case 'comments':
$settings['comment'][0]['status'] = CommentItemInterface::OPEN;
break;
}
}
$nodes[$node_rank][$num] = $this
->drupalCreateNode($settings);
}
}
$edit = [];
$edit['subject[0][value]'] = 'my comment title';
$edit['comment_body[0][value]'] = 'some random comment';
$this
->drupalGet('comment/reply/node/' . $nodes['comments'][1]
->id() . '/comment');
$this
->drupalPostForm(NULL, $edit, t('Preview'));
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->config('statistics.settings')
->set('count_content_views', 1)
->save();
$nid = $nodes['views'][1]
->id();
Database::getConnection()
->insert('node_counter')
->fields([
'totalcount' => 5,
'daycount' => 5,
'timestamp' => REQUEST_TIME,
'nid' => $nid,
])
->execute();
$this
->cronRun();
$this
->drupalGet('admin/config/search/pages/manage/node_search');
$this
->assertText(t('Content ranking'));
foreach ($node_ranks as $node_rank) {
$this
->assertNotEmpty($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.');
}
$edit = [];
foreach ($node_ranks as $node_rank) {
$edit['rankings[' . $node_rank . '][value]'] = 10;
$this
->drupalPostForm('admin/config/search/pages/manage/node_search', $edit, t('Save search page'));
$this
->drupalGet('admin/config/search/pages/manage/node_search');
$this
->assertNotEmpty($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.');
$this->nodeSearch = SearchPage::load('node_search');
$this->nodeSearch
->getPlugin()
->setSearch('rocks', [], []);
$set = $this->nodeSearch
->getPlugin()
->execute();
$this
->assertEqual($set[0]['node']
->id(), $nodes[$node_rank][1]
->id(), 'Search ranking "' . $node_rank . '" order.');
$edit['rankings[' . $node_rank . '][value]'] = 0;
}
$this
->drupalPostForm('admin/config/search/pages/manage/node_search', $edit, t('Save search page'));
$this
->drupalGet('admin/config/search/pages/manage/node_search');
foreach ($node_ranks as $node_rank) {
$this
->assertNotEmpty($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.');
}
$node_ranks = [
'sticky' => 10,
'promote' => 1,
'relevance' => 0,
'recent' => 0,
'comments' => 0,
'views' => 0,
];
$configuration = $this->nodeSearch
->getPlugin()
->getConfiguration();
foreach ($node_ranks as $var => $value) {
$configuration['rankings'][$var] = $value;
}
$this->nodeSearch
->getPlugin()
->setConfiguration($configuration);
$this->nodeSearch
->save();
$this->nodeSearch
->getPlugin()
->setSearch('rocks', [], []);
$set = $this->nodeSearch
->getPlugin()
->execute();
$this
->assertEqual($set[0]['node']
->id(), $nodes['sticky'][1]
->id(), 'Search ranking for sticky first worked.');
$this
->assertEqual($set[1]['node']
->id(), $nodes['promote'][1]
->id(), 'Search ranking for promoted second worked.');
$node_ranks = [
'sticky' => 0,
'promote' => 0,
'relevance' => 0,
'recent' => 10,
'comments' => 1,
'views' => 0,
];
$configuration = $this->nodeSearch
->getPlugin()
->getConfiguration();
foreach ($node_ranks as $var => $value) {
$configuration['rankings'][$var] = $value;
}
$this->nodeSearch
->getPlugin()
->setConfiguration($configuration);
$this->nodeSearch
->save();
$this->nodeSearch
->getPlugin()
->setSearch('rocks', [], []);
$set = $this->nodeSearch
->getPlugin()
->execute();
$this
->assertEqual($set[0]['node']
->id(), $nodes['recent'][1]
->id(), 'Search ranking for recent first worked.');
$this
->assertEqual($set[1]['node']
->id(), $nodes['comments'][1]
->id(), 'Search ranking for comments second worked.');
}