Merge "Parser test to test language conversion around HTML tags."
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTestCase.php
1 <?php
2
3 abstract class ApiTestCase extends MediaWikiLangTestCase {
4 protected static $apiUrl;
5
6 /**
7 * @var ApiTestContext
8 */
9 protected $apiContext;
10
11 protected function setUp() {
12 global $wgContLang, $wgAuth, $wgMemc, $wgRequest, $wgUser, $wgServer;
13
14 parent::setUp();
15 self::$apiUrl = $wgServer . wfScript( 'api' );
16 $wgMemc = new EmptyBagOStuff();
17 $wgContLang = Language::factory( 'en' );
18 $wgAuth = new StubObject( 'wgAuth', 'AuthPlugin' );
19 $wgRequest = new FauxRequest( array() );
20
21 ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
22
23 self::$users = array(
24 'sysop' => new TestUser(
25 'Apitestsysop',
26 'Api Test Sysop',
27 'api_test_sysop@example.com',
28 array( 'sysop' )
29 ),
30 'uploader' => new TestUser(
31 'Apitestuser',
32 'Api Test User',
33 'api_test_user@example.com',
34 array()
35 )
36 );
37
38 $wgUser = self::$users['sysop']->user;
39
40 $this->apiContext = new ApiTestContext();
41
42 }
43
44 /**
45 * Does the API request and returns the result.
46 *
47 * The returned value is an array containing
48 * - the result data (array)
49 * - the request (WebRequest)
50 * - the session data of the request (array)
51 * - if $appendModule is true, the Api module $module
52 *
53 * @param array $params
54 * @param array|null $session
55 * @param bool $appendModule
56 * @param User|null $user
57 *
58 * @return array
59 */
60 protected function doApiRequest( array $params, array $session = null, $appendModule = false, User $user = null ) {
61 global $wgRequest, $wgUser;
62
63 if ( is_null( $session ) ) {
64 // re-use existing global session by default
65 $session = $wgRequest->getSessionArray();
66 }
67
68 // set up global environment
69 if ( $user ) {
70 $wgUser = $user;
71 }
72
73 $wgRequest = new FauxRequest( $params, true, $session );
74 RequestContext::getMain()->setRequest( $wgRequest );
75
76 // set up local environment
77 $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
78
79 $module = new ApiMain( $context, true );
80
81 // run it!
82 $module->execute();
83
84 // construct result
85 $results = array(
86 $module->getResultData(),
87 $context->getRequest(),
88 $context->getRequest()->getSessionArray()
89 );
90
91 if ( $appendModule ) {
92 $results[] = $module;
93 }
94
95 return $results;
96 }
97
98 /**
99 * Add an edit token to the API request
100 * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
101 * request, without actually requesting a "real" edit token
102 * @param $params Array: key-value API params
103 * @param $session Array|null: session array
104 * @param $user User|null A User object for the context
105 */
106 protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) {
107 global $wgRequest;
108
109 if ( $session === null ) {
110 $session = $wgRequest->getSessionArray();
111 }
112
113 if ( $session['wsToken'] ) {
114 // add edit token to fake session
115 $session['wsEditToken'] = $session['wsToken'];
116 // add token to request parameters
117 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
118 return $this->doApiRequest( $params, $session, false, $user );
119 } else {
120 throw new Exception( "request data not in right format" );
121 }
122 }
123
124 protected function doLogin() {
125 $data = $this->doApiRequest( array(
126 'action' => 'login',
127 'lgname' => self::$users['sysop']->username,
128 'lgpassword' => self::$users['sysop']->password ) );
129
130 $token = $data[0]['login']['token'];
131
132 $data = $this->doApiRequest( array(
133 'action' => 'login',
134 'lgtoken' => $token,
135 'lgname' => self::$users['sysop']->username,
136 'lgpassword' => self::$users['sysop']->password
137 ), $data[2] );
138
139 return $data;
140 }
141
142 protected function getTokenList( $user, $session = null ) {
143 $data = $this->doApiRequest( array(
144 'action' => 'query',
145 'titles' => 'Main Page',
146 'intoken' => 'edit|delete|protect|move|block|unblock|watch',
147 'prop' => 'info' ), $session, false, $user->user );
148 return $data;
149 }
150 }
151
152 class UserWrapper {
153 public $userName, $password, $user;
154
155 public function __construct( $userName, $password, $group = '' ) {
156 $this->userName = $userName;
157 $this->password = $password;
158
159 $this->user = User::newFromName( $this->userName );
160 if ( !$this->user->getID() ) {
161 $this->user = User::createNew( $this->userName, array(
162 "email" => "test@example.com",
163 "real_name" => "Test User" ) );
164 }
165 $this->user->setPassword( $this->password );
166
167 if ( $group !== '' ) {
168 $this->user->addGroup( $group );
169 }
170 $this->user->saveSettings();
171 }
172 }
173
174 class MockApi extends ApiBase {
175 public function execute() { }
176 public function getVersion() { }
177
178 public function __construct() { }
179
180 public function getAllowedParams() {
181 return array(
182 'filename' => null,
183 'enablechunks' => false,
184 'sessionkey' => null,
185 );
186 }
187 }
188
189 class ApiTestContext extends RequestContext {
190
191 /**
192 * Returns a DerivativeContext with the request variables in place
193 *
194 * @param $request WebRequest request object including parameters and session
195 * @param $user User or null
196 * @return DerivativeContext
197 */
198 public function newTestContext( WebRequest $request, User $user = null ) {
199 $context = new DerivativeContext( $this );
200 $context->setRequest( $request );
201 if ( $user !== null ) {
202 $context->setUser( $user );
203 }
204 return $context;
205 }
206 }