Merge "Add input checks for Language::sprintfDate()"
[lhc/web/wiklou.git] / tests / phpunit / includes / RecentChangeTest.php
1 <?php
2 /**
3 * @group Database
4 */
5 class RecentChangeTest extends MediaWikiTestCase {
6 protected $title;
7 protected $target;
8 protected $user;
9 protected $user_comment;
10 protected $context;
11
12 function __construct() {
13 parent::__construct();
14
15 $this->title = Title::newFromText( 'SomeTitle' );
16 $this->target = Title::newFromText( 'TestTarget' );
17 $this->user = User::newFromName( 'UserName' );
18
19 $this->user_comment = '<User comment about action>';
20 $this->context = RequestContext::newExtraneousContext( $this->title );
21 }
22
23 /**
24 * The testIrcMsgForAction* tests are supposed to cover the hacky
25 * LogFormatter::getIRCActionText / bug 34508
26 *
27 * Third parties bots listen to those messages. They are clever enough
28 * to fetch the i18n messages from the wiki and then analyze the IRC feed
29 * to reverse engineer the $1, $2 messages.
30 * One thing bots can not detect is when MediaWiki change the meaning of
31 * a message like what happened when we deployed 1.19. $1 became the user
32 * performing the action which broke basically all bots around.
33 *
34 * Should cover the following log actions (which are most commonly used by bots):
35 * - block/block
36 * - block/unblock
37 * - delete/delete
38 * - delete/restore
39 * - newusers/create
40 * - newusers/create2
41 * - newusers/autocreate
42 * - move/move
43 * - move/move_redir
44 * - protect/protect
45 * - protect/modifyprotect
46 * - protect/unprotect
47 * - upload/upload
48 *
49 * As well as the following Auto Edit Summaries:
50 * - blank
51 * - replace
52 * - rollback
53 * - undo
54 */
55
56 /**
57 * @covers LogFormatter::getIRCActionText
58 */
59 function testIrcMsgForLogTypeBlock() {
60 $sep = $this->context->msg( 'colon-separator' )->text();
61
62 # block/block
63 $this->assertIRCComment(
64 $this->context->msg( 'blocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
65 'block', 'block',
66 array(),
67 $this->user_comment
68 );
69 # block/unblock
70 $this->assertIRCComment(
71 $this->context->msg( 'unblocklogentry', 'SomeTitle' )->plain() . $sep . $this->user_comment,
72 'block', 'unblock',
73 array(),
74 $this->user_comment
75 );
76 }
77
78 /**
79 * @covers LogFormatter::getIRCActionText
80 */
81 function testIrcMsgForLogTypeDelete() {
82 $sep = $this->context->msg( 'colon-separator' )->text();
83
84 # delete/delete
85 $this->assertIRCComment(
86 $this->context->msg( 'deletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
87 'delete', 'delete',
88 array(),
89 $this->user_comment
90 );
91
92 # delete/restore
93 $this->assertIRCComment(
94 $this->context->msg( 'undeletedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
95 'delete', 'restore',
96 array(),
97 $this->user_comment
98 );
99 }
100
101 /**
102 * @covers LogFormatter::getIRCActionText
103 */
104 function testIrcMsgForLogTypeNewusers() {
105 $this->assertIRCComment(
106 'New user account',
107 'newusers', 'newusers',
108 array()
109 );
110 $this->assertIRCComment(
111 'New user account',
112 'newusers', 'create',
113 array()
114 );
115 $this->assertIRCComment(
116 'created new account SomeTitle',
117 'newusers', 'create2',
118 array()
119 );
120 $this->assertIRCComment(
121 'Account created automatically',
122 'newusers', 'autocreate',
123 array()
124 );
125 }
126
127 /**
128 * @covers LogFormatter::getIRCActionText
129 */
130 function testIrcMsgForLogTypeMove() {
131 $move_params = array(
132 '4::target' => $this->target->getPrefixedText(),
133 '5::noredir' => 0,
134 );
135 $sep = $this->context->msg( 'colon-separator' )->text();
136
137 # move/move
138 $this->assertIRCComment(
139 $this->context->msg( '1movedto2', 'SomeTitle', 'TestTarget' )->plain() . $sep . $this->user_comment,
140 'move', 'move',
141 $move_params,
142 $this->user_comment
143 );
144
145 # move/move_redir
146 $this->assertIRCComment(
147 $this->context->msg( '1movedto2_redir', 'SomeTitle', 'TestTarget' )->plain() . $sep . $this->user_comment,
148 'move', 'move_redir',
149 $move_params,
150 $this->user_comment
151 );
152 }
153
154 /**
155 * @covers LogFormatter::getIRCActionText
156 */
157 function testIrcMsgForLogTypePatrol() {
158 # patrol/patrol
159 $this->assertIRCComment(
160 $this->context->msg( 'patrol-log-line', 'revision 777', '[[SomeTitle]]', '' )->plain(),
161 'patrol', 'patrol',
162 array(
163 '4::curid' => '777',
164 '5::previd' => '666',
165 '6::auto' => 0,
166 )
167 );
168 }
169
170 /**
171 * @covers LogFormatter::getIRCActionText
172 */
173 function testIrcMsgForLogTypeProtect() {
174 $protectParams = array(
175 '[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)'
176 );
177 $sep = $this->context->msg( 'colon-separator' )->text();
178
179 # protect/protect
180 $this->assertIRCComment(
181 $this->context->msg( 'protectedarticle', 'SomeTitle ' . $protectParams[0] )->plain() . $sep . $this->user_comment,
182 'protect', 'protect',
183 $protectParams,
184 $this->user_comment
185 );
186
187 # protect/unprotect
188 $this->assertIRCComment(
189 $this->context->msg( 'unprotectedarticle', 'SomeTitle' )->plain() . $sep . $this->user_comment,
190 'protect', 'unprotect',
191 array(),
192 $this->user_comment
193 );
194
195 # protect/modify
196 $this->assertIRCComment(
197 $this->context->msg( 'modifiedarticleprotection', 'SomeTitle ' . $protectParams[0] )->plain() . $sep . $this->user_comment,
198 'protect', 'modify',
199 $protectParams,
200 $this->user_comment
201 );
202 }
203
204 /**
205 * @covers LogFormatter::getIRCActionText
206 */
207 function testIrcMsgForLogTypeUpload() {
208 $sep = $this->context->msg( 'colon-separator' )->text();
209
210 # upload/upload
211 $this->assertIRCComment(
212 $this->context->msg( 'uploadedimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
213 'upload', 'upload',
214 array(),
215 $this->user_comment
216 );
217
218 # upload/overwrite
219 $this->assertIRCComment(
220 $this->context->msg( 'overwroteimage', 'SomeTitle' )->plain() . $sep . $this->user_comment,
221 'upload', 'overwrite',
222 array(),
223 $this->user_comment
224 );
225 }
226
227 /**
228 * @todo: Emulate these edits somehow and extract
229 * raw edit summary from RecentChange object
230 * --
231 */
232 /*
233 function testIrcMsgForBlankingAES() {
234 // $this->context->msg( 'autosumm-blank', .. );
235 }
236
237 function testIrcMsgForReplaceAES() {
238 // $this->context->msg( 'autosumm-replace', .. );
239 }
240
241 function testIrcMsgForRollbackAES() {
242 // $this->context->msg( 'revertpage', .. );
243 }
244
245 function testIrcMsgForUndoAES() {
246 // $this->context->msg( 'undo-summary', .. );
247 }
248 */
249
250 /**
251 * @param $expected String Expected IRC text without colors codes
252 * @param $type String Log type (move, delete, suppress, patrol ...)
253 * @param $action String A log type action
254 * @param $comment String (optional) A comment for the log action
255 * @param $msg String (optional) A message for PHPUnit :-)
256 */
257 function assertIRCComment( $expected, $type, $action, $params, $comment = null, $msg = '' ) {
258
259 $logEntry = new ManualLogEntry( $type, $action );
260 $logEntry->setPerformer( $this->user );
261 $logEntry->setTarget( $this->title );
262 if ( $comment !== null ) {
263 $logEntry->setComment( $comment );
264 }
265 $logEntry->setParameters( $params );
266
267 $formatter = LogFormatter::newFromEntry( $logEntry );
268 $formatter->setContext( $this->context );
269
270 // Apply the same transformation as done in RecentChange::getIRCLine for rc_comment
271 $ircRcComment = RecentChange::cleanupForIRC( $formatter->getIRCActionComment() );
272
273 $this->assertEquals(
274 $expected,
275 $ircRcComment,
276 $msg
277 );
278 }
279 }