3 abstract class ApiTestCase
extends MediaWikiLangTestCase
{
4 protected static $apiUrl;
11 protected function setUp() {
12 global $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser, $wgServer;
15 self
::$apiUrl = $wgServer . wfScript( 'api' );
16 $wgMemc = new EmptyBagOStuff();
17 $wgContLang = Language
::factory( 'en' );
18 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
19 $wgRequest = new FauxRequest( array() );
21 ApiQueryInfo
::resetTokenCache(); // tokens are invalid because we cleared the session
24 'sysop' => new TestUser(
27 'api_test_sysop@example.com',
30 'uploader' => new TestUser(
33 'api_test_user@example.com',
38 $wgUser = self
::$users['sysop']->user
;
40 $this->apiContext
= new ApiTestContext();
45 * Does the API request and returns the result.
47 * The returned value is an array containing
48 * - the result data (array)
49 * - the request (WebRequest)
50 * - the session data of the request (array)
51 * - if $appendModule is true, the Api module $module
53 * @param array $params
54 * @param array|null $session
55 * @param bool $appendModule
56 * @param User|null $user
60 protected function doApiRequest( array $params, array $session = null, $appendModule = false, User
$user = null ) {
61 global $wgRequest, $wgUser;
63 if ( is_null( $session ) ) {
64 // re-use existing global session by default
65 $session = $wgRequest->getSessionArray();
68 // set up global environment
73 $wgRequest = new FauxRequest( $params, true, $session );
74 RequestContext
::getMain()->setRequest( $wgRequest );
76 // set up local environment
77 $context = $this->apiContext
->newTestContext( $wgRequest, $wgUser );
79 $module = new ApiMain( $context, true );
86 $module->getResultData(),
87 $context->getRequest(),
88 $context->getRequest()->getSessionArray()
91 if ( $appendModule ) {
99 * Add an edit token to the API request
100 * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
101 * request, without actually requesting a "real" edit token
102 * @param $params Array: key-value API params
103 * @param $session Array|null: session array
104 * @param $user User|null A User object for the context
106 protected function doApiRequestWithToken( array $params, array $session = null, User
$user = null ) {
109 if ( $session === null ) {
110 $session = $wgRequest->getSessionArray();
113 if ( $session['wsToken'] ) {
114 // add edit token to fake session
115 $session['wsEditToken'] = $session['wsToken'];
116 // add token to request parameters
117 $params['token'] = md5( $session['wsToken'] ) . User
::EDIT_TOKEN_SUFFIX
;
118 return $this->doApiRequest( $params, $session, false, $user );
120 throw new Exception( "request data not in right format" );
124 protected function doLogin() {
125 $data = $this->doApiRequest( array(
127 'lgname' => self
::$users['sysop']->username
,
128 'lgpassword' => self
::$users['sysop']->password
) );
130 $token = $data[0]['login']['token'];
132 $data = $this->doApiRequest( array(
135 'lgname' => self
::$users['sysop']->username
,
136 'lgpassword' => self
::$users['sysop']->password
142 protected function getTokenList( $user, $session = null ) {
143 $data = $this->doApiRequest( array(
145 'titles' => 'Main Page',
146 'intoken' => 'edit|delete|protect|move|block|unblock|watch',
147 'prop' => 'info' ), $session, false, $user->user
);
151 public function testApiTestGroup() {
152 $groups = PHPUnit_Util_Test
::getGroups( get_class( $this ) );
153 $constraint = PHPUnit_Framework_Assert
::logicalOr(
154 $this->contains( 'medium' ),
155 $this->contains( 'large' )
157 $this->assertThat( $groups, $constraint,
158 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
164 public $userName, $password, $user;
166 public function __construct( $userName, $password, $group = '' ) {
167 $this->userName
= $userName;
168 $this->password
= $password;
170 $this->user
= User
::newFromName( $this->userName
);
171 if ( !$this->user
->getID() ) {
172 $this->user
= User
::createNew( $this->userName
, array(
173 "email" => "test@example.com",
174 "real_name" => "Test User" ) );
176 $this->user
->setPassword( $this->password
);
178 if ( $group !== '' ) {
179 $this->user
->addGroup( $group );
181 $this->user
->saveSettings();
185 class MockApi
extends ApiBase
{
186 public function execute() { }
187 public function getVersion() { }
189 public function __construct() { }
191 public function getAllowedParams() {
194 'enablechunks' => false,
195 'sessionkey' => null,
200 class ApiTestContext
extends RequestContext
{
203 * Returns a DerivativeContext with the request variables in place
205 * @param $request WebRequest request object including parameters and session
206 * @param $user User or null
207 * @return DerivativeContext
209 public function newTestContext( WebRequest
$request, User
$user = null ) {
210 $context = new DerivativeContext( $this );
211 $context->setRequest( $request );
212 if ( $user !== null ) {
213 $context->setUser( $user );