View source
<?php
namespace Drupal\Tests\search\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Database\Database;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\filter\Entity\FilterFormat;
use Drupal\search\Entity\SearchPage;
use Drupal\search\SearchIndexInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
class SearchRankingTest extends BrowserTestBase {
use CommentTestTrait;
use CronRunTrait;
protected $nodeSearch;
protected static $modules = [
'node',
'search',
'statistics',
'comment',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->nodeSearch = SearchPage::load('node_search');
$this
->drupalLogin($this
->drupalCreateUser([
'post comments',
'skip comment approval',
'create page content',
'administer search',
]));
}
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
->submitForm($edit, 'Preview');
$this
->submitForm($edit, '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
->assertSession()
->pageTextContains('Content ranking');
foreach ($node_ranks as $node_rank) {
$this
->assertSession()
->optionExists('edit-rankings-' . $node_rank . '-value', '0');
}
$edit = [];
foreach ($node_ranks as $node_rank) {
$edit['rankings[' . $node_rank . '][value]'] = 10;
$this
->drupalGet('admin/config/search/pages/manage/node_search');
$this
->submitForm($edit, 'Save search page');
$this
->drupalGet('admin/config/search/pages/manage/node_search');
$this
->assertSession()
->optionExists('edit-rankings-' . $node_rank . '-value', '10');
$this->nodeSearch = SearchPage::load('node_search');
$this->nodeSearch
->getPlugin()
->setSearch('rocks', [], []);
$set = $this->nodeSearch
->getPlugin()
->execute();
$this
->assertEquals($nodes[$node_rank][1]
->id(), $set[0]['node']
->id(), 'Search ranking "' . $node_rank . '" order.');
$edit['rankings[' . $node_rank . '][value]'] = 0;
}
$this
->drupalGet('admin/config/search/pages/manage/node_search');
$this
->submitForm($edit, 'Save search page');
$this
->drupalGet('admin/config/search/pages/manage/node_search');
foreach ($node_ranks as $node_rank) {
$this
->assertSession()
->optionExists('edit-rankings-' . $node_rank . '-value', '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
->assertEquals($nodes['sticky'][1]
->id(), $set[0]['node']
->id(), 'Search ranking for sticky first worked.');
$this
->assertEquals($nodes['promote'][1]
->id(), $set[1]['node']
->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
->assertEquals($nodes['recent'][1]
->id(), $set[0]['node']
->id(), 'Search ranking for recent first worked.');
$this
->assertEquals($nodes['comments'][1]
->id(), $set[1]['node']
->id(), 'Search ranking for comments second worked.');
}
public function testHTMLRankings() {
$full_html_format = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
]);
$full_html_format
->save();
$sorted_tags = [
'h1',
'h2',
'h3',
'h4',
'a',
'h5',
'h6',
'notag',
];
$shuffled_tags = $sorted_tags;
shuffle($shuffled_tags);
$settings = [
'type' => 'page',
'title' => 'Simple node',
];
$nodes = [];
foreach ($shuffled_tags as $tag) {
switch ($tag) {
case 'a':
$settings['body'] = [
[
'value' => Link::fromTextAndUrl('Drupal Rocks', Url::fromRoute('<front>'))
->toString(),
'format' => 'full_html',
],
];
break;
case 'notag':
$settings['body'] = [
[
'value' => 'Drupal Rocks',
],
];
break;
default:
$settings['body'] = [
[
'value' => "<{$tag}>Drupal Rocks</{$tag}>",
'format' => 'full_html',
],
];
break;
}
$nodes[$tag] = $this
->drupalCreateNode($settings);
}
$this->nodeSearch
->getPlugin()
->updateIndex();
$search_index = \Drupal::service('search.index');
assert($search_index instanceof SearchIndexInterface);
$this->nodeSearch
->getPlugin()
->setSearch('rocks', [], []);
$set = $this->nodeSearch
->getPlugin()
->execute();
foreach ($sorted_tags as $tag_rank => $tag) {
if ($tag == 'notag') {
$this
->assertEquals($nodes[$tag]
->id(), $set[$tag_rank]['node']
->id(), 'Search tag ranking for plain text order.');
}
else {
$this
->assertEquals($nodes[$tag]
->id(), $set[$tag_rank]['node']
->id(), 'Search tag ranking for "<' . $sorted_tags[$tag_rank] . '>" order.');
}
}
$unsorted_tags = [
'u',
'b',
'i',
'strong',
'em',
];
foreach ($unsorted_tags as $tag) {
$settings['body'] = [
[
'value' => "<{$tag}>Drupal Rocks</{$tag}>",
'format' => 'full_html',
],
];
$node = $this
->drupalCreateNode($settings);
$this->nodeSearch
->getPlugin()
->updateIndex();
$this->nodeSearch
->getPlugin()
->setSearch('rocks', [], []);
$set = $this->nodeSearch
->getPlugin()
->execute();
$set = array_slice($set, -2, 1);
$this
->assertEquals($node
->id(), $set[0]['node']
->id(), 'Search tag ranking for "<' . $tag . '>" order.');
$node
->delete();
}
}
}