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