MediaWiki REL1_30
LogFormatterTest.php
Go to the documentation of this file.
1<?php
2
7 private static $oldExtMsgFiles;
8
12 protected $user;
13
17 protected $title;
18
22 protected $context;
23
27 protected $target;
28
32 protected $user_comment;
33
34 public static function setUpBeforeClass() {
35 parent::setUpBeforeClass();
36
38 self::$oldExtMsgFiles = $wgExtensionMessagesFiles;
39 $wgExtensionMessagesFiles['LogTests'] = __DIR__ . '/LogTests.i18n.php';
40 Language::getLocalisationCache()->recache( 'en' );
41 }
42
43 public static function tearDownAfterClass() {
46 Language::getLocalisationCache()->recache( 'en' );
47
48 parent::tearDownAfterClass();
49 }
50
51 protected function setUp() {
52 parent::setUp();
53
54 $this->setMwGlobals( [
55 'wgLogTypes' => [ 'phpunit' ],
56 'wgLogActionsHandlers' => [ 'phpunit/test' => 'LogFormatter',
57 'phpunit/param' => 'LogFormatter' ],
58 'wgUser' => User::newFromName( 'Testuser' ),
59 ] );
60
61 $this->user = User::newFromName( 'Testuser' );
62 $this->title = Title::newFromText( 'SomeTitle' );
63 $this->target = Title::newFromText( 'TestTarget' );
64
65 $this->context = new RequestContext();
66 $this->context->setUser( $this->user );
67 $this->context->setTitle( $this->title );
68 $this->context->setLanguage( RequestContext::getMain()->getLanguage() );
69
70 $this->user_comment = '<User comment about action>';
71 }
72
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' );
78
79 $logEntry->setParameters( $params );
80
81 return $logEntry;
82 }
83
87 public function testNormalLogParams() {
88 $entry = $this->newLogEntry( 'test', [] );
89 $formatter = LogFormatter::newFromEntry( $entry );
90 $formatter->setContext( $this->context );
91
92 $formatter->setShowUserToolLinks( false );
93 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
94 unset( $formatter->parsedParameters );
95
96 $formatter->setShowUserToolLinks( true );
97 $paramsWithTools = $formatter->getMessageParametersForTesting();
98
99 $userLink = Linker::userLink(
100 $this->user->getId(),
101 $this->user->getName()
102 );
103
105 $this->user->getId(),
106 $this->user->getName(),
107 $this->user->getEditCount()
108 );
109
110 $titleLink = Linker::link( $this->title, null, [], [] );
111
112 // $paramsWithoutTools and $paramsWithTools should be only different
113 // in index 0
114 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
115 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
116
117 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
118 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
119
120 $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
121
122 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
123 }
124
129 public function testLogParamsTypeRaw() {
130 $params = [ '4:raw:raw' => Linker::link( $this->title, null, [], [] ) ];
131 $expected = Linker::link( $this->title, null, [], [] );
132
133 $entry = $this->newLogEntry( 'param', $params );
134 $formatter = LogFormatter::newFromEntry( $entry );
135 $formatter->setContext( $this->context );
136
137 $logParam = $formatter->getActionText();
138
139 $this->assertEquals( $expected, $logParam );
140 }
141
146 public function testLogParamsTypeMsg() {
147 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
148 $expected = wfMessage( 'log-description-phpunit' )->text();
149
150 $entry = $this->newLogEntry( 'param', $params );
151 $formatter = LogFormatter::newFromEntry( $entry );
152 $formatter->setContext( $this->context );
153
154 $logParam = $formatter->getActionText();
155
156 $this->assertEquals( $expected, $logParam );
157 }
158
163 public function testLogParamsTypeMsgContent() {
164 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
165 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
166
167 $entry = $this->newLogEntry( 'param', $params );
168 $formatter = LogFormatter::newFromEntry( $entry );
169 $formatter->setContext( $this->context );
170
171 $logParam = $formatter->getActionText();
172
173 $this->assertEquals( $expected, $logParam );
174 }
175
180 public function testLogParamsTypeNumber() {
181 global $wgLang;
182
183 $params = [ '4:number:number' => 123456789 ];
184 $expected = $wgLang->formatNum( 123456789 );
185
186 $entry = $this->newLogEntry( 'param', $params );
187 $formatter = LogFormatter::newFromEntry( $entry );
188 $formatter->setContext( $this->context );
189
190 $logParam = $formatter->getActionText();
191
192 $this->assertEquals( $expected, $logParam );
193 }
194
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()
204 );
205
206 $entry = $this->newLogEntry( 'param', $params );
207 $formatter = LogFormatter::newFromEntry( $entry );
208 $formatter->setContext( $this->context );
209
210 $logParam = $formatter->getActionText();
211
212 $this->assertEquals( $expected, $logParam );
213 }
214
219 public function testLogParamsTypeTitleLink() {
220 $params = [ '4:title-link:titleLink' => $this->title->getText() ];
221 $expected = Linker::link( $this->title, null, [], [] );
222
223 $entry = $this->newLogEntry( 'param', $params );
224 $formatter = LogFormatter::newFromEntry( $entry );
225 $formatter->setContext( $this->context );
226
227 $logParam = $formatter->getActionText();
228
229 $this->assertEquals( $expected, $logParam );
230 }
231
236 public function testLogParamsTypePlain() {
237 $params = [ '4:plain:plain' => 'Some plain text' ];
238 $expected = 'Some plain text';
239
240 $entry = $this->newLogEntry( 'param', $params );
241 $formatter = LogFormatter::newFromEntry( $entry );
242 $formatter->setContext( $this->context );
243
244 $logParam = $formatter->getActionText();
245
246 $this->assertEquals( $expected, $logParam );
247 }
248
253 public function testLogComment() {
254 $entry = $this->newLogEntry( 'test', [] );
255 $formatter = LogFormatter::newFromEntry( $entry );
256 $formatter->setContext( $this->context );
257
258 $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
259
260 $this->assertEquals( $comment, $formatter->getComment() );
261 }
262
268 public function testApiParamFormatting( $key, $value, $expected ) {
269 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
270 $formatter = LogFormatter::newFromEntry( $entry );
271 $formatter->setContext( $this->context );
272
273 ApiResult::setIndexedTagName( $expected, 'param' );
274 ApiResult::setArrayType( $expected, 'assoc' );
275
276 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
277 }
278
279 public static function provideApiParamFormatting() {
280 return [
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(),
298 ] ],
299 [ '4:msg-content:key', 'parentheses', [
300 'key_key' => 'parentheses',
301 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
302 ] ],
303 [ '4:title:key', 'project:foo', [
304 'key_ns' => NS_PROJECT,
305 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
306 ] ],
307 [ '4:title-link:key', 'project:foo', [
308 'key_ns' => NS_PROJECT,
309 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
310 ] ],
311 [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
312 [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
313 ];
314 }
315
358 public function testIrcMsgForLogTypeBlock() {
359 $sep = $this->context->msg( 'colon-separator' )->text();
360
361 # block/block
362 $this->assertIRCComment(
363 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
364 . $sep . $this->user_comment,
365 'block', 'block',
366 [
367 '5::duration' => 'duration',
368 '6::flags' => 'flags',
369 ],
370 $this->user_comment
371 );
372 # block/block - legacy
373 $this->assertIRCComment(
374 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
375 . $sep . $this->user_comment,
376 'block', 'block',
377 [
378 'duration',
379 'flags',
380 ],
381 $this->user_comment,
382 '',
383 true
384 );
385 # block/unblock
386 $this->assertIRCComment(
387 $this->context->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
388 'block', 'unblock',
389 [],
390 $this->user_comment
391 );
392 # block/reblock
393 $this->assertIRCComment(
394 $this->context->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
395 . $sep . $this->user_comment,
396 'block', 'reblock',
397 [
398 '5::duration' => 'duration',
399 '6::flags' => 'flags',
400 ],
401 $this->user_comment
402 );
403 }
404
409 public function testIrcMsgForLogTypeDelete() {
410 $sep = $this->context->msg( 'colon-separator' )->text();
411
412 # delete/delete
413 $this->assertIRCComment(
414 $this->context->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
415 'delete', 'delete',
416 [],
417 $this->user_comment
418 );
419
420 # delete/restore
421 $this->assertIRCComment(
422 $this->context->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
423 'delete', 'restore',
424 [],
425 $this->user_comment
426 );
427 }
428
434 $this->assertIRCComment(
435 'New user account',
436 'newusers', 'newusers',
437 []
438 );
439 $this->assertIRCComment(
440 'New user account',
441 'newusers', 'create',
442 []
443 );
444 $this->assertIRCComment(
445 'created new account SomeTitle',
446 'newusers', 'create2',
447 []
448 );
449 $this->assertIRCComment(
450 'Account created automatically',
451 'newusers', 'autocreate',
452 []
453 );
454 }
455
460 public function testIrcMsgForLogTypeMove() {
461 $move_params = [
462 '4::target' => $this->target->getPrefixedText(),
463 '5::noredir' => 0,
464 ];
465 $sep = $this->context->msg( 'colon-separator' )->text();
466
467 # move/move
468 $this->assertIRCComment(
469 $this->context->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
470 ->plain() . $sep . $this->user_comment,
471 'move', 'move',
472 $move_params,
473 $this->user_comment
474 );
475
476 # move/move_redir
477 $this->assertIRCComment(
478 $this->context->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
479 ->plain() . $sep . $this->user_comment,
480 'move', 'move_redir',
481 $move_params,
482 $this->user_comment
483 );
484 }
485
490 public function testIrcMsgForLogTypePatrol() {
491 # patrol/patrol
492 $this->assertIRCComment(
493 $this->context->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
494 'patrol', 'patrol',
495 [
496 '4::curid' => '777',
497 '5::previd' => '666',
498 '6::auto' => 0,
499 ]
500 );
501 }
502
507 public function testIrcMsgForLogTypeProtect() {
508 $protectParams = [
509 '4::description' => '[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)'
510 ];
511 $sep = $this->context->msg( 'colon-separator' )->text();
512
513 # protect/protect
514 $this->assertIRCComment(
515 $this->context->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
516 ->plain() . $sep . $this->user_comment,
517 'protect', 'protect',
518 $protectParams,
519 $this->user_comment
520 );
521
522 # protect/unprotect
523 $this->assertIRCComment(
524 $this->context->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
525 'protect', 'unprotect',
526 [],
527 $this->user_comment
528 );
529
530 # protect/modify
531 $this->assertIRCComment(
532 $this->context->msg(
533 'modifiedarticleprotection',
534 'SomeTitle ' . $protectParams['4::description']
535 )->plain() . $sep . $this->user_comment,
536 'protect', 'modify',
537 $protectParams,
538 $this->user_comment
539 );
540
541 # protect/move_prot
542 $this->assertIRCComment(
543 $this->context->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
544 ->plain() . $sep . $this->user_comment,
545 'protect', 'move_prot',
546 [
547 '4::oldtitle' => 'OldTitle'
548 ],
549 $this->user_comment
550 );
551 }
552
557 public function testIrcMsgForLogTypeUpload() {
558 $sep = $this->context->msg( 'colon-separator' )->text();
559
560 # upload/upload
561 $this->assertIRCComment(
562 $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
563 'upload', 'upload',
564 [],
565 $this->user_comment
566 );
567
568 # upload/overwrite
569 $this->assertIRCComment(
570 $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
571 'upload', 'overwrite',
572 [],
573 $this->user_comment
574 );
575 }
576
581 public function testIrcMsgForLogTypeMerge() {
582 $sep = $this->context->msg( 'colon-separator' )->text();
583
584 # merge/merge
585 $this->assertIRCComment(
586 $this->context->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
587 . $sep . $this->user_comment,
588 'merge', 'merge',
589 [
590 '4::dest' => 'Dest',
591 '5::mergepoint' => 'timestamp',
592 ],
593 $this->user_comment
594 );
595 }
596
601 public function testIrcMsgForLogTypeImport() {
602 $sep = $this->context->msg( 'colon-separator' )->text();
603
604 # import/upload
605 $msg = $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
606 $sep .
608 $this->assertIRCComment(
609 $msg,
610 'import', 'upload',
611 [],
612 $this->user_comment
613 );
614
615 # import/interwiki
616 $msg = $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
617 $sep .
619 $this->assertIRCComment(
620 $msg,
621 'import', 'interwiki',
622 [],
623 $this->user_comment
624 );
625 }
626
635 protected function assertIRCComment( $expected, $type, $action, $params,
636 $comment = null, $msg = '', $legacy = false
637 ) {
638 $logEntry = new ManualLogEntry( $type, $action );
639 $logEntry->setPerformer( $this->user );
640 $logEntry->setTarget( $this->title );
641 if ( $comment !== null ) {
642 $logEntry->setComment( $comment );
643 }
644 $logEntry->setParameters( $params );
645 $logEntry->setLegacy( $legacy );
646
647 $formatter = LogFormatter::newFromEntry( $logEntry );
648 $formatter->setContext( $this->context );
649
650 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
651 $ircRcComment = IRCColourfulRCFeedFormatter::cleanupForIRC( $formatter->getIRCActionComment() );
652
653 $this->assertEquals(
654 $expected,
655 $ircRcComment,
656 $msg
657 );
658 }
659
660}
const META_TYPE
Key for the 'type' metadata item.
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
static cleanupForIRC( $text)
Remove newlines, carriage returns and decode html entites.
static link( $target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition Linker.php:107
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:893
static userToolLinksRedContribs( $userId, $userText, $edits=null)
Alias for userToolLinks( $userId, $userText, true );.
Definition Linker.php:983
static commentBlock( $comment, $title=null, $local=false, $wikiId=null)
Wrap a comment in standard punctuation and formatting if it's non-empty, otherwise return empty strin...
Definition Linker.php:1445
testIrcMsgForLogTypeMove()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testIrcMsgForLogTypePatrol()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testLogParamsTypeRaw()
LogFormatter::newFromEntry LogFormatter::getActionText.
testApiParamFormatting( $key, $value, $expected)
provideApiParamFormatting LogFormatter::formatParametersForApi LogFormatter::formatParameterValueForA...
testIrcMsgForLogTypeProtect()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testLogParamsTypeMsg()
LogFormatter::newFromEntry LogFormatter::getActionText.
testLogParamsTypePlain()
LogFormatter::newFromEntry LogFormatter::getActionText.
testIrcMsgForLogTypeDelete()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testLogParamsTypeTitleLink()
LogFormatter::newFromEntry LogFormatter::getActionText.
testIrcMsgForLogTypeImport()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
RequestContext $context
testIrcMsgForLogTypeBlock()
The testIrcMsgForAction* tests are supposed to cover the hacky LogFormatter::getIRCActionText / T3650...
testNormalLogParams()
LogFormatter::newFromEntry.
testLogParamsTypeUserLink()
LogFormatter::newFromEntry LogFormatter::getActionText.
testIrcMsgForLogTypeNewusers()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testLogParamsTypeNumber()
LogFormatter::newFromEntry LogFormatter::getActionText.
testLogComment()
LogFormatter::newFromEntry LogFormatter::getComment.
assertIRCComment( $expected, $type, $action, $params, $comment=null, $msg='', $legacy=false)
testIrcMsgForLogTypeUpload()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testIrcMsgForLogTypeMerge()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
static provideApiParamFormatting()
testLogParamsTypeMsgContent()
LogFormatter::newFromEntry LogFormatter::getActionText.
newLogEntry( $action, $params)
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
Class for creating log entries manually, to inject them into the database.
Definition LogEntry.php:400
Base class that store and restore the Language objects.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Group all the pieces relevant to the context of a request into one instance.
static getMain()
Static methods.
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition design.txt:56
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:1976
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
const NS_PROJECT
Definition Defines.php:69
$wgExtensionMessagesFiles['ExtensionNameMagic']
Definition magicword.txt:43
$params