5469ccf3321f8923fe208ffe188f8712a98bed46
6 class ApiOptionsTest
extends MediaWikiLangTestCase
{
8 private $mTested, $mUserMock, $mContext, $mSession;
10 private static $Success = array( 'options' => 'success' );
12 protected function setUp() {
15 $this->mUserMock
= $this->getMockBuilder( 'User' )
16 ->disableOriginalConstructor()
19 // Create a new context
20 $this->mContext
= new DerivativeContext( new RequestContext() );
21 $this->mContext
->setUser( $this->mUserMock
);
23 $main = new ApiMain( $this->mContext
);
26 $this->mSession
= array();
28 $this->mTested
= new ApiOptions( $main, 'options' );
31 private function getSampleRequest( $custom = array() ) {
36 'optionvalue' => null,
38 return array_merge( $request, $custom );
41 private function executeQuery( $request ) {
42 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
43 $this->mTested
->execute();
44 return $this->mTested
->getResult()->getData();
48 * @expectedException UsageException
50 public function testNoToken() {
51 $request = $this->getSampleRequest( array( 'token' => null ) );
53 $this->executeQuery( $request );
56 public function testAnon() {
57 $this->mUserMock
->expects( $this->once() )
59 ->will( $this->returnValue( true ) );
62 $request = $this->getSampleRequest();
64 $this->executeQuery( $request );
65 } catch ( UsageException
$e ) {
66 $this->assertEquals( 'notloggedin', $e->getCodeString() );
67 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
70 $this->fail( "UsageException was not thrown" );
73 public function testNoOptionname() {
75 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
77 $this->executeQuery( $request );
78 } catch ( UsageException
$e ) {
79 $this->assertEquals( 'nooptionname', $e->getCodeString() );
80 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
83 $this->fail( "UsageException was not thrown" );
86 public function testNoChanges() {
87 $this->mUserMock
->expects( $this->never() )
88 ->method( 'resetOptions' );
90 $this->mUserMock
->expects( $this->never() )
91 ->method( 'setOption' );
93 $this->mUserMock
->expects( $this->never() )
94 ->method( 'saveSettings' );
97 $request = $this->getSampleRequest();
99 $this->executeQuery( $request );
100 } catch ( UsageException
$e ) {
101 $this->assertEquals( 'nochanges', $e->getCodeString() );
102 $this->assertEquals( 'No changes were requested', $e->getMessage() );
105 $this->fail( "UsageException was not thrown" );
111 public function testReset() {
112 $this->mUserMock
->expects( $this->once() )
113 ->method( 'resetOptions' );
115 $this->mUserMock
->expects( $this->never() )
116 ->method( 'setOption' );
118 $this->mUserMock
->expects( $this->once() )
119 ->method( 'saveSettings' );
121 $request = $this->getSampleRequest( array( 'reset' => '' ) );
123 $response = $this->executeQuery( $request );
125 $this->assertEquals( self
::$Success, $response );
131 public function testOptionWithValue() {
132 $this->mUserMock
->expects( $this->never() )
133 ->method( 'resetOptions' );
135 $this->mUserMock
->expects( $this->once() )
136 ->method( 'setOption' )
137 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
139 $this->mUserMock
->expects( $this->once() )
140 ->method( 'saveSettings' );
142 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
144 $response = $this->executeQuery( $request );
146 $this->assertEquals( self
::$Success, $response );
152 public function testOptionResetValue() {
153 $this->mUserMock
->expects( $this->never() )
154 ->method( 'resetOptions' );
156 $this->mUserMock
->expects( $this->once() )
157 ->method( 'setOption' )
158 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
160 $this->mUserMock
->expects( $this->once() )
161 ->method( 'saveSettings' );
163 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
164 $response = $this->executeQuery( $request );
166 $this->assertEquals( self
::$Success, $response );
172 public function testChange() {
173 $this->mUserMock
->expects( $this->never() )
174 ->method( 'resetOptions' );
176 $this->mUserMock
->expects( $this->at( 1 ) )
177 ->method( 'setOption' )
178 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
180 $this->mUserMock
->expects( $this->at( 2 ) )
181 ->method( 'setOption' )
182 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
184 $this->mUserMock
->expects( $this->at( 3 ) )
185 ->method( 'setOption' )
186 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
188 $this->mUserMock
->expects( $this->once() )
189 ->method( 'saveSettings' );
191 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
193 $response = $this->executeQuery( $request );
195 $this->assertEquals( self
::$Success, $response );
201 public function testResetChangeOption() {
202 $this->mUserMock
->expects( $this->once() )
203 ->method( 'resetOptions' );
205 $this->mUserMock
->expects( $this->at( 2 ) )
206 ->method( 'setOption' )
207 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
209 $this->mUserMock
->expects( $this->at( 3 ) )
210 ->method( 'setOption' )
211 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
213 $this->mUserMock
->expects( $this->once() )
214 ->method( 'saveSettings' );
218 'change' => 'willBeHappy=Happy',
219 'optionname' => 'name',
220 'optionvalue' => 'value'
223 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
225 $this->assertEquals( self
::$Success, $response );