0cd1b3432c4cb003d00fe70a6fddb3ed10a5ebd4
[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 return array_map(
59 function ( $k, $v ) {
60 return is_numeric( $k ) ? $v : "$k = $v";
61 },
62 array_keys( $conds ),
63 $conds
64 );
65 }
66
67 /** return false if condition begin with 'rc_timestamp ' */
68 private static function filterOutRcTimestampCondition( $var ) {
69 return ( false === strpos( $var, 'rc_timestamp ' ) );
70 }
71
72 public function testRcNsFilter() {
73 $this->assertConditions(
74 [ # expected
75 'rc_bot' => 0,
76 "rc_type != '6'",
77 "rc_namespace = '0'",
78 ],
79 [
80 'namespace' => NS_MAIN,
81 ],
82 "rc conditions with no options (aka default setting)"
83 );
84 }
85
86 public function testRcNsFilterInversion() {
87 $this->assertConditions(
88 [ # expected
89 'rc_bot' => 0,
90 "rc_type != '6'",
91 "rc_namespace != '0'",
92 ],
93 [
94 'namespace' => NS_MAIN,
95 'invert' => 1,
96 ],
97 "rc conditions with namespace inverted"
98 );
99 }
100
101 /**
102 * @bug 2429
103 * @dataProvider provideNamespacesAssociations
104 */
105 public function testRcNsFilterAssociation( $ns1, $ns2 ) {
106 $this->assertConditions(
107 [ # expected
108 'rc_bot' => 0,
109 "rc_type != '6'",
110 "(rc_namespace = '$ns1' OR rc_namespace = '$ns2')",
111 ],
112 [
113 'namespace' => $ns1,
114 'associated' => 1,
115 ],
116 "rc conditions with namespace inverted"
117 );
118 }
119
120 /**
121 * @bug 2429
122 * @dataProvider provideNamespacesAssociations
123 */
124 public function testRcNsFilterAssociationWithInversion( $ns1, $ns2 ) {
125 $this->assertConditions(
126 [ # expected
127 'rc_bot' => 0,
128 "rc_type != '6'",
129 "(rc_namespace != '$ns1' AND rc_namespace != '$ns2')",
130 ],
131 [
132 'namespace' => $ns1,
133 'associated' => 1,
134 'invert' => 1,
135 ],
136 "rc conditions with namespace inverted"
137 );
138 }
139
140 /**
141 * Provides associated namespaces to test recent changes
142 * namespaces association filtering.
143 */
144 public static function provideNamespacesAssociations() {
145 return [ # (NS => Associated_NS)
146 [ NS_MAIN, NS_TALK ],
147 [ NS_TALK, NS_MAIN ],
148 ];
149 }
150
151 public function testRcHidemyselfFilter() {
152 $user = $this->getTestUser()->getUser();
153 $this->assertConditions(
154 [ # expected
155 'rc_bot' => 0,
156 0 => "rc_user != '{$user->getId()}'",
157 1 => "rc_type != '6'",
158 ],
159 [
160 'hidemyself' => 1,
161 ],
162 "rc conditions: hidemyself=1 (logged in)",
163 $user
164 );
165
166 $user = User::newFromName( '10.11.12.13', false );
167 $this->assertConditions(
168 [ # expected
169 'rc_bot' => 0,
170 0 => "rc_user_text != '10.11.12.13'",
171 1 => "rc_type != '6'",
172 ],
173 [
174 'hidemyself' => 1,
175 ],
176 "rc conditions: hidemyself=1 (anon)",
177 $user
178 );
179 }
180
181 public function testRcHidebyothersFilter() {
182 $user = $this->getTestUser()->getUser();
183 $this->assertConditions(
184 [ # expected
185 'rc_bot' => 0,
186 0 => "rc_user = '{$user->getId()}'",
187 1 => "rc_type != '6'",
188 ],
189 [
190 'hidebyothers' => 1,
191 ],
192 "rc conditions: hidebyothers=1 (logged in)",
193 $user
194 );
195
196 $user = User::newFromName( '10.11.12.13', false );
197 $this->assertConditions(
198 [ # expected
199 'rc_bot' => 0,
200 0 => "rc_user_text = '10.11.12.13'",
201 1 => "rc_type != '6'",
202 ],
203 [
204 'hidebyothers' => 1,
205 ],
206 "rc conditions: hidebyothers=1 (anon)",
207 $user
208 );
209 }
210
211 public function testRcHidemyselfHidebyothersFilter() {
212 $user = $this->getTestUser()->getUser();
213 $this->assertConditions(
214 [ # expected
215 'rc_bot' => 0,
216 0 => "rc_user != '{$user->getId()}'",
217 1 => "rc_user = '{$user->getId()}'",
218 2 => "rc_type != '6'",
219 ],
220 [
221 'hidemyself' => 1,
222 'hidebyothers' => 1,
223 ],
224 "rc conditions: hidemyself=1 hidebyothers=1 (logged in)",
225 $user
226 );
227 }
228
229 public function testRcHidepageedits() {
230 $this->assertConditions(
231 [ # expected
232 'rc_bot' => 0,
233 "rc_type != '6'",
234 "rc_type != '0'",
235 ],
236 [
237 'hidepageedits' => 1,
238 ],
239 "rc conditions: hidepageedits=1"
240 );
241 }
242
243 public function testRcHidenewpages() {
244 $this->assertConditions(
245 [ # expected
246 'rc_bot' => 0,
247 "rc_type != '6'",
248 "rc_type != '1'",
249 ],
250 [
251 'hidenewpages' => 1,
252 ],
253 "rc conditions: hidenewpages=1"
254 );
255 }
256
257 public function testRcHidelog() {
258 $this->assertConditions(
259 [ # expected
260 'rc_bot' => 0,
261 "rc_type != '6'",
262 "rc_type != '3'",
263 ],
264 [
265 'hidelog' => 1,
266 ],
267 "rc conditions: hidelog=1"
268 );
269 }
270
271 public function testRcHidehumans() {
272 $this->assertConditions(
273 [ # expected
274 'rc_bot' => 1,
275 "rc_type != '6'",
276 ],
277 [
278 'hidebots' => 0,
279 'hidehumans' => 1,
280 ],
281 "rc conditions: hidebots=0 hidehumans=1"
282 );
283 }
284 }