ac18afe7aa8fc5526874af0c383b001ab210ad47
7 class ApiOptionsTest
extends MediaWikiLangTestCase
{
9 private $mTested, $mUserMock, $mContext, $mSession;
11 private $mOldGetPreferencesHooks = false;
13 private static $Success = array( 'options' => 'success' );
15 protected function setUp() {
18 $this->mUserMock
= $this->getMockBuilder( 'User' )
19 ->disableOriginalConstructor()
23 $this->mUserMock
->expects( $this->any() )
24 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user')) );
26 // Create a new context
27 $this->mContext
= new DerivativeContext( new RequestContext() );
28 $this->mContext
->getContext()->setTitle( Title
::newFromText( 'Test' ) );
29 $this->mContext
->setUser( $this->mUserMock
);
31 $main = new ApiMain( $this->mContext
);
34 $this->mSession
= array();
36 $this->mTested
= new ApiOptions( $main, 'options' );
39 if ( !isset( $wgHooks['GetPreferences'] ) ) {
40 $wgHooks['GetPreferences'] = array();
42 $this->mOldGetPreferencesHooks
= $wgHooks['GetPreferences'];
43 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
46 protected function tearDown() {
49 if ( $this->mOldGetPreferencesHooks
!== false ) {
50 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks
;
51 $this->mOldGetPreferencesHooks
= false;
57 public function hookGetPreferences( $user, &$preferences ) {
58 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
59 $preferences[$k] = array(
69 private function getSampleRequest( $custom = array() ) {
74 'optionvalue' => null,
76 return array_merge( $request, $custom );
79 private function executeQuery( $request ) {
80 $this->mContext
->setRequest( new FauxRequest( $request, true, $this->mSession
) );
81 $this->mTested
->execute();
82 return $this->mTested
->getResult()->getData();
86 * @expectedException UsageException
88 public function testNoToken() {
89 $request = $this->getSampleRequest( array( 'token' => null ) );
91 $this->executeQuery( $request );
94 public function testAnon() {
95 $this->mUserMock
->expects( $this->once() )
97 ->will( $this->returnValue( true ) );
100 $request = $this->getSampleRequest();
102 $this->executeQuery( $request );
103 } catch ( UsageException
$e ) {
104 $this->assertEquals( 'notloggedin', $e->getCodeString() );
105 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
108 $this->fail( "UsageException was not thrown" );
111 public function testNoOptionname() {
113 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
115 $this->executeQuery( $request );
116 } catch ( UsageException
$e ) {
117 $this->assertEquals( 'nooptionname', $e->getCodeString() );
118 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
121 $this->fail( "UsageException was not thrown" );
124 public function testNoChanges() {
125 $this->mUserMock
->expects( $this->never() )
126 ->method( 'resetOptions' );
128 $this->mUserMock
->expects( $this->never() )
129 ->method( 'setOption' );
131 $this->mUserMock
->expects( $this->never() )
132 ->method( 'saveSettings' );
135 $request = $this->getSampleRequest();
137 $this->executeQuery( $request );
138 } catch ( UsageException
$e ) {
139 $this->assertEquals( 'nochanges', $e->getCodeString() );
140 $this->assertEquals( 'No changes were requested', $e->getMessage() );
143 $this->fail( "UsageException was not thrown" );
146 public function testReset() {
147 $this->mUserMock
->expects( $this->once() )
148 ->method( 'resetOptions' );
150 $this->mUserMock
->expects( $this->never() )
151 ->method( 'setOption' );
153 $this->mUserMock
->expects( $this->once() )
154 ->method( 'saveSettings' );
156 $request = $this->getSampleRequest( array( 'reset' => '' ) );
158 $response = $this->executeQuery( $request );
160 $this->assertEquals( self
::$Success, $response );
163 public function testOptionWithValue() {
164 $this->mUserMock
->expects( $this->never() )
165 ->method( 'resetOptions' );
167 $this->mUserMock
->expects( $this->once() )
168 ->method( 'setOption' )
169 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
171 $this->mUserMock
->expects( $this->once() )
172 ->method( 'saveSettings' );
174 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
176 $response = $this->executeQuery( $request );
178 $this->assertEquals( self
::$Success, $response );
181 public function testOptionResetValue() {
182 $this->mUserMock
->expects( $this->never() )
183 ->method( 'resetOptions' );
185 $this->mUserMock
->expects( $this->once() )
186 ->method( 'setOption' )
187 ->with( $this->equalTo( 'name' ), $this->equalTo( null ) );
189 $this->mUserMock
->expects( $this->once() )
190 ->method( 'saveSettings' );
192 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
193 $response = $this->executeQuery( $request );
195 $this->assertEquals( self
::$Success, $response );
198 public function testChange() {
199 $this->mUserMock
->expects( $this->never() )
200 ->method( 'resetOptions' );
202 $this->mUserMock
->expects( $this->at( 1 ) )
203 ->method( 'getOptions' );
205 $this->mUserMock
->expects( $this->at( 2 ) )
206 ->method( 'setOption' )
207 ->with( $this->equalTo( 'willBeNull' ), $this->equalTo( null ) );
209 $this->mUserMock
->expects( $this->at( 3 ) )
210 ->method( 'getOptions' );
212 $this->mUserMock
->expects( $this->at( 4 ) )
213 ->method( 'setOption' )
214 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
216 $this->mUserMock
->expects( $this->at( 5 ) )
217 ->method( 'getOptions' );
219 $this->mUserMock
->expects( $this->at( 6 ) )
220 ->method( 'setOption' )
221 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
223 $this->mUserMock
->expects( $this->once() )
224 ->method( 'saveSettings' );
226 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
228 $response = $this->executeQuery( $request );
230 $this->assertEquals( self
::$Success, $response );
233 public function testResetChangeOption() {
234 $this->mUserMock
->expects( $this->once() )
235 ->method( 'resetOptions' );
237 $this->mUserMock
->expects( $this->at( 2 ) )
238 ->method( 'getOptions' );
240 $this->mUserMock
->expects( $this->at( 3 ) )
241 ->method( 'setOption' )
242 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
244 $this->mUserMock
->expects( $this->at( 4 ) )
245 ->method( 'getOptions' );
247 $this->mUserMock
->expects( $this->at( 5 ) )
248 ->method( 'setOption' )
249 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
251 $this->mUserMock
->expects( $this->once() )
252 ->method( 'saveSettings' );
256 'change' => 'willBeHappy=Happy',
257 'optionname' => 'name',
258 'optionvalue' => 'value'
261 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
263 $this->assertEquals( self
::$Success, $response );