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