ee77c86985ae7cb562d0510393985df9b6cae52b
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 global $wgServerName, $wgServer;
59 if ( !isset( $wgServerName ) ||
!isset( $wgServer ) ) {
60 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
61 'be set in LocalSettings.php' );
63 /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
64 $resp = Http
::get( self
::$apiUrl . "?format=xml" );
66 libxml_use_internal_errors( true );
67 $sxe = simplexml_load_string( $resp );
68 $this->assertNotType( "bool", $sxe );
69 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
72 function testApiLoginNoName() {
73 global $wgServerName, $wgServer;
75 if ( !isset( $wgServerName ) ||
!isset( $wgServer ) ) {
76 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
77 'be set in LocalSettings.php' );
79 $resp = Http
::post( self
::$apiUrl . "?action=login&format=xml",
80 array( "postData" => array(
82 "lgpassword" => self
::$passWord ) ) );
83 libxml_use_internal_errors( true );
84 $sxe = simplexml_load_string( $resp );
85 $this->assertNotType( "bool", $sxe );
86 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
87 $a = $sxe->login
[0]->attributes()->result
;
88 $this->assertEquals( ' result="NoName"', $a->asXML() );
91 function testApiLoginBadPass() {
92 global $wgServerName, $wgServer;
94 if ( !isset( $wgServerName ) ||
!isset( $wgServer ) ) {
95 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
96 'be set in LocalSettings.php' );
98 $resp = Http
::post( self
::$apiUrl . "?action=login&format=xml",
99 array( "postData" => array(
100 "lgname" => self
::$userName,
101 "lgpassword" => "bad" ) ) );
102 libxml_use_internal_errors( true );
103 $sxe = simplexml_load_string( $resp );
104 $this->assertNotType( "bool", $sxe );
105 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
106 $a = $sxe->login
[0]->attributes()->result
[0];
107 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
109 $token = (string)$sxe->login
[0]->attributes()->token
;
111 $resp = Http
::post( self
::$apiUrl . "?action=login&format=xml",
112 array( "postData" => array(
114 "lgname" => self
::$userName,
115 "lgpassword" => "bad" ) ) );
118 $sxe = simplexml_load_string( $resp );
119 $this->assertNotType( "bool", $sxe );
120 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
121 $a = $sxe->login
[0]->attributes()->result
[0];
123 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
126 function testApiLoginGoodPass() {
127 global $wgServerName, $wgServer;
129 if ( !isset( $wgServerName ) ||
!isset( $wgServer ) ) {
130 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
131 'be set in LocalSettings.php' );
133 $req = HttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
134 array( "method" => "POST",
136 "lgname" => self
::$userName,
137 "lgpassword" => self
::$passWord ) ) );
140 libxml_use_internal_errors( true );
141 $sxe = simplexml_load_string( $req->getContent() );
142 $this->assertNotType( "bool", $sxe );
143 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
144 $this->assertNotType( "null", $sxe->login
[0] );
146 $a = $sxe->login
[0]->attributes()->result
[0];
147 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
148 $token = (string)$sxe->login
[0]->attributes()->token
;
150 $req->setData( array(
152 "lgname" => self
::$userName,
153 "lgpassword" => self
::$passWord ) );
156 $sxe = simplexml_load_string( $req->getContent() );
158 $this->assertNotType( "bool", $sxe );
159 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
160 $a = $sxe->login
[0]->attributes()->result
[0];
162 $this->assertEquals( ' result="Success"', $a->asXML() );
165 function testApiGotCookie() {
166 global $wgServerName, $wgServer, $wgScriptPath;
168 if ( !isset( $wgServerName ) ||
!isset( $wgServer ) ) {
169 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
170 'be set in LocalSettings.php' );
172 $req = HttpRequest
::factory( self
::$apiUrl . "?action=login&format=xml",
173 array( "method" => "POST",
175 "lgname" => self
::$userName,
176 "lgpassword" => self
::$passWord ) ) );
179 libxml_use_internal_errors( true );
180 $sxe = simplexml_load_string( $req->getContent() );
181 $this->assertNotType( "bool", $sxe );
182 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
183 $this->assertNotType( "null", $sxe->login
[0] );
185 $a = $sxe->login
[0]->attributes()->result
[0];
186 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
187 $token = (string)$sxe->login
[0]->attributes()->token
;
189 $req->setData( array(
191 "lgname" => self
::$userName,
192 "lgpassword" => self
::$passWord ) );
195 $cj = $req->getCookieJar();
196 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName );
197 $this->assertNotEquals( '', $serializedCookie );
198 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self
::$userName . '; .*Token=/', $serializedCookie );
205 * @depends testApiGotCookie
207 function testApiListPages( CookieJar
$cj ) {
208 $this->markTestIncomplete( "Not done with this yet" );
209 global $wgServerName, $wgServer;
211 if ( $wgServerName == "localhost" ||
$wgServer == "http://localhost" ) {
212 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
213 'be set in LocalSettings.php' );
215 $req = HttpRequest
::factory( self
::$apiUrl . "?action=query&format=xml&prop=revisions&" .
216 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
217 $req->setCookieJar( $cj );
219 libxml_use_internal_errors( true );
220 $sxe = simplexml_load_string( $req->getContent() );
221 $this->assertNotType( "bool", $sxe );
222 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
223 $a = $sxe->query
[0]->pages
[0]->page
[0]->attributes();