(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 return $mapping;
120 }
121
122 private function getSampleRequest( $custom = array() ) {
123 $request = array(
124 'token' => '123ABC',
125 'change' => null,
126 'optionname' => null,
127 'optionvalue' => null,
128 );
129 return array_merge( $request, $custom );
130 }
131
132 private function executeQuery( $request ) {
133 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
134 $this->mTested->execute();
135 return $this->mTested->getResult()->getData();
136 }
137
138 /**
139 * @expectedException UsageException
140 */
141 public function testNoToken() {
142 $request = $this->getSampleRequest( array( 'token' => null ) );
143
144 $this->executeQuery( $request );
145 }
146
147 public function testAnon() {
148 $this->mUserMock->expects( $this->once() )
149 ->method( 'isAnon' )
150 ->will( $this->returnValue( true ) );
151
152 try {
153 $request = $this->getSampleRequest();
154
155 $this->executeQuery( $request );
156 } catch ( UsageException $e ) {
157 $this->assertEquals( 'notloggedin', $e->getCodeString() );
158 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
159 return;
160 }
161 $this->fail( "UsageException was not thrown" );
162 }
163
164 public function testNoOptionname() {
165 try {
166 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
167
168 $this->executeQuery( $request );
169 } catch ( UsageException $e ) {
170 $this->assertEquals( 'nooptionname', $e->getCodeString() );
171 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
172 return;
173 }
174 $this->fail( "UsageException was not thrown" );
175 }
176
177 public function testNoChanges() {
178 $this->mUserMock->expects( $this->never() )
179 ->method( 'resetOptions' );
180
181 $this->mUserMock->expects( $this->never() )
182 ->method( 'setOption' );
183
184 $this->mUserMock->expects( $this->never() )
185 ->method( 'saveSettings' );
186
187 try {
188 $request = $this->getSampleRequest();
189
190 $this->executeQuery( $request );
191 } catch ( UsageException $e ) {
192 $this->assertEquals( 'nochanges', $e->getCodeString() );
193 $this->assertEquals( 'No changes were requested', $e->getMessage() );
194 return;
195 }
196 $this->fail( "UsageException was not thrown" );
197 }
198
199 public function testReset() {
200 $this->mUserMock->expects( $this->once() )
201 ->method( 'resetOptions' )
202 ->with( $this->equalTo( array( 'all' ) ) );
203
204 $this->mUserMock->expects( $this->never() )
205 ->method( 'setOption' );
206
207 $this->mUserMock->expects( $this->once() )
208 ->method( 'saveSettings' );
209
210 $request = $this->getSampleRequest( array( 'reset' => '' ) );
211
212 $response = $this->executeQuery( $request );
213
214 $this->assertEquals( self::$Success, $response );
215 }
216
217 public function testResetKinds() {
218 $this->mUserMock->expects( $this->once() )
219 ->method( 'resetOptions' )
220 ->with( $this->equalTo( array( 'registered' ) ) );
221
222 $this->mUserMock->expects( $this->never() )
223 ->method( 'setOption' );
224
225 $this->mUserMock->expects( $this->once() )
226 ->method( 'saveSettings' );
227
228 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
229
230 $response = $this->executeQuery( $request );
231
232 $this->assertEquals( self::$Success, $response );
233 }
234
235 public function testOptionWithValue() {
236 $this->mUserMock->expects( $this->never() )
237 ->method( 'resetOptions' );
238
239 $this->mUserMock->expects( $this->once() )
240 ->method( 'setOption' )
241 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
242
243 $this->mUserMock->expects( $this->once() )
244 ->method( 'saveSettings' );
245
246 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
247
248 $response = $this->executeQuery( $request );
249
250 $this->assertEquals( self::$Success, $response );
251 }
252
253 public function testOptionResetValue() {
254 $this->mUserMock->expects( $this->never() )
255 ->method( 'resetOptions' );
256
257 $this->mUserMock->expects( $this->once() )
258 ->method( 'setOption' )
259 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
260
261 $this->mUserMock->expects( $this->once() )
262 ->method( 'saveSettings' );
263
264 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
265 $response = $this->executeQuery( $request );
266
267 $this->assertEquals( self::$Success, $response );
268 }
269
270 public function testChange() {
271 $this->mUserMock->expects( $this->never() )
272 ->method( 'resetOptions' );
273
274 $this->mUserMock->expects( $this->at( 2 ) )
275 ->method( 'getOptions' );
276
277 $this->mUserMock->expects( $this->at( 3 ) )
278 ->method( 'setOption' )
279 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
280
281 $this->mUserMock->expects( $this->at( 4 ) )
282 ->method( 'getOptions' );
283
284 $this->mUserMock->expects( $this->at( 5 ) )
285 ->method( 'setOption' )
286 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
287
288 $this->mUserMock->expects( $this->at( 6 ) )
289 ->method( 'getOptions' );
290
291 $this->mUserMock->expects( $this->at( 7 ) )
292 ->method( 'setOption' )
293 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
294
295 $this->mUserMock->expects( $this->once() )
296 ->method( 'saveSettings' );
297
298 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
299
300 $response = $this->executeQuery( $request );
301
302 $this->assertEquals( self::$Success, $response );
303 }
304
305 public function testResetChangeOption() {
306 $this->mUserMock->expects( $this->once() )
307 ->method( 'resetOptions' );
308
309 $this->mUserMock->expects( $this->at( 3 ) )
310 ->method( 'getOptions' );
311
312 $this->mUserMock->expects( $this->at( 4 ) )
313 ->method( 'setOption' )
314 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
315
316 $this->mUserMock->expects( $this->at( 5 ) )
317 ->method( 'getOptions' );
318
319 $this->mUserMock->expects( $this->at( 6 ) )
320 ->method( 'setOption' )
321 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
322
323 $this->mUserMock->expects( $this->once() )
324 ->method( 'saveSettings' );
325
326 $args = array(
327 'reset' => '',
328 'change' => 'willBeHappy=Happy',
329 'optionname' => 'name',
330 'optionvalue' => 'value'
331 );
332
333 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
334
335 $this->assertEquals( self::$Success, $response );
336 }
337
338 public function testMultiSelect() {
339 $this->mUserMock->expects( $this->never() )
340 ->method( 'resetOptions' );
341
342 $this->mUserMock->expects( $this->at( 2 ) )
343 ->method( 'setOption' )
344 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
345
346 $this->mUserMock->expects( $this->at( 3 ) )
347 ->method( 'setOption' )
348 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
349
350 $this->mUserMock->expects( $this->at( 4 ) )
351 ->method( 'setOption' )
352 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
353
354 $this->mUserMock->expects( $this->at( 5 ) )
355 ->method( 'setOption' )
356 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
357
358 $this->mUserMock->expects( $this->once() )
359 ->method( 'saveSettings' );
360
361 $request = $this->getSampleRequest( array(
362 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
363 ) );
364
365 $response = $this->executeQuery( $request );
366
367 $this->assertEquals( self::$Success, $response );
368 }
369
370 public function testUnknownOption() {
371 $this->mUserMock->expects( $this->never() )
372 ->method( 'resetOptions' );
373
374 $this->mUserMock->expects( $this->never() )
375 ->method( 'saveSettings' );
376
377 $request = $this->getSampleRequest( array(
378 'change' => 'unknownOption=1'
379 ) );
380
381 $response = $this->executeQuery( $request );
382
383 $this->assertEquals( array(
384 'options' => 'success',
385 'warnings' => array(
386 'options' => array(
387 '*' => "Validation error for 'unknownOption': not a valid preference"
388 )
389 )
390 ), $response );
391 }
392
393 public function testUserjsOption() {
394 $this->mUserMock->expects( $this->never() )
395 ->method( 'resetOptions' );
396
397 $this->mUserMock->expects( $this->at( 2 ) )
398 ->method( 'setOption' )
399 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
400
401 $this->mUserMock->expects( $this->once() )
402 ->method( 'saveSettings' );
403
404 $request = $this->getSampleRequest( array(
405 'change' => 'userjs-option=1'
406 ) );
407
408 $response = $this->executeQuery( $request );
409
410 $this->assertEquals( self::$Success, $response );
411 }
412 }