Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / tests / phpunit / includes / logging / LogFormatterTest.php
blob1ef3df6ca193f7702b7a88e68a14d23e4327dc5e
1 <?php
3 /**
4 * @group Database
5 */
6 class LogFormatterTest extends MediaWikiLangTestCase {
7 private static $oldExtMsgFiles;
9 /**
10 * @var User
12 protected $user;
14 /**
15 * @var Title
17 protected $title;
19 /**
20 * @var RequestContext
22 protected $context;
24 /**
25 * @var Title
27 protected $target;
29 /**
30 * @var string
32 protected $user_comment;
34 public static function setUpBeforeClass() {
35 parent::setUpBeforeClass();
37 global $wgExtensionMessagesFiles;
38 self::$oldExtMsgFiles = $wgExtensionMessagesFiles;
39 $wgExtensionMessagesFiles['LogTests'] = __DIR__ . '/LogTests.i18n.php';
40 Language::getLocalisationCache()->recache( 'en' );
43 public static function tearDownAfterClass() {
44 global $wgExtensionMessagesFiles;
45 $wgExtensionMessagesFiles = self::$oldExtMsgFiles;
46 Language::getLocalisationCache()->recache( 'en' );
48 parent::tearDownAfterClass();
51 protected function setUp() {
52 parent::setUp();
54 $this->setMwGlobals( [
55 'wgLogTypes' => [ 'phpunit' ],
56 'wgLogActionsHandlers' => [ 'phpunit/test' => 'LogFormatter',
57 'phpunit/param' => 'LogFormatter' ],
58 'wgUser' => User::newFromName( 'Testuser' ),
59 ] );
61 $this->user = User::newFromName( 'Testuser' );
62 $this->title = Title::newFromText( 'SomeTitle' );
63 $this->target = Title::newFromText( 'TestTarget' );
65 $this->context = new RequestContext();
66 $this->context->setUser( $this->user );
67 $this->context->setTitle( $this->title );
68 $this->context->setLanguage( RequestContext::getMain()->getLanguage() );
70 $this->user_comment = '<User comment about action>';
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' );
79 $logEntry->setParameters( $params );
81 return $logEntry;
84 /**
85 * @covers LogFormatter::newFromEntry
87 public function testNormalLogParams() {
88 $entry = $this->newLogEntry( 'test', [] );
89 $formatter = LogFormatter::newFromEntry( $entry );
90 $formatter->setContext( $this->context );
92 $formatter->setShowUserToolLinks( false );
93 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
94 unset( $formatter->parsedParameters );
96 $formatter->setShowUserToolLinks( true );
97 $paramsWithTools = $formatter->getMessageParametersForTesting();
99 $userLink = Linker::userLink(
100 $this->user->getId(),
101 $this->user->getName()
104 $userTools = Linker::userToolLinksRedContribs(
105 $this->user->getId(),
106 $this->user->getName(),
107 $this->user->getEditCount()
110 $titleLink = Linker::link( $this->title, null, [], [] );
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] );
117 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
118 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
120 $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
122 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
126 * @covers LogFormatter::newFromEntry
127 * @covers LogFormatter::getActionText
129 public function testLogParamsTypeRaw() {
130 $params = [ '4:raw:raw' => Linker::link( $this->title, null, [], [] ) ];
131 $expected = Linker::link( $this->title, null, [], [] );
133 $entry = $this->newLogEntry( 'param', $params );
134 $formatter = LogFormatter::newFromEntry( $entry );
135 $formatter->setContext( $this->context );
137 $logParam = $formatter->getActionText();
139 $this->assertEquals( $expected, $logParam );
143 * @covers LogFormatter::newFromEntry
144 * @covers LogFormatter::getActionText
146 public function testLogParamsTypeMsg() {
147 $params = [ '4:msg:msg' => 'log-description-phpunit' ];
148 $expected = wfMessage( 'log-description-phpunit' )->text();
150 $entry = $this->newLogEntry( 'param', $params );
151 $formatter = LogFormatter::newFromEntry( $entry );
152 $formatter->setContext( $this->context );
154 $logParam = $formatter->getActionText();
156 $this->assertEquals( $expected, $logParam );
160 * @covers LogFormatter::newFromEntry
161 * @covers LogFormatter::getActionText
163 public function testLogParamsTypeMsgContent() {
164 $params = [ '4:msg-content:msgContent' => 'log-description-phpunit' ];
165 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
167 $entry = $this->newLogEntry( 'param', $params );
168 $formatter = LogFormatter::newFromEntry( $entry );
169 $formatter->setContext( $this->context );
171 $logParam = $formatter->getActionText();
173 $this->assertEquals( $expected, $logParam );
177 * @covers LogFormatter::newFromEntry
178 * @covers LogFormatter::getActionText
180 public function testLogParamsTypeNumber() {
181 global $wgLang;
183 $params = [ '4:number:number' => 123456789 ];
184 $expected = $wgLang->formatNum( 123456789 );
186 $entry = $this->newLogEntry( 'param', $params );
187 $formatter = LogFormatter::newFromEntry( $entry );
188 $formatter->setContext( $this->context );
190 $logParam = $formatter->getActionText();
192 $this->assertEquals( $expected, $logParam );
196 * @covers LogFormatter::newFromEntry
197 * @covers LogFormatter::getActionText
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()
206 $entry = $this->newLogEntry( 'param', $params );
207 $formatter = LogFormatter::newFromEntry( $entry );
208 $formatter->setContext( $this->context );
210 $logParam = $formatter->getActionText();
212 $this->assertEquals( $expected, $logParam );
216 * @covers LogFormatter::newFromEntry
217 * @covers LogFormatter::getActionText
219 public function testLogParamsTypeTitleLink() {
220 $params = [ '4:title-link:titleLink' => $this->title->getText() ];
221 $expected = Linker::link( $this->title, null, [], [] );
223 $entry = $this->newLogEntry( 'param', $params );
224 $formatter = LogFormatter::newFromEntry( $entry );
225 $formatter->setContext( $this->context );
227 $logParam = $formatter->getActionText();
229 $this->assertEquals( $expected, $logParam );
233 * @covers LogFormatter::newFromEntry
234 * @covers LogFormatter::getActionText
236 public function testLogParamsTypePlain() {
237 $params = [ '4:plain:plain' => 'Some plain text' ];
238 $expected = 'Some plain text';
240 $entry = $this->newLogEntry( 'param', $params );
241 $formatter = LogFormatter::newFromEntry( $entry );
242 $formatter->setContext( $this->context );
244 $logParam = $formatter->getActionText();
246 $this->assertEquals( $expected, $logParam );
250 * @covers LogFormatter::newFromEntry
251 * @covers LogFormatter::getComment
253 public function testLogComment() {
254 $entry = $this->newLogEntry( 'test', [] );
255 $formatter = LogFormatter::newFromEntry( $entry );
256 $formatter->setContext( $this->context );
258 $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
260 $this->assertEquals( $comment, $formatter->getComment() );
264 * @dataProvider provideApiParamFormatting
265 * @covers LogFormatter::formatParametersForApi
266 * @covers LogFormatter::formatParameterValueForApi
268 public function testApiParamFormatting( $key, $value, $expected ) {
269 $entry = $this->newLogEntry( 'param', [ $key => $value ] );
270 $formatter = LogFormatter::newFromEntry( $entry );
271 $formatter->setContext( $this->context );
273 ApiResult::setIndexedTagName( $expected, 'param' );
274 ApiResult::setArrayType( $expected, 'assoc' );
276 $this->assertEquals( $expected, $formatter->formatParametersForApi() );
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' ] ],
317 * The testIrcMsgForAction* tests are supposed to cover the hacky
318 * LogFormatter::getIRCActionText / T36508
320 * Third parties bots listen to those messages. They are clever enough
321 * to fetch the i18n messages from the wiki and then analyze the IRC feed
322 * to reverse engineer the $1, $2 messages.
323 * One thing bots can not detect is when MediaWiki change the meaning of
324 * a message like what happened when we deployed 1.19. $1 became the user
325 * performing the action which broke basically all bots around.
327 * Should cover the following log actions (which are most commonly used by bots):
328 * - block/block
329 * - block/unblock
330 * - block/reblock
331 * - delete/delete
332 * - delete/restore
333 * - newusers/create
334 * - newusers/create2
335 * - newusers/autocreate
336 * - move/move
337 * - move/move_redir
338 * - protect/protect
339 * - protect/modifyprotect
340 * - protect/unprotect
341 * - protect/move_prot
342 * - upload/upload
343 * - merge/merge
344 * - import/upload
345 * - import/interwiki
347 * As well as the following Auto Edit Summaries:
348 * - blank
349 * - replace
350 * - rollback
351 * - undo
355 * @covers LogFormatter::getIRCActionComment
356 * @covers LogFormatter::getIRCActionText
358 public function testIrcMsgForLogTypeBlock() {
359 $sep = $this->context->msg( 'colon-separator' )->text();
361 # block/block
362 $this->assertIRCComment(
363 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
364 . $sep . $this->user_comment,
365 'block', 'block',
367 '5::duration' => 'duration',
368 '6::flags' => 'flags',
370 $this->user_comment
372 # block/block - legacy
373 $this->assertIRCComment(
374 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
375 . $sep . $this->user_comment,
376 'block', 'block',
378 'duration',
379 'flags',
381 $this->user_comment,
383 true
385 # block/unblock
386 $this->assertIRCComment(
387 $this->context->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
388 'block', 'unblock',
390 $this->user_comment
392 # block/reblock
393 $this->assertIRCComment(
394 $this->context->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
395 . $sep . $this->user_comment,
396 'block', 'reblock',
398 '5::duration' => 'duration',
399 '6::flags' => 'flags',
401 $this->user_comment
406 * @covers LogFormatter::getIRCActionComment
407 * @covers LogFormatter::getIRCActionText
409 public function testIrcMsgForLogTypeDelete() {
410 $sep = $this->context->msg( 'colon-separator' )->text();
412 # delete/delete
413 $this->assertIRCComment(
414 $this->context->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
415 'delete', 'delete',
417 $this->user_comment
420 # delete/restore
421 $this->assertIRCComment(
422 $this->context->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
423 'delete', 'restore',
425 $this->user_comment
430 * @covers LogFormatter::getIRCActionComment
431 * @covers LogFormatter::getIRCActionText
433 public function testIrcMsgForLogTypeNewusers() {
434 $this->assertIRCComment(
435 'New user account',
436 'newusers', 'newusers',
439 $this->assertIRCComment(
440 'New user account',
441 'newusers', 'create',
444 $this->assertIRCComment(
445 'created new account SomeTitle',
446 'newusers', 'create2',
449 $this->assertIRCComment(
450 'Account created automatically',
451 'newusers', 'autocreate',
457 * @covers LogFormatter::getIRCActionComment
458 * @covers LogFormatter::getIRCActionText
460 public function testIrcMsgForLogTypeMove() {
461 $move_params = [
462 '4::target' => $this->target->getPrefixedText(),
463 '5::noredir' => 0,
465 $sep = $this->context->msg( 'colon-separator' )->text();
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
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
487 * @covers LogFormatter::getIRCActionComment
488 * @covers LogFormatter::getIRCActionText
490 public function testIrcMsgForLogTypePatrol() {
491 # patrol/patrol
492 $this->assertIRCComment(
493 $this->context->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
494 'patrol', 'patrol',
496 '4::curid' => '777',
497 '5::previd' => '666',
498 '6::auto' => 0,
504 * @covers LogFormatter::getIRCActionComment
505 * @covers LogFormatter::getIRCActionText
507 public function testIrcMsgForLogTypeProtect() {
508 $protectParams = [
509 '4::description' => '[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)'
511 $sep = $this->context->msg( 'colon-separator' )->text();
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
522 # protect/unprotect
523 $this->assertIRCComment(
524 $this->context->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
525 'protect', 'unprotect',
527 $this->user_comment
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
541 # protect/move_prot
542 $this->assertIRCComment(
543 $this->context->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
544 ->plain() . $sep . $this->user_comment,
545 'protect', 'move_prot',
547 '4::oldtitle' => 'OldTitle'
549 $this->user_comment
554 * @covers LogFormatter::getIRCActionComment
555 * @covers LogFormatter::getIRCActionText
557 public function testIrcMsgForLogTypeUpload() {
558 $sep = $this->context->msg( 'colon-separator' )->text();
560 # upload/upload
561 $this->assertIRCComment(
562 $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
563 'upload', 'upload',
565 $this->user_comment
568 # upload/overwrite
569 $this->assertIRCComment(
570 $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
571 'upload', 'overwrite',
573 $this->user_comment
578 * @covers LogFormatter::getIRCActionComment
579 * @covers LogFormatter::getIRCActionText
581 public function testIrcMsgForLogTypeMerge() {
582 $sep = $this->context->msg( 'colon-separator' )->text();
584 # merge/merge
585 $this->assertIRCComment(
586 $this->context->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
587 . $sep . $this->user_comment,
588 'merge', 'merge',
590 '4::dest' => 'Dest',
591 '5::mergepoint' => 'timestamp',
593 $this->user_comment
598 * @covers LogFormatter::getIRCActionComment
599 * @covers LogFormatter::getIRCActionText
601 public function testIrcMsgForLogTypeImport() {
602 $sep = $this->context->msg( 'colon-separator' )->text();
604 # import/upload
605 $msg = $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
606 $sep .
607 $this->user_comment;
608 $this->assertIRCComment(
609 $msg,
610 'import', 'upload',
612 $this->user_comment
615 # import/interwiki
616 $msg = $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
617 $sep .
618 $this->user_comment;
619 $this->assertIRCComment(
620 $msg,
621 'import', 'interwiki',
623 $this->user_comment
628 * @param string $expected Expected IRC text without colors codes
629 * @param string $type Log type (move, delete, suppress, patrol ...)
630 * @param string $action A log type action
631 * @param array $params
632 * @param string $comment (optional) A comment for the log action
633 * @param string $msg (optional) A message for PHPUnit :-)
635 protected function assertIRCComment( $expected, $type, $action, $params,
636 $comment = null, $msg = '', $legacy = false
638 $logEntry = new ManualLogEntry( $type, $action );
639 $logEntry->setPerformer( $this->user );
640 $logEntry->setTarget( $this->title );
641 if ( $comment !== null ) {
642 $logEntry->setComment( $comment );
644 $logEntry->setParameters( $params );
645 $logEntry->setLegacy( $legacy );
647 $formatter = LogFormatter::newFromEntry( $logEntry );
648 $formatter->setContext( $this->context );
650 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
651 $ircRcComment = IRCColourfulRCFeedFormatter::cleanupForIRC( $formatter->getIRCActionComment() );
653 $this->assertEquals(
654 $expected,
655 $ircRcComment,
656 $msg