c3eacd5bddc864d71acd22646c3e2dcff4b12c90
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 */
7 class ApiTest extends ApiTestCase {
8
9 function testRequireOnlyOneParameterDefault() {
10 $mock = new MockApi();
11
12 $this->assertEquals(
13 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
14 "enablechunks" => false ), "filename", "enablechunks" ) );
15 }
16
17 /**
18 * @expectedException UsageException
19 */
20 function testRequireOnlyOneParameterZero() {
21 $mock = new MockApi();
22
23 $this->assertEquals(
24 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
25 "enablechunks" => 0 ), "filename", "enablechunks" ) );
26 }
27
28 /**
29 * @expectedException UsageException
30 */
31 function testRequireOnlyOneParameterTrue() {
32 $mock = new MockApi();
33
34 $this->assertEquals(
35 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
36 "enablechunks" => true ), "filename", "enablechunks" ) );
37 }
38
39 /**
40 * Test that the API will accept a FauxRequest and execute. The help action
41 * (default) throws a UsageException. Just validate we're getting proper XML
42 *
43 * @expectedException UsageException
44 */
45 function testApi() {
46
47 $api = new ApiMain(
48 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
49 );
50 $api->execute();
51 $api->getPrinter()->setBufferResult( true );
52 $api->printResult( false );
53 $resp = $api->getPrinter()->getBuffer();
54
55 libxml_use_internal_errors( true );
56 $sxe = simplexml_load_string( $resp );
57 $this->assertNotInternalType( "bool", $sxe );
58 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
59 }
60
61 /**
62 * Test result of attempted login with an empty username
63 */
64 function testApiLoginNoName() {
65 $data = $this->doApiRequest( array( 'action' => 'login',
66 'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
67 ) );
68 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
69 }
70
71 function testApiLoginBadPass() {
72 global $wgServer;
73
74 $user = self::$users['sysop'];
75 $user->user->logOut();
76
77 if ( !isset( $wgServer ) ) {
78 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
79 }
80 $ret = $this->doApiRequest( array(
81 "action" => "login",
82 "lgname" => $user->username,
83 "lgpassword" => "bad",
84 )
85 );
86
87 $result = $ret[0];
88
89 $this->assertNotInternalType( "bool", $result );
90 $a = $result["login"]["result"];
91 $this->assertEquals( "NeedToken", $a );
92
93 $token = $result["login"]["token"];
94
95 $ret = $this->doApiRequest( array(
96 "action" => "login",
97 "lgtoken" => $token,
98 "lgname" => $user->username,
99 "lgpassword" => "badnowayinhell",
100 ), $ret[2]
101 );
102
103 $result = $ret[0];
104
105 $this->assertNotInternalType( "bool", $result );
106 $a = $result["login"]["result"];
107
108 $this->assertEquals( "WrongPass", $a );
109 }
110
111 function testApiLoginGoodPass() {
112 global $wgServer;
113
114 if ( !isset( $wgServer ) ) {
115 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
116 }
117
118 $user = self::$users['sysop'];
119 $user->user->logOut();
120
121 $ret = $this->doApiRequest( array(
122 "action" => "login",
123 "lgname" => $user->username,
124 "lgpassword" => $user->password,
125 )
126 );
127
128 $result = $ret[0];
129 $this->assertNotInternalType( "bool", $result );
130 $this->assertNotInternalType( "null", $result["login"] );
131
132 $a = $result["login"]["result"];
133 $this->assertEquals( "NeedToken", $a );
134 $token = $result["login"]["token"];
135
136 $ret = $this->doApiRequest( array(
137 "action" => "login",
138 "lgtoken" => $token,
139 "lgname" => $user->username,
140 "lgpassword" => $user->password,
141 ), $ret[2]
142 );
143
144 $result = $ret[0];
145
146 $this->assertNotInternalType( "bool", $result );
147 $a = $result["login"]["result"];
148
149 $this->assertEquals( "Success", $a );
150 }
151
152 /**
153 * @group Broken
154 */
155 function testApiGotCookie() {
156 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
157
158 global $wgServer, $wgScriptPath;
159
160 if ( !isset( $wgServer ) ) {
161 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
162 }
163 $user = self::$users['sysop'];
164
165 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
166 array( "method" => "POST",
167 "postData" => array(
168 "lgname" => $user->username,
169 "lgpassword" => $user->password ) ) );
170 $req->execute();
171
172 libxml_use_internal_errors( true );
173 $sxe = simplexml_load_string( $req->getContent() );
174 $this->assertNotInternalType( "bool", $sxe );
175 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
176 $this->assertNotInternalType( "null", $sxe->login[0] );
177
178 $a = $sxe->login[0]->attributes()->result[0];
179 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
180 $token = (string)$sxe->login[0]->attributes()->token;
181
182 $req->setData( array(
183 "lgtoken" => $token,
184 "lgname" => $user->username,
185 "lgpassword" => $user->password ) );
186 $req->execute();
187
188 $cj = $req->getCookieJar();
189 $serverName = parse_url( $wgServer, PHP_URL_HOST );
190 $this->assertNotEquals( false, $serverName );
191 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
192 $this->assertNotEquals( '', $serializedCookie );
193 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
194
195 return $cj;
196 }
197
198 /**
199 * @todo Finish filling me out...what are we trying to test here?
200 */
201 function testApiListPages() {
202 global $wgServer;
203 if ( !isset( $wgServer ) ) {
204 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
205 }
206
207 $ret = $this->doApiRequest( array(
208 'action' => 'query',
209 'prop' => 'revisions',
210 'titles' => 'Main Page',
211 'rvprop' => 'timestamp|user|comment|content',
212 ) );
213
214 $result = $ret[0]['query']['pages'];
215 $this->markTestIncomplete( "Somebody needs to finish loving me" );
216 }
217
218 function testRunLogin() {
219 $sysopUser = self::$users['sysop'];
220 $data = $this->doApiRequest( array(
221 'action' => 'login',
222 'lgname' => $sysopUser->username,
223 'lgpassword' => $sysopUser->password ) );
224
225 $this->assertArrayHasKey( "login", $data[0] );
226 $this->assertArrayHasKey( "result", $data[0]['login'] );
227 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
228 $token = $data[0]['login']['token'];
229
230 $data = $this->doApiRequest( array(
231 'action' => 'login',
232 "lgtoken" => $token,
233 "lgname" => $sysopUser->username,
234 "lgpassword" => $sysopUser->password ), $data[2] );
235
236 $this->assertArrayHasKey( "login", $data[0] );
237 $this->assertArrayHasKey( "result", $data[0]['login'] );
238 $this->assertEquals( "Success", $data[0]['login']['result'] );
239 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
240
241 return $data;
242 }
243
244 function testGettingToken() {
245 foreach ( self::$users as $user ) {
246 $this->runTokenTest( $user );
247 }
248 }
249
250 function runTokenTest( $user ) {
251
252 $data = $this->getTokenList( $user );
253
254 $this->assertArrayHasKey( 'query', $data[0] );
255 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
256 $keys = array_keys( $data[0]['query']['pages'] );
257 $key = array_pop( $keys );
258
259 $rights = $user->user->getRights();
260
261 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
262 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
263 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
264
265 if ( isset( $rights['delete'] ) ) {
266 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
267 }
268
269 if ( isset( $rights['block'] ) ) {
270 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
271 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
272 }
273
274 if ( isset( $rights['protect'] ) ) {
275 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
276 }
277
278 return $data;
279 }
280 }