MediaWiki REL1_32
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 );
110
111 $titleLink = Linker::link( $this->title, null, [], [] );
112
113 // $paramsWithoutTools and $paramsWithTools should be only different
114 // in index 0
115 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
116 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
117
118 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
119 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
120
121 $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
122
123 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
124 }
125
130 public function testLogParamsTypeRaw() {
131 $params = [ '4:raw:raw' => Linker::link( $this->title, null, [], [] ) ];
132 $expected = Linker::link( $this->title, null, [], [] );
133
134 $entry = $this->newLogEntry( 'param', $params );
135 $formatter = LogFormatter::newFromEntry( $entry );
136 $formatter->setContext( $this->context );
137
138 $logParam = $formatter->getActionText();
139
140 $this->assertEquals( $expected, $logParam );
141 }
142
147 public function testLogParamsTypeMsg() {
148 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
149 $expected = wfMessage( 'log-description-phpunit' )->text();
150
151 $entry = $this->newLogEntry( 'param', $params );
152 $formatter = LogFormatter::newFromEntry( $entry );
153 $formatter->setContext( $this->context );
154
155 $logParam = $formatter->getActionText();
156
157 $this->assertEquals( $expected, $logParam );
158 }
159
164 public function testLogParamsTypeMsgContent() {
165 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
166 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
167
168 $entry = $this->newLogEntry( 'param', $params );
169 $formatter = LogFormatter::newFromEntry( $entry );
170 $formatter->setContext( $this->context );
171
172 $logParam = $formatter->getActionText();
173
174 $this->assertEquals( $expected, $logParam );
175 }
176
181 public function testLogParamsTypeNumber() {
182 global $wgLang;
183
184 $params = [ '4:number:number' => 123456789 ];
185 $expected = $wgLang->formatNum( 123456789 );
186
187 $entry = $this->newLogEntry( 'param', $params );
188 $formatter = LogFormatter::newFromEntry( $entry );
189 $formatter->setContext( $this->context );
190
191 $logParam = $formatter->getActionText();
192
193 $this->assertEquals( $expected, $logParam );
194 }
195
200 public function testLogParamsTypeUserLink() {
201 $params = [ '4:user-link:userLink' => $this->user->getName() ];
202 $expected = Linker::userLink(
203 $this->user->getId(),
204 $this->user->getName()
205 );
206
207 $entry = $this->newLogEntry( 'param', $params );
208 $formatter = LogFormatter::newFromEntry( $entry );
209 $formatter->setContext( $this->context );
210
211 $logParam = $formatter->getActionText();
212
213 $this->assertEquals( $expected, $logParam );
214 }
215
220 public function testLogParamsTypeTitleLink() {
221 $params = [ '4:title-link:titleLink' => $this->title->getText() ];
222 $expected = Linker::link( $this->title, null, [], [] );
223
224 $entry = $this->newLogEntry( 'param', $params );
225 $formatter = LogFormatter::newFromEntry( $entry );
226 $formatter->setContext( $this->context );
227
228 $logParam = $formatter->getActionText();
229
230 $this->assertEquals( $expected, $logParam );
231 }
232
237 public function testLogParamsTypePlain() {
238 $params = [ '4:plain:plain' => 'Some plain text' ];
239 $expected = 'Some plain text';
240
241 $entry = $this->newLogEntry( 'param', $params );
242 $formatter = LogFormatter::newFromEntry( $entry );
243 $formatter->setContext( $this->context );
244
245 $logParam = $formatter->getActionText();
246
247 $this->assertEquals( $expected, $logParam );
248 }
249
254 public function testLogComment() {
255 $entry = $this->newLogEntry( 'test', [] );
256 $formatter = LogFormatter::newFromEntry( $entry );
257 $formatter->setContext( $this->context );
258
259 $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
260
261 $this->assertEquals( $comment, $formatter->getComment() );
262 }
263
269 public function testApiParamFormatting( $key, $value, $expected ) {
270 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
271 $formatter = LogFormatter::newFromEntry( $entry );
272 $formatter->setContext( $this->context );
273
274 ApiResult::setIndexedTagName( $expected, 'param' );
275 ApiResult::setArrayType( $expected, 'assoc' );
276
277 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
278 }
279
280 public static function provideApiParamFormatting() {
281 return [
282 [ 0, 'value', [ 'value' ] ],
283 [ 'named', 'value', [ 'named' => 'value' ] ],
284 [ '::key', 'value', [ 'key' => 'value' ] ],
285 [ '4::key', 'value', [ 'key' => 'value' ] ],
286 [ '4:raw:key', 'value', [ 'key' => 'value' ] ],
287 [ '4:plain:key', 'value', [ 'key' => 'value' ] ],
288 [ '4:bool:key', '1', [ 'key' => true ] ],
289 [ '4:bool:key', '0', [ 'key' => false ] ],
290 [ '4:number:key', '123', [ 'key' => 123 ] ],
291 [ '4:number:key', '123.5', [ 'key' => 123.5 ] ],
292 [ '4:array:key', [], [ 'key' => [ ApiResult::META_TYPE => 'array' ] ] ],
293 [ '4:assoc:key', [], [ 'key' => [ ApiResult::META_TYPE => 'assoc' ] ] ],
294 [ '4:kvp:key', [], [ 'key' => [ ApiResult::META_TYPE => 'kvp' ] ] ],
295 [ '4:timestamp:key', '20150102030405', [ 'key' => '2015-01-02T03:04:05Z' ] ],
296 [ '4:msg:key', 'parentheses', [
297 'key_key' => 'parentheses',
298 'key_text' => wfMessage( 'parentheses' )->text(),
299 ] ],
300 [ '4:msg-content:key', 'parentheses', [
301 'key_key' => 'parentheses',
302 'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
303 ] ],
304 [ '4:title:key', 'project:foo', [
305 'key_ns' => NS_PROJECT,
306 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
307 ] ],
308 [ '4:title-link:key', 'project:foo', [
309 'key_ns' => NS_PROJECT,
310 'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
311 ] ],
312 [ '4:title-link:key', '<invalid>', [
313 'key_ns' => NS_SPECIAL,
314 'key_title' => SpecialPage::getTitleFor( 'Badtitle', '<invalid>' )->getFullText(),
315 ] ],
316 [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
317 [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
318 ];
319 }
320
363 public function testIrcMsgForLogTypeBlock() {
364 $sep = $this->context->msg( 'colon-separator' )->text();
365
366 # block/block
367 $this->assertIRCComment(
368 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
369 . $sep . $this->user_comment,
370 'block', 'block',
371 [
372 '5::duration' => 'duration',
373 '6::flags' => 'flags',
374 ],
375 $this->user_comment
376 );
377 # block/block - legacy
378 $this->assertIRCComment(
379 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
380 . $sep . $this->user_comment,
381 'block', 'block',
382 [
383 'duration',
384 'flags',
385 ],
386 $this->user_comment,
387 '',
388 true
389 );
390 # block/unblock
391 $this->assertIRCComment(
392 $this->context->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
393 'block', 'unblock',
394 [],
395 $this->user_comment
396 );
397 # block/reblock
398 $this->assertIRCComment(
399 $this->context->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
400 . $sep . $this->user_comment,
401 'block', 'reblock',
402 [
403 '5::duration' => 'duration',
404 '6::flags' => 'flags',
405 ],
406 $this->user_comment
407 );
408 }
409
414 public function testIrcMsgForLogTypeDelete() {
415 $sep = $this->context->msg( 'colon-separator' )->text();
416
417 # delete/delete
418 $this->assertIRCComment(
419 $this->context->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
420 'delete', 'delete',
421 [],
422 $this->user_comment
423 );
424
425 # delete/restore
426 $this->assertIRCComment(
427 $this->context->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
428 'delete', 'restore',
429 [],
430 $this->user_comment
431 );
432 }
433
439 $this->assertIRCComment(
440 'New user account',
441 'newusers', 'newusers',
442 []
443 );
444 $this->assertIRCComment(
445 'New user account',
446 'newusers', 'create',
447 []
448 );
449 $this->assertIRCComment(
450 'created new account SomeTitle',
451 'newusers', 'create2',
452 []
453 );
454 $this->assertIRCComment(
455 'Account created automatically',
456 'newusers', 'autocreate',
457 []
458 );
459 }
460
465 public function testIrcMsgForLogTypeMove() {
466 $move_params = [
467 '4::target' => $this->target->getPrefixedText(),
468 '5::noredir' => 0,
469 ];
470 $sep = $this->context->msg( 'colon-separator' )->text();
471
472 # move/move
473 $this->assertIRCComment(
474 $this->context->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
475 ->plain() . $sep . $this->user_comment,
476 'move', 'move',
477 $move_params,
478 $this->user_comment
479 );
480
481 # move/move_redir
482 $this->assertIRCComment(
483 $this->context->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
484 ->plain() . $sep . $this->user_comment,
485 'move', 'move_redir',
486 $move_params,
487 $this->user_comment
488 );
489 }
490
495 public function testIrcMsgForLogTypePatrol() {
496 # patrol/patrol
497 $this->assertIRCComment(
498 $this->context->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
499 'patrol', 'patrol',
500 [
501 '4::curid' => '777',
502 '5::previd' => '666',
503 '6::auto' => 0,
504 ]
505 );
506 }
507
512 public function testIrcMsgForLogTypeProtect() {
513 $protectParams = [
514 '4::description' => '[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)'
515 ];
516 $sep = $this->context->msg( 'colon-separator' )->text();
517
518 # protect/protect
519 $this->assertIRCComment(
520 $this->context->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
521 ->plain() . $sep . $this->user_comment,
522 'protect', 'protect',
523 $protectParams,
524 $this->user_comment
525 );
526
527 # protect/unprotect
528 $this->assertIRCComment(
529 $this->context->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
530 'protect', 'unprotect',
531 [],
532 $this->user_comment
533 );
534
535 # protect/modify
536 $this->assertIRCComment(
537 $this->context->msg(
538 'modifiedarticleprotection',
539 'SomeTitle ' . $protectParams['4::description']
540 )->plain() . $sep . $this->user_comment,
541 'protect', 'modify',
542 $protectParams,
543 $this->user_comment
544 );
545
546 # protect/move_prot
547 $this->assertIRCComment(
548 $this->context->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
549 ->plain() . $sep . $this->user_comment,
550 'protect', 'move_prot',
551 [
552 '4::oldtitle' => 'OldTitle'
553 ],
554 $this->user_comment
555 );
556 }
557
562 public function testIrcMsgForLogTypeUpload() {
563 $sep = $this->context->msg( 'colon-separator' )->text();
564
565 # upload/upload
566 $this->assertIRCComment(
567 $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
568 'upload', 'upload',
569 [],
570 $this->user_comment
571 );
572
573 # upload/overwrite
574 $this->assertIRCComment(
575 $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
576 'upload', 'overwrite',
577 [],
578 $this->user_comment
579 );
580 }
581
586 public function testIrcMsgForLogTypeMerge() {
587 $sep = $this->context->msg( 'colon-separator' )->text();
588
589 # merge/merge
590 $this->assertIRCComment(
591 $this->context->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
592 . $sep . $this->user_comment,
593 'merge', 'merge',
594 [
595 '4::dest' => 'Dest',
596 '5::mergepoint' => 'timestamp',
597 ],
598 $this->user_comment
599 );
600 }
601
606 public function testIrcMsgForLogTypeImport() {
607 $sep = $this->context->msg( 'colon-separator' )->text();
608
609 # import/upload
610 $msg = $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
611 $sep .
613 $this->assertIRCComment(
614 $msg,
615 'import', 'upload',
616 [],
617 $this->user_comment
618 );
619
620 # import/interwiki
621 $msg = $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
622 $sep .
624 $this->assertIRCComment(
625 $msg,
626 'import', 'interwiki',
627 [],
628 $this->user_comment
629 );
630 }
631
640 protected function assertIRCComment( $expected, $type, $action, $params,
641 $comment = null, $msg = '', $legacy = false
642 ) {
643 $logEntry = new ManualLogEntry( $type, $action );
644 $logEntry->setPerformer( $this->user );
645 $logEntry->setTarget( $this->title );
646 if ( $comment !== null ) {
647 $logEntry->setComment( $comment );
648 }
649 $logEntry->setParameters( $params );
650 $logEntry->setLegacy( $legacy );
651
652 $formatter = LogFormatter::newFromEntry( $logEntry );
653 $formatter->setContext( $this->context );
654
655 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
656 $ircRcComment = IRCColourfulRCFeedFormatter::cleanupForIRC( $formatter->getIRCActionComment() );
657
658 $this->assertEquals(
659 $expected,
660 $ircRcComment,
661 $msg
662 );
663 }
664
665}
$wgLang
Definition Setup.php:910
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:876
static userToolLinksRedContribs( $userId, $userText, $edits=null)
Alias for userToolLinks( $userId, $userText, true );.
Definition Linker.php:976
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:1441
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:437
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:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:592
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Wikitext formatted, in the key only.
const NS_SPECIAL
Definition Defines.php:53
const NS_PROJECT
Definition Defines.php:68
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:2055
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
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$wgExtensionMessagesFiles['ExtensionNameMagic']
Definition magicword.txt:43
title
$params