MediaWiki REL1_33
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::class,
57 'phpunit/param' => LogFormatter::class ],
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
95 $formatter2 = LogFormatter::newFromEntry( $entry );
96 $formatter2->setContext( $this->context );
97 $formatter2->setShowUserToolLinks( true );
98 $paramsWithTools = $formatter2->getMessageParametersForTesting();
99
100 $userLink = Linker::userLink(
101 $this->user->getId(),
102 $this->user->getName()
103 );
104
106 $this->user->getId(),
107 $this->user->getName(),
108 $this->user->getEditCount(),
109 false
110 );
111
112 $titleLink = Linker::link( $this->title, null, [], [] );
113
114 // $paramsWithoutTools and $paramsWithTools should be only different
115 // in index 0
116 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
117 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
118
119 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
120 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
121
122 $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
123
124 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
125 }
126
131 public function testLogParamsTypeRaw() {
132 $params = [ '4:raw:raw' => Linker::link( $this->title, null, [], [] ) ];
133 $expected = Linker::link( $this->title, null, [], [] );
134
135 $entry = $this->newLogEntry( 'param', $params );
136 $formatter = LogFormatter::newFromEntry( $entry );
137 $formatter->setContext( $this->context );
138
139 $logParam = $formatter->getActionText();
140
141 $this->assertEquals( $expected, $logParam );
142 }
143
148 public function testLogParamsTypeMsg() {
149 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
150 $expected = wfMessage( 'log-description-phpunit' )->text();
151
152 $entry = $this->newLogEntry( 'param', $params );
153 $formatter = LogFormatter::newFromEntry( $entry );
154 $formatter->setContext( $this->context );
155
156 $logParam = $formatter->getActionText();
157
158 $this->assertEquals( $expected, $logParam );
159 }
160
165 public function testLogParamsTypeMsgContent() {
166 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
167 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
168
169 $entry = $this->newLogEntry( 'param', $params );
170 $formatter = LogFormatter::newFromEntry( $entry );
171 $formatter->setContext( $this->context );
172
173 $logParam = $formatter->getActionText();
174
175 $this->assertEquals( $expected, $logParam );
176 }
177
182 public function testLogParamsTypeNumber() {
183 global $wgLang;
184
185 $params = [ '4:number:number' => 123456789 ];
186 $expected = $wgLang->formatNum( 123456789 );
187
188 $entry = $this->newLogEntry( 'param', $params );
189 $formatter = LogFormatter::newFromEntry( $entry );
190 $formatter->setContext( $this->context );
191
192 $logParam = $formatter->getActionText();
193
194 $this->assertEquals( $expected, $logParam );
195 }
196
201 public function testLogParamsTypeUserLink() {
202 $params = [ '4:user-link:userLink' => $this->user->getName() ];
203 $expected = Linker::userLink(
204 $this->user->getId(),
205 $this->user->getName()
206 );
207
208 $entry = $this->newLogEntry( 'param', $params );
209 $formatter = LogFormatter::newFromEntry( $entry );
210 $formatter->setContext( $this->context );
211
212 $logParam = $formatter->getActionText();
213
214 $this->assertEquals( $expected, $logParam );
215 }
216
221 public function testLogParamsTypeTitleLink() {
222 $params = [ '4:title-link:titleLink' => $this->title->getText() ];
223 $expected = Linker::link( $this->title, null, [], [] );
224
225 $entry = $this->newLogEntry( 'param', $params );
226 $formatter = LogFormatter::newFromEntry( $entry );
227 $formatter->setContext( $this->context );
228
229 $logParam = $formatter->getActionText();
230
231 $this->assertEquals( $expected, $logParam );
232 }
233
238 public function testLogParamsTypePlain() {
239 $params = [ '4:plain:plain' => 'Some plain text' ];
240 $expected = 'Some plain text';
241
242 $entry = $this->newLogEntry( 'param', $params );
243 $formatter = LogFormatter::newFromEntry( $entry );
244 $formatter->setContext( $this->context );
245
246 $logParam = $formatter->getActionText();
247
248 $this->assertEquals( $expected, $logParam );
249 }
250
255 public function testLogComment() {
256 $entry = $this->newLogEntry( 'test', [] );
257 $formatter = LogFormatter::newFromEntry( $entry );
258 $formatter->setContext( $this->context );
259
260 $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
261
262 $this->assertEquals( $comment, $formatter->getComment() );
263 }
264
270 public function testApiParamFormatting( $key, $value, $expected ) {
271 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
272 $formatter = LogFormatter::newFromEntry( $entry );
273 $formatter->setContext( $this->context );
274
275 ApiResult::setIndexedTagName( $expected, 'param' );
276 ApiResult::setArrayType( $expected, 'assoc' );
277
278 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
279 }
280
281 public static function provideApiParamFormatting() {
282 return [
283 [ 0, 'value', [ 'value' ] ],
284 [ 'named', 'value', [ 'named' => 'value' ] ],
285 [ '::key', 'value', [ 'key' => 'value' ] ],
286 [ '4::key', 'value', [ 'key' => 'value' ] ],
287 [ '4:raw:key', 'value', [ 'key' => 'value' ] ],
288 [ '4:plain:key', 'value', [ 'key' => 'value' ] ],
289 [ '4:bool:key', '1', [ 'key' => true ] ],
290 [ '4:bool:key', '0', [ 'key' => false ] ],
291 [ '4:number:key', '123', [ 'key' => 123 ] ],
292 [ '4:number:key', '123.5', [ 'key' => 123.5 ] ],
293 [ '4:array:key', [], [ 'key' => [ ApiResult::META_TYPE => 'array' ] ] ],
294 [ '4:assoc:key', [], [ 'key' => [ ApiResult::META_TYPE => 'assoc' ] ] ],
295 [ '4:kvp:key', [], [ 'key' => [ ApiResult::META_TYPE => 'kvp' ] ] ],
296 [ '4:timestamp:key', '20150102030405', [ 'key' => '2015-01-02T03:04:05Z' ] ],
297 [ '4:msg:key', 'parentheses', [
298 'key_key' => 'parentheses',
299 'key_text' => wfMessage( 'parentheses' )->text(),
300 ] ],
301 [ '4:msg-content:key', 'parentheses', [
302 'key_key' => 'parentheses',
303 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
304 ] ],
305 [ '4:title:key', 'project:foo', [
306 'key_ns' => NS_PROJECT,
307 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
308 ] ],
309 [ '4:title-link:key', 'project:foo', [
310 'key_ns' => NS_PROJECT,
311 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
312 ] ],
313 [ '4:title-link:key', '<invalid>', [
314 'key_ns' => NS_SPECIAL,
315 'key_title' => SpecialPage::getTitleFor( 'Badtitle', '<invalid>' )->getFullText(),
316 ] ],
317 [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
318 [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
319 ];
320 }
321
364 public function testIrcMsgForLogTypeBlock() {
365 $sep = $this->context->msg( 'colon-separator' )->text();
366
367 # block/block
368 $this->assertIRCComment(
369 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
370 . $sep . $this->user_comment,
371 'block', 'block',
372 [
373 '5::duration' => 'duration',
374 '6::flags' => 'flags',
375 ],
376 $this->user_comment
377 );
378 # block/block - legacy
379 $this->assertIRCComment(
380 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
381 . $sep . $this->user_comment,
382 'block', 'block',
383 [
384 'duration',
385 'flags',
386 ],
387 $this->user_comment,
388 '',
389 true
390 );
391 # block/unblock
392 $this->assertIRCComment(
393 $this->context->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
394 'block', 'unblock',
395 [],
396 $this->user_comment
397 );
398 # block/reblock
399 $this->assertIRCComment(
400 $this->context->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
401 . $sep . $this->user_comment,
402 'block', 'reblock',
403 [
404 '5::duration' => 'duration',
405 '6::flags' => 'flags',
406 ],
407 $this->user_comment
408 );
409 }
410
415 public function testIrcMsgForLogTypeDelete() {
416 $sep = $this->context->msg( 'colon-separator' )->text();
417
418 # delete/delete
419 $this->assertIRCComment(
420 $this->context->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
421 'delete', 'delete',
422 [],
423 $this->user_comment
424 );
425
426 # delete/restore
427 $this->assertIRCComment(
428 $this->context->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
429 'delete', 'restore',
430 [],
431 $this->user_comment
432 );
433 }
434
440 $this->assertIRCComment(
441 'New user account',
442 'newusers', 'newusers',
443 []
444 );
445 $this->assertIRCComment(
446 'New user account',
447 'newusers', 'create',
448 []
449 );
450 $this->assertIRCComment(
451 'created new account SomeTitle',
452 'newusers', 'create2',
453 []
454 );
455 $this->assertIRCComment(
456 'Account created automatically',
457 'newusers', 'autocreate',
458 []
459 );
460 }
461
466 public function testIrcMsgForLogTypeMove() {
467 $move_params = [
468 '4::target' => $this->target->getPrefixedText(),
469 '5::noredir' => 0,
470 ];
471 $sep = $this->context->msg( 'colon-separator' )->text();
472
473 # move/move
474 $this->assertIRCComment(
475 $this->context->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
476 ->plain() . $sep . $this->user_comment,
477 'move', 'move',
478 $move_params,
479 $this->user_comment
480 );
481
482 # move/move_redir
483 $this->assertIRCComment(
484 $this->context->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
485 ->plain() . $sep . $this->user_comment,
486 'move', 'move_redir',
487 $move_params,
488 $this->user_comment
489 );
490 }
491
496 public function testIrcMsgForLogTypePatrol() {
497 # patrol/patrol
498 $this->assertIRCComment(
499 $this->context->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
500 'patrol', 'patrol',
501 [
502 '4::curid' => '777',
503 '5::previd' => '666',
504 '6::auto' => 0,
505 ]
506 );
507 }
508
513 public function testIrcMsgForLogTypeProtect() {
514 $protectParams = [
515 '4::description' => '[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)'
516 ];
517 $sep = $this->context->msg( 'colon-separator' )->text();
518
519 # protect/protect
520 $this->assertIRCComment(
521 $this->context->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
522 ->plain() . $sep . $this->user_comment,
523 'protect', 'protect',
524 $protectParams,
525 $this->user_comment
526 );
527
528 # protect/unprotect
529 $this->assertIRCComment(
530 $this->context->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
531 'protect', 'unprotect',
532 [],
533 $this->user_comment
534 );
535
536 # protect/modify
537 $this->assertIRCComment(
538 $this->context->msg(
539 'modifiedarticleprotection',
540 'SomeTitle ' . $protectParams['4::description']
541 )->plain() . $sep . $this->user_comment,
542 'protect', 'modify',
543 $protectParams,
544 $this->user_comment
545 );
546
547 # protect/move_prot
548 $this->assertIRCComment(
549 $this->context->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
550 ->plain() . $sep . $this->user_comment,
551 'protect', 'move_prot',
552 [
553 '4::oldtitle' => 'OldTitle'
554 ],
555 $this->user_comment
556 );
557 }
558
563 public function testIrcMsgForLogTypeUpload() {
564 $sep = $this->context->msg( 'colon-separator' )->text();
565
566 # upload/upload
567 $this->assertIRCComment(
568 $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
569 'upload', 'upload',
570 [],
571 $this->user_comment
572 );
573
574 # upload/overwrite
575 $this->assertIRCComment(
576 $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
577 'upload', 'overwrite',
578 [],
579 $this->user_comment
580 );
581 }
582
587 public function testIrcMsgForLogTypeMerge() {
588 $sep = $this->context->msg( 'colon-separator' )->text();
589
590 # merge/merge
591 $this->assertIRCComment(
592 $this->context->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
593 . $sep . $this->user_comment,
594 'merge', 'merge',
595 [
596 '4::dest' => 'Dest',
597 '5::mergepoint' => 'timestamp',
598 ],
599 $this->user_comment
600 );
601 }
602
607 public function testIrcMsgForLogTypeImport() {
608 $sep = $this->context->msg( 'colon-separator' )->text();
609
610 # import/upload
611 $msg = $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
612 $sep .
614 $this->assertIRCComment(
615 $msg,
616 'import', 'upload',
617 [],
618 $this->user_comment
619 );
620
621 # import/interwiki
622 $msg = $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
623 $sep .
625 $this->assertIRCComment(
626 $msg,
627 'import', 'interwiki',
628 [],
629 $this->user_comment
630 );
631 }
632
641 protected function assertIRCComment( $expected, $type, $action, $params,
642 $comment = null, $msg = '', $legacy = false
643 ) {
644 $logEntry = new ManualLogEntry( $type, $action );
645 $logEntry->setPerformer( $this->user );
646 $logEntry->setTarget( $this->title );
647 if ( $comment !== null ) {
648 $logEntry->setComment( $comment );
649 }
650 $logEntry->setParameters( $params );
651 $logEntry->setLegacy( $legacy );
652
653 $formatter = LogFormatter::newFromEntry( $logEntry );
654 $formatter->setContext( $this->context );
655
656 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
657 $ircRcComment = IRCColourfulRCFeedFormatter::cleanupForIRC( $formatter->getIRCActionComment() );
658
659 $this->assertEquals(
660 $expected,
661 $ircRcComment,
662 $msg
663 );
664 }
665
666}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
$wgLang
Definition Setup.php:875
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:84
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition Linker.php:892
static commentBlock( $comment, $title=null, $local=false, $wikiId=null, $useParentheses=true)
Wrap a comment in standard punctuation and formatting if it's non-empty, otherwise return empty strin...
Definition Linker.php:1480
static userToolLinksRedContribs( $userId, $userText, $edits=null, $useParentheses=true)
Alias for userToolLinks( $userId, $userText, true );.
Definition Linker.php:1004
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 new log entries and inserting them into the database.
Definition LogEntry.php:441
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.
Represents a title within MediaWiki.
Definition Title.php:40
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:585
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:2004
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 use $formDescriptor instead 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
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
const NS_SPECIAL
Definition Defines.php:62
const NS_PROJECT
Definition Defines.php:77
$wgExtensionMessagesFiles['ExtensionNameMagic']
Definition magicword.txt:43
$params
title