6 class LogFormatterTest
extends MediaWikiLangTestCase
{
31 protected $user_comment;
33 protected function setUp() {
38 $this->setMwGlobals( array(
39 'wgLogTypes' => array( 'phpunit' ),
40 'wgLogActionsHandlers' => array( 'phpunit/test' => 'LogFormatter',
41 'phpunit/param' => 'LogFormatter' ),
42 'wgUser' => User
::newFromName( 'Testuser' ),
43 'wgExtensionMessagesFiles' => array( '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', array() );
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, array(), array() );
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 = array( '4:raw:raw' => Linker
::link( $this->title
, null, array(), array() ) );
125 $expected = Linker
::link( $this->title
, null, array(), array() );
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 = array( '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 = array( '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 = array( '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 = array( '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 = array( '4:title-link:titleLink' => $this->title
->getText() );
215 $expected = Linker
::link( $this->title
, null, array(), array() );
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 = array( '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', array() );
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', array( $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 array( 0, 'value', array( 'value' ) ),
276 array( 'named', 'value', array( 'named' => 'value' ) ),
277 array( '::key', 'value', array( 'key' => 'value' ) ),
278 array( '4::key', 'value', array( 'key' => 'value' ) ),
279 array( '4:raw:key', 'value', array( 'key' => 'value' ) ),
280 array( '4:plain:key', 'value', array( 'key' => 'value' ) ),
281 array( '4:bool:key', '1', array( 'key' => true ) ),
282 array( '4:bool:key', '0', array( 'key' => false ) ),
283 array( '4:number:key', '123', array( 'key' => 123 ) ),
284 array( '4:number:key', '123.5', array( 'key' => 123.5 ) ),
285 array( '4:array:key', array(), array( 'key' => array( ApiResult
::META_TYPE
=> 'array' ) ) ),
286 array( '4:assoc:key', array(), array( 'key' => array( ApiResult
::META_TYPE
=> 'assoc' ) ) ),
287 array( '4:kvp:key', array(), array( 'key' => array( ApiResult
::META_TYPE
=> 'kvp' ) ) ),
288 array( '4:timestamp:key', '20150102030405', array( 'key' => '2015-01-02T03:04:05Z' ) ),
289 array( '4:msg:key', 'parentheses', array(
290 'key_key' => 'parentheses',
291 'key_text' => wfMessage( 'parentheses' )->text(),
293 array( '4:msg-content:key', 'parentheses', array(
294 'key_key' => 'parentheses',
295 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
297 array( '4:title:key', 'project:foo', array(
298 'key_ns' => NS_PROJECT
,
299 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
301 array( '4:title-link:key', 'project:foo', array(
302 'key_ns' => NS_PROJECT
,
303 'key_title' => Title
::newFromText( 'project:foo' )->getFullText(),
305 array( '4:user:key', 'foo', array( 'key' => 'Foo' ) ),
306 array( '4:user-link:key', 'foo', array( 'key' => 'Foo' ) ),
311 * @covers LogFormatter::getIRCActionComment
312 * @covers LogFormatter::getIRCActionText
314 public function testIrcMsgForLogTypeBlock() {
315 $sep = $this->context
->msg( 'colon-separator' )->text();
318 $this->assertIRCComment(
319 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
320 . $sep . $this->user_comment
,
323 '5::duration' => 'duration',
324 '6::flags' => 'flags',
328 # block/block - legacy
329 $this->assertIRCComment(
330 $this->context
->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
331 . $sep . $this->user_comment
,
342 $this->assertIRCComment(
343 $this->context
->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
349 $this->assertIRCComment(
350 $this->context
->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
351 . $sep . $this->user_comment
,
354 '5::duration' => 'duration',
355 '6::flags' => 'flags',
362 * @covers LogFormatter::getIRCActionComment
363 * @covers LogFormatter::getIRCActionText
365 public function testIrcMsgForLogTypeDelete() {
366 $sep = $this->context
->msg( 'colon-separator' )->text();
369 $this->assertIRCComment(
370 $this->context
->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
377 $this->assertIRCComment(
378 $this->context
->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
386 * @covers LogFormatter::getIRCActionComment
387 * @covers LogFormatter::getIRCActionText
389 public function testIrcMsgForLogTypeNewusers() {
390 $this->assertIRCComment(
392 'newusers', 'newusers',
395 $this->assertIRCComment(
397 'newusers', 'create',
400 $this->assertIRCComment(
401 'created new account SomeTitle',
402 'newusers', 'create2',
405 $this->assertIRCComment(
406 'Account created automatically',
407 'newusers', 'autocreate',
413 * @covers LogFormatter::getIRCActionComment
414 * @covers LogFormatter::getIRCActionText
416 public function testIrcMsgForLogTypeMove() {
417 $move_params = array(
418 '4::target' => $this->target
->getPrefixedText(),
421 $sep = $this->context
->msg( 'colon-separator' )->text();
424 $this->assertIRCComment(
425 $this->context
->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
426 ->plain() . $sep . $this->user_comment
,
433 $this->assertIRCComment(
434 $this->context
->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
435 ->plain() . $sep . $this->user_comment
,
436 'move', 'move_redir',
443 * @covers LogFormatter::getIRCActionComment
444 * @covers LogFormatter::getIRCActionText
446 public function testIrcMsgForLogTypePatrol() {
448 $this->assertIRCComment(
449 $this->context
->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
453 '5::previd' => '666',
460 * @covers LogFormatter::getIRCActionComment
461 * @covers LogFormatter::getIRCActionText
463 public function testIrcMsgForLogTypeProtect() {
464 $protectParams = array(
465 '4::description' => '[edit=sysop] (indefinite) [move=sysop] (indefinite)'
467 $sep = $this->context
->msg( 'colon-separator' )->text();
470 $this->assertIRCComment(
471 $this->context
->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
472 ->plain() . $sep . $this->user_comment
,
473 'protect', 'protect',
479 $this->assertIRCComment(
480 $this->context
->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
481 'protect', 'unprotect',
487 $this->assertIRCComment(
488 $this->context
->msg( 'modifiedarticleprotection', 'SomeTitle ' . $protectParams['4::description'] )
489 ->plain() . $sep . $this->user_comment
,
496 $this->assertIRCComment(
497 $this->context
->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
498 ->plain() . $sep . $this->user_comment
,
499 'protect', 'move_prot',
501 '4::oldtitle' => 'OldTitle'
508 * @covers LogFormatter::getIRCActionComment
509 * @covers LogFormatter::getIRCActionText
511 public function testIrcMsgForLogTypeUpload() {
512 $sep = $this->context
->msg( 'colon-separator' )->text();
515 $this->assertIRCComment(
516 $this->context
->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
523 $this->assertIRCComment(
524 $this->context
->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment
,
525 'upload', 'overwrite',
532 * @covers LogFormatter::getIRCActionComment
533 * @covers LogFormatter::getIRCActionText
535 public function testIrcMsgForLogTypeMerge() {
536 $sep = $this->context
->msg( 'colon-separator' )->text();
539 $this->assertIRCComment(
540 $this->context
->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
541 . $sep . $this->user_comment
,
545 '5::mergepoint' => 'timestamp',
552 * @covers LogFormatter::getIRCActionComment
553 * @covers LogFormatter::getIRCActionText
555 public function testIrcMsgForLogTypeImport() {
556 $sep = $this->context
->msg( 'colon-separator' )->text();
559 $msg = $this->context
->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
562 $this->assertIRCComment(
570 $msg = $this->context
->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
573 $this->assertIRCComment(
575 'import', 'interwiki',
582 * @param string $expected Expected IRC text without colors codes
583 * @param string $type Log type (move, delete, suppress, patrol ...)
584 * @param string $action A log type action
585 * @param array $params
586 * @param string $comment (optional) A comment for the log action
587 * @param string $msg (optional) A message for PHPUnit :-)
589 protected function assertIRCComment( $expected, $type, $action, $params,
590 $comment = null, $msg = '', $legacy = false
592 $logEntry = new ManualLogEntry( $type, $action );
593 $logEntry->setPerformer( $this->user
);
594 $logEntry->setTarget( $this->title
);
595 if ( $comment !== null ) {
596 $logEntry->setComment( $comment );
598 $logEntry->setParameters( $params );
599 $logEntry->setLegacy( $legacy );
601 $formatter = LogFormatter
::newFromEntry( $logEntry );
602 $formatter->setContext( $this->context
);
604 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
605 $ircRcComment = IRCColourfulRCFeedFormatter
::cleanupForIRC( $formatter->getIRCActionComment() );