function StatisticsAdminTest::testExpiredLogs

Tests that cron clears day counts and expired access logs.

File

drupal/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsAdminTest.php, line 138
Definition of Drupal\statistics\Tests\StatisticsAdminTest.

Class

StatisticsAdminTest
Tests the statistics administration screen.

Namespace

Drupal\statistics\Tests

Code

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

  // Manually calling statistics.php, simulating ajax behavior.
  $nid = $this->test_node->nid;
  $post = http_build_query(array(
    'nid' => $nid,
  ));
  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  );
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  $this->client
    ->post($stats_path, $headers, $post)
    ->send();
  $this
    ->drupalGet('node/' . $this->test_node->nid);
  $this->client
    ->post($stats_path, $headers, $post)
    ->send();
  $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->test_node->nid, 'No hit URL found.');
  $result = db_select('node_counter', 'nc')
    ->fields('nc', array(
    'daycount',
  ))
    ->condition('nid', $this->test_node->nid, '=')
    ->execute()
    ->fetchField();
  $this
    ->assertFalse($result, 'Daycounter is zero.');
}