Merge "http: Support HTTP Basic Authentication"
[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 $expected,
52 $queryConditions,
53 $message
54 );
55 }
56
57 /** return false if condition begin with 'rc_timestamp ' */
58 private static function filterOutRcTimestampCondition( $var ) {
59 return ( false === strpos( $var, 'rc_timestamp ' ) );
60 }
61
62 public function testRcNsFilter() {
63 $this->assertConditions(
64 [ # expected
65 'rc_bot' => 0,
66 0 => "rc_type != '6'",
67 1 => "rc_namespace = '0'",
68 ],
69 [
70 'namespace' => NS_MAIN,
71 ],
72 "rc conditions with no options (aka default setting)"
73 );
74 }
75
76 public function testRcNsFilterInversion() {
77 $this->assertConditions(
78 [ # expected
79 'rc_bot' => 0,
80 0 => "rc_type != '6'",
81 1 => sprintf( "rc_namespace != '%s'", NS_MAIN ),
82 ],
83 [
84 'namespace' => NS_MAIN,
85 'invert' => 1,
86 ],
87 "rc conditions with namespace inverted"
88 );
89 }
90
91 /**
92 * @bug 2429
93 * @dataProvider provideNamespacesAssociations
94 */
95 public function testRcNsFilterAssociation( $ns1, $ns2 ) {
96 $this->assertConditions(
97 [ # expected
98 'rc_bot' => 0,
99 0 => "rc_type != '6'",
100 1 => sprintf( "(rc_namespace = '%s' OR rc_namespace = '%s')", $ns1, $ns2 ),
101 ],
102 [
103 'namespace' => $ns1,
104 'associated' => 1,
105 ],
106 "rc conditions with namespace inverted"
107 );
108 }
109
110 /**
111 * @bug 2429
112 * @dataProvider provideNamespacesAssociations
113 */
114 public function testRcNsFilterAssociationWithInversion( $ns1, $ns2 ) {
115 $this->assertConditions(
116 [ # expected
117 'rc_bot' => 0,
118 0 => "rc_type != '6'",
119 1 => sprintf( "(rc_namespace != '%s' AND rc_namespace != '%s')", $ns1, $ns2 ),
120 ],
121 [
122 'namespace' => $ns1,
123 'associated' => 1,
124 'invert' => 1,
125 ],
126 "rc conditions with namespace inverted"
127 );
128 }
129
130 /**
131 * Provides associated namespaces to test recent changes
132 * namespaces association filtering.
133 */
134 public static function provideNamespacesAssociations() {
135 return [ # (NS => Associated_NS)
136 [ NS_MAIN, NS_TALK ],
137 [ NS_TALK, NS_MAIN ],
138 ];
139 }
140
141 public function testRcHidemyselfFilter() {
142 $user = $this->getTestUser()->getUser();
143 $this->assertConditions(
144 [ # expected
145 'rc_bot' => 0,
146 0 => "rc_user != '{$user->getId()}'",
147 1 => "rc_type != '6'",
148 ],
149 [
150 'hidemyself' => 1,
151 ],
152 "rc conditions: hidemyself=1 (logged in)",
153 $user
154 );
155
156 $user = User::newFromName( '10.11.12.13', false );
157 $this->assertConditions(
158 [ # expected
159 'rc_bot' => 0,
160 0 => "rc_user_text != '10.11.12.13'",
161 1 => "rc_type != '6'",
162 ],
163 [
164 'hidemyself' => 1,
165 ],
166 "rc conditions: hidemyself=1 (anon)",
167 $user
168 );
169 }
170
171 public function testRcHidebyothersFilter() {
172 $user = $this->getTestUser()->getUser();
173 $this->assertConditions(
174 [ # expected
175 'rc_bot' => 0,
176 0 => "rc_user = '{$user->getId()}'",
177 1 => "rc_type != '6'",
178 ],
179 [
180 'hidebyothers' => 1,
181 ],
182 "rc conditions: hidebyothers=1 (logged in)",
183 $user
184 );
185
186 $user = User::newFromName( '10.11.12.13', false );
187 $this->assertConditions(
188 [ # expected
189 'rc_bot' => 0,
190 0 => "rc_user_text = '10.11.12.13'",
191 1 => "rc_type != '6'",
192 ],
193 [
194 'hidebyothers' => 1,
195 ],
196 "rc conditions: hidebyothers=1 (anon)",
197 $user
198 );
199 }
200
201 public function testRcHidemyselfHidebyothersFilter() {
202 $user = $this->getTestUser()->getUser();
203 $this->assertConditions(
204 [ # expected
205 'rc_bot' => 0,
206 0 => "rc_user != '{$user->getId()}'",
207 1 => "rc_user = '{$user->getId()}'",
208 2 => "rc_type != '6'",
209 ],
210 [
211 'hidemyself' => 1,
212 'hidebyothers' => 1,
213 ],
214 "rc conditions: hidemyself=1 hidebyothers=1 (logged in)",
215 $user
216 );
217 }
218 }