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
|
|
|
|
2011-10-27 01:06:50 +00:00
|
|
|
/**
|
|
|
|
|
* @var ApiTestContext
|
|
|
|
|
*/
|
|
|
|
|
protected $apiContext;
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2013-02-08 17:57:16 +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
|
|
|
|
|
|
2011-04-06 19:50:54 +00:00
|
|
|
self::$users = array(
|
2012-09-11 00:07:18 +00:00
|
|
|
'sysop' => new TestUser(
|
2011-04-06 19:50:54 +00:00
|
|
|
'Apitestsysop',
|
|
|
|
|
'Api Test Sysop',
|
2011-12-21 22:22:01 +00:00
|
|
|
'api_test_sysop@example.com',
|
2011-04-06 19:50:54 +00:00
|
|
|
array( 'sysop' )
|
|
|
|
|
),
|
2012-09-11 00:07:18 +00:00
|
|
|
'uploader' => new TestUser(
|
2011-04-06 19:50:54 +00:00
|
|
|
'Apitestuser',
|
|
|
|
|
'Api Test User',
|
2011-12-21 22:22:01 +00:00
|
|
|
'api_test_user@example.com',
|
2011-04-06 19:50:54 +00:00
|
|
|
array()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2013-02-08 17:57:16 +00:00
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgMemc' => new EmptyBagOStuff(),
|
|
|
|
|
'wgAuth' => new StubObject( 'wgAuth', 'AuthPlugin' ),
|
|
|
|
|
'wgRequest' => new FauxRequest( array() ),
|
|
|
|
|
'wgUser' => self::$users['sysop']->user,
|
|
|
|
|
) );
|
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
|
|
|
}
|
|
|
|
|
|
2013-02-08 06:45:19 +00:00
|
|
|
/**
|
|
|
|
|
* Edits or creates a page/revision
|
2014-04-17 18:43:42 +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
|
|
|
|
|
* @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 );
|
|
|
|
|
|
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
|
2011-10-27 01:06:50 +00:00
|
|
|
$results = array(
|
|
|
|
|
$module->getResultData(),
|
|
|
|
|
$context->getRequest(),
|
|
|
|
|
$context->getRequest()->getSessionArray()
|
|
|
|
|
);
|
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
|
|
|
|
|
* @param array|null $session session array
|
|
|
|
|
* @param User|null $user A User object for the context
|
|
|
|
|
* @return mixed Result of the API call
|
2013-02-08 06:45:19 +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();
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-06 19:50:54 +00:00
|
|
|
if ( $session['wsToken'] ) {
|
|
|
|
|
// add edit token to fake session
|
|
|
|
|
$session['wsEditToken'] = $session['wsToken'];
|
|
|
|
|
// add token to request parameters
|
|
|
|
|
$params['token'] = md5( $session['wsToken'] ) . User::EDIT_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 {
|
|
|
|
|
throw new Exception( "request data not in right format" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-05 18:15:49 +00:00
|
|
|
protected function doLogin( $user = 'sysop' ) {
|
2013-08-24 15:06:25 +00:00
|
|
|
if ( !array_key_exists( $user, self::$users ) ) {
|
2013-08-05 18:15:49 +00:00
|
|
|
throw new MWException( "Can not log in to undefined user $user" );
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-01 16:34:02 +00:00
|
|
|
$data = $this->doApiRequest( array(
|
|
|
|
|
'action' => 'login',
|
2014-07-21 12:47:42 +00:00
|
|
|
'lgname' => self::$users[$user]->username,
|
|
|
|
|
'lgpassword' => self::$users[$user]->password ) );
|
2011-07-01 16:34:02 +00:00
|
|
|
|
|
|
|
|
$token = $data[0]['login']['token'];
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
$data = $this->doApiRequest(
|
|
|
|
|
array(
|
|
|
|
|
'action' => 'login',
|
|
|
|
|
'lgtoken' => $token,
|
2014-07-21 12:47:42 +00:00
|
|
|
'lgname' => self::$users[$user]->username,
|
|
|
|
|
'lgpassword' => self::$users[$user]->password,
|
2013-02-14 11:56:23 +00:00
|
|
|
),
|
|
|
|
|
$data[2]
|
|
|
|
|
);
|
2011-07-01 16:34:02 +00:00
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-22 20:42:42 +00:00
|
|
|
protected function getTokenList( $user, $session = null ) {
|
2011-07-01 16:34:02 +00:00
|
|
|
$data = $this->doApiRequest( array(
|
2013-08-05 19:31:45 +00:00
|
|
|
'action' => 'tokens',
|
|
|
|
|
'type' => 'edit|delete|protect|move|block|unblock|watch'
|
|
|
|
|
), $session, false, $user->user );
|
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
|
|
|
|
|
|
|
|
public function testApiTestGroup() {
|
|
|
|
|
$groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
|
|
|
|
|
$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
|
|
|
}
|