Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / api / ApiTestCase.php
blob8c27b1018a2cf956ff199db3a6152dcda8ac1358
1 <?php
3 abstract class ApiTestCase extends MediaWikiLangTestCase {
4 protected static $apiUrl;
6 /**
7 * @var ApiTestContext
8 */
9 protected $apiContext;
11 /**
12 * @var array
14 protected $tablesUsed = array( 'user', 'user_groups', 'user_properties' );
16 protected function setUp() {
17 global $wgServer;
19 parent::setUp();
20 self::$apiUrl = $wgServer . wfScript( 'api' );
22 ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
24 self::$users = array(
25 'sysop' => new TestUser(
26 'Apitestsysop',
27 'Api Test Sysop',
28 'api_test_sysop@example.com',
29 array( 'sysop' )
31 'uploader' => new TestUser(
32 'Apitestuser',
33 'Api Test User',
34 'api_test_user@example.com',
35 array()
39 $this->setMwGlobals( array(
40 'wgMemc' => new EmptyBagOStuff(),
41 'wgAuth' => new StubObject( 'wgAuth', 'AuthPlugin' ),
42 'wgRequest' => new FauxRequest( array() ),
43 'wgUser' => self::$users['sysop']->user,
44 ) );
46 $this->apiContext = new ApiTestContext();
49 protected function tearDown() {
50 // Avoid leaking session over tests
51 if ( session_id() != '' ) {
52 global $wgUser;
53 $wgUser->logout();
54 session_destroy();
57 parent::tearDown();
60 /**
61 * Edits or creates a page/revision
62 * @param string $pageName Page title
63 * @param string $text Content of the page
64 * @param string $summary Optional summary string for the revision
65 * @param int $defaultNs Optional namespace id
66 * @return array Array as returned by WikiPage::doEditContent()
68 protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
69 $title = Title::newFromText( $pageName, $defaultNs );
70 $page = WikiPage::factory( $title );
72 return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
75 /**
76 * Does the API request and returns the result.
78 * The returned value is an array containing
79 * - the result data (array)
80 * - the request (WebRequest)
81 * - the session data of the request (array)
82 * - if $appendModule is true, the Api module $module
84 * @param array $params
85 * @param array|null $session
86 * @param bool $appendModule
87 * @param User|null $user
89 * @return array
91 protected function doApiRequest( array $params, array $session = null,
92 $appendModule = false, User $user = null
93 ) {
94 global $wgRequest, $wgUser;
96 if ( is_null( $session ) ) {
97 // re-use existing global session by default
98 $session = $wgRequest->getSessionArray();
101 // set up global environment
102 if ( $user ) {
103 $wgUser = $user;
106 $wgRequest = new FauxRequest( $params, true, $session );
107 RequestContext::getMain()->setRequest( $wgRequest );
109 // set up local environment
110 $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
112 $module = new ApiMain( $context, true );
114 // run it!
115 $module->execute();
117 // construct result
118 $results = array(
119 $module->getResultData(),
120 $context->getRequest(),
121 $context->getRequest()->getSessionArray()
124 if ( $appendModule ) {
125 $results[] = $module;
128 return $results;
132 * Add an edit token to the API request
133 * This is cheating a bit -- we grab a token in the correct format and then
134 * add it to the pseudo-session and to the request, without actually
135 * requesting a "real" edit token.
137 * @param array $params Key-value API params
138 * @param array|null $session Session array
139 * @param User|null $user A User object for the context
140 * @return array Result of the API call
141 * @throws Exception In case wsToken is not set in the session
143 protected function doApiRequestWithToken( array $params, array $session = null,
144 User $user = null
146 global $wgRequest;
148 if ( $session === null ) {
149 $session = $wgRequest->getSessionArray();
152 if ( isset( $session['wsToken'] ) && $session['wsToken'] ) {
153 // @todo Why does this directly mess with the session? Fix that.
154 // add edit token to fake session
155 $session['wsEditToken'] = $session['wsToken'];
156 // add token to request parameters
157 $timestamp = wfTimestamp();
158 $params['token'] = hash_hmac( 'md5', $timestamp, $session['wsToken'] ) .
159 dechex( $timestamp ) .
160 User::EDIT_TOKEN_SUFFIX;
162 return $this->doApiRequest( $params, $session, false, $user );
163 } else {
164 throw new Exception( "Session token not available" );
168 protected function doLogin( $user = 'sysop' ) {
169 if ( !array_key_exists( $user, self::$users ) ) {
170 throw new MWException( "Can not log in to undefined user $user" );
173 $data = $this->doApiRequest( array(
174 'action' => 'login',
175 'lgname' => self::$users[$user]->username,
176 'lgpassword' => self::$users[$user]->password ) );
178 $token = $data[0]['login']['token'];
180 $data = $this->doApiRequest(
181 array(
182 'action' => 'login',
183 'lgtoken' => $token,
184 'lgname' => self::$users[$user]->username,
185 'lgpassword' => self::$users[$user]->password,
187 $data[2]
190 return $data;
193 protected function getTokenList( $user, $session = null ) {
194 $data = $this->doApiRequest( array(
195 'action' => 'tokens',
196 'type' => 'edit|delete|protect|move|block|unblock|watch'
197 ), $session, false, $user->user );
199 if ( !array_key_exists( 'tokens', $data[0] ) ) {
200 throw new MWException( 'Api failed to return a token list' );
203 return $data[0]['tokens'];
206 public function testApiTestGroup() {
207 $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
208 $constraint = PHPUnit_Framework_Assert::logicalOr(
209 $this->contains( 'medium' ),
210 $this->contains( 'large' )
212 $this->assertThat( $groups, $constraint,
213 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'