e23ab70cffd13fa8b44d106036819d9b626fffcc
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 ) ) {
51 $context = $this->apiContext
->newTestContext( $params, $session, $user );
52 $module = new ApiMain( $context, true );
56 $module->getResultData(),
57 $context->getRequest(),
58 $context->getRequest()->getSessionArray()
68 * Add an edit token to the API request
69 * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
70 * request, without actually requesting a "real" edit token
71 * @param $params: key-value API params
72 * @param $session: session array
74 protected function doApiRequestWithToken( $params, $session, $user = null ) {
75 if ( $session['wsToken'] ) {
76 // add edit token to fake session
77 $session['wsEditToken'] = $session['wsToken'];
78 // add token to request parameters
79 $params['token'] = md5( $session['wsToken'] ) . User
::EDIT_TOKEN_SUFFIX
;
80 return $this->doApiRequest( $params, $session, false, $user );
82 throw new Exception( "request data not in right format" );
86 protected function doLogin() {
87 $data = $this->doApiRequest( array(
89 'lgname' => self
::$users['sysop']->username
,
90 'lgpassword' => self
::$users['sysop']->password
) );
92 $token = $data[0]['login']['token'];
94 $data = $this->doApiRequest( array(
97 'lgname' => self
::$users['sysop']->username
,
98 'lgpassword' => self
::$users['sysop']->password
104 protected function getTokenList( $user ) {
105 $data = $this->doApiRequest( array(
107 'titles' => 'Main Page',
108 'intoken' => 'edit|delete|protect|move|block|unblock',
109 'prop' => 'info' ), false, $user->user
);
115 public $userName, $password, $user;
117 public function __construct( $userName, $password, $group = '' ) {
118 $this->userName
= $userName;
119 $this->password
= $password;
121 $this->user
= User
::newFromName( $this->userName
);
122 if ( !$this->user
->getID() ) {
123 $this->user
= User
::createNew( $this->userName
, array(
124 "email" => "test@example.com",
125 "real_name" => "Test User" ) );
127 $this->user
->setPassword( $this->password
);
129 if ( $group !== '' ) {
130 $this->user
->addGroup( $group );
132 $this->user
->saveSettings();
136 class MockApi
extends ApiBase
{
137 public function execute() { }
138 public function getVersion() { }
140 public function __construct() { }
142 public function getAllowedParams() {
145 'enablechunks' => false,
146 'sessionkey' => null,
151 class ApiTestContext
extends RequestContext
{
154 * Returns a DerivativeContext with the request variables in place
156 * @param $params Array key-value API params
157 * @param $session Array session data
158 * @param $user User or null
159 * @return DerivativeContext
161 public function newTestContext( $params, $session, $user = null ) {
162 $context = new DerivativeContext( $this );
163 $context->setRequest( new FauxRequest( $params, true, $session ) );
164 if ( $user !== null ) {
165 $context->setUser( $user );