Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / logging / LogFormatterTest.php
blob6210d0985c95c7da0968a6a882e7678bec44b997
1 <?php
2 /**
3 * @group Database
4 */
5 class LogFormatterTest extends MediaWikiLangTestCase {
7 /**
8 * @var User
9 */
10 protected $user;
12 /**
13 * @var Title
15 protected $title;
17 /**
18 * @var RequestContext
20 protected $context;
22 protected function setUp() {
23 parent::setUp();
25 global $wgLang;
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' ),
33 ) );
35 Language::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 protected function tearDown() {
47 parent::tearDown();
49 global $wgLang;
50 Language::getLocalisationCache()->recache( $wgLang->getCode() );
53 public function newLogEntry( $action, $params ) {
54 $logEntry = new ManualLogEntry( 'phpunit', $action );
55 $logEntry->setPerformer( $this->user );
56 $logEntry->setTarget( $this->title );
57 $logEntry->setComment( 'A very good reason' );
59 $logEntry->setParameters( $params );
61 return $logEntry;
64 /**
65 * @covers LogFormatter::newFromEntry
67 public function testNormalLogParams() {
68 $entry = $this->newLogEntry( 'test', array() );
69 $formatter = LogFormatter::newFromEntry( $entry );
70 $formatter->setContext( $this->context );
72 $formatter->setShowUserToolLinks( false );
73 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
74 unset( $formatter->parsedParameters );
76 $formatter->setShowUserToolLinks( true );
77 $paramsWithTools = $formatter->getMessageParametersForTesting();
79 $userLink = Linker::userLink(
80 $this->user->getId(),
81 $this->user->getName()
84 $userTools = Linker::userToolLinksRedContribs(
85 $this->user->getId(),
86 $this->user->getName(),
87 $this->user->getEditCount()
90 $titleLink = Linker::link( $this->title, null, array(), array() );
92 // $paramsWithoutTools and $paramsWithTools should be only different
93 // in index 0
94 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
95 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
97 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
98 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
100 $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
102 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
106 * @covers LogFormatter::newFromEntry
107 * @covers LogFormatter::getActionText
109 public function testLogParamsTypeRaw() {
110 $params = array( '4:raw:raw' => Linker::link( $this->title, null, array(), array() ) );
111 $expected = Linker::link( $this->title, null, array(), array() );
113 $entry = $this->newLogEntry( 'param', $params );
114 $formatter = LogFormatter::newFromEntry( $entry );
115 $formatter->setContext( $this->context );
117 $logParam = $formatter->getActionText();
119 $this->assertEquals( $expected, $logParam );
123 * @covers LogFormatter::newFromEntry
124 * @covers LogFormatter::getActionText
126 public function testLogParamsTypeMsg() {
127 $params = array( '4:msg:msg' => 'log-description-phpunit' );
128 $expected = wfMessage( 'log-description-phpunit' )->text();
130 $entry = $this->newLogEntry( 'param', $params );
131 $formatter = LogFormatter::newFromEntry( $entry );
132 $formatter->setContext( $this->context );
134 $logParam = $formatter->getActionText();
136 $this->assertEquals( $expected, $logParam );
140 * @covers LogFormatter::newFromEntry
141 * @covers LogFormatter::getActionText
143 public function testLogParamsTypeMsgContent() {
144 $params = array( '4:msg-content:msgContent' => 'log-description-phpunit' );
145 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
147 $entry = $this->newLogEntry( 'param', $params );
148 $formatter = LogFormatter::newFromEntry( $entry );
149 $formatter->setContext( $this->context );
151 $logParam = $formatter->getActionText();
153 $this->assertEquals( $expected, $logParam );
157 * @covers LogFormatter::newFromEntry
158 * @covers LogFormatter::getActionText
160 public function testLogParamsTypeNumber() {
161 global $wgLang;
163 $params = array( '4:number:number' => 123456789 );
164 $expected = $wgLang->formatNum( 123456789 );
166 $entry = $this->newLogEntry( 'param', $params );
167 $formatter = LogFormatter::newFromEntry( $entry );
168 $formatter->setContext( $this->context );
170 $logParam = $formatter->getActionText();
172 $this->assertEquals( $expected, $logParam );
176 * @covers LogFormatter::newFromEntry
177 * @covers LogFormatter::getActionText
179 public function testLogParamsTypeUserLink() {
180 $params = array( '4:user-link:userLink' => $this->user->getName() );
181 $expected = Linker::userLink(
182 $this->user->getId(),
183 $this->user->getName()
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 testLogParamsTypeTitleLink() {
200 $params = array( '4:title-link:titleLink' => $this->title->getText() );
201 $expected = Linker::link( $this->title, null, array(), array() );
203 $entry = $this->newLogEntry( 'param', $params );
204 $formatter = LogFormatter::newFromEntry( $entry );
205 $formatter->setContext( $this->context );
207 $logParam = $formatter->getActionText();
209 $this->assertEquals( $expected, $logParam );
213 * @covers LogFormatter::newFromEntry
214 * @covers LogFormatter::getActionText
216 public function testLogParamsTypePlain() {
217 $params = array( '4:plain:plain' => 'Some plain text' );
218 $expected = 'Some plain text';
220 $entry = $this->newLogEntry( 'param', $params );
221 $formatter = LogFormatter::newFromEntry( $entry );
222 $formatter->setContext( $this->context );
224 $logParam = $formatter->getActionText();
226 $this->assertEquals( $expected, $logParam );
230 * @covers LogFormatter::newFromEntry
231 * @covers LogFormatter::getComment
233 public function testLogComment() {
234 $entry = $this->newLogEntry( 'test', array() );
235 $formatter = LogFormatter::newFromEntry( $entry );
236 $formatter->setContext( $this->context );
238 $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
240 $this->assertEquals( $comment, $formatter->getComment() );