6 class ApiTest
extends ApiTestCase
{
8 function testRequireOnlyOneParameterDefault() {
12 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
13 "enablechunks" => false ), "filename", "enablechunks" ) );
17 * @expectedException UsageException
19 function testRequireOnlyOneParameterZero() {
20 $mock = new MockApi();
23 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
24 "enablechunks" => 0 ), "filename", "enablechunks" ) );
28 * @expectedException UsageException
30 function testRequireOnlyOneParameterTrue() {
31 $mock = new MockApi();
34 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
35 "enablechunks" => true ), "filename", "enablechunks" ) );
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
47 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
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" ) );
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
,
67 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
70 function testApiLoginBadPass() {
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(
81 "lgname" => $user->username
,
82 "lgpassword" => "bad",
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(
97 "lgname" => $user->username
,
98 "lgpassword" => "badnowayinhell",
104 $this->assertNotInternalType( "bool", $result );
105 $a = $result["login"]["result"];
107 $this->assertEquals( "WrongPass", $a );
110 function testApiLoginGoodPass() {
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(
122 "lgname" => $user->username
,
123 "lgpassword" => $user->password
,
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(
138 "lgname" => $user->username
,
139 "lgpassword" => $user->password
,
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",
164 "lgname" => $user->username
,
165 "lgpassword" => $user->password
) ) );
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(
180 "lgname" => $user->username
,
181 "lgpassword" => $user->password
) );
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 );
195 * @depends testApiGotCookie
197 function testApiListPages( CookieJar
$cj ) {
198 $this->markTestIncomplete( "Not done with this yet" );
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 );
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(
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(
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'] );
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] );