Merge "Use structured logging/MWLoggerFactory for TransactionProfiler"
[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 * - upload/upload
50 * - merge/merge
51 * - import/upload
52 * - import/interwiki
53 *
54 * As well as the following Auto Edit Summaries:
55 * - blank
56 * - replace
57 * - rollback
58 * - undo
59 */
60
61 /**
62 * @covers LogFormatter::getIRCActionText
63 */
64 public function testIrcMsgForLogTypeBlock() {
65 $sep = $this->context->msg( 'colon-separator' )->text();
66
67 # block/block
68 $this->assertIRCComment(
69 $this->context->msg( 'blocklogentry', 'SomeTitle', 'duration', '(flags)' )->plain()
70 . $sep . $this->user_comment,
71 'block', 'block',
72 array(
73 '5::duration' => 'duration',
74 '6::flags' => 'flags',
75 ),
76 $this->user_comment
77 );
78 # block/unblock
79 $this->assertIRCComment(
80 $this->context->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
81 'block', 'unblock',
82 array(),
83 $this->user_comment
84 );
85 # block/reblock
86 $this->assertIRCComment(
87 $this->context->msg( 'reblock-logentry', 'SomeTitle', 'duration', '(flags)' )->plain()
88 . $sep . $this->user_comment,
89 'block', 'reblock',
90 array(
91 '5::duration' => 'duration',
92 '6::flags' => 'flags',
93 ),
94 $this->user_comment
95 );
96 }
97
98 /**
99 * @covers LogFormatter::getIRCActionText
100 */
101 public function testIrcMsgForLogTypeDelete() {
102 $sep = $this->context->msg( 'colon-separator' )->text();
103
104 # delete/delete
105 $this->assertIRCComment(
106 $this->context->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
107 'delete', 'delete',
108 array(),
109 $this->user_comment
110 );
111
112 # delete/restore
113 $this->assertIRCComment(
114 $this->context->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
115 'delete', 'restore',
116 array(),
117 $this->user_comment
118 );
119 }
120
121 /**
122 * @covers LogFormatter::getIRCActionText
123 */
124 public function testIrcMsgForLogTypeNewusers() {
125 $this->assertIRCComment(
126 'New user account',
127 'newusers', 'newusers',
128 array()
129 );
130 $this->assertIRCComment(
131 'New user account',
132 'newusers', 'create',
133 array()
134 );
135 $this->assertIRCComment(
136 'created new account SomeTitle',
137 'newusers', 'create2',
138 array()
139 );
140 $this->assertIRCComment(
141 'Account created automatically',
142 'newusers', 'autocreate',
143 array()
144 );
145 }
146
147 /**
148 * @covers LogFormatter::getIRCActionText
149 */
150 public function testIrcMsgForLogTypeMove() {
151 $move_params = array(
152 '4::target' => $this->target->getPrefixedText(),
153 '5::noredir' => 0,
154 );
155 $sep = $this->context->msg( 'colon-separator' )->text();
156
157 # move/move
158 $this->assertIRCComment(
159 $this->context->msg( '1movedto2', 'SomeTitle', 'TestTarget' )
160 ->plain() . $sep . $this->user_comment,
161 'move', 'move',
162 $move_params,
163 $this->user_comment
164 );
165
166 # move/move_redir
167 $this->assertIRCComment(
168 $this->context->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )
169 ->plain() . $sep . $this->user_comment,
170 'move', 'move_redir',
171 $move_params,
172 $this->user_comment
173 );
174 }
175
176 /**
177 * @covers LogFormatter::getIRCActionText
178 */
179 public function testIrcMsgForLogTypePatrol() {
180 # patrol/patrol
181 $this->assertIRCComment(
182 $this->context->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
183 'patrol', 'patrol',
184 array(
185 '4::curid' => '777',
186 '5::previd' => '666',
187 '6::auto' => 0,
188 )
189 );
190 }
191
192 /**
193 * @covers LogFormatter::getIRCActionText
194 */
195 public function testIrcMsgForLogTypeProtect() {
196 $protectParams = array(
197 '[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)'
198 );
199 $sep = $this->context->msg( 'colon-separator' )->text();
200
201 # protect/protect
202 $this->assertIRCComment(
203 $this->context->msg( 'protectedarticle', 'SomeTitle ' . $protectParams[0] )
204 ->plain() . $sep . $this->user_comment,
205 'protect', 'protect',
206 $protectParams,
207 $this->user_comment
208 );
209
210 # protect/unprotect
211 $this->assertIRCComment(
212 $this->context->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
213 'protect', 'unprotect',
214 array(),
215 $this->user_comment
216 );
217
218 # protect/modify
219 $this->assertIRCComment(
220 $this->context->msg( 'modifiedarticleprotection', 'SomeTitle ' . $protectParams[0] )
221 ->plain() . $sep . $this->user_comment,
222 'protect', 'modify',
223 $protectParams,
224 $this->user_comment
225 );
226 }
227
228 /**
229 * @covers LogFormatter::getIRCActionText
230 */
231 public function testIrcMsgForLogTypeUpload() {
232 $sep = $this->context->msg( 'colon-separator' )->text();
233
234 # upload/upload
235 $this->assertIRCComment(
236 $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
237 'upload', 'upload',
238 array(),
239 $this->user_comment
240 );
241
242 # upload/overwrite
243 $this->assertIRCComment(
244 $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
245 'upload', 'overwrite',
246 array(),
247 $this->user_comment
248 );
249 }
250
251 /**
252 * @covers LogFormatter::getIRCActionText
253 */
254 public function testIrcMsgForLogTypeMerge() {
255 $sep = $this->context->msg( 'colon-separator' )->text();
256
257 # merge/merge
258 $this->assertIRCComment(
259 $this->context->msg( 'pagemerge-logentry', 'SomeTitle', 'Dest', 'timestamp' )->plain()
260 . $sep . $this->user_comment,
261 'merge', 'merge',
262 array(
263 '4::dest' => 'Dest',
264 '5::mergepoint' => 'timestamp',
265 ),
266 $this->user_comment
267 );
268 }
269
270 /**
271 * @covers LogFormatter::getIRCActionText
272 */
273 public function testIrcMsgForLogTypeImport() {
274 $sep = $this->context->msg( 'colon-separator' )->text();
275
276 # import/upload
277 $this->assertIRCComment(
278 $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() . $sep . $this->user_comment,
279 'import', 'upload',
280 array(),
281 $this->user_comment
282 );
283
284 # import/interwiki
285 $this->assertIRCComment(
286 $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() . $sep . $this->user_comment,
287 'import', 'interwiki',
288 array(),
289 $this->user_comment
290 );
291 }
292
293 /**
294 * @todo Emulate these edits somehow and extract
295 * raw edit summary from RecentChange object
296 * --
297 */
298 /*
299 public function testIrcMsgForBlankingAES() {
300 // $this->context->msg( 'autosumm-blank', .. );
301 }
302
303 public function testIrcMsgForReplaceAES() {
304 // $this->context->msg( 'autosumm-replace', .. );
305 }
306
307 public function testIrcMsgForRollbackAES() {
308 // $this->context->msg( 'revertpage', .. );
309 }
310
311 public function testIrcMsgForUndoAES() {
312 // $this->context->msg( 'undo-summary', .. );
313 }
314 */
315
316 /**
317 * @param string $expected Expected IRC text without colors codes
318 * @param string $type Log type (move, delete, suppress, patrol ...)
319 * @param string $action A log type action
320 * @param array $params
321 * @param string $comment (optional) A comment for the log action
322 * @param string $msg (optional) A message for PHPUnit :-)
323 */
324 protected function assertIRCComment( $expected, $type, $action, $params,
325 $comment = null, $msg = ''
326 ) {
327 $logEntry = new ManualLogEntry( $type, $action );
328 $logEntry->setPerformer( $this->user );
329 $logEntry->setTarget( $this->title );
330 if ( $comment !== null ) {
331 $logEntry->setComment( $comment );
332 }
333 $logEntry->setParameters( $params );
334
335 $formatter = LogFormatter::newFromEntry( $logEntry );
336 $formatter->setContext( $this->context );
337
338 // Apply the same transformation as done in IRCColourfulRCFeedFormatter::getLine for rc_comment
339 $ircRcComment = IRCColourfulRCFeedFormatter::cleanupForIRC( $formatter->getIRCActionComment() );
340
341 $this->assertEquals(
342 $expected,
343 $ircRcComment,
344 $msg
345 );
346 }
347 }