* Fix a bug to keep consecutive HTTP requests from sharing results
[lhc/web/wiklou.git] / maintenance / tests / ApiTest.php
1 <?php
2
3 require_once( "ApiSetup.php" );
4
5 class MockApi extends ApiBase {
6 public function execute() {}
7 public function getVersion() {}
8
9 public function __construct() {}
10
11 public function getAllowedParams() {
12 $params = array(
13 'filename' => null,
14 'enablechunks' => false,
15 'sessionkey' => null,
16 );
17 }
18
19
20 }
21
22
23 class ApiTest extends ApiSetup {
24
25 function setup() {
26 parent::setup();
27 }
28
29 function testRequireOnlyOneParameterDefault() {
30 $mock = new MockApi();
31
32 $this->assertEquals(
33 null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt",
34 "enablechunks" => false), "filename", "enablechunks"));
35 }
36
37 /**
38 * @expectedException UsageException
39 */
40 function testRequireOnlyOneParameterZero() {
41 $mock = new MockApi();
42
43 $this->assertEquals(
44 null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt",
45 "enablechunks" => 0), "filename", "enablechunks"));
46 }
47
48 /**
49 * @expectedException UsageException
50 */
51 function testRequireOnlyOneParameterTrue() {
52 $mock = new MockApi();
53
54 $this->assertEquals(
55 null, $mock->requireOnlyOneParameter(array("filename" => "foo.txt",
56 "enablechunks" => true), "filename", "enablechunks"));
57 }
58
59 function testApi() {
60 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
61
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');
67 }
68 /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
69 $resp = Http::get( self::$apiUrl . "?format=xml" );
70
71 libxml_use_internal_errors( true );
72 $sxe = simplexml_load_string( $resp );
73 $this->assertNotType( "bool", $sxe );
74 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
75 }
76
77 function testApiLoginNoName() {
78 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
79
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');
85 }
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() );
96 }
97
98 function testApiLoginBadPass() {
99 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
100
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');
106 }
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() );
117
118 $token = (string)$sxe->login[0]->attributes()->token;
119
120 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
121 array( "postData" => array(
122 "lgtoken" => $token,
123 "lgname" => self::$userName,
124 "lgpassword" => "bad" ) ) );
125
126
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];
131
132 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
133 }
134
135 function testApiLoginGoodPass() {
136 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
137
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');
143 }
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();
150
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" ) );
155
156 $a = $sxe->login[0]->attributes()->result[0];
157 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
158 $token = (string)$sxe->login[0]->attributes()->token;
159
160 $req->setData(array(
161 "lgtoken" => $token,
162 "lgname" => self::$userName,
163 "lgpassword" => self::$passWord ) );
164 $req->execute();
165
166 $sxe = simplexml_load_string( $req->getContent() );
167
168 $this->assertNotType( "bool", $sxe );
169 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
170 $a = $sxe->login[0]->attributes()->result[0];
171
172 $this->assertEquals( ' result="Success"', $a->asXML() );
173 }
174
175 function testApiGotCookie() {
176 global $wgServerName, $wgServer, $wgScriptPath, $wgDBprefix, $wgDBtype;
177
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');
183 }
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();
190
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" ) );
195
196 $a = $sxe->login[0]->attributes()->result[0];
197 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
198 $token = (string)$sxe->login[0]->attributes()->token;
199
200 $req->setData(array(
201 "lgtoken" => $token,
202 "lgname" => self::$userName,
203 "lgpassword" => self::$passWord ) );
204 $req->execute();
205
206 $cj = $req->getCookieJar();
207 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
208 $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );
209
210
211 return $cj;
212 }
213
214 /**
215 * @depends testApiGotCookie
216 */
217 function testApiListPages(CookieJar $cj) {
218 $this->markTestIncomplete("Not done with this yet");
219 global $wgServerName, $wgServer, $wgDBprefix, $wgDBtype;
220
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');
226 }
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();
236 }
237 }