d2b283adb9b4eba5c8b2b83945189210269f80c0
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Logger / LoggerChannelTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Logger\LoggerChannelTest.
6  */
7
8 namespace Drupal\Tests\Core\Logger;
9
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;
17
18 /**
19  * @coversDefaultClass \Drupal\Core\Logger\LoggerChannel
20  * @group Logger
21  */
22 class LoggerChannelTest extends UnitTestCase {
23
24   /**
25    * Tests LoggerChannel::log().
26    *
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.
34    *
35    * @dataProvider providerTestLog
36    * @covers ::log
37    * @covers ::setCurrentUser
38    * @covers ::setRequestStack
39    */
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())
45       ->method('log')
46       ->with($this->anything(), $message, $this->callback($expected));
47     $channel->addLogger($logger);
48     if ($request) {
49       $requestStack = new RequestStack();
50       $requestStack->push($request);
51       $channel->setRequestStack($requestStack);
52     }
53     if ($current_user) {
54       $channel->setCurrentUser($current_user);
55     }
56     $channel->log(rand(0, 7), $message);
57   }
58
59   /**
60    * Tests LoggerChannel::log() recursion protection.
61    *
62    * @covers ::log
63    */
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))
68       ->method('log');
69     $channel->addLogger($logger);
70     $channel->addLogger(new NaughtyRecursiveLogger($channel));
71     $channel->log(rand(0, 7), $this->randomMachineName());
72   }
73
74   /**
75    * Tests LoggerChannel::addLoggers().
76    *
77    * @covers ::addLogger
78    * @covers ::sortLoggers
79    */
80   public function testSortLoggers() {
81     $channel = new LoggerChannel($this->randomMachineName());
82     $index_order = '';
83     for ($i = 0; $i < 4; $i++) {
84       $logger = $this->getMock('Psr\Log\LoggerInterface');
85       $logger->expects($this->once())
86         ->method('log')
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.
90           $index_order .= $i;
91         }));
92       $channel->addLogger($logger, $i);
93     }
94
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');
98   }
99
100   /**
101    * Data provider for self::testLog().
102    */
103   public function providerTestLog() {
104     $account_mock = $this->getMock('Drupal\Core\Session\AccountInterface');
105     $account_mock->expects($this->exactly(2))
106       ->method('id')
107       ->will($this->returnValue(1));
108
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');
114
115     // No request or account.
116     $cases [] = [
117       function ($context) {
118         return $context['channel'] == 'test' && empty($context['uid']) && empty($context['ip']);
119       },
120     ];
121     // With account but not request. Since the request is not available the
122     // current user should not be used.
123     $cases [] = [
124       function ($context) {
125         return $context['uid'] === 0 && empty($context['ip']);
126       },
127       NULL,
128       $account_mock,
129     ];
130     // With request but not account.
131     $cases [] = [
132       function ($context) {
133         return $context['ip'] === '127.0.0.1' && empty($context['uid']);
134       },
135       $request_mock,
136     ];
137     // Both request and account.
138     $cases [] = [
139       function ($context) {
140         return $context['ip'] === '127.0.0.1' && $context['uid'] === 1;
141       },
142       $request_mock,
143       $account_mock,
144     ];
145     return $cases;
146   }
147
148 }
149
150 class NaughtyRecursiveLogger implements LoggerInterface {
151   use LoggerTrait;
152
153   protected $channel;
154   protected $message;
155
156   public function __construct(LoggerChannel $channel) {
157     $this->channel = $channel;
158   }
159
160   public function log($level, $message, array $context = []) {
161     $this->channel->log(rand(0, 7), $message, $context);
162   }
163
164 }