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' => new TestUser(
23 'api_test_sysop@example.com',
26 'uploader' => new TestUser(
29 'api_test_user@example.com',
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
,
41 $this->apiContext
= new ApiTestContext();
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 );
55 return $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $summary );
59 * Does the API request and returns the result.
61 * The returned value is an array containing
62 * - the result data (array)
63 * - the request (WebRequest)
64 * - the session data of the request (array)
65 * - if $appendModule is true, the Api module $module
67 * @param array $params
68 * @param array|null $session
69 * @param bool $appendModule
70 * @param User|null $user
74 protected function doApiRequest( array $params, array $session = null, $appendModule = false, User
$user = null ) {
75 global $wgRequest, $wgUser;
77 if ( is_null( $session ) ) {
78 // re-use existing global session by default
79 $session = $wgRequest->getSessionArray();
82 // set up global environment
87 $wgRequest = new FauxRequest( $params, true, $session );
88 RequestContext
::getMain()->setRequest( $wgRequest );
90 // set up local environment
91 $context = $this->apiContext
->newTestContext( $wgRequest, $wgUser );
93 $module = new ApiMain( $context, true );
100 $module->getResultData(),
101 $context->getRequest(),
102 $context->getRequest()->getSessionArray()
105 if ( $appendModule ) {
106 $results[] = $module;
113 * Add an edit token to the API request
114 * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
115 * request, without actually requesting a "real" edit token
116 * @param $params Array: key-value API params
117 * @param $session Array|null: session array
118 * @param $user User|null A User object for the context
119 * @return result of the API call
120 * @throws Exception in case wsToken is not set in the session
122 protected function doApiRequestWithToken( array $params, array $session = null, User
$user = null ) {
125 if ( $session === null ) {
126 $session = $wgRequest->getSessionArray();
129 if ( $session['wsToken'] ) {
130 // add edit token to fake session
131 $session['wsEditToken'] = $session['wsToken'];
132 // add token to request parameters
133 $params['token'] = md5( $session['wsToken'] ) . User
::EDIT_TOKEN_SUFFIX
;
134 return $this->doApiRequest( $params, $session, false, $user );
136 throw new Exception( "request data not in right format" );
140 protected function doLogin() {
141 $data = $this->doApiRequest( array(
143 'lgname' => self
::$users['sysop']->username
,
144 'lgpassword' => self
::$users['sysop']->password
) );
146 $token = $data[0]['login']['token'];
148 $data = $this->doApiRequest(
152 'lgname' => self
::$users['sysop']->username
,
153 'lgpassword' => self
::$users['sysop']->password
,
161 protected function getTokenList( $user, $session = null ) {
162 $data = $this->doApiRequest( array(
164 'titles' => 'Main Page',
165 'intoken' => 'edit|delete|protect|move|block|unblock|watch',
166 'prop' => 'info' ), $session, false, $user->user
);
170 public function testApiTestGroup() {
171 $groups = PHPUnit_Util_Test
::getGroups( get_class( $this ) );
172 $constraint = PHPUnit_Framework_Assert
::logicalOr(
173 $this->contains( 'medium' ),
174 $this->contains( 'large' )
176 $this->assertThat( $groups, $constraint,
177 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
187 public function __construct( $userName, $password, $group = '' ) {
188 $this->userName
= $userName;
189 $this->password
= $password;
191 $this->user
= User
::newFromName( $this->userName
);
192 if ( !$this->user
->getID() ) {
193 $this->user
= User
::createNew( $this->userName
, array(
194 "email" => "test@example.com",
195 "real_name" => "Test User" ) );
197 $this->user
->setPassword( $this->password
);
199 if ( $group !== '' ) {
200 $this->user
->addGroup( $group );
202 $this->user
->saveSettings();
206 class MockApi
extends ApiBase
{
207 public function execute() {}
209 public function getVersion() {}
211 public function __construct() {}
213 public function getAllowedParams() {
216 'enablechunks' => false,
217 'sessionkey' => null,
222 class ApiTestContext
extends RequestContext
{
225 * Returns a DerivativeContext with the request variables in place
227 * @param $request WebRequest request object including parameters and session
228 * @param $user User or null
229 * @return DerivativeContext
231 public function newTestContext( WebRequest
$request, User
$user = null ) {
232 $context = new DerivativeContext( $this );
233 $context->setRequest( $request );
234 if ( $user !== null ) {
235 $context->setUser( $user );