5 class LogFormatterTest
extends MediaWikiLangTestCase
{
22 protected function setUp() {
27 $this->setMwGlobals( array(
28 'wgLogTypes' => array( 'phpunit' ),
29 'wgLogActionsHandlers' => array( 'phpunit/test' => 'LogFormatter',
30 'phpunit/param' => 'LogFormatter' ),
31 'wgUser' => User
::newFromName( 'Testuser' ),
32 'wgExtensionMessagesFiles' => array( 'LogTests' => __DIR__
.'/LogTests.i18n.php' ),
35 $wgLang->getLocalisationCache()->recache( $wgLang->getCode() );
37 $this->user
= User
::newFromName( 'Testuser' );
38 $this->title
= Title
::newMainPage();
40 $this->context
= new RequestContext();
41 $this->context
->setUser( $this->user
);
42 $this->context
->setTitle( $this->title
);
43 $this->context
->setLanguage( $wgLang );
46 public function newLogEntry( $action, $params ) {
47 $logEntry = new ManualLogEntry( 'phpunit', $action );
48 $logEntry->setPerformer( $this->user
);
49 $logEntry->setTarget( $this->title
);
50 $logEntry->setComment( 'A very good reason' );
52 $logEntry->setParameters( $params );
57 public function testNormalLogParams() {
58 $entry = $this->newLogEntry( 'test', array() );
59 $formatter = LogFormatter
::newFromEntry( $entry );
60 $formatter->setContext( $this->context
);
62 $formatter->setShowUserToolLinks( false );
63 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
64 unset( $formatter->parsedParameters
);
66 $formatter->setShowUserToolLinks( true );
67 $paramsWithTools = $formatter->getMessageParametersForTesting();
69 $userLink = Linker
::userLink(
71 $this->user
->getName()
74 $userTools = Linker
::userToolLinksRedContribs(
76 $this->user
->getName(),
77 $this->user
->getEditCount()
80 $titleLink = Linker
::link( $this->title
, null, array(), array() );
82 // $paramsWithoutTools and $paramsWithTools should be only different
84 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
85 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
87 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
88 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
90 $this->assertEquals( $this->user
->getName(), $paramsWithoutTools[1] );
92 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
95 public function testLogParamsTypeRaw() {
96 $params = array( '4:raw:raw' => Linker
::link( $this->title
, null, array(), array() ) );
97 $expected = Linker
::link( $this->title
, null, array(), array() );
99 $entry = $this->newLogEntry( 'param', $params );
100 $formatter = LogFormatter
::newFromEntry( $entry );
101 $formatter->setContext( $this->context
);
103 $logParam = $formatter->getActionText();
105 $this->assertEquals( $expected, $logParam );
108 public function testLogParamsTypeMsg() {
109 $params = array( '4:msg:msg' => 'log-description-phpunit' );
110 $expected = wfMessage( 'log-description-phpunit' )->text();
112 $entry = $this->newLogEntry( 'param', $params );
113 $formatter = LogFormatter
::newFromEntry( $entry );
114 $formatter->setContext( $this->context
);
116 $logParam = $formatter->getActionText();
118 $this->assertEquals( $expected, $logParam );
121 public function testLogParamsTypeMsgContent() {
122 $params = array( '4:msg-content:msgContent' => 'log-description-phpunit' );
123 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
125 $entry = $this->newLogEntry( 'param', $params );
126 $formatter = LogFormatter
::newFromEntry( $entry );
127 $formatter->setContext( $this->context
);
129 $logParam = $formatter->getActionText();
131 $this->assertEquals( $expected, $logParam );
134 public function testLogParamsTypeNumber() {
137 $params = array( '4:number:number' => 123456789 );
138 $expected = $wgLang->formatNum( 123456789 );
140 $entry = $this->newLogEntry( 'param', $params );
141 $formatter = LogFormatter
::newFromEntry( $entry );
142 $formatter->setContext( $this->context
);
144 $logParam = $formatter->getActionText();
146 $this->assertEquals( $expected, $logParam );
149 public function testLogParamsTypeUserLink() {
150 $params = array( '4:user-link:userLink' => $this->user
->getName() );
151 $expected = Linker
::userLink(
152 $this->user
->getId(),
153 $this->user
->getName()
156 $entry = $this->newLogEntry( 'param', $params );
157 $formatter = LogFormatter
::newFromEntry( $entry );
158 $formatter->setContext( $this->context
);
160 $logParam = $formatter->getActionText();
162 $this->assertEquals( $expected, $logParam );
165 public function testLogParamsTypeTitleLink() {
166 $params = array( '4:title-link:titleLink' => $this->title
->getText() );
167 $expected = Linker
::link( $this->title
, null, array(), array() );
169 $entry = $this->newLogEntry( 'param', $params );
170 $formatter = LogFormatter
::newFromEntry( $entry );
171 $formatter->setContext( $this->context
);
173 $logParam = $formatter->getActionText();
175 $this->assertEquals( $expected, $logParam );
178 public function testLogParamsTypePlain() {
179 $params = array( '4:plain:plain' => 'Some plain text' );
180 $expected = 'Some plain text';
182 $entry = $this->newLogEntry( 'param', $params );
183 $formatter = LogFormatter
::newFromEntry( $entry );
184 $formatter->setContext( $this->context
);
186 $logParam = $formatter->getActionText();
188 $this->assertEquals( $expected, $logParam );
191 public function testLogComment() {
192 $entry = $this->newLogEntry( 'test', array() );
193 $formatter = LogFormatter
::newFromEntry( $entry );
194 $formatter->setContext( $this->context
);
196 $comment = ltrim( Linker
::commentBlock( $entry->getComment() ) );
198 $this->assertEquals( $comment, $formatter->getComment() );