From f247d598cbeee14f3675a39b8ace51aeae3482f2 Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Sat, 25 Feb 2023 09:46:08 +0000 Subject: [PATCH] Issue #3341098 by quietone: Move non-migrations tests to Statistics --- .../src/Kernel/BlockConfigSchemaTest.php | 1 - .../src/Functional/SearchRankingTest.php | 20 +-- .../Functional/search/SearchRankingTest.php | 116 ++++++++++++++++++ .../UpdatePathTestBaseFilledTest.php | 1 - .../src/Functional/Handler/HandlerAllTest.php | 1 - core/phpstan-baseline.neon | 2 +- 6 files changed, 120 insertions(+), 21 deletions(-) create mode 100644 core/modules/statistics/tests/src/Functional/search/SearchRankingTest.php diff --git a/core/modules/block/tests/src/Kernel/BlockConfigSchemaTest.php b/core/modules/block/tests/src/Kernel/BlockConfigSchemaTest.php index 42d6bd50bd18..dead2e53917d 100644 --- a/core/modules/block/tests/src/Kernel/BlockConfigSchemaTest.php +++ b/core/modules/block/tests/src/Kernel/BlockConfigSchemaTest.php @@ -25,7 +25,6 @@ class BlockConfigSchemaTest extends KernelTestBase { 'comment', 'forum', 'node', - 'statistics', // \Drupal\block\Entity\Block->preSave() calls system_region_list(). 'system', 'taxonomy', diff --git a/core/modules/search/tests/src/Functional/SearchRankingTest.php b/core/modules/search/tests/src/Functional/SearchRankingTest.php index 30fb78ff1643..341809fd543e 100644 --- a/core/modules/search/tests/src/Functional/SearchRankingTest.php +++ b/core/modules/search/tests/src/Functional/SearchRankingTest.php @@ -4,7 +4,6 @@ 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; @@ -33,7 +32,7 @@ class SearchRankingTest extends BrowserTestBase { /** * {@inheritdoc} */ - protected static $modules = ['node', 'search', 'statistics', 'comment']; + protected static $modules = ['node', 'search', 'comment']; /** * {@inheritdoc} @@ -65,7 +64,7 @@ public function testRankings() { $this->addDefaultCommentField('node', 'page'); // Build a list of the rankings to test. - $node_ranks = ['sticky', 'promote', 'relevance', 'recent', 'comments', 'views']; + $node_ranks = ['sticky', 'promote', 'relevance', 'recent', 'comments']; // Create nodes for testing. $nodes = []; @@ -116,18 +115,7 @@ public function testRankings() { $this->submitForm($edit, 'Preview'); $this->submitForm($edit, 'Save'); - // Enable counting of statistics. - $this->config('statistics.settings')->set('count_content_views', 1)->save(); - - // Simulating content views is kind of difficult in the test. Leave that - // to the Statistics module. 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' => REQUEST_TIME, 'nid' => $nid]) - ->execute(); - - // Run cron to update the search index and comment/statistics totals. + // Run cron to update the search index totals. $this->cronRun(); // Test that the settings form displays the content ranking section. @@ -177,7 +165,6 @@ public function testRankings() { 'relevance' => 0, 'recent' => 0, 'comments' => 0, - 'views' => 0, ]; $configuration = $this->nodeSearch->getPlugin()->getConfiguration(); foreach ($node_ranks as $var => $value) { @@ -202,7 +189,6 @@ public function testRankings() { 'relevance' => 0, 'recent' => 10, 'comments' => 1, - 'views' => 0, ]; $configuration = $this->nodeSearch->getPlugin()->getConfiguration(); foreach ($node_ranks as $var => $value) { diff --git a/core/modules/statistics/tests/src/Functional/search/SearchRankingTest.php b/core/modules/statistics/tests/src/Functional/search/SearchRankingTest.php new file mode 100644 index 000000000000..9b98988e4177 --- /dev/null +++ b/core/modules/statistics/tests/src/Functional/search/SearchRankingTest.php @@ -0,0 +1,116 @@ +<?php + +namespace Drupal\Tests\statistics\Functional\search; + +use Drupal\Core\Database\Database; +use Drupal\search\Entity\SearchPage; +use Drupal\Tests\BrowserTestBase; +use Drupal\Tests\Traits\Core\CronRunTrait; + +/** + * Indexes content and tests ranking factors. + * + * @group statistics + */ +class SearchRankingTest extends BrowserTestBase { + + use CronRunTrait; + + /** + * The node search page. + * + * @var \Drupal\search\SearchPageInterface + */ + protected $nodeSearch; + + /** + * {@inheritdoc} + */ + protected static $modules = ['node', 'search', 'statistics']; + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); + + // Create a plugin instance. + $this->nodeSearch = SearchPage::load('node_search'); + + // Log in with sufficient privileges. + $this->drupalLogin($this->drupalCreateUser([ + 'create page content', + 'administer search', + ])); + } + + /** + * Tests statistics ranking on search pages. + */ + public function testRankings() { + // 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()); + } + +} diff --git a/core/modules/system/tests/src/Functional/UpdateSystem/UpdatePathTestBaseFilledTest.php b/core/modules/system/tests/src/Functional/UpdateSystem/UpdatePathTestBaseFilledTest.php index 826581ffbc55..4c6ca042fd04 100644 --- a/core/modules/system/tests/src/Functional/UpdateSystem/UpdatePathTestBaseFilledTest.php +++ b/core/modules/system/tests/src/Functional/UpdateSystem/UpdatePathTestBaseFilledTest.php @@ -375,7 +375,6 @@ public function testUpdatedSite() { 'search', 'serialization', 'shortcut', - 'statistics', 'syslog', 'system', 'taxonomy', diff --git a/core/modules/views/tests/src/Functional/Handler/HandlerAllTest.php b/core/modules/views/tests/src/Functional/Handler/HandlerAllTest.php index 52925792a882..783f0b849bcf 100644 --- a/core/modules/views/tests/src/Functional/Handler/HandlerAllTest.php +++ b/core/modules/views/tests/src/Functional/Handler/HandlerAllTest.php @@ -38,7 +38,6 @@ class HandlerAllTest extends ViewTestBase { 'locale', 'node', 'search', - 'statistics', 'taxonomy', 'user', ]; diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon index a956663a84a5..741fa2d7ac99 100644 --- a/core/phpstan-baseline.neon +++ b/core/phpstan-baseline.neon @@ -2032,7 +2032,7 @@ parameters: - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 3 + count: 2 path: modules/search/tests/src/Functional/SearchRankingTest.php - -- GitLab