7 class ApiTest
extends ApiTestCase
{
9 function testRequireOnlyOneParameterDefault() {
10 $mock = new MockApi();
13 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
14 "enablechunks" => false ), "filename", "enablechunks" ) );
18 * @expectedException UsageException
20 function testRequireOnlyOneParameterZero() {
21 $mock = new MockApi();
24 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
25 "enablechunks" => 0 ), "filename", "enablechunks" ) );
29 * @expectedException UsageException
31 function testRequireOnlyOneParameterTrue() {
32 $mock = new MockApi();
35 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
36 "enablechunks" => true ), "filename", "enablechunks" ) );
40 * Test that the API will accept a FauxRequest and execute. The help action
41 * (default) throws a UsageException. Just validate we're getting proper XML
43 * @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( array(
98 "lgname" => $user->username
,
99 "lgpassword" => "badnowayinhell",
105 $this->assertNotInternalType( "bool", $result );
106 $a = $result["login"]["result"];
108 $this->assertEquals( "WrongPass", $a );
111 function testApiLoginGoodPass() {
114 if ( !isset( $wgServer ) ) {
115 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
118 $user = self
::$users['sysop'];
119 $user->user
->logOut();
121 $ret = $this->doApiRequest( array(
123 "lgname" => $user->username
,
124 "lgpassword" => $user->password
,
129 $this->assertNotInternalType( "bool", $result );
130 $this->assertNotInternalType( "null", $result["login"] );
132 $a = $result["login"]["result"];
133 $this->assertEquals( "NeedToken", $a );
134 $token = $result["login"]["token"];
136 $ret = $this->doApiRequest( array(
139 "lgname" => $user->username
,
140 "lgpassword" => $user->password
,
146 $this->assertNotInternalType( "bool", $result );
147 $a = $result["login"]["result"];
149 $this->assertEquals( "Success", $a );
155 function testApiGotCookie() {
156 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
158 global $wgServer, $wgScriptPath;
160 if ( !isset( $wgServer ) ) {
161 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
163 $user = self
::$users['sysop'];
165 $req = MWHttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
166 array( "method" => "POST",
168 "lgname" => $user->username
,
169 "lgpassword" => $user->password
) ) );
172 libxml_use_internal_errors( true );
173 $sxe = simplexml_load_string( $req->getContent() );
174 $this->assertNotInternalType( "bool", $sxe );
175 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
176 $this->assertNotInternalType( "null", $sxe->login
[0] );
178 $a = $sxe->login
[0]->attributes()->result
[0];
179 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
180 $token = (string)$sxe->login
[0]->attributes()->token
;
182 $req->setData( array(
184 "lgname" => $user->username
,
185 "lgpassword" => $user->password
) );
188 $cj = $req->getCookieJar();
189 $serverName = parse_url( $wgServer, PHP_URL_HOST
);
190 $this->assertNotEquals( false, $serverName );
191 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
192 $this->assertNotEquals( '', $serializedCookie );
193 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName
. '; .*Token=/', $serializedCookie );
198 function testRunLogin() {
199 $sysopUser = self
::$users['sysop'];
200 $data = $this->doApiRequest( array(
202 'lgname' => $sysopUser->username
,
203 'lgpassword' => $sysopUser->password
) );
205 $this->assertArrayHasKey( "login", $data[0] );
206 $this->assertArrayHasKey( "result", $data[0]['login'] );
207 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
208 $token = $data[0]['login']['token'];
210 $data = $this->doApiRequest( array(
213 "lgname" => $sysopUser->username
,
214 "lgpassword" => $sysopUser->password
), $data[2] );
216 $this->assertArrayHasKey( "login", $data[0] );
217 $this->assertArrayHasKey( "result", $data[0]['login'] );
218 $this->assertEquals( "Success", $data[0]['login']['result'] );
219 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
224 function testGettingToken() {
225 foreach ( self
::$users as $user ) {
226 $this->runTokenTest( $user );
230 function runTokenTest( $user ) {
232 $data = $this->getTokenList( $user );
234 $this->assertArrayHasKey( 'query', $data[0] );
235 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
236 $keys = array_keys( $data[0]['query']['pages'] );
237 $key = array_pop( $keys );
239 $rights = $user->user
->getRights();
241 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
242 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
243 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
245 if ( isset( $rights['delete'] ) ) {
246 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
249 if ( isset( $rights['block'] ) ) {
250 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
251 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
254 if ( isset( $rights['protect'] ) ) {
255 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );