Remove superfluous re- from confirmemail_body_set
[mediawiki.git] / tests / phpunit / includes / api / ApiOptionsTest.php
blobad1e73ab4a3af63891a5093208ef42419bec8f57
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 and rights
24 $this->mUserMock->expects( $this->any() )
25 ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user' ) ) );
26 $this->mUserMock->expects( $this->any() )
27 ->method( 'isAllowed' )->will( $this->returnValue( true ) );
29 // Set up callback for User::getOptionKinds
30 $this->mUserMock->expects( $this->any() )
31 ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) );
33 // Create a new context
34 $this->mContext = new DerivativeContext( new RequestContext() );
35 $this->mContext->getContext()->setTitle( Title::newFromText( 'Test' ) );
36 $this->mContext->setUser( $this->mUserMock );
38 $main = new ApiMain( $this->mContext );
40 // Empty session
41 $this->mSession = array();
43 $this->mTested = new ApiOptions( $main, 'options' );
45 global $wgHooks;
46 if ( !isset( $wgHooks['GetPreferences'] ) ) {
47 $wgHooks['GetPreferences'] = array();
49 $this->mOldGetPreferencesHooks = $wgHooks['GetPreferences'];
50 $wgHooks['GetPreferences'][] = array( $this, 'hookGetPreferences' );
53 protected function tearDown() {
54 global $wgHooks;
56 if ( $this->mOldGetPreferencesHooks !== false ) {
57 $wgHooks['GetPreferences'] = $this->mOldGetPreferencesHooks;
58 $this->mOldGetPreferencesHooks = false;
61 parent::tearDown();
64 public function hookGetPreferences( $user, &$preferences ) {
65 $preferences = array();
67 foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) {
68 $preferences[$k] = array(
69 'type' => 'text',
70 'section' => 'test',
71 'label' => '&#160;',
75 $preferences['testmultiselect'] = array(
76 'type' => 'multiselect',
77 'options' => array(
78 'Test' => array(
79 '<span dir="auto">Some HTML here for option 1</span>' => 'opt1',
80 '<span dir="auto">Some HTML here for option 2</span>' => 'opt2',
81 '<span dir="auto">Some HTML here for option 3</span>' => 'opt3',
82 '<span dir="auto">Some HTML here for option 4</span>' => 'opt4',
85 'section' => 'test',
86 'label' => '&#160;',
87 'prefix' => 'testmultiselect-',
88 'default' => array(),
91 return true;
94 public function getOptionKinds( IContextSource $context, $options = null ) {
95 // Match with above.
96 $kinds = array(
97 'name' => 'registered',
98 'willBeNull' => 'registered',
99 'willBeEmpty' => 'registered',
100 'willBeHappy' => 'registered',
101 'testmultiselect-opt1' => 'registered-multiselect',
102 'testmultiselect-opt2' => 'registered-multiselect',
103 'testmultiselect-opt3' => 'registered-multiselect',
104 'testmultiselect-opt4' => 'registered-multiselect',
107 if ( $options === null ) {
108 return $kinds;
111 $mapping = array();
112 foreach ( $options as $key => $value ) {
113 if ( isset( $kinds[$key] ) ) {
114 $mapping[$key] = $kinds[$key];
115 } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
116 $mapping[$key] = 'userjs';
117 } else {
118 $mapping[$key] = 'unused';
122 return $mapping;
125 private function getSampleRequest( $custom = array() ) {
126 $request = array(
127 'token' => '123ABC',
128 'change' => null,
129 'optionname' => null,
130 'optionvalue' => null,
133 return array_merge( $request, $custom );
136 private function executeQuery( $request ) {
137 $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) );
138 $this->mTested->execute();
140 return $this->mTested->getResult()->getData();
144 * @expectedException UsageException
146 public function testNoToken() {
147 $request = $this->getSampleRequest( array( 'token' => null ) );
149 $this->executeQuery( $request );
152 public function testAnon() {
153 $this->mUserMock->expects( $this->once() )
154 ->method( 'isAnon' )
155 ->will( $this->returnValue( true ) );
157 try {
158 $request = $this->getSampleRequest();
160 $this->executeQuery( $request );
161 } catch ( UsageException $e ) {
162 $this->assertEquals( 'notloggedin', $e->getCodeString() );
163 $this->assertEquals( 'Anonymous users cannot change preferences', $e->getMessage() );
165 return;
167 $this->fail( "UsageException was not thrown" );
170 public function testNoOptionname() {
171 try {
172 $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) );
174 $this->executeQuery( $request );
175 } catch ( UsageException $e ) {
176 $this->assertEquals( 'nooptionname', $e->getCodeString() );
177 $this->assertEquals( 'The optionname parameter must be set', $e->getMessage() );
179 return;
181 $this->fail( "UsageException was not thrown" );
184 public function testNoChanges() {
185 $this->mUserMock->expects( $this->never() )
186 ->method( 'resetOptions' );
188 $this->mUserMock->expects( $this->never() )
189 ->method( 'setOption' );
191 $this->mUserMock->expects( $this->never() )
192 ->method( 'saveSettings' );
194 try {
195 $request = $this->getSampleRequest();
197 $this->executeQuery( $request );
198 } catch ( UsageException $e ) {
199 $this->assertEquals( 'nochanges', $e->getCodeString() );
200 $this->assertEquals( 'No changes were requested', $e->getMessage() );
202 return;
204 $this->fail( "UsageException was not thrown" );
207 public function testReset() {
208 $this->mUserMock->expects( $this->once() )
209 ->method( 'resetOptions' )
210 ->with( $this->equalTo( array( 'all' ) ) );
212 $this->mUserMock->expects( $this->never() )
213 ->method( 'setOption' );
215 $this->mUserMock->expects( $this->once() )
216 ->method( 'saveSettings' );
218 $request = $this->getSampleRequest( array( 'reset' => '' ) );
220 $response = $this->executeQuery( $request );
222 $this->assertEquals( self::$Success, $response );
225 public function testResetKinds() {
226 $this->mUserMock->expects( $this->once() )
227 ->method( 'resetOptions' )
228 ->with( $this->equalTo( array( 'registered' ) ) );
230 $this->mUserMock->expects( $this->never() )
231 ->method( 'setOption' );
233 $this->mUserMock->expects( $this->once() )
234 ->method( 'saveSettings' );
236 $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) );
238 $response = $this->executeQuery( $request );
240 $this->assertEquals( self::$Success, $response );
243 public function testOptionWithValue() {
244 $this->mUserMock->expects( $this->never() )
245 ->method( 'resetOptions' );
247 $this->mUserMock->expects( $this->once() )
248 ->method( 'setOption' )
249 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
251 $this->mUserMock->expects( $this->once() )
252 ->method( 'saveSettings' );
254 $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) );
256 $response = $this->executeQuery( $request );
258 $this->assertEquals( self::$Success, $response );
261 public function testOptionResetValue() {
262 $this->mUserMock->expects( $this->never() )
263 ->method( 'resetOptions' );
265 $this->mUserMock->expects( $this->once() )
266 ->method( 'setOption' )
267 ->with( $this->equalTo( 'name' ), $this->identicalTo( null ) );
269 $this->mUserMock->expects( $this->once() )
270 ->method( 'saveSettings' );
272 $request = $this->getSampleRequest( array( 'optionname' => 'name' ) );
273 $response = $this->executeQuery( $request );
275 $this->assertEquals( self::$Success, $response );
278 public function testChange() {
279 $this->mUserMock->expects( $this->never() )
280 ->method( 'resetOptions' );
282 $this->mUserMock->expects( $this->at( 2 ) )
283 ->method( 'getOptions' );
285 $this->mUserMock->expects( $this->at( 4 ) )
286 ->method( 'setOption' )
287 ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) );
289 $this->mUserMock->expects( $this->at( 5 ) )
290 ->method( 'getOptions' );
292 $this->mUserMock->expects( $this->at( 6 ) )
293 ->method( 'setOption' )
294 ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) );
296 $this->mUserMock->expects( $this->at( 7 ) )
297 ->method( 'getOptions' );
299 $this->mUserMock->expects( $this->at( 8 ) )
300 ->method( 'setOption' )
301 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
303 $this->mUserMock->expects( $this->once() )
304 ->method( 'saveSettings' );
306 $request = $this->getSampleRequest( array( 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' ) );
308 $response = $this->executeQuery( $request );
310 $this->assertEquals( self::$Success, $response );
313 public function testResetChangeOption() {
314 $this->mUserMock->expects( $this->once() )
315 ->method( 'resetOptions' );
317 $this->mUserMock->expects( $this->at( 4 ) )
318 ->method( 'getOptions' );
320 $this->mUserMock->expects( $this->at( 5 ) )
321 ->method( 'setOption' )
322 ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) );
324 $this->mUserMock->expects( $this->at( 6 ) )
325 ->method( 'getOptions' );
327 $this->mUserMock->expects( $this->at( 7 ) )
328 ->method( 'setOption' )
329 ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) );
331 $this->mUserMock->expects( $this->once() )
332 ->method( 'saveSettings' );
334 $args = array(
335 'reset' => '',
336 'change' => 'willBeHappy=Happy',
337 'optionname' => 'name',
338 'optionvalue' => 'value'
341 $response = $this->executeQuery( $this->getSampleRequest( $args ) );
343 $this->assertEquals( self::$Success, $response );
346 public function testMultiSelect() {
347 $this->mUserMock->expects( $this->never() )
348 ->method( 'resetOptions' );
350 $this->mUserMock->expects( $this->at( 3 ) )
351 ->method( 'setOption' )
352 ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) );
354 $this->mUserMock->expects( $this->at( 4 ) )
355 ->method( 'setOption' )
356 ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) );
358 $this->mUserMock->expects( $this->at( 5 ) )
359 ->method( 'setOption' )
360 ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) );
362 $this->mUserMock->expects( $this->at( 6 ) )
363 ->method( 'setOption' )
364 ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) );
366 $this->mUserMock->expects( $this->once() )
367 ->method( 'saveSettings' );
369 $request = $this->getSampleRequest( array(
370 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|testmultiselect-opt3=|testmultiselect-opt4=0'
371 ) );
373 $response = $this->executeQuery( $request );
375 $this->assertEquals( self::$Success, $response );
378 public function testUnknownOption() {
379 $this->mUserMock->expects( $this->never() )
380 ->method( 'resetOptions' );
382 $this->mUserMock->expects( $this->never() )
383 ->method( 'saveSettings' );
385 $request = $this->getSampleRequest( array(
386 'change' => 'unknownOption=1'
387 ) );
389 $response = $this->executeQuery( $request );
391 $this->assertEquals( array(
392 'options' => 'success',
393 'warnings' => array(
394 'options' => array(
395 '*' => "Validation error for 'unknownOption': not a valid preference"
398 ), $response );
401 public function testUserjsOption() {
402 $this->mUserMock->expects( $this->never() )
403 ->method( 'resetOptions' );
405 $this->mUserMock->expects( $this->at( 3 ) )
406 ->method( 'setOption' )
407 ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) );
409 $this->mUserMock->expects( $this->once() )
410 ->method( 'saveSettings' );
412 $request = $this->getSampleRequest( array(
413 'change' => 'userjs-option=1'
414 ) );
416 $response = $this->executeQuery( $request );
418 $this->assertEquals( self::$Success, $response );