Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\statistics\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
6 use Drupal\Tests\Traits\Core\CronRunTrait;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Tests the statistics admin.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @group statistics
|
Chris@0
|
12 */
|
Chris@0
|
13 class StatisticsAdminTest extends BrowserTestBase {
|
Chris@0
|
14
|
Chris@0
|
15 use CronRunTrait;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Modules to enable.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var array
|
Chris@0
|
21 */
|
Chris@0
|
22 public static $modules = ['node', 'statistics'];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * A user that has permission to administer statistics.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\user\UserInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $privilegedUser;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * A page node for which to check content statistics.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var \Drupal\node\NodeInterface
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $testNode;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * The Guzzle HTTP client.
|
Chris@0
|
40 *
|
Chris@17
|
41 * @var \GuzzleHttp\Client
|
Chris@0
|
42 */
|
Chris@0
|
43 protected $client;
|
Chris@0
|
44
|
Chris@0
|
45 protected function setUp() {
|
Chris@0
|
46 parent::setUp();
|
Chris@0
|
47
|
Chris@0
|
48 // Set the max age to 0 to simplify testing.
|
Chris@0
|
49 $this->config('statistics.settings')->set('display_max_age', 0)->save();
|
Chris@0
|
50
|
Chris@0
|
51 // Create Basic page node type.
|
Chris@0
|
52 if ($this->profile != 'standard') {
|
Chris@0
|
53 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
|
Chris@0
|
54 }
|
Chris@0
|
55 $this->privilegedUser = $this->drupalCreateUser(['administer statistics', 'view post access counter', 'create page content']);
|
Chris@0
|
56 $this->drupalLogin($this->privilegedUser);
|
Chris@0
|
57 $this->testNode = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->privilegedUser->id()]);
|
Chris@0
|
58 $this->client = \Drupal::httpClient();
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Verifies that the statistics settings page works.
|
Chris@0
|
63 */
|
Chris@0
|
64 public function testStatisticsSettings() {
|
Chris@0
|
65 $config = $this->config('statistics.settings');
|
Chris@0
|
66 $this->assertFalse($config->get('count_content_views'), 'Count content view log is disabled by default.');
|
Chris@0
|
67
|
Chris@0
|
68 // Enable counter on content view.
|
Chris@0
|
69 $edit['statistics_count_content_views'] = 1;
|
Chris@0
|
70 $this->drupalPostForm('admin/config/system/statistics', $edit, t('Save configuration'));
|
Chris@0
|
71 $config = $this->config('statistics.settings');
|
Chris@0
|
72 $this->assertTrue($config->get('count_content_views'), 'Count content view log is enabled.');
|
Chris@0
|
73
|
Chris@0
|
74 // Hit the node.
|
Chris@0
|
75 $this->drupalGet('node/' . $this->testNode->id());
|
Chris@0
|
76 // Manually calling statistics.php, simulating ajax behavior.
|
Chris@0
|
77 $nid = $this->testNode->id();
|
Chris@0
|
78 $post = ['nid' => $nid];
|
Chris@0
|
79 global $base_url;
|
Chris@0
|
80 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
Chris@0
|
81 $this->client->post($stats_path, ['form_params' => $post]);
|
Chris@0
|
82
|
Chris@0
|
83 // Hit the node again (the counter is incremented after the hit, so
|
Chris@0
|
84 // "1 view" will actually be shown when the node is hit the second time).
|
Chris@0
|
85 $this->drupalGet('node/' . $this->testNode->id());
|
Chris@0
|
86 $this->client->post($stats_path, ['form_params' => $post]);
|
Chris@0
|
87 $this->assertText('1 view', 'Node is viewed once.');
|
Chris@0
|
88
|
Chris@0
|
89 $this->drupalGet('node/' . $this->testNode->id());
|
Chris@0
|
90 $this->client->post($stats_path, ['form_params' => $post]);
|
Chris@0
|
91 $this->assertText('2 views', 'Node is viewed 2 times.');
|
Chris@0
|
92
|
Chris@0
|
93 // Increase the max age to test that nodes are no longer immediately
|
Chris@0
|
94 // updated, visit the node once more to populate the cache.
|
Chris@0
|
95 $this->config('statistics.settings')->set('display_max_age', 3600)->save();
|
Chris@0
|
96 $this->drupalGet('node/' . $this->testNode->id());
|
Chris@0
|
97 $this->assertText('3 views', 'Node is viewed 3 times.');
|
Chris@0
|
98
|
Chris@0
|
99 $this->client->post($stats_path, ['form_params' => $post]);
|
Chris@0
|
100 $this->drupalGet('node/' . $this->testNode->id());
|
Chris@0
|
101 $this->assertText('3 views', 'Views counter was not updated.');
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * Tests that when a node is deleted, the node counter is deleted too.
|
Chris@0
|
106 */
|
Chris@0
|
107 public function testDeleteNode() {
|
Chris@0
|
108 $this->config('statistics.settings')->set('count_content_views', 1)->save();
|
Chris@0
|
109
|
Chris@0
|
110 $this->drupalGet('node/' . $this->testNode->id());
|
Chris@0
|
111 // Manually calling statistics.php, simulating ajax behavior.
|
Chris@0
|
112 $nid = $this->testNode->id();
|
Chris@0
|
113 $post = ['nid' => $nid];
|
Chris@0
|
114 global $base_url;
|
Chris@0
|
115 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
Chris@0
|
116 $this->client->post($stats_path, ['form_params' => $post]);
|
Chris@0
|
117
|
Chris@0
|
118 $result = db_select('node_counter', 'n')
|
Chris@0
|
119 ->fields('n', ['nid'])
|
Chris@0
|
120 ->condition('n.nid', $this->testNode->id())
|
Chris@0
|
121 ->execute()
|
Chris@0
|
122 ->fetchAssoc();
|
Chris@0
|
123 $this->assertEqual($result['nid'], $this->testNode->id(), 'Verifying that the node counter is incremented.');
|
Chris@0
|
124
|
Chris@0
|
125 $this->testNode->delete();
|
Chris@0
|
126
|
Chris@0
|
127 $result = db_select('node_counter', 'n')
|
Chris@0
|
128 ->fields('n', ['nid'])
|
Chris@0
|
129 ->condition('n.nid', $this->testNode->id())
|
Chris@0
|
130 ->execute()
|
Chris@0
|
131 ->fetchAssoc();
|
Chris@0
|
132 $this->assertFalse($result, 'Verifying that the node counter is deleted.');
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Tests that cron clears day counts and expired access logs.
|
Chris@0
|
137 */
|
Chris@0
|
138 public function testExpiredLogs() {
|
Chris@0
|
139 $this->config('statistics.settings')
|
Chris@0
|
140 ->set('count_content_views', 1)
|
Chris@0
|
141 ->save();
|
Chris@0
|
142 \Drupal::state()->set('statistics.day_timestamp', 8640000);
|
Chris@0
|
143
|
Chris@0
|
144 $this->drupalGet('node/' . $this->testNode->id());
|
Chris@0
|
145 // Manually calling statistics.php, simulating ajax behavior.
|
Chris@0
|
146 $nid = $this->testNode->id();
|
Chris@0
|
147 $post = ['nid' => $nid];
|
Chris@0
|
148 global $base_url;
|
Chris@0
|
149 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
Chris@0
|
150 $this->client->post($stats_path, ['form_params' => $post]);
|
Chris@0
|
151 $this->drupalGet('node/' . $this->testNode->id());
|
Chris@0
|
152 $this->client->post($stats_path, ['form_params' => $post]);
|
Chris@0
|
153 $this->assertText('1 view', 'Node is viewed once.');
|
Chris@0
|
154
|
Chris@0
|
155 // statistics_cron() will subtract
|
Chris@0
|
156 // statistics.settings:accesslog.max_lifetime config from REQUEST_TIME in
|
Chris@0
|
157 // the delete query, so wait two secs here to make sure the access log will
|
Chris@0
|
158 // be flushed for the node just hit.
|
Chris@0
|
159 sleep(2);
|
Chris@0
|
160 $this->cronRun();
|
Chris@0
|
161
|
Chris@0
|
162 $this->drupalGet('admin/reports/pages');
|
Chris@0
|
163 $this->assertNoText('node/' . $this->testNode->id(), 'No hit URL found.');
|
Chris@0
|
164
|
Chris@0
|
165 $result = db_select('node_counter', 'nc')
|
Chris@0
|
166 ->fields('nc', ['daycount'])
|
Chris@0
|
167 ->condition('nid', $this->testNode->id(), '=')
|
Chris@0
|
168 ->execute()
|
Chris@0
|
169 ->fetchField();
|
Chris@0
|
170 $this->assertFalse($result, 'Daycounter is zero.');
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 }
|