ee77c86985ae7cb562d0510393985df9b6cae52b
[lhc/web/wiklou.git] / maintenance / tests / phpunit / includes / api / ApiTest.php
1 <?php
2
3 require_once( dirname( __FILE__ ) . '/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 return array(
13 'filename' => null,
14 'enablechunks' => false,
15 'sessionkey' => null,
16 );
17 }
18 }
19
20 class ApiTest extends ApiTestSetup {
21
22 function setup() {
23 parent::setup();
24 }
25
26 function testRequireOnlyOneParameterDefault() {
27 $mock = new MockApi();
28
29 $this->assertEquals(
30 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
31 "enablechunks" => false ), "filename", "enablechunks" ) );
32 }
33
34 /**
35 * @expectedException UsageException
36 */
37 function testRequireOnlyOneParameterZero() {
38 $mock = new MockApi();
39
40 $this->assertEquals(
41 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
42 "enablechunks" => 0 ), "filename", "enablechunks" ) );
43 }
44
45 /**
46 * @expectedException UsageException
47 */
48 function testRequireOnlyOneParameterTrue() {
49 $mock = new MockApi();
50
51 $this->assertEquals(
52 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
53 "enablechunks" => true ), "filename", "enablechunks" ) );
54 }
55
56 function testApi() {
57 global $wgServerName, $wgServer;
58
59 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
60 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
61 'be set in LocalSettings.php' );
62 }
63 /* Haven't thought about test ordering yet -- but this depends on HttpTest.php */
64 $resp = Http::get( self::$apiUrl . "?format=xml" );
65
66 libxml_use_internal_errors( true );
67 $sxe = simplexml_load_string( $resp );
68 $this->assertNotType( "bool", $sxe );
69 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
70 }
71
72 function testApiLoginNoName() {
73 global $wgServerName, $wgServer;
74
75 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
76 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
77 'be set in LocalSettings.php' );
78 }
79 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
80 array( "postData" => array(
81 "lgname" => "",
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() );
89 }
90
91 function testApiLoginBadPass() {
92 global $wgServerName, $wgServer;
93
94 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
95 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
96 'be set in LocalSettings.php' );
97 }
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() );
108
109 $token = (string)$sxe->login[0]->attributes()->token;
110
111 $resp = Http::post( self::$apiUrl . "?action=login&format=xml",
112 array( "postData" => array(
113 "lgtoken" => $token,
114 "lgname" => self::$userName,
115 "lgpassword" => "bad" ) ) );
116
117
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];
122
123 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
124 }
125
126 function testApiLoginGoodPass() {
127 global $wgServerName, $wgServer;
128
129 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
130 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
131 'be set in LocalSettings.php' );
132 }
133 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
134 array( "method" => "POST",
135 "postData" => array(
136 "lgname" => self::$userName,
137 "lgpassword" => self::$passWord ) ) );
138 $req->execute();
139
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] );
145
146 $a = $sxe->login[0]->attributes()->result[0];
147 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
148 $token = (string)$sxe->login[0]->attributes()->token;
149
150 $req->setData( array(
151 "lgtoken" => $token,
152 "lgname" => self::$userName,
153 "lgpassword" => self::$passWord ) );
154 $req->execute();
155
156 $sxe = simplexml_load_string( $req->getContent() );
157
158 $this->assertNotType( "bool", $sxe );
159 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
160 $a = $sxe->login[0]->attributes()->result[0];
161
162 $this->assertEquals( ' result="Success"', $a->asXML() );
163 }
164
165 function testApiGotCookie() {
166 global $wgServerName, $wgServer, $wgScriptPath;
167
168 if ( !isset( $wgServerName ) || !isset( $wgServer ) ) {
169 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
170 'be set in LocalSettings.php' );
171 }
172 $req = HttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
173 array( "method" => "POST",
174 "postData" => array(
175 "lgname" => self::$userName,
176 "lgpassword" => self::$passWord ) ) );
177 $req->execute();
178
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] );
184
185 $a = $sxe->login[0]->attributes()->result[0];
186 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
187 $token = (string)$sxe->login[0]->attributes()->token;
188
189 $req->setData( array(
190 "lgtoken" => $token,
191 "lgname" => self::$userName,
192 "lgpassword" => self::$passWord ) );
193 $req->execute();
194
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 );
199
200
201 return $cj;
202 }
203
204 /**
205 * @depends testApiGotCookie
206 */
207 function testApiListPages( CookieJar $cj ) {
208 $this->markTestIncomplete( "Not done with this yet" );
209 global $wgServerName, $wgServer;
210
211 if ( $wgServerName == "localhost" || $wgServer == "http://localhost" ) {
212 $this->markTestIncomplete( 'This test needs $wgServerName and $wgServer to ' .
213 'be set in LocalSettings.php' );
214 }
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 );
218 $req->execute();
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();
224 }
225 }