View source
<?php
namespace Drupal\Tests\dblog\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\dblog\Functional\FakeLogEntries;
class DbLogTest extends KernelTestBase {
use FakeLogEntries;
protected static $modules = [
'dblog',
'system',
];
protected function setUp() : void {
parent::setUp();
$this
->installSchema('dblog', [
'watchdog',
]);
$this
->installSchema('system', [
'sequences',
]);
$this
->installConfig([
'system',
]);
}
public function testDbLogCron() {
$row_limit = 100;
$this
->generateLogEntries($row_limit + 10);
$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,
]));
$implementation_count = 0;
\Drupal::moduleHandler()
->invokeAllWith('cron', function (callable $hook, string $module) use (&$implementation_count) {
$implementation_count++;
});
$cron_detailed_count = $this
->runCron();
$this
->assertEquals($implementation_count + 2, $cron_detailed_count, new FormattableMarkup('Cron added @count of @expected new log entries', [
'@count' => $cron_detailed_count,
'@expected' => $implementation_count + 2,
]));
$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,
]));
}
private function runCron() {
$connection = Database::getConnection();
$query = $connection
->select('watchdog');
$query
->addExpression('MAX([wid])');
$last_id = $query
->execute()
->fetchField();
$this->container
->get('cron')
->run();
$query = $connection
->select('watchdog');
$query
->addExpression('MAX([wid])');
$current_id = $query
->execute()
->fetchField();
return $current_id - $last_id;
}
}