test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / api / ApiOptionsTest.php
blobae74e384a18c39d06a1567c639a88085ec37ffa4
1 <?php
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 */
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() {
17 parent::setUp();
19 $this->mUserMock = $this->getMockBuilder( 'User' )
20 ->disableOriginalConstructor()
21 ->getMock();
23 // Set up groups
24 $this->mUserMock->expects( $this->any() )
25 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user' ) ) );
27 // Set up callback for User::getOptionKinds
28 $this->mUserMock->expects( $this->any() )
29 ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) );
31 // Create a new context
32 $this->mContext = new DerivativeContext( new RequestContext() );
33 $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
34 $this->mContext->setUser( $this->mUserMock );
36 $main = new ApiMain( $this->mContext );
38 // Empty session
39 $this->mSession = array();
41 $this->mTested = new ApiOptions( $main, 'options' );
43 global $wgHooks;
44 if ( !isset( $wgHooks['GetPreferences'] ) ) {
45 $wgHooks['GetPreferences'] = array();
47 $this->mOldGetPreferencesHooks = $wgHooks['GetPreferences'];
48 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
51 protected function tearDown() {
52 global $wgHooks;
54 if ( $this->mOldGetPreferencesHooks !== false ) {
55 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks;
56 $this->mOldGetPreferencesHooks = false;
59 parent::tearDown();
62 public function hookGetPreferences( $user, &$preferences ) {
63 $preferences = array();
65 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
66 $preferences[$k] = array(
67 'type' => 'text',
68 'section' => 'test',
69 'label' => '&#160;',
73 $preferences['testmultiselect'] = array(
74 'type' => 'multiselect',
75 'options' => array(
76 'Test' => array(
77 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
78 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
79 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
80 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
83 'section' => 'test',
84 'label' => '&#160;',
85 'prefix' => 'testmultiselect-',
86 'default' => array(),
89 return true;
92 public function getOptionKinds( IContextSource $context, $options = null ) {
93 // Match with above.
94 $kinds = array(
95 'name' => 'registered',
96 'willBeNull' => 'registered',
97 'willBeEmpty' => 'registered',
98 'willBeHappy' => 'registered',
99 'testmultiselect-opt1' => 'registered-multiselect',
100 'testmultiselect-opt2' => 'registered-multiselect',
101 'testmultiselect-opt3' => 'registered-multiselect',
102 'testmultiselect-opt4' => 'registered-multiselect',
105 if ( $options === null ) {
106 return $kinds;
109 $mapping = array();
110 foreach ( $options as $key => $value ) {
111 if ( isset( $kinds[$key] ) ) {
112 $mapping[$key] = $kinds[$key];
113 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
114 $mapping[$key] = 'userjs';
115 } else {
116 $mapping[$key] = 'unused';
120 return $mapping;
123 private function getSampleRequest( $custom = array() ) {
124 $request = array(
125 'token' => '123ABC',
126 'change' => null,
127 'optionname' => null,
128 'optionvalue' => null,
131 return array_merge( $request, $custom );
134 private function executeQuery( $request ) {
135 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
136 $this->mTested->execute();
138 return $this->mTested->getResult()->getData();
142 * @expectedException UsageException
144 public function testNoToken() {
145 $request = $this->getSampleRequest( array( 'token' => null ) );
147 $this->executeQuery( $request );
150 public function testAnon() {
151 $this->mUserMock->expects( $this->once() )
152 ->method( 'isAnon' )
153 ->will( $this->returnValue( true ) );
155 try {
156 $request = $this->getSampleRequest();
158 $this->executeQuery( $request );
159 } catch ( UsageException $e ) {
160 $this->assertEquals( 'notloggedin', $e->getCodeString() );
161 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
163 return;
165 $this->fail( "UsageException was not thrown" );
168 public function testNoOptionname() {
169 try {
170 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
172 $this->executeQuery( $request );
173 } catch ( UsageException $e ) {
174 $this->assertEquals( 'nooptionname', $e->getCodeString() );
175 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
177 return;
179 $this->fail( "UsageException was not thrown" );
182 public function testNoChanges() {
183 $this->mUserMock->expects( $this->never() )
184 ->method( 'resetOptions' );
186 $this->mUserMock->expects( $this->never() )
187 ->method( 'setOption' );
189 $this->mUserMock->expects( $this->never() )
190 ->method( 'saveSettings' );
192 try {
193 $request = $this->getSampleRequest();
195 $this->executeQuery( $request );
196 } catch ( UsageException $e ) {
197 $this->assertEquals( 'nochanges', $e->getCodeString() );
198 $this->assertEquals( 'No changes were requested', $e->getMessage() );
200 return;
202 $this->fail( "UsageException was not thrown" );
205 public function testReset() {
206 $this->mUserMock->expects( $this->once() )
207 ->method( 'resetOptions' )
208 ->with( $this->equalTo( array( 'all' ) ) );
210 $this->mUserMock->expects( $this->never() )
211 ->method( 'setOption' );
213 $this->mUserMock->expects( $this->once() )
214 ->method( 'saveSettings' );
216 $request = $this->getSampleRequest( array( 'reset' => '' ) );
218 $response = $this->executeQuery( $request );
220 $this->assertEquals( self::$Success, $response );
223 public function testResetKinds() {
224 $this->mUserMock->expects( $this->once() )
225 ->method( 'resetOptions' )
226 ->with( $this->equalTo( array( 'registered' ) ) );
228 $this->mUserMock->expects( $this->never() )
229 ->method( 'setOption' );
231 $this->mUserMock->expects( $this->once() )
232 ->method( 'saveSettings' );
234 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
236 $response = $this->executeQuery( $request );
238 $this->assertEquals( self::$Success, $response );
241 public function testOptionWithValue() {
242 $this->mUserMock->expects( $this->never() )
243 ->method( 'resetOptions' );
245 $this->mUserMock->expects( $this->once() )
246 ->method( 'setOption' )
247 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
249 $this->mUserMock->expects( $this->once() )
250 ->method( 'saveSettings' );
252 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
254 $response = $this->executeQuery( $request );
256 $this->assertEquals( self::$Success, $response );
259 public function testOptionResetValue() {
260 $this->mUserMock->expects( $this->never() )
261 ->method( 'resetOptions' );
263 $this->mUserMock->expects( $this->once() )
264 ->method( 'setOption' )
265 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
267 $this->mUserMock->expects( $this->once() )
268 ->method( 'saveSettings' );
270 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
271 $response = $this->executeQuery( $request );
273 $this->assertEquals( self::$Success, $response );
276 public function testChange() {
277 $this->mUserMock->expects( $this->never() )
278 ->method( 'resetOptions' );
280 $this->mUserMock->expects( $this->at( 2 ) )
281 ->method( 'getOptions' );
283 $this->mUserMock->expects( $this->at( 3 ) )
284 ->method( 'setOption' )
285 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
287 $this->mUserMock->expects( $this->at( 4 ) )
288 ->method( 'getOptions' );
290 $this->mUserMock->expects( $this->at( 5 ) )
291 ->method( 'setOption' )
292 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
294 $this->mUserMock->expects( $this->at( 6 ) )
295 ->method( 'getOptions' );
297 $this->mUserMock->expects( $this->at( 7 ) )
298 ->method( 'setOption' )
299 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
301 $this->mUserMock->expects( $this->once() )
302 ->method( 'saveSettings' );
304 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
306 $response = $this->executeQuery( $request );
308 $this->assertEquals( self::$Success, $response );
311 public function testResetChangeOption() {
312 $this->mUserMock->expects( $this->once() )
313 ->method( 'resetOptions' );
315 $this->mUserMock->expects( $this->at( 3 ) )
316 ->method( 'getOptions' );
318 $this->mUserMock->expects( $this->at( 4 ) )
319 ->method( 'setOption' )
320 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
322 $this->mUserMock->expects( $this->at( 5 ) )
323 ->method( 'getOptions' );
325 $this->mUserMock->expects( $this->at( 6 ) )
326 ->method( 'setOption' )
327 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
329 $this->mUserMock->expects( $this->once() )
330 ->method( 'saveSettings' );
332 $args = array(
333 'reset' => '',
334 'change' => 'willBeHappy=Happy',
335 'optionname' => 'name',
336 'optionvalue' => 'value'
339 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
341 $this->assertEquals( self::$Success, $response );
344 public function testMultiSelect() {
345 $this->mUserMock->expects( $this->never() )
346 ->method( 'resetOptions' );
348 $this->mUserMock->expects( $this->at( 2 ) )
349 ->method( 'setOption' )
350 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
352 $this->mUserMock->expects( $this->at( 3 ) )
353 ->method( 'setOption' )
354 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
356 $this->mUserMock->expects( $this->at( 4 ) )
357 ->method( 'setOption' )
358 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
360 $this->mUserMock->expects( $this->at( 5 ) )
361 ->method( 'setOption' )
362 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
364 $this->mUserMock->expects( $this->once() )
365 ->method( 'saveSettings' );
367 $request = $this->getSampleRequest( array(
368 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
369 ) );
371 $response = $this->executeQuery( $request );
373 $this->assertEquals( self::$Success, $response );
376 public function testUnknownOption() {
377 $this->mUserMock->expects( $this->never() )
378 ->method( 'resetOptions' );
380 $this->mUserMock->expects( $this->never() )
381 ->method( 'saveSettings' );
383 $request = $this->getSampleRequest( array(
384 'change' => 'unknownOption=1'
385 ) );
387 $response = $this->executeQuery( $request );
389 $this->assertEquals( array(
390 'options' => 'success',
391 'warnings' => array(
392 'options' => array(
393 '*' => "Validation error for 'unknownOption': not a valid preference"
396 ), $response );
399 public function testUserjsOption() {
400 $this->mUserMock->expects( $this->never() )
401 ->method( 'resetOptions' );
403 $this->mUserMock->expects( $this->at( 2 ) )
404 ->method( 'setOption' )
405 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
407 $this->mUserMock->expects( $this->once() )
408 ->method( 'saveSettings' );
410 $request = $this->getSampleRequest( array(
411 'change' => 'userjs-option=1'
412 ) );
414 $response = $this->executeQuery( $request );
416 $this->assertEquals( self::$Success, $response );