Refactor much of the API testing code.
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTest.php
1 <?php
2
3 class MockApi extends ApiBase {
4 public function execute() { }
5 public function getVersion() { }
6
7 public function __construct() { }
8
9 public function getAllowedParams() {
10 return array(
11 'filename' => null,
12 'enablechunks' => false,
13 'sessionkey' => null,
14 );
15 }
16 }
17
18 /**
19 * @group Database
20 * @group Destructive
21 */
22 class ApiTest extends ApiTestSetup {
23
24 function testRequireOnlyOneParameterDefault() {
25 $mock = new MockApi();
26
27 $this->assertEquals(
28 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
29 "enablechunks" => false ), "filename", "enablechunks" ) );
30 }
31
32 /**
33 * @expectedException UsageException
34 */
35 function testRequireOnlyOneParameterZero() {
36 $mock = new MockApi();
37
38 $this->assertEquals(
39 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
40 "enablechunks" => 0 ), "filename", "enablechunks" ) );
41 }
42
43 /**
44 * @expectedException UsageException
45 */
46 function testRequireOnlyOneParameterTrue() {
47 $mock = new MockApi();
48
49 $this->assertEquals(
50 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
51 "enablechunks" => true ), "filename", "enablechunks" ) );
52 }
53
54 /**
55 * Test that the API will accept a FauxRequest and execute. The help action
56 * (default) throws a UsageException. Just validate we're getting proper XML
57 *
58 * @expectedException UsageException
59 */
60 function testApi() {
61
62 $api = new ApiMain(
63 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
64 );
65 $api->execute();
66 $api->getPrinter()->setBufferResult( true );
67 $api->printResult( false );
68 $resp = $api->getPrinter()->getBuffer();
69
70 libxml_use_internal_errors( true );
71 $sxe = simplexml_load_string( $resp );
72 $this->assertNotInternalType( "bool", $sxe );
73 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
74 }
75
76 /**
77 * Test result of attempted login with an empty username
78 */
79 function testApiLoginNoName() {
80 $data = $this->doApiRequest( array( 'action' => 'login',
81 'lgname' => '', 'lgpassword' => $this->user->password,
82 ) );
83 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
84 }
85
86 function testApiLoginBadPass() {
87 global $wgServer;
88
89 $user = $this->user;
90
91 if ( !isset( $wgServer ) ) {
92 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
93 }
94 $ret = $this->doApiRequest( array(
95 "action" => "login",
96 "lgname" => $user->userName,
97 "lgpassword" => "bad",
98 )
99 );
100
101 $result = $ret[0];
102
103 $this->assertNotInternalType( "bool", $result );
104 $a = $result["login"]["result"];
105 $this->assertEquals( "NeedToken", $a );
106
107 $token = $result["login"]["token"];
108
109 $ret = $this->doApiRequest( array(
110 "action" => "login",
111 "lgtoken" => $token,
112 "lgname" => $user->userName,
113 "lgpassword" => "bad",
114 )
115 );
116
117 $result = $ret[0];
118
119 $this->assertNotInternalType( "bool", $result );
120 $a = $result["login"]["result"];
121
122 $this->assertEquals( "WrongPass", $a );
123 }
124
125 function testApiLoginGoodPass() {
126 global $wgServer;
127
128 if ( !isset( $wgServer ) ) {
129 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
130 }
131
132 $user = $this->user;
133
134 $ret = $this->doApiRequest( array(
135 "action" => "login",
136 "lgname" => $user->userName,
137 "lgpassword" => $user->password,
138 )
139 );
140
141 $result = $ret[0];
142 $this->assertNotInternalType( "bool", $result );
143 $this->assertNotInternalType( "null", $result["login"] );
144
145 $a = $result["login"]["result"];
146 $this->assertEquals( "NeedToken", $a );
147 $token = $result["login"]["token"];
148
149 $ret = $this->doApiRequest( array(
150 "action" => "login",
151 "lgtoken" => $token,
152 "lgname" => $user->userName,
153 "lgpassword" => $user->password,
154 )
155 );
156
157 $result = $ret[0];
158
159 $this->assertNotInternalType( "bool", $result );
160 $a = $result["login"]["result"];
161
162 $this->assertEquals( "Success", $a );
163 }
164
165 function testApiGotCookie() {
166 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
167
168 global $wgServer, $wgScriptPath;
169
170 if ( !isset( $wgServer ) ) {
171 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
172 }
173 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
174 array( "method" => "POST",
175 "postData" => array(
176 "lgname" => $this->user->userName,
177 "lgpassword" => $this->user->password ) ) );
178 $req->execute();
179
180 libxml_use_internal_errors( true );
181 $sxe = simplexml_load_string( $req->getContent() );
182 $this->assertNotInternalType( "bool", $sxe );
183 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
184 $this->assertNotInternalType( "null", $sxe->login[0] );
185
186 $a = $sxe->login[0]->attributes()->result[0];
187 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
188 $token = (string)$sxe->login[0]->attributes()->token;
189
190 $req->setData( array(
191 "lgtoken" => $token,
192 "lgname" => $this->user->userName,
193 "lgpassword" => $this->user->password ) );
194 $req->execute();
195
196 $cj = $req->getCookieJar();
197 $serverName = parse_url( $wgServer, PHP_URL_HOST );
198 $this->assertNotEquals( false, $serverName );
199 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
200 $this->assertNotEquals( '', $serializedCookie );
201 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $this->user->userName . '; .*Token=/', $serializedCookie );
202
203 return $cj;
204 }
205
206 /**
207 * @depends testApiGotCookie
208 */
209 function testApiListPages( CookieJar $cj ) {
210 $this->markTestIncomplete( "Not done with this yet" );
211 global $wgServer;
212
213 if ( $wgServer == "http://localhost" ) {
214 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
215 }
216 $req = MWHttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&" .
217 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
218 $req->setCookieJar( $cj );
219 $req->execute();
220 libxml_use_internal_errors( true );
221 $sxe = simplexml_load_string( $req->getContent() );
222 $this->assertNotInternalType( "bool", $sxe );
223 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
224 $a = $sxe->query[0]->pages[0]->page[0]->attributes();
225 }
226
227 function testRunLogin() {
228 $data = $this->doApiRequest( array(
229 'action' => 'login',
230 'lgname' => $this->sysopUser->userName,
231 'lgpassword' => $this->sysopUser->password ) );
232
233 $this->assertArrayHasKey( "login", $data[0] );
234 $this->assertArrayHasKey( "result", $data[0]['login'] );
235 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
236 $token = $data[0]['login']['token'];
237
238 $data = $this->doApiRequest( array(
239 'action' => 'login',
240 "lgtoken" => $token,
241 "lgname" => $this->sysopUser->userName,
242 "lgpassword" => $this->sysopUser->password ), $data );
243
244 $this->assertArrayHasKey( "login", $data[0] );
245 $this->assertArrayHasKey( "result", $data[0]['login'] );
246 $this->assertEquals( "Success", $data[0]['login']['result'] );
247 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
248
249 return $data;
250 }
251
252 function testGettingToken() {
253 foreach ( array( $this->user, $this->sysopUser ) as $user ) {
254 $this->runTokenTest( $user );
255 }
256 }
257
258 function runTokenTest( $user ) {
259
260 $data = $this->getTokenList( $user );
261
262 $this->assertArrayHasKey( 'query', $data[0] );
263 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
264 $keys = array_keys( $data[0]['query']['pages'] );
265 $key = array_pop( $keys );
266
267 $rights = $user->user->getRights();
268
269 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
270 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
271 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
272
273 if ( isset( $rights['delete'] ) ) {
274 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
275 }
276
277 if ( isset( $rights['block'] ) ) {
278 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
279 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
280 }
281
282 if ( isset( $rights['protect'] ) ) {
283 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
284 }
285
286 return $data;
287 }
288 }