comparison core/modules/dblog/tests/src/Functional/FakeLogEntries.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents
children 12f9dff5fda9
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
1 <?php
2
3 namespace Drupal\Tests\dblog\Functional;
4
5 use Drupal\Core\Logger\RfcLogLevel;
6 use Drupal\Core\Session\AnonymousUserSession;
7
8 /**
9 * Provides methods to generate log entries.
10 *
11 * This trait is meant to be used only by test classes.
12 */
13 trait FakeLogEntries {
14
15 /**
16 * Generates a number of random database log events.
17 *
18 * @param int $count
19 * Number of watchdog entries to generate.
20 * @param array $options
21 * These options are used to override the defaults for the test.
22 * An associative array containing any of the following keys:
23 * - 'channel': String identifying the log channel to be output to.
24 * If the channel is not set, the default of 'custom' will be used.
25 * - 'message': String containing a message to be output to the log.
26 * A simple default message is used if not provided.
27 * - 'variables': Array of variables that match the message string.
28 * - 'severity': Log severity level as defined in logging_severity_levels.
29 * - 'link': String linking to view the result of the event.
30 * - 'user': String identifying the username.
31 * - 'uid': Int identifying the user id for the user.
32 * - 'request_uri': String identifying the location of the request.
33 * - 'referer': String identifying the referring url.
34 * - 'ip': String The ip address of the client machine triggering the log
35 * entry.
36 * - 'timestamp': Int unix timestamp.
37 */
38 private function generateLogEntries($count, $options = []) {
39 global $base_root;
40
41 $user = !empty($this->adminUser) ? $this->adminUser : new AnonymousUserSession();
42
43 // Prepare the fields to be logged.
44 $log = $options + [
45 'channel' => 'custom',
46 'message' => 'Dblog test log message',
47 'variables' => [],
48 'severity' => RfcLogLevel::NOTICE,
49 'link' => NULL,
50 'user' => $user,
51 'uid' => $user->id(),
52 'request_uri' => $base_root . \Drupal::request()->getRequestUri(),
53 'referer' => \Drupal::request()->server->get('HTTP_REFERER'),
54 'ip' => '127.0.0.1',
55 'timestamp' => REQUEST_TIME,
56 ];
57
58 $logger = $this->container->get('logger.dblog');
59 $message = $log['message'] . ' Entry #';
60 for ($i = 0; $i < $count; $i++) {
61 $log['message'] = $message . $i;
62 $logger->log($log['severity'], $log['message'], $log);
63 }
64 }
65
66 }