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