3 namespace Drupal\Tests\statistics\FunctionalJavascript;
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\user\Entity\Role;
11 * Tests that statistics works.
15 class StatisticsLoggingTest extends WebDriverTestBase {
20 public static $modules = ['node', 'statistics', 'language'];
25 * @var \Drupal\node\Entity\Node
32 protected function setUp() {
35 $this->config('statistics.settings')
36 ->set('count_content_views', 1)
39 Role::load(AccountInterface::ANONYMOUS_ROLE)
40 ->grantPermission('view post access counter')
43 // Add another language to enable multilingual path processor.
44 ConfigurableLanguage::create(['id' => 'xx'])->save();
45 $this->config('language.negotiation')->set('url.prefixes.en', 'en')->save();
47 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
48 $this->node = $this->drupalCreateNode();
52 * Tests that statistics works with different addressing variants.
54 public function testLoggingPage() {
55 // At the first request, the page does not contain statistics counter.
56 $this->assertNull($this->getStatisticsCounter('node/1'));
57 $this->assertSame(1, $this->getStatisticsCounter('node/1'));
58 $this->assertSame(2, $this->getStatisticsCounter('en/node/1'));
59 $this->assertSame(3, $this->getStatisticsCounter('en/node/1'));
60 $this->assertSame(4, $this->getStatisticsCounter('index.php/node/1'));
61 $this->assertSame(5, $this->getStatisticsCounter('index.php/node/1'));
62 $this->assertSame(6, $this->getStatisticsCounter('index.php/en/node/1'));
63 $this->assertSame(7, $this->getStatisticsCounter('index.php/en/node/1'));
67 * Gets counter of views by path.
73 * A counter of views. Returns NULL if the page does not contain statistics.
75 protected function getStatisticsCounter($path) {
76 $this->drupalGet($path);
77 // Wait while statistics module send ajax request.
78 $this->assertSession()->assertWaitOnAjaxRequest();
79 // Resaving the node to call the hook_node_links_alter(), which is used to
80 // update information on the page. See statistics_node_links_alter().
83 $field_counter = $this->getSession()->getPage()->find('css', '.statistics-counter');
84 return $field_counter ? (int) explode(' ', $field_counter->getText())[0] : NULL;