3 abstract class ApiTestCase
extends MediaWikiLangTestCase
{
4 protected static $apiUrl;
11 protected function setUp() {
15 self
::$apiUrl = $wgServer . wfScript( 'api' );
17 ApiQueryInfo
::resetTokenCache(); // tokens are invalid because we cleared the session
20 'sysop' => static::getTestSysop(),
21 'uploader' => static::getTestUser(),
24 $this->setMwGlobals( [
25 'wgAuth' => new MediaWiki\Auth\AuthManagerAuthPlugin
,
26 'wgRequest' => new FauxRequest( [] ),
27 'wgUser' => self
::$users['sysop']->getUser(),
30 $this->apiContext
= new ApiTestContext();
33 protected function tearDown() {
34 // Avoid leaking session over tests
35 MediaWiki\Session\SessionManager
::getGlobalSession()->clear();
41 * Edits or creates a page/revision
42 * @param string $pageName Page title
43 * @param string $text Content of the page
44 * @param string $summary Optional summary string for the revision
45 * @param int $defaultNs Optional namespace id
46 * @return array Array as returned by WikiPage::doEditContent()
48 protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN
) {
49 $title = Title
::newFromText( $pageName, $defaultNs );
50 $page = WikiPage
::factory( $title );
52 return $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $summary );
56 * Does the API request and returns the result.
58 * The returned value is an array containing
59 * - the result data (array)
60 * - the request (WebRequest)
61 * - the session data of the request (array)
62 * - if $appendModule is true, the Api module $module
64 * @param array $params
65 * @param array|null $session
66 * @param bool $appendModule
67 * @param User|null $user
71 protected function doApiRequest( array $params, array $session = null,
72 $appendModule = false, User
$user = null
74 global $wgRequest, $wgUser;
76 if ( is_null( $session ) ) {
77 // re-use existing global session by default
78 $session = $wgRequest->getSessionArray();
81 // set up global environment
86 $wgRequest = new FauxRequest( $params, true, $session );
87 RequestContext
::getMain()->setRequest( $wgRequest );
88 RequestContext
::getMain()->setUser( $wgUser );
89 MediaWiki\Auth\AuthManager
::resetCache();
91 // set up local environment
92 $context = $this->apiContext
->newTestContext( $wgRequest, $wgUser );
94 $module = new ApiMain( $context, true );
101 $module->getResult()->getResultData( null, [ 'Strip' => 'all' ] ),
102 $context->getRequest(),
103 $context->getRequest()->getSessionArray()
106 if ( $appendModule ) {
107 $results[] = $module;
114 * Add an edit token to the API request
115 * This is cheating a bit -- we grab a token in the correct format and then
116 * add it to the pseudo-session and to the request, without actually
117 * requesting a "real" edit token.
119 * @param array $params Key-value API params
120 * @param array|null $session Session array
121 * @param User|null $user A User object for the context
122 * @return array Result of the API call
123 * @throws Exception In case wsToken is not set in the session
125 protected function doApiRequestWithToken( array $params, array $session = null,
130 if ( $session === null ) {
131 $session = $wgRequest->getSessionArray();
134 if ( isset( $session['wsToken'] ) && $session['wsToken'] ) {
135 // @todo Why does this directly mess with the session? Fix that.
136 // add edit token to fake session
137 $session['wsTokenSecrets']['default'] = $session['wsToken'];
138 // add token to request parameters
139 $timestamp = wfTimestamp();
140 $params['token'] = hash_hmac( 'md5', $timestamp, $session['wsToken'] ) .
141 dechex( $timestamp ) .
142 MediaWiki\Session\Token
::SUFFIX
;
144 return $this->doApiRequest( $params, $session, false, $user );
146 throw new Exception( "Session token not available" );
150 protected function doLogin( $testUser = 'sysop' ) {
151 if ( $testUser === null ) {
152 $testUser = static::getTestSysop();
153 } elseif ( is_string( $testUser ) && array_key_exists( $testUser, self
::$users ) ) {
154 $testUser = self
::$users[ $testUser ];
155 } elseif ( !$testUser instanceof TestUser
) {
156 throw new MWException( "Can not log in to undefined user $testUser" );
159 $data = $this->doApiRequest( [
161 'lgname' => $testUser->getUser()->getName(),
162 'lgpassword' => $testUser->getPassword() ] );
164 $token = $data[0]['login']['token'];
166 $data = $this->doApiRequest(
170 'lgname' => $testUser->getUser()->getName(),
171 'lgpassword' => $testUser->getPassword(),
176 if ( $data[0]['login']['result'] === 'Success' ) {
179 $wgUser = $testUser->getUser();
180 RequestContext
::getMain()->setUser( $wgUser );
186 protected function getTokenList( TestUser
$user, $session = null ) {
187 $data = $this->doApiRequest( [
188 'action' => 'tokens',
189 'type' => 'edit|delete|protect|move|block|unblock|watch'
190 ], $session, false, $user->getUser() );
192 if ( !array_key_exists( 'tokens', $data[0] ) ) {
193 throw new MWException( 'Api failed to return a token list' );
196 return $data[0]['tokens'];
199 public function testApiTestGroup() {
200 $groups = PHPUnit_Util_Test
::getGroups( get_class( $this ) );
201 $constraint = PHPUnit_Framework_Assert
::logicalOr(
202 $this->contains( 'medium' ),
203 $this->contains( 'large' )
205 $this->assertThat( $groups, $constraint,
206 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'