From: addshore Date: Wed, 23 Oct 2013 16:01:33 +0000 (+0100) Subject: Cleanup Api phpunit Tests X-Git-Tag: 1.31.0-rc.0~18406^2 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=dc18b813e81bbd83ff9986fedd1eef1b201132f4;p=lhc%2Fweb%2Fwiklou.git Cleanup Api phpunit Tests - Splits multiple classes into individual files - Adds @covers tags - Fixes scope Change-Id: I7d2816d3574fa53a2aaa8e2a84b7a7ecdd245252 --- diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 00ce13c893..dec8e22dd1 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -55,10 +55,10 @@ $wgAutoloadClasses += array( # tests/phpunit/includes/api 'ApiFormatTestBase' => "$testDir/phpunit/includes/api/format/ApiFormatTestBase.php", 'ApiTestCase' => "$testDir/phpunit/includes/api/ApiTestCase.php", - 'ApiTestContext' => "$testDir/phpunit/includes/api/ApiTestCase.php", - 'MockApi' => "$testDir/phpunit/includes/api/ApiTestCase.php", + 'ApiTestContext' => "$testDir/phpunit/includes/api/ApiTestContext.php", + 'MockApi' => "$testDir/phpunit/includes/api/MockApi.php", + 'UserWrapper' => "$testDir/phpunit/includes/api/UserWrapper.php", 'RandomImageGenerator' => "$testDir/phpunit/includes/api/RandomImageGenerator.php", - 'UserWrapper' => "$testDir/phpunit/includes/api/ApiTestCase.php", # tests/phpunit/includes/content 'DummyContentHandlerForTesting' => "$testDir/phpunit/includes/content/ContentHandlerTest.php", diff --git a/tests/phpunit/includes/api/ApiAccountCreationTest.php b/tests/phpunit/includes/api/ApiAccountCreationTest.php deleted file mode 100644 index 68f80ac9e8..0000000000 --- a/tests/phpunit/includes/api/ApiAccountCreationTest.php +++ /dev/null @@ -1,159 +0,0 @@ -setMwGlobals( array( 'wgEnableEmail' => true ) ); - } - - /** - * Test the account creation API with a valid request. Also - * make sure the new account can log in and is valid. - * - * This test does multiple API requests so it might end up being - * a bit slow. Raise the default timeout. - * @group medium - */ - public function testValid() { - global $wgServer; - - if ( !isset( $wgServer ) ) { - $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' ); - } - - $password = User::randomPassword(); - - $ret = $this->doApiRequest( array( - 'action' => 'createaccount', - 'name' => 'Apitestnew', - 'password' => $password, - 'email' => 'test@domain.test', - 'realname' => 'Test Name' - ) ); - - $result = $ret[0]; - $this->assertNotInternalType( 'bool', $result ); - $this->assertNotInternalType( 'null', $result['createaccount'] ); - - // Should first ask for token. - $a = $result['createaccount']; - $this->assertEquals( 'needtoken', $a['result'] ); - $token = $a['token']; - - // Finally create the account - $ret = $this->doApiRequest( - array( - 'action' => 'createaccount', - 'name' => 'Apitestnew', - 'password' => $password, - 'token' => $token, - 'email' => 'test@domain.test', - 'realname' => 'Test Name' - ), - $ret[2] - ); - - $result = $ret[0]; - $this->assertNotInternalType( 'bool', $result ); - $this->assertEquals( 'success', $result['createaccount']['result'] ); - - // Try logging in with the new user. - $ret = $this->doApiRequest( array( - 'action' => 'login', - 'lgname' => 'Apitestnew', - 'lgpassword' => $password, - ) ); - - $result = $ret[0]; - $this->assertNotInternalType( 'bool', $result ); - $this->assertNotInternalType( 'null', $result['login'] ); - - $a = $result['login']['result']; - $this->assertEquals( 'NeedToken', $a ); - $token = $result['login']['token']; - - $ret = $this->doApiRequest( - array( - 'action' => 'login', - 'lgtoken' => $token, - 'lgname' => 'Apitestnew', - 'lgpassword' => $password, - ), - $ret[2] - ); - - $result = $ret[0]; - - $this->assertNotInternalType( 'bool', $result ); - $a = $result['login']['result']; - - $this->assertEquals( 'Success', $a ); - - // log out to destroy the session - $ret = $this->doApiRequest( - array( - 'action' => 'logout', - ), - $ret[2] - ); - $this->assertEquals( array(), $ret[0] ); - } - - /** - * Make sure requests with no names are invalid. - * @expectedException UsageException - */ - public function testNoName() { - $this->doApiRequest( array( - 'action' => 'createaccount', - 'token' => LoginForm::getCreateaccountToken(), - 'password' => 'password', - ) ); - } - - /** - * Make sure requests with no password are invalid. - * @expectedException UsageException - */ - public function testNoPassword() { - $this->doApiRequest( array( - 'action' => 'createaccount', - 'name' => 'testName', - 'token' => LoginForm::getCreateaccountToken(), - ) ); - } - - /** - * Make sure requests with existing users are invalid. - * @expectedException UsageException - */ - public function testExistingUser() { - $this->doApiRequest( array( - 'action' => 'createaccount', - 'name' => 'Apitestsysop', - 'token' => LoginForm::getCreateaccountToken(), - 'password' => 'password', - 'email' => 'test@domain.test', - ) ); - } - - /** - * Make sure requests with invalid emails are invalid. - * @expectedException UsageException - */ - public function testInvalidEmail() { - $this->doApiRequest( array( - 'action' => 'createaccount', - 'name' => 'Test User', - 'token' => LoginForm::getCreateaccountToken(), - 'password' => 'password', - 'email' => 'invalid', - ) ); - } -} diff --git a/tests/phpunit/includes/api/ApiBlockTest.php b/tests/phpunit/includes/api/ApiBlockTest.php index 8afb748a8d..d98eec6a67 100644 --- a/tests/phpunit/includes/api/ApiBlockTest.php +++ b/tests/phpunit/includes/api/ApiBlockTest.php @@ -4,6 +4,8 @@ * @group API * @group Database * @group medium + * + * @covers ApiBlock */ class ApiBlockTest extends ApiTestCase { protected function setUp() { @@ -11,7 +13,7 @@ class ApiBlockTest extends ApiTestCase { $this->doLogin(); } - function getTokens() { + protected function getTokens() { return $this->getTokenList( self::$users['sysop'] ); } @@ -63,17 +65,13 @@ class ApiBlockTest extends ApiTestCase { } /** - * Attempting to block without a token should give a UsageException with - * error message: - * "The token parameter must be set" - * - * @dataProvider provideBlockUnblockAction * @expectedException UsageException + * @expectedExceptionMessage The token parameter must be set */ - public function testBlockingActionWithNoToken( $action ) { + public function testBlockingActionWithNoToken( ) { $this->doApiRequest( array( - 'action' => $action, + 'action' => 'block', 'user' => 'UTApiBlockee', 'reason' => 'Some reason', ), @@ -82,14 +80,4 @@ class ApiBlockTest extends ApiTestCase { self::$users['sysop']->user ); } - - /** - * Just provide the 'block' and 'unblock' action to test both API calls - */ - public static function provideBlockUnblockAction() { - return array( - array( 'block' ), - array( 'unblock' ), - ); - } } diff --git a/tests/phpunit/includes/api/ApiCreateAccountTest.php b/tests/phpunit/includes/api/ApiCreateAccountTest.php new file mode 100644 index 0000000000..a7232457b6 --- /dev/null +++ b/tests/phpunit/includes/api/ApiCreateAccountTest.php @@ -0,0 +1,161 @@ +setMwGlobals( array( 'wgEnableEmail' => true ) ); + } + + /** + * Test the account creation API with a valid request. Also + * make sure the new account can log in and is valid. + * + * This test does multiple API requests so it might end up being + * a bit slow. Raise the default timeout. + * @group medium + */ + public function testValid() { + global $wgServer; + + if ( !isset( $wgServer ) ) { + $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' ); + } + + $password = User::randomPassword(); + + $ret = $this->doApiRequest( array( + 'action' => 'createaccount', + 'name' => 'Apitestnew', + 'password' => $password, + 'email' => 'test@domain.test', + 'realname' => 'Test Name' + ) ); + + $result = $ret[0]; + $this->assertNotInternalType( 'bool', $result ); + $this->assertNotInternalType( 'null', $result['createaccount'] ); + + // Should first ask for token. + $a = $result['createaccount']; + $this->assertEquals( 'needtoken', $a['result'] ); + $token = $a['token']; + + // Finally create the account + $ret = $this->doApiRequest( + array( + 'action' => 'createaccount', + 'name' => 'Apitestnew', + 'password' => $password, + 'token' => $token, + 'email' => 'test@domain.test', + 'realname' => 'Test Name' + ), + $ret[2] + ); + + $result = $ret[0]; + $this->assertNotInternalType( 'bool', $result ); + $this->assertEquals( 'success', $result['createaccount']['result'] ); + + // Try logging in with the new user. + $ret = $this->doApiRequest( array( + 'action' => 'login', + 'lgname' => 'Apitestnew', + 'lgpassword' => $password, + ) ); + + $result = $ret[0]; + $this->assertNotInternalType( 'bool', $result ); + $this->assertNotInternalType( 'null', $result['login'] ); + + $a = $result['login']['result']; + $this->assertEquals( 'NeedToken', $a ); + $token = $result['login']['token']; + + $ret = $this->doApiRequest( + array( + 'action' => 'login', + 'lgtoken' => $token, + 'lgname' => 'Apitestnew', + 'lgpassword' => $password, + ), + $ret[2] + ); + + $result = $ret[0]; + + $this->assertNotInternalType( 'bool', $result ); + $a = $result['login']['result']; + + $this->assertEquals( 'Success', $a ); + + // log out to destroy the session + $ret = $this->doApiRequest( + array( + 'action' => 'logout', + ), + $ret[2] + ); + $this->assertEquals( array(), $ret[0] ); + } + + /** + * Make sure requests with no names are invalid. + * @expectedException UsageException + */ + public function testNoName() { + $this->doApiRequest( array( + 'action' => 'createaccount', + 'token' => LoginForm::getCreateaccountToken(), + 'password' => 'password', + ) ); + } + + /** + * Make sure requests with no password are invalid. + * @expectedException UsageException + */ + public function testNoPassword() { + $this->doApiRequest( array( + 'action' => 'createaccount', + 'name' => 'testName', + 'token' => LoginForm::getCreateaccountToken(), + ) ); + } + + /** + * Make sure requests with existing users are invalid. + * @expectedException UsageException + */ + public function testExistingUser() { + $this->doApiRequest( array( + 'action' => 'createaccount', + 'name' => 'Apitestsysop', + 'token' => LoginForm::getCreateaccountToken(), + 'password' => 'password', + 'email' => 'test@domain.test', + ) ); + } + + /** + * Make sure requests with invalid emails are invalid. + * @expectedException UsageException + */ + public function testInvalidEmail() { + $this->doApiRequest( array( + 'action' => 'createaccount', + 'name' => 'Test User', + 'token' => LoginForm::getCreateaccountToken(), + 'password' => 'password', + 'email' => 'invalid', + ) ); + } +} diff --git a/tests/phpunit/includes/api/ApiEditPageTest.php b/tests/phpunit/includes/api/ApiEditPageTest.php index 0c49b12b97..7521dcf743 100644 --- a/tests/phpunit/includes/api/ApiEditPageTest.php +++ b/tests/phpunit/includes/api/ApiEditPageTest.php @@ -8,6 +8,8 @@ * @group API * @group Database * @group medium + * + * @covers ApiEditPage */ class ApiEditPageTest extends ApiTestCase { diff --git a/tests/phpunit/includes/api/ApiOptionsTest.php b/tests/phpunit/includes/api/ApiOptionsTest.php index ad1e73ab4a..3168f323c0 100644 --- a/tests/phpunit/includes/api/ApiOptionsTest.php +++ b/tests/phpunit/includes/api/ApiOptionsTest.php @@ -4,10 +4,18 @@ * @group API * @group Database * @group medium + * + * @covers ApiOptions */ class ApiOptionsTest extends MediaWikiLangTestCase { - private $mTested, $mUserMock, $mContext, $mSession; + /** @var PHPUnit_Framework_MockObject_MockObject */ + private $mUserMock ; + /** @var ApiOptions */ + private $mTested; + private $mSession; + /** @var DerivativeContext */ + private $mContext; private $mOldGetPreferencesHooks = false; diff --git a/tests/phpunit/includes/api/ApiParseTest.php b/tests/phpunit/includes/api/ApiParseTest.php index 2d714e65a2..d303d4b0d2 100644 --- a/tests/phpunit/includes/api/ApiParseTest.php +++ b/tests/phpunit/includes/api/ApiParseTest.php @@ -4,6 +4,8 @@ * @group API * @group Database * @group medium + * + * @covers ApiParse */ class ApiParseTest extends ApiTestCase { diff --git a/tests/phpunit/includes/api/ApiPurgeTest.php b/tests/phpunit/includes/api/ApiPurgeTest.php index 28b5ff4d4d..d25a4c1d31 100644 --- a/tests/phpunit/includes/api/ApiPurgeTest.php +++ b/tests/phpunit/includes/api/ApiPurgeTest.php @@ -4,6 +4,8 @@ * @group API * @group Database * @group medium + * + * @covers ApiPurge */ class ApiPurgeTest extends ApiTestCase { diff --git a/tests/phpunit/includes/api/ApiTestCase.php b/tests/phpunit/includes/api/ApiTestCase.php index 94ef9c6868..ad297dd935 100644 --- a/tests/phpunit/includes/api/ApiTestCase.php +++ b/tests/phpunit/includes/api/ApiTestCase.php @@ -117,7 +117,7 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { * @param $params Array: key-value API params * @param $session Array|null: session array * @param $user User|null A User object for the context - * @return result of the API call + * @return mixed result of the API call * @throws Exception in case wsToken is not set in the session */ protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) { @@ -188,66 +188,3 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { ); } } - -class UserWrapper { - public $userName; - public $password; - public $user; - - public function __construct( $userName, $password, $group = '' ) { - $this->userName = $userName; - $this->password = $password; - - $this->user = User::newFromName( $this->userName ); - if ( !$this->user->getID() ) { - $this->user = User::createNew( $this->userName, array( - "email" => "test@example.com", - "real_name" => "Test User" ) ); - } - $this->user->setPassword( $this->password ); - - if ( $group !== '' ) { - $this->user->addGroup( $group ); - } - $this->user->saveSettings(); - } -} - -class MockApi extends ApiBase { - public function execute() { - } - - public function getVersion() { - } - - public function __construct() { - } - - public function getAllowedParams() { - return array( - 'filename' => null, - 'enablechunks' => false, - 'sessionkey' => null, - ); - } -} - -class ApiTestContext extends RequestContext { - - /** - * Returns a DerivativeContext with the request variables in place - * - * @param $request WebRequest request object including parameters and session - * @param $user User or null - * @return DerivativeContext - */ - public function newTestContext( WebRequest $request, User $user = null ) { - $context = new DerivativeContext( $this ); - $context->setRequest( $request ); - if ( $user !== null ) { - $context->setUser( $user ); - } - - return $context; - } -} diff --git a/tests/phpunit/includes/api/ApiTestContext.php b/tests/phpunit/includes/api/ApiTestContext.php new file mode 100644 index 0000000000..43637c2344 --- /dev/null +++ b/tests/phpunit/includes/api/ApiTestContext.php @@ -0,0 +1,21 @@ +setRequest( $request ); + if ( $user !== null ) { + $context->setUser( $user ); + } + + return $context; + } +} \ No newline at end of file diff --git a/tests/phpunit/includes/api/ApiUnblockTest.php b/tests/phpunit/includes/api/ApiUnblockTest.php new file mode 100644 index 0000000000..2c2370a8eb --- /dev/null +++ b/tests/phpunit/includes/api/ApiUnblockTest.php @@ -0,0 +1,31 @@ +doLogin(); + } + + /** + * @expectedException UsageException + */ + public function testWithNoToken( ) { + $this->doApiRequest( + array( + 'action' => 'unblock', + 'user' => 'UTApiBlockee', + 'reason' => 'Some reason', + ), + null, + false, + self::$users['sysop']->user + ); + } +} diff --git a/tests/phpunit/includes/api/MockApi.php b/tests/phpunit/includes/api/MockApi.php new file mode 100644 index 0000000000..3686048cc0 --- /dev/null +++ b/tests/phpunit/includes/api/MockApi.php @@ -0,0 +1,20 @@ + null, + 'enablechunks' => false, + 'sessionkey' => null, + ); + } +} \ No newline at end of file diff --git a/tests/phpunit/includes/api/PrefixUniquenessTest.php b/tests/phpunit/includes/api/PrefixUniquenessTest.php index d9be85e320..a1fc2c296e 100644 --- a/tests/phpunit/includes/api/PrefixUniquenessTest.php +++ b/tests/phpunit/includes/api/PrefixUniquenessTest.php @@ -6,6 +6,7 @@ * @group API */ class PrefixUniquenessTest extends MediaWikiTestCase { + public function testPrefixes() { $main = new ApiMain( new FauxRequest() ); $query = new ApiQuery( $main, 'foo', 'bar' ); @@ -13,6 +14,7 @@ class PrefixUniquenessTest extends MediaWikiTestCase { $prefixes = array(); foreach ( $modules as $name => $class ) { + /** @var ApiMain $module */ $module = new $class( $main, $name ); $prefix = $module->getModulePrefix(); if ( isset( $prefixes[$prefix] ) ) { diff --git a/tests/phpunit/includes/api/UserWrapper.php b/tests/phpunit/includes/api/UserWrapper.php new file mode 100644 index 0000000000..3262e6ccbc --- /dev/null +++ b/tests/phpunit/includes/api/UserWrapper.php @@ -0,0 +1,25 @@ +userName = $userName; + $this->password = $password; + + $this->user = User::newFromName( $this->userName ); + if ( !$this->user->getID() ) { + $this->user = User::createNew( $this->userName, array( + "email" => "test@example.com", + "real_name" => "Test User" ) ); + } + $this->user->setPassword( $this->password ); + + if ( $group !== '' ) { + $this->user->addGroup( $group ); + } + $this->user->saveSettings(); + } +} \ No newline at end of file