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