2013-02-14 11:56:23 +00:00
|
|
|
<?php
|
2011-04-06 19:50:54 +00:00
|
|
|
|
2019-08-26 12:24:37 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2018-03-28 12:32:19 +00:00
|
|
|
use MediaWiki\Session\SessionManager;
|
2019-10-06 04:54:59 +00:00
|
|
|
use PHPUnit\Framework\Assert;
|
|
|
|
|
use PHPUnit\Util\Test;
|
2018-03-28 12:32:19 +00:00
|
|
|
|
2011-05-01 23:02:27 +00:00
|
|
|
abstract class ApiTestCase extends MediaWikiLangTestCase {
|
2011-07-01 16:34:02 +00:00
|
|
|
protected static $apiUrl;
|
2011-04-06 19:50:54 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
protected static $errorFormatter = null;
|
|
|
|
|
|
2011-10-27 01:06:50 +00:00
|
|
|
/**
|
|
|
|
|
* @var ApiTestContext
|
|
|
|
|
*/
|
|
|
|
|
protected $apiContext;
|
|
|
|
|
|
2019-10-20 18:11:08 +00:00
|
|
|
protected function setUp() : void {
|
2016-04-01 16:49:26 +00:00
|
|
|
global $wgServer;
|
2011-04-06 19:50:54 +00:00
|
|
|
|
2011-05-01 23:02:27 +00:00
|
|
|
parent::setUp();
|
2011-07-01 16:34:02 +00:00
|
|
|
self::$apiUrl = $wgServer . wfScript( 'api' );
|
2011-04-06 19:50:54 +00:00
|
|
|
|
2012-09-24 13:07:36 +00:00
|
|
|
ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
self::$users = [
|
2016-05-18 09:19:20 +00:00
|
|
|
'sysop' => static::getTestSysop(),
|
|
|
|
|
'uploader' => static::getTestUser(),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-04-06 19:50:54 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgRequest' => new FauxRequest( [] ),
|
2016-05-06 15:11:08 +00:00
|
|
|
'wgUser' => self::$users['sysop']->getUser(),
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2011-04-06 19:50:54 +00:00
|
|
|
|
2011-10-27 01:06:50 +00:00
|
|
|
$this->apiContext = new ApiTestContext();
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 18:11:08 +00:00
|
|
|
protected function tearDown() : void {
|
2014-12-09 20:35:40 +00:00
|
|
|
// Avoid leaking session over tests
|
2016-02-01 20:44:03 +00:00
|
|
|
MediaWiki\Session\SessionManager::getGlobalSession()->clear();
|
2014-12-09 20:35:40 +00:00
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 17:36:10 +00:00
|
|
|
/**
|
|
|
|
|
* Does the API request and returns the result.
|
|
|
|
|
*
|
|
|
|
|
* The returned value is an array containing
|
|
|
|
|
* - the result data (array)
|
|
|
|
|
* - the request (WebRequest)
|
|
|
|
|
* - the session data of the request (array)
|
|
|
|
|
* - if $appendModule is true, the Api module $module
|
|
|
|
|
*
|
|
|
|
|
* @param array $params
|
|
|
|
|
* @param array|null $session
|
|
|
|
|
* @param bool $appendModule
|
|
|
|
|
* @param User|null $user
|
2018-03-28 12:32:19 +00:00
|
|
|
* @param string|null $tokenType Set to a string like 'csrf' to send an
|
|
|
|
|
* appropriate token
|
2012-11-01 17:36:10 +00:00
|
|
|
*
|
2018-03-23 11:33:10 +00:00
|
|
|
* @throws ApiUsageException
|
2012-11-01 17:36:10 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2014-04-24 15:05:10 +00:00
|
|
|
protected function doApiRequest( array $params, array $session = null,
|
2018-03-28 12:32:19 +00:00
|
|
|
$appendModule = false, User $user = null, $tokenType = null
|
2014-04-24 15:05:10 +00:00
|
|
|
) {
|
2012-06-22 20:42:42 +00:00
|
|
|
global $wgRequest, $wgUser;
|
2012-06-21 20:25:37 +00:00
|
|
|
|
2012-06-22 20:42:42 +00:00
|
|
|
if ( is_null( $session ) ) {
|
2012-11-01 17:36:10 +00:00
|
|
|
// re-use existing global session by default
|
2012-06-21 20:25:37 +00:00
|
|
|
$session = $wgRequest->getSessionArray();
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-28 12:32:19 +00:00
|
|
|
$sessionObj = SessionManager::singleton()->getEmptySession();
|
|
|
|
|
|
|
|
|
|
if ( $session !== null ) {
|
|
|
|
|
foreach ( $session as $key => $value ) {
|
|
|
|
|
$sessionObj->set( $key, $value );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 17:36:10 +00:00
|
|
|
// set up global environment
|
2012-06-22 20:42:42 +00:00
|
|
|
if ( $user ) {
|
|
|
|
|
$wgUser = $user;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 12:32:19 +00:00
|
|
|
if ( $tokenType !== null ) {
|
2018-04-09 18:38:09 +00:00
|
|
|
if ( $tokenType === 'auto' ) {
|
|
|
|
|
$tokenType = ( new ApiMain() )->getModuleManager()
|
|
|
|
|
->getModule( $params['action'], 'action' )->needsToken();
|
|
|
|
|
}
|
2018-03-28 12:32:19 +00:00
|
|
|
$params['token'] = ApiQueryTokens::getToken(
|
|
|
|
|
$wgUser, $sessionObj, ApiQueryTokens::getTokenTypeSalts()[$tokenType]
|
|
|
|
|
)->toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wgRequest = new FauxRequest( $params, true, $sessionObj );
|
2012-06-22 20:42:42 +00:00
|
|
|
RequestContext::getMain()->setRequest( $wgRequest );
|
2015-06-05 09:52:40 +00:00
|
|
|
RequestContext::getMain()->setUser( $wgUser );
|
2015-11-22 20:17:00 +00:00
|
|
|
MediaWiki\Auth\AuthManager::resetCache();
|
2012-06-22 20:42:42 +00:00
|
|
|
|
2012-11-01 17:36:10 +00:00
|
|
|
// set up local environment
|
2012-06-22 20:42:42 +00:00
|
|
|
$context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
|
|
|
|
|
|
2011-10-27 01:06:50 +00:00
|
|
|
$module = new ApiMain( $context, true );
|
2012-06-22 20:42:42 +00:00
|
|
|
|
2012-11-01 17:36:10 +00:00
|
|
|
// run it!
|
2011-04-06 19:50:54 +00:00
|
|
|
$module->execute();
|
|
|
|
|
|
2012-11-01 17:36:10 +00:00
|
|
|
// construct result
|
2016-02-17 09:09:32 +00:00
|
|
|
$results = [
|
|
|
|
|
$module->getResult()->getResultData( null, [ 'Strip' => 'all' ] ),
|
2011-10-27 01:06:50 +00:00
|
|
|
$context->getRequest(),
|
|
|
|
|
$context->getRequest()->getSessionArray()
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-11-01 17:36:10 +00:00
|
|
|
|
|
|
|
|
if ( $appendModule ) {
|
2011-07-01 16:34:02 +00:00
|
|
|
$results[] = $module;
|
|
|
|
|
}
|
2011-08-13 14:00:22 +00:00
|
|
|
|
2011-07-01 16:34:02 +00:00
|
|
|
return $results;
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-28 12:32:19 +00:00
|
|
|
* Convenience function to access the token parameter of doApiRequest()
|
|
|
|
|
* more succinctly.
|
2014-04-24 15:05:10 +00:00
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param array $params Key-value API params
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param array|null $session Session array
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param User|null $user A User object for the context
|
2018-03-28 12:32:19 +00:00
|
|
|
* @param string $tokenType Which token type to pass
|
2014-08-13 17:54:49 +00:00
|
|
|
* @return array Result of the API call
|
2011-04-06 19:50:54 +00:00
|
|
|
*/
|
2014-04-24 15:05:10 +00:00
|
|
|
protected function doApiRequestWithToken( array $params, array $session = null,
|
2018-04-09 18:38:09 +00:00
|
|
|
User $user = null, $tokenType = 'auto'
|
2014-04-24 15:05:10 +00:00
|
|
|
) {
|
2018-03-28 12:32:19 +00:00
|
|
|
return $this->doApiRequest( $params, $session, false, $user, $tokenType );
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-28 14:01:46 +00:00
|
|
|
/**
|
|
|
|
|
* Previously this would do API requests to log in, as well as setting $wgUser and the request
|
|
|
|
|
* context's user. The API requests are unnecessary, and the global-setting is unwanted, so
|
|
|
|
|
* this method should not be called. Instead, pass appropriate User values directly to
|
|
|
|
|
* functions that need them. For functions that still rely on $wgUser, set that directly. If
|
|
|
|
|
* you just want to log in the test sysop user, don't do anything -- that's the default.
|
|
|
|
|
*
|
|
|
|
|
* @param TestUser|string $testUser Object, or key to self::$users such as 'sysop' or 'uploader'
|
|
|
|
|
* @deprecated since 1.31
|
|
|
|
|
*/
|
|
|
|
|
protected function doLogin( $testUser = null ) {
|
|
|
|
|
global $wgUser;
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
if ( $testUser === null ) {
|
|
|
|
|
$testUser = static::getTestSysop();
|
|
|
|
|
} elseif ( is_string( $testUser ) && array_key_exists( $testUser, self::$users ) ) {
|
2018-03-28 14:01:46 +00:00
|
|
|
$testUser = self::$users[$testUser];
|
2016-05-18 09:19:20 +00:00
|
|
|
} elseif ( !$testUser instanceof TestUser ) {
|
2018-03-28 14:01:46 +00:00
|
|
|
throw new MWException( "Can't log in to undefined user $testUser" );
|
2013-08-05 18:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2018-03-28 14:01:46 +00:00
|
|
|
$wgUser = $testUser->getUser();
|
|
|
|
|
RequestContext::getMain()->setUser( $wgUser );
|
2011-07-01 16:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-06 15:11:08 +00:00
|
|
|
protected function getTokenList( TestUser $user, $session = null ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$data = $this->doApiRequest( [
|
2013-08-05 19:31:45 +00:00
|
|
|
'action' => 'tokens',
|
|
|
|
|
'type' => 'edit|delete|protect|move|block|unblock|watch'
|
2016-05-06 15:11:08 +00:00
|
|
|
], $session, false, $user->getUser() );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2013-08-24 15:06:25 +00:00
|
|
|
if ( !array_key_exists( 'tokens', $data[0] ) ) {
|
2013-08-05 19:31:45 +00:00
|
|
|
throw new MWException( 'Api failed to return a token list' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data[0]['tokens'];
|
2011-07-01 16:34:02 +00:00
|
|
|
}
|
2013-01-18 19:02:28 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
protected static function getErrorFormatter() {
|
|
|
|
|
if ( self::$errorFormatter === null ) {
|
|
|
|
|
self::$errorFormatter = new ApiErrorFormatter(
|
|
|
|
|
new ApiResult( false ),
|
2019-08-26 12:24:37 +00:00
|
|
|
MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' ),
|
2016-10-19 16:54:25 +00:00
|
|
|
'none'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return self::$errorFormatter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function apiExceptionHasCode( ApiUsageException $ex, $code ) {
|
|
|
|
|
return (bool)array_filter(
|
|
|
|
|
self::getErrorFormatter()->arrayFromStatus( $ex->getStatusValue() ),
|
|
|
|
|
function ( $e ) use ( $code ) {
|
|
|
|
|
return is_array( $e ) && $e['code'] === $code;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-28 08:38:21 +00:00
|
|
|
/**
|
|
|
|
|
* @coversNothing
|
|
|
|
|
*/
|
2013-01-18 19:02:28 +00:00
|
|
|
public function testApiTestGroup() {
|
2019-10-06 04:54:59 +00:00
|
|
|
$groups = Test::getGroups( static::class );
|
|
|
|
|
$constraint = Assert::logicalOr(
|
2013-01-18 19:02:28 +00:00
|
|
|
$this->contains( 'medium' ),
|
|
|
|
|
$this->contains( 'large' )
|
|
|
|
|
);
|
|
|
|
|
$this->assertThat( $groups, $constraint,
|
|
|
|
|
'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-07-31 13:19:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Expect an ApiUsageException to be thrown with the given parameters, which are the same as
|
|
|
|
|
* ApiUsageException::newWithMessage()'s parameters. This allows checking for an exception
|
|
|
|
|
* whose text is given by a message key instead of text, so as not to hard-code the message's
|
|
|
|
|
* text into test code.
|
|
|
|
|
*/
|
|
|
|
|
protected function setExpectedApiException(
|
|
|
|
|
$msg, $code = null, array $data = null, $httpCode = 0
|
|
|
|
|
) {
|
|
|
|
|
$expected = ApiUsageException::newWithMessage( null, $msg, $code, $data, $httpCode );
|
2019-10-06 04:54:59 +00:00
|
|
|
$this->expectException( ApiUsageException::class );
|
|
|
|
|
$this->expectExceptionMessage( $expected->getMessage() );
|
2018-07-31 13:19:10 +00:00
|
|
|
}
|
2011-07-01 16:34:02 +00:00
|
|
|
}
|