annotate core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php @ 19:fa3358dc1485 tip

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