You are here

public function DbLogTest::testDbLogCron in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/dblog/tests/src/Kernel/DbLogTest.php \Drupal\Tests\dblog\Kernel\DbLogTest::testDbLogCron()
  2. 10 core/modules/dblog/tests/src/Kernel/DbLogTest.php \Drupal\Tests\dblog\Kernel\DbLogTest::testDbLogCron()

Tests that cron correctly applies the database log row limit.

File

core/modules/dblog/tests/src/Kernel/DbLogTest.php, line 38

Class

DbLogTest
Generate events and verify dblog entries.

Namespace

Drupal\Tests\dblog\Kernel

Code

public function testDbLogCron() {
  $row_limit = 100;

  // Generate additional log entries.
  $this
    ->generateLogEntries($row_limit + 10);

  // Verify that the database log row count exceeds the row limit.
  $count = Database::getConnection()
    ->select('watchdog')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertGreaterThan($row_limit, $count, new FormattableMarkup('Dblog row count of @count exceeds row limit of @limit', [
    '@count' => $count,
    '@limit' => $row_limit,
  ]));

  // Get the number of enabled modules. Cron adds a log entry for each module.
  $list = $this->container
    ->get('module_handler')
    ->getImplementations('cron');
  $module_count = count($list);
  $cron_detailed_count = $this
    ->runCron();
  $this
    ->assertEquals($module_count + 2, $cron_detailed_count, new FormattableMarkup('Cron added @count of @expected new log entries', [
    '@count' => $cron_detailed_count,
    '@expected' => $module_count + 2,
  ]));

  // Test disabling of detailed cron logging.
  $this
    ->config('system.cron')
    ->set('logging', 0)
    ->save();
  $cron_count = $this
    ->runCron();
  $this
    ->assertEquals(1, $cron_count, new FormattableMarkup('Cron added @count of @expected new log entries', [
    '@count' => $cron_count,
    '@expected' => 1,
  ]));
}