ApiOptions: fix resetting some preferences to default
[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 $this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [
65 'testradio' => 'option1',
66 ] );
67 // Workaround for static caching in User::getDefaultOptions()
68 $this->setContentLang( Language::factory( 'qqq' ) );
69 }
70
71 public function hookGetPreferences( $user, &$preferences ) {
72 $preferences = [];
73
74 foreach ( [ 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ] as $k ) {
75 $preferences[$k] = [
76 'type' => 'text',
77 'section' => 'test',
78 'label' => "\u{00A0}",
79 ];
80 }
81
82 $preferences['testmultiselect'] = [
83 'type' => 'multiselect',
84 'options' => [
85 'Test' => [
86 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
87 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
88 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
89 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
90 ],
91 ],
92 'section' => 'test',
93 'label' => "\u{00A0}",
94 'prefix' => 'testmultiselect-',
95 'default' => [],
96 ];
97
98 $preferences['testradio'] = [
99 'type' => 'radio',
100 'options' => [ 'Option 1' => 'option1', 'Option 2' => 'option2' ],
101 'section' => 'test',
102 ];
103 }
104
105 /**
106 * @param IContextSource $context
107 * @param array|null $options
108 *
109 * @return array
110 */
111 public function getOptionKinds( IContextSource $context, $options = null ) {
112 // Match with above.
113 $kinds = [
114 'name' => 'registered',
115 'willBeNull' => 'registered',
116 'willBeEmpty' => 'registered',
117 'willBeHappy' => 'registered',
118 'testradio' => 'registered',
119 'testmultiselect-opt1' => 'registered-multiselect',
120 'testmultiselect-opt2' => 'registered-multiselect',
121 'testmultiselect-opt3' => 'registered-multiselect',
122 'testmultiselect-opt4' => 'registered-multiselect',
123 'special' => 'special',
124 ];
125
126 if ( $options === null ) {
127 return $kinds;
128 }
129
130 $mapping = [];
131 foreach ( $options as $key => $value ) {
132 if ( isset( $kinds[$key] ) ) {
133 $mapping[$key] = $kinds[$key];
134 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
135 $mapping[$key] = 'userjs';
136 } else {
137 $mapping[$key] = 'unused';
138 }
139 }
140
141 return $mapping;
142 }
143
144 private function getSampleRequest( $custom = [] ) {
145 $request = [
146 'token' => '123ABC',
147 'change' => null,
148 'optionname' => null,
149 'optionvalue' => null,
150 ];
151
152 return array_merge( $request, $custom );
153 }
154
155 private function executeQuery( $request ) {
156 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
157 $this->mTested->execute();
158
159 return $this->mTested->getResult()->getResultData( null, [ 'Strip' => 'all' ] );
160 }
161
162 /**
163 * @expectedException ApiUsageException
164 */
165 public function testNoToken() {
166 $request = $this->getSampleRequest( [ 'token' => null ] );
167
168 $this->executeQuery( $request );
169 }
170
171 public function testAnon() {
172 $this->mUserMock->expects( $this->once() )
173 ->method( 'isAnon' )
174 ->will( $this->returnValue( true ) );
175
176 try {
177 $request = $this->getSampleRequest();
178
179 $this->executeQuery( $request );
180 } catch ( ApiUsageException $e ) {
181 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $e, 'notloggedin' ) );
182 return;
183 }
184 $this->fail( "ApiUsageException was not thrown" );
185 }
186
187 public function testNoOptionname() {
188 try {
189 $request = $this->getSampleRequest( [ 'optionvalue' => '1' ] );
190
191 $this->executeQuery( $request );
192 } catch ( ApiUsageException $e ) {
193 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $e, 'nooptionname' ) );
194 return;
195 }
196 $this->fail( "ApiUsageException was not thrown" );
197 }
198
199 public function testNoChanges() {
200 $this->mUserMock->expects( $this->never() )
201 ->method( 'resetOptions' );
202
203 $this->mUserMock->expects( $this->never() )
204 ->method( 'setOption' );
205
206 $this->mUserMock->expects( $this->never() )
207 ->method( 'saveSettings' );
208
209 try {
210 $request = $this->getSampleRequest();
211
212 $this->executeQuery( $request );
213 } catch ( ApiUsageException $e ) {
214 $this->assertTrue( ApiTestCase::apiExceptionHasCode( $e, 'nochanges' ) );
215 return;
216 }
217 $this->fail( "ApiUsageException was not thrown" );
218 }
219
220 public function testReset() {
221 $this->mUserMock->expects( $this->once() )
222 ->method( 'resetOptions' )
223 ->with( $this->equalTo( [ 'all' ] ) );
224
225 $this->mUserMock->expects( $this->never() )
226 ->method( 'setOption' );
227
228 $this->mUserMock->expects( $this->once() )
229 ->method( 'saveSettings' );
230
231 $request = $this->getSampleRequest( [ 'reset' => '' ] );
232
233 $response = $this->executeQuery( $request );
234
235 $this->assertEquals( self::$Success, $response );
236 }
237
238 public function testResetKinds() {
239 $this->mUserMock->expects( $this->once() )
240 ->method( 'resetOptions' )
241 ->with( $this->equalTo( [ 'registered' ] ) );
242
243 $this->mUserMock->expects( $this->never() )
244 ->method( 'setOption' );
245
246 $this->mUserMock->expects( $this->once() )
247 ->method( 'saveSettings' );
248
249 $request = $this->getSampleRequest( [ 'reset' => '', 'resetkinds' => 'registered' ] );
250
251 $response = $this->executeQuery( $request );
252
253 $this->assertEquals( self::$Success, $response );
254 }
255
256 public function testResetChangeOption() {
257 $this->mUserMock->expects( $this->once() )
258 ->method( 'resetOptions' );
259
260 $this->mUserMock->expects( $this->exactly( 2 ) )
261 ->method( 'setOption' )
262 ->withConsecutive(
263 [ $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) ],
264 [ $this->equalTo( 'name' ), $this->equalTo( 'value' ) ]
265 );
266
267 $this->mUserMock->expects( $this->once() )
268 ->method( 'saveSettings' );
269
270 $args = [
271 'reset' => '',
272 'change' => 'willBeHappy=Happy',
273 'optionname' => 'name',
274 'optionvalue' => 'value'
275 ];
276
277 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
278
279 $this->assertEquals( self::$Success, $response );
280 }
281
282 /**
283 * @dataProvider provideOptionManupulation
284 * @param array $params
285 * @param array $setOptions
286 * @param array|null $result
287 */
288 public function testOptionManupulation( array $params, array $setOptions, array $result = null,
289 $message = ''
290 ) {
291 $this->mUserMock->expects( $this->never() )
292 ->method( 'resetOptions' );
293
294 $this->mUserMock->expects( $this->exactly( count( $setOptions ) ) )
295 ->method( 'setOption' )
296 ->withConsecutive( ...$setOptions );
297
298 if ( $setOptions ) {
299 $this->mUserMock->expects( $this->once() )
300 ->method( 'saveSettings' );
301 } else {
302 $this->mUserMock->expects( $this->never() )
303 ->method( 'saveSettings' );
304 }
305
306 $request = $this->getSampleRequest( $params );
307 $response = $this->executeQuery( $request );
308
309 if ( !$result ) {
310 $result = self::$Success;
311 }
312 $this->assertEquals( $result, $response, $message );
313 }
314
315 public function provideOptionManupulation() {
316 return [
317 [
318 [ 'change' => 'userjs-option=1' ],
319 [ [ 'userjs-option', '1' ] ],
320 null,
321 'Setting userjs options',
322 ],
323 [
324 [ 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ],
325 [
326 [ 'willBeNull', null ],
327 [ 'willBeEmpty', '' ],
328 [ 'willBeHappy', 'Happy' ],
329 ],
330 null,
331 'Basic option setting',
332 ],
333 [
334 [ 'change' => 'testradio=option2' ],
335 [ [ 'testradio', 'option2' ] ],
336 null,
337 'Changing radio options',
338 ],
339 [
340 [ 'change' => 'testradio' ],
341 [ [ 'testradio', null ] ],
342 null,
343 'Resetting radio options',
344 ],
345 [
346 [ 'change' => 'unknownOption=1' ],
347 [],
348 [
349 'options' => 'success',
350 'warnings' => [
351 'options' => [
352 'warnings' => "Validation error for \"unknownOption\": not a valid preference."
353 ],
354 ],
355 ],
356 'Unrecognized options should be rejected',
357 ],
358 [
359 [ 'change' => 'special=1' ],
360 [],
361 [
362 'options' => 'success',
363 'warnings' => [
364 'options' => [
365 'warnings' => "Validation error for \"special\": cannot be set by this module."
366 ]
367 ]
368 ],
369 'Refuse setting special options',
370 ],
371 [
372 [
373 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|'
374 . 'testmultiselect-opt3=|testmultiselect-opt4=0'
375 ],
376 [
377 [ 'testmultiselect-opt1', true ],
378 [ 'testmultiselect-opt2', null ],
379 [ 'testmultiselect-opt3', false ],
380 [ 'testmultiselect-opt4', false ],
381 ],
382 null,
383 'Setting multiselect options',
384 ],
385 [
386 [ 'optionname' => 'name', 'optionvalue' => 'value' ],
387 [ [ 'name', 'value' ] ],
388 null,
389 'Setting options via optionname/optionvalue'
390 ],
391 [
392 [ 'optionname' => 'name' ],
393 [ [ 'name', null ] ],
394 null,
395 'Resetting options via optionname without optionvalue',
396 ],
397 ];
398 }
399 }