3 namespace Drupal\statistics\Tests\Views;
5 use Drupal\views\Tests\ViewTestBase;
6 use Drupal\views\Tests\ViewTestData;
9 * Tests basic integration of views data from the statistics module.
14 class IntegrationTest extends ViewTestBase {
22 public static $modules = ['statistics', 'statistics_test_views', 'node'];
25 * Stores the user object that accesses the page.
27 * @var \Drupal\user\UserInterface
32 * Stores the node object which is used by the test.
34 * @var \Drupal\node\Entity\Node
39 * Views used by this test.
43 public static $testViews = ['test_statistics_integration'];
45 protected function setUp($import_test_views = TRUE) {
46 parent::setUp($import_test_views);
48 ViewTestData::createTestViews(get_class($this), ['statistics_test_views']);
50 // Create a new user for viewing nodes and statistics.
51 $this->webUser = $this->drupalCreateUser(['access content', 'view post access counter']);
53 // Create a new user for viewing nodes only.
54 $this->deniedUser = $this->drupalCreateUser(['access content']);
56 $this->drupalCreateContentType(['type' => 'page']);
57 $this->node = $this->drupalCreateNode(['type' => 'page']);
59 // Enable counting of content views.
60 $this->config('statistics.settings')
61 ->set('count_content_views', 1)
67 * Tests the integration of the {node_counter} table in views.
69 public function testNodeCounterIntegration() {
70 $this->drupalLogin($this->webUser);
72 $this->drupalGet('node/' . $this->node->id());
73 // Manually calling statistics.php, simulating ajax behavior.
74 // @see \Drupal\statistics\Tests\StatisticsLoggingTest::testLogging().
76 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
77 $client = \Drupal::httpClient();
78 $client->post($stats_path, ['form_params' => ['nid' => $this->node->id()]]);
79 $this->drupalGet('test_statistics_integration');
81 $expected = statistics_get($this->node->id());
82 // Convert the timestamp to year, to match the expected output of the date
84 $expected['timestamp'] = date('Y', $expected['timestamp']);
86 foreach ($expected as $field => $value) {
87 $xpath = "//div[contains(@class, views-field-$field)]/span[@class = 'field-content']";
88 $this->assertFieldByXpath($xpath, $value, "The $field output matches the expected.");
91 $this->drupalLogout();
92 $this->drupalLogin($this->deniedUser);
93 $this->drupalGet('test_statistics_integration');
94 $this->assertResponse(200);
96 foreach ($expected as $field => $value) {
97 $xpath = "//div[contains(@class, views-field-$field)]/span[@class = 'field-content']";
98 $this->assertNoFieldByXpath($xpath, $value, "The $field output is not displayed.");