Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\Tests\dblog\Kernel;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\KernelTests\KernelTestBase;
|
Chris@17
|
6 use Drupal\Tests\dblog\Functional\FakeLogEntries;
|
Chris@17
|
7
|
Chris@17
|
8 /**
|
Chris@17
|
9 * Generate events and verify dblog entries.
|
Chris@17
|
10 *
|
Chris@17
|
11 * @group dblog
|
Chris@17
|
12 */
|
Chris@17
|
13 class DbLogTest extends KernelTestBase {
|
Chris@17
|
14
|
Chris@17
|
15 use FakeLogEntries;
|
Chris@17
|
16
|
Chris@17
|
17 /**
|
Chris@17
|
18 * {@inheritdoc}
|
Chris@17
|
19 */
|
Chris@17
|
20 protected static $modules = ['dblog', 'system'];
|
Chris@17
|
21
|
Chris@17
|
22 /**
|
Chris@17
|
23 * {@inheritdoc}
|
Chris@17
|
24 */
|
Chris@17
|
25 protected function setUp() {
|
Chris@17
|
26 parent::setUp();
|
Chris@17
|
27
|
Chris@17
|
28 $this->installSchema('dblog', ['watchdog']);
|
Chris@17
|
29 $this->installSchema('system', ['key_value_expire', 'sequences']);
|
Chris@17
|
30 $this->installConfig(['system']);
|
Chris@17
|
31 }
|
Chris@17
|
32
|
Chris@17
|
33 /**
|
Chris@17
|
34 * Tests that cron correctly applies the database log row limit.
|
Chris@17
|
35 */
|
Chris@17
|
36 public function testDbLogCron() {
|
Chris@17
|
37 $row_limit = 100;
|
Chris@17
|
38 // Generate additional log entries.
|
Chris@17
|
39 $this->generateLogEntries($row_limit + 10);
|
Chris@17
|
40 // Verify that the database log row count exceeds the row limit.
|
Chris@17
|
41 $count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField();
|
Chris@17
|
42 $this->assertGreaterThan($row_limit, $count, format_string('Dblog row count of @count exceeds row limit of @limit', ['@count' => $count, '@limit' => $row_limit]));
|
Chris@17
|
43
|
Chris@17
|
44 // Get the number of enabled modules. Cron adds a log entry for each module.
|
Chris@17
|
45 $list = $this->container->get('module_handler')->getImplementations('cron');
|
Chris@17
|
46 $module_count = count($list);
|
Chris@17
|
47 $cron_detailed_count = $this->runCron();
|
Chris@17
|
48 $this->assertEquals($module_count + 2, $cron_detailed_count, format_string('Cron added @count of @expected new log entries', ['@count' => $cron_detailed_count, '@expected' => $module_count + 2]));
|
Chris@17
|
49
|
Chris@17
|
50 // Test disabling of detailed cron logging.
|
Chris@17
|
51 $this->config('system.cron')->set('logging', 0)->save();
|
Chris@17
|
52 $cron_count = $this->runCron();
|
Chris@17
|
53 $this->assertEquals(1, $cron_count, format_string('Cron added @count of @expected new log entries', ['@count' => $cron_count, '@expected' => 1]));
|
Chris@17
|
54 }
|
Chris@17
|
55
|
Chris@17
|
56 /**
|
Chris@17
|
57 * Runs cron and returns number of new log entries.
|
Chris@17
|
58 *
|
Chris@17
|
59 * @return int
|
Chris@17
|
60 * Number of new watchdog entries.
|
Chris@17
|
61 */
|
Chris@17
|
62 private function runCron() {
|
Chris@17
|
63 // Get last ID to compare against; log entries get deleted, so we can't
|
Chris@17
|
64 // reliably add the number of newly created log entries to the current count
|
Chris@17
|
65 // to measure number of log entries created by cron.
|
Chris@17
|
66 $last_id = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
|
Chris@17
|
67
|
Chris@17
|
68 // Run a cron job.
|
Chris@17
|
69 $this->container->get('cron')->run();
|
Chris@17
|
70
|
Chris@17
|
71 // Get last ID after cron was run.
|
Chris@17
|
72 $current_id = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
|
Chris@17
|
73
|
Chris@17
|
74 return $current_id - $last_id;
|
Chris@17
|
75 }
|
Chris@17
|
76
|
Chris@17
|
77 }
|