Declare LogFormatter::$parsedParameters property
[lhc/web/wiklou.git] / tests / phpunit / includes / logging / LogFormatterTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class LogFormatterTest extends MediaWikiLangTestCase {
7 private static $oldExtMsgFiles;
8
9 /**
10 * @var User
11 */
12 protected $user;
13
14 /**
15 * @var Title
16 */
17 protected $title;
18
19 /**
20 * @var RequestContext
21 */
22 protected $context;
23
24 /**
25 * @var Title
26 */
27 protected $target;
28
29 /**
30 * @var string
31 */
32 protected $user_comment;
33
34 public static function setUpBeforeClass() {
35 parent::setUpBeforeClass();
36
37 global $wgExtensionMessagesFiles;
38 self::$oldExtMsgFiles = $wgExtensionMessagesFiles;
39 $wgExtensionMessagesFiles['LogTests'] = __DIR__ . '/LogTests.i18n.php';
40 Language::getLocalisationCache()->recache( 'en' );
41 }
42
43 public static function tearDownAfterClass() {
44 global $wgExtensionMessagesFiles;
45 $wgExtensionMessagesFiles = self::$oldExtMsgFiles;
46 Language::getLocalisationCache()->recache( 'en' );
47
48 parent::tearDownAfterClass();
49 }
50
51 protected function setUp() {
52 parent::setUp();
53
54 $this->setMwGlobals( [
55 'wgLogTypes' => [ 'phpunit' ],
56 'wgLogActionsHandlers' => [ 'phpunit/test' => LogFormatter::class,
57 'phpunit/param' => LogFormatter::class ],
58 'wgUser' => User::newFromName( 'Testuser' ),
59 ] );
60
61 $this->user = User::newFromName( 'Testuser' );
62 $this->title = Title::newFromText( 'SomeTitle' );
63 $this->target = Title::newFromText( 'TestTarget' );
64
65 $this->context = new RequestContext();
66 $this->context->setUser( $this->user );
67 $this->context->setTitle( $this->title );
68 $this->context->setLanguage( RequestContext::getMain()->getLanguage() );
69
70 $this->user_comment = '<User comment about action>';
71 }
72
73 public function newLogEntry( $action, $params ) {
74 $logEntry = new ManualLogEntry( 'phpunit', $action );
75 $logEntry->setPerformer( $this->user );
76 $logEntry->setTarget( $this->title );
77 $logEntry->setComment( 'A very good reason' );
78
79 $logEntry->setParameters( $params );
80
81 return $logEntry;
82 }
83
84 /**
85 * @covers LogFormatter::newFromEntry
86 */
87 public function testNormalLogParams() {
88 $entry = $this->newLogEntry( 'test', [] );
89 $formatter = LogFormatter::newFromEntry( $entry );
90 $formatter->setContext( $this->context );
91
92 $formatter->setShowUserToolLinks( false );
93 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
94
95 $formatter2 = LogFormatter::newFromEntry( $entry );
96 $formatter2->setContext( $this->context );
97 $formatter2->setShowUserToolLinks( true );
98 $paramsWithTools = $formatter2->getMessageParametersForTesting();
99
100 $userLink = Linker::userLink(
101 $this->user->getId(),
102 $this->user->getName()
103 );
104
105 $userTools = Linker::userToolLinksRedContribs(
106 $this->user->getId(),
107 $this->user->getName(),
108 $this->user->getEditCount()
109 );
110
111 $titleLink = Linker::link( $this->title, null, [], [] );
112
113 // $paramsWithoutTools and $paramsWithTools should be only different
114 // in index 0
115 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
116 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
117
118 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
119 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
120
121 $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
122
123 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
124 }
125
126 /**
127 * @covers LogFormatter::newFromEntry
128 * @covers LogFormatter::getActionText
129 */
130 public function testLogParamsTypeRaw() {
131 $params = [ '4:raw:raw' => Linker::link( $this->title, null, [], [] ) ];
132 $expected = Linker::link( $this->title, null, [], [] );
133
134 $entry = $this->newLogEntry( 'param', $params );
135 $formatter = LogFormatter::newFromEntry( $entry );
136 $formatter->setContext( $this->context );
137
138 $logParam = $formatter->getActionText();
139
140 $this->assertEquals( $expected, $logParam );
141 }
142
143 /**
144 * @covers LogFormatter::newFromEntry
145 * @covers LogFormatter::getActionText
146 */
147 public function testLogParamsTypeMsg() {
148 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
149 $expected = wfMessage( 'log-description-phpunit' )->text();
150
151 $entry = $this->newLogEntry( 'param', $params );
152 $formatter = LogFormatter::newFromEntry( $entry );
153 $formatter->setContext( $this->context );
154
155 $logParam = $formatter->getActionText();
156
157 $this->assertEquals( $expected, $logParam );
158 }
159
160 /**
161 * @covers LogFormatter::newFromEntry
162 * @covers LogFormatter::getActionText
163 */
164 public function testLogParamsTypeMsgContent() {
165 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
166 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
167
168 $entry = $this->newLogEntry( 'param', $params );
169 $formatter = LogFormatter::newFromEntry( $entry );
170 $formatter->setContext( $this->context );
171
172 $logParam = $formatter->getActionText();
173
174 $this->assertEquals( $expected, $logParam );
175 }
176
177 /**
178 * @covers LogFormatter::newFromEntry
179 * @covers LogFormatter::getActionText
180 */
181 public function testLogParamsTypeNumber() {
182 global $wgLang;
183
184 $params = [ '4:number:number' => 123456789 ];
185 $expected = $wgLang->formatNum( 123456789 );
186
187 $entry = $this->newLogEntry( 'param', $params );
188 $formatter = LogFormatter::newFromEntry( $entry );
189 $formatter->setContext( $this->context );
190
191 $logParam = $formatter->getActionText();
192
193 $this->assertEquals( $expected, $logParam );
194 }
195
196 /**
197 * @covers LogFormatter::newFromEntry
198 * @covers LogFormatter::getActionText
199 */
200 public function testLogParamsTypeUserLink() {
201 $params = [ '4:user-link:userLink' => $this->user->getName() ];
202 $expected = Linker::userLink(
203 $this->user->getId(),
204 $this->user->getName()
205 );
206
207 $entry = $this->newLogEntry( 'param', $params );
208 $formatter = LogFormatter::newFromEntry( $entry );
209 $formatter->setContext( $this->context );
210
211 $logParam = $formatter->getActionText();
212
213 $this->assertEquals( $expected, $logParam );
214 }
215
216 /**
217 * @covers LogFormatter::newFromEntry
218 * @covers LogFormatter::getActionText
219 */
220 public function testLogParamsTypeTitleLink() {
221 $params = [ '4:title-link:titleLink' => $this->title->getText() ];
222 $expected = Linker::link( $this->title, null, [], [] );
223
224 $entry = $this->newLogEntry( 'param', $params );
225 $formatter = LogFormatter::newFromEntry( $entry );
226 $formatter->setContext( $this->context );
227
228 $logParam = $formatter->getActionText();
229
230 $this->assertEquals( $expected, $logParam );
231 }
232
233 /**
234 * @covers LogFormatter::newFromEntry
235 * @covers LogFormatter::getActionText
236 */
237 public function testLogParamsTypePlain() {
238 $params = [ '4:plain:plain' => 'Some plain text' ];
239 $expected = 'Some plain text';
240
241 $entry = $this->newLogEntry( 'param', $params );
242 $formatter = LogFormatter::newFromEntry( $entry );
243 $formatter->setContext( $this->context );
244
245 $logParam = $formatter->getActionText();
246
247 $this->assertEquals( $expected, $logParam );
248 }
249
250 /**
251 * @covers LogFormatter::newFromEntry
252 * @covers LogFormatter::getComment
253 */
254 public function testLogComment() {
255 $entry = $this->newLogEntry( 'test', [] );
256 $formatter = LogFormatter::newFromEntry( $entry );
257 $formatter->setContext( $this->context );
258
259 $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
260
261 $this->assertEquals( $comment, $formatter->getComment() );
262 }
263
264 /**
265 * @dataProvider provideApiParamFormatting
266 * @covers LogFormatter::formatParametersForApi
267 * @covers LogFormatter::formatParameterValueForApi
268 */
269 public function testApiParamFormatting( $key, $value, $expected ) {
270 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
271 $formatter = LogFormatter::newFromEntry( $entry );
272 $formatter->setContext( $this->context );
273
274 ApiResult::setIndexedTagName( $expected, 'param' );
275 ApiResult::setArrayType( $expected, 'assoc' );
276
277 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
278 }
279
280 public static function provideApiParamFormatting() {
281 return [
282 [ 0, 'value', [ 'value' ] ],
283 [ 'named', 'value', [ 'named' => 'value' ] ],
284 [ '::key', 'value', [ 'key' => 'value' ] ],
285 [ '4::key', 'value', [ 'key' => 'value' ] ],
286 [ '4:raw:key', 'value', [ 'key' => 'value' ] ],
287 [ '4:plain:key', 'value', [ 'key' => 'value' ] ],
288 [ '4:bool:key', '1', [ 'key' => true ] ],
289 [ '4:bool:key', '0', [ 'key' => false ] ],
290 [ '4:number:key', '123', [ 'key' => 123 ] ],
291 [ '4:number:key', '123.5', [ 'key' => 123.5 ] ],
292 [ '4:array:key', [], [ 'key' => [ ApiResult::META_TYPE => 'array' ] ] ],
293 [ '4:assoc:key', [], [ 'key' => [ ApiResult::META_TYPE => 'assoc' ] ] ],
294 [ '4:kvp:key', [], [ 'key' => [ ApiResult::META_TYPE => 'kvp' ] ] ],
295 [ '4:timestamp:key', '20150102030405', [ 'key' => '2015-01-02T03:04:05Z' ] ],
296 [ '4:msg:key', 'parentheses', [
297 'key_key' => 'parentheses',
298 'key_text' => wfMessage( 'parentheses' )->text(),
299 ] ],
300 [ '4:msg-content:key', 'parentheses', [
301 'key_key' => 'parentheses',
302 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
303 ] ],
304 [ '4:title:key', 'project:foo', [
305 'key_ns' => NS_PROJECT,
306 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
307 ] ],
308 [ '4:title-link:key', 'project:foo', [
309 'key_ns' => NS_PROJECT,
310 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
311 ] ],
312 [ '4:title-link:key', '<invalid>', [
313 'key_ns' => NS_SPECIAL,
314 'key_title' => SpecialPage::getTitleFor( 'Badtitle', '<invalid>' )->getFullText(),
315 ] ],
316 [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
317 [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
318 ];
319 }
320
321 /**
322 * The testIrcMsgForAction* tests are supposed to cover the hacky
323 * LogFormatter::getIRCActionText / T36508
324 *
325 * Third parties bots listen to those messages. They are clever enough
326 * to fetch the i18n messages from the wiki and then analyze the IRC feed
327 * to reverse engineer the $1, $2 messages.
328 * One thing bots can not detect is when MediaWiki change the meaning of
329 * a message like what happened when we deployed 1.19. $1 became the user
330 * performing the action which broke basically all bots around.
331 *
332 * Should cover the following log actions (which are most commonly used by bots):
333 * - block/block
334 * - block/unblock
335 * - block/reblock
336 * - delete/delete
337 * - delete/restore
338 * - newusers/create
339 * - newusers/create2
340 * - newusers/autocreate
341 * - move/move
342 * - move/move_redir
343 * - protect/protect
344 * - protect/modifyprotect
345 * - protect/unprotect
346 * - protect/move_prot
347 * - upload/upload
348 * - merge/merge
349 * - import/upload
350 * - import/interwiki
351 *
352 * As well as the following Auto Edit Summaries:
353 * - blank
354 * - replace
355 * - rollback
356 * - undo
357 */
358
359 /**
360 * @covers LogFormatter::getIRCActionComment
361 * @covers LogFormatter::getIRCActionText
362 */
363 public function testIrcMsgForLogTypeBlock() {
364 $sep = $this->context->msg( 'colon-separator' )->text();
365
366 # block/block
367 $this->assertIRCComment(
368 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
369 . $sep . $this->user_comment,
370 'block', 'block',
371 [
372 '5::duration' => 'duration',
373 '6::flags' => 'flags',
374 ],
375 $this->user_comment
376 );
377 # block/block - legacy
378 $this->assertIRCComment(
379 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
380 . $sep . $this->user_comment,
381 'block', 'block',
382 [
383 'duration',
384 'flags',
385 ],
386 $this->user_comment,
387 '',
388 true
389 );
390 # block/unblock
391 $this->assertIRCComment(
392 $this->context->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
393 'block', 'unblock',
394 [],
395 $this->user_comment
396 );
397 # block/reblock
398 $this->assertIRCComment(
399 $this->context->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
400 . $sep . $this->user_comment,
401 'block', 'reblock',
402 [
403 '5::duration' => 'duration',
404 '6::flags' => 'flags',
405 ],
406 $this->user_comment
407 );
408 }
409
410 /**
411 * @covers LogFormatter::getIRCActionComment
412 * @covers LogFormatter::getIRCActionText
413 */
414 public function testIrcMsgForLogTypeDelete() {
415 $sep = $this->context->msg( 'colon-separator' )->text();
416
417 # delete/delete
418 $this->assertIRCComment(
419 $this->context->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
420 'delete', 'delete',
421 [],
422 $this->user_comment
423 );
424
425 # delete/restore
426 $this->assertIRCComment(
427 $this->context->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
428 'delete', 'restore',
429 [],
430 $this->user_comment
431 );
432 }
433
434 /**
435 * @covers LogFormatter::getIRCActionComment
436 * @covers LogFormatter::getIRCActionText
437 */
438 public function testIrcMsgForLogTypeNewusers() {
439 $this->assertIRCComment(
440 'New user account',
441 'newusers', 'newusers',
442 []
443 );
444 $this->assertIRCComment(
445 'New user account',
446 'newusers', 'create',
447 []
448 );
449 $this->assertIRCComment(
450 'created new account SomeTitle',
451 'newusers', 'create2',
452 []
453 );
454 $this->assertIRCComment(
455 'Account created automatically',
456 'newusers', 'autocreate',
457 []
458 );
459 }
460
461 /**
462 * @covers LogFormatter::getIRCActionComment
463 * @covers LogFormatter::getIRCActionText
464 */
465 public function testIrcMsgForLogTypeMove() {
466 $move_params = [
467 '4::target' => $this->target->getPrefixedText(),
468 '5::noredir' => 0,
469 ];
470 $sep = $this->context->msg( 'colon-separator' )->text();
471
472 # move/move
473 $this->assertIRCComment(
474 $this->context->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
475 ->plain() . $sep . $this->user_comment,
476 'move', 'move',
477 $move_params,
478 $this->user_comment
479 );
480
481 # move/move_redir
482 $this->assertIRCComment(
483 $this->context->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
484 ->plain() . $sep . $this->user_comment,
485 'move', 'move_redir',
486 $move_params,
487 $this->user_comment
488 );
489 }
490
491 /**
492 * @covers LogFormatter::getIRCActionComment
493 * @covers LogFormatter::getIRCActionText
494 */
495 public function testIrcMsgForLogTypePatrol() {
496 # patrol/patrol
497 $this->assertIRCComment(
498 $this->context->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
499 'patrol', 'patrol',
500 [
501 '4::curid' => '777',
502 '5::previd' => '666',
503 '6::auto' => 0,
504 ]
505 );
506 }
507
508 /**
509 * @covers LogFormatter::getIRCActionComment
510 * @covers LogFormatter::getIRCActionText
511 */
512 public function testIrcMsgForLogTypeProtect() {
513 $protectParams = [
514 '4::description' => '[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)'
515 ];
516 $sep = $this->context->msg( 'colon-separator' )->text();
517
518 # protect/protect
519 $this->assertIRCComment(
520 $this->context->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
521 ->plain() . $sep . $this->user_comment,
522 'protect', 'protect',
523 $protectParams,
524 $this->user_comment
525 );
526
527 # protect/unprotect
528 $this->assertIRCComment(
529 $this->context->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
530 'protect', 'unprotect',
531 [],
532 $this->user_comment
533 );
534
535 # protect/modify
536 $this->assertIRCComment(
537 $this->context->msg(
538 'modifiedarticleprotection',
539 'SomeTitle ' . $protectParams['4::description']
540 )->plain() . $sep . $this->user_comment,
541 'protect', 'modify',
542 $protectParams,
543 $this->user_comment
544 );
545
546 # protect/move_prot
547 $this->assertIRCComment(
548 $this->context->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
549 ->plain() . $sep . $this->user_comment,
550 'protect', 'move_prot',
551 [
552 '4::oldtitle' => 'OldTitle'
553 ],
554 $this->user_comment
555 );
556 }
557
558 /**
559 * @covers LogFormatter::getIRCActionComment
560 * @covers LogFormatter::getIRCActionText
561 */
562 public function testIrcMsgForLogTypeUpload() {
563 $sep = $this->context->msg( 'colon-separator' )->text();
564
565 # upload/upload
566 $this->assertIRCComment(
567 $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
568 'upload', 'upload',
569 [],
570 $this->user_comment
571 );
572
573 # upload/overwrite
574 $this->assertIRCComment(
575 $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
576 'upload', 'overwrite',
577 [],
578 $this->user_comment
579 );
580 }
581
582 /**
583 * @covers LogFormatter::getIRCActionComment
584 * @covers LogFormatter::getIRCActionText
585 */
586 public function testIrcMsgForLogTypeMerge() {
587 $sep = $this->context->msg( 'colon-separator' )->text();
588
589 # merge/merge
590 $this->assertIRCComment(
591 $this->context->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
592 . $sep . $this->user_comment,
593 'merge', 'merge',
594 [
595 '4::dest' => 'Dest',
596 '5::mergepoint' => 'timestamp',
597 ],
598 $this->user_comment
599 );
600 }
601
602 /**
603 * @covers LogFormatter::getIRCActionComment
604 * @covers LogFormatter::getIRCActionText
605 */
606 public function testIrcMsgForLogTypeImport() {
607 $sep = $this->context->msg( 'colon-separator' )->text();
608
609 # import/upload
610 $msg = $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
611 $sep .
612 $this->user_comment;
613 $this->assertIRCComment(
614 $msg,
615 'import', 'upload',
616 [],
617 $this->user_comment
618 );
619
620 # import/interwiki
621 $msg = $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
622 $sep .
623 $this->user_comment;
624 $this->assertIRCComment(
625 $msg,
626 'import', 'interwiki',
627 [],
628 $this->user_comment
629 );
630 }
631
632 /**
633 * @param string $expected Expected IRC text without colors codes
634 * @param string $type Log type (move, delete, suppress, patrol ...)
635 * @param string $action A log type action
636 * @param array $params
637 * @param string $comment (optional) A comment for the log action
638 * @param string $msg (optional) A message for PHPUnit :-)
639 */
640 protected function assertIRCComment( $expected, $type, $action, $params,
641 $comment = null, $msg = '', $legacy = false
642 ) {
643 $logEntry = new ManualLogEntry( $type, $action );
644 $logEntry->setPerformer( $this->user );
645 $logEntry->setTarget( $this->title );
646 if ( $comment !== null ) {
647 $logEntry->setComment( $comment );
648 }
649 $logEntry->setParameters( $params );
650 $logEntry->setLegacy( $legacy );
651
652 $formatter = LogFormatter::newFromEntry( $logEntry );
653 $formatter->setContext( $this->context );
654
655 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
656 $ircRcComment = IRCColourfulRCFeedFormatter::cleanupForIRC( $formatter->getIRCActionComment() );
657
658 $this->assertEquals(
659 $expected,
660 $ircRcComment,
661 $msg
662 );
663 }
664
665 }