Fix session handling in API test cases.
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTestCase.php
1 <?php
2
3 abstract class ApiTestCase extends MediaWikiLangTestCase {
4 /**
5 * @var Array of ApiTestUser
6 */
7 public static $users;
8 protected static $apiUrl;
9
10 /**
11 * @var ApiTestContext
12 */
13 protected $apiContext;
14
15 function setUp() {
16 global $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser, $wgServer;
17
18 parent::setUp();
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() );
24
25 self::$users = array(
26 'sysop' => new ApiTestUser(
27 'Apitestsysop',
28 'Api Test Sysop',
29 'api_test_sysop@example.com',
30 array( 'sysop' )
31 ),
32 'uploader' => new ApiTestUser(
33 'Apitestuser',
34 'Api Test User',
35 'api_test_user@example.com',
36 array()
37 )
38 );
39
40 $wgUser = self::$users['sysop']->user;
41
42 $this->apiContext = new ApiTestContext();
43
44 }
45
46 protected function doApiRequest( $params, $session = null, $appendModule = false, $user = null ) {
47 if ( is_null( $session ) ) {
48 # use global session by default
49
50 global $wgRequest;
51 $session = $wgRequest->getSessionArray();
52 }
53
54 $context = $this->apiContext->newTestContext( $params, $session, $user );
55 $module = new ApiMain( $context, true );
56 $module->execute();
57
58 $results = array(
59 $module->getResultData(),
60 $context->getRequest(),
61 $context->getRequest()->getSessionArray()
62 );
63 if( $appendModule ) {
64 $results[] = $module;
65 }
66
67 return $results;
68 }
69
70 /**
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
77 */
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 );
85 } else {
86 throw new Exception( "request data not in right format" );
87 }
88 }
89
90 protected function doLogin() {
91 $data = $this->doApiRequest( array(
92 'action' => 'login',
93 'lgname' => self::$users['sysop']->username,
94 'lgpassword' => self::$users['sysop']->password ) );
95
96 $token = $data[0]['login']['token'];
97
98 $data = $this->doApiRequest( array(
99 'action' => 'login',
100 'lgtoken' => $token,
101 'lgname' => self::$users['sysop']->username,
102 'lgpassword' => self::$users['sysop']->password
103 ), $data );
104
105 return $data;
106 }
107
108 protected function getTokenList( $user ) {
109 $data = $this->doApiRequest( array(
110 'action' => 'query',
111 'titles' => 'Main Page',
112 'intoken' => 'edit|delete|protect|move|block|unblock',
113 'prop' => 'info' ), false, $user->user );
114 return $data;
115 }
116 }
117
118 class UserWrapper {
119 public $userName, $password, $user;
120
121 public function __construct( $userName, $password, $group = '' ) {
122 $this->userName = $userName;
123 $this->password = $password;
124
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" ) );
130 }
131 $this->user->setPassword( $this->password );
132
133 if ( $group !== '' ) {
134 $this->user->addGroup( $group );
135 }
136 $this->user->saveSettings();
137 }
138 }
139
140 class MockApi extends ApiBase {
141 public function execute() { }
142 public function getVersion() { }
143
144 public function __construct() { }
145
146 public function getAllowedParams() {
147 return array(
148 'filename' => null,
149 'enablechunks' => false,
150 'sessionkey' => null,
151 );
152 }
153 }
154
155 class ApiTestContext extends RequestContext {
156
157 /**
158 * Returns a DerivativeContext with the request variables in place
159 *
160 * @param $params Array key-value API params
161 * @param $session Array session data
162 * @param $user User or null
163 * @return DerivativeContext
164 */
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 );
170 }
171 return $context;
172 }
173 }