function SearchRankingTest::testRankings

Same name in this branch
  1. 10 core/modules/search/tests/src/Functional/SearchRankingTest.php \Drupal\Tests\search\Functional\SearchRankingTest::testRankings()
Same name and namespace in other branches
  1. 9 core/modules/search/tests/src/Functional/SearchRankingTest.php \Drupal\Tests\search\Functional\SearchRankingTest::testRankings()
  2. 8.9.x core/modules/search/tests/src/Functional/SearchRankingTest.php \Drupal\Tests\search\Functional\SearchRankingTest::testRankings()
  3. 11.x core/modules/search/tests/src/Functional/SearchRankingTest.php \Drupal\Tests\search\Functional\SearchRankingTest::testRankings()
  4. 11.x core/modules/statistics/tests/src/Functional/search/SearchRankingTest.php \Drupal\Tests\statistics\Functional\search\SearchRankingTest::testRankings()

Tests statistics ranking on search pages.

File

core/modules/statistics/tests/src/Functional/search/SearchRankingTest.php, line 62

Class

SearchRankingTest
Indexes content and tests ranking factors.

Namespace

Drupal\Tests\statistics\Functional\search

Code

public function testRankings() : void {
  // Create nodes for testing.
  $nodes = [];
  $settings = [
    'type' => 'page',
    'title' => 'Drupal rocks',
    'body' => [
      [
        'value' => "Drupal's search rocks",
      ],
    ],
    // Node is one day old.
'created' => \Drupal::time()->getRequestTime() - 24 * 3600,
    'sticky' => 0,
    'promote' => 0,
  ];
  foreach ([
    0,
    1,
  ] as $num) {
    $nodes['views'][$num] = $this->drupalCreateNode($settings);
  }
  // Enable counting of statistics.
  $this->config('statistics.settings')
    ->set('count_content_views', 1)
    ->save();
  // Simulating content views is kind of difficult in the test. So instead go
  // ahead and manually update the counter for this node.
  $nid = $nodes['views'][1]->id();
  Database::getConnection()->insert('node_counter')
    ->fields([
    'totalcount' => 5,
    'daycount' => 5,
    'timestamp' => \Drupal::time()->getRequestTime(),
    'nid' => $nid,
  ])
    ->execute();
  // Run cron to update the search index and statistics totals.
  $this->cronRun();
  // Test that the settings form displays the content ranking section.
  $this->drupalGet('admin/config/search/pages/manage/node_search');
  $this->assertSession()
    ->pageTextContains('Content ranking');
  // Check that views ranking is visible and set to 0.
  $this->assertSession()
    ->optionExists('edit-rankings-views-value', '0');
  // Test each of the possible rankings.
  $edit = [];
  // Enable views ranking.
  $edit['rankings[views][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-views-value', '10');
  // Reload the plugin to get the up-to-date values.
  $this->nodeSearch = SearchPage::load('node_search');
  // Do the search and assert the results.
  $this->nodeSearch
    ->getPlugin()
    ->setSearch('rocks', [], []);
  $set = $this->nodeSearch
    ->getPlugin()
    ->execute();
  $this->assertEquals($nodes['views'][1]->id(), $set[0]['node']->id());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.