6579fe3194b317a30eadf533295d8a9b219a3808
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,
20 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->assertNotType( "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
::$passWord
84 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
87 function testApiLoginBadPass() {
90 if ( !isset( $wgServer ) ) {
91 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
93 $ret = $this->doApiRequest( array(
95 "lgname" => self
::$userName,
96 "lgpassword" => "bad",
102 $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" => self
::$userName,
113 "lgpassword" => "bad",
119 $this->assertNotType( "bool", $result );
120 $a = $result["login"]["result"];
122 $this->assertEquals( "NeedToken", $a );
125 function testApiLoginGoodPass() {
128 if ( !isset( $wgServer ) ) {
129 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
132 $ret = $this->doApiRequest( array(
134 "lgname" => self
::$userName,
135 "lgpassword" => self
::$passWord,
140 $this->assertNotType( "bool", $result );
141 $this->assertNotType( "null", $result["login"] );
143 $a = $result["login"]["result"];
144 $this->assertEquals( "NeedToken", $a );
145 $token = $result["login"]["token"];
147 $ret = $this->doApiRequest( array(
150 "lgname" => self
::$userName,
151 "lgpassword" => self
::$passWord,
157 $this->assertNotType( "bool", $result );
158 $a = $result["login"]["result"];
160 $this->assertEquals( "Success", $a );
163 function testApiGotCookie() {
164 global $wgServer, $wgScriptPath;
166 if ( !isset( $wgServer ) ) {
167 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
169 $req = HttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
170 array( "method" => "POST",
172 "lgname" => self
::$userName,
173 "lgpassword" => self
::$passWord ) ) );
176 libxml_use_internal_errors( true );
177 $sxe = simplexml_load_string( $req->getContent() );
178 $this->assertNotType( "bool", $sxe );
179 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
180 $this->assertNotType( "null", $sxe->login
[0] );
182 $a = $sxe->login
[0]->attributes()->result
[0];
183 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
184 $token = (string)$sxe->login
[0]->attributes()->token
;
186 $req->setData( array(
188 "lgname" => self
::$userName,
189 "lgpassword" => self
::$passWord ) );
192 $cj = $req->getCookieJar();
193 $serverName = parse_url( $wgServer, PHP_URL_HOST
);
194 $this->assertNotEquals( false, $serverName );
195 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
196 $this->assertNotEquals( '', $serializedCookie );
197 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self
::$userName . '; .*Token=/', $serializedCookie );
203 * @depends testApiGotCookie
205 function testApiListPages( CookieJar
$cj ) {
206 $this->markTestIncomplete( "Not done with this yet" );
209 if ( $wgServer == "http://localhost" ) {
210 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
212 $req = HttpRequest
::factory( self
::$apiUrl . "?action=query&format=xml&prop=revisions&" .
213 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
214 $req->setCookieJar( $cj );
216 libxml_use_internal_errors( true );
217 $sxe = simplexml_load_string( $req->getContent() );
218 $this->assertNotType( "bool", $sxe );
219 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
220 $a = $sxe->query
[0]->pages
[0]->page
[0]->attributes();