DbLogTest.php in Drupal 9
File
core/modules/dblog/tests/src/Kernel/DbLogTest.php
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,
]));
$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,
]));
$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;
}
}
Classes
Name |
Description |
DbLogTest |
Generate events and verify dblog entries. |