comparison core/modules/dblog/tests/src/Kernel/DbLogTest.php @ 17:129ea1e6d783

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