Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Logger\LoggerChannelTest
*/
namespace Drupal\Tests\Logger;
use Drupal\Core\Logger\LoggerChannel;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
// @todo Remove once watchdog() is removed.
if (!defined('WATCHDOG_EMERGENCY')) {
define('WATCHDOG_EMERGENCY', 0);
define('WATCHDOG_ALERT', 1);
define('WATCHDOG_CRITICAL', 2);
define('WATCHDOG_WARNING', 4);
define('WATCHDOG_INFO', 6);
define('WATCHDOG_DEBUG', 7);
}
// WATCHDOG_NOTICE is also defined in FormValidatorTest, so check independently.
if (!defined('WATCHDOG_NOTICE')) {
define('WATCHDOG_NOTICE', 5);
}
// WATCHDOG_ERROR is also defined in FormBuilderTest, so check independently.
if (!defined('WATCHDOG_ERROR')) {
define('WATCHDOG_ERROR', 3);
}
/**
* Tests the logger channel.
*
* @see \Drupal\Core\Logger\LoggerChannel
* @coversDefaultClass \Drupal\Core\Logger\LoggerChannel
*
* @group Drupal
* @group Logger
*/
class LoggerChannelTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'Logger channel',
'description' => 'Unit tests for the logger channel object.',
'group' => 'Logger',
);
}
/**
* Tests LoggerChannel::log().
*
* @param callable $expected
* An anonymous function to use with $this->callback() of the logger mock.
* The function should check the $context array for expected values.
* @param \Symfony\Component\HttpFoundation\Request $request
* Will be passed to the channel under test if present.
* @param \Drupal\Core\Session\AccountInterface $current_user
* Will be passed to the channel under test if present.
*
* @dataProvider providerTestLog
* @covers ::log
* @covers ::setCurrentUser
* @covers ::setRequest
*/
public function testLog(callable $expected, Request $request = NULL, AccountInterface $current_user = NULL) {
$channel = new LoggerChannel('test');
$message = $this->randomName();
$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger->expects($this->once())
->method('log')
->with($this->anything(), $message, $this->callback($expected));
$channel->addLogger($logger);
if ($request) {
$channel->setRequest($request);
}
if ($current_user) {
$channel->setCurrentUser($current_user);
}
$channel->log(rand(0, 7), $message);
}
/**
* Tests LoggerChannel::addLoggers().
*
* @covers ::addLogger
* @covers ::sortLoggers
*/
public function testSortLoggers() {
$channel = new LoggerChannel($this->randomName());
$index_order = '';
for ($i = 0; $i < 4; $i++) {
$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger->expects($this->once())
->method('log')
->will($this->returnCallback(function () use ($i, &$index_order) {
// Append the $i to the index order, so that we know the order that
// loggers got called with.
$index_order .= $i;
}));
$channel->addLogger($logger, $i);
}
$channel->log(rand(0, 7), $this->randomName());
// Ensure that the logger added in the end fired first.
$this->assertEquals($index_order, '3210');
}
/**
* Data provider for self::testLog().
*/
public function providerTestLog() {
$account_mock = $this->getMock('Drupal\Core\Session\AccountInterface');
$account_mock->expects($this->exactly(2))
->method('id')
->will($this->returnValue(1));
$request_mock = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request_mock->expects($this->exactly(2))
->method('getClientIp')
->will($this->returnValue('127.0.0.1'));
$request_mock->headers = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
// No request or account.
$cases [] = array(
function ($context) {
return $context['channel'] == 'test' && empty($contex['uid']) && empty($context['ip']);
},
);
// With account but not request.
$cases [] = array(
function ($context) {
return $context['uid'] === 1 && empty($context['ip']);
},
NULL,
$account_mock,
);
// With request but not account.
$cases [] = array(
function ($context) {
return $context['ip'] === '127.0.0.1' && empty($contex['uid']);
},
$request_mock,
);
// Both request and account.
$cases [] = array(
function ($context) {
return $context['ip'] === '127.0.0.1' && $context['uid'] === 1;
},
$request_mock,
$account_mock,
);
return $cases;
}
}