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();
94 unset( $formatter->parsedParameters
);
96 $formatter->setShowUserToolLinks( true );
97 $paramsWithTools = $formatter->getMessageParametersForTesting();
99 $userLink = Linker
::userLink(
100 $this->user
->getId(),
101 $this->user
->getName()
104 $userTools = Linker
::userToolLinksRedContribs(
105 $this->user
->getId(),
106 $this->user
->getName(),
107 $this->user
->getEditCount()
110 $titleLink = Linker
::link( $this->title
, null, [], [] );
112 // $paramsWithoutTools and $paramsWithTools should be only different
114 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
115 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
117 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
118 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
120 $this->assertEquals( $this->user
->getName(), $paramsWithoutTools[1] );
122 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
126 * @covers LogFormatter::newFromEntry
127 * @covers LogFormatter::getActionText
129 public function testLogParamsTypeRaw() {
130 $params = [ '4:raw:raw' => Linker
::link( $this->title
, null, [], [] ) ];
131 $expected = Linker
::link( $this->title
, null, [], [] );
133 $entry = $this->newLogEntry( 'param', $params );
134 $formatter = LogFormatter
::newFromEntry( $entry );
135 $formatter->setContext( $this->context
);
137 $logParam = $formatter->getActionText();
139 $this->assertEquals( $expected, $logParam );
143 * @covers LogFormatter::newFromEntry
144 * @covers LogFormatter::getActionText
146 public function testLogParamsTypeMsg() {
147 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
148 $expected = wfMessage( 'log-description-phpunit' )->text();
150 $entry = $this->newLogEntry( 'param', $params );
151 $formatter = LogFormatter
::newFromEntry( $entry );
152 $formatter->setContext( $this->context
);
154 $logParam = $formatter->getActionText();
156 $this->assertEquals( $expected, $logParam );
160 * @covers LogFormatter::newFromEntry
161 * @covers LogFormatter::getActionText
163 public function testLogParamsTypeMsgContent() {
164 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
165 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
167 $entry = $this->newLogEntry( 'param', $params );
168 $formatter = LogFormatter
::newFromEntry( $entry );
169 $formatter->setContext( $this->context
);
171 $logParam = $formatter->getActionText();
173 $this->assertEquals( $expected, $logParam );
177 * @covers LogFormatter::newFromEntry
178 * @covers LogFormatter::getActionText
180 public function testLogParamsTypeNumber() {
183 $params = [ '4:number:number' => 123456789 ];
184 $expected = $wgLang->formatNum( 123456789 );
186 $entry = $this->newLogEntry( 'param', $params );
187 $formatter = LogFormatter
::newFromEntry( $entry );
188 $formatter->setContext( $this->context
);
190 $logParam = $formatter->getActionText();
192 $this->assertEquals( $expected, $logParam );
196 * @covers LogFormatter::newFromEntry
197 * @covers LogFormatter::getActionText
199 public function testLogParamsTypeUserLink() {
200 $params = [ '4:user-link:userLink' => $this->user
->getName() ];
201 $expected = Linker
::userLink(
202 $this->user
->getId(),
203 $this->user
->getName()
206 $entry = $this->newLogEntry( 'param', $params );
207 $formatter = LogFormatter
::newFromEntry( $entry );
208 $formatter->setContext( $this->context
);
210 $logParam = $formatter->getActionText();
212 $this->assertEquals( $expected, $logParam );
216 * @covers LogFormatter::newFromEntry
217 * @covers LogFormatter::getActionText
219 public function testLogParamsTypeTitleLink() {
220 $params = [ '4:title-link:titleLink' => $this->title
->getText() ];
221 $expected = Linker
::link( $this->title
, null, [], [] );
223 $entry = $this->newLogEntry( 'param', $params );
224 $formatter = LogFormatter
::newFromEntry( $entry );
225 $formatter->setContext( $this->context
);
227 $logParam = $formatter->getActionText();
229 $this->assertEquals( $expected, $logParam );
233 * @covers LogFormatter::newFromEntry
234 * @covers LogFormatter::getActionText
236 public function testLogParamsTypePlain() {
237 $params = [ '4:plain:plain' => 'Some plain text' ];
238 $expected = 'Some plain text';
240 $entry = $this->newLogEntry( 'param', $params );
241 $formatter = LogFormatter
::newFromEntry( $entry );
242 $formatter->setContext( $this->context
);
244 $logParam = $formatter->getActionText();
246 $this->assertEquals( $expected, $logParam );
250 * @covers LogFormatter::newFromEntry
251 * @covers LogFormatter::getComment
253 public function testLogComment() {
254 $entry = $this->newLogEntry( 'test', [] );
255 $formatter = LogFormatter
::newFromEntry( $entry );
256 $formatter->setContext( $this->context
);
258 $comment = ltrim( Linker
::commentBlock( $entry->getComment() ) );
260 $this->assertEquals( $comment, $formatter->getComment() );
264 * @dataProvider provideApiParamFormatting
265 * @covers LogFormatter::formatParametersForApi
266 * @covers LogFormatter::formatParameterValueForApi
268 public function testApiParamFormatting( $key, $value, $expected ) {
269 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
270 $formatter = LogFormatter
::newFromEntry( $entry );
271 $formatter->setContext( $this->context
);
273 ApiResult
::setIndexedTagName( $expected, 'param' );
274 ApiResult
::setArrayType( $expected, 'assoc' );
276 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
279 public static function provideApiParamFormatting() {
281 [ 0, 'value', [ 'value' ] ],
282 [ 'named', 'value', [ 'named' => 'value' ] ],
283 [ '::key', 'value', [ 'key' => 'value' ] ],
284 [ '4::key', 'value', [ 'key' => 'value' ] ],
285 [ '4:raw:key', 'value', [ 'key' => 'value' ] ],
286 [ '4:plain:key', 'value', [ 'key' => 'value' ] ],
287 [ '4:bool:key', '1', [ 'key' => true ] ],
288 [ '4:bool:key', '0', [ 'key' => false ] ],
289 [ '4:number:key', '123', [ 'key' => 123 ] ],
290 [ '4:number:key', '123.5', [ 'key' => 123.5 ] ],
291 [ '4:array:key', [], [ 'key' => [ ApiResult
::META_TYPE
=> 'array' ] ] ],
292 [ '4:assoc:key', [], [ 'key' => [ ApiResult
::META_TYPE
=> 'assoc' ] ] ],
293 [ '4:kvp:key', [], [ 'key' => [ ApiResult
::META_TYPE
=> 'kvp' ] ] ],
294 [ '4:timestamp:key', '20150102030405', [ 'key' => '2015-01-02T03:04:05Z' ] ],
295 [ '4:msg:key', 'parentheses', [
296 'key_key' => 'parentheses',
297 'key_text' => wfMessage( 'parentheses' )->text(),
299 [ '4:msg-content:key', 'parentheses', [
300 'key_key' => 'parentheses',
301 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
303 [ '4:title:key', 'project:foo', [
304 'key_ns' => NS_PROJECT
,
305 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
307 [ '4:title-link:key', 'project:foo', [
308 'key_ns' => NS_PROJECT
,
309 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
311 [ '4:title-link:key', '<invalid>', [
312 'key_ns' => NS_SPECIAL
,
313 'key_title' => SpecialPage
::getTitleFor( 'Badtitle', '<invalid>' )->getFullText(),
315 [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
316 [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
321 * The testIrcMsgForAction* tests are supposed to cover the hacky
322 * LogFormatter::getIRCActionText / T36508
324 * Third parties bots listen to those messages. They are clever enough
325 * to fetch the i18n messages from the wiki and then analyze the IRC feed
326 * to reverse engineer the $1, $2 messages.
327 * One thing bots can not detect is when MediaWiki change the meaning of
328 * a message like what happened when we deployed 1.19. $1 became the user
329 * performing the action which broke basically all bots around.
331 * Should cover the following log actions (which are most commonly used by bots):
339 * - newusers/autocreate
343 * - protect/modifyprotect
344 * - protect/unprotect
345 * - protect/move_prot
351 * As well as the following Auto Edit Summaries:
359 * @covers LogFormatter::getIRCActionComment
360 * @covers LogFormatter::getIRCActionText
362 public function testIrcMsgForLogTypeBlock() {
363 $sep = $this->context
->msg( 'colon-separator' )->text();
366 $this->assertIRCComment(
367 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
368 . $sep . $this->user_comment
,
371 '5::duration' => 'duration',
372 '6::flags' => 'flags',
376 # block/block - legacy
377 $this->assertIRCComment(
378 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
379 . $sep . $this->user_comment
,
390 $this->assertIRCComment(
391 $this->context
->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
397 $this->assertIRCComment(
398 $this->context
->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
399 . $sep . $this->user_comment
,
402 '5::duration' => 'duration',
403 '6::flags' => 'flags',
410 * @covers LogFormatter::getIRCActionComment
411 * @covers LogFormatter::getIRCActionText
413 public function testIrcMsgForLogTypeDelete() {
414 $sep = $this->context
->msg( 'colon-separator' )->text();
417 $this->assertIRCComment(
418 $this->context
->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
425 $this->assertIRCComment(
426 $this->context
->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
434 * @covers LogFormatter::getIRCActionComment
435 * @covers LogFormatter::getIRCActionText
437 public function testIrcMsgForLogTypeNewusers() {
438 $this->assertIRCComment(
440 'newusers', 'newusers',
443 $this->assertIRCComment(
445 'newusers', 'create',
448 $this->assertIRCComment(
449 'created new account SomeTitle',
450 'newusers', 'create2',
453 $this->assertIRCComment(
454 'Account created automatically',
455 'newusers', 'autocreate',
461 * @covers LogFormatter::getIRCActionComment
462 * @covers LogFormatter::getIRCActionText
464 public function testIrcMsgForLogTypeMove() {
466 '4::target' => $this->target
->getPrefixedText(),
469 $sep = $this->context
->msg( 'colon-separator' )->text();
472 $this->assertIRCComment(
473 $this->context
->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
474 ->plain() . $sep . $this->user_comment
,
481 $this->assertIRCComment(
482 $this->context
->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
483 ->plain() . $sep . $this->user_comment
,
484 'move', 'move_redir',
491 * @covers LogFormatter::getIRCActionComment
492 * @covers LogFormatter::getIRCActionText
494 public function testIrcMsgForLogTypePatrol() {
496 $this->assertIRCComment(
497 $this->context
->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
501 '5::previd' => '666',
508 * @covers LogFormatter::getIRCActionComment
509 * @covers LogFormatter::getIRCActionText
511 public function testIrcMsgForLogTypeProtect() {
513 '4::description' => '[edit=sysop] (indefinite) [move=sysop] (indefinite)'
515 $sep = $this->context
->msg( 'colon-separator' )->text();
518 $this->assertIRCComment(
519 $this->context
->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
520 ->plain() . $sep . $this->user_comment
,
521 'protect', 'protect',
527 $this->assertIRCComment(
528 $this->context
->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
529 'protect', 'unprotect',
535 $this->assertIRCComment(
537 'modifiedarticleprotection',
538 'SomeTitle ' . $protectParams['4::description']
539 )->plain() . $sep . $this->user_comment
,
546 $this->assertIRCComment(
547 $this->context
->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
548 ->plain() . $sep . $this->user_comment
,
549 'protect', 'move_prot',
551 '4::oldtitle' => 'OldTitle'
558 * @covers LogFormatter::getIRCActionComment
559 * @covers LogFormatter::getIRCActionText
561 public function testIrcMsgForLogTypeUpload() {
562 $sep = $this->context
->msg( 'colon-separator' )->text();
565 $this->assertIRCComment(
566 $this->context
->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
573 $this->assertIRCComment(
574 $this->context
->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
575 'upload', 'overwrite',
582 * @covers LogFormatter::getIRCActionComment
583 * @covers LogFormatter::getIRCActionText
585 public function testIrcMsgForLogTypeMerge() {
586 $sep = $this->context
->msg( 'colon-separator' )->text();
589 $this->assertIRCComment(
590 $this->context
->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
591 . $sep . $this->user_comment
,
595 '5::mergepoint' => 'timestamp',
602 * @covers LogFormatter::getIRCActionComment
603 * @covers LogFormatter::getIRCActionText
605 public function testIrcMsgForLogTypeImport() {
606 $sep = $this->context
->msg( 'colon-separator' )->text();
609 $msg = $this->context
->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
612 $this->assertIRCComment(
620 $msg = $this->context
->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
623 $this->assertIRCComment(
625 'import', 'interwiki',
632 * @param string $expected Expected IRC text without colors codes
633 * @param string $type Log type (move, delete, suppress, patrol ...)
634 * @param string $action A log type action
635 * @param array $params
636 * @param string $comment (optional) A comment for the log action
637 * @param string $msg (optional) A message for PHPUnit :-)
639 protected function assertIRCComment( $expected, $type, $action, $params,
640 $comment = null, $msg = '', $legacy = false
642 $logEntry = new ManualLogEntry( $type, $action );
643 $logEntry->setPerformer( $this->user
);
644 $logEntry->setTarget( $this->title
);
645 if ( $comment !== null ) {
646 $logEntry->setComment( $comment );
648 $logEntry->setParameters( $params );
649 $logEntry->setLegacy( $legacy );
651 $formatter = LogFormatter
::newFromEntry( $logEntry );
652 $formatter->setContext( $this->context
);
654 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
655 $ircRcComment = IRCColourfulRCFeedFormatter
::cleanupForIRC( $formatter->getIRCActionComment() );