annotate core/modules/dblog/tests/src/Functional/FakeLogEntries.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
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 * - 'uid': Int identifying the user id for the user.
Chris@17 31 * - 'request_uri': String identifying the location of the request.
Chris@17 32 * - 'referer': String identifying the referring url.
Chris@17 33 * - 'ip': String The ip address of the client machine triggering the log
Chris@17 34 * entry.
Chris@17 35 * - 'timestamp': Int unix timestamp.
Chris@17 36 */
Chris@17 37 private function generateLogEntries($count, $options = []) {
Chris@17 38 global $base_root;
Chris@17 39
Chris@17 40 $user = !empty($this->adminUser) ? $this->adminUser : new AnonymousUserSession();
Chris@17 41
Chris@17 42 // Prepare the fields to be logged.
Chris@17 43 $log = $options + [
Chris@17 44 'channel' => 'custom',
Chris@17 45 'message' => 'Dblog test log message',
Chris@17 46 'variables' => [],
Chris@17 47 'severity' => RfcLogLevel::NOTICE,
Chris@17 48 'link' => NULL,
Chris@17 49 'uid' => $user->id(),
Chris@17 50 'request_uri' => $base_root . \Drupal::request()->getRequestUri(),
Chris@17 51 'referer' => \Drupal::request()->server->get('HTTP_REFERER'),
Chris@17 52 'ip' => '127.0.0.1',
Chris@17 53 'timestamp' => REQUEST_TIME,
Chris@17 54 ];
Chris@17 55
Chris@17 56 $logger = $this->container->get('logger.dblog');
Chris@17 57 $message = $log['message'] . ' Entry #';
Chris@17 58 for ($i = 0; $i < $count; $i++) {
Chris@17 59 $log['message'] = $message . $i;
Chris@17 60 $logger->log($log['severity'], $log['message'], $log);
Chris@17 61 }
Chris@17 62 }
Chris@17 63
Chris@17 64 }