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