2013-02-14 11:56:23 +00:00
|
|
|
<?php
|
2011-04-06 19:50:54 +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;
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
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( [
|
2016-04-01 16:49:26 +00:00
|
|
|
'wgAuth' => new MediaWiki\Auth\AuthManagerAuthPlugin,
|
2016-02-17 09:09:32 +00:00
|
|
|
'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
|
|
|
}
|
|
|
|
|
|
2014-12-09 20:35:40 +00:00
|
|
|
protected function tearDown() {
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 06:45:19 +00:00
|
|
|
/**
|
|
|
|
|
* Edits or creates a page/revision
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param string $pageName Page title
|
|
|
|
|
* @param string $text Content of the page
|
|
|
|
|
* @param string $summary Optional summary string for the revision
|
|
|
|
|
* @param int $defaultNs Optional namespace id
|
2014-08-13 17:54:49 +00:00
|
|
|
* @return array Array as returned by WikiPage::doEditContent()
|
2013-02-08 06:45:19 +00:00
|
|
|
*/
|
|
|
|
|
protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
|
|
|
|
|
$title = Title::newFromText( $pageName, $defaultNs );
|
|
|
|
|
$page = WikiPage::factory( $title );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2013-02-08 06:45:19 +00:00
|
|
|
return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2014-04-24 15:05:10 +00:00
|
|
|
protected function doApiRequest( array $params, array $session = null,
|
|
|
|
|
$appendModule = false, User $user = null
|
|
|
|
|
) {
|
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
|
|
|
}
|
|
|
|
|
|
2012-11-01 17:36:10 +00:00
|
|
|
// set up global environment
|
2012-06-22 20:42:42 +00:00
|
|
|
if ( $user ) {
|
|
|
|
|
$wgUser = $user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$wgRequest = new FauxRequest( $params, true, $session );
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add an edit token to the API request
|
2014-04-24 15:05:10 +00:00
|
|
|
* This is cheating a bit -- we grab a token in the correct format and then
|
|
|
|
|
* add it to the pseudo-session and to the request, without actually
|
|
|
|
|
* requesting a "real" edit token.
|
|
|
|
|
*
|
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
|
2014-08-13 17:54:49 +00:00
|
|
|
* @return array Result of the API call
|
2014-07-24 12:55:43 +00:00
|
|
|
* @throws Exception In case wsToken is not set in the session
|
2011-04-06 19:50:54 +00:00
|
|
|
*/
|
2014-04-24 15:05:10 +00:00
|
|
|
protected function doApiRequestWithToken( array $params, array $session = null,
|
|
|
|
|
User $user = null
|
|
|
|
|
) {
|
2012-06-22 22:04:09 +00:00
|
|
|
global $wgRequest;
|
|
|
|
|
|
|
|
|
|
if ( $session === null ) {
|
|
|
|
|
$session = $wgRequest->getSessionArray();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-13 17:47:57 +00:00
|
|
|
if ( isset( $session['wsToken'] ) && $session['wsToken'] ) {
|
2014-08-26 18:12:32 +00:00
|
|
|
// @todo Why does this directly mess with the session? Fix that.
|
2011-04-06 19:50:54 +00:00
|
|
|
// add edit token to fake session
|
2016-02-01 20:44:03 +00:00
|
|
|
$session['wsTokenSecrets']['default'] = $session['wsToken'];
|
2011-04-06 19:50:54 +00:00
|
|
|
// add token to request parameters
|
2014-08-26 18:12:32 +00:00
|
|
|
$timestamp = wfTimestamp();
|
|
|
|
|
$params['token'] = hash_hmac( 'md5', $timestamp, $session['wsToken'] ) .
|
|
|
|
|
dechex( $timestamp ) .
|
2016-02-01 20:44:03 +00:00
|
|
|
MediaWiki\Session\Token::SUFFIX;
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-10-27 18:46:40 +00:00
|
|
|
return $this->doApiRequest( $params, $session, false, $user );
|
2011-04-06 19:50:54 +00:00
|
|
|
} else {
|
2014-08-13 17:47:57 +00:00
|
|
|
throw new Exception( "Session token not available" );
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
protected function doLogin( $testUser = 'sysop' ) {
|
|
|
|
|
if ( $testUser === null ) {
|
|
|
|
|
$testUser = static::getTestSysop();
|
|
|
|
|
} elseif ( is_string( $testUser ) && array_key_exists( $testUser, self::$users ) ) {
|
|
|
|
|
$testUser = self::$users[ $testUser ];
|
|
|
|
|
} elseif ( !$testUser instanceof TestUser ) {
|
|
|
|
|
throw new MWException( "Can not log in to undefined user $testUser" );
|
2013-08-05 18:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$data = $this->doApiRequest( [
|
2011-07-01 16:34:02 +00:00
|
|
|
'action' => 'login',
|
2016-05-06 15:06:46 +00:00
|
|
|
'lgname' => $testUser->getUser()->getName(),
|
|
|
|
|
'lgpassword' => $testUser->getPassword() ] );
|
2011-07-01 16:34:02 +00:00
|
|
|
|
|
|
|
|
$token = $data[0]['login']['token'];
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
$data = $this->doApiRequest(
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2013-02-14 11:56:23 +00:00
|
|
|
'action' => 'login',
|
|
|
|
|
'lgtoken' => $token,
|
2016-05-06 15:06:46 +00:00
|
|
|
'lgname' => $testUser->getUser()->getName(),
|
|
|
|
|
'lgpassword' => $testUser->getPassword(),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2013-02-14 11:56:23 +00:00
|
|
|
$data[2]
|
|
|
|
|
);
|
2011-07-01 16:34:02 +00:00
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
if ( $data[0]['login']['result'] === 'Success' ) {
|
|
|
|
|
// DWIM
|
|
|
|
|
global $wgUser;
|
2016-05-18 09:19:20 +00:00
|
|
|
$wgUser = $testUser->getUser();
|
2016-01-11 18:20:22 +00:00
|
|
|
RequestContext::getMain()->setUser( $wgUser );
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-01 16:34:02 +00:00
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
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 ),
|
|
|
|
|
Language::factory( 'en' ),
|
|
|
|
|
'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() {
|
2017-03-07 02:14:14 +00:00
|
|
|
$groups = PHPUnit_Util_Test::getGroups( static::class );
|
2013-01-18 19:02:28 +00:00
|
|
|
$constraint = PHPUnit_Framework_Assert::logicalOr(
|
|
|
|
|
$this->contains( 'medium' ),
|
|
|
|
|
$this->contains( 'large' )
|
|
|
|
|
);
|
|
|
|
|
$this->assertThat( $groups, $constraint,
|
|
|
|
|
'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
|
|
|
|
|
);
|
|
|
|
|
}
|
2011-07-01 16:34:02 +00:00
|
|
|
}
|