8 class ApiTest
extends ApiTestCase
{
10 function testRequireOnlyOneParameterDefault() {
11 $mock = new MockApi();
14 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
15 "enablechunks" => false ), "filename", "enablechunks" ) );
19 * @expectedException UsageException
21 function testRequireOnlyOneParameterZero() {
22 $mock = new MockApi();
25 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
26 "enablechunks" => 0 ), "filename", "enablechunks" ) );
30 * @expectedException UsageException
32 function testRequireOnlyOneParameterTrue() {
33 $mock = new MockApi();
36 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
37 "enablechunks" => true ), "filename", "enablechunks" ) );
41 * Test that the API will accept a FauxRequest and execute. The help action
42 * (default) throws a UsageException. Just validate we're getting proper XML
44 * @expectedException UsageException
48 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
51 $api->getPrinter()->setBufferResult( true );
52 $api->printResult( false );
53 $resp = $api->getPrinter()->getBuffer();
55 libxml_use_internal_errors( true );
56 $sxe = simplexml_load_string( $resp );
57 $this->assertNotInternalType( "bool", $sxe );
58 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
62 * Test result of attempted login with an empty username
64 function testApiLoginNoName() {
65 $data = $this->doApiRequest( array( 'action' => 'login',
66 'lgname' => '', 'lgpassword' => self
::$users['sysop']->password
,
68 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
71 function testApiLoginBadPass() {
74 $user = self
::$users['sysop'];
75 $user->user
->logOut();
77 if ( !isset( $wgServer ) ) {
78 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
80 $ret = $this->doApiRequest( array(
82 "lgname" => $user->username
,
83 "lgpassword" => "bad",
89 $this->assertNotInternalType( "bool", $result );
90 $a = $result["login"]["result"];
91 $this->assertEquals( "NeedToken", $a );
93 $token = $result["login"]["token"];
95 $ret = $this->doApiRequest(
99 "lgname" => $user->username
,
100 "lgpassword" => "badnowayinhell",
107 $this->assertNotInternalType( "bool", $result );
108 $a = $result["login"]["result"];
110 $this->assertEquals( "WrongPass", $a );
113 function testApiLoginGoodPass() {
116 if ( !isset( $wgServer ) ) {
117 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
120 $user = self
::$users['sysop'];
121 $user->user
->logOut();
123 $ret = $this->doApiRequest( array(
125 "lgname" => $user->username
,
126 "lgpassword" => $user->password
,
131 $this->assertNotInternalType( "bool", $result );
132 $this->assertNotInternalType( "null", $result["login"] );
134 $a = $result["login"]["result"];
135 $this->assertEquals( "NeedToken", $a );
136 $token = $result["login"]["token"];
138 $ret = $this->doApiRequest(
142 "lgname" => $user->username
,
143 "lgpassword" => $user->password
,
150 $this->assertNotInternalType( "bool", $result );
151 $a = $result["login"]["result"];
153 $this->assertEquals( "Success", $a );
159 function testApiGotCookie() {
160 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
162 global $wgServer, $wgScriptPath;
164 if ( !isset( $wgServer ) ) {
165 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
167 $user = self
::$users['sysop'];
169 $req = MWHttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
170 array( "method" => "POST",
172 "lgname" => $user->username
,
173 "lgpassword" => $user->password
179 libxml_use_internal_errors( true );
180 $sxe = simplexml_load_string( $req->getContent() );
181 $this->assertNotInternalType( "bool", $sxe );
182 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
183 $this->assertNotInternalType( "null", $sxe->login
[0] );
185 $a = $sxe->login
[0]->attributes()->result
[0];
186 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
187 $token = (string)$sxe->login
[0]->attributes()->token
;
189 $req->setData( array(
191 "lgname" => $user->username
,
192 "lgpassword" => $user->password
) );
195 $cj = $req->getCookieJar();
196 $serverName = parse_url( $wgServer, PHP_URL_HOST
);
197 $this->assertNotEquals( false, $serverName );
198 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
199 $this->assertNotEquals( '', $serializedCookie );
200 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName
. '; .*Token=/', $serializedCookie );
205 function testRunLogin() {
206 $sysopUser = self
::$users['sysop'];
207 $data = $this->doApiRequest( array(
209 'lgname' => $sysopUser->username
,
210 'lgpassword' => $sysopUser->password
) );
212 $this->assertArrayHasKey( "login", $data[0] );
213 $this->assertArrayHasKey( "result", $data[0]['login'] );
214 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
215 $token = $data[0]['login']['token'];
217 $data = $this->doApiRequest( array(
220 "lgname" => $sysopUser->username
,
221 "lgpassword" => $sysopUser->password
), $data[2] );
223 $this->assertArrayHasKey( "login", $data[0] );
224 $this->assertArrayHasKey( "result", $data[0]['login'] );
225 $this->assertEquals( "Success", $data[0]['login']['result'] );
226 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
231 function testGettingToken() {
232 foreach ( self
::$users as $user ) {
233 $this->runTokenTest( $user );
237 function runTokenTest( $user ) {
238 $data = $this->getTokenList( $user );
240 $this->assertArrayHasKey( 'query', $data[0] );
241 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
242 $keys = array_keys( $data[0]['query']['pages'] );
243 $key = array_pop( $keys );
245 $rights = $user->user
->getRights();
247 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
248 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
249 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
251 if ( isset( $rights['delete'] ) ) {
252 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
255 if ( isset( $rights['block'] ) ) {
256 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
257 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
260 if ( isset( $rights['protect'] ) ) {
261 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );