function StatisticsAdminTest::testExpiredLogs

Same name and namespace in other branches
  1. 9 core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testExpiredLogs()
  2. 8.9.x core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testExpiredLogs()
  3. 11.x 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 159

Class

StatisticsAdminTest
Tests the statistics admin.

Namespace

Drupal\Tests\statistics\Functional

Code

public function testExpiredLogs() : void {
  $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 . '/' . $this->getModulePath('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->assertSession()
    ->pageTextContains('1 view');
  // statistics_cron() will subtract
  // statistics.settings:accesslog.max_lifetime config from
  // \Drupal::time()->getRequestTime() 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();
  // Verify that no hit URL is found.
  $this->drupalGet('admin/reports/pages');
  $this->assertSession()
    ->pageTextNotContains('node/' . $this->testNode
    ->id());
  $result = Database::getConnection()->select('node_counter', 'nc')
    ->fields('nc', [
    'daycount',
  ])
    ->condition('nid', $this->testNode
    ->id(), '=')
    ->execute()
    ->fetchField();
  $this->assertEmpty($result, 'Daycount is zero.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.