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",
88 $this->assertNotInternalType( "bool", $result );
89 $a = $result["login"]["result"];
90 $this->assertEquals( "NeedToken", $a );
92 $token = $result["login"]["token"];
94 $ret = $this->doApiRequest(
98 "lgname" => $user->username
,
99 "lgpassword" => "badnowayinhell",
106 $this->assertNotInternalType( "bool", $result );
107 $a = $result["login"]["result"];
109 $this->assertEquals( "WrongPass", $a );
112 function testApiLoginGoodPass() {
115 if ( !isset( $wgServer ) ) {
116 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
119 $user = self
::$users['sysop'];
120 $user->user
->logOut();
122 $ret = $this->doApiRequest( array(
124 "lgname" => $user->username
,
125 "lgpassword" => $user->password
,
130 $this->assertNotInternalType( "bool", $result );
131 $this->assertNotInternalType( "null", $result["login"] );
133 $a = $result["login"]["result"];
134 $this->assertEquals( "NeedToken", $a );
135 $token = $result["login"]["token"];
137 $ret = $this->doApiRequest(
141 "lgname" => $user->username
,
142 "lgpassword" => $user->password
,
149 $this->assertNotInternalType( "bool", $result );
150 $a = $result["login"]["result"];
152 $this->assertEquals( "Success", $a );
158 function testApiGotCookie() {
159 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
161 global $wgServer, $wgScriptPath;
163 if ( !isset( $wgServer ) ) {
164 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
166 $user = self
::$users['sysop'];
168 $req = MWHttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
169 array( "method" => "POST",
171 "lgname" => $user->username
,
172 "lgpassword" => $user->password
178 libxml_use_internal_errors( true );
179 $sxe = simplexml_load_string( $req->getContent() );
180 $this->assertNotInternalType( "bool", $sxe );
181 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
182 $this->assertNotInternalType( "null", $sxe->login
[0] );
184 $a = $sxe->login
[0]->attributes()->result
[0];
185 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
186 $token = (string)$sxe->login
[0]->attributes()->token
;
188 $req->setData( array(
190 "lgname" => $user->username
,
191 "lgpassword" => $user->password
) );
194 $cj = $req->getCookieJar();
195 $serverName = parse_url( $wgServer, PHP_URL_HOST
);
196 $this->assertNotEquals( false, $serverName );
197 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
198 $this->assertNotEquals( '', $serializedCookie );
199 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName
. '; .*Token=/', $serializedCookie );
204 function testRunLogin() {
205 $sysopUser = self
::$users['sysop'];
206 $data = $this->doApiRequest( array(
208 'lgname' => $sysopUser->username
,
209 'lgpassword' => $sysopUser->password
) );
211 $this->assertArrayHasKey( "login", $data[0] );
212 $this->assertArrayHasKey( "result", $data[0]['login'] );
213 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
214 $token = $data[0]['login']['token'];
216 $data = $this->doApiRequest( array(
219 "lgname" => $sysopUser->username
,
220 "lgpassword" => $sysopUser->password
), $data[2] );
222 $this->assertArrayHasKey( "login", $data[0] );
223 $this->assertArrayHasKey( "result", $data[0]['login'] );
224 $this->assertEquals( "Success", $data[0]['login']['result'] );
225 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
230 function testGettingToken() {
231 foreach ( self
::$users as $user ) {
232 $this->runTokenTest( $user );
236 function runTokenTest( $user ) {
237 $tokens = $this->getTokenList( $user );
239 $rights = $user->user
->getRights();
241 $this->assertArrayHasKey( 'edittoken', $tokens );
242 $this->assertArrayHasKey( 'movetoken', $tokens );
244 if ( isset( $rights['delete'] ) ) {
245 $this->assertArrayHasKey( 'deletetoken', $tokens );
248 if ( isset( $rights['block'] ) ) {
249 $this->assertArrayHasKey( 'blocktoken', $tokens );
250 $this->assertArrayHasKey( 'unblocktoken', $tokens );
253 if ( isset( $rights['protect'] ) ) {
254 $this->assertArrayHasKey( 'protecttoken', $tokens );