You are here

public function StatisticsAdminTest::testExpiredLogs in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testExpiredLogs()

Tests that cron clears day counts and expired access logs.

File

core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php, line 149

Class

StatisticsAdminTest
Tests the statistics admin.

Namespace

Drupal\Tests\statistics\Functional

Code

public function testExpiredLogs() {
  $this
    ->config('statistics.settings')
    ->set('count_content_views', 1)
    ->save();
  \Drupal::state()
    ->set('statistics.day_timestamp', 8640000);
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());

  // Manually calling statistics.php, simulating ajax behavior.
  $nid = $this->testNode
    ->id();
  $post = [
    'nid' => $nid,
  ];
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  $this->client
    ->post($stats_path, [
    'form_params' => $post,
  ]);
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this->client
    ->post($stats_path, [
    'form_params' => $post,
  ]);
  $this
    ->assertText('1 view', 'Node is viewed once.');

  // 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.
  sleep(2);
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/reports/pages');
  $this
    ->assertNoText('node/' . $this->testNode
    ->id(), 'No hit URL found.');
  $result = Database::getConnection()
    ->select('node_counter', 'nc')
    ->fields('nc', [
    'daycount',
  ])
    ->condition('nid', $this->testNode
    ->id(), '=')
    ->execute()
    ->fetchField();
  $this
    ->assertEmpty($result, 'Daycounter is zero.');
}