StatisticsLoggingTest.php in Drupal 10
File
core/modules/statistics/tests/src/FunctionalJavascript/StatisticsLoggingTest.php
View source
<?php
namespace Drupal\Tests\statistics\FunctionalJavascript;
use Drupal\Core\Session\AccountInterface;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\user\Entity\Role;
class StatisticsLoggingTest extends WebDriverTestBase {
protected static $modules = [
'node',
'statistics',
'language',
];
protected $defaultTheme = 'stark';
protected $node;
protected function setUp() : void {
parent::setUp();
$this
->config('statistics.settings')
->set('count_content_views', 1)
->save();
Role::load(AccountInterface::ANONYMOUS_ROLE)
->grantPermission('view post access counter')
->save();
ConfigurableLanguage::create([
'id' => 'xx',
'label' => 'Test language',
])
->save();
$this
->config('language.negotiation')
->set('url.prefixes.en', 'en')
->save();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->node = $this
->drupalCreateNode();
}
public function testLoggingPage() {
$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'));
}
protected function getStatisticsCounter($path) {
$this
->drupalGet($path);
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this->node
->save();
$field_counter = $this
->getSession()
->getPage()
->find('css', '.links li');
return $field_counter ? (int) explode(' ', $field_counter
->getText())[0] : NULL;
}
}