# 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",
+++ /dev/null
-<?php
-
-/**
- * @group Database
- * @group API
- * @group medium
- */
-class ApiCreateAccountTest extends ApiTestCase {
- function setUp() {
- parent::setUp();
- LoginForm::setCreateaccountToken();
- $this->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',
- ) );
- }
-}
* @group API
* @group Database
* @group medium
+ *
+ * @covers ApiBlock
*/
class ApiBlockTest extends ApiTestCase {
protected function setUp() {
$this->doLogin();
}
- function getTokens() {
+ protected function getTokens() {
return $this->getTokenList( self::$users['sysop'] );
}
}
/**
- * 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',
),
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' ),
- );
- }
}
--- /dev/null
+<?php
+
+/**
+ * @group Database
+ * @group API
+ * @group medium
+ *
+ * @covers ApiCreateAccount
+ */
+class ApiCreateAccountTest extends ApiTestCase {
+ protected function setUp() {
+ parent::setUp();
+ LoginForm::setCreateaccountToken();
+ $this->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',
+ ) );
+ }
+}
* @group API
* @group Database
* @group medium
+ *
+ * @covers ApiEditPage
*/
class ApiEditPageTest extends ApiTestCase {
* @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;
* @group API
* @group Database
* @group medium
+ *
+ * @covers ApiParse
*/
class ApiParseTest extends ApiTestCase {
* @group API
* @group Database
* @group medium
+ *
+ * @covers ApiPurge
*/
class ApiPurgeTest extends ApiTestCase {
* @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 ) {
);
}
}
-
-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;
- }
-}
--- /dev/null
+<?php
+
+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;
+ }
+}
\ No newline at end of file
--- /dev/null
+<?php
+
+/**
+ * @group API
+ * @group Database
+ * @group medium
+ *
+ * @covers ApiUnblock
+ */
+class ApiUnblockTest extends ApiTestCase {
+ protected function setUp() {
+ parent::setUp();
+ $this->doLogin();
+ }
+
+ /**
+ * @expectedException UsageException
+ */
+ public function testWithNoToken( ) {
+ $this->doApiRequest(
+ array(
+ 'action' => 'unblock',
+ 'user' => 'UTApiBlockee',
+ 'reason' => 'Some reason',
+ ),
+ null,
+ false,
+ self::$users['sysop']->user
+ );
+ }
+}
--- /dev/null
+<?php
+
+class MockApi extends ApiBase {
+ public function execute() {
+ }
+
+ public function getVersion() {
+ }
+
+ public function __construct() {
+ }
+
+ public function getAllowedParams() {
+ return array(
+ 'filename' => null,
+ 'enablechunks' => false,
+ 'sessionkey' => null,
+ );
+ }
+}
\ No newline at end of file
* @group API
*/
class PrefixUniquenessTest extends MediaWikiTestCase {
+
public function testPrefixes() {
$main = new ApiMain( new FauxRequest() );
$query = new ApiQuery( $main, 'foo', 'bar' );
$prefixes = array();
foreach ( $modules as $name => $class ) {
+ /** @var ApiMain $module */
$module = new $class( $main, $name );
$prefix = $module->getModulePrefix();
if ( isset( $prefixes[$prefix] ) ) {
--- /dev/null
+<?php
+
+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();
+ }
+}
\ No newline at end of file