Migrate move protect log to new log system
[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(
247 '4::oldtitle' => 'OldTitle'
248 ),
249 $this->user_comment
250 );
251 }
252
253 /**
254 * @covers LogFormatter::getIRCActionText
255 */
256 public function testIrcMsgForLogTypeUpload() {
257 $sep = $this->context->msg( 'colon-separator' )->text();
258
259 # upload/upload
260 $this->assertIRCComment(
261 $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
262 'upload', 'upload',
263 array(),
264 $this->user_comment
265 );
266
267 # upload/overwrite
268 $this->assertIRCComment(
269 $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
270 'upload', 'overwrite',
271 array(),
272 $this->user_comment
273 );
274 }
275
276 /**
277 * @covers LogFormatter::getIRCActionText
278 */
279 public function testIrcMsgForLogTypeMerge() {
280 $sep = $this->context->msg( 'colon-separator' )->text();
281
282 # merge/merge
283 $this->assertIRCComment(
284 $this->context->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
285 . $sep . $this->user_comment,
286 'merge', 'merge',
287 array(
288 '4::dest' => 'Dest',
289 '5::mergepoint' => 'timestamp',
290 ),
291 $this->user_comment
292 );
293 }
294
295 /**
296 * @covers LogFormatter::getIRCActionText
297 */
298 public function testIrcMsgForLogTypeImport() {
299 $sep = $this->context->msg( 'colon-separator' )->text();
300
301 # import/upload
302 $msg = $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() .
303 $sep .
304 $this->user_comment;
305 $this->assertIRCComment(
306 $msg,
307 'import', 'upload',
308 array(),
309 $this->user_comment
310 );
311
312 # import/interwiki
313 $msg = $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() .
314 $sep .
315 $this->user_comment;
316 $this->assertIRCComment(
317 $msg,
318 'import', 'interwiki',
319 array(),
320 $this->user_comment
321 );
322 }
323
324 /**
325 * @todo Emulate these edits somehow and extract
326 * raw edit summary from RecentChange object
327 * --
328 */
329 /*
330 public function testIrcMsgForBlankingAES() {
331 // $this->context->msg( 'autosumm-blank', .. );
332 }
333
334 public function testIrcMsgForReplaceAES() {
335 // $this->context->msg( 'autosumm-replace', .. );
336 }
337
338 public function testIrcMsgForRollbackAES() {
339 // $this->context->msg( 'revertpage', .. );
340 }
341
342 public function testIrcMsgForUndoAES() {
343 // $this->context->msg( 'undo-summary', .. );
344 }
345 */
346
347 /**
348 * @covers RecentChange::parseParams
349 */
350 public function testParseParams() {
351 $params = array(
352 'root' => array(
353 'A' => 1,
354 'B' => 'two'
355 )
356 );
357
358 $this->assertParseParams(
359 $params,
360 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}'
361 );
362
363 $this->assertParseParams(
364 null,
365 null
366 );
367
368 $this->assertParseParams(
369 null,
370 serialize( false )
371 );
372
373 $this->assertParseParams(
374 null,
375 'not-an-array'
376 );
377 }
378
379 /**
380 * @param array $expectedParseParams
381 * @param string|null $rawRcParams
382 */
383 protected function assertParseParams( $expectedParseParams, $rawRcParams ) {
384 $rc = new RecentChange;
385 $rc->setAttribs( array( 'rc_params' => $rawRcParams ) );
386
387 $actualParseParams = $rc->parseParams();
388
389 $this->assertEquals( $expectedParseParams, $actualParseParams );
390 }
391
392 /**
393 * @param string $expected Expected IRC text without colors codes
394 * @param string $type Log type (move, delete, suppress, patrol ...)
395 * @param string $action A log type action
396 * @param array $params
397 * @param string $comment (optional) A comment for the log action
398 * @param string $msg (optional) A message for PHPUnit :-)
399 */
400 protected function assertIRCComment( $expected, $type, $action, $params,
401 $comment = null, $msg = '', $legacy = false
402 ) {
403 $logEntry = new ManualLogEntry( $type, $action );
404 $logEntry->setPerformer( $this->user );
405 $logEntry->setTarget( $this->title );
406 if ( $comment !== null ) {
407 $logEntry->setComment( $comment );
408 }
409 $logEntry->setParameters( $params );
410 $logEntry->setLegacy( $legacy );
411
412 $formatter = LogFormatter::newFromEntry( $logEntry );
413 $formatter->setContext( $this->context );
414
415 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
416 $ircRcComment = IRCColourfulRCFeedFormatter::cleanupForIRC( $formatter->getIRCActionComment() );
417
418 $this->assertEquals(
419 $expected,
420 $ircRcComment,
421 $msg
422 );
423 }
424 }