Merge "(bug 17602) fix Monobook action tabs not quite touching the page body"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiOptionsTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 */
8 class ApiOptionsTest extends MediaWikiLangTestCase {
9
10 private $mTested, $mUserMock, $mContext, $mSession;
11
12 private $mOldGetPreferencesHooks = false;
13
14 private static $Success = array( 'options' => 'success' );
15
16 protected function setUp() {
17 parent::setUp();
18
19 $this->mUserMock = $this->getMockBuilder( 'User' )
20 ->disableOriginalConstructor()
21 ->getMock();
22
23 // Set up groups
24 $this->mUserMock->expects( $this->any() )
25 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user' ) ) );
26
27 // Set up callback for User::getOptionKinds
28 $this->mUserMock->expects( $this->any() )
29 ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) );
30
31 // Create a new context
32 $this->mContext = new DerivativeContext( new RequestContext() );
33 $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
34 $this->mContext->setUser( $this->mUserMock );
35
36 $main = new ApiMain( $this->mContext );
37
38 // Empty session
39 $this->mSession = array();
40
41 $this->mTested = new ApiOptions( $main, 'options' );
42
43 global $wgHooks;
44 if ( !isset( $wgHooks['GetPreferences'] ) ) {
45 $wgHooks['GetPreferences'] = array();
46 }
47 $this->mOldGetPreferencesHooks = $wgHooks['GetPreferences'];
48 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
49 }
50
51 protected function tearDown() {
52 global $wgHooks;
53
54 if ( $this->mOldGetPreferencesHooks !== false ) {
55 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks;
56 $this->mOldGetPreferencesHooks = false;
57 }
58
59 parent::tearDown();
60 }
61
62 public function hookGetPreferences( $user, &$preferences ) {
63 $preferences = array();
64
65 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
66 $preferences[$k] = array(
67 'type' => 'text',
68 'section' => 'test',
69 'label' => '&#160;',
70 );
71 }
72
73 $preferences['testmultiselect'] = array(
74 'type' => 'multiselect',
75 'options' => array(
76 'Test' => array(
77 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
78 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
79 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
80 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
81 ),
82 ),
83 'section' => 'test',
84 'label' => '&#160;',
85 'prefix' => 'testmultiselect-',
86 'default' => array(),
87 );
88
89 return true;
90 }
91
92 public function getOptionKinds( IContextSource $context, $options = null ) {
93 // Match with above.
94 $kinds = array(
95 'name' => 'registered',
96 'willBeNull' => 'registered',
97 'willBeEmpty' => 'registered',
98 'willBeHappy' => 'registered',
99 'testmultiselect-opt1' => 'registered-multiselect',
100 'testmultiselect-opt2' => 'registered-multiselect',
101 'testmultiselect-opt3' => 'registered-multiselect',
102 'testmultiselect-opt4' => 'registered-multiselect',
103 );
104
105 if ( $options === null ) {
106 return $kinds;
107 }
108
109 $mapping = array();
110 foreach ( $options as $key => $value ) {
111 if ( isset( $kinds[$key] ) ) {
112 $mapping[$key] = $kinds[$key];
113 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
114 $mapping[$key] = 'userjs';
115 } else {
116 $mapping[$key] = 'unused';
117 }
118 }
119
120 return $mapping;
121 }
122
123 private function getSampleRequest( $custom = array() ) {
124 $request = array(
125 'token' => '123ABC',
126 'change' => null,
127 'optionname' => null,
128 'optionvalue' => null,
129 );
130
131 return array_merge( $request, $custom );
132 }
133
134 private function executeQuery( $request ) {
135 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
136 $this->mTested->execute();
137
138 return $this->mTested->getResult()->getData();
139 }
140
141 /**
142 * @expectedException UsageException
143 */
144 public function testNoToken() {
145 $request = $this->getSampleRequest( array( 'token' => null ) );
146
147 $this->executeQuery( $request );
148 }
149
150 public function testAnon() {
151 $this->mUserMock->expects( $this->once() )
152 ->method( 'isAnon' )
153 ->will( $this->returnValue( true ) );
154
155 try {
156 $request = $this->getSampleRequest();
157
158 $this->executeQuery( $request );
159 } catch ( UsageException $e ) {
160 $this->assertEquals( 'notloggedin', $e->getCodeString() );
161 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
162
163 return;
164 }
165 $this->fail( "UsageException was not thrown" );
166 }
167
168 public function testNoOptionname() {
169 try {
170 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
171
172 $this->executeQuery( $request );
173 } catch ( UsageException $e ) {
174 $this->assertEquals( 'nooptionname', $e->getCodeString() );
175 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
176
177 return;
178 }
179 $this->fail( "UsageException was not thrown" );
180 }
181
182 public function testNoChanges() {
183 $this->mUserMock->expects( $this->never() )
184 ->method( 'resetOptions' );
185
186 $this->mUserMock->expects( $this->never() )
187 ->method( 'setOption' );
188
189 $this->mUserMock->expects( $this->never() )
190 ->method( 'saveSettings' );
191
192 try {
193 $request = $this->getSampleRequest();
194
195 $this->executeQuery( $request );
196 } catch ( UsageException $e ) {
197 $this->assertEquals( 'nochanges', $e->getCodeString() );
198 $this->assertEquals( 'No changes were requested', $e->getMessage() );
199
200 return;
201 }
202 $this->fail( "UsageException was not thrown" );
203 }
204
205 public function testReset() {
206 $this->mUserMock->expects( $this->once() )
207 ->method( 'resetOptions' )
208 ->with( $this->equalTo( array( 'all' ) ) );
209
210 $this->mUserMock->expects( $this->never() )
211 ->method( 'setOption' );
212
213 $this->mUserMock->expects( $this->once() )
214 ->method( 'saveSettings' );
215
216 $request = $this->getSampleRequest( array( 'reset' => '' ) );
217
218 $response = $this->executeQuery( $request );
219
220 $this->assertEquals( self::$Success, $response );
221 }
222
223 public function testResetKinds() {
224 $this->mUserMock->expects( $this->once() )
225 ->method( 'resetOptions' )
226 ->with( $this->equalTo( array( 'registered' ) ) );
227
228 $this->mUserMock->expects( $this->never() )
229 ->method( 'setOption' );
230
231 $this->mUserMock->expects( $this->once() )
232 ->method( 'saveSettings' );
233
234 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
235
236 $response = $this->executeQuery( $request );
237
238 $this->assertEquals( self::$Success, $response );
239 }
240
241 public function testOptionWithValue() {
242 $this->mUserMock->expects( $this->never() )
243 ->method( 'resetOptions' );
244
245 $this->mUserMock->expects( $this->once() )
246 ->method( 'setOption' )
247 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
248
249 $this->mUserMock->expects( $this->once() )
250 ->method( 'saveSettings' );
251
252 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
253
254 $response = $this->executeQuery( $request );
255
256 $this->assertEquals( self::$Success, $response );
257 }
258
259 public function testOptionResetValue() {
260 $this->mUserMock->expects( $this->never() )
261 ->method( 'resetOptions' );
262
263 $this->mUserMock->expects( $this->once() )
264 ->method( 'setOption' )
265 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
266
267 $this->mUserMock->expects( $this->once() )
268 ->method( 'saveSettings' );
269
270 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
271 $response = $this->executeQuery( $request );
272
273 $this->assertEquals( self::$Success, $response );
274 }
275
276 public function testChange() {
277 $this->mUserMock->expects( $this->never() )
278 ->method( 'resetOptions' );
279
280 $this->mUserMock->expects( $this->at( 2 ) )
281 ->method( 'getOptions' );
282
283 $this->mUserMock->expects( $this->at( 3 ) )
284 ->method( 'setOption' )
285 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
286
287 $this->mUserMock->expects( $this->at( 4 ) )
288 ->method( 'getOptions' );
289
290 $this->mUserMock->expects( $this->at( 5 ) )
291 ->method( 'setOption' )
292 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
293
294 $this->mUserMock->expects( $this->at( 6 ) )
295 ->method( 'getOptions' );
296
297 $this->mUserMock->expects( $this->at( 7 ) )
298 ->method( 'setOption' )
299 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
300
301 $this->mUserMock->expects( $this->once() )
302 ->method( 'saveSettings' );
303
304 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
305
306 $response = $this->executeQuery( $request );
307
308 $this->assertEquals( self::$Success, $response );
309 }
310
311 public function testResetChangeOption() {
312 $this->mUserMock->expects( $this->once() )
313 ->method( 'resetOptions' );
314
315 $this->mUserMock->expects( $this->at( 3 ) )
316 ->method( 'getOptions' );
317
318 $this->mUserMock->expects( $this->at( 4 ) )
319 ->method( 'setOption' )
320 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
321
322 $this->mUserMock->expects( $this->at( 5 ) )
323 ->method( 'getOptions' );
324
325 $this->mUserMock->expects( $this->at( 6 ) )
326 ->method( 'setOption' )
327 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
328
329 $this->mUserMock->expects( $this->once() )
330 ->method( 'saveSettings' );
331
332 $args = array(
333 'reset' => '',
334 'change' => 'willBeHappy=Happy',
335 'optionname' => 'name',
336 'optionvalue' => 'value'
337 );
338
339 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
340
341 $this->assertEquals( self::$Success, $response );
342 }
343
344 public function testMultiSelect() {
345 $this->mUserMock->expects( $this->never() )
346 ->method( 'resetOptions' );
347
348 $this->mUserMock->expects( $this->at( 2 ) )
349 ->method( 'setOption' )
350 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
351
352 $this->mUserMock->expects( $this->at( 3 ) )
353 ->method( 'setOption' )
354 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
355
356 $this->mUserMock->expects( $this->at( 4 ) )
357 ->method( 'setOption' )
358 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
359
360 $this->mUserMock->expects( $this->at( 5 ) )
361 ->method( 'setOption' )
362 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
363
364 $this->mUserMock->expects( $this->once() )
365 ->method( 'saveSettings' );
366
367 $request = $this->getSampleRequest( array(
368 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
369 ) );
370
371 $response = $this->executeQuery( $request );
372
373 $this->assertEquals( self::$Success, $response );
374 }
375
376 public function testUnknownOption() {
377 $this->mUserMock->expects( $this->never() )
378 ->method( 'resetOptions' );
379
380 $this->mUserMock->expects( $this->never() )
381 ->method( 'saveSettings' );
382
383 $request = $this->getSampleRequest( array(
384 'change' => 'unknownOption=1'
385 ) );
386
387 $response = $this->executeQuery( $request );
388
389 $this->assertEquals( array(
390 'options' => 'success',
391 'warnings' => array(
392 'options' => array(
393 '*' => "Validation error for 'unknownOption': not a valid preference"
394 )
395 )
396 ), $response );
397 }
398
399 public function testUserjsOption() {
400 $this->mUserMock->expects( $this->never() )
401 ->method( 'resetOptions' );
402
403 $this->mUserMock->expects( $this->at( 2 ) )
404 ->method( 'setOption' )
405 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
406
407 $this->mUserMock->expects( $this->once() )
408 ->method( 'saveSettings' );
409
410 $request = $this->getSampleRequest( array(
411 'change' => 'userjs-option=1'
412 ) );
413
414 $response = $this->executeQuery( $request );
415
416 $this->assertEquals( self::$Success, $response );
417 }
418 }