Make wfUrlEncode(null) reset the static. Two skipped tests work now.
[mediawiki.git] / tests / phpunit / includes / api / ApiTest.php
bloba587e6b19f59902d0924ca5b9abff0edd69ea487
1 <?php
3 /**
4 * @group Database
5 */
6 class ApiTest extends ApiTestCase {
8 function testRequireOnlyOneParameterDefault() {
9 $mock = new MockApi();
11 $this->assertEquals(
12 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
13 "enablechunks" => false ), "filename", "enablechunks" ) );
16 /**
17 * @expectedException UsageException
19 function testRequireOnlyOneParameterZero() {
20 $mock = new MockApi();
22 $this->assertEquals(
23 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
24 "enablechunks" => 0 ), "filename", "enablechunks" ) );
27 /**
28 * @expectedException UsageException
30 function testRequireOnlyOneParameterTrue() {
31 $mock = new MockApi();
33 $this->assertEquals(
34 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
35 "enablechunks" => true ), "filename", "enablechunks" ) );
38 /**
39 * Test that the API will accept a FauxRequest and execute. The help action
40 * (default) throws a UsageException. Just validate we're getting proper XML
42 * @expectedException UsageException
44 function testApi() {
46 $api = new ApiMain(
47 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
49 $api->execute();
50 $api->getPrinter()->setBufferResult( true );
51 $api->printResult( false );
52 $resp = $api->getPrinter()->getBuffer();
54 libxml_use_internal_errors( true );
55 $sxe = simplexml_load_string( $resp );
56 $this->assertNotInternalType( "bool", $sxe );
57 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
60 /**
61 * Test result of attempted login with an empty username
63 function testApiLoginNoName() {
64 $data = $this->doApiRequest( array( 'action' => 'login',
65 'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
66 ) );
67 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
70 function testApiLoginBadPass() {
71 global $wgServer;
73 $user = self::$users['sysop'];
74 $user->user->logOut();
76 if ( !isset( $wgServer ) ) {
77 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
79 $ret = $this->doApiRequest( array(
80 "action" => "login",
81 "lgname" => $user->username,
82 "lgpassword" => "bad",
86 $result = $ret[0];
88 $this->assertNotInternalType( "bool", $result );
89 $a = $result["login"]["result"];
90 $this->assertEquals( "NeedToken", $a );
92 $token = $result["login"]["token"];
94 $ret = $this->doApiRequest( array(
95 "action" => "login",
96 "lgtoken" => $token,
97 "lgname" => $user->username,
98 "lgpassword" => "badnowayinhell",
102 $result = $ret[0];
104 $this->assertNotInternalType( "bool", $result );
105 $a = $result["login"]["result"];
107 $this->assertEquals( "WrongPass", $a );
110 function testApiLoginGoodPass() {
111 global $wgServer;
113 if ( !isset( $wgServer ) ) {
114 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
117 $user = self::$users['sysop'];
118 $user->user->logOut();
120 $ret = $this->doApiRequest( array(
121 "action" => "login",
122 "lgname" => $user->username,
123 "lgpassword" => $user->password,
127 $result = $ret[0];
128 $this->assertNotInternalType( "bool", $result );
129 $this->assertNotInternalType( "null", $result["login"] );
131 $a = $result["login"]["result"];
132 $this->assertEquals( "NeedToken", $a );
133 $token = $result["login"]["token"];
135 $ret = $this->doApiRequest( array(
136 "action" => "login",
137 "lgtoken" => $token,
138 "lgname" => $user->username,
139 "lgpassword" => $user->password,
143 $result = $ret[0];
145 $this->assertNotInternalType( "bool", $result );
146 $a = $result["login"]["result"];
148 $this->assertEquals( "Success", $a );
151 function testApiGotCookie() {
152 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
154 global $wgServer, $wgScriptPath;
156 if ( !isset( $wgServer ) ) {
157 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
159 $user = self::$users['sysop'];
161 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
162 array( "method" => "POST",
163 "postData" => array(
164 "lgname" => $user->username,
165 "lgpassword" => $user->password ) ) );
166 $req->execute();
168 libxml_use_internal_errors( true );
169 $sxe = simplexml_load_string( $req->getContent() );
170 $this->assertNotInternalType( "bool", $sxe );
171 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
172 $this->assertNotInternalType( "null", $sxe->login[0] );
174 $a = $sxe->login[0]->attributes()->result[0];
175 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
176 $token = (string)$sxe->login[0]->attributes()->token;
178 $req->setData( array(
179 "lgtoken" => $token,
180 "lgname" => $user->username,
181 "lgpassword" => $user->password ) );
182 $req->execute();
184 $cj = $req->getCookieJar();
185 $serverName = parse_url( $wgServer, PHP_URL_HOST );
186 $this->assertNotEquals( false, $serverName );
187 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
188 $this->assertNotEquals( '', $serializedCookie );
189 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
191 return $cj;
195 * @depends testApiGotCookie
197 function testApiListPages( CookieJar $cj ) {
198 $this->markTestIncomplete( "Not done with this yet" );
199 global $wgServer;
201 if ( $wgServer == "http://localhost" ) {
202 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
204 $req = MWHttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&" .
205 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
206 $req->setCookieJar( $cj );
207 $req->execute();
208 libxml_use_internal_errors( true );
209 $sxe = simplexml_load_string( $req->getContent() );
210 $this->assertNotInternalType( "bool", $sxe );
211 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
212 $a = $sxe->query[0]->pages[0]->page[0]->attributes();
215 function testRunLogin() {
216 $sysopUser = self::$users['sysop'];
217 $data = $this->doApiRequest( array(
218 'action' => 'login',
219 'lgname' => $sysopUser->username,
220 'lgpassword' => $sysopUser->password ) );
222 $this->assertArrayHasKey( "login", $data[0] );
223 $this->assertArrayHasKey( "result", $data[0]['login'] );
224 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
225 $token = $data[0]['login']['token'];
227 $data = $this->doApiRequest( array(
228 'action' => 'login',
229 "lgtoken" => $token,
230 "lgname" => $sysopUser->username,
231 "lgpassword" => $sysopUser->password ), $data );
233 $this->assertArrayHasKey( "login", $data[0] );
234 $this->assertArrayHasKey( "result", $data[0]['login'] );
235 $this->assertEquals( "Success", $data[0]['login']['result'] );
236 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
238 return $data;
241 function testGettingToken() {
242 foreach ( self::$users as $user ) {
243 $this->runTokenTest( $user );
247 function runTokenTest( $user ) {
249 $data = $this->getTokenList( $user );
251 $this->assertArrayHasKey( 'query', $data[0] );
252 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
253 $keys = array_keys( $data[0]['query']['pages'] );
254 $key = array_pop( $keys );
256 $rights = $user->user->getRights();
258 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
259 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
260 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
262 if ( isset( $rights['delete'] ) ) {
263 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
266 if ( isset( $rights['block'] ) ) {
267 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
268 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
271 if ( isset( $rights['protect'] ) ) {
272 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
275 return $data;