test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / api / ApiTestCase.php
blob1559bef103a1bf80b3ea706c5029f71c61db53d8
1 <?php
3 abstract class ApiTestCase extends MediaWikiLangTestCase {
4 protected static $apiUrl;
6 /**
7 * @var ApiTestContext
8 */
9 protected $apiContext;
11 protected function setUp() {
12 global $wgServer;
14 parent::setUp();
15 self::$apiUrl = $wgServer . wfScript( 'api' );
17 ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
19 self::$users = array(
20 'sysop' => new TestUser(
21 'Apitestsysop',
22 'Api Test Sysop',
23 'api_test_sysop@example.com',
24 array( 'sysop' )
26 'uploader' => new TestUser(
27 'Apitestuser',
28 'Api Test User',
29 'api_test_user@example.com',
30 array()
34 $this->setMwGlobals( array(
35 'wgMemc' => new EmptyBagOStuff(),
36 'wgAuth' => new StubObject( 'wgAuth', 'AuthPlugin' ),
37 'wgRequest' => new FauxRequest( array() ),
38 'wgUser' => self::$users['sysop']->user,
39 ) );
41 $this->apiContext = new ApiTestContext();
44 /**
45 * Edits or creates a page/revision
46 * @param $pageName string page title
47 * @param $text string content of the page
48 * @param $summary string optional summary string for the revision
49 * @param $defaultNs int optional namespace id
50 * @return array as returned by WikiPage::doEditContent()
52 protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
53 $title = Title::newFromText( $pageName, $defaultNs );
54 $page = WikiPage::factory( $title );
56 return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
59 /**
60 * Does the API request and returns the result.
62 * The returned value is an array containing
63 * - the result data (array)
64 * - the request (WebRequest)
65 * - the session data of the request (array)
66 * - if $appendModule is true, the Api module $module
68 * @param array $params
69 * @param array|null $session
70 * @param bool $appendModule
71 * @param User|null $user
73 * @return array
75 protected function doApiRequest( array $params, array $session = null, $appendModule = false, User $user = null ) {
76 global $wgRequest, $wgUser;
78 if ( is_null( $session ) ) {
79 // re-use existing global session by default
80 $session = $wgRequest->getSessionArray();
83 // set up global environment
84 if ( $user ) {
85 $wgUser = $user;
88 $wgRequest = new FauxRequest( $params, true, $session );
89 RequestContext::getMain()->setRequest( $wgRequest );
91 // set up local environment
92 $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
94 $module = new ApiMain( $context, true );
96 // run it!
97 $module->execute();
99 // construct result
100 $results = array(
101 $module->getResultData(),
102 $context->getRequest(),
103 $context->getRequest()->getSessionArray()
106 if ( $appendModule ) {
107 $results[] = $module;
110 return $results;
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 add it to the pseudo-session and to the
116 * request, without actually requesting a "real" edit token
117 * @param $params Array: key-value API params
118 * @param $session Array|null: session array
119 * @param $user User|null A User object for the context
120 * @return result of the API call
121 * @throws Exception in case wsToken is not set in the session
123 protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) {
124 global $wgRequest;
126 if ( $session === null ) {
127 $session = $wgRequest->getSessionArray();
130 if ( $session['wsToken'] ) {
131 // add edit token to fake session
132 $session['wsEditToken'] = $session['wsToken'];
133 // add token to request parameters
134 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
136 return $this->doApiRequest( $params, $session, false, $user );
137 } else {
138 throw new Exception( "request data not in right format" );
142 protected function doLogin() {
143 $data = $this->doApiRequest( array(
144 'action' => 'login',
145 'lgname' => self::$users['sysop']->username,
146 'lgpassword' => self::$users['sysop']->password ) );
148 $token = $data[0]['login']['token'];
150 $data = $this->doApiRequest(
151 array(
152 'action' => 'login',
153 'lgtoken' => $token,
154 'lgname' => self::$users['sysop']->username,
155 'lgpassword' => self::$users['sysop']->password,
157 $data[2]
160 return $data;
163 protected function getTokenList( $user, $session = null ) {
164 $data = $this->doApiRequest( array(
165 'action' => 'query',
166 'titles' => 'Main Page',
167 'intoken' => 'edit|delete|protect|move|block|unblock|watch',
168 'prop' => 'info' ), $session, false, $user->user );
170 return $data;
173 public function testApiTestGroup() {
174 $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
175 $constraint = PHPUnit_Framework_Assert::logicalOr(
176 $this->contains( 'medium' ),
177 $this->contains( 'large' )
179 $this->assertThat( $groups, $constraint,
180 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
185 class UserWrapper {
186 public $userName;
187 public $password;
188 public $user;
190 public function __construct( $userName, $password, $group = '' ) {
191 $this->userName = $userName;
192 $this->password = $password;
194 $this->user = User::newFromName( $this->userName );
195 if ( !$this->user->getID() ) {
196 $this->user = User::createNew( $this->userName, array(
197 "email" => "test@example.com",
198 "real_name" => "Test User" ) );
200 $this->user->setPassword( $this->password );
202 if ( $group !== '' ) {
203 $this->user->addGroup( $group );
205 $this->user->saveSettings();
209 class MockApi extends ApiBase {
210 public function execute() {
213 public function getVersion() {
216 public function __construct() {
219 public function getAllowedParams() {
220 return array(
221 'filename' => null,
222 'enablechunks' => false,
223 'sessionkey' => null,
228 class ApiTestContext extends RequestContext {
231 * Returns a DerivativeContext with the request variables in place
233 * @param $request WebRequest request object including parameters and session
234 * @param $user User or null
235 * @return DerivativeContext
237 public function newTestContext( WebRequest $request, User $user = null ) {
238 $context = new DerivativeContext( $this );
239 $context->setRequest( $request );
240 if ( $user !== null ) {
241 $context->setUser( $user );
244 return $context;