Merge "Automatically deduplicate root jobs on insertion"
[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 /**
12 * @var array
13 */
14 protected $tablesUsed = array( 'user', 'user_groups', 'user_properties' );
15
16 protected function setUp() {
17 global $wgServer;
18
19 parent::setUp();
20 self::$apiUrl = $wgServer . wfScript( 'api' );
21
22 ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
23
24 self::$users = array(
25 'sysop' => new TestUser(
26 'Apitestsysop',
27 'Api Test Sysop',
28 'api_test_sysop@example.com',
29 array( 'sysop' )
30 ),
31 'uploader' => new TestUser(
32 'Apitestuser',
33 'Api Test User',
34 'api_test_user@example.com',
35 array()
36 )
37 );
38
39 $this->setMwGlobals( array(
40 'wgMemc' => new EmptyBagOStuff(),
41 'wgAuth' => new StubObject( 'wgAuth', 'AuthPlugin' ),
42 'wgRequest' => new FauxRequest( array() ),
43 'wgUser' => self::$users['sysop']->user,
44 ) );
45
46 $this->apiContext = new ApiTestContext();
47 }
48
49 protected function tearDown() {
50 // Avoid leaking session over tests
51 if ( session_id() != '' ) {
52 global $wgUser;
53 $wgUser->logout();
54 session_destroy();
55 }
56
57 parent::tearDown();
58 }
59
60 /**
61 * Edits or creates a page/revision
62 * @param string $pageName Page title
63 * @param string $text Content of the page
64 * @param string $summary Optional summary string for the revision
65 * @param int $defaultNs Optional namespace id
66 * @return array Array as returned by WikiPage::doEditContent()
67 */
68 protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN ) {
69 $title = Title::newFromText( $pageName, $defaultNs );
70 $page = WikiPage::factory( $title );
71
72 return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
73 }
74
75 /**
76 * Does the API request and returns the result.
77 *
78 * The returned value is an array containing
79 * - the result data (array)
80 * - the request (WebRequest)
81 * - the session data of the request (array)
82 * - if $appendModule is true, the Api module $module
83 *
84 * @param array $params
85 * @param array|null $session
86 * @param bool $appendModule
87 * @param User|null $user
88 *
89 * @return array
90 */
91 protected function doApiRequest( array $params, array $session = null,
92 $appendModule = false, User $user = null
93 ) {
94 global $wgRequest, $wgUser;
95
96 if ( is_null( $session ) ) {
97 // re-use existing global session by default
98 $session = $wgRequest->getSessionArray();
99 }
100
101 // set up global environment
102 if ( $user ) {
103 $wgUser = $user;
104 }
105
106 $wgRequest = new FauxRequest( $params, true, $session );
107 RequestContext::getMain()->setRequest( $wgRequest );
108 RequestContext::getMain()->setUser( $wgUser );
109
110 // set up local environment
111 $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
112
113 $module = new ApiMain( $context, true );
114
115 // run it!
116 $module->execute();
117
118 // construct result
119 $results = array(
120 $module->getResult()->getResultData( null, array( 'Strip' => 'all' ) ),
121 $context->getRequest(),
122 $context->getRequest()->getSessionArray()
123 );
124
125 if ( $appendModule ) {
126 $results[] = $module;
127 }
128
129 return $results;
130 }
131
132 /**
133 * Add an edit token to the API request
134 * This is cheating a bit -- we grab a token in the correct format and then
135 * add it to the pseudo-session and to the request, without actually
136 * requesting a "real" edit token.
137 *
138 * @param array $params Key-value API params
139 * @param array|null $session Session array
140 * @param User|null $user A User object for the context
141 * @return array Result of the API call
142 * @throws Exception In case wsToken is not set in the session
143 */
144 protected function doApiRequestWithToken( array $params, array $session = null,
145 User $user = null
146 ) {
147 global $wgRequest;
148
149 if ( $session === null ) {
150 $session = $wgRequest->getSessionArray();
151 }
152
153 if ( isset( $session['wsToken'] ) && $session['wsToken'] ) {
154 // @todo Why does this directly mess with the session? Fix that.
155 // add edit token to fake session
156 $session['wsEditToken'] = $session['wsToken'];
157 // add token to request parameters
158 $timestamp = wfTimestamp();
159 $params['token'] = hash_hmac( 'md5', $timestamp, $session['wsToken'] ) .
160 dechex( $timestamp ) .
161 User::EDIT_TOKEN_SUFFIX;
162
163 return $this->doApiRequest( $params, $session, false, $user );
164 } else {
165 throw new Exception( "Session token not available" );
166 }
167 }
168
169 protected function doLogin( $user = 'sysop' ) {
170 if ( !array_key_exists( $user, self::$users ) ) {
171 throw new MWException( "Can not log in to undefined user $user" );
172 }
173
174 $data = $this->doApiRequest( array(
175 'action' => 'login',
176 'lgname' => self::$users[$user]->username,
177 'lgpassword' => self::$users[$user]->password ) );
178
179 $token = $data[0]['login']['token'];
180
181 $data = $this->doApiRequest(
182 array(
183 'action' => 'login',
184 'lgtoken' => $token,
185 'lgname' => self::$users[$user]->username,
186 'lgpassword' => self::$users[$user]->password,
187 ),
188 $data[2]
189 );
190
191 return $data;
192 }
193
194 protected function getTokenList( $user, $session = null ) {
195 $data = $this->doApiRequest( array(
196 'action' => 'tokens',
197 'type' => 'edit|delete|protect|move|block|unblock|watch'
198 ), $session, false, $user->user );
199
200 if ( !array_key_exists( 'tokens', $data[0] ) ) {
201 throw new MWException( 'Api failed to return a token list' );
202 }
203
204 return $data[0]['tokens'];
205 }
206
207 public function testApiTestGroup() {
208 $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
209 $constraint = PHPUnit_Framework_Assert::logicalOr(
210 $this->contains( 'medium' ),
211 $this->contains( 'large' )
212 );
213 $this->assertThat( $groups, $constraint,
214 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
215 );
216 }
217 }