Merge "Add and use Title::getOtherPage()"
[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
109 // set up local environment
110 $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
111
112 $module = new ApiMain( $context, true );
113
114 // run it!
115 $module->execute();
116
117 // construct result
118 $results = array(
119 $module->getResultData(),
120 $context->getRequest(),
121 $context->getRequest()->getSessionArray()
122 );
123
124 if ( $appendModule ) {
125 $results[] = $module;
126 }
127
128 return $results;
129 }
130
131 /**
132 * Add an edit token to the API request
133 * This is cheating a bit -- we grab a token in the correct format and then
134 * add it to the pseudo-session and to the request, without actually
135 * requesting a "real" edit token.
136 *
137 * @param array $params Key-value API params
138 * @param array|null $session Session array
139 * @param User|null $user A User object for the context
140 * @return array Result of the API call
141 * @throws Exception In case wsToken is not set in the session
142 */
143 protected function doApiRequestWithToken( array $params, array $session = null,
144 User $user = null
145 ) {
146 global $wgRequest;
147
148 if ( $session === null ) {
149 $session = $wgRequest->getSessionArray();
150 }
151
152 if ( isset( $session['wsToken'] ) && $session['wsToken'] ) {
153 // @todo Why does this directly mess with the session? Fix that.
154 // add edit token to fake session
155 $session['wsEditToken'] = $session['wsToken'];
156 // add token to request parameters
157 $timestamp = wfTimestamp();
158 $params['token'] = hash_hmac( 'md5', $timestamp, $session['wsToken'] ) .
159 dechex( $timestamp ) .
160 User::EDIT_TOKEN_SUFFIX;
161
162 return $this->doApiRequest( $params, $session, false, $user );
163 } else {
164 throw new Exception( "Session token not available" );
165 }
166 }
167
168 protected function doLogin( $user = 'sysop' ) {
169 if ( !array_key_exists( $user, self::$users ) ) {
170 throw new MWException( "Can not log in to undefined user $user" );
171 }
172
173 $data = $this->doApiRequest( array(
174 'action' => 'login',
175 'lgname' => self::$users[$user]->username,
176 'lgpassword' => self::$users[$user]->password ) );
177
178 $token = $data[0]['login']['token'];
179
180 $data = $this->doApiRequest(
181 array(
182 'action' => 'login',
183 'lgtoken' => $token,
184 'lgname' => self::$users[$user]->username,
185 'lgpassword' => self::$users[$user]->password,
186 ),
187 $data[2]
188 );
189
190 return $data;
191 }
192
193 protected function getTokenList( $user, $session = null ) {
194 $data = $this->doApiRequest( array(
195 'action' => 'tokens',
196 'type' => 'edit|delete|protect|move|block|unblock|watch'
197 ), $session, false, $user->user );
198
199 if ( !array_key_exists( 'tokens', $data[0] ) ) {
200 throw new MWException( 'Api failed to return a token list' );
201 }
202
203 return $data[0]['tokens'];
204 }
205
206 public function testApiTestGroup() {
207 $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
208 $constraint = PHPUnit_Framework_Assert::logicalOr(
209 $this->contains( 'medium' ),
210 $this->contains( 'large' )
211 );
212 $this->assertThat( $groups, $constraint,
213 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
214 );
215 }
216 }