Merge "Revert "Add executable rights for executable (bash) files""
[lhc/web/wiklou.git] / tests / phpunit / includes / logging / LogFormatterTest.php
1 <?php
2 /**
3 * @group Database
4 */
5 class LogFormatterTest extends MediaWikiLangTestCase {
6
7 /**
8 * @var User
9 */
10 protected $user;
11
12 /**
13 * @var Title
14 */
15 protected $title;
16
17 /**
18 * @var RequestContext
19 */
20 protected $context;
21
22 protected function setUp() {
23 parent::setUp();
24
25 global $wgLang;
26
27 $this->setMwGlobals( array(
28 'wgLogTypes' => array( 'phpunit' ),
29 'wgLogActionsHandlers' => array( 'phpunit/test' => 'LogFormatter',
30 'phpunit/param' => 'LogFormatter' ),
31 'wgUser' => User::newFromName( 'Testuser' ),
32 'wgExtensionMessagesFiles' => array( 'LogTests' => __DIR__ . '/LogTests.i18n.php' ),
33 ) );
34
35 Language::getLocalisationCache()->recache( $wgLang->getCode() );
36
37 $this->user = User::newFromName( 'Testuser' );
38 $this->title = Title::newMainPage();
39
40 $this->context = new RequestContext();
41 $this->context->setUser( $this->user );
42 $this->context->setTitle( $this->title );
43 $this->context->setLanguage( $wgLang );
44 }
45
46 protected function tearDown() {
47 parent::tearDown();
48
49 global $wgLang;
50 Language::getLocalisationCache()->recache( $wgLang->getCode() );
51 }
52
53 public function newLogEntry( $action, $params ) {
54 $logEntry = new ManualLogEntry( 'phpunit', $action );
55 $logEntry->setPerformer( $this->user );
56 $logEntry->setTarget( $this->title );
57 $logEntry->setComment( 'A very good reason' );
58
59 $logEntry->setParameters( $params );
60
61 return $logEntry;
62 }
63
64 /**
65 * @covers LogFormatter::newFromEntry
66 */
67 public function testNormalLogParams() {
68 $entry = $this->newLogEntry( 'test', array() );
69 $formatter = LogFormatter::newFromEntry( $entry );
70 $formatter->setContext( $this->context );
71
72 $formatter->setShowUserToolLinks( false );
73 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
74 unset( $formatter->parsedParameters );
75
76 $formatter->setShowUserToolLinks( true );
77 $paramsWithTools = $formatter->getMessageParametersForTesting();
78
79 $userLink = Linker::userLink(
80 $this->user->getId(),
81 $this->user->getName()
82 );
83
84 $userTools = Linker::userToolLinksRedContribs(
85 $this->user->getId(),
86 $this->user->getName(),
87 $this->user->getEditCount()
88 );
89
90 $titleLink = Linker::link( $this->title, null, array(), array() );
91
92 // $paramsWithoutTools and $paramsWithTools should be only different
93 // in index 0
94 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
95 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
96
97 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
98 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
99
100 $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
101
102 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
103 }
104
105 /**
106 * @covers LogFormatter::newFromEntry
107 * @covers LogFormatter::getActionText
108 */
109 public function testLogParamsTypeRaw() {
110 $params = array( '4:raw:raw' => Linker::link( $this->title, null, array(), array() ) );
111 $expected = Linker::link( $this->title, null, array(), array() );
112
113 $entry = $this->newLogEntry( 'param', $params );
114 $formatter = LogFormatter::newFromEntry( $entry );
115 $formatter->setContext( $this->context );
116
117 $logParam = $formatter->getActionText();
118
119 $this->assertEquals( $expected, $logParam );
120 }
121
122 /**
123 * @covers LogFormatter::newFromEntry
124 * @covers LogFormatter::getActionText
125 */
126 public function testLogParamsTypeMsg() {
127 $params = array( '4:msg:msg' => 'log-description-phpunit' );
128 $expected = wfMessage( 'log-description-phpunit' )->text();
129
130 $entry = $this->newLogEntry( 'param', $params );
131 $formatter = LogFormatter::newFromEntry( $entry );
132 $formatter->setContext( $this->context );
133
134 $logParam = $formatter->getActionText();
135
136 $this->assertEquals( $expected, $logParam );
137 }
138
139 /**
140 * @covers LogFormatter::newFromEntry
141 * @covers LogFormatter::getActionText
142 */
143 public function testLogParamsTypeMsgContent() {
144 $params = array( '4:msg-content:msgContent' => 'log-description-phpunit' );
145 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
146
147 $entry = $this->newLogEntry( 'param', $params );
148 $formatter = LogFormatter::newFromEntry( $entry );
149 $formatter->setContext( $this->context );
150
151 $logParam = $formatter->getActionText();
152
153 $this->assertEquals( $expected, $logParam );
154 }
155
156 /**
157 * @covers LogFormatter::newFromEntry
158 * @covers LogFormatter::getActionText
159 */
160 public function testLogParamsTypeNumber() {
161 global $wgLang;
162
163 $params = array( '4:number:number' => 123456789 );
164 $expected = $wgLang->formatNum( 123456789 );
165
166 $entry = $this->newLogEntry( 'param', $params );
167 $formatter = LogFormatter::newFromEntry( $entry );
168 $formatter->setContext( $this->context );
169
170 $logParam = $formatter->getActionText();
171
172 $this->assertEquals( $expected, $logParam );
173 }
174
175 /**
176 * @covers LogFormatter::newFromEntry
177 * @covers LogFormatter::getActionText
178 */
179 public function testLogParamsTypeUserLink() {
180 $params = array( '4:user-link:userLink' => $this->user->getName() );
181 $expected = Linker::userLink(
182 $this->user->getId(),
183 $this->user->getName()
184 );
185
186 $entry = $this->newLogEntry( 'param', $params );
187 $formatter = LogFormatter::newFromEntry( $entry );
188 $formatter->setContext( $this->context );
189
190 $logParam = $formatter->getActionText();
191
192 $this->assertEquals( $expected, $logParam );
193 }
194
195 /**
196 * @covers LogFormatter::newFromEntry
197 * @covers LogFormatter::getActionText
198 */
199 public function testLogParamsTypeTitleLink() {
200 $params = array( '4:title-link:titleLink' => $this->title->getText() );
201 $expected = Linker::link( $this->title, null, array(), array() );
202
203 $entry = $this->newLogEntry( 'param', $params );
204 $formatter = LogFormatter::newFromEntry( $entry );
205 $formatter->setContext( $this->context );
206
207 $logParam = $formatter->getActionText();
208
209 $this->assertEquals( $expected, $logParam );
210 }
211
212 /**
213 * @covers LogFormatter::newFromEntry
214 * @covers LogFormatter::getActionText
215 */
216 public function testLogParamsTypePlain() {
217 $params = array( '4:plain:plain' => 'Some plain text' );
218 $expected = 'Some plain text';
219
220 $entry = $this->newLogEntry( 'param', $params );
221 $formatter = LogFormatter::newFromEntry( $entry );
222 $formatter->setContext( $this->context );
223
224 $logParam = $formatter->getActionText();
225
226 $this->assertEquals( $expected, $logParam );
227 }
228
229 /**
230 * @covers LogFormatter::newFromEntry
231 * @covers LogFormatter::getComment
232 */
233 public function testLogComment() {
234 $entry = $this->newLogEntry( 'test', array() );
235 $formatter = LogFormatter::newFromEntry( $entry );
236 $formatter->setContext( $this->context );
237
238 $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
239
240 $this->assertEquals( $comment, $formatter->getComment() );
241 }
242
243 /**
244 * @dataProvider provideApiParamFormatting
245 * @covers LogFormatter::formatParametersForApi
246 * @covers LogFormatter::formatParameterValueForApi
247 */
248 public function testApiParamFormatting( $key, $value, $expected ) {
249 $entry = $this->newLogEntry( 'param', array( $key => $value ) );
250 $formatter = LogFormatter::newFromEntry( $entry );
251 $formatter->setContext( $this->context );
252
253 ApiResult::setIndexedTagName( $expected, 'param' );
254 ApiResult::setArrayType( $expected, 'assoc' );
255
256 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
257 }
258
259 public static function provideApiParamFormatting() {
260 return array(
261 array( 0, 'value', array( 'value' ) ),
262 array( 'named', 'value', array( 'named' => 'value' ) ),
263 array( '::key', 'value', array( 'key' => 'value' ) ),
264 array( '4::key', 'value', array( 'key' => 'value' ) ),
265 array( '4:raw:key', 'value', array( 'key' => 'value' ) ),
266 array( '4:plain:key', 'value', array( 'key' => 'value' ) ),
267 array( '4:bool:key', '1', array( 'key' => true ) ),
268 array( '4:bool:key', '0', array( 'key' => false ) ),
269 array( '4:number:key', '123', array( 'key' => 123 ) ),
270 array( '4:number:key', '123.5', array( 'key' => 123.5 ) ),
271 array( '4:array:key', array(), array( 'key' => array( ApiResult::META_TYPE => 'array' ) ) ),
272 array( '4:assoc:key', array(), array( 'key' => array( ApiResult::META_TYPE => 'assoc' ) ) ),
273 array( '4:kvp:key', array(), array( 'key' => array( ApiResult::META_TYPE => 'kvp' ) ) ),
274 array( '4:timestamp:key', '20150102030405', array( 'key' => '2015-01-02T03:04:05Z' ) ),
275 array( '4:msg:key', 'parentheses', array(
276 'key_key' => 'parentheses',
277 'key_text' => wfMessage( 'parentheses' )->text(),
278 ) ),
279 array( '4:msg-content:key', 'parentheses', array(
280 'key_key' => 'parentheses',
281 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
282 ) ),
283 array( '4:title:key', 'project:foo', array(
284 'key_ns' => NS_PROJECT,
285 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
286 ) ),
287 array( '4:title-link:key', 'project:foo', array(
288 'key_ns' => NS_PROJECT,
289 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
290 ) ),
291 array( '4:user:key', 'foo', array( 'key' => 'Foo' ) ),
292 array( '4:user-link:key', 'foo', array( 'key' => 'Foo' ) ),
293 );
294 }
295 }