You are here

function StatisticsAdminTest::testExpiredLogs in Zircon Profile 8

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

Tests that cron clears day counts and expired access logs.

File

core/modules/statistics/src/Tests/StatisticsAdminTest.php, line 141
Contains \Drupal\statistics\Tests\StatisticsAdminTest.

Class

StatisticsAdminTest
Tests the statistics admin.

Namespace

Drupal\statistics\Tests

Code

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 = array(
    'nid' => $nid,
  );
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  $this->client
    ->post($stats_path, array(
    'form_params' => $post,
  ));
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this->client
    ->post($stats_path, array(
    '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 = db_select('node_counter', 'nc')
    ->fields('nc', array(
    'daycount',
  ))
    ->condition('nid', $this->testNode
    ->id(), '=')
    ->execute()
    ->fetchField();
  $this
    ->assertFalse($result, 'Daycounter is zero.');
}