6 class ApiOptionsTest
extends MediaWikiLangTestCase
{
8 private $mTested, $mApiMainMock, $mUserMock, $mContext, $mSession;
10 private static $Success = array( 'options' => 'success' );
15 $this->mUserMock
= $this->getMockBuilder( 'User' )
16 ->disableOriginalConstructor()
19 $this->mApiMainMock
= $this->getMockBuilder( 'ApiBase' )
20 ->disableOriginalConstructor()
23 // Create a new context
24 $this->mContext
= new DerivativeContext( new RequestContext() );
25 $this->mContext
->setUser( $this->mUserMock
);
27 $this->mApiMainMock
->expects( $this->any() )
28 ->method( 'getContext' )
29 ->will( $this->returnValue( $this->mContext
) );
31 $this->mApiMainMock
->expects( $this->any() )
32 ->method( 'getResult' )
33 ->will( $this->returnValue( new ApiResult( $this->mApiMainMock
) ) );
37 $this->mSession
= array();
39 $this->mTested
= new ApiOptions( $this->mApiMainMock
, 'options' );
42 private function getSampleRequest( $custom = array() ) {
47 'optionvalue' => null,
49 return array_merge( $request, $custom );
52 private function executeQuery( $request ) {
53 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
54 $this->mTested
->execute();
55 return $this->mTested
->getResult()->getData();
59 * @expectedException UsageException
61 public function testNoToken() {
62 $request = $this->getSampleRequest( array( 'token' => null ) );
64 $this->executeQuery( $request );
67 public function testAnon() {
68 $this->mUserMock
->expects( $this->once() )
70 ->will( $this->returnValue( true ) );
73 $request = $this->getSampleRequest();
75 $this->executeQuery( $request );
76 } catch ( UsageException
$e ) {
77 $this->assertEquals( 'notloggedin', $e->getCodeString() );
78 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
81 $this->fail( "UsageException was not thrown" );
84 public function testNoOptionname() {
86 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
88 $this->executeQuery( $request );
89 } catch ( UsageException
$e ) {
90 $this->assertEquals( 'nooptionname', $e->getCodeString() );
91 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
94 $this->fail( "UsageException was not thrown" );
97 public function testNoChanges() {
98 $this->mUserMock
->expects( $this->never() )
99 ->method( 'resetOptions' );
101 $this->mUserMock
->expects( $this->never() )
102 ->method( 'setOption' );
104 $this->mUserMock
->expects( $this->never() )
105 ->method( 'saveSettings' );
108 $request = $this->getSampleRequest();
110 $this->executeQuery( $request );
111 } catch ( UsageException
$e ) {
112 $this->assertEquals( 'nochanges', $e->getCodeString() );
113 $this->assertEquals( 'No changes were requested', $e->getMessage() );
116 $this->fail( "UsageException was not thrown" );
119 public function testReset() {
120 $this->mUserMock
->expects( $this->once() )
121 ->method( 'resetOptions' );
123 $this->mUserMock
->expects( $this->never() )
124 ->method( 'setOption' );
126 $this->mUserMock
->expects( $this->once() )
127 ->method( 'saveSettings' );
129 $request = $this->getSampleRequest( array( 'reset' => '' ) );
131 $response = $this->executeQuery( $request );
133 $this->assertEquals( self
::$Success, $response );
136 public function testOptionWithValue() {
137 $this->mUserMock
->expects( $this->never() )
138 ->method( 'resetOptions' );
140 $this->mUserMock
->expects( $this->once() )
141 ->method( 'setOption' )
142 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
144 $this->mUserMock
->expects( $this->once() )
145 ->method( 'saveSettings' );
147 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
149 $response = $this->executeQuery( $request );
151 $this->assertEquals( self
::$Success, $response );
154 public function testOptionResetValue() {
155 $this->mUserMock
->expects( $this->never() )
156 ->method( 'resetOptions' );
158 $this->mUserMock
->expects( $this->once() )
159 ->method( 'setOption' )
160 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
162 $this->mUserMock
->expects( $this->once() )
163 ->method( 'saveSettings' );
165 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
166 $response = $this->executeQuery( $request );
168 $this->assertEquals( self
::$Success, $response );
171 public function testChange() {
172 $this->mUserMock
->expects( $this->never() )
173 ->method( 'resetOptions' );
175 $this->mUserMock
->expects( $this->at( 1 ) )
176 ->method( 'setOption' )
177 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
179 $this->mUserMock
->expects( $this->at( 2 ) )
180 ->method( 'setOption' )
181 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
183 $this->mUserMock
->expects( $this->at( 3 ) )
184 ->method( 'setOption' )
185 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
187 $this->mUserMock
->expects( $this->once() )
188 ->method( 'saveSettings' );
190 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
192 $response = $this->executeQuery( $request );
194 $this->assertEquals( self
::$Success, $response );
197 public function testResetChangeOption() {
198 $this->mUserMock
->expects( $this->once() )
199 ->method( 'resetOptions' );
201 $this->mUserMock
->expects( $this->at( 2 ) )
202 ->method( 'setOption' )
203 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
205 $this->mUserMock
->expects( $this->at( 3 ) )
206 ->method( 'setOption' )
207 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
209 $this->mUserMock
->expects( $this->once() )
210 ->method( 'saveSettings' );
214 'change' => 'willBeHappy=Happy',
215 'optionname' => 'name',
216 'optionvalue' => 'value'
219 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
221 $this->assertEquals( self
::$Success, $response );