MediaWiki  1.28.0
LogFormatterTest.php
Go to the documentation of this file.
1 <?php
2 
7 
11  protected $user;
12 
16  protected $title;
17 
21  protected $context;
22 
26  protected $target;
27 
31  protected $user_comment;
32 
33  protected function setUp() {
34  parent::setUp();
35 
37 
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' ],
44  ] );
45 
46  Language::getLocalisationCache()->recache( $wgLang->getCode() );
47 
48  $this->user = User::newFromName( 'Testuser' );
49  $this->title = Title::newFromText( 'SomeTitle' );
50  $this->target = Title::newFromText( 'TestTarget' );
51 
52  $this->context = new RequestContext();
53  $this->context->setUser( $this->user );
54  $this->context->setTitle( $this->title );
55  $this->context->setLanguage( $wgLang );
56 
57  $this->user_comment = '<User comment about action>';
58  }
59 
60  protected function tearDown() {
61  parent::tearDown();
62 
64  Language::getLocalisationCache()->recache( $wgLang->getCode() );
65  }
66 
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' );
72 
73  $logEntry->setParameters( $params );
74 
75  return $logEntry;
76  }
77 
81  public function testNormalLogParams() {
82  $entry = $this->newLogEntry( 'test', [] );
83  $formatter = LogFormatter::newFromEntry( $entry );
84  $formatter->setContext( $this->context );
85 
86  $formatter->setShowUserToolLinks( false );
87  $paramsWithoutTools = $formatter->getMessageParametersForTesting();
88  unset( $formatter->parsedParameters );
89 
90  $formatter->setShowUserToolLinks( true );
91  $paramsWithTools = $formatter->getMessageParametersForTesting();
92 
93  $userLink = Linker::userLink(
94  $this->user->getId(),
95  $this->user->getName()
96  );
97 
99  $this->user->getId(),
100  $this->user->getName(),
101  $this->user->getEditCount()
102  );
103 
104  $titleLink = Linker::link( $this->title, null, [], [] );
105 
106  // $paramsWithoutTools and $paramsWithTools should be only different
107  // in index 0
108  $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
109  $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
110 
111  $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
112  $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
113 
114  $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
115 
116  $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
117  }
118 
123  public function testLogParamsTypeRaw() {
124  $params = [ '4:raw:raw' => Linker::link( $this->title, null, [], [] ) ];
125  $expected = Linker::link( $this->title, null, [], [] );
126 
127  $entry = $this->newLogEntry( 'param', $params );
128  $formatter = LogFormatter::newFromEntry( $entry );
129  $formatter->setContext( $this->context );
130 
131  $logParam = $formatter->getActionText();
132 
133  $this->assertEquals( $expected, $logParam );
134  }
135 
140  public function testLogParamsTypeMsg() {
141  $params = [ '4:msg:msg' => 'log-description-phpunit' ];
142  $expected = wfMessage( 'log-description-phpunit' )->text();
143 
144  $entry = $this->newLogEntry( 'param', $params );
145  $formatter = LogFormatter::newFromEntry( $entry );
146  $formatter->setContext( $this->context );
147 
148  $logParam = $formatter->getActionText();
149 
150  $this->assertEquals( $expected, $logParam );
151  }
152 
157  public function testLogParamsTypeMsgContent() {
158  $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
159  $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
160 
161  $entry = $this->newLogEntry( 'param', $params );
162  $formatter = LogFormatter::newFromEntry( $entry );
163  $formatter->setContext( $this->context );
164 
165  $logParam = $formatter->getActionText();
166 
167  $this->assertEquals( $expected, $logParam );
168  }
169 
174  public function testLogParamsTypeNumber() {
175  global $wgLang;
176 
177  $params = [ '4:number:number' => 123456789 ];
178  $expected = $wgLang->formatNum( 123456789 );
179 
180  $entry = $this->newLogEntry( 'param', $params );
181  $formatter = LogFormatter::newFromEntry( $entry );
182  $formatter->setContext( $this->context );
183 
184  $logParam = $formatter->getActionText();
185 
186  $this->assertEquals( $expected, $logParam );
187  }
188 
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()
198  );
199 
200  $entry = $this->newLogEntry( 'param', $params );
201  $formatter = LogFormatter::newFromEntry( $entry );
202  $formatter->setContext( $this->context );
203 
204  $logParam = $formatter->getActionText();
205 
206  $this->assertEquals( $expected, $logParam );
207  }
208 
213  public function testLogParamsTypeTitleLink() {
214  $params = [ '4:title-link:titleLink' => $this->title->getText() ];
215  $expected = Linker::link( $this->title, null, [], [] );
216 
217  $entry = $this->newLogEntry( 'param', $params );
218  $formatter = LogFormatter::newFromEntry( $entry );
219  $formatter->setContext( $this->context );
220 
221  $logParam = $formatter->getActionText();
222 
223  $this->assertEquals( $expected, $logParam );
224  }
225 
230  public function testLogParamsTypePlain() {
231  $params = [ '4:plain:plain' => 'Some plain text' ];
232  $expected = 'Some plain text';
233 
234  $entry = $this->newLogEntry( 'param', $params );
235  $formatter = LogFormatter::newFromEntry( $entry );
236  $formatter->setContext( $this->context );
237 
238  $logParam = $formatter->getActionText();
239 
240  $this->assertEquals( $expected, $logParam );
241  }
242 
247  public function testLogComment() {
248  $entry = $this->newLogEntry( 'test', [] );
249  $formatter = LogFormatter::newFromEntry( $entry );
250  $formatter->setContext( $this->context );
251 
252  $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
253 
254  $this->assertEquals( $comment, $formatter->getComment() );
255  }
256 
262  public function testApiParamFormatting( $key, $value, $expected ) {
263  $entry = $this->newLogEntry( 'param', [ $key => $value ] );
264  $formatter = LogFormatter::newFromEntry( $entry );
265  $formatter->setContext( $this->context );
266 
267  ApiResult::setIndexedTagName( $expected, 'param' );
268  ApiResult::setArrayType( $expected, 'assoc' );
269 
270  $this->assertEquals( $expected, $formatter->formatParametersForApi() );
271  }
272 
273  public static function provideApiParamFormatting() {
274  return [
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(),
292  ] ],
293  [ '4:msg-content:key', 'parentheses', [
294  'key_key' => 'parentheses',
295  'key_text' => wfMessage( 'parentheses' )->inContentLanguage()->text(),
296  ] ],
297  [ '4:title:key', 'project:foo', [
298  'key_ns' => NS_PROJECT,
299  'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
300  ] ],
301  [ '4:title-link:key', 'project:foo', [
302  'key_ns' => NS_PROJECT,
303  'key_title' => Title::newFromText( 'project:foo' )->getFullText(),
304  ] ],
305  [ '4:user:key', 'foo', [ 'key' => 'Foo' ] ],
306  [ '4:user-link:key', 'foo', [ 'key' => 'Foo' ] ],
307  ];
308  }
309 
352  public function testIrcMsgForLogTypeBlock() {
353  $sep = $this->context->msg( 'colon-separator' )->text();
354 
355  # block/block
356  $this->assertIRCComment(
357  $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
358  . $sep . $this->user_comment,
359  'block', 'block',
360  [
361  '5::duration' => 'duration',
362  '6::flags' => 'flags',
363  ],
365  );
366  # block/block - legacy
367  $this->assertIRCComment(
368  $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
369  . $sep . $this->user_comment,
370  'block', 'block',
371  [
372  'duration',
373  'flags',
374  ],
376  '',
377  true
378  );
379  # block/unblock
380  $this->assertIRCComment(
381  $this->context->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
382  'block', 'unblock',
383  [],
385  );
386  # block/reblock
387  $this->assertIRCComment(
388  $this->context->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
389  . $sep . $this->user_comment,
390  'block', 'reblock',
391  [
392  '5::duration' => 'duration',
393  '6::flags' => 'flags',
394  ],
396  );
397  }
398 
403  public function testIrcMsgForLogTypeDelete() {
404  $sep = $this->context->msg( 'colon-separator' )->text();
405 
406  # delete/delete
407  $this->assertIRCComment(
408  $this->context->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
409  'delete', 'delete',
410  [],
412  );
413 
414  # delete/restore
415  $this->assertIRCComment(
416  $this->context->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
417  'delete', 'restore',
418  [],
420  );
421  }
422 
427  public function testIrcMsgForLogTypeNewusers() {
428  $this->assertIRCComment(
429  'New user account',
430  'newusers', 'newusers',
431  []
432  );
433  $this->assertIRCComment(
434  'New user account',
435  'newusers', 'create',
436  []
437  );
438  $this->assertIRCComment(
439  'created new account SomeTitle',
440  'newusers', 'create2',
441  []
442  );
443  $this->assertIRCComment(
444  'Account created automatically',
445  'newusers', 'autocreate',
446  []
447  );
448  }
449 
454  public function testIrcMsgForLogTypeMove() {
455  $move_params = [
456  '4::target' => $this->target->getPrefixedText(),
457  '5::noredir' => 0,
458  ];
459  $sep = $this->context->msg( 'colon-separator' )->text();
460 
461  # move/move
462  $this->assertIRCComment(
463  $this->context->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
464  ->plain() . $sep . $this->user_comment,
465  'move', 'move',
466  $move_params,
468  );
469 
470  # move/move_redir
471  $this->assertIRCComment(
472  $this->context->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
473  ->plain() . $sep . $this->user_comment,
474  'move', 'move_redir',
475  $move_params,
477  );
478  }
479 
484  public function testIrcMsgForLogTypePatrol() {
485  # patrol/patrol
486  $this->assertIRCComment(
487  $this->context->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
488  'patrol', 'patrol',
489  [
490  '4::curid' => '777',
491  '5::previd' => '666',
492  '6::auto' => 0,
493  ]
494  );
495  }
496 
501  public function testIrcMsgForLogTypeProtect() {
502  $protectParams = [
503  '4::description' => '[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)'
504  ];
505  $sep = $this->context->msg( 'colon-separator' )->text();
506 
507  # protect/protect
508  $this->assertIRCComment(
509  $this->context->msg( 'protectedarticle', 'SomeTitle ' . $protectParams['4::description'] )
510  ->plain() . $sep . $this->user_comment,
511  'protect', 'protect',
512  $protectParams,
514  );
515 
516  # protect/unprotect
517  $this->assertIRCComment(
518  $this->context->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
519  'protect', 'unprotect',
520  [],
522  );
523 
524  # protect/modify
525  $this->assertIRCComment(
526  $this->context->msg(
527  'modifiedarticleprotection',
528  'SomeTitle ' . $protectParams['4::description']
529  )->plain() . $sep . $this->user_comment,
530  'protect', 'modify',
531  $protectParams,
533  );
534 
535  # protect/move_prot
536  $this->assertIRCComment(
537  $this->context->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
538  ->plain() . $sep . $this->user_comment,
539  'protect', 'move_prot',
540  [
541  '4::oldtitle' => 'OldTitle'
542  ],
544  );
545  }
546 
551  public function testIrcMsgForLogTypeUpload() {
552  $sep = $this->context->msg( 'colon-separator' )->text();
553 
554  # upload/upload
555  $this->assertIRCComment(
556  $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
557  'upload', 'upload',
558  [],
560  );
561 
562  # upload/overwrite
563  $this->assertIRCComment(
564  $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
565  'upload', 'overwrite',
566  [],
568  );
569  }
570 
575  public function testIrcMsgForLogTypeMerge() {
576  $sep = $this->context->msg( 'colon-separator' )->text();
577 
578  # merge/merge
579  $this->assertIRCComment(
580  $this->context->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
581  . $sep . $this->user_comment,
582  'merge', 'merge',
583  [
584  '4::dest' => 'Dest',
585  '5::mergepoint' => 'timestamp',
586  ],
588  );
589  }
590 
595  public function testIrcMsgForLogTypeImport() {
596  $sep = $this->context->msg( 'colon-separator' )->text();
597 
598  # import/upload
599  $msg = $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
600  $sep .
602  $this->assertIRCComment(
603  $msg,
604  'import', 'upload',
605  [],
606  $this->user_comment
607  );
608 
609  # import/interwiki
610  $msg = $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
611  $sep .
613  $this->assertIRCComment(
614  $msg,
615  'import', 'interwiki',
616  [],
617  $this->user_comment
618  );
619  }
620 
629  protected function assertIRCComment( $expected, $type, $action, $params,
630  $comment = null, $msg = '', $legacy = false
631  ) {
632  $logEntry = new ManualLogEntry( $type, $action );
633  $logEntry->setPerformer( $this->user );
634  $logEntry->setTarget( $this->title );
635  if ( $comment !== null ) {
636  $logEntry->setComment( $comment );
637  }
638  $logEntry->setParameters( $params );
639  $logEntry->setLegacy( $legacy );
640 
641  $formatter = LogFormatter::newFromEntry( $logEntry );
642  $formatter->setContext( $this->context );
643 
644  // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
645  $ircRcComment = IRCColourfulRCFeedFormatter::cleanupForIRC( $formatter->getIRCActionComment() );
646 
647  $this->assertEquals(
648  $expected,
649  $ircRcComment,
650  $msg
651  );
652  }
653 
654 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
static getLocalisationCache()
Get the LocalisationCache instance.
Definition: Language.php:404
testIrcMsgForLogTypeBlock()
The testIrcMsgForAction* tests are supposed to cover the hacky LogFormatter::getIRCActionText / bug 3...
assertIRCComment($expected, $type, $action, $params, $comment=null, $msg= '', $legacy=false)
Group all the pieces relevant to the context of a request into one instance.
testIrcMsgForLogTypePatrol()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testIrcMsgForLogTypeDelete()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
testLogParamsTypeRaw()
LogFormatter::newFromEntry LogFormatter::getActionText.
static newFromEntry(LogEntry $entry)
Constructs a new formatter suitable for given entry.
static provideApiParamFormatting()
$comment
$value
const META_TYPE
Key for the 'type' metadata item.
Definition: ApiResult.php:108
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
testNormalLogParams()
LogFormatter::newFromEntry.
title
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:618
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
testLogParamsTypePlain()
LogFormatter::newFromEntry LogFormatter::getActionText.
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:1936
const NS_PROJECT
Definition: Defines.php:60
testIrcMsgForLogTypeMerge()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testIrcMsgForLogTypeUpload()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
testIrcMsgForLogTypeNewusers()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testApiParamFormatting($key, $value, $expected)
provideApiParamFormatting LogFormatter::formatParametersForApi LogFormatter::formatParameterValueForA...
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.
Definition: distributors.txt:9
static cleanupForIRC($text)
Remove newlines, carriage returns and decode html entites.
$params
Base class that store and restore the Language objects.
testLogParamsTypeNumber()
LogFormatter::newFromEntry LogFormatter::getActionText.
testIrcMsgForLogTypeProtect()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testLogComment()
LogFormatter::newFromEntry LogFormatter::getComment.
testLogParamsTypeTitleLink()
LogFormatter::newFromEntry LogFormatter::getActionText.
static userToolLinksRedContribs($userId, $userText, $edits=null)
Alias for userToolLinks( $userId, $userText, true );.
Definition: Linker.php:1073
static link($target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition: Linker.php:203
testIrcMsgForLogTypeImport()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
static userLink($userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:984
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:35
testLogParamsTypeUserLink()
LogFormatter::newFromEntry LogFormatter::getActionText.
Class for creating log entries manually, to inject them into the database.
Definition: LogEntry.php:394
newLogEntry($action, $params)
testLogParamsTypeMsgContent()
LogFormatter::newFromEntry LogFormatter::getActionText.
testIrcMsgForLogTypeMove()
LogFormatter::getIRCActionComment LogFormatter::getIRCActionText.
testLogParamsTypeMsg()
LogFormatter::newFromEntry LogFormatter::getActionText.
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:1525
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
Definition: ApiResult.php:730
RequestContext $context
setMwGlobals($pairs, $value=null)
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491