6 class LogFormatterTest
extends MediaWikiLangTestCase
{
7 private static $oldExtMsgFiles;
32 protected $user_comment;
34 public static function setUpBeforeClass() {
35 parent
::setUpBeforeClass();
37 global $wgExtensionMessagesFiles;
38 self
::$oldExtMsgFiles = $wgExtensionMessagesFiles;
39 $wgExtensionMessagesFiles['LogTests'] = __DIR__
. '/LogTests.i18n.php';
40 Language
::getLocalisationCache()->recache( 'en' );
43 public static function tearDownAfterClass() {
44 global $wgExtensionMessagesFiles;
45 $wgExtensionMessagesFiles = self
::$oldExtMsgFiles;
46 Language
::getLocalisationCache()->recache( 'en' );
48 parent
::tearDownAfterClass();
51 protected function setUp() {
54 $this->setMwGlobals( [
55 'wgLogTypes' => [ 'phpunit' ],
56 'wgLogActionsHandlers' => [ 'phpunit/test' => LogFormatter
::class,
57 'phpunit/param' => LogFormatter
::class ],
58 'wgUser' => User
::newFromName( 'Testuser' ),
61 $this->user
= User
::newFromName( 'Testuser' );
62 $this->title
= Title
::newFromText( 'SomeTitle' );
63 $this->target
= Title
::newFromText( 'TestTarget' );
65 $this->context
= new RequestContext();
66 $this->context
->setUser( $this->user
);
67 $this->context
->setTitle( $this->title
);
68 $this->context
->setLanguage( RequestContext
::getMain()->getLanguage() );
70 $this->user_comment
= '<User comment about action>';
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' );
79 $logEntry->setParameters( $params );
85 * @covers LogFormatter::newFromEntry
87 public function testNormalLogParams() {
88 $entry = $this->newLogEntry( 'test', [] );
89 $formatter = LogFormatter
::newFromEntry( $entry );
90 $formatter->setContext( $this->context
);
92 $formatter->setShowUserToolLinks( false );
93 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
95 $formatter2 = LogFormatter
::newFromEntry( $entry );
96 $formatter2->setContext( $this->context
);
97 $formatter2->setShowUserToolLinks( true );
98 $paramsWithTools = $formatter2->getMessageParametersForTesting();
100 $userLink = Linker
::userLink(
101 $this->user
->getId(),
102 $this->user
->getName()
105 $userTools = Linker
::userToolLinksRedContribs(
106 $this->user
->getId(),
107 $this->user
->getName(),
108 $this->user
->getEditCount(),
112 $titleLink = Linker
::link( $this->title
, null, [], [] );
114 // $paramsWithoutTools and $paramsWithTools should be only different
116 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
117 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
119 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
120 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
122 $this->assertEquals( $this->user
->getName(), $paramsWithoutTools[1] );
124 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
128 * @covers LogFormatter::newFromEntry
129 * @covers LogFormatter::getActionText
131 public function testLogParamsTypeRaw() {
132 $params = [ '4:raw:raw' => Linker
::link( $this->title
, null, [], [] ) ];
133 $expected = Linker
::link( $this->title
, null, [], [] );
135 $entry = $this->newLogEntry( 'param', $params );
136 $formatter = LogFormatter
::newFromEntry( $entry );
137 $formatter->setContext( $this->context
);
139 $logParam = $formatter->getActionText();
141 $this->assertEquals( $expected, $logParam );
145 * @covers LogFormatter::newFromEntry
146 * @covers LogFormatter::getActionText
148 public function testLogParamsTypeMsg() {
149 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
150 $expected = wfMessage( 'log-description-phpunit' )->text();
152 $entry = $this->newLogEntry( 'param', $params );
153 $formatter = LogFormatter
::newFromEntry( $entry );
154 $formatter->setContext( $this->context
);
156 $logParam = $formatter->getActionText();
158 $this->assertEquals( $expected, $logParam );
162 * @covers LogFormatter::newFromEntry
163 * @covers LogFormatter::getActionText
165 public function testLogParamsTypeMsgContent() {
166 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
167 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
169 $entry = $this->newLogEntry( 'param', $params );
170 $formatter = LogFormatter
::newFromEntry( $entry );
171 $formatter->setContext( $this->context
);
173 $logParam = $formatter->getActionText();
175 $this->assertEquals( $expected, $logParam );
179 * @covers LogFormatter::newFromEntry
180 * @covers LogFormatter::getActionText
182 public function testLogParamsTypeNumber() {
185 $params = [ '4:number:number' => 123456789 ];
186 $expected = $wgLang->formatNum( 123456789 );
188 $entry = $this->newLogEntry( 'param', $params );
189 $formatter = LogFormatter
::newFromEntry( $entry );
190 $formatter->setContext( $this->context
);
192 $logParam = $formatter->getActionText();
194 $this->assertEquals( $expected, $logParam );
198 * @covers LogFormatter::newFromEntry
199 * @covers LogFormatter::getActionText
201 public function testLogParamsTypeUserLink() {
202 $params = [ '4:user-link:userLink' => $this->user
->getName() ];
203 $expected = Linker
::userLink(
204 $this->user
->getId(),
205 $this->user
->getName()
208 $entry = $this->newLogEntry( 'param', $params );
209 $formatter = LogFormatter
::newFromEntry( $entry );
210 $formatter->setContext( $this->context
);
212 $logParam = $formatter->getActionText();
214 $this->assertEquals( $expected, $logParam );
218 * @covers LogFormatter::newFromEntry
219 * @covers LogFormatter::getActionText
221 public function testLogParamsTypeTitleLink() {
222 $params = [ '4:title-link:titleLink' => $this->title
->getText() ];
223 $expected = Linker
::link( $this->title
, null, [], [] );
225 $entry = $this->newLogEntry( 'param', $params );
226 $formatter = LogFormatter
::newFromEntry( $entry );
227 $formatter->setContext( $this->context
);
229 $logParam = $formatter->getActionText();
231 $this->assertEquals( $expected, $logParam );
235 * @covers LogFormatter::newFromEntry
236 * @covers LogFormatter::getActionText
238 public function testLogParamsTypePlain() {
239 $params = [ '4:plain:plain' => 'Some plain text' ];
240 $expected = 'Some plain text';
242 $entry = $this->newLogEntry( 'param', $params );
243 $formatter = LogFormatter
::newFromEntry( $entry );
244 $formatter->setContext( $this->context
);
246 $logParam = $formatter->getActionText();
248 $this->assertEquals( $expected, $logParam );
252 * @covers LogFormatter::newFromEntry
253 * @covers LogFormatter::getComment
255 public function testLogComment() {
256 $entry = $this->newLogEntry( 'test', [] );
257 $formatter = LogFormatter
::newFromEntry( $entry );
258 $formatter->setContext( $this->context
);
260 $comment = ltrim( Linker
::commentBlock( $entry->getComment() ) );
262 $this->assertEquals( $comment, $formatter->getComment() );
266 * @dataProvider provideApiParamFormatting
267 * @covers LogFormatter::formatParametersForApi
268 * @covers LogFormatter::formatParameterValueForApi
270 public function testApiParamFormatting( $key, $value, $expected ) {
271 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
272 $formatter = LogFormatter
::newFromEntry( $entry );
273 $formatter->setContext( $this->context
);
275 ApiResult
::setIndexedTagName( $expected, 'param' );
276 ApiResult
::setArrayType( $expected, 'assoc' );
278 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
281 public static function provideApiParamFormatting() {
283 [ 0, 'value', [ 'value' ] ],
284 [ 'named', 'value', [ 'named' => 'value' ] ],
285 [ '::key', 'value', [ 'key' => 'value' ] ],
286 [ '4::key', 'value', [ 'key' => 'value' ] ],
287 [ '4:raw:key', 'value', [ 'key' => 'value' ] ],
288 [ '4:plain:key', 'value', [ 'key' => 'value' ] ],
289 [ '4:bool:key', '1', [ 'key' => true ] ],
290 [ '4:bool:key', '0', [ 'key' => false ] ],
291 [ '4:number:key', '123', [ 'key' => 123 ] ],
292 [ '4:number:key', '123.5', [ 'key' => 123.5 ] ],
293 [ '4:array:key', [], [ 'key' => [ ApiResult
::META_TYPE
=> 'array' ] ] ],
294 [ '4:assoc:key', [], [ 'key' => [ ApiResult
::META_TYPE
=> 'assoc' ] ] ],
295 [ '4:kvp:key', [], [ 'key' => [ ApiResult
::META_TYPE
=> 'kvp' ] ] ],
296 [ '4:timestamp:key', '20150102030405', [ 'key' => '2015-01-02T03:04:05Z' ] ],
297 [ '4:msg:key', 'parentheses', [
298 'key_key' => 'parentheses',
299 'key_text' => wfMessage( 'parentheses' )->text(),
301 [ '4:msg-content:key', 'parentheses', [
302 'key_key' => 'parentheses',
303 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
305 [ '4:title:key', 'project:foo', [
306 'key_ns' => NS_PROJECT
,
307 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
309 [ '4:title-link:key', 'project:foo', [
310 'key_ns' => NS_PROJECT
,
311 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
313 [ '4:title-link:key', '<invalid>', [
314 'key_ns' => NS_SPECIAL
,
315 'key_title' => SpecialPage
::getTitleFor( 'Badtitle', '<invalid>' )->getFullText(),
317 [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
318 [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
323 * The testIrcMsgForAction* tests are supposed to cover the hacky
324 * LogFormatter::getIRCActionText / T36508
326 * Third parties bots listen to those messages. They are clever enough
327 * to fetch the i18n messages from the wiki and then analyze the IRC feed
328 * to reverse engineer the $1, $2 messages.
329 * One thing bots can not detect is when MediaWiki change the meaning of
330 * a message like what happened when we deployed 1.19. $1 became the user
331 * performing the action which broke basically all bots around.
333 * Should cover the following log actions (which are most commonly used by bots):
341 * - newusers/autocreate
345 * - protect/modifyprotect
346 * - protect/unprotect
347 * - protect/move_prot
353 * As well as the following Auto Edit Summaries:
361 * @covers LogFormatter::getIRCActionComment
362 * @covers LogFormatter::getIRCActionText
364 public function testIrcMsgForLogTypeBlock() {
365 $sep = $this->context
->msg( 'colon-separator' )->text();
368 $this->assertIRCComment(
369 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
370 . $sep . $this->user_comment
,
373 '5::duration' => 'duration',
374 '6::flags' => 'flags',
378 # block/block - legacy
379 $this->assertIRCComment(
380 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
381 . $sep . $this->user_comment
,
392 $this->assertIRCComment(
393 $this->context
->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
399 $this->assertIRCComment(
400 $this->context
->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
401 . $sep . $this->user_comment
,
404 '5::duration' => 'duration',
405 '6::flags' => 'flags',
412 * @covers LogFormatter::getIRCActionComment
413 * @covers LogFormatter::getIRCActionText
415 public function testIrcMsgForLogTypeDelete() {
416 $sep = $this->context
->msg( 'colon-separator' )->text();
419 $this->assertIRCComment(
420 $this->context
->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
427 $this->assertIRCComment(
428 $this->context
->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
436 * @covers LogFormatter::getIRCActionComment
437 * @covers LogFormatter::getIRCActionText
439 public function testIrcMsgForLogTypeNewusers() {
440 $this->assertIRCComment(
442 'newusers', 'newusers',
445 $this->assertIRCComment(
447 'newusers', 'create',
450 $this->assertIRCComment(
451 'created new account SomeTitle',
452 'newusers', 'create2',
455 $this->assertIRCComment(
456 'Account created automatically',
457 'newusers', 'autocreate',
463 * @covers LogFormatter::getIRCActionComment
464 * @covers LogFormatter::getIRCActionText
466 public function testIrcMsgForLogTypeMove() {
468 '4::target' => $this->target
->getPrefixedText(),
471 $sep = $this->context
->msg( 'colon-separator' )->text();
474 $this->assertIRCComment(
475 $this->context
->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
476 ->plain() . $sep . $this->user_comment
,
483 $this->assertIRCComment(
484 $this->context
->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
485 ->plain() . $sep . $this->user_comment
,
486 'move', 'move_redir',
493 * @covers LogFormatter::getIRCActionComment
494 * @covers LogFormatter::getIRCActionText
496 public function testIrcMsgForLogTypePatrol() {
498 $this->assertIRCComment(
499 $this->context
->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
503 '5::previd' => '666',
510 * @covers LogFormatter::getIRCActionComment
511 * @covers LogFormatter::getIRCActionText
513 public function testIrcMsgForLogTypeProtect() {
515 '4::description' => '[edit=sysop] (indefinite) [move=sysop] (indefinite)'
517 $sep = $this->context
->msg( 'colon-separator' )->text();
520 $this->assertIRCComment(
521 $this->context
->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
522 ->plain() . $sep . $this->user_comment
,
523 'protect', 'protect',
529 $this->assertIRCComment(
530 $this->context
->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
531 'protect', 'unprotect',
537 $this->assertIRCComment(
539 'modifiedarticleprotection',
540 'SomeTitle ' . $protectParams['4::description']
541 )->plain() . $sep . $this->user_comment
,
548 $this->assertIRCComment(
549 $this->context
->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
550 ->plain() . $sep . $this->user_comment
,
551 'protect', 'move_prot',
553 '4::oldtitle' => 'OldTitle'
560 * @covers LogFormatter::getIRCActionComment
561 * @covers LogFormatter::getIRCActionText
563 public function testIrcMsgForLogTypeUpload() {
564 $sep = $this->context
->msg( 'colon-separator' )->text();
567 $this->assertIRCComment(
568 $this->context
->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
575 $this->assertIRCComment(
576 $this->context
->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
577 'upload', 'overwrite',
584 * @covers LogFormatter::getIRCActionComment
585 * @covers LogFormatter::getIRCActionText
587 public function testIrcMsgForLogTypeMerge() {
588 $sep = $this->context
->msg( 'colon-separator' )->text();
591 $this->assertIRCComment(
592 $this->context
->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
593 . $sep . $this->user_comment
,
597 '5::mergepoint' => 'timestamp',
604 * @covers LogFormatter::getIRCActionComment
605 * @covers LogFormatter::getIRCActionText
607 public function testIrcMsgForLogTypeImport() {
608 $sep = $this->context
->msg( 'colon-separator' )->text();
611 $msg = $this->context
->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
614 $this->assertIRCComment(
622 $msg = $this->context
->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
625 $this->assertIRCComment(
627 'import', 'interwiki',
634 * @param string $expected Expected IRC text without colors codes
635 * @param string $type Log type (move, delete, suppress, patrol ...)
636 * @param string $action A log type action
637 * @param array $params
638 * @param string $comment (optional) A comment for the log action
639 * @param string $msg (optional) A message for PHPUnit :-)
641 protected function assertIRCComment( $expected, $type, $action, $params,
642 $comment = null, $msg = '', $legacy = false
644 $logEntry = new ManualLogEntry( $type, $action );
645 $logEntry->setPerformer( $this->user
);
646 $logEntry->setTarget( $this->title
);
647 if ( $comment !== null ) {
648 $logEntry->setComment( $comment );
650 $logEntry->setParameters( $params );
651 $logEntry->setLegacy( $legacy );
653 $formatter = LogFormatter
::newFromEntry( $logEntry );
654 $formatter->setContext( $this->context
);
656 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
657 $ircRcComment = IRCColourfulRCFeedFormatter
::cleanupForIRC( $formatter->getIRCActionComment() );