5 * Contains \Drupal\Tests\Core\Logger\LoggerChannelTest.
8 namespace Drupal\Tests\Core\Logger;
10 use Drupal\Core\Logger\LoggerChannel;
11 use Drupal\Core\Session\AccountInterface;
12 use Drupal\Tests\UnitTestCase;
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\HttpFoundation\RequestStack;
15 use Psr\Log\LoggerInterface;
16 use Psr\Log\LoggerTrait;
19 * @coversDefaultClass \Drupal\Core\Logger\LoggerChannel
22 class LoggerChannelTest extends UnitTestCase {
25 * Tests LoggerChannel::log().
27 * @param callable $expected
28 * An anonymous function to use with $this->callback() of the logger mock.
29 * The function should check the $context array for expected values.
30 * @param \Symfony\Component\HttpFoundation\Request $request
31 * Will be passed to the channel under test if present.
32 * @param \Drupal\Core\Session\AccountInterface $current_user
33 * Will be passed to the channel under test if present.
35 * @dataProvider providerTestLog
37 * @covers ::setCurrentUser
38 * @covers ::setRequestStack
40 public function testLog(callable $expected, Request $request = NULL, AccountInterface $current_user = NULL) {
41 $channel = new LoggerChannel('test');
42 $message = $this->randomMachineName();
43 $logger = $this->getMock('Psr\Log\LoggerInterface');
44 $logger->expects($this->once())
46 ->with($this->anything(), $message, $this->callback($expected));
47 $channel->addLogger($logger);
49 $requestStack = new RequestStack();
50 $requestStack->push($request);
51 $channel->setRequestStack($requestStack);
54 $channel->setCurrentUser($current_user);
56 $channel->log(rand(0, 7), $message);
60 * Tests LoggerChannel::log() recursion protection.
64 public function testLogRecursionProtection() {
65 $channel = new LoggerChannel('test');
66 $logger = $this->getMock('Psr\Log\LoggerInterface');
67 $logger->expects($this->exactly(LoggerChannel::MAX_CALL_DEPTH))
69 $channel->addLogger($logger);
70 $channel->addLogger(new NaughtyRecursiveLogger($channel));
71 $channel->log(rand(0, 7), $this->randomMachineName());
75 * Tests LoggerChannel::addLoggers().
78 * @covers ::sortLoggers
80 public function testSortLoggers() {
81 $channel = new LoggerChannel($this->randomMachineName());
83 for ($i = 0; $i < 4; $i++) {
84 $logger = $this->getMock('Psr\Log\LoggerInterface');
85 $logger->expects($this->once())
87 ->will($this->returnCallback(function () use ($i, &$index_order) {
88 // Append the $i to the index order, so that we know the order that
89 // loggers got called with.
92 $channel->addLogger($logger, $i);
95 $channel->log(rand(0, 7), $this->randomMachineName());
96 // Ensure that the logger added in the end fired first.
97 $this->assertEquals($index_order, '3210');
101 * Data provider for self::testLog().
103 public function providerTestLog() {
104 $account_mock = $this->getMock('Drupal\Core\Session\AccountInterface');
105 $account_mock->expects($this->exactly(2))
107 ->will($this->returnValue(1));
109 $request_mock = $this->getMock('Symfony\Component\HttpFoundation\Request');
110 $request_mock->expects($this->exactly(2))
111 ->method('getClientIp')
112 ->will($this->returnValue('127.0.0.1'));
113 $request_mock->headers = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
115 // No request or account.
117 function ($context) {
118 return $context['channel'] == 'test' && empty($context['uid']) && empty($context['ip']);
121 // With account but not request. Since the request is not available the
122 // current user should not be used.
124 function ($context) {
125 return $context['uid'] === 0 && empty($context['ip']);
130 // With request but not account.
132 function ($context) {
133 return $context['ip'] === '127.0.0.1' && empty($context['uid']);
137 // Both request and account.
139 function ($context) {
140 return $context['ip'] === '127.0.0.1' && $context['uid'] === 1;
150 class NaughtyRecursiveLogger implements LoggerInterface {
156 public function __construct(LoggerChannel $channel) {
157 $this->channel = $channel;
160 public function log($level, $message, array $context = []) {
161 $this->channel->log(rand(0, 7), $message, $context);