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,
24 class ApiTest
extends ApiTestSetup
{
26 function testRequireOnlyOneParameterDefault() {
27 $mock = new MockApi();
30 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
31 "enablechunks" => false ), "filename", "enablechunks" ) );
35 * @expectedException UsageException
37 function testRequireOnlyOneParameterZero() {
38 $mock = new MockApi();
41 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
42 "enablechunks" => 0 ), "filename", "enablechunks" ) );
46 * @expectedException UsageException
48 function testRequireOnlyOneParameterTrue() {
49 $mock = new MockApi();
52 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
53 "enablechunks" => true ), "filename", "enablechunks" ) );
57 * Test that the API will accept a FauxRequest and execute. The help action
58 * (default) throws a UsageException. Just validate we're getting proper XML
60 * @expectedException UsageException
64 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
67 $api->getPrinter()->setBufferResult( true );
68 $api->printResult( false );
69 $resp = $api->getPrinter()->getBuffer();
71 libxml_use_internal_errors( true );
72 $sxe = simplexml_load_string( $resp );
73 $this->assertNotInternalType( "bool", $sxe );
74 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
78 * Test result of attempted login with an empty username
80 function testApiLoginNoName() {
81 $data = $this->doApiRequest( array( 'action' => 'login',
82 'lgname' => '', 'lgpassword' => self
::$user->password
,
84 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
87 function testApiLoginBadPass() {
92 if ( !isset( $wgServer ) ) {
93 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
95 $ret = $this->doApiRequest( array(
97 "lgname" => $user->userName
,
98 "lgpassword" => "bad",
104 $this->assertNotInternalType( "bool", $result );
105 $a = $result["login"]["result"];
106 $this->assertEquals( "NeedToken", $a );
108 $token = $result["login"]["token"];
110 $ret = $this->doApiRequest( array(
113 "lgname" => $user->userName
,
114 "lgpassword" => "bad",
120 $this->assertNotInternalType( "bool", $result );
121 $a = $result["login"]["result"];
123 $this->assertEquals( "WrongPass", $a );
126 function testApiLoginGoodPass() {
129 if ( !isset( $wgServer ) ) {
130 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
135 $ret = $this->doApiRequest( array(
137 "lgname" => $user->userName
,
138 "lgpassword" => $user->password
,
143 $this->assertNotInternalType( "bool", $result );
144 $this->assertNotInternalType( "null", $result["login"] );
146 $a = $result["login"]["result"];
147 $this->assertEquals( "NeedToken", $a );
148 $token = $result["login"]["token"];
150 $ret = $this->doApiRequest( array(
153 "lgname" => $user->userName
,
154 "lgpassword" => $user->password
,
160 $this->assertNotInternalType( "bool", $result );
161 $a = $result["login"]["result"];
163 $this->assertEquals( "Success", $a );
166 function testApiGotCookie() {
167 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
169 global $wgServer, $wgScriptPath;
171 if ( !isset( $wgServer ) ) {
172 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
174 $req = MWHttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
175 array( "method" => "POST",
177 "lgname" => self
::$user->userName
,
178 "lgpassword" => self
::$user->password
) ) );
181 libxml_use_internal_errors( true );
182 $sxe = simplexml_load_string( $req->getContent() );
183 $this->assertNotInternalType( "bool", $sxe );
184 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
185 $this->assertNotInternalType( "null", $sxe->login
[0] );
187 $a = $sxe->login
[0]->attributes()->result
[0];
188 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
189 $token = (string)$sxe->login
[0]->attributes()->token
;
191 $req->setData( array(
193 "lgname" => self
::$user->userName
,
194 "lgpassword" => self
::$user->password
) );
197 $cj = $req->getCookieJar();
198 $serverName = parse_url( $wgServer, PHP_URL_HOST
);
199 $this->assertNotEquals( false, $serverName );
200 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
201 $this->assertNotEquals( '', $serializedCookie );
202 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self
::$user->userName
. '; .*Token=/', $serializedCookie );
208 * @depends testApiGotCookie
210 function testApiListPages( CookieJar
$cj ) {
211 $this->markTestIncomplete( "Not done with this yet" );
214 if ( $wgServer == "http://localhost" ) {
215 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
217 $req = MWHttpRequest
::factory( self
::$apiUrl . "?action=query&format=xml&prop=revisions&" .
218 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
219 $req->setCookieJar( $cj );
221 libxml_use_internal_errors( true );
222 $sxe = simplexml_load_string( $req->getContent() );
223 $this->assertNotInternalType( "bool", $sxe );
224 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
225 $a = $sxe->query
[0]->pages
[0]->page
[0]->attributes();