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