8 class ApiOptionsTest
extends MediaWikiLangTestCase
{
10 private $mTested, $mUserMock, $mContext, $mSession;
12 private $mOldGetPreferencesHooks = false;
14 private static $Success = array( 'options' => 'success' );
16 protected function setUp() {
19 $this->mUserMock
= $this->getMockBuilder( 'User' )
20 ->disableOriginalConstructor()
24 $this->mUserMock
->expects( $this->any() )
25 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user')) );
27 // Create a new context
28 $this->mContext
= new DerivativeContext( new RequestContext() );
29 $this->mContext
->getContext()->setTitle( Title
::newFromText( 'Test' ) );
30 $this->mContext
->setUser( $this->mUserMock
);
32 $main = new ApiMain( $this->mContext
);
35 $this->mSession
= array();
37 $this->mTested
= new ApiOptions( $main, 'options' );
40 if ( !isset( $wgHooks['GetPreferences'] ) ) {
41 $wgHooks['GetPreferences'] = array();
43 $this->mOldGetPreferencesHooks
= $wgHooks['GetPreferences'];
44 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
47 protected function tearDown() {
50 if ( $this->mOldGetPreferencesHooks
!== false ) {
51 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks
;
52 $this->mOldGetPreferencesHooks
= false;
58 public function hookGetPreferences( $user, &$preferences ) {
59 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
60 $preferences[$k] = array(
67 $preferences['testmultiselect'] = array(
68 'type' => 'multiselect',
71 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
72 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
73 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
74 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
79 'prefix' => 'testmultiselect-',
86 private function getSampleRequest( $custom = array() ) {
91 'optionvalue' => null,
93 return array_merge( $request, $custom );
96 private function executeQuery( $request ) {
97 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
98 $this->mTested
->execute();
99 return $this->mTested
->getResult()->getData();
103 * @expectedException UsageException
105 public function testNoToken() {
106 $request = $this->getSampleRequest( array( 'token' => null ) );
108 $this->executeQuery( $request );
111 public function testAnon() {
112 $this->mUserMock
->expects( $this->once() )
114 ->will( $this->returnValue( true ) );
117 $request = $this->getSampleRequest();
119 $this->executeQuery( $request );
120 } catch ( UsageException
$e ) {
121 $this->assertEquals( 'notloggedin', $e->getCodeString() );
122 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
125 $this->fail( "UsageException was not thrown" );
128 public function testNoOptionname() {
130 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
132 $this->executeQuery( $request );
133 } catch ( UsageException
$e ) {
134 $this->assertEquals( 'nooptionname', $e->getCodeString() );
135 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
138 $this->fail( "UsageException was not thrown" );
141 public function testNoChanges() {
142 $this->mUserMock
->expects( $this->never() )
143 ->method( 'resetOptions' );
145 $this->mUserMock
->expects( $this->never() )
146 ->method( 'setOption' );
148 $this->mUserMock
->expects( $this->never() )
149 ->method( 'saveSettings' );
152 $request = $this->getSampleRequest();
154 $this->executeQuery( $request );
155 } catch ( UsageException
$e ) {
156 $this->assertEquals( 'nochanges', $e->getCodeString() );
157 $this->assertEquals( 'No changes were requested', $e->getMessage() );
160 $this->fail( "UsageException was not thrown" );
163 public function testReset() {
164 $this->mUserMock
->expects( $this->once() )
165 ->method( 'resetOptions' );
167 $this->mUserMock
->expects( $this->never() )
168 ->method( 'setOption' );
170 $this->mUserMock
->expects( $this->once() )
171 ->method( 'saveSettings' );
173 $request = $this->getSampleRequest( array( 'reset' => '' ) );
175 $response = $this->executeQuery( $request );
177 $this->assertEquals( self
::$Success, $response );
180 public function testOptionWithValue() {
181 $this->mUserMock
->expects( $this->never() )
182 ->method( 'resetOptions' );
184 $this->mUserMock
->expects( $this->once() )
185 ->method( 'setOption' )
186 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
188 $this->mUserMock
->expects( $this->once() )
189 ->method( 'saveSettings' );
191 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
193 $response = $this->executeQuery( $request );
195 $this->assertEquals( self
::$Success, $response );
198 public function testOptionResetValue() {
199 $this->mUserMock
->expects( $this->never() )
200 ->method( 'resetOptions' );
202 $this->mUserMock
->expects( $this->once() )
203 ->method( 'setOption' )
204 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
206 $this->mUserMock
->expects( $this->once() )
207 ->method( 'saveSettings' );
209 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
210 $response = $this->executeQuery( $request );
212 $this->assertEquals( self
::$Success, $response );
215 public function testChange() {
216 $this->mUserMock
->expects( $this->never() )
217 ->method( 'resetOptions' );
219 $this->mUserMock
->expects( $this->at( 1 ) )
220 ->method( 'getOptions' );
222 $this->mUserMock
->expects( $this->at( 2 ) )
223 ->method( 'setOption' )
224 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
226 $this->mUserMock
->expects( $this->at( 3 ) )
227 ->method( 'getOptions' );
229 $this->mUserMock
->expects( $this->at( 4 ) )
230 ->method( 'setOption' )
231 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
233 $this->mUserMock
->expects( $this->at( 5 ) )
234 ->method( 'getOptions' );
236 $this->mUserMock
->expects( $this->at( 6 ) )
237 ->method( 'setOption' )
238 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
240 $this->mUserMock
->expects( $this->once() )
241 ->method( 'saveSettings' );
243 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
245 $response = $this->executeQuery( $request );
247 $this->assertEquals( self
::$Success, $response );
250 public function testResetChangeOption() {
251 $this->mUserMock
->expects( $this->once() )
252 ->method( 'resetOptions' );
254 $this->mUserMock
->expects( $this->at( 2 ) )
255 ->method( 'getOptions' );
257 $this->mUserMock
->expects( $this->at( 3 ) )
258 ->method( 'setOption' )
259 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
261 $this->mUserMock
->expects( $this->at( 4 ) )
262 ->method( 'getOptions' );
264 $this->mUserMock
->expects( $this->at( 5 ) )
265 ->method( 'setOption' )
266 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
268 $this->mUserMock
->expects( $this->once() )
269 ->method( 'saveSettings' );
273 'change' => 'willBeHappy=Happy',
274 'optionname' => 'name',
275 'optionvalue' => 'value'
278 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
280 $this->assertEquals( self
::$Success, $response );
283 public function testMultiSelect() {
284 $this->mUserMock
->expects( $this->never() )
285 ->method( 'resetOptions' );
287 $this->mUserMock
->expects( $this->at( 1 ) )
288 ->method( 'setOption' )
289 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->equalTo( true ) );
291 $this->mUserMock
->expects( $this->at( 2 ) )
292 ->method( 'setOption' )
293 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->equalTo( false ) );
295 $this->mUserMock
->expects( $this->at( 3 ) )
296 ->method( 'setOption' )
297 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->equalTo( false ) );
299 $this->mUserMock
->expects( $this->at( 4 ) )
300 ->method( 'setOption' )
301 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->equalTo( false ) );
303 $this->mUserMock
->expects( $this->once() )
304 ->method( 'saveSettings' );
306 $request = $this->getSampleRequest( array(
307 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
310 $response = $this->executeQuery( $request );
312 $this->assertEquals( self
::$Success, $response );