Merge "Add dotall modifier to EDITSECTION_REGEX"
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / SpecialRecentchangesTest.php
1 <?php
2 /**
3 * Test class for SpecialRecentchanges class
4 *
5 * Copyright © 2011, Antoine Musso
6 *
7 * @author Antoine Musso
8 * @group Database
9 *
10 * @covers SpecialRecentChanges
11 */
12 class SpecialRecentchangesTest extends MediaWikiTestCase {
13
14 protected function setUp() {
15 parent::setUp();
16 $this->setMwGlobals( 'wgRCWatchCategoryMembership', true );
17 }
18
19 /**
20 * @var SpecialRecentChanges
21 */
22 protected $rc;
23
24 /** helper to test SpecialRecentchanges::buildMainQueryConds() */
25 private function assertConditions(
26 $expected,
27 $requestOptions = null,
28 $message = '',
29 $user = null
30 ) {
31 $context = new RequestContext;
32 $context->setRequest( new FauxRequest( $requestOptions ) );
33 if ( $user ) {
34 $context->setUser( $user );
35 }
36
37 # setup the rc object
38 $this->rc = new SpecialRecentChanges();
39 $this->rc->setContext( $context );
40 $formOptions = $this->rc->setup( null );
41
42 #  Filter out rc_timestamp conditions which depends on the test runtime
43 # This condition is not needed as of march 2, 2011 -- hashar
44 # @todo FIXME: Find a way to generate the correct rc_timestamp
45 $queryConditions = array_filter(
46 $this->rc->buildMainQueryConds( $formOptions ),
47 'SpecialRecentchangesTest::filterOutRcTimestampCondition'
48 );
49
50 $this->assertEquals(
51 self::normalizeCondition( $expected ),
52 self::normalizeCondition( $queryConditions ),
53 $message
54 );
55 }
56
57 private static function normalizeCondition( $conds ) {
58 $normalized = array_map(
59 function ( $k, $v ) {
60 return is_numeric( $k ) ? $v : "$k = $v";
61 },
62 array_keys( $conds ),
63 $conds
64 );
65 sort( $normalized );
66 return $normalized;
67 }
68
69 /** return false if condition begin with 'rc_timestamp ' */
70 private static function filterOutRcTimestampCondition( $var ) {
71 return ( false === strpos( $var, 'rc_timestamp ' ) );
72 }
73
74 public function testRcNsFilter() {
75 $this->assertConditions(
76 [ # expected
77 'rc_bot' => 0,
78 "rc_type != '6'",
79 "rc_namespace = '0'",
80 ],
81 [
82 'namespace' => NS_MAIN,
83 ],
84 "rc conditions with no options (aka default setting)"
85 );
86 }
87
88 public function testRcNsFilterInversion() {
89 $this->assertConditions(
90 [ # expected
91 'rc_bot' => 0,
92 "rc_type != '6'",
93 "rc_namespace != '0'",
94 ],
95 [
96 'namespace' => NS_MAIN,
97 'invert' => 1,
98 ],
99 "rc conditions with namespace inverted"
100 );
101 }
102
103 /**
104 * @bug 2429
105 * @dataProvider provideNamespacesAssociations
106 */
107 public function testRcNsFilterAssociation( $ns1, $ns2 ) {
108 $this->assertConditions(
109 [ # expected
110 'rc_bot' => 0,
111 "rc_type != '6'",
112 "(rc_namespace = '$ns1' OR rc_namespace = '$ns2')",
113 ],
114 [
115 'namespace' => $ns1,
116 'associated' => 1,
117 ],
118 "rc conditions with namespace inverted"
119 );
120 }
121
122 /**
123 * @bug 2429
124 * @dataProvider provideNamespacesAssociations
125 */
126 public function testRcNsFilterAssociationWithInversion( $ns1, $ns2 ) {
127 $this->assertConditions(
128 [ # expected
129 'rc_bot' => 0,
130 "rc_type != '6'",
131 "(rc_namespace != '$ns1' AND rc_namespace != '$ns2')",
132 ],
133 [
134 'namespace' => $ns1,
135 'associated' => 1,
136 'invert' => 1,
137 ],
138 "rc conditions with namespace inverted"
139 );
140 }
141
142 /**
143 * Provides associated namespaces to test recent changes
144 * namespaces association filtering.
145 */
146 public static function provideNamespacesAssociations() {
147 return [ # (NS => Associated_NS)
148 [ NS_MAIN, NS_TALK ],
149 [ NS_TALK, NS_MAIN ],
150 ];
151 }
152
153 public function testRcHidemyselfFilter() {
154 $user = $this->getTestUser()->getUser();
155 $this->assertConditions(
156 [ # expected
157 'rc_bot' => 0,
158 "rc_user != '{$user->getId()}'",
159 "rc_type != '6'",
160 ],
161 [
162 'hidemyself' => 1,
163 ],
164 "rc conditions: hidemyself=1 (logged in)",
165 $user
166 );
167
168 $user = User::newFromName( '10.11.12.13', false );
169 $this->assertConditions(
170 [ # expected
171 'rc_bot' => 0,
172 "rc_user_text != '10.11.12.13'",
173 "rc_type != '6'",
174 ],
175 [
176 'hidemyself' => 1,
177 ],
178 "rc conditions: hidemyself=1 (anon)",
179 $user
180 );
181 }
182
183 public function testRcHidebyothersFilter() {
184 $user = $this->getTestUser()->getUser();
185 $this->assertConditions(
186 [ # expected
187 'rc_bot' => 0,
188 "rc_user = '{$user->getId()}'",
189 "rc_type != '6'",
190 ],
191 [
192 'hidebyothers' => 1,
193 ],
194 "rc conditions: hidebyothers=1 (logged in)",
195 $user
196 );
197
198 $user = User::newFromName( '10.11.12.13', false );
199 $this->assertConditions(
200 [ # expected
201 'rc_bot' => 0,
202 "rc_user_text = '10.11.12.13'",
203 "rc_type != '6'",
204 ],
205 [
206 'hidebyothers' => 1,
207 ],
208 "rc conditions: hidebyothers=1 (anon)",
209 $user
210 );
211 }
212
213 public function testRcHidemyselfHidebyothersFilter() {
214 $user = $this->getTestUser()->getUser();
215 $this->assertConditions(
216 [ # expected
217 'rc_bot' => 0,
218 "rc_user != '{$user->getId()}'",
219 "rc_user = '{$user->getId()}'",
220 "rc_type != '6'",
221 ],
222 [
223 'hidemyself' => 1,
224 'hidebyothers' => 1,
225 ],
226 "rc conditions: hidemyself=1 hidebyothers=1 (logged in)",
227 $user
228 );
229 }
230
231 public function testRcHidepageedits() {
232 $this->assertConditions(
233 [ # expected
234 'rc_bot' => 0,
235 "rc_type != '6'",
236 "rc_type != '0'",
237 ],
238 [
239 'hidepageedits' => 1,
240 ],
241 "rc conditions: hidepageedits=1"
242 );
243 }
244
245 public function testRcHidenewpages() {
246 $this->assertConditions(
247 [ # expected
248 'rc_bot' => 0,
249 "rc_type != '6'",
250 "rc_type != '1'",
251 ],
252 [
253 'hidenewpages' => 1,
254 ],
255 "rc conditions: hidenewpages=1"
256 );
257 }
258
259 public function testRcHidelog() {
260 $this->assertConditions(
261 [ # expected
262 'rc_bot' => 0,
263 "rc_type != '6'",
264 "rc_type != '3'",
265 ],
266 [
267 'hidelog' => 1,
268 ],
269 "rc conditions: hidelog=1"
270 );
271 }
272
273 public function testRcHidehumans() {
274 $this->assertConditions(
275 [ # expected
276 'rc_bot' => 1,
277 "rc_type != '6'",
278 ],
279 [
280 'hidebots' => 0,
281 'hidehumans' => 1,
282 ],
283 "rc conditions: hidebots=0 hidehumans=1"
284 );
285 }
286
287 public function testRcHidepatrolledDisabledFilter() {
288 $user = $this->getTestUser()->getUser();
289 $this->assertConditions(
290 [ # expected
291 'rc_bot' => 0,
292 "rc_type != '6'",
293 ],
294 [
295 'hidepatrolled' => 1,
296 ],
297 "rc conditions: hidepatrolled=1 (user not allowed)",
298 $user
299 );
300 }
301
302 public function testRcHideunpatrolledDisabledFilter() {
303 $user = $this->getTestUser()->getUser();
304 $this->assertConditions(
305 [ # expected
306 'rc_bot' => 0,
307 "rc_type != '6'",
308 ],
309 [
310 'hideunpatrolled' => 1,
311 ],
312 "rc conditions: hideunpatrolled=1 (user not allowed)",
313 $user
314 );
315 }
316 public function testRcHidepatrolledFilter() {
317 $user = $this->getTestSysop()->getUser();
318 $this->assertConditions(
319 [ # expected
320 'rc_bot' => 0,
321 "rc_patrolled = 0",
322 "rc_type != '6'",
323 ],
324 [
325 'hidepatrolled' => 1,
326 ],
327 "rc conditions: hidepatrolled=1",
328 $user
329 );
330 }
331
332 public function testRcHideunpatrolledFilter() {
333 $user = $this->getTestSysop()->getUser();
334 $this->assertConditions(
335 [ # expected
336 'rc_bot' => 0,
337 "rc_patrolled = 1",
338 "rc_type != '6'",
339 ],
340 [
341 'hideunpatrolled' => 1,
342 ],
343 "rc conditions: hideunpatrolled=1",
344 $user
345 );
346 }
347
348 public function testRcHideminorFilter() {
349 $this->assertConditions(
350 [ # expected
351 'rc_bot' => 0,
352 "rc_minor = 0",
353 "rc_type != '6'",
354 ],
355 [
356 'hideminor' => 1,
357 ],
358 "rc conditions: hideminor=1"
359 );
360 }
361
362 public function testRcHidemajorFilter() {
363 $this->assertConditions(
364 [ # expected
365 'rc_bot' => 0,
366 "rc_minor = 1",
367 "rc_type != '6'",
368 ],
369 [
370 'hidemajor' => 1,
371 ],
372 "rc conditions: hidemajor=1"
373 );
374 }
375
376 // This is probably going to change when we do auto-fix of
377 // filters combinations that don't make sense but for now
378 // it's the behavior therefore it's the test.
379 public function testRcHidepatrolledHideunpatrolledFilter() {
380 $user = $this->getTestSysop()->getUser();
381 $this->assertConditions(
382 [ # expected
383 'rc_bot' => 0,
384 "rc_patrolled = 0",
385 "rc_patrolled = 1",
386 "rc_type != '6'",
387 ],
388 [
389 'hidepatrolled' => 1,
390 'hideunpatrolled' => 1,
391 ],
392 "rc conditions: hidepatrolled=1 hideunpatrolled=1",
393 $user
394 );
395 }
396 }