20 private static $Success = [
'options' =>
'success' ];
25 $this->mUserMock = $this->getMockBuilder(
User::class )
26 ->disableOriginalConstructor()
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 ) );
36 $this->mUserMock->expects( $this->
any() )
37 ->method(
'getOptionKinds' )->will( $this->returnCallback( [ $this,
'getOptionKinds' ] ) );
40 $this->mUserMock->expects( $this->
any() )
41 ->method(
'getInstanceForUpdate' )->will( $this->returnValue( $this->mUserMock ) );
46 $this->mContext->setUser( $this->mUserMock );
48 $main =
new ApiMain( $this->mContext );
53 $this->mTested =
new ApiOptions( $main,
'options' );
57 [ $this,
'hookGetPreferences' ]
65 foreach ( [
'name',
'willBeNull',
'willBeEmpty',
'willBeHappy' ]
as $k ) {
73 $preferences[
'testmultiselect'] = [
74 'type' =>
'multiselect',
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',
85 'prefix' =>
'testmultiselect-',
101 'name' =>
'registered',
102 'willBeNull' =>
'registered',
103 'willBeEmpty' =>
'registered',
104 'willBeHappy' =>
'registered',
105 'testmultiselect-opt1' =>
'registered-multiselect',
106 'testmultiselect-opt2' =>
'registered-multiselect',
107 'testmultiselect-opt3' =>
'registered-multiselect',
108 'testmultiselect-opt4' =>
'registered-multiselect',
109 'special' =>
'special',
118 if ( isset( $kinds[$key] ) ) {
119 $mapping[$key] = $kinds[$key];
120 } elseif ( substr( $key, 0, 7 ) ===
'userjs-' ) {
121 $mapping[$key] =
'userjs';
123 $mapping[$key] =
'unused';
134 'optionname' =>
null,
135 'optionvalue' =>
null,
138 return array_merge(
$request, $custom );
143 $this->mTested->execute();
145 return $this->mTested->getResult()->getResultData(
null, [
'Strip' =>
'all' ] );
158 $this->mUserMock->expects( $this->once() )
160 ->will( $this->returnValue(
true ) );
170 $this->fail(
"ApiUsageException was not thrown" );
182 $this->fail(
"ApiUsageException was not thrown" );
186 $this->mUserMock->expects( $this->never() )
187 ->method(
'resetOptions' );
189 $this->mUserMock->expects( $this->never() )
190 ->method(
'setOption' );
192 $this->mUserMock->expects( $this->never() )
193 ->method(
'saveSettings' );
203 $this->fail(
"ApiUsageException was not thrown" );
207 $this->mUserMock->expects( $this->once() )
208 ->method(
'resetOptions' )
209 ->with( $this->equalTo( [
'all' ] ) );
211 $this->mUserMock->expects( $this->never() )
212 ->method(
'setOption' );
214 $this->mUserMock->expects( $this->once() )
215 ->method(
'saveSettings' );
221 $this->assertEquals( self::$Success,
$response );
225 $this->mUserMock->expects( $this->once() )
226 ->method(
'resetOptions' )
227 ->with( $this->equalTo( [
'registered' ] ) );
229 $this->mUserMock->expects( $this->never() )
230 ->method(
'setOption' );
232 $this->mUserMock->expects( $this->once() )
233 ->method(
'saveSettings' );
239 $this->assertEquals( self::$Success,
$response );
243 $this->mUserMock->expects( $this->never() )
244 ->method(
'resetOptions' );
246 $this->mUserMock->expects( $this->once() )
247 ->method(
'setOption' )
248 ->with( $this->equalTo(
'name' ), $this->equalTo(
'value' ) );
250 $this->mUserMock->expects( $this->once() )
251 ->method(
'saveSettings' );
257 $this->assertEquals( self::$Success,
$response );
261 $this->mUserMock->expects( $this->never() )
262 ->method(
'resetOptions' );
264 $this->mUserMock->expects( $this->once() )
265 ->method(
'setOption' )
266 ->with( $this->equalTo(
'name' ), $this->identicalTo(
null ) );
268 $this->mUserMock->expects( $this->once() )
269 ->method(
'saveSettings' );
274 $this->assertEquals( self::$Success,
$response );
278 $this->mUserMock->expects( $this->never() )
279 ->method(
'resetOptions' );
281 $this->mUserMock->expects( $this->exactly( 3 ) )
282 ->method(
'setOption' )
284 [ $this->equalTo(
'willBeNull' ), $this->identicalTo(
null ) ],
285 [ $this->equalTo(
'willBeEmpty' ), $this->equalTo(
'' ) ],
286 [ $this->equalTo(
'willBeHappy' ), $this->equalTo(
'Happy' ) ]
289 $this->mUserMock->expects( $this->once() )
290 ->method(
'saveSettings' );
293 'change' =>
'willBeNull|willBeEmpty=|willBeHappy=Happy'
298 $this->assertEquals( self::$Success,
$response );
302 $this->mUserMock->expects( $this->once() )
303 ->method(
'resetOptions' );
305 $this->mUserMock->expects( $this->exactly( 2 ) )
306 ->method(
'setOption' )
308 [ $this->equalTo(
'willBeHappy' ), $this->equalTo(
'Happy' ) ],
309 [ $this->equalTo(
'name' ), $this->equalTo(
'value' ) ]
312 $this->mUserMock->expects( $this->once() )
313 ->method(
'saveSettings' );
317 'change' =>
'willBeHappy=Happy',
318 'optionname' =>
'name',
319 'optionvalue' =>
'value'
324 $this->assertEquals( self::$Success,
$response );
328 $this->mUserMock->expects( $this->never() )
329 ->method(
'resetOptions' );
331 $this->mUserMock->expects( $this->exactly( 4 ) )
332 ->method(
'setOption' )
334 [ $this->equalTo(
'testmultiselect-opt1' ), $this->identicalTo(
true ) ],
335 [ $this->equalTo(
'testmultiselect-opt2' ), $this->identicalTo(
null ) ],
336 [ $this->equalTo(
'testmultiselect-opt3' ), $this->identicalTo(
false ) ],
337 [ $this->equalTo(
'testmultiselect-opt4' ), $this->identicalTo(
false ) ]
340 $this->mUserMock->expects( $this->once() )
341 ->method(
'saveSettings' );
344 'change' =>
'testmultiselect-opt1=1|testmultiselect-opt2|'
345 .
'testmultiselect-opt3=|testmultiselect-opt4=0'
350 $this->assertEquals( self::$Success,
$response );
354 $this->mUserMock->expects( $this->never() )
355 ->method(
'resetOptions' );
357 $this->mUserMock->expects( $this->never() )
358 ->method(
'saveSettings' );
361 'change' =>
'special=1'
366 $this->assertEquals( [
367 'options' =>
'success',
370 'warnings' =>
"Validation error for \"special\": cannot be set by this module."
377 $this->mUserMock->expects( $this->never() )
378 ->method(
'resetOptions' );
380 $this->mUserMock->expects( $this->never() )
381 ->method(
'saveSettings' );
384 'change' =>
'unknownOption=1'
389 $this->assertEquals( [
390 'options' =>
'success',
393 'warnings' =>
"Validation error for \"unknownOption\": not a valid preference."
400 $this->mUserMock->expects( $this->never() )
401 ->method(
'resetOptions' );
403 $this->mUserMock->expects( $this->once() )
404 ->method(
'setOption' )
405 ->with( $this->equalTo(
'userjs-option' ), $this->equalTo(
'1' ) );
407 $this->mUserMock->expects( $this->once() )
408 ->method(
'saveSettings' );
411 'change' =>
'userjs-option=1'
416 $this->assertEquals( self::$Success,
$response );