Stylize maintenance folder..
[mediawiki.git] / maintenance / tests / ApiTest.php
blob5733528831742dacac7e19100bc80a79662c56c0
1 <?php
3 require_once( "ApiSetup.php" );
5 class MockApi extends ApiBase {
6 public function execute() { }
7 public function getVersion() { }
9 public function __construct() { }
11 public function getAllowedParams() {
12 $params = array(
13 'filename' => null,
14 'enablechunks' => false,
15 'sessionkey' => null,
23 class ApiTest extends ApiSetup {
25 function setup() {
26 parent::setup();
29 function testRequireOnlyOneParameterDefault() {
30 $mock = new MockApi();
32 $this->assertEquals(
33 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
34 "enablechunks" => false ), "filename", "enablechunks" ) );
37 /**
38 * @expectedException UsageException
40 function testRequireOnlyOneParameterZero() {
41 $mock = new MockApi();
43 $this->assertEquals(
44 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
45 "enablechunks" => 0 ), "filename", "enablechunks" ) );
48 /**
49 * @expectedException UsageException
51 function testRequireOnlyOneParameterTrue() {
52 $mock = new MockApi();
54 $this->assertEquals(
55 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
56 "enablechunks" => true ), "filename", "enablechunks" ) );
59 function testApi() {
60 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
62 if ( $wgDBprefix === "parsertest_" || ( $wgDBtype == 'oracle' && $wgDBprefix === 'pt_' ) )
63 $this->markTestSkipped( "This test can't (yet?) be run with the parser tests" );
64 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
65 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
66 'be set in LocalSettings.php' );
68 /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
69 $resp = Http::get( self::$apiUrl . "?format=xml" );
71 libxml_use_internal_errors( true );
72 $sxe = simplexml_load_string( $resp );
73 $this->assertNotType( "bool", $sxe );
74 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
77 function testApiLoginNoName() {
78 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
80 if ( $wgDBprefix === "parsertest_" || ( $wgDBtype == 'oracle' && $wgDBprefix === 'pt_' ) )
81 $this->markTestSkipped( "This test can't (yet?) be run with the parser tests" );
82 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
83 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
84 'be set in LocalSettings.php' );
86 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
87 array( "postData" => array(
88 "lgname" => "",
89 "lgpassword" => self::$passWord ) ) );
90 libxml_use_internal_errors( true );
91 $sxe = simplexml_load_string( $resp );
92 $this->assertNotType( "bool", $sxe );
93 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
94 $a = $sxe->login[0]->attributes()->result;
95 $this->assertEquals( ' result="NoName"', $a->asXML() );
98 function testApiLoginBadPass() {
99 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
101 if ( $wgDBprefix === "parsertest_" || ( $wgDBtype == 'oracle' && $wgDBprefix === 'pt_' ) )
102 $this->markTestSkipped( "This test can't (yet?) be run with the parser tests" );
103 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
104 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
105 'be set in LocalSettings.php' );
107 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
108 array( "postData" => array(
109 "lgname" => self::$userName,
110 "lgpassword" => "bad" ) ) );
111 libxml_use_internal_errors( true );
112 $sxe = simplexml_load_string( $resp );
113 $this->assertNotType( "bool", $sxe );
114 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
115 $a = $sxe->login[0]->attributes()->result[0];
116 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
118 $token = (string)$sxe->login[0]->attributes()->token;
120 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
121 array( "postData" => array(
122 "lgtoken" => $token,
123 "lgname" => self::$userName,
124 "lgpassword" => "bad" ) ) );
127 $sxe = simplexml_load_string( $resp );
128 $this->assertNotType( "bool", $sxe );
129 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
130 $a = $sxe->login[0]->attributes()->result[0];
132 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
135 function testApiLoginGoodPass() {
136 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
138 if ( $wgDBprefix === "parsertest_" || ( $wgDBtype == 'oracle' && $wgDBprefix === 'pt_' ) )
139 $this->markTestSkipped( "This test can't (yet?) be run with the parser tests" );
140 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
141 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
142 'be set in LocalSettings.php' );
144 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
145 array( "method" => "POST",
146 "postData" => array(
147 "lgname" => self::$userName,
148 "lgpassword" => self::$passWord ) ) );
149 $req->execute();
151 libxml_use_internal_errors( true );
152 $sxe = simplexml_load_string( $req->getContent() );
153 $this->assertNotType( "bool", $sxe );
154 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
156 $a = $sxe->login[0]->attributes()->result[0];
157 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
158 $token = (string)$sxe->login[0]->attributes()->token;
160 $req->setData( array(
161 "lgtoken" => $token,
162 "lgname" => self::$userName,
163 "lgpassword" => self::$passWord ) );
164 $req->execute();
166 $sxe = simplexml_load_string( $req->getContent() );
168 $this->assertNotType( "bool", $sxe );
169 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
170 $a = $sxe->login[0]->attributes()->result[0];
172 $this->assertEquals( ' result="Success"', $a->asXML() );
175 function testApiGotCookie() {
176 global $wgServerName, $wgServer, $wgScriptPath, $wgDBprefix, $wgDBtype;
178 if ( $wgDBprefix === "parsertest_" || ( $wgDBtype == 'oracle' && $wgDBprefix === 'pt_' ) )
179 $this->markTestSkipped( "This test can't (yet?) be run with the parser tests" );
180 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
181 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
182 'be set in LocalSettings.php' );
184 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
185 array( "method" => "POST",
186 "postData" => array(
187 "lgname" => self::$userName,
188 "lgpassword" => self::$passWord ) ) );
189 $req->execute();
191 libxml_use_internal_errors( true );
192 $sxe = simplexml_load_string( $req->getContent() );
193 $this->assertNotType( "bool", $sxe );
194 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
196 $a = $sxe->login[0]->attributes()->result[0];
197 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
198 $token = (string)$sxe->login[0]->attributes()->token;
200 $req->setData( array(
201 "lgtoken" => $token,
202 "lgname" => self::$userName,
203 "lgpassword" => self::$passWord ) );
204 $req->execute();
206 $cj = $req->getCookieJar();
207 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
208 $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );
211 return $cj;
215 * @depends testApiGotCookie
217 function testApiListPages( CookieJar $cj ) {
218 $this->markTestIncomplete( "Not done with this yet" );
219 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
221 if ( $wgDBprefix === "parsertest_" || ( $wgDBtype == 'oracle' && $wgDBprefix === 'pt_' ) )
222 $this->markTestSkipped( "This test can't (yet?) be run with the parser tests" );
223 if ( $wgServerName == "localhost" || $wgServer == "http://localhost" ) {
224 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
225 'be set in LocalSettings.php' );
227 $req = HttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&" .
228 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
229 $req->setCookieJar( $cj );
230 $req->execute();
231 libxml_use_internal_errors( true );
232 $sxe = simplexml_load_string( $req->getContent() );
233 $this->assertNotType( "bool", $sxe );
234 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
235 $a = $sxe->query[0]->pages[0]->page[0]->attributes();