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