Merge "(Bug 44192) Do not attempt to send a real e-mail in ApiAccountCreationTest"
[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 $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
44 /**
45 * Edits or creates a page/revision
46 * @param $pageName string page title
47 * @param $text string content of the page
48 * @param $summary string optional summary string for the revision
49 * @param $defaultNs int optional namespace id
50 * @return array as returned by WikiPage::doEditContent()
51 */
52 protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
53 $title = Title::newFromText( $pageName, $defaultNs );
54 $page = WikiPage::factory( $title );
55 return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
56 }
57
58 /**
59 * Does the API request and returns the result.
60 *
61 * The returned value is an array containing
62 * - the result data (array)
63 * - the request (WebRequest)
64 * - the session data of the request (array)
65 * - if $appendModule is true, the Api module $module
66 *
67 * @param array $params
68 * @param array|null $session
69 * @param bool $appendModule
70 * @param User|null $user
71 *
72 * @return array
73 */
74 protected function doApiRequest( array $params, array $session = null, $appendModule = false, User $user = null ) {
75 global $wgRequest, $wgUser;
76
77 if ( is_null( $session ) ) {
78 // re-use existing global session by default
79 $session = $wgRequest->getSessionArray();
80 }
81
82 // set up global environment
83 if ( $user ) {
84 $wgUser = $user;
85 }
86
87 $wgRequest = new FauxRequest( $params, true, $session );
88 RequestContext::getMain()->setRequest( $wgRequest );
89
90 // set up local environment
91 $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
92
93 $module = new ApiMain( $context, true );
94
95 // run it!
96 $module->execute();
97
98 // construct result
99 $results = array(
100 $module->getResultData(),
101 $context->getRequest(),
102 $context->getRequest()->getSessionArray()
103 );
104
105 if ( $appendModule ) {
106 $results[] = $module;
107 }
108
109 return $results;
110 }
111
112 /**
113 * Add an edit token to the API request
114 * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
115 * request, without actually requesting a "real" edit token
116 * @param $params Array: key-value API params
117 * @param $session Array|null: session array
118 * @param $user User|null A User object for the context
119 * @return result of the API call
120 * @throws Exception in case wsToken is not set in the session
121 */
122 protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) {
123 global $wgRequest;
124
125 if ( $session === null ) {
126 $session = $wgRequest->getSessionArray();
127 }
128
129 if ( $session['wsToken'] ) {
130 // add edit token to fake session
131 $session['wsEditToken'] = $session['wsToken'];
132 // add token to request parameters
133 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
134 return $this->doApiRequest( $params, $session, false, $user );
135 } else {
136 throw new Exception( "request data not in right format" );
137 }
138 }
139
140 protected function doLogin() {
141 $data = $this->doApiRequest( array(
142 'action' => 'login',
143 'lgname' => self::$users['sysop']->username,
144 'lgpassword' => self::$users['sysop']->password ) );
145
146 $token = $data[0]['login']['token'];
147
148 $data = $this->doApiRequest(
149 array(
150 'action' => 'login',
151 'lgtoken' => $token,
152 'lgname' => self::$users['sysop']->username,
153 'lgpassword' => self::$users['sysop']->password,
154 ),
155 $data[2]
156 );
157
158 return $data;
159 }
160
161 protected function getTokenList( $user, $session = null ) {
162 $data = $this->doApiRequest( array(
163 'action' => 'query',
164 'titles' => 'Main Page',
165 'intoken' => 'edit|delete|protect|move|block|unblock|watch',
166 'prop' => 'info' ), $session, false, $user->user );
167 return $data;
168 }
169
170 public function testApiTestGroup() {
171 $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
172 $constraint = PHPUnit_Framework_Assert::logicalOr(
173 $this->contains( 'medium' ),
174 $this->contains( 'large' )
175 );
176 $this->assertThat( $groups, $constraint,
177 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
178 );
179 }
180 }
181
182 class UserWrapper {
183 public $userName;
184 public $password;
185 public $user;
186
187 public function __construct( $userName, $password, $group = '' ) {
188 $this->userName = $userName;
189 $this->password = $password;
190
191 $this->user = User::newFromName( $this->userName );
192 if ( !$this->user->getID() ) {
193 $this->user = User::createNew( $this->userName, array(
194 "email" => "test@example.com",
195 "real_name" => "Test User" ) );
196 }
197 $this->user->setPassword( $this->password );
198
199 if ( $group !== '' ) {
200 $this->user->addGroup( $group );
201 }
202 $this->user->saveSettings();
203 }
204 }
205
206 class MockApi extends ApiBase {
207 public function execute() {}
208
209 public function getVersion() {}
210
211 public function __construct() {}
212
213 public function getAllowedParams() {
214 return array(
215 'filename' => null,
216 'enablechunks' => false,
217 'sessionkey' => null,
218 );
219 }
220 }
221
222 class ApiTestContext extends RequestContext {
223
224 /**
225 * Returns a DerivativeContext with the request variables in place
226 *
227 * @param $request WebRequest request object including parameters and session
228 * @param $user User or null
229 * @return DerivativeContext
230 */
231 public function newTestContext( WebRequest $request, User $user = null ) {
232 $context = new DerivativeContext( $this );
233 $context->setRequest( $request );
234 if ( $user !== null ) {
235 $context->setUser( $user );
236 }
237 return $context;
238 }
239 }