You are here

function LoggerPluginTest::testCleanup in Ultimate Cron 8.2

Tests log cleanup of the database logger.

File

tests/src/Kernel/LoggerPluginTest.php, line 47

Class

LoggerPluginTest
Tests the default scheduler plugins.

Namespace

Drupal\Tests\ultimate_cron\Kernel

Code

function testCleanup() {
  $this
    ->installSchema('ultimate_cron', [
    'ultimate_cron_log',
    'ultimate_cron_lock',
  ]);
  \Drupal::service('ultimate_cron.discovery')
    ->discoverCronJobs();
  $job = CronJob::load('ultimate_cron_logger_test_cron');
  $job
    ->setConfiguration('logger', [
    'retain' => 10,
  ]);
  $job
    ->save();

  // Run the job 12 times.
  for ($i = 0; $i < 12; $i++) {
    $job
      ->getPlugin('launcher')
      ->launch($job);
  }

  // There are 12 run log entries and one from the modified job.
  $log_entries = $job
    ->getLogEntries(ULTIMATE_CRON_LOG_TYPE_ALL, 15);
  $this
    ->assertCount(13, $log_entries);

  // Run cleanup.
  ultimate_cron_cron();

  // There should be exactly 10 log entries now.
  $log_entries = $job
    ->getLogEntries(ULTIMATE_CRON_LOG_TYPE_ALL, 15);
  $this
    ->assertCount(10, $log_entries);

  // Switch to expire-based cleanup.
  $job
    ->setConfiguration('logger', [
    'expire' => 60,
    'method' => DatabaseLogger::CLEANUP_METHOD_EXPIRE,
  ]);
  $job
    ->save();
  $ids = array_slice(array_keys($log_entries), 5);

  // Date back 5 log entries.
  \Drupal::database()
    ->update('ultimate_cron_log')
    ->expression('start_time', 'start_time - 65')
    ->condition('lid', $ids, 'IN')
    ->execute();

  // Run cleanup.
  ultimate_cron_cron();

  // There should be exactly 6 log entries now, as saving caused another
  // modified entry to be saved.
  $log_entries = $job
    ->getLogEntries(ULTIMATE_CRON_LOG_TYPE_ALL, 15);
  $this
    ->assertCount(6, $log_entries);
}