6 class ApiOptionsTest
extends MediaWikiLangTestCase
{
8 private $mTested, $mUserMock, $mContext, $mSession;
10 private static $Success = array( 'options' => 'success' );
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" );
108 public function testReset() {
109 $this->mUserMock
->expects( $this->once() )
110 ->method( 'resetOptions' );
112 $this->mUserMock
->expects( $this->never() )
113 ->method( 'setOption' );
115 $this->mUserMock
->expects( $this->once() )
116 ->method( 'saveSettings' );
118 $request = $this->getSampleRequest( array( 'reset' => '' ) );
120 $response = $this->executeQuery( $request );
122 $this->assertEquals( self
::$Success, $response );
125 public function testOptionWithValue() {
126 $this->mUserMock
->expects( $this->never() )
127 ->method( 'resetOptions' );
129 $this->mUserMock
->expects( $this->once() )
130 ->method( 'setOption' )
131 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
133 $this->mUserMock
->expects( $this->once() )
134 ->method( 'saveSettings' );
136 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
138 $response = $this->executeQuery( $request );
140 $this->assertEquals( self
::$Success, $response );
143 public function testOptionResetValue() {
144 $this->mUserMock
->expects( $this->never() )
145 ->method( 'resetOptions' );
147 $this->mUserMock
->expects( $this->once() )
148 ->method( 'setOption' )
149 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
151 $this->mUserMock
->expects( $this->once() )
152 ->method( 'saveSettings' );
154 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
155 $response = $this->executeQuery( $request );
157 $this->assertEquals( self
::$Success, $response );
160 public function testChange() {
161 $this->mUserMock
->expects( $this->never() )
162 ->method( 'resetOptions' );
164 $this->mUserMock
->expects( $this->at( 1 ) )
165 ->method( 'setOption' )
166 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
168 $this->mUserMock
->expects( $this->at( 2 ) )
169 ->method( 'setOption' )
170 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
172 $this->mUserMock
->expects( $this->at( 3 ) )
173 ->method( 'setOption' )
174 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
176 $this->mUserMock
->expects( $this->once() )
177 ->method( 'saveSettings' );
179 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
181 $response = $this->executeQuery( $request );
183 $this->assertEquals( self
::$Success, $response );
186 public function testResetChangeOption() {
187 $this->mUserMock
->expects( $this->once() )
188 ->method( 'resetOptions' );
190 $this->mUserMock
->expects( $this->at( 2 ) )
191 ->method( 'setOption' )
192 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
194 $this->mUserMock
->expects( $this->at( 3 ) )
195 ->method( 'setOption' )
196 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
198 $this->mUserMock
->expects( $this->once() )
199 ->method( 'saveSettings' );
203 'change' => 'willBeHappy=Happy',
204 'optionname' => 'name',
205 'optionvalue' => 'value'
208 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
210 $this->assertEquals( self
::$Success, $response );