Add .mw-editsection-like class, behavior same as .mw-editsection
[lhc/web/wiklou.git] / tests / phpunit / includes / logging / LogFormatterTest.php
1 <?php
2 /**
3 * @group Database
4 */
5 class LogFormatterTest extends MediaWikiLangTestCase {
6
7 /**
8 * @var User
9 */
10 protected $user;
11
12 /**
13 * @var Title
14 */
15 protected $title;
16
17 /**
18 * @var RequestContext
19 */
20 protected $context;
21
22 protected function setUp() {
23 parent::setUp();
24
25 global $wgLang;
26
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 ) );
34
35 $wgLang->getLocalisationCache()->recache( $wgLang->getCode() );
36
37 $this->user = User::newFromName( 'Testuser' );
38 $this->title = Title::newMainPage();
39
40 $this->context = new RequestContext();
41 $this->context->setUser( $this->user );
42 $this->context->setTitle( $this->title );
43 $this->context->setLanguage( $wgLang );
44 }
45
46 protected function tearDown() {
47 parent::tearDown();
48
49 global $wgLang;
50 $wgLang->getLocalisationCache()->recache( $wgLang->getCode() );
51 }
52
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' );
58
59 $logEntry->setParameters( $params );
60
61 return $logEntry;
62 }
63
64 public function testNormalLogParams() {
65 $entry = $this->newLogEntry( 'test', array() );
66 $formatter = LogFormatter::newFromEntry( $entry );
67 $formatter->setContext( $this->context );
68
69 $formatter->setShowUserToolLinks( false );
70 $paramsWithoutTools = $formatter->getMessageParametersForTesting();
71 unset( $formatter->parsedParameters );
72
73 $formatter->setShowUserToolLinks( true );
74 $paramsWithTools = $formatter->getMessageParametersForTesting();
75
76 $userLink = Linker::userLink(
77 $this->user->getId(),
78 $this->user->getName()
79 );
80
81 $userTools = Linker::userToolLinksRedContribs(
82 $this->user->getId(),
83 $this->user->getName(),
84 $this->user->getEditCount()
85 );
86
87 $titleLink = Linker::link( $this->title, null, array(), array() );
88
89 // $paramsWithoutTools and $paramsWithTools should be only different
90 // in index 0
91 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
92 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
93
94 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
95 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
96
97 $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
98
99 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
100 }
101
102 public function testLogParamsTypeRaw() {
103 $params = array( '4:raw:raw' => Linker::link( $this->title, null, array(), array() ) );
104 $expected = Linker::link( $this->title, null, array(), array() );
105
106 $entry = $this->newLogEntry( 'param', $params );
107 $formatter = LogFormatter::newFromEntry( $entry );
108 $formatter->setContext( $this->context );
109
110 $logParam = $formatter->getActionText();
111
112 $this->assertEquals( $expected, $logParam );
113 }
114
115 public function testLogParamsTypeMsg() {
116 $params = array( '4:msg:msg' => 'log-description-phpunit' );
117 $expected = wfMessage( 'log-description-phpunit' )->text();
118
119 $entry = $this->newLogEntry( 'param', $params );
120 $formatter = LogFormatter::newFromEntry( $entry );
121 $formatter->setContext( $this->context );
122
123 $logParam = $formatter->getActionText();
124
125 $this->assertEquals( $expected, $logParam );
126 }
127
128 public function testLogParamsTypeMsgContent() {
129 $params = array( '4:msg-content:msgContent' => 'log-description-phpunit' );
130 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
131
132 $entry = $this->newLogEntry( 'param', $params );
133 $formatter = LogFormatter::newFromEntry( $entry );
134 $formatter->setContext( $this->context );
135
136 $logParam = $formatter->getActionText();
137
138 $this->assertEquals( $expected, $logParam );
139 }
140
141 public function testLogParamsTypeNumber() {
142 global $wgLang;
143
144 $params = array( '4:number:number' => 123456789 );
145 $expected = $wgLang->formatNum( 123456789 );
146
147 $entry = $this->newLogEntry( 'param', $params );
148 $formatter = LogFormatter::newFromEntry( $entry );
149 $formatter->setContext( $this->context );
150
151 $logParam = $formatter->getActionText();
152
153 $this->assertEquals( $expected, $logParam );
154 }
155
156 public function testLogParamsTypeUserLink() {
157 $params = array( '4:user-link:userLink' => $this->user->getName() );
158 $expected = Linker::userLink(
159 $this->user->getId(),
160 $this->user->getName()
161 );
162
163 $entry = $this->newLogEntry( 'param', $params );
164 $formatter = LogFormatter::newFromEntry( $entry );
165 $formatter->setContext( $this->context );
166
167 $logParam = $formatter->getActionText();
168
169 $this->assertEquals( $expected, $logParam );
170 }
171
172 public function testLogParamsTypeTitleLink() {
173 $params = array( '4:title-link:titleLink' => $this->title->getText() );
174 $expected = Linker::link( $this->title, null, array(), array() );
175
176 $entry = $this->newLogEntry( 'param', $params );
177 $formatter = LogFormatter::newFromEntry( $entry );
178 $formatter->setContext( $this->context );
179
180 $logParam = $formatter->getActionText();
181
182 $this->assertEquals( $expected, $logParam );
183 }
184
185 public function testLogParamsTypePlain() {
186 $params = array( '4:plain:plain' => 'Some plain text' );
187 $expected = 'Some plain text';
188
189 $entry = $this->newLogEntry( 'param', $params );
190 $formatter = LogFormatter::newFromEntry( $entry );
191 $formatter->setContext( $this->context );
192
193 $logParam = $formatter->getActionText();
194
195 $this->assertEquals( $expected, $logParam );
196 }
197
198 public function testLogComment() {
199 $entry = $this->newLogEntry( 'test', array() );
200 $formatter = LogFormatter::newFromEntry( $entry );
201 $formatter->setContext( $this->context );
202
203 $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
204
205 $this->assertEquals( $comment, $formatter->getComment() );
206 }
207 }