IntegrationTest.php in Drupal 9
File
core/modules/statistics/tests/src/Functional/Views/IntegrationTest.php
View source
<?php
namespace Drupal\Tests\statistics\Functional\Views;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
class IntegrationTest extends ViewTestBase {
protected static $modules = [
'statistics',
'statistics_test_views',
'node',
];
protected $defaultTheme = 'stark';
protected $webUser;
protected $node;
public static $testViews = [
'test_statistics_integration',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
ViewTestData::createTestViews(static::class, [
'statistics_test_views',
]);
$this->webUser = $this
->drupalCreateUser([
'access content',
'view post access counter',
]);
$this->deniedUser = $this
->drupalCreateUser([
'access content',
]);
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this->node = $this
->drupalCreateNode([
'type' => 'page',
]);
$this
->config('statistics.settings')
->set('count_content_views', 1)
->save();
}
public function testNodeCounterIntegration() {
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/' . $this->node
->id());
global $base_url;
$stats_path = $base_url . '/' . $this
->getModulePath('statistics') . '/statistics.php';
$client = $this
->getHttpClient();
$client
->post($stats_path, [
'form_params' => [
'nid' => $this->node
->id(),
],
]);
$this
->drupalGet('test_statistics_integration');
$statistics = \Drupal::service('statistics.storage.node')
->fetchView($this->node
->id());
$this
->assertSession()
->pageTextContains('Total views: 1');
$this
->assertSession()
->pageTextContains('Views today: 1');
$this
->assertSession()
->pageTextContains('Most recent view: ' . date('Y', $statistics
->getTimestamp()));
$this
->drupalLogout();
$this
->drupalLogin($this->deniedUser);
$this
->drupalGet('test_statistics_integration');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextNotContains('Total views:');
$this
->assertSession()
->pageTextNotContains('Views today:');
$this
->assertSession()
->pageTextNotContains('Most recent view:');
}
}
Classes
Name |
Description |
IntegrationTest |
Tests basic integration of views data from the statistics module. |