MediaWiki  1.23.8
ApiTestCase.php
Go to the documentation of this file.
1 <?php
2 
3 abstract class ApiTestCase extends MediaWikiLangTestCase {
4  protected static $apiUrl;
5 
9  protected $apiContext;
10 
11  protected function setUp() {
12  global $wgServer;
13 
14  parent::setUp();
15  self::$apiUrl = $wgServer . wfScript( 'api' );
16 
17  ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
18 
19  self::$users = array(
20  'sysop' => new TestUser(
21  'Apitestsysop',
22  'Api Test Sysop',
23  'api_test_sysop@example.com',
24  array( 'sysop' )
25  ),
26  'uploader' => new TestUser(
27  'Apitestuser',
28  'Api Test User',
29  'api_test_user@example.com',
30  array()
31  )
32  );
33 
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  ) );
40 
41  $this->apiContext = new ApiTestContext();
42  }
43 
52  protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
53  $title = Title::newFromText( $pageName, $defaultNs );
54  $page = WikiPage::factory( $title );
55 
56  return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
57  }
58 
75  protected function doApiRequest( array $params, array $session = null, $appendModule = false, User $user = null ) {
76  global $wgRequest, $wgUser;
77 
78  if ( is_null( $session ) ) {
79  // re-use existing global session by default
80  $session = $wgRequest->getSessionArray();
81  }
82 
83  // set up global environment
84  if ( $user ) {
85  $wgUser = $user;
86  }
87 
88  $wgRequest = new FauxRequest( $params, true, $session );
89  RequestContext::getMain()->setRequest( $wgRequest );
90 
91  // set up local environment
92  $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
93 
94  $module = new ApiMain( $context, true );
95 
96  // run it!
97  $module->execute();
98 
99  // construct result
100  $results = array(
101  $module->getResultData(),
102  $context->getRequest(),
103  $context->getRequest()->getSessionArray()
104  );
105 
106  if ( $appendModule ) {
107  $results[] = $module;
108  }
109 
110  return $results;
111  }
112 
123  protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) {
124  global $wgRequest;
125 
126  if ( $session === null ) {
127  $session = $wgRequest->getSessionArray();
128  }
129 
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;
135 
136  return $this->doApiRequest( $params, $session, false, $user );
137  } else {
138  throw new Exception( "request data not in right format" );
139  }
140  }
141 
142  protected function doLogin( $user = 'sysop' ) {
143  if ( !array_key_exists( $user, self::$users ) ) {
144  throw new MWException( "Can not log in to undefined user $user" );
145  }
146 
147  $data = $this->doApiRequest( array(
148  'action' => 'login',
149  'lgname' => self::$users[ $user ]->username,
150  'lgpassword' => self::$users[ $user ]->password ) );
151 
152  $token = $data[0]['login']['token'];
153 
154  $data = $this->doApiRequest(
155  array(
156  'action' => 'login',
157  'lgtoken' => $token,
158  'lgname' => self::$users[ $user ]->username,
159  'lgpassword' => self::$users[ $user ]->password,
160  ),
161  $data[2]
162  );
163 
164  return $data;
165  }
166 
167  protected function getTokenList( $user, $session = null ) {
168  $data = $this->doApiRequest( array(
169  'action' => 'tokens',
170  'type' => 'edit|delete|protect|move|block|unblock|watch'
171  ), $session, false, $user->user );
172 
173  if ( !array_key_exists( 'tokens', $data[0] ) ) {
174  throw new MWException( 'Api failed to return a token list' );
175  }
176 
177  return $data[0]['tokens'];
178  }
179 
180  public function testApiTestGroup() {
181  $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
182  $constraint = PHPUnit_Framework_Assert::logicalOr(
183  $this->contains( 'medium' ),
184  $this->contains( 'large' )
185  );
186  $this->assertThat( $groups, $constraint,
187  'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
188  );
189  }
190 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
$wgUser
$wgUser
Definition: Setup.php:552
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: WebRequest.php:1275
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiTestCase\getTokenList
getTokenList( $user, $session=null)
Definition: ApiTestCase.php:166
EmptyBagOStuff
A BagOStuff object with no objects in it.
Definition: EmptyBagOStuff.php:29
StubObject
Class to implement stub globals, which are globals that delay loading the their associated module cod...
Definition: StubObject.php:44
ApiQueryInfo\resetTokenCache
static resetTokenCache()
Definition: ApiQueryInfo.php:114
$params
$params
Definition: styleTest.css.php:40
ApiTestCase\testApiTestGroup
testApiTestGroup()
Definition: ApiTestCase.php:179
TestUser
Wraps the user object, so we can also retain full access to properties like password if we log in via...
Definition: TestUser.php:6
NS_MAIN
const NS_MAIN
Definition: Defines.php:79
MWException
MediaWiki exception.
Definition: MWException.php:26
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:3739
ApiTestCase\$apiUrl
static $apiUrl
Definition: ApiTestCase.php:4
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
ApiTestCase\setUp
setUp()
Definition: ApiTestCase.php:10
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:144
User\EDIT_TOKEN_SUFFIX
const EDIT_TOKEN_SUFFIX
Definition: User.php:66
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
user
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same user
Definition: distributors.txt:9
ApiTestCase
Definition: ApiTestCase.php:3
ApiTestCase\editPage
editPage( $pageName, $text, $summary='', $defaultNs=NS_MAIN)
Edits or creates a page/revision.
Definition: ApiTestCase.php:51
ApiTestContext
Definition: ApiTestContext.php:3
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:420
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:237
$summary
$summary
Definition: importImages.php:120
ApiTestCase\doLogin
doLogin( $user='sysop')
Definition: ApiTestCase.php:141
ApiTestCase\doApiRequest
doApiRequest(array $params, array $session=null, $appendModule=false, User $user=null)
Does the API request and returns the result.
Definition: ApiTestCase.php:74
ApiTestCase\$apiContext
ApiTestContext $apiContext
Definition: ApiTestCase.php:8
ApiTestCase\doApiRequestWithToken
doApiRequestWithToken(array $params, array $session=null, User $user=null)
Add an edit token to the API request This is cheating a bit – we grab a token in the correct format a...
Definition: ApiTestCase.php:122
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59