diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module index 4d7f42a33c72a784bce3994c3b195740b0fd1dd4..15e702e962ff7220a7b1251200963945724d9baf 100644 --- a/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -41,7 +41,7 @@ function statistics_help($route_name, RouteMatchInterface $route_match) { function statistics_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) { if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) { $build['#attached']['library'][] = 'statistics/drupal.statistics'; - $settings = ['data' => ['nid' => $node->id()], 'url' => Url::fromUri('base:' . drupal_get_path('module', 'statistics') . '/statistics.php')->toString()]; + $settings = ['data' => ['nid' => $node->id()], 'url' => \Drupal::request()->getBasePath() . '/' . drupal_get_path('module', 'statistics') . '/statistics.php']; $build['#attached']['drupalSettings']['statistics'] = $settings; } } diff --git a/core/modules/statistics/tests/src/FunctionalJavascript/StatisticsLoggingTest.php b/core/modules/statistics/tests/src/FunctionalJavascript/StatisticsLoggingTest.php new file mode 100644 index 0000000000000000000000000000000000000000..b00f0ed47dfbea0217cb7a6b5b4d2cc8ef9180cb --- /dev/null +++ b/core/modules/statistics/tests/src/FunctionalJavascript/StatisticsLoggingTest.php @@ -0,0 +1,87 @@ +<?php + +namespace Drupal\Tests\statistics\FunctionalJavascript; + +use Drupal\Core\Session\AccountInterface; +use Drupal\FunctionalJavascriptTests\JavascriptTestBase; +use Drupal\language\Entity\ConfigurableLanguage; +use Drupal\user\Entity\Role; + +/** + * Tests that statistics works. + * + * @group system + */ +class StatisticsLoggingTest extends JavascriptTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['node', 'statistics', 'language']; + + /** + * Node for tests. + * + * @var \Drupal\node\Entity\Node + */ + protected $node; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->config('statistics.settings') + ->set('count_content_views', 1) + ->save(); + + Role::load(AccountInterface::ANONYMOUS_ROLE) + ->grantPermission('view post access counter') + ->save(); + + // Add another language to enable multilingual path processor. + ConfigurableLanguage::create(['id' => 'xx'])->save(); + $this->config('language.negotiation')->set('url.prefixes.en', 'en')->save(); + + $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); + $this->node = $this->drupalCreateNode(); + } + + /** + * Tests that statistics works with different addressing variants. + */ + public function testLoggingPage() { + // At the first request, the page does not contain statistics counter. + $this->assertNull($this->getStatisticsCounter('node/1')); + $this->assertSame(1, $this->getStatisticsCounter('node/1')); + $this->assertSame(2, $this->getStatisticsCounter('en/node/1')); + $this->assertSame(3, $this->getStatisticsCounter('en/node/1')); + $this->assertSame(4, $this->getStatisticsCounter('index.php/node/1')); + $this->assertSame(5, $this->getStatisticsCounter('index.php/node/1')); + $this->assertSame(6, $this->getStatisticsCounter('index.php/en/node/1')); + $this->assertSame(7, $this->getStatisticsCounter('index.php/en/node/1')); + } + + /** + * Gets counter of views by path. + * + * @param string $path + * A path to node. + * + * @return int|null + * A counter of views. Returns NULL if the page does not contain statistics. + */ + protected function getStatisticsCounter($path) { + $this->drupalGet($path); + // Wait while statistics module send ajax request. + $this->assertSession()->assertWaitOnAjaxRequest(); + // Resaving the node to call the hook_node_links_alter(), which is used to + // update information on the page. See statistics_node_links_alter(). + $this->node->save(); + + $field_counter = $this->getSession()->getPage()->find('css', '.statistics-counter'); + return $field_counter ? (int) explode(' ', $field_counter->getText())[0] : NULL; + } + +}