StatisticsAdminTest.php 5.9 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\statistics\Functional;
4

5 6
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
7 8

/**
9 10 11
 * Tests the statistics admin.
 *
 * @group statistics
12
 */
13 14 15
class StatisticsAdminTest extends BrowserTestBase {

  use CronRunTrait;
16

17 18 19 20 21
  /**
   * Modules to enable.
   *
   * @var array
   */
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 40
  /**
   * The Guzzle HTTP client.
   *
41
   * @var \GuzzleHttp\Client
42 43 44
   */
  protected $client;

45
  protected function setUp() {
46
    parent::setUp();
47

48 49 50
    // Set the max age to 0 to simplify testing.
    $this->config('statistics.settings')->set('display_max_age', 0)->save();

51 52
    // Create Basic page node type.
    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 63
  }

  /**
   * 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.');
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 84

    // Hit the node again (the counter is incremented after the hit, so
    // "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 94 95 96 97 98

    // Increase the max age to test that nodes are no longer immediately
    // updated, visit the node once more to populate the cache.
    $this->config('statistics.settings')->set('display_max_age', 3600)->save();
    $this->drupalGet('node/' . $this->testNode->id());
    $this->assertText('3 views', 'Node is viewed 3 times.');

99
    $this->client->post($stats_path, ['form_params' => $post]);
100 101
    $this->drupalGet('node/' . $this->testNode->id());
    $this->assertText('3 views', 'Views counter was not updated.');
102 103 104 105 106
  }

  /**
   * 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();
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 122
      ->execute()
      ->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 131 132 133 134 135 136 137
      ->execute()
      ->fetchAssoc();
    $this->assertFalse($result, 'Verifying that the node counter is deleted.');
  }

  /**
   * Tests that cron clears day counts and expired access logs.
   */
138
  public function testExpiredLogs() {
139
    $this->config('statistics.settings')
140 141
      ->set('count_content_views', 1)
      ->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 156 157 158
    // statistics_cron() will subtract
    // statistics.settings:accesslog.max_lifetime config from REQUEST_TIME in
    // the delete query, so wait two secs here to make sure the access log will
    // be flushed for the node just hit.
159 160 161 162
    sleep(2);
    $this->cronRun();

    $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 169
      ->execute()
      ->fetchField();
170
    $this->assertFalse($result, 'Daycounter is zero.');
171
  }
172

173
}