annotate core/modules/dblog/tests/src/Functional/FakeLogEntries.php @ 5:12f9dff5fda9 tip

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