6 class LogFormatterTest
extends MediaWikiLangTestCase
{
31 protected $user_comment;
33 protected function setUp() {
38 $this->setMwGlobals( [
39 'wgLogTypes' => [ 'phpunit' ],
40 'wgLogActionsHandlers' => [ 'phpunit/test' => 'LogFormatter',
41 'phpunit/param' => 'LogFormatter' ],
42 'wgUser' => User
::newFromName( 'Testuser' ),
43 'wgExtensionMessagesFiles' => [ 'LogTests' => __DIR__
. '/LogTests.i18n.php' ],
46 Language
::getLocalisationCache()->recache( $wgLang->getCode() );
48 $this->user
= User
::newFromName( 'Testuser' );
49 $this->title
= Title
::newFromText( 'SomeTitle' );
50 $this->target
= Title
::newFromText( 'TestTarget' );
52 $this->context
= new RequestContext();
53 $this->context
->setUser( $this->user
);
54 $this->context
->setTitle( $this->title
);
55 $this->context
->setLanguage( $wgLang );
57 $this->user_comment
= '<User comment about action>';
60 protected function tearDown() {
64 Language
::getLocalisationCache()->recache( $wgLang->getCode() );
67 public function newLogEntry( $action, $params ) {
68 $logEntry = new ManualLogEntry( 'phpunit', $action );
69 $logEntry->setPerformer( $this->user
);
70 $logEntry->setTarget( $this->title
);
71 $logEntry->setComment( 'A very good reason' );
73 $logEntry->setParameters( $params );
79 * @covers LogFormatter::newFromEntry
81 public function testNormalLogParams() {
82 $entry = $this->newLogEntry( 'test', [] );
83 $formatter = LogFormatter
::newFromEntry( $entry );
84 $formatter->setContext( $this->context
);
86 $formatter->setShowUserToolLinks( false );
87 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
88 unset( $formatter->parsedParameters
);
90 $formatter->setShowUserToolLinks( true );
91 $paramsWithTools = $formatter->getMessageParametersForTesting();
93 $userLink = Linker
::userLink(
95 $this->user
->getName()
98 $userTools = Linker
::userToolLinksRedContribs(
100 $this->user
->getName(),
101 $this->user
->getEditCount()
104 $titleLink = Linker
::link( $this->title
, null, [], [] );
106 // $paramsWithoutTools and $paramsWithTools should be only different
108 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
109 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
111 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
112 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
114 $this->assertEquals( $this->user
->getName(), $paramsWithoutTools[1] );
116 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
120 * @covers LogFormatter::newFromEntry
121 * @covers LogFormatter::getActionText
123 public function testLogParamsTypeRaw() {
124 $params = [ '4:raw:raw' => Linker
::link( $this->title
, null, [], [] ) ];
125 $expected = Linker
::link( $this->title
, null, [], [] );
127 $entry = $this->newLogEntry( 'param', $params );
128 $formatter = LogFormatter
::newFromEntry( $entry );
129 $formatter->setContext( $this->context
);
131 $logParam = $formatter->getActionText();
133 $this->assertEquals( $expected, $logParam );
137 * @covers LogFormatter::newFromEntry
138 * @covers LogFormatter::getActionText
140 public function testLogParamsTypeMsg() {
141 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
142 $expected = wfMessage( 'log-description-phpunit' )->text();
144 $entry = $this->newLogEntry( 'param', $params );
145 $formatter = LogFormatter
::newFromEntry( $entry );
146 $formatter->setContext( $this->context
);
148 $logParam = $formatter->getActionText();
150 $this->assertEquals( $expected, $logParam );
154 * @covers LogFormatter::newFromEntry
155 * @covers LogFormatter::getActionText
157 public function testLogParamsTypeMsgContent() {
158 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
159 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
161 $entry = $this->newLogEntry( 'param', $params );
162 $formatter = LogFormatter
::newFromEntry( $entry );
163 $formatter->setContext( $this->context
);
165 $logParam = $formatter->getActionText();
167 $this->assertEquals( $expected, $logParam );
171 * @covers LogFormatter::newFromEntry
172 * @covers LogFormatter::getActionText
174 public function testLogParamsTypeNumber() {
177 $params = [ '4:number:number' => 123456789 ];
178 $expected = $wgLang->formatNum( 123456789 );
180 $entry = $this->newLogEntry( 'param', $params );
181 $formatter = LogFormatter
::newFromEntry( $entry );
182 $formatter->setContext( $this->context
);
184 $logParam = $formatter->getActionText();
186 $this->assertEquals( $expected, $logParam );
190 * @covers LogFormatter::newFromEntry
191 * @covers LogFormatter::getActionText
193 public function testLogParamsTypeUserLink() {
194 $params = [ '4:user-link:userLink' => $this->user
->getName() ];
195 $expected = Linker
::userLink(
196 $this->user
->getId(),
197 $this->user
->getName()
200 $entry = $this->newLogEntry( 'param', $params );
201 $formatter = LogFormatter
::newFromEntry( $entry );
202 $formatter->setContext( $this->context
);
204 $logParam = $formatter->getActionText();
206 $this->assertEquals( $expected, $logParam );
210 * @covers LogFormatter::newFromEntry
211 * @covers LogFormatter::getActionText
213 public function testLogParamsTypeTitleLink() {
214 $params = [ '4:title-link:titleLink' => $this->title
->getText() ];
215 $expected = Linker
::link( $this->title
, null, [], [] );
217 $entry = $this->newLogEntry( 'param', $params );
218 $formatter = LogFormatter
::newFromEntry( $entry );
219 $formatter->setContext( $this->context
);
221 $logParam = $formatter->getActionText();
223 $this->assertEquals( $expected, $logParam );
227 * @covers LogFormatter::newFromEntry
228 * @covers LogFormatter::getActionText
230 public function testLogParamsTypePlain() {
231 $params = [ '4:plain:plain' => 'Some plain text' ];
232 $expected = 'Some plain text';
234 $entry = $this->newLogEntry( 'param', $params );
235 $formatter = LogFormatter
::newFromEntry( $entry );
236 $formatter->setContext( $this->context
);
238 $logParam = $formatter->getActionText();
240 $this->assertEquals( $expected, $logParam );
244 * @covers LogFormatter::newFromEntry
245 * @covers LogFormatter::getComment
247 public function testLogComment() {
248 $entry = $this->newLogEntry( 'test', [] );
249 $formatter = LogFormatter
::newFromEntry( $entry );
250 $formatter->setContext( $this->context
);
252 $comment = ltrim( Linker
::commentBlock( $entry->getComment() ) );
254 $this->assertEquals( $comment, $formatter->getComment() );
258 * @dataProvider provideApiParamFormatting
259 * @covers LogFormatter::formatParametersForApi
260 * @covers LogFormatter::formatParameterValueForApi
262 public function testApiParamFormatting( $key, $value, $expected ) {
263 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
264 $formatter = LogFormatter
::newFromEntry( $entry );
265 $formatter->setContext( $this->context
);
267 ApiResult
::setIndexedTagName( $expected, 'param' );
268 ApiResult
::setArrayType( $expected, 'assoc' );
270 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
273 public static function provideApiParamFormatting() {
275 [ 0, 'value', [ 'value' ] ],
276 [ 'named', 'value', [ 'named' => 'value' ] ],
277 [ '::key', 'value', [ 'key' => 'value' ] ],
278 [ '4::key', 'value', [ 'key' => 'value' ] ],
279 [ '4:raw:key', 'value', [ 'key' => 'value' ] ],
280 [ '4:plain:key', 'value', [ 'key' => 'value' ] ],
281 [ '4:bool:key', '1', [ 'key' => true ] ],
282 [ '4:bool:key', '0', [ 'key' => false ] ],
283 [ '4:number:key', '123', [ 'key' => 123 ] ],
284 [ '4:number:key', '123.5', [ 'key' => 123.5 ] ],
285 [ '4:array:key', [], [ 'key' => [ ApiResult
::META_TYPE
=> 'array' ] ] ],
286 [ '4:assoc:key', [], [ 'key' => [ ApiResult
::META_TYPE
=> 'assoc' ] ] ],
287 [ '4:kvp:key', [], [ 'key' => [ ApiResult
::META_TYPE
=> 'kvp' ] ] ],
288 [ '4:timestamp:key', '20150102030405', [ 'key' => '2015-01-02T03:04:05Z' ] ],
289 [ '4:msg:key', 'parentheses', [
290 'key_key' => 'parentheses',
291 'key_text' => wfMessage( 'parentheses' )->text(),
293 [ '4:msg-content:key', 'parentheses', [
294 'key_key' => 'parentheses',
295 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
297 [ '4:title:key', 'project:foo', [
298 'key_ns' => NS_PROJECT
,
299 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
301 [ '4:title-link:key', 'project:foo', [
302 'key_ns' => NS_PROJECT
,
303 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
305 [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
306 [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
311 * The testIrcMsgForAction* tests are supposed to cover the hacky
312 * LogFormatter::getIRCActionText / bug 34508
314 * Third parties bots listen to those messages. They are clever enough
315 * to fetch the i18n messages from the wiki and then analyze the IRC feed
316 * to reverse engineer the $1, $2 messages.
317 * One thing bots can not detect is when MediaWiki change the meaning of
318 * a message like what happened when we deployed 1.19. $1 became the user
319 * performing the action which broke basically all bots around.
321 * Should cover the following log actions (which are most commonly used by bots):
329 * - newusers/autocreate
333 * - protect/modifyprotect
334 * - protect/unprotect
335 * - protect/move_prot
341 * As well as the following Auto Edit Summaries:
349 * @covers LogFormatter::getIRCActionComment
350 * @covers LogFormatter::getIRCActionText
352 public function testIrcMsgForLogTypeBlock() {
353 $sep = $this->context
->msg( 'colon-separator' )->text();
356 $this->assertIRCComment(
357 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
358 . $sep . $this->user_comment
,
361 '5::duration' => 'duration',
362 '6::flags' => 'flags',
366 # block/block - legacy
367 $this->assertIRCComment(
368 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
369 . $sep . $this->user_comment
,
380 $this->assertIRCComment(
381 $this->context
->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
387 $this->assertIRCComment(
388 $this->context
->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
389 . $sep . $this->user_comment
,
392 '5::duration' => 'duration',
393 '6::flags' => 'flags',
400 * @covers LogFormatter::getIRCActionComment
401 * @covers LogFormatter::getIRCActionText
403 public function testIrcMsgForLogTypeDelete() {
404 $sep = $this->context
->msg( 'colon-separator' )->text();
407 $this->assertIRCComment(
408 $this->context
->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
415 $this->assertIRCComment(
416 $this->context
->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
424 * @covers LogFormatter::getIRCActionComment
425 * @covers LogFormatter::getIRCActionText
427 public function testIrcMsgForLogTypeNewusers() {
428 $this->assertIRCComment(
430 'newusers', 'newusers',
433 $this->assertIRCComment(
435 'newusers', 'create',
438 $this->assertIRCComment(
439 'created new account SomeTitle',
440 'newusers', 'create2',
443 $this->assertIRCComment(
444 'Account created automatically',
445 'newusers', 'autocreate',
451 * @covers LogFormatter::getIRCActionComment
452 * @covers LogFormatter::getIRCActionText
454 public function testIrcMsgForLogTypeMove() {
456 '4::target' => $this->target
->getPrefixedText(),
459 $sep = $this->context
->msg( 'colon-separator' )->text();
462 $this->assertIRCComment(
463 $this->context
->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
464 ->plain() . $sep . $this->user_comment
,
471 $this->assertIRCComment(
472 $this->context
->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
473 ->plain() . $sep . $this->user_comment
,
474 'move', 'move_redir',
481 * @covers LogFormatter::getIRCActionComment
482 * @covers LogFormatter::getIRCActionText
484 public function testIrcMsgForLogTypePatrol() {
486 $this->assertIRCComment(
487 $this->context
->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
491 '5::previd' => '666',
498 * @covers LogFormatter::getIRCActionComment
499 * @covers LogFormatter::getIRCActionText
501 public function testIrcMsgForLogTypeProtect() {
503 '4::description' => '[edit=sysop] (indefinite) [move=sysop] (indefinite)'
505 $sep = $this->context
->msg( 'colon-separator' )->text();
508 $this->assertIRCComment(
509 $this->context
->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
510 ->plain() . $sep . $this->user_comment
,
511 'protect', 'protect',
517 $this->assertIRCComment(
518 $this->context
->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
519 'protect', 'unprotect',
525 $this->assertIRCComment(
527 'modifiedarticleprotection',
528 'SomeTitle ' . $protectParams['4::description']
529 )->plain() . $sep . $this->user_comment
,
536 $this->assertIRCComment(
537 $this->context
->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
538 ->plain() . $sep . $this->user_comment
,
539 'protect', 'move_prot',
541 '4::oldtitle' => 'OldTitle'
548 * @covers LogFormatter::getIRCActionComment
549 * @covers LogFormatter::getIRCActionText
551 public function testIrcMsgForLogTypeUpload() {
552 $sep = $this->context
->msg( 'colon-separator' )->text();
555 $this->assertIRCComment(
556 $this->context
->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
563 $this->assertIRCComment(
564 $this->context
->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
565 'upload', 'overwrite',
572 * @covers LogFormatter::getIRCActionComment
573 * @covers LogFormatter::getIRCActionText
575 public function testIrcMsgForLogTypeMerge() {
576 $sep = $this->context
->msg( 'colon-separator' )->text();
579 $this->assertIRCComment(
580 $this->context
->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
581 . $sep . $this->user_comment
,
585 '5::mergepoint' => 'timestamp',
592 * @covers LogFormatter::getIRCActionComment
593 * @covers LogFormatter::getIRCActionText
595 public function testIrcMsgForLogTypeImport() {
596 $sep = $this->context
->msg( 'colon-separator' )->text();
599 $msg = $this->context
->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
602 $this->assertIRCComment(
610 $msg = $this->context
->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
613 $this->assertIRCComment(
615 'import', 'interwiki',
622 * @param string $expected Expected IRC text without colors codes
623 * @param string $type Log type (move, delete, suppress, patrol ...)
624 * @param string $action A log type action
625 * @param array $params
626 * @param string $comment (optional) A comment for the log action
627 * @param string $msg (optional) A message for PHPUnit :-)
629 protected function assertIRCComment( $expected, $type, $action, $params,
630 $comment = null, $msg = '', $legacy = false
632 $logEntry = new ManualLogEntry( $type, $action );
633 $logEntry->setPerformer( $this->user
);
634 $logEntry->setTarget( $this->title
);
635 if ( $comment !== null ) {
636 $logEntry->setComment( $comment );
638 $logEntry->setParameters( $params );
639 $logEntry->setLegacy( $legacy );
641 $formatter = LogFormatter
::newFromEntry( $logEntry );
642 $formatter->setContext( $this->context
);
644 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
645 $ircRcComment = IRCColourfulRCFeedFormatter
::cleanupForIRC( $formatter->getIRCActionComment() );