ab35453dbc8d38287ec6ec31e174dbeec9e1cea6
[lhc/web/wiklou.git] / tests / phpunit / includes / changes / RecentChangeTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class RecentChangeTest extends MediaWikiTestCase {
7 protected $title;
8 protected $target;
9 protected $user;
10 protected $user_comment;
11 protected $context;
12
13 public function __construct() {
14 parent::__construct();
15
16 $this->title = Title::newFromText( 'SomeTitle' );
17 $this->target = Title::newFromText( 'TestTarget' );
18 $this->user = User::newFromName( 'UserName' );
19
20 $this->user_comment = '<User comment about action>';
21 $this->context = RequestContext::newExtraneousContext( $this->title );
22 }
23
24 /**
25 * The testIrcMsgForAction* tests are supposed to cover the hacky
26 * LogFormatter::getIRCActionText / bug 34508
27 *
28 * Third parties bots listen to those messages. They are clever enough
29 * to fetch the i18n messages from the wiki and then analyze the IRC feed
30 * to reverse engineer the $1, $2 messages.
31 * One thing bots can not detect is when MediaWiki change the meaning of
32 * a message like what happened when we deployed 1.19. $1 became the user
33 * performing the action which broke basically all bots around.
34 *
35 * Should cover the following log actions (which are most commonly used by bots):
36 * - block/block
37 * - block/unblock
38 * - block/reblock
39 * - delete/delete
40 * - delete/restore
41 * - newusers/create
42 * - newusers/create2
43 * - newusers/autocreate
44 * - move/move
45 * - move/move_redir
46 * - protect/protect
47 * - protect/modifyprotect
48 * - protect/unprotect
49 * - protect/move_prot
50 * - upload/upload
51 * - merge/merge
52 * - import/upload
53 * - import/interwiki
54 *
55 * As well as the following Auto Edit Summaries:
56 * - blank
57 * - replace
58 * - rollback
59 * - undo
60 */
61
62 /**
63 * @covers LogFormatter::getIRCActionText
64 */
65 public function testIrcMsgForLogTypeBlock() {
66 $sep = $this->context->msg( 'colon-separator' )->text();
67
68 # block/block
69 $this->assertIRCComment(
70 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
71 . $sep . $this->user_comment,
72 'block', 'block',
73 array(
74 '5::duration' => 'duration',
75 '6::flags' => 'flags',
76 ),
77 $this->user_comment
78 );
79 # block/block - legacy
80 $this->assertIRCComment(
81 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
82 . $sep . $this->user_comment,
83 'block', 'block',
84 array(
85 'duration',
86 'flags',
87 ),
88 $this->user_comment,
89 '',
90 true
91 );
92 # block/unblock
93 $this->assertIRCComment(
94 $this->context->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
95 'block', 'unblock',
96 array(),
97 $this->user_comment
98 );
99 # block/reblock
100 $this->assertIRCComment(
101 $this->context->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
102 . $sep . $this->user_comment,
103 'block', 'reblock',
104 array(
105 '5::duration' => 'duration',
106 '6::flags' => 'flags',
107 ),
108 $this->user_comment
109 );
110 }
111
112 /**
113 * @covers LogFormatter::getIRCActionText
114 */
115 public function testIrcMsgForLogTypeDelete() {
116 $sep = $this->context->msg( 'colon-separator' )->text();
117
118 # delete/delete
119 $this->assertIRCComment(
120 $this->context->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
121 'delete', 'delete',
122 array(),
123 $this->user_comment
124 );
125
126 # delete/restore
127 $this->assertIRCComment(
128 $this->context->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
129 'delete', 'restore',
130 array(),
131 $this->user_comment
132 );
133 }
134
135 /**
136 * @covers LogFormatter::getIRCActionText
137 */
138 public function testIrcMsgForLogTypeNewusers() {
139 $this->assertIRCComment(
140 'New user account',
141 'newusers', 'newusers',
142 array()
143 );
144 $this->assertIRCComment(
145 'New user account',
146 'newusers', 'create',
147 array()
148 );
149 $this->assertIRCComment(
150 'created new account SomeTitle',
151 'newusers', 'create2',
152 array()
153 );
154 $this->assertIRCComment(
155 'Account created automatically',
156 'newusers', 'autocreate',
157 array()
158 );
159 }
160
161 /**
162 * @covers LogFormatter::getIRCActionText
163 */
164 public function testIrcMsgForLogTypeMove() {
165 $move_params = array(
166 '4::target' => $this->target->getPrefixedText(),
167 '5::noredir' => 0,
168 );
169 $sep = $this->context->msg( 'colon-separator' )->text();
170
171 # move/move
172 $this->assertIRCComment(
173 $this->context->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
174 ->plain() . $sep . $this->user_comment,
175 'move', 'move',
176 $move_params,
177 $this->user_comment
178 );
179
180 # move/move_redir
181 $this->assertIRCComment(
182 $this->context->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
183 ->plain() . $sep . $this->user_comment,
184 'move', 'move_redir',
185 $move_params,
186 $this->user_comment
187 );
188 }
189
190 /**
191 * @covers LogFormatter::getIRCActionText
192 */
193 public function testIrcMsgForLogTypePatrol() {
194 # patrol/patrol
195 $this->assertIRCComment(
196 $this->context->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
197 'patrol', 'patrol',
198 array(
199 '4::curid' => '777',
200 '5::previd' => '666',
201 '6::auto' => 0,
202 )
203 );
204 }
205
206 /**
207 * @covers LogFormatter::getIRCActionText
208 */
209 public function testIrcMsgForLogTypeProtect() {
210 $protectParams = array(
211 '[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)'
212 );
213 $sep = $this->context->msg( 'colon-separator' )->text();
214
215 # protect/protect
216 $this->assertIRCComment(
217 $this->context->msg( 'protectedarticle', 'SomeTitle ' . $protectParams[0] )
218 ->plain() . $sep . $this->user_comment,
219 'protect', 'protect',
220 $protectParams,
221 $this->user_comment
222 );
223
224 # protect/unprotect
225 $this->assertIRCComment(
226 $this->context->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
227 'protect', 'unprotect',
228 array(),
229 $this->user_comment
230 );
231
232 # protect/modify
233 $this->assertIRCComment(
234 $this->context->msg( 'modifiedarticleprotection', 'SomeTitle ' . $protectParams[0] )
235 ->plain() . $sep . $this->user_comment,
236 'protect', 'modify',
237 $protectParams,
238 $this->user_comment
239 );
240
241 # protect/move_prot
242 $this->assertIRCComment(
243 $this->context->msg( 'movedarticleprotection', 'SomeTitle', 'OldTitle' )
244 ->plain() . $sep . $this->user_comment,
245 'protect', 'move_prot',
246 array( 'OldTitle' ),
247 $this->user_comment
248 );
249 }
250
251 /**
252 * @covers LogFormatter::getIRCActionText
253 */
254 public function testIrcMsgForLogTypeUpload() {
255 $sep = $this->context->msg( 'colon-separator' )->text();
256
257 # upload/upload
258 $this->assertIRCComment(
259 $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
260 'upload', 'upload',
261 array(),
262 $this->user_comment
263 );
264
265 # upload/overwrite
266 $this->assertIRCComment(
267 $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
268 'upload', 'overwrite',
269 array(),
270 $this->user_comment
271 );
272 }
273
274 /**
275 * @covers LogFormatter::getIRCActionText
276 */
277 public function testIrcMsgForLogTypeMerge() {
278 $sep = $this->context->msg( 'colon-separator' )->text();
279
280 # merge/merge
281 $this->assertIRCComment(
282 $this->context->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
283 . $sep . $this->user_comment,
284 'merge', 'merge',
285 array(
286 '4::dest' => 'Dest',
287 '5::mergepoint' => 'timestamp',
288 ),
289 $this->user_comment
290 );
291 }
292
293 /**
294 * @covers LogFormatter::getIRCActionText
295 */
296 public function testIrcMsgForLogTypeImport() {
297 $sep = $this->context->msg( 'colon-separator' )->text();
298
299 # import/upload
300 $msg = $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
301 $sep .
302 $this->user_comment;
303 $this->assertIRCComment(
304 $msg,
305 'import', 'upload',
306 array(),
307 $this->user_comment
308 );
309
310 # import/interwiki
311 $msg = $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
312 $sep .
313 $this->user_comment;
314 $this->assertIRCComment(
315 $msg,
316 'import', 'interwiki',
317 array(),
318 $this->user_comment
319 );
320 }
321
322 /**
323 * @todo Emulate these edits somehow and extract
324 * raw edit summary from RecentChange object
325 * --
326 */
327 /*
328 public function testIrcMsgForBlankingAES() {
329 // $this->context->msg( 'autosumm-blank', .. );
330 }
331
332 public function testIrcMsgForReplaceAES() {
333 // $this->context->msg( 'autosumm-replace', .. );
334 }
335
336 public function testIrcMsgForRollbackAES() {
337 // $this->context->msg( 'revertpage', .. );
338 }
339
340 public function testIrcMsgForUndoAES() {
341 // $this->context->msg( 'undo-summary', .. );
342 }
343 */
344
345 /**
346 * @covers RecentChange::parseParams
347 */
348 public function testParseParams() {
349 $params = array(
350 'root' => array(
351 'A' => 1,
352 'B' => 'two'
353 )
354 );
355
356 $this->assertParseParams(
357 $params,
358 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
359 );
360
361 $this->assertParseParams(
362 null,
363 null
364 );
365
366 $this->assertParseParams(
367 null,
368 serialize( false )
369 );
370
371 $this->assertParseParams(
372 null,
373 'not-an-array'
374 );
375 }
376
377 /**
378 * @param array $expectedParseParams
379 * @param string|null $rawRcParams
380 */
381 protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
382 $rc = new RecentChange;
383 $rc->setAttribs( array( 'rc_params' => $rawRcParams ) );
384
385 $actualParseParams = $rc->parseParams();
386
387 $this->assertEquals( $expectedParseParams, $actualParseParams );
388 }
389
390 /**
391 * @param string $expected Expected IRC text without colors codes
392 * @param string $type Log type (move, delete, suppress, patrol ...)
393 * @param string $action A log type action
394 * @param array $params
395 * @param string $comment (optional) A comment for the log action
396 * @param string $msg (optional) A message for PHPUnit :-)
397 */
398 protected function assertIRCComment( $expected, $type, $action, $params,
399 $comment = null, $msg = '', $legacy = false
400 ) {
401 $logEntry = new ManualLogEntry( $type, $action );
402 $logEntry->setPerformer( $this->user );
403 $logEntry->setTarget( $this->title );
404 if ( $comment !== null ) {
405 $logEntry->setComment( $comment );
406 }
407 $logEntry->setParameters( $params );
408 $logEntry->setLegacy( $legacy );
409
410 $formatter = LogFormatter::newFromEntry( $logEntry );
411 $formatter->setContext( $this->context );
412
413 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
414 $ircRcComment = IRCColourfulRCFeedFormatter::cleanupForIRC( $formatter->getIRCActionComment() );
415
416 $this->assertEquals(
417 $expected,
418 $ircRcComment,
419 $msg
420 );
421 }
422 }