X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fapi%2FApiOptionsTest.php;h=0a577c1cb6929fffadd8eca52d0215987e10402b;hb=bef9303bf6fd0cd5a33f58f28bd3bb4f27226f7e;hp=51154ae398219fa5aa45df05afd5a4678adc56c9;hpb=979681f48871c16efd485e78d78f98c9a094212b;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/api/ApiOptionsTest.php b/tests/phpunit/includes/api/ApiOptionsTest.php index 51154ae398..0a577c1cb6 100644 --- a/tests/phpunit/includes/api/ApiOptionsTest.php +++ b/tests/phpunit/includes/api/ApiOptionsTest.php @@ -17,7 +17,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase { /** @var DerivativeContext */ private $mContext; - private static $Success = array( 'options' => 'success' ); + private static $Success = [ 'options' => 'success' ]; protected function setUp() { parent::setUp(); @@ -28,13 +28,17 @@ class ApiOptionsTest extends MediaWikiLangTestCase { // Set up groups and rights $this->mUserMock->expects( $this->any() ) - ->method( 'getEffectiveGroups' )->will( $this->returnValue( array( '*', 'user' ) ) ); + ->method( 'getEffectiveGroups' )->will( $this->returnValue( [ '*', 'user' ] ) ); $this->mUserMock->expects( $this->any() ) ->method( 'isAllowed' )->will( $this->returnValue( true ) ); // Set up callback for User::getOptionKinds $this->mUserMock->expects( $this->any() ) - ->method( 'getOptionKinds' )->will( $this->returnCallback( array( $this, 'getOptionKinds' ) ) ); + ->method( 'getOptionKinds' )->will( $this->returnCallback( [ $this, 'getOptionKinds' ] ) ); + + // No actual DB data + $this->mUserMock->expects( $this->any() ) + ->method( 'getInstanceForUpdate' )->will( $this->returnValue( $this->mUserMock ) ); // Create a new context $this->mContext = new DerivativeContext( new RequestContext() ); @@ -44,43 +48,43 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $main = new ApiMain( $this->mContext ); // Empty session - $this->mSession = array(); + $this->mSession = []; $this->mTested = new ApiOptions( $main, 'options' ); - $this->mergeMwGlobalArrayValue( 'wgHooks', array( - 'GetPreferences' => array( - array( $this, 'hookGetPreferences' ) - ) - ) ); + $this->mergeMwGlobalArrayValue( 'wgHooks', [ + 'GetPreferences' => [ + [ $this, 'hookGetPreferences' ] + ] + ] ); } public function hookGetPreferences( $user, &$preferences ) { - $preferences = array(); + $preferences = []; - foreach ( array( 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ) as $k ) { - $preferences[$k] = array( + foreach ( [ 'name', 'willBeNull', 'willBeEmpty', 'willBeHappy' ] as $k ) { + $preferences[$k] = [ 'type' => 'text', 'section' => 'test', 'label' => ' ', - ); + ]; } - $preferences['testmultiselect'] = array( + $preferences['testmultiselect'] = [ 'type' => 'multiselect', - 'options' => array( - 'Test' => array( + 'options' => [ + 'Test' => [ 'Some HTML here for option 1' => 'opt1', 'Some HTML here for option 2' => 'opt2', 'Some HTML here for option 3' => 'opt3', 'Some HTML here for option 4' => 'opt4', - ), - ), + ], + ], 'section' => 'test', 'label' => ' ', 'prefix' => 'testmultiselect-', - 'default' => array(), - ); + 'default' => [], + ]; return true; } @@ -93,7 +97,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase { */ public function getOptionKinds( IContextSource $context, $options = null ) { // Match with above. - $kinds = array( + $kinds = [ 'name' => 'registered', 'willBeNull' => 'registered', 'willBeEmpty' => 'registered', @@ -103,13 +107,13 @@ class ApiOptionsTest extends MediaWikiLangTestCase { 'testmultiselect-opt3' => 'registered-multiselect', 'testmultiselect-opt4' => 'registered-multiselect', 'special' => 'special', - ); + ]; if ( $options === null ) { return $kinds; } - $mapping = array(); + $mapping = []; foreach ( $options as $key => $value ) { if ( isset( $kinds[$key] ) ) { $mapping[$key] = $kinds[$key]; @@ -123,13 +127,13 @@ class ApiOptionsTest extends MediaWikiLangTestCase { return $mapping; } - private function getSampleRequest( $custom = array() ) { - $request = array( + private function getSampleRequest( $custom = [] ) { + $request = [ 'token' => '123ABC', 'change' => null, 'optionname' => null, 'optionvalue' => null, - ); + ]; return array_merge( $request, $custom ); } @@ -138,14 +142,14 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mContext->setRequest( new FauxRequest( $request, true, $this->mSession ) ); $this->mTested->execute(); - return $this->mTested->getResult()->getResultData( null, array( 'Strip' => 'all' ) ); + return $this->mTested->getResult()->getResultData( null, [ 'Strip' => 'all' ] ); } /** * @expectedException UsageException */ public function testNoToken() { - $request = $this->getSampleRequest( array( 'token' => null ) ); + $request = $this->getSampleRequest( [ 'token' => null ] ); $this->executeQuery( $request ); } @@ -170,7 +174,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase { public function testNoOptionname() { try { - $request = $this->getSampleRequest( array( 'optionvalue' => '1' ) ); + $request = $this->getSampleRequest( [ 'optionvalue' => '1' ] ); $this->executeQuery( $request ); } catch ( UsageException $e ) { @@ -208,7 +212,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase { public function testReset() { $this->mUserMock->expects( $this->once() ) ->method( 'resetOptions' ) - ->with( $this->equalTo( array( 'all' ) ) ); + ->with( $this->equalTo( [ 'all' ] ) ); $this->mUserMock->expects( $this->never() ) ->method( 'setOption' ); @@ -216,7 +220,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mUserMock->expects( $this->once() ) ->method( 'saveSettings' ); - $request = $this->getSampleRequest( array( 'reset' => '' ) ); + $request = $this->getSampleRequest( [ 'reset' => '' ] ); $response = $this->executeQuery( $request ); @@ -226,7 +230,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase { public function testResetKinds() { $this->mUserMock->expects( $this->once() ) ->method( 'resetOptions' ) - ->with( $this->equalTo( array( 'registered' ) ) ); + ->with( $this->equalTo( [ 'registered' ] ) ); $this->mUserMock->expects( $this->never() ) ->method( 'setOption' ); @@ -234,7 +238,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mUserMock->expects( $this->once() ) ->method( 'saveSettings' ); - $request = $this->getSampleRequest( array( 'reset' => '', 'resetkinds' => 'registered' ) ); + $request = $this->getSampleRequest( [ 'reset' => '', 'resetkinds' => 'registered' ] ); $response = $this->executeQuery( $request ); @@ -252,7 +256,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mUserMock->expects( $this->once() ) ->method( 'saveSettings' ); - $request = $this->getSampleRequest( array( 'optionname' => 'name', 'optionvalue' => 'value' ) ); + $request = $this->getSampleRequest( [ 'optionname' => 'name', 'optionvalue' => 'value' ] ); $response = $this->executeQuery( $request ); @@ -270,7 +274,7 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mUserMock->expects( $this->once() ) ->method( 'saveSettings' ); - $request = $this->getSampleRequest( array( 'optionname' => 'name' ) ); + $request = $this->getSampleRequest( [ 'optionname' => 'name' ] ); $response = $this->executeQuery( $request ); $this->assertEquals( self::$Success, $response ); @@ -283,30 +287,30 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mUserMock->expects( $this->at( 2 ) ) ->method( 'getOptions' ); - $this->mUserMock->expects( $this->at( 4 ) ) + $this->mUserMock->expects( $this->at( 5 ) ) ->method( 'setOption' ) ->with( $this->equalTo( 'willBeNull' ), $this->identicalTo( null ) ); - $this->mUserMock->expects( $this->at( 5 ) ) + $this->mUserMock->expects( $this->at( 6 ) ) ->method( 'getOptions' ); - $this->mUserMock->expects( $this->at( 6 ) ) + $this->mUserMock->expects( $this->at( 7 ) ) ->method( 'setOption' ) ->with( $this->equalTo( 'willBeEmpty' ), $this->equalTo( '' ) ); - $this->mUserMock->expects( $this->at( 7 ) ) + $this->mUserMock->expects( $this->at( 8 ) ) ->method( 'getOptions' ); - $this->mUserMock->expects( $this->at( 8 ) ) + $this->mUserMock->expects( $this->at( 9 ) ) ->method( 'setOption' ) ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) ); $this->mUserMock->expects( $this->once() ) ->method( 'saveSettings' ); - $request = $this->getSampleRequest( array( + $request = $this->getSampleRequest( [ 'change' => 'willBeNull|willBeEmpty=|willBeHappy=Happy' - ) ); + ] ); $response = $this->executeQuery( $request ); @@ -317,29 +321,29 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mUserMock->expects( $this->once() ) ->method( 'resetOptions' ); - $this->mUserMock->expects( $this->at( 4 ) ) + $this->mUserMock->expects( $this->at( 5 ) ) ->method( 'getOptions' ); - $this->mUserMock->expects( $this->at( 5 ) ) + $this->mUserMock->expects( $this->at( 6 ) ) ->method( 'setOption' ) ->with( $this->equalTo( 'willBeHappy' ), $this->equalTo( 'Happy' ) ); - $this->mUserMock->expects( $this->at( 6 ) ) + $this->mUserMock->expects( $this->at( 7 ) ) ->method( 'getOptions' ); - $this->mUserMock->expects( $this->at( 7 ) ) + $this->mUserMock->expects( $this->at( 8 ) ) ->method( 'setOption' ) ->with( $this->equalTo( 'name' ), $this->equalTo( 'value' ) ); $this->mUserMock->expects( $this->once() ) ->method( 'saveSettings' ); - $args = array( + $args = [ 'reset' => '', 'change' => 'willBeHappy=Happy', 'optionname' => 'name', 'optionvalue' => 'value' - ); + ]; $response = $this->executeQuery( $this->getSampleRequest( $args ) ); @@ -350,29 +354,29 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mUserMock->expects( $this->never() ) ->method( 'resetOptions' ); - $this->mUserMock->expects( $this->at( 3 ) ) + $this->mUserMock->expects( $this->at( 4 ) ) ->method( 'setOption' ) ->with( $this->equalTo( 'testmultiselect-opt1' ), $this->identicalTo( true ) ); - $this->mUserMock->expects( $this->at( 4 ) ) + $this->mUserMock->expects( $this->at( 5 ) ) ->method( 'setOption' ) ->with( $this->equalTo( 'testmultiselect-opt2' ), $this->identicalTo( null ) ); - $this->mUserMock->expects( $this->at( 5 ) ) + $this->mUserMock->expects( $this->at( 6 ) ) ->method( 'setOption' ) ->with( $this->equalTo( 'testmultiselect-opt3' ), $this->identicalTo( false ) ); - $this->mUserMock->expects( $this->at( 6 ) ) + $this->mUserMock->expects( $this->at( 7 ) ) ->method( 'setOption' ) ->with( $this->equalTo( 'testmultiselect-opt4' ), $this->identicalTo( false ) ); $this->mUserMock->expects( $this->once() ) ->method( 'saveSettings' ); - $request = $this->getSampleRequest( array( + $request = $this->getSampleRequest( [ 'change' => 'testmultiselect-opt1=1|testmultiselect-opt2|' . 'testmultiselect-opt3=|testmultiselect-opt4=0' - ) ); + ] ); $response = $this->executeQuery( $request ); @@ -386,20 +390,20 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mUserMock->expects( $this->never() ) ->method( 'saveSettings' ); - $request = $this->getSampleRequest( array( + $request = $this->getSampleRequest( [ 'change' => 'special=1' - ) ); + ] ); $response = $this->executeQuery( $request ); - $this->assertEquals( array( + $this->assertEquals( [ 'options' => 'success', - 'warnings' => array( - 'options' => array( + 'warnings' => [ + 'options' => [ 'warnings' => "Validation error for 'special': cannot be set by this module" - ) - ) - ), $response ); + ] + ] + ], $response ); } public function testUnknownOption() { @@ -409,36 +413,36 @@ class ApiOptionsTest extends MediaWikiLangTestCase { $this->mUserMock->expects( $this->never() ) ->method( 'saveSettings' ); - $request = $this->getSampleRequest( array( + $request = $this->getSampleRequest( [ 'change' => 'unknownOption=1' - ) ); + ] ); $response = $this->executeQuery( $request ); - $this->assertEquals( array( + $this->assertEquals( [ 'options' => 'success', - 'warnings' => array( - 'options' => array( + 'warnings' => [ + 'options' => [ 'warnings' => "Validation error for 'unknownOption': not a valid preference" - ) - ) - ), $response ); + ] + ] + ], $response ); } public function testUserjsOption() { $this->mUserMock->expects( $this->never() ) ->method( 'resetOptions' ); - $this->mUserMock->expects( $this->at( 3 ) ) + $this->mUserMock->expects( $this->once() ) ->method( 'setOption' ) ->with( $this->equalTo( 'userjs-option' ), $this->equalTo( '1' ) ); $this->mUserMock->expects( $this->once() ) ->method( 'saveSettings' ); - $request = $this->getSampleRequest( array( + $request = $this->getSampleRequest( [ 'change' => 'userjs-option=1' - ) ); + ] ); $response = $this->executeQuery( $request );