Merge "Add DROP INDEX support to DatabaseSqlite::replaceVars method"
[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
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, $appendModule = false, User $user = null ) {
76 global $wgRequest, $wgUser;
77
78 if ( is_null( $session ) ) {
79 // re-use existing global session by default
80 $session = $wgRequest->getSessionArray();
81 }
82
83 // set up global environment
84 if ( $user ) {
85 $wgUser = $user;
86 }
87
88 $wgRequest = new FauxRequest( $params, true, $session );
89 RequestContext::getMain()->setRequest( $wgRequest );
90
91 // set up local environment
92 $context = $this->apiContext->newTestContext( $wgRequest, $wgUser );
93
94 $module = new ApiMain( $context, true );
95
96 // run it!
97 $module->execute();
98
99 // construct result
100 $results = array(
101 $module->getResultData(),
102 $context->getRequest(),
103 $context->getRequest()->getSessionArray()
104 );
105
106 if ( $appendModule ) {
107 $results[] = $module;
108 }
109
110 return $results;
111 }
112
113 /**
114 * Add an edit token to the API request
115 * This is cheating a bit -- we grab a token in the correct format and then add it to the pseudo-session and to the
116 * request, without actually requesting a "real" edit token
117 * @param $params Array: key-value API params
118 * @param $session Array|null: session array
119 * @param $user User|null A User object for the context
120 * @return mixed result of the API call
121 * @throws Exception in case wsToken is not set in the session
122 */
123 protected function doApiRequestWithToken( array $params, array $session = null, User $user = null ) {
124 global $wgRequest;
125
126 if ( $session === null ) {
127 $session = $wgRequest->getSessionArray();
128 }
129
130 if ( $session['wsToken'] ) {
131 // add edit token to fake session
132 $session['wsEditToken'] = $session['wsToken'];
133 // add token to request parameters
134 $params['token'] = md5( $session['wsToken'] ) . User::EDIT_TOKEN_SUFFIX;
135
136 return $this->doApiRequest( $params, $session, false, $user );
137 } else {
138 throw new Exception( "request data not in right format" );
139 }
140 }
141
142 protected function doLogin( $user = 'sysop' ) {
143 if ( !array_key_exists( $user, self::$users ) ) {
144 throw new MWException( "Can not log in to undefined user $user" );
145 }
146
147 $data = $this->doApiRequest( array(
148 'action' => 'login',
149 'lgname' => self::$users[ $user ]->username,
150 'lgpassword' => self::$users[ $user ]->password ) );
151
152 $token = $data[0]['login']['token'];
153
154 $data = $this->doApiRequest(
155 array(
156 'action' => 'login',
157 'lgtoken' => $token,
158 'lgname' => self::$users[ $user ]->username,
159 'lgpassword' => self::$users[ $user ]->password,
160 ),
161 $data[2]
162 );
163
164 return $data;
165 }
166
167 protected function getTokenList( $user, $session = null ) {
168 $data = $this->doApiRequest( array(
169 'action' => 'tokens',
170 'type' => 'edit|delete|protect|move|block|unblock|watch'
171 ), $session, false, $user->user );
172
173 if ( !array_key_exists( 'tokens', $data[0] ) ) {
174 throw new MWException( 'Api failed to return a token list' );
175 }
176
177 return $data[0]['tokens'];
178 }
179
180 public function testApiTestGroup() {
181 $groups = PHPUnit_Util_Test::getGroups( get_class( $this ) );
182 $constraint = PHPUnit_Framework_Assert::logicalOr(
183 $this->contains( 'medium' ),
184 $this->contains( 'large' )
185 );
186 $this->assertThat( $groups, $constraint,
187 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'
188 );
189 }
190 }