Mercurial > hg > isophonics-drupal-site
comparison core/modules/dblog/tests/src/Functional/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 | 4c8ae668cc8c |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
6 use Drupal\Component\Utility\Unicode; | 6 use Drupal\Component\Utility\Unicode; |
7 use Drupal\Core\Logger\RfcLogLevel; | 7 use Drupal\Core\Logger\RfcLogLevel; |
8 use Drupal\Core\Url; | 8 use Drupal\Core\Url; |
9 use Drupal\dblog\Controller\DbLogController; | 9 use Drupal\dblog\Controller\DbLogController; |
10 use Drupal\Tests\BrowserTestBase; | 10 use Drupal\Tests\BrowserTestBase; |
11 use Drupal\Tests\Traits\Core\CronRunTrait; | |
12 | 11 |
13 /** | 12 /** |
14 * Generate events and verify dblog entries; verify user access to log reports | 13 * Generate events and verify dblog entries; verify user access to log reports |
15 * based on permissions. | 14 * based on permissions. |
16 * | 15 * |
17 * @group dblog | 16 * @group dblog |
18 */ | 17 */ |
19 class DbLogTest extends BrowserTestBase { | 18 class DbLogTest extends BrowserTestBase { |
20 use CronRunTrait; | 19 use FakeLogEntries; |
21 | 20 |
22 /** | 21 /** |
23 * Modules to enable. | 22 * Modules to enable. |
24 * | 23 * |
25 * @var array | 24 * @var array |
64 // Log in the admin user. | 63 // Log in the admin user. |
65 $this->drupalLogin($this->adminUser); | 64 $this->drupalLogin($this->adminUser); |
66 | 65 |
67 $row_limit = 100; | 66 $row_limit = 100; |
68 $this->verifyRowLimit($row_limit); | 67 $this->verifyRowLimit($row_limit); |
69 $this->verifyCron($row_limit); | |
70 $this->verifyEvents(); | 68 $this->verifyEvents(); |
71 $this->verifyReports(); | 69 $this->verifyReports(); |
72 $this->verifyBreadcrumbs(); | 70 $this->verifyBreadcrumbs(); |
73 $this->verifyLinkEscaping(); | 71 $this->verifyLinkEscaping(); |
74 // Verify the overview table sorting. | 72 // Verify the overview table sorting. |
132 $this->assertResponse(200); | 130 $this->assertResponse(200); |
133 | 131 |
134 // Check row limit variable. | 132 // Check row limit variable. |
135 $current_limit = $this->config('dblog.settings')->get('row_limit'); | 133 $current_limit = $this->config('dblog.settings')->get('row_limit'); |
136 $this->assertTrue($current_limit == $row_limit, format_string('[Cache] Row limit variable of @count equals row limit of @limit', ['@count' => $current_limit, '@limit' => $row_limit])); | 134 $this->assertTrue($current_limit == $row_limit, format_string('[Cache] Row limit variable of @count equals row limit of @limit', ['@count' => $current_limit, '@limit' => $row_limit])); |
137 } | |
138 | |
139 /** | |
140 * Verifies that cron correctly applies the database log row limit. | |
141 * | |
142 * @param int $row_limit | |
143 * The row limit. | |
144 */ | |
145 private function verifyCron($row_limit) { | |
146 // Generate additional log entries. | |
147 $this->generateLogEntries($row_limit + 10); | |
148 // Verify that the database log row count exceeds the row limit. | |
149 $count = db_query('SELECT COUNT(wid) FROM {watchdog}')->fetchField(); | |
150 $this->assertTrue($count > $row_limit, format_string('Dblog row count of @count exceeds row limit of @limit', ['@count' => $count, '@limit' => $row_limit])); | |
151 | |
152 // Get the number of enabled modules. Cron adds a log entry for each module. | |
153 $list = \Drupal::moduleHandler()->getImplementations('cron'); | |
154 $module_count = count($list); | |
155 $cron_detailed_count = $this->runCron(); | |
156 $this->assertTrue($cron_detailed_count == $module_count + 2, format_string('Cron added @count of @expected new log entries', ['@count' => $cron_detailed_count, '@expected' => $module_count + 2])); | |
157 | |
158 // Test disabling of detailed cron logging. | |
159 $this->config('system.cron')->set('logging', 0)->save(); | |
160 $cron_count = $this->runCron(); | |
161 $this->assertTrue($cron_count = 1, format_string('Cron added @count of @expected new log entries', ['@count' => $cron_count, '@expected' => 1])); | |
162 } | |
163 | |
164 /** | |
165 * Runs cron and returns number of new log entries. | |
166 * | |
167 * @return int | |
168 * Number of new watchdog entries. | |
169 */ | |
170 private function runCron() { | |
171 // Get last ID to compare against; log entries get deleted, so we can't | |
172 // reliably add the number of newly created log entries to the current count | |
173 // to measure number of log entries created by cron. | |
174 $last_id = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); | |
175 | |
176 // Run a cron job. | |
177 $this->cronRun(); | |
178 | |
179 // Get last ID after cron was run. | |
180 $current_id = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField(); | |
181 | |
182 return $current_id - $last_id; | |
183 } | |
184 | |
185 /** | |
186 * Generates a number of random database log events. | |
187 * | |
188 * @param int $count | |
189 * Number of watchdog entries to generate. | |
190 * @param array $options | |
191 * These options are used to override the defaults for the test. | |
192 * An associative array containing any of the following keys: | |
193 * - 'channel': String identifying the log channel to be output to. | |
194 * If the channel is not set, the default of 'custom' will be used. | |
195 * - 'message': String containing a message to be output to the log. | |
196 * A simple default message is used if not provided. | |
197 * - 'variables': Array of variables that match the message string. | |
198 * - 'severity': Log severity level as defined in logging_severity_levels. | |
199 * - 'link': String linking to view the result of the event. | |
200 * - 'user': String identifying the username. | |
201 * - 'uid': Int identifying the user id for the user. | |
202 * - 'request_uri': String identifying the location of the request. | |
203 * - 'referer': String identifying the referring url. | |
204 * - 'ip': String The ip address of the client machine triggering the log | |
205 * entry. | |
206 * - 'timestamp': Int unix timestamp. | |
207 */ | |
208 private function generateLogEntries($count, $options = []) { | |
209 global $base_root; | |
210 | |
211 // Prepare the fields to be logged | |
212 $log = $options + [ | |
213 'channel' => 'custom', | |
214 'message' => 'Dblog test log message', | |
215 'variables' => [], | |
216 'severity' => RfcLogLevel::NOTICE, | |
217 'link' => NULL, | |
218 'user' => $this->adminUser, | |
219 'uid' => $this->adminUser->id(), | |
220 'request_uri' => $base_root . \Drupal::request()->getRequestUri(), | |
221 'referer' => \Drupal::request()->server->get('HTTP_REFERER'), | |
222 'ip' => '127.0.0.1', | |
223 'timestamp' => REQUEST_TIME, | |
224 ]; | |
225 | |
226 $logger = $this->container->get('logger.dblog'); | |
227 $message = $log['message'] . ' Entry #'; | |
228 for ($i = 0; $i < $count; $i++) { | |
229 $log['message'] = $message . $i; | |
230 $logger->log($log['severity'], $log['message'], $log); | |
231 } | |
232 } | 135 } |
233 | 136 |
234 /** | 137 /** |
235 * Clear the entry logs by clicking on 'Clear log messages' button. | 138 * Clear the entry logs by clicking on 'Clear log messages' button. |
236 */ | 139 */ |
430 // Found link with the message text. | 333 // Found link with the message text. |
431 $links = array_shift($links); | 334 $links = array_shift($links); |
432 $value = $links->getAttribute('href'); | 335 $value = $links->getAttribute('href'); |
433 | 336 |
434 // Extract link to details page. | 337 // Extract link to details page. |
435 $link = Unicode::substr($value, strpos($value, 'admin/reports/dblog/event/')); | 338 $link = mb_substr($value, strpos($value, 'admin/reports/dblog/event/')); |
436 $this->drupalGet($link); | 339 $this->drupalGet($link); |
437 // Check for full message text on the details page. | 340 // Check for full message text on the details page. |
438 $this->assertRaw($message, 'DBLog event details was found: [delete user]'); | 341 $this->assertRaw($message, 'DBLog event details was found: [delete user]'); |
439 } | 342 } |
440 $this->assertTrue($link, 'DBLog event was recorded: [delete user]'); | 343 $this->assertTrue($link, 'DBLog event was recorded: [delete user]'); |