Version 1
[yaffs-website] / web / core / modules / statistics / tests / src / Functional / StatisticsAdminTest.php
1 <?php
2
3 namespace Drupal\Tests\statistics\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Tests\Traits\Core\CronRunTrait;
7
8 /**
9  * Tests the statistics admin.
10  *
11  * @group statistics
12  */
13 class StatisticsAdminTest extends BrowserTestBase {
14
15   use CronRunTrait;
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['node', 'statistics'];
23
24   /**
25    * A user that has permission to administer statistics.
26    *
27    * @var \Drupal\user\UserInterface
28    */
29   protected $privilegedUser;
30
31   /**
32    * A page node for which to check content statistics.
33    *
34    * @var \Drupal\node\NodeInterface
35    */
36   protected $testNode;
37
38   /**
39    * The Guzzle HTTP client.
40    *
41    * @var \GuzzleHttp\Client;
42    */
43   protected $client;
44
45   protected function setUp() {
46     parent::setUp();
47
48     // Set the max age to 0 to simplify testing.
49     $this->config('statistics.settings')->set('display_max_age', 0)->save();
50
51     // Create Basic page node type.
52     if ($this->profile != 'standard') {
53       $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
54     }
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();
59   }
60
61   /**
62    * Verifies that the statistics settings page works.
63    */
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.');
67
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.');
73
74     // Hit the node.
75     $this->drupalGet('node/' . $this->testNode->id());
76     // Manually calling statistics.php, simulating ajax behavior.
77     $nid = $this->testNode->id();
78     $post = ['nid' => $nid];
79     global $base_url;
80     $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
81     $this->client->post($stats_path, ['form_params' => $post]);
82
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.');
88
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.');
92
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.');
98
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.');
102   }
103
104   /**
105    * Tests that when a node is deleted, the node counter is deleted too.
106    */
107   public function testDeleteNode() {
108     $this->config('statistics.settings')->set('count_content_views', 1)->save();
109
110     $this->drupalGet('node/' . $this->testNode->id());
111     // Manually calling statistics.php, simulating ajax behavior.
112     $nid = $this->testNode->id();
113     $post = ['nid' => $nid];
114     global $base_url;
115     $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
116     $this->client->post($stats_path, ['form_params' => $post]);
117
118     $result = db_select('node_counter', 'n')
119       ->fields('n', ['nid'])
120       ->condition('n.nid', $this->testNode->id())
121       ->execute()
122       ->fetchAssoc();
123     $this->assertEqual($result['nid'], $this->testNode->id(), 'Verifying that the node counter is incremented.');
124
125     $this->testNode->delete();
126
127     $result = db_select('node_counter', 'n')
128       ->fields('n', ['nid'])
129       ->condition('n.nid', $this->testNode->id())
130       ->execute()
131       ->fetchAssoc();
132     $this->assertFalse($result, 'Verifying that the node counter is deleted.');
133   }
134
135   /**
136    * Tests that cron clears day counts and expired access logs.
137    */
138   public function testExpiredLogs() {
139     $this->config('statistics.settings')
140       ->set('count_content_views', 1)
141       ->save();
142     \Drupal::state()->set('statistics.day_timestamp', 8640000);
143
144     $this->drupalGet('node/' . $this->testNode->id());
145     // Manually calling statistics.php, simulating ajax behavior.
146     $nid = $this->testNode->id();
147     $post = ['nid' => $nid];
148     global $base_url;
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.');
154
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.
159     sleep(2);
160     $this->cronRun();
161
162     $this->drupalGet('admin/reports/pages');
163     $this->assertNoText('node/' . $this->testNode->id(), 'No hit URL found.');
164
165     $result = db_select('node_counter', 'nc')
166       ->fields('nc', ['daycount'])
167       ->condition('nid', $this->testNode->id(), '=')
168       ->execute()
169       ->fetchField();
170     $this->assertFalse($result, 'Daycounter is zero.');
171   }
172
173 }