w/s changes
[lhc/web/wiklou.git] / maintenance / tests / ApiTest.php
1 <?php
2
3 class MockApi extends ApiBase {
4 public function execute() { }
5 public function getVersion() { }
6
7 public function __construct() { }
8
9 public function getAllowedParams() {
10 return array(
11 'filename' => null,
12 'enablechunks' => false,
13 'sessionkey' => null,
14 );
15 }
16 }
17
18 class ApiTest extends ApiTestSetup {
19
20 function setup() {
21 parent::setup();
22 }
23
24 function testRequireOnlyOneParameterDefault() {
25 $mock = new MockApi();
26
27 $this->assertEquals(
28 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
29 "enablechunks" => false ), "filename", "enablechunks" ) );
30 }
31
32 /**
33 * @expectedException UsageException
34 */
35 function testRequireOnlyOneParameterZero() {
36 $mock = new MockApi();
37
38 $this->assertEquals(
39 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
40 "enablechunks" => 0 ), "filename", "enablechunks" ) );
41 }
42
43 /**
44 * @expectedException UsageException
45 */
46 function testRequireOnlyOneParameterTrue() {
47 $mock = new MockApi();
48
49 $this->assertEquals(
50 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
51 "enablechunks" => true ), "filename", "enablechunks" ) );
52 }
53
54 function testApi() {
55 global $wgServerName, $wgServer;
56
57 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
58 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
59 'be set in LocalSettings.php' );
60 }
61 /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
62 $resp = Http::get( self::$apiUrl . "?format=xml" );
63
64 libxml_use_internal_errors( true );
65 $sxe = simplexml_load_string( $resp );
66 $this->assertNotType( "bool", $sxe );
67 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
68 }
69
70 function testApiLoginNoName() {
71 global $wgServerName, $wgServer;
72
73 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
74 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
75 'be set in LocalSettings.php' );
76 }
77 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
78 array( "postData" => array(
79 "lgname" => "",
80 "lgpassword" => self::$passWord ) ) );
81 libxml_use_internal_errors( true );
82 $sxe = simplexml_load_string( $resp );
83 $this->assertNotType( "bool", $sxe );
84 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
85 $a = $sxe->login[0]->attributes()->result;
86 $this->assertEquals( ' result="NoName"', $a->asXML() );
87 }
88
89 function testApiLoginBadPass() {
90 global $wgServerName, $wgServer;
91
92 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
93 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
94 'be set in LocalSettings.php' );
95 }
96 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
97 array( "postData" => array(
98 "lgname" => self::$userName,
99 "lgpassword" => "bad" ) ) );
100 libxml_use_internal_errors( true );
101 $sxe = simplexml_load_string( $resp );
102 $this->assertNotType( "bool", $sxe );
103 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
104 $a = $sxe->login[0]->attributes()->result[0];
105 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
106
107 $token = (string)$sxe->login[0]->attributes()->token;
108
109 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
110 array( "postData" => array(
111 "lgtoken" => $token,
112 "lgname" => self::$userName,
113 "lgpassword" => "bad" ) ) );
114
115
116 $sxe = simplexml_load_string( $resp );
117 $this->assertNotType( "bool", $sxe );
118 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
119 $a = $sxe->login[0]->attributes()->result[0];
120
121 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
122 }
123
124 function testApiLoginGoodPass() {
125 global $wgServerName, $wgServer;
126
127 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
128 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
129 'be set in LocalSettings.php' );
130 }
131 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
132 array( "method" => "POST",
133 "postData" => array(
134 "lgname" => self::$userName,
135 "lgpassword" => self::$passWord ) ) );
136 $req->execute();
137
138 libxml_use_internal_errors( true );
139 $sxe = simplexml_load_string( $req->getContent() );
140 $this->assertNotType( "bool", $sxe );
141 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
142
143 $a = $sxe->login[0]->attributes()->result[0];
144 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
145 $token = (string)$sxe->login[0]->attributes()->token;
146
147 $req->setData( array(
148 "lgtoken" => $token,
149 "lgname" => self::$userName,
150 "lgpassword" => self::$passWord ) );
151 $req->execute();
152
153 $sxe = simplexml_load_string( $req->getContent() );
154
155 $this->assertNotType( "bool", $sxe );
156 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
157 $a = $sxe->login[0]->attributes()->result[0];
158
159 $this->assertEquals( ' result="Success"', $a->asXML() );
160 }
161
162 function testApiGotCookie() {
163 global $wgServerName, $wgServer, $wgScriptPath;
164
165 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
166 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
167 'be set in LocalSettings.php' );
168 }
169 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
170 array( "method" => "POST",
171 "postData" => array(
172 "lgname" => self::$userName,
173 "lgpassword" => self::$passWord ) ) );
174 $req->execute();
175
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
181 $a = $sxe->login[0]->attributes()->result[0];
182 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
183 $token = (string)$sxe->login[0]->attributes()->token;
184
185 $req->setData( array(
186 "lgtoken" => $token,
187 "lgname" => self::$userName,
188 "lgpassword" => self::$passWord ) );
189 $req->execute();
190
191 $cj = $req->getCookieJar();
192 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . self::$userName . '; .*Token=/',
193 $cj->serializeToHttpRequest( $wgScriptPath, $wgServerName ) );
194
195
196 return $cj;
197 }
198
199 /**
200 * @depends testApiGotCookie
201 */
202 function testApiListPages( CookieJar $cj ) {
203 $this->markTestIncomplete( "Not done with this yet" );
204 global $wgServerName, $wgServer;
205
206 if ( $wgServerName == "localhost" || $wgServer == "http://localhost" ) {
207 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
208 'be set in LocalSettings.php' );
209 }
210 $req = HttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&" .
211 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
212 $req->setCookieJar( $cj );
213 $req->execute();
214 libxml_use_internal_errors( true );
215 $sxe = simplexml_load_string( $req->getContent() );
216 $this->assertNotType( "bool", $sxe );
217 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
218 $a = $sxe->query[0]->pages[0]->page[0]->attributes();
219 }
220 }