Merge "Language: s/error_log/wfWarn/"
[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 string $pageName Page title
47 * @param string $text Content of the page
48 * @param string $summary Optional summary string for the revision
49 * @param int $defaultNs Optional namespace id
50 * @return array 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
56 return $page->doEditContent( ContentHandler::makeContent( $text, $title ), $summary );
57 }
58
59 /**
60 * Does the API request and returns the result.
61 *
62 * The returned value is an array containing
63 * - the result data (array)
64 * - the request (WebRequest)
65 * - the session data of the request (array)
66 * - if $appendModule is true, the Api module $module
67 *
68 * @param array $params
69 * @param array|null $session
70 * @param bool $appendModule
71 * @param User|null $user
72 *
73 * @return array
74 */
75 protected function doApiRequest( array $params, array $session = null,
76 $appendModule = false, User $user = null
77 ) {
78 global $wgRequest, $wgUser;
79
80 if ( is_null( $session ) ) {
81 // re-use existing global session by default
82 $session = $wgRequest->getSessionArray();
83 }
84
85 // set up global environment
86 if ( $user ) {
87 $wgUser = $user;
88 }
89
90 $wgRequest = new FauxRequest( $params, true, $session );
91 RequestContext::getMain()->setRequest( $wgRequest );
92
93 // set up local environment
94 $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
95
96 $module = new ApiMain( $context, true );
97
98 // run it!
99 $module->execute();
100
101 // construct result
102 $results = array(
103 $module->getResultData(),
104 $context->getRequest(),
105 $context->getRequest()->getSessionArray()
106 );
107
108 if ( $appendModule ) {
109 $results[] = $module;
110 }
111
112 return $results;
113 }
114
115 /**
116 * Add an edit token to the API request
117 * This is cheating a bit -- we grab a token in the correct format and then
118 * add it to the pseudo-session and to the request, without actually
119 * requesting a "real" edit token.
120 *
121 * @param array $params Key-value API params
122 * @param array|null $session Session array
123 * @param User|null $user A User object for the context
124 * @return array Result of the API call
125 * @throws Exception In case wsToken is not set in the session
126 */
127 protected function doApiRequestWithToken( array $params, array $session = null,
128 User $user = null
129 ) {
130 global $wgRequest;
131
132 if ( $session === null ) {
133 $session = $wgRequest->getSessionArray();
134 }
135
136 if ( isset( $session['wsToken'] ) && $session['wsToken'] ) {
137 // add edit token to fake session
138 $session['wsEditToken'] = $session['wsToken'];
139 // add token to request parameters
140 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
141
142 return $this->doApiRequest( $params, $session, false, $user );
143 } else {
144 throw new Exception( "Session token not available" );
145 }
146 }
147
148 protected function doLogin( $user = 'sysop' ) {
149 if ( !array_key_exists( $user, self::$users ) ) {
150 throw new MWException( "Can not log in to undefined user $user" );
151 }
152
153 $data = $this->doApiRequest( array(
154 'action' => 'login',
155 'lgname' => self::$users[$user]->username,
156 'lgpassword' => self::$users[$user]->password ) );
157
158 $token = $data[0]['login']['token'];
159
160 $data = $this->doApiRequest(
161 array(
162 'action' => 'login',
163 'lgtoken' => $token,
164 'lgname' => self::$users[$user]->username,
165 'lgpassword' => self::$users[$user]->password,
166 ),
167 $data[2]
168 );
169
170 return $data;
171 }
172
173 protected function getTokenList( $user, $session = null ) {
174 $data = $this->doApiRequest( array(
175 'action' => 'tokens',
176 'type' => 'edit|delete|protect|move|block|unblock|watch'
177 ), $session, false, $user->user );
178
179 if ( !array_key_exists( 'tokens', $data[0] ) ) {
180 throw new MWException( 'Api failed to return a token list' );
181 }
182
183 return $data[0]['tokens'];
184 }
185
186 public function testApiTestGroup() {
187 $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
188 $constraint = PHPUnit_Framework_Assert::logicalOr(
189 $this->contains( 'medium' ),
190 $this->contains( 'large' )
191 );
192 $this->assertThat( $groups, $constraint,
193 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
194 );
195 }
196 }