3 require_once( dirname( __FILE__
) . '/ApiSetup.php' );
5 class MockApi
extends ApiBase
{
6 public function execute() { }
7 public function getVersion() { }
9 public function __construct() { }
11 public function getAllowedParams() {
14 'enablechunks' => false,
23 class ApiTest
extends ApiTestSetup
{
25 function testRequireOnlyOneParameterDefault() {
26 $mock = new MockApi();
29 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
30 "enablechunks" => false ), "filename", "enablechunks" ) );
34 * @expectedException UsageException
36 function testRequireOnlyOneParameterZero() {
37 $mock = new MockApi();
40 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
41 "enablechunks" => 0 ), "filename", "enablechunks" ) );
45 * @expectedException UsageException
47 function testRequireOnlyOneParameterTrue() {
48 $mock = new MockApi();
51 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
52 "enablechunks" => true ), "filename", "enablechunks" ) );
56 * Test that the API will accept a FauxRequest and execute. The help action
57 * (default) throws a UsageException. Just validate we're getting proper XML
59 * @expectedException UsageException
63 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
66 $api->getPrinter()->setBufferResult( true );
67 $api->printResult( false );
68 $resp = $api->getPrinter()->getBuffer();
70 libxml_use_internal_errors( true );
71 $sxe = simplexml_load_string( $resp );
72 $this->assertNotType( "bool", $sxe );
73 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
77 * Test result of attempted login with an empty username
79 function testApiLoginNoName() {
80 $data = $this->doApiRequest( array( 'action' => 'login',
81 'lgname' => '', 'lgpassword' => self
::$user->password
,
83 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
86 function testApiLoginBadPass() {
91 if ( !isset( $wgServer ) ) {
92 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
94 $ret = $this->doApiRequest( array(
96 "lgname" => $user->userName
,
97 "lgpassword" => "bad",
103 $this->assertNotType( "bool", $result );
104 $a = $result["login"]["result"];
105 $this->assertEquals( "NeedToken", $a );
107 $token = $result["login"]["token"];
109 $ret = $this->doApiRequest( array(
112 "lgname" => $user->userName
,
113 "lgpassword" => "bad",
119 $this->assertNotType( "bool", $result );
120 $a = $result["login"]["result"];
122 $this->assertEquals( "WrongPass", $a );
125 function testApiLoginGoodPass() {
128 if ( !isset( $wgServer ) ) {
129 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
134 $ret = $this->doApiRequest( array(
136 "lgname" => $user->userName
,
137 "lgpassword" => $user->password
,
142 $this->assertNotType( "bool", $result );
143 $this->assertNotType( "null", $result["login"] );
145 $a = $result["login"]["result"];
146 $this->assertEquals( "NeedToken", $a );
147 $token = $result["login"]["token"];
149 $ret = $this->doApiRequest( array(
152 "lgname" => $user->userName
,
153 "lgpassword" => $user->password
,
159 $this->assertNotType( "bool", $result );
160 $a = $result["login"]["result"];
162 $this->assertEquals( "Success", $a );
165 function testApiGotCookie() {
166 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
168 global $wgServer, $wgScriptPath;
170 if ( !isset( $wgServer ) ) {
171 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
173 $req = HttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
174 array( "method" => "POST",
176 "lgname" => self
::$user->userName
,
177 "lgpassword" => self
::$user->password
) ) );
180 libxml_use_internal_errors( true );
181 $sxe = simplexml_load_string( $req->getContent() );
182 $this->assertNotType( "bool", $sxe );
183 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
184 $this->assertNotType( "null", $sxe->login
[0] );
186 $a = $sxe->login
[0]->attributes()->result
[0];
187 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
188 $token = (string)$sxe->login
[0]->attributes()->token
;
190 $req->setData( array(
192 "lgname" => self
::$user->userName
,
193 "lgpassword" => self
::$user->password
) );
196 $cj = $req->getCookieJar();
197 $serverName = parse_url( $wgServer, PHP_URL_HOST
);
198 $this->assertNotEquals( false, $serverName );
199 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
200 $this->assertNotEquals( '', $serializedCookie );
201 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self
::$user->userName
. '; .*Token=/', $serializedCookie );
207 * @depends testApiGotCookie
209 function testApiListPages( CookieJar
$cj ) {
210 $this->markTestIncomplete( "Not done with this yet" );
213 if ( $wgServer == "http://localhost" ) {
214 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
216 $req = HttpRequest
::factory( self
::$apiUrl . "?action=query&format=xml&prop=revisions&" .
217 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
218 $req->setCookieJar( $cj );
220 libxml_use_internal_errors( true );
221 $sxe = simplexml_load_string( $req->getContent() );
222 $this->assertNotType( "bool", $sxe );
223 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
224 $a = $sxe->query
[0]->pages
[0]->page
[0]->attributes();