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