23739bf8efde5864dd69d1d0e338337ecd547f66
3 abstract class ApiTestCase
extends MediaWikiLangTestCase
{
5 * @var Array of ApiTestUser
8 protected static $apiUrl;
13 protected $apiContext;
16 global $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser, $wgServer;
19 self
::$apiUrl = $wgServer . wfScript( 'api' );
20 $wgMemc = new EmptyBagOStuff();
21 $wgContLang = Language
::factory( 'en' );
22 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
23 $wgRequest = new FauxRequest( array() );
26 'sysop' => new ApiTestUser(
29 'api_test_sysop@example.com',
32 'uploader' => new ApiTestUser(
35 'api_test_user@example.com',
40 $wgUser = self
::$users['sysop']->user
;
42 $this->apiContext
= new ApiTestContext();
46 protected function doApiRequest( $params, $session = null, $appendModule = false, $user = null ) {
47 if ( is_null( $session ) ) {
48 # use global session by default
51 $session = $wgRequest->getSessionArray();
54 $context = $this->apiContext
->newTestContext( $params, $session, $user );
55 $module = new ApiMain( $context, true );
59 $module->getResultData(),
60 $context->getRequest(),
61 $context->getRequest()->getSessionArray()
71 * Add an edit token to the API request
72 * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
73 * request, without actually requesting a "real" edit token
74 * @param $params: key-value API params
75 * @param $session: session array
76 * @param $user String|null A User object for the context
78 protected function doApiRequestWithToken( $params, $session, $user = null ) {
79 if ( $session['wsToken'] ) {
80 // add edit token to fake session
81 $session['wsEditToken'] = $session['wsToken'];
82 // add token to request parameters
83 $params['token'] = md5( $session['wsToken'] ) . User
::EDIT_TOKEN_SUFFIX
;
84 return $this->doApiRequest( $params, $session, false, $user );
86 throw new Exception( "request data not in right format" );
90 protected function doLogin() {
91 $data = $this->doApiRequest( array(
93 'lgname' => self
::$users['sysop']->username
,
94 'lgpassword' => self
::$users['sysop']->password
) );
96 $token = $data[0]['login']['token'];
98 $data = $this->doApiRequest( array(
101 'lgname' => self
::$users['sysop']->username
,
102 'lgpassword' => self
::$users['sysop']->password
108 protected function getTokenList( $user ) {
109 $data = $this->doApiRequest( array(
111 'titles' => 'Main Page',
112 'intoken' => 'edit|delete|protect|move|block|unblock',
113 'prop' => 'info' ), false, $user->user
);
119 public $userName, $password, $user;
121 public function __construct( $userName, $password, $group = '' ) {
122 $this->userName
= $userName;
123 $this->password
= $password;
125 $this->user
= User
::newFromName( $this->userName
);
126 if ( !$this->user
->getID() ) {
127 $this->user
= User
::createNew( $this->userName
, array(
128 "email" => "test@example.com",
129 "real_name" => "Test User" ) );
131 $this->user
->setPassword( $this->password
);
133 if ( $group !== '' ) {
134 $this->user
->addGroup( $group );
136 $this->user
->saveSettings();
140 class MockApi
extends ApiBase
{
141 public function execute() { }
142 public function getVersion() { }
144 public function __construct() { }
146 public function getAllowedParams() {
149 'enablechunks' => false,
150 'sessionkey' => null,
155 class ApiTestContext
extends RequestContext
{
158 * Returns a DerivativeContext with the request variables in place
160 * @param $params Array key-value API params
161 * @param $session Array session data
162 * @param $user User or null
163 * @return DerivativeContext
165 public function newTestContext( $params, $session, $user = null ) {
166 $context = new DerivativeContext( $this );
167 $context->setRequest( new FauxRequest( $params, true, $session ) );
168 if ( $user !== null ) {
169 $context->setUser( $user );