3 namespace Drupal\Tests\statistics\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Tests\Traits\Core\CronRunTrait;
9 * Tests the statistics admin.
13 class StatisticsAdminTest extends BrowserTestBase {
22 public static $modules = ['node', 'statistics'];
25 * A user that has permission to administer statistics.
27 * @var \Drupal\user\UserInterface
29 protected $privilegedUser;
32 * A page node for which to check content statistics.
34 * @var \Drupal\node\NodeInterface
39 * The Guzzle HTTP client.
41 * @var \GuzzleHttp\Client;
45 protected function setUp() {
48 // Set the max age to 0 to simplify testing.
49 $this->config('statistics.settings')->set('display_max_age', 0)->save();
51 // Create Basic page node type.
52 if ($this->profile != 'standard') {
53 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
55 $this->privilegedUser = $this->drupalCreateUser(['administer statistics', 'view post access counter', 'create page content']);
56 $this->drupalLogin($this->privilegedUser);
57 $this->testNode = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->privilegedUser->id()]);
58 $this->client = \Drupal::httpClient();
62 * Verifies that the statistics settings page works.
64 public function testStatisticsSettings() {
65 $config = $this->config('statistics.settings');
66 $this->assertFalse($config->get('count_content_views'), 'Count content view log is disabled by default.');
68 // Enable counter on content view.
69 $edit['statistics_count_content_views'] = 1;
70 $this->drupalPostForm('admin/config/system/statistics', $edit, t('Save configuration'));
71 $config = $this->config('statistics.settings');
72 $this->assertTrue($config->get('count_content_views'), 'Count content view log is enabled.');
75 $this->drupalGet('node/' . $this->testNode->id());
76 // Manually calling statistics.php, simulating ajax behavior.
77 $nid = $this->testNode->id();
78 $post = ['nid' => $nid];
80 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
81 $this->client->post($stats_path, ['form_params' => $post]);
83 // Hit the node again (the counter is incremented after the hit, so
84 // "1 view" will actually be shown when the node is hit the second time).
85 $this->drupalGet('node/' . $this->testNode->id());
86 $this->client->post($stats_path, ['form_params' => $post]);
87 $this->assertText('1 view', 'Node is viewed once.');
89 $this->drupalGet('node/' . $this->testNode->id());
90 $this->client->post($stats_path, ['form_params' => $post]);
91 $this->assertText('2 views', 'Node is viewed 2 times.');
93 // Increase the max age to test that nodes are no longer immediately
94 // updated, visit the node once more to populate the cache.
95 $this->config('statistics.settings')->set('display_max_age', 3600)->save();
96 $this->drupalGet('node/' . $this->testNode->id());
97 $this->assertText('3 views', 'Node is viewed 3 times.');
99 $this->client->post($stats_path, ['form_params' => $post]);
100 $this->drupalGet('node/' . $this->testNode->id());
101 $this->assertText('3 views', 'Views counter was not updated.');
105 * Tests that when a node is deleted, the node counter is deleted too.
107 public function testDeleteNode() {
108 $this->config('statistics.settings')->set('count_content_views', 1)->save();
110 $this->drupalGet('node/' . $this->testNode->id());
111 // Manually calling statistics.php, simulating ajax behavior.
112 $nid = $this->testNode->id();
113 $post = ['nid' => $nid];
115 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
116 $this->client->post($stats_path, ['form_params' => $post]);
118 $result = db_select('node_counter', 'n')
119 ->fields('n', ['nid'])
120 ->condition('n.nid', $this->testNode->id())
123 $this->assertEqual($result['nid'], $this->testNode->id(), 'Verifying that the node counter is incremented.');
125 $this->testNode->delete();
127 $result = db_select('node_counter', 'n')
128 ->fields('n', ['nid'])
129 ->condition('n.nid', $this->testNode->id())
132 $this->assertFalse($result, 'Verifying that the node counter is deleted.');
136 * Tests that cron clears day counts and expired access logs.
138 public function testExpiredLogs() {
139 $this->config('statistics.settings')
140 ->set('count_content_views', 1)
142 \Drupal::state()->set('statistics.day_timestamp', 8640000);
144 $this->drupalGet('node/' . $this->testNode->id());
145 // Manually calling statistics.php, simulating ajax behavior.
146 $nid = $this->testNode->id();
147 $post = ['nid' => $nid];
149 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
150 $this->client->post($stats_path, ['form_params' => $post]);
151 $this->drupalGet('node/' . $this->testNode->id());
152 $this->client->post($stats_path, ['form_params' => $post]);
153 $this->assertText('1 view', 'Node is viewed once.');
155 // statistics_cron() will subtract
156 // statistics.settings:accesslog.max_lifetime config from REQUEST_TIME in
157 // the delete query, so wait two secs here to make sure the access log will
158 // be flushed for the node just hit.
162 $this->drupalGet('admin/reports/pages');
163 $this->assertNoText('node/' . $this->testNode->id(), 'No hit URL found.');
165 $result = db_select('node_counter', 'nc')
166 ->fields('nc', ['daycount'])
167 ->condition('nid', $this->testNode->id(), '=')
170 $this->assertFalse($result, 'Daycounter is zero.');