You are here

public function StatisticsAdminTest::testExpiredLogs in Drupal 9

Same name and namespace in other branches
  1. 8 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 151

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 . '/' . $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 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();

  // 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, 'Daycounter is zero.');
}