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