3 use MediaWiki\User\UserIdentityValue
;
8 class LogFormatterTest
extends MediaWikiLangTestCase
{
9 private static $oldExtMsgFiles;
34 protected $user_comment;
36 public static function setUpBeforeClass() {
37 parent
::setUpBeforeClass();
39 global $wgExtensionMessagesFiles;
40 self
::$oldExtMsgFiles = $wgExtensionMessagesFiles;
41 $wgExtensionMessagesFiles['LogTests'] = __DIR__
. '/LogTests.i18n.php';
42 Language
::getLocalisationCache()->recache( 'en' );
45 public static function tearDownAfterClass() {
46 global $wgExtensionMessagesFiles;
47 $wgExtensionMessagesFiles = self
::$oldExtMsgFiles;
48 Language
::getLocalisationCache()->recache( 'en' );
50 parent
::tearDownAfterClass();
53 protected function setUp() {
56 $this->setMwGlobals( [
57 'wgLogTypes' => [ 'phpunit' ],
58 'wgLogActionsHandlers' => [ 'phpunit/test' => LogFormatter
::class,
59 'phpunit/param' => LogFormatter
::class ],
60 'wgUser' => User
::newFromName( 'Testuser' ),
63 $this->user
= User
::newFromName( 'Testuser' );
64 $this->title
= Title
::newFromText( 'SomeTitle' );
65 $this->target
= Title
::newFromText( 'TestTarget' );
67 $this->context
= new RequestContext();
68 $this->context
->setUser( $this->user
);
69 $this->context
->setTitle( $this->title
);
70 $this->context
->setLanguage( RequestContext
::getMain()->getLanguage() );
72 $this->user_comment
= '<User comment about action>';
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' );
81 $logEntry->setParameters( $params );
87 * @covers LogFormatter::newFromEntry
89 public function testNormalLogParams() {
90 $entry = $this->newLogEntry( 'test', [] );
91 $formatter = LogFormatter
::newFromEntry( $entry );
92 $formatter->setContext( $this->context
);
94 $formatter->setShowUserToolLinks( false );
95 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
97 $formatter2 = LogFormatter
::newFromEntry( $entry );
98 $formatter2->setContext( $this->context
);
99 $formatter2->setShowUserToolLinks( true );
100 $paramsWithTools = $formatter2->getMessageParametersForTesting();
102 $userLink = Linker
::userLink(
103 $this->user
->getId(),
104 $this->user
->getName()
107 $userTools = Linker
::userToolLinksRedContribs(
108 $this->user
->getId(),
109 $this->user
->getName(),
110 $this->user
->getEditCount(),
114 $titleLink = Linker
::link( $this->title
, null, [], [] );
116 // $paramsWithoutTools and $paramsWithTools should be only different
118 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
119 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
121 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
122 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
124 $this->assertEquals( $this->user
->getName(), $paramsWithoutTools[1] );
126 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
130 * @covers LogFormatter::newFromEntry
131 * @covers LogFormatter::getActionText
133 public function testLogParamsTypeRaw() {
134 $params = [ '4:raw:raw' => Linker
::link( $this->title
, null, [], [] ) ];
135 $expected = Linker
::link( $this->title
, null, [], [] );
137 $entry = $this->newLogEntry( 'param', $params );
138 $formatter = LogFormatter
::newFromEntry( $entry );
139 $formatter->setContext( $this->context
);
141 $logParam = $formatter->getActionText();
143 $this->assertEquals( $expected, $logParam );
147 * @covers LogFormatter::newFromEntry
148 * @covers LogFormatter::getActionText
150 public function testLogParamsTypeMsg() {
151 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
152 $expected = wfMessage( 'log-description-phpunit' )->text();
154 $entry = $this->newLogEntry( 'param', $params );
155 $formatter = LogFormatter
::newFromEntry( $entry );
156 $formatter->setContext( $this->context
);
158 $logParam = $formatter->getActionText();
160 $this->assertEquals( $expected, $logParam );
164 * @covers LogFormatter::newFromEntry
165 * @covers LogFormatter::getActionText
167 public function testLogParamsTypeMsgContent() {
168 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
169 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
171 $entry = $this->newLogEntry( 'param', $params );
172 $formatter = LogFormatter
::newFromEntry( $entry );
173 $formatter->setContext( $this->context
);
175 $logParam = $formatter->getActionText();
177 $this->assertEquals( $expected, $logParam );
181 * @covers LogFormatter::newFromEntry
182 * @covers LogFormatter::getActionText
184 public function testLogParamsTypeNumber() {
187 $params = [ '4:number:number' => 123456789 ];
188 $expected = $wgLang->formatNum( 123456789 );
190 $entry = $this->newLogEntry( 'param', $params );
191 $formatter = LogFormatter
::newFromEntry( $entry );
192 $formatter->setContext( $this->context
);
194 $logParam = $formatter->getActionText();
196 $this->assertEquals( $expected, $logParam );
200 * @covers LogFormatter::newFromEntry
201 * @covers LogFormatter::getActionText
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()
210 $entry = $this->newLogEntry( 'param', $params );
211 $formatter = LogFormatter
::newFromEntry( $entry );
212 $formatter->setContext( $this->context
);
214 $logParam = $formatter->getActionText();
216 $this->assertEquals( $expected, $logParam );
220 * @covers LogFormatter::newFromEntry
221 * @covers LogFormatter::getActionText
223 public function testLogParamsTypeUserLink_empty() {
224 $params = [ '4:user-link:userLink' => ':' ];
226 $entry = $this->newLogEntry( 'param', $params );
227 $formatter = LogFormatter
::newFromEntry( $entry );
229 $this->context
->setLanguage( Language
::factory( 'qqx' ) );
230 $formatter->setContext( $this->context
);
232 $logParam = $formatter->getActionText();
233 $this->assertContains( '(empty-username)', $logParam );
237 * @covers LogFormatter::newFromEntry
238 * @covers LogFormatter::getActionText
240 public function testLogParamsTypeTitleLink() {
241 $params = [ '4:title-link:titleLink' => $this->title
->getText() ];
242 $expected = Linker
::link( $this->title
, null, [], [] );
244 $entry = $this->newLogEntry( 'param', $params );
245 $formatter = LogFormatter
::newFromEntry( $entry );
246 $formatter->setContext( $this->context
);
248 $logParam = $formatter->getActionText();
250 $this->assertEquals( $expected, $logParam );
254 * @covers LogFormatter::newFromEntry
255 * @covers LogFormatter::getActionText
257 public function testLogParamsTypePlain() {
258 $params = [ '4:plain:plain' => 'Some plain text' ];
259 $expected = 'Some plain text';
261 $entry = $this->newLogEntry( 'param', $params );
262 $formatter = LogFormatter
::newFromEntry( $entry );
263 $formatter->setContext( $this->context
);
265 $logParam = $formatter->getActionText();
267 $this->assertEquals( $expected, $logParam );
271 * @covers LogFormatter::getPerformerElement
273 public function testGetPerformerElement() {
274 $entry = $this->newLogEntry( 'param', [] );
275 $entry->setPerformer( new UserIdentityValue( 1328435, 'Test', 0 ) );
277 $formatter = LogFormatter
::newFromEntry( $entry );
278 $formatter->setContext( $this->context
);
280 $element = $formatter->getPerformerElement();
281 $this->assertContains( 'User:Test', $element );
285 * @covers LogFormatter::newFromEntry
286 * @covers LogFormatter::getComment
288 public function testLogComment() {
289 $entry = $this->newLogEntry( 'test', [] );
290 $formatter = LogFormatter
::newFromEntry( $entry );
291 $formatter->setContext( $this->context
);
293 $comment = ltrim( Linker
::commentBlock( $entry->getComment() ) );
295 $this->assertEquals( $comment, $formatter->getComment() );
299 * @dataProvider provideApiParamFormatting
300 * @covers LogFormatter::formatParametersForApi
301 * @covers LogFormatter::formatParameterValueForApi
303 public function testApiParamFormatting( $key, $value, $expected ) {
304 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
305 $formatter = LogFormatter
::newFromEntry( $entry );
306 $formatter->setContext( $this->context
);
308 ApiResult
::setIndexedTagName( $expected, 'param' );
309 ApiResult
::setArrayType( $expected, 'assoc' );
311 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
314 public static function provideApiParamFormatting() {
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(),
334 [ '4:msg-content:key', 'parentheses', [
335 'key_key' => 'parentheses',
336 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
338 [ '4:title:key', 'project:foo', [
339 'key_ns' => NS_PROJECT
,
340 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
342 [ '4:title-link:key', 'project:foo', [
343 'key_ns' => NS_PROJECT
,
344 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
346 [ '4:title-link:key', '<invalid>', [
347 'key_ns' => NS_SPECIAL
,
348 'key_title' => SpecialPage
::getTitleFor( 'Badtitle', '<invalid>' )->getFullText(),
350 [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
351 [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
356 * The testIrcMsgForAction* tests are supposed to cover the hacky
357 * LogFormatter::getIRCActionText / T36508
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.
366 * Should cover the following log actions (which are most commonly used by bots):
374 * - newusers/autocreate
378 * - protect/modifyprotect
379 * - protect/unprotect
380 * - protect/move_prot
386 * As well as the following Auto Edit Summaries:
394 * @covers LogFormatter::getIRCActionComment
395 * @covers LogFormatter::getIRCActionText
397 public function testIrcMsgForLogTypeBlock() {
398 $sep = $this->context
->msg( 'colon-separator' )->text();
401 $this->assertIRCComment(
402 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
403 . $sep . $this->user_comment
,
406 '5::duration' => 'duration',
407 '6::flags' => 'flags',
411 # block/block - legacy
412 $this->assertIRCComment(
413 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
414 . $sep . $this->user_comment
,
425 $this->assertIRCComment(
426 $this->context
->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
432 $this->assertIRCComment(
433 $this->context
->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
434 . $sep . $this->user_comment
,
437 '5::duration' => 'duration',
438 '6::flags' => 'flags',
445 * @covers LogFormatter::getIRCActionComment
446 * @covers LogFormatter::getIRCActionText
448 public function testIrcMsgForLogTypeDelete() {
449 $sep = $this->context
->msg( 'colon-separator' )->text();
452 $this->assertIRCComment(
453 $this->context
->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
460 $this->assertIRCComment(
461 $this->context
->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
469 * @covers LogFormatter::getIRCActionComment
470 * @covers LogFormatter::getIRCActionText
472 public function testIrcMsgForLogTypeNewusers() {
473 $this->assertIRCComment(
475 'newusers', 'newusers',
478 $this->assertIRCComment(
480 'newusers', 'create',
483 $this->assertIRCComment(
484 'created new account SomeTitle',
485 'newusers', 'create2',
488 $this->assertIRCComment(
489 'Account created automatically',
490 'newusers', 'autocreate',
496 * @covers LogFormatter::getIRCActionComment
497 * @covers LogFormatter::getIRCActionText
499 public function testIrcMsgForLogTypeMove() {
501 '4::target' => $this->target
->getPrefixedText(),
504 $sep = $this->context
->msg( 'colon-separator' )->text();
507 $this->assertIRCComment(
508 $this->context
->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
509 ->plain() . $sep . $this->user_comment
,
516 $this->assertIRCComment(
517 $this->context
->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
518 ->plain() . $sep . $this->user_comment
,
519 'move', 'move_redir',
526 * @covers LogFormatter::getIRCActionComment
527 * @covers LogFormatter::getIRCActionText
529 public function testIrcMsgForLogTypePatrol() {
531 $this->assertIRCComment(
532 $this->context
->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
536 '5::previd' => '666',
543 * @covers LogFormatter::getIRCActionComment
544 * @covers LogFormatter::getIRCActionText
546 public function testIrcMsgForLogTypeProtect() {
548 '4::description' => '[edit=sysop] (indefinite) [move=sysop] (indefinite)'
550 $sep = $this->context
->msg( 'colon-separator' )->text();
553 $this->assertIRCComment(
554 $this->context
->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
555 ->plain() . $sep . $this->user_comment
,
556 'protect', 'protect',
562 $this->assertIRCComment(
563 $this->context
->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
564 'protect', 'unprotect',
570 $this->assertIRCComment(
572 'modifiedarticleprotection',
573 'SomeTitle ' . $protectParams['4::description']
574 )->plain() . $sep . $this->user_comment
,
581 $this->assertIRCComment(
582 $this->context
->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
583 ->plain() . $sep . $this->user_comment
,
584 'protect', 'move_prot',
586 '4::oldtitle' => 'OldTitle'
593 * @covers LogFormatter::getIRCActionComment
594 * @covers LogFormatter::getIRCActionText
596 public function testIrcMsgForLogTypeUpload() {
597 $sep = $this->context
->msg( 'colon-separator' )->text();
600 $this->assertIRCComment(
601 $this->context
->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
608 $this->assertIRCComment(
609 $this->context
->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
610 'upload', 'overwrite',
617 * @covers LogFormatter::getIRCActionComment
618 * @covers LogFormatter::getIRCActionText
620 public function testIrcMsgForLogTypeMerge() {
621 $sep = $this->context
->msg( 'colon-separator' )->text();
624 $this->assertIRCComment(
625 $this->context
->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
626 . $sep . $this->user_comment
,
630 '5::mergepoint' => 'timestamp',
637 * @covers LogFormatter::getIRCActionComment
638 * @covers LogFormatter::getIRCActionText
640 public function testIrcMsgForLogTypeImport() {
641 $sep = $this->context
->msg( 'colon-separator' )->text();
644 $msg = $this->context
->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
647 $this->assertIRCComment(
655 $msg = $this->context
->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
658 $this->assertIRCComment(
660 'import', 'interwiki',
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 :-)
674 protected function assertIRCComment( $expected, $type, $action, $params,
675 $comment = null, $msg = '', $legacy = false
677 $logEntry = new ManualLogEntry( $type, $action );
678 $logEntry->setPerformer( $this->user
);
679 $logEntry->setTarget( $this->title
);
680 if ( $comment !== null ) {
681 $logEntry->setComment( $comment );
683 $logEntry->setParameters( $params );
684 $logEntry->setLegacy( $legacy );
686 $formatter = LogFormatter
::newFromEntry( $logEntry );
687 $formatter->setContext( $this->context
);
689 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
690 $ircRcComment = IRCColourfulRCFeedFormatter
::cleanupForIRC( $formatter->getIRCActionComment() );