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()
111 $titleLink = Linker
::link( $this->title
, null, [], [] );
113 // $paramsWithoutTools and $paramsWithTools should be only different
115 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
116 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
118 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
119 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
121 $this->assertEquals( $this->user
->getName(), $paramsWithoutTools[1] );
123 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
127 * @covers LogFormatter::newFromEntry
128 * @covers LogFormatter::getActionText
130 public function testLogParamsTypeRaw() {
131 $params = [ '4:raw:raw' => Linker
::link( $this->title
, null, [], [] ) ];
132 $expected = Linker
::link( $this->title
, null, [], [] );
134 $entry = $this->newLogEntry( 'param', $params );
135 $formatter = LogFormatter
::newFromEntry( $entry );
136 $formatter->setContext( $this->context
);
138 $logParam = $formatter->getActionText();
140 $this->assertEquals( $expected, $logParam );
144 * @covers LogFormatter::newFromEntry
145 * @covers LogFormatter::getActionText
147 public function testLogParamsTypeMsg() {
148 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
149 $expected = wfMessage( 'log-description-phpunit' )->text();
151 $entry = $this->newLogEntry( 'param', $params );
152 $formatter = LogFormatter
::newFromEntry( $entry );
153 $formatter->setContext( $this->context
);
155 $logParam = $formatter->getActionText();
157 $this->assertEquals( $expected, $logParam );
161 * @covers LogFormatter::newFromEntry
162 * @covers LogFormatter::getActionText
164 public function testLogParamsTypeMsgContent() {
165 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
166 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
168 $entry = $this->newLogEntry( 'param', $params );
169 $formatter = LogFormatter
::newFromEntry( $entry );
170 $formatter->setContext( $this->context
);
172 $logParam = $formatter->getActionText();
174 $this->assertEquals( $expected, $logParam );
178 * @covers LogFormatter::newFromEntry
179 * @covers LogFormatter::getActionText
181 public function testLogParamsTypeNumber() {
184 $params = [ '4:number:number' => 123456789 ];
185 $expected = $wgLang->formatNum( 123456789 );
187 $entry = $this->newLogEntry( 'param', $params );
188 $formatter = LogFormatter
::newFromEntry( $entry );
189 $formatter->setContext( $this->context
);
191 $logParam = $formatter->getActionText();
193 $this->assertEquals( $expected, $logParam );
197 * @covers LogFormatter::newFromEntry
198 * @covers LogFormatter::getActionText
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()
207 $entry = $this->newLogEntry( 'param', $params );
208 $formatter = LogFormatter
::newFromEntry( $entry );
209 $formatter->setContext( $this->context
);
211 $logParam = $formatter->getActionText();
213 $this->assertEquals( $expected, $logParam );
217 * @covers LogFormatter::newFromEntry
218 * @covers LogFormatter::getActionText
220 public function testLogParamsTypeTitleLink() {
221 $params = [ '4:title-link:titleLink' => $this->title
->getText() ];
222 $expected = Linker
::link( $this->title
, null, [], [] );
224 $entry = $this->newLogEntry( 'param', $params );
225 $formatter = LogFormatter
::newFromEntry( $entry );
226 $formatter->setContext( $this->context
);
228 $logParam = $formatter->getActionText();
230 $this->assertEquals( $expected, $logParam );
234 * @covers LogFormatter::newFromEntry
235 * @covers LogFormatter::getActionText
237 public function testLogParamsTypePlain() {
238 $params = [ '4:plain:plain' => 'Some plain text' ];
239 $expected = 'Some plain text';
241 $entry = $this->newLogEntry( 'param', $params );
242 $formatter = LogFormatter
::newFromEntry( $entry );
243 $formatter->setContext( $this->context
);
245 $logParam = $formatter->getActionText();
247 $this->assertEquals( $expected, $logParam );
251 * @covers LogFormatter::newFromEntry
252 * @covers LogFormatter::getComment
254 public function testLogComment() {
255 $entry = $this->newLogEntry( 'test', [] );
256 $formatter = LogFormatter
::newFromEntry( $entry );
257 $formatter->setContext( $this->context
);
259 $comment = ltrim( Linker
::commentBlock( $entry->getComment() ) );
261 $this->assertEquals( $comment, $formatter->getComment() );
265 * @dataProvider provideApiParamFormatting
266 * @covers LogFormatter::formatParametersForApi
267 * @covers LogFormatter::formatParameterValueForApi
269 public function testApiParamFormatting( $key, $value, $expected ) {
270 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
271 $formatter = LogFormatter
::newFromEntry( $entry );
272 $formatter->setContext( $this->context
);
274 ApiResult
::setIndexedTagName( $expected, 'param' );
275 ApiResult
::setArrayType( $expected, 'assoc' );
277 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
280 public static function provideApiParamFormatting() {
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(),
300 [ '4:msg-content:key', 'parentheses', [
301 'key_key' => 'parentheses',
302 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
304 [ '4:title:key', 'project:foo', [
305 'key_ns' => NS_PROJECT
,
306 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
308 [ '4:title-link:key', 'project:foo', [
309 'key_ns' => NS_PROJECT
,
310 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
312 [ '4:title-link:key', '<invalid>', [
313 'key_ns' => NS_SPECIAL
,
314 'key_title' => SpecialPage
::getTitleFor( 'Badtitle', '<invalid>' )->getFullText(),
316 [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
317 [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
322 * The testIrcMsgForAction* tests are supposed to cover the hacky
323 * LogFormatter::getIRCActionText / T36508
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.
332 * Should cover the following log actions (which are most commonly used by bots):
340 * - newusers/autocreate
344 * - protect/modifyprotect
345 * - protect/unprotect
346 * - protect/move_prot
352 * As well as the following Auto Edit Summaries:
360 * @covers LogFormatter::getIRCActionComment
361 * @covers LogFormatter::getIRCActionText
363 public function testIrcMsgForLogTypeBlock() {
364 $sep = $this->context
->msg( 'colon-separator' )->text();
367 $this->assertIRCComment(
368 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
369 . $sep . $this->user_comment
,
372 '5::duration' => 'duration',
373 '6::flags' => 'flags',
377 # block/block - legacy
378 $this->assertIRCComment(
379 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
380 . $sep . $this->user_comment
,
391 $this->assertIRCComment(
392 $this->context
->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
398 $this->assertIRCComment(
399 $this->context
->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
400 . $sep . $this->user_comment
,
403 '5::duration' => 'duration',
404 '6::flags' => 'flags',
411 * @covers LogFormatter::getIRCActionComment
412 * @covers LogFormatter::getIRCActionText
414 public function testIrcMsgForLogTypeDelete() {
415 $sep = $this->context
->msg( 'colon-separator' )->text();
418 $this->assertIRCComment(
419 $this->context
->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
426 $this->assertIRCComment(
427 $this->context
->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
435 * @covers LogFormatter::getIRCActionComment
436 * @covers LogFormatter::getIRCActionText
438 public function testIrcMsgForLogTypeNewusers() {
439 $this->assertIRCComment(
441 'newusers', 'newusers',
444 $this->assertIRCComment(
446 'newusers', 'create',
449 $this->assertIRCComment(
450 'created new account SomeTitle',
451 'newusers', 'create2',
454 $this->assertIRCComment(
455 'Account created automatically',
456 'newusers', 'autocreate',
462 * @covers LogFormatter::getIRCActionComment
463 * @covers LogFormatter::getIRCActionText
465 public function testIrcMsgForLogTypeMove() {
467 '4::target' => $this->target
->getPrefixedText(),
470 $sep = $this->context
->msg( 'colon-separator' )->text();
473 $this->assertIRCComment(
474 $this->context
->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
475 ->plain() . $sep . $this->user_comment
,
482 $this->assertIRCComment(
483 $this->context
->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
484 ->plain() . $sep . $this->user_comment
,
485 'move', 'move_redir',
492 * @covers LogFormatter::getIRCActionComment
493 * @covers LogFormatter::getIRCActionText
495 public function testIrcMsgForLogTypePatrol() {
497 $this->assertIRCComment(
498 $this->context
->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
502 '5::previd' => '666',
509 * @covers LogFormatter::getIRCActionComment
510 * @covers LogFormatter::getIRCActionText
512 public function testIrcMsgForLogTypeProtect() {
514 '4::description' => '[edit=sysop] (indefinite) [move=sysop] (indefinite)'
516 $sep = $this->context
->msg( 'colon-separator' )->text();
519 $this->assertIRCComment(
520 $this->context
->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
521 ->plain() . $sep . $this->user_comment
,
522 'protect', 'protect',
528 $this->assertIRCComment(
529 $this->context
->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
530 'protect', 'unprotect',
536 $this->assertIRCComment(
538 'modifiedarticleprotection',
539 'SomeTitle ' . $protectParams['4::description']
540 )->plain() . $sep . $this->user_comment
,
547 $this->assertIRCComment(
548 $this->context
->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
549 ->plain() . $sep . $this->user_comment
,
550 'protect', 'move_prot',
552 '4::oldtitle' => 'OldTitle'
559 * @covers LogFormatter::getIRCActionComment
560 * @covers LogFormatter::getIRCActionText
562 public function testIrcMsgForLogTypeUpload() {
563 $sep = $this->context
->msg( 'colon-separator' )->text();
566 $this->assertIRCComment(
567 $this->context
->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
574 $this->assertIRCComment(
575 $this->context
->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
576 'upload', 'overwrite',
583 * @covers LogFormatter::getIRCActionComment
584 * @covers LogFormatter::getIRCActionText
586 public function testIrcMsgForLogTypeMerge() {
587 $sep = $this->context
->msg( 'colon-separator' )->text();
590 $this->assertIRCComment(
591 $this->context
->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
592 . $sep . $this->user_comment
,
596 '5::mergepoint' => 'timestamp',
603 * @covers LogFormatter::getIRCActionComment
604 * @covers LogFormatter::getIRCActionText
606 public function testIrcMsgForLogTypeImport() {
607 $sep = $this->context
->msg( 'colon-separator' )->text();
610 $msg = $this->context
->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
613 $this->assertIRCComment(
621 $msg = $this->context
->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
624 $this->assertIRCComment(
626 'import', 'interwiki',
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 :-)
640 protected function assertIRCComment( $expected, $type, $action, $params,
641 $comment = null, $msg = '', $legacy = false
643 $logEntry = new ManualLogEntry( $type, $action );
644 $logEntry->setPerformer( $this->user
);
645 $logEntry->setTarget( $this->title
);
646 if ( $comment !== null ) {
647 $logEntry->setComment( $comment );
649 $logEntry->setParameters( $params );
650 $logEntry->setLegacy( $legacy );
652 $formatter = LogFormatter
::newFromEntry( $logEntry );
653 $formatter->setContext( $this->context
);
655 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
656 $ircRcComment = IRCColourfulRCFeedFormatter
::cleanupForIRC( $formatter->getIRCActionComment() );