3 abstract class ApiTestCase
extends MediaWikiLangTestCase
{
4 protected static $apiUrl;
14 protected $tablesUsed = [ 'user', 'user_groups', 'user_properties' ];
16 protected function setUp() {
17 global $wgServer, $wgDisableAuthManager;
20 self
::$apiUrl = $wgServer . wfScript( 'api' );
22 ApiQueryInfo
::resetTokenCache(); // tokens are invalid because we cleared the session
25 'sysop' => new TestUser(
28 'api_test_sysop@example.com',
31 'uploader' => new TestUser(
34 'api_test_user@example.com',
39 $this->setMwGlobals( [
40 'wgAuth' => $wgDisableAuthManager ?
new AuthPlugin
: new MediaWiki\Auth\AuthManagerAuthPlugin
,
41 'wgRequest' => new FauxRequest( [] ),
42 'wgUser' => self
::$users['sysop']->user
,
45 $this->apiContext
= new ApiTestContext();
48 protected function tearDown() {
49 // Avoid leaking session over tests
50 MediaWiki\Session\SessionManager
::getGlobalSession()->clear();
56 * Edits or creates a page/revision
57 * @param string $pageName Page title
58 * @param string $text Content of the page
59 * @param string $summary Optional summary string for the revision
60 * @param int $defaultNs Optional namespace id
61 * @return array Array as returned by WikiPage::doEditContent()
63 protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN
) {
64 $title = Title
::newFromText( $pageName, $defaultNs );
65 $page = WikiPage
::factory( $title );
67 return $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $summary );
71 * Does the API request and returns the result.
73 * The returned value is an array containing
74 * - the result data (array)
75 * - the request (WebRequest)
76 * - the session data of the request (array)
77 * - if $appendModule is true, the Api module $module
79 * @param array $params
80 * @param array|null $session
81 * @param bool $appendModule
82 * @param User|null $user
86 protected function doApiRequest( array $params, array $session = null,
87 $appendModule = false, User
$user = null
89 global $wgRequest, $wgUser;
91 if ( is_null( $session ) ) {
92 // re-use existing global session by default
93 $session = $wgRequest->getSessionArray();
96 // set up global environment
101 $wgRequest = new FauxRequest( $params, true, $session );
102 RequestContext
::getMain()->setRequest( $wgRequest );
103 RequestContext
::getMain()->setUser( $wgUser );
104 MediaWiki\Auth\AuthManager
::resetCache();
106 // set up local environment
107 $context = $this->apiContext
->newTestContext( $wgRequest, $wgUser );
109 $module = new ApiMain( $context, true );
116 $module->getResult()->getResultData( null, [ 'Strip' => 'all' ] ),
117 $context->getRequest(),
118 $context->getRequest()->getSessionArray()
121 if ( $appendModule ) {
122 $results[] = $module;
129 * Add an edit token to the API request
130 * This is cheating a bit -- we grab a token in the correct format and then
131 * add it to the pseudo-session and to the request, without actually
132 * requesting a "real" edit token.
134 * @param array $params Key-value API params
135 * @param array|null $session Session array
136 * @param User|null $user A User object for the context
137 * @return array Result of the API call
138 * @throws Exception In case wsToken is not set in the session
140 protected function doApiRequestWithToken( array $params, array $session = null,
145 if ( $session === null ) {
146 $session = $wgRequest->getSessionArray();
149 if ( isset( $session['wsToken'] ) && $session['wsToken'] ) {
150 // @todo Why does this directly mess with the session? Fix that.
151 // add edit token to fake session
152 $session['wsTokenSecrets']['default'] = $session['wsToken'];
153 // add token to request parameters
154 $timestamp = wfTimestamp();
155 $params['token'] = hash_hmac( 'md5', $timestamp, $session['wsToken'] ) .
156 dechex( $timestamp ) .
157 MediaWiki\Session\Token
::SUFFIX
;
159 return $this->doApiRequest( $params, $session, false, $user );
161 throw new Exception( "Session token not available" );
165 protected function doLogin( $user = 'sysop' ) {
166 if ( !array_key_exists( $user, self
::$users ) ) {
167 throw new MWException( "Can not log in to undefined user $user" );
170 $data = $this->doApiRequest( [
172 'lgname' => self
::$users[$user]->username
,
173 'lgpassword' => self
::$users[$user]->password
] );
175 $token = $data[0]['login']['token'];
177 $data = $this->doApiRequest(
181 'lgname' => self
::$users[$user]->username
,
182 'lgpassword' => self
::$users[$user]->password
,
187 if ( $data[0]['login']['result'] === 'Success' ) {
190 $wgUser = self
::$users[$user]->getUser();
191 RequestContext
::getMain()->setUser( $wgUser );
197 protected function getTokenList( $user, $session = null ) {
198 $data = $this->doApiRequest( [
199 'action' => 'tokens',
200 'type' => 'edit|delete|protect|move|block|unblock|watch'
201 ], $session, false, $user->user
);
203 if ( !array_key_exists( 'tokens', $data[0] ) ) {
204 throw new MWException( 'Api failed to return a token list' );
207 return $data[0]['tokens'];
210 public function testApiTestGroup() {
211 $groups = PHPUnit_Util_Test
::getGroups( get_class( $this ) );
212 $constraint = PHPUnit_Framework_Assert
::logicalOr(
213 $this->contains( 'medium' ),
214 $this->contains( 'large' )
216 $this->assertThat( $groups, $constraint,
217 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'