22770288b489b58d930aba0f865a614068bda03f
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 */
8 class ApiTest extends ApiTestCase {
9
10 function testRequireOnlyOneParameterDefault() {
11 $mock = new MockApi();
12
13 $this->assertEquals(
14 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
15 "enablechunks" => false ), "filename", "enablechunks" ) );
16 }
17
18 /**
19 * @expectedException UsageException
20 */
21 function testRequireOnlyOneParameterZero() {
22 $mock = new MockApi();
23
24 $this->assertEquals(
25 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
26 "enablechunks" => 0 ), "filename", "enablechunks" ) );
27 }
28
29 /**
30 * @expectedException UsageException
31 */
32 function testRequireOnlyOneParameterTrue() {
33 $mock = new MockApi();
34
35 $this->assertEquals(
36 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
37 "enablechunks" => true ), "filename", "enablechunks" ) );
38 }
39
40 /**
41 * Test that the API will accept a FauxRequest and execute. The help action
42 * (default) throws a UsageException. Just validate we're getting proper XML
43 *
44 * @expectedException UsageException
45 */
46 function testApi() {
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(
96 array(
97 "action" => "login",
98 "lgtoken" => $token,
99 "lgname" => $user->username,
100 "lgpassword" => "badnowayinhell",
101 ),
102 $ret[2]
103 );
104
105 $result = $ret[0];
106
107 $this->assertNotInternalType( "bool", $result );
108 $a = $result["login"]["result"];
109
110 $this->assertEquals( "WrongPass", $a );
111 }
112
113 function testApiLoginGoodPass() {
114 global $wgServer;
115
116 if ( !isset( $wgServer ) ) {
117 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
118 }
119
120 $user = self::$users['sysop'];
121 $user->user->logOut();
122
123 $ret = $this->doApiRequest( array(
124 "action" => "login",
125 "lgname" => $user->username,
126 "lgpassword" => $user->password,
127 )
128 );
129
130 $result = $ret[0];
131 $this->assertNotInternalType( "bool", $result );
132 $this->assertNotInternalType( "null", $result["login"] );
133
134 $a = $result["login"]["result"];
135 $this->assertEquals( "NeedToken", $a );
136 $token = $result["login"]["token"];
137
138 $ret = $this->doApiRequest(
139 array(
140 "action" => "login",
141 "lgtoken" => $token,
142 "lgname" => $user->username,
143 "lgpassword" => $user->password,
144 ),
145 $ret[2]
146 );
147
148 $result = $ret[0];
149
150 $this->assertNotInternalType( "bool", $result );
151 $a = $result["login"]["result"];
152
153 $this->assertEquals( "Success", $a );
154 }
155
156 /**
157 * @group Broken
158 */
159 function testApiGotCookie() {
160 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
161
162 global $wgServer, $wgScriptPath;
163
164 if ( !isset( $wgServer ) ) {
165 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
166 }
167 $user = self::$users['sysop'];
168
169 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
170 array( "method" => "POST",
171 "postData" => array(
172 "lgname" => $user->username,
173 "lgpassword" => $user->password
174 )
175 )
176 );
177 $req->execute();
178
179 libxml_use_internal_errors( true );
180 $sxe = simplexml_load_string( $req->getContent() );
181 $this->assertNotInternalType( "bool", $sxe );
182 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
183 $this->assertNotInternalType( "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" => $user->username,
192 "lgpassword" => $user->password ) );
193 $req->execute();
194
195 $cj = $req->getCookieJar();
196 $serverName = parse_url( $wgServer, PHP_URL_HOST );
197 $this->assertNotEquals( false, $serverName );
198 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
199 $this->assertNotEquals( '', $serializedCookie );
200 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
201
202 return $cj;
203 }
204
205 function testRunLogin() {
206 $sysopUser = self::$users['sysop'];
207 $data = $this->doApiRequest( array(
208 'action' => 'login',
209 'lgname' => $sysopUser->username,
210 'lgpassword' => $sysopUser->password ) );
211
212 $this->assertArrayHasKey( "login", $data[0] );
213 $this->assertArrayHasKey( "result", $data[0]['login'] );
214 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
215 $token = $data[0]['login']['token'];
216
217 $data = $this->doApiRequest( array(
218 'action' => 'login',
219 "lgtoken" => $token,
220 "lgname" => $sysopUser->username,
221 "lgpassword" => $sysopUser->password ), $data[2] );
222
223 $this->assertArrayHasKey( "login", $data[0] );
224 $this->assertArrayHasKey( "result", $data[0]['login'] );
225 $this->assertEquals( "Success", $data[0]['login']['result'] );
226 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
227
228 return $data;
229 }
230
231 function testGettingToken() {
232 foreach ( self::$users as $user ) {
233 $this->runTokenTest( $user );
234 }
235 }
236
237 function runTokenTest( $user ) {
238 $data = $this->getTokenList( $user );
239
240 $this->assertArrayHasKey( 'query', $data[0] );
241 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
242 $keys = array_keys( $data[0]['query']['pages'] );
243 $key = array_pop( $keys );
244
245 $rights = $user->user->getRights();
246
247 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
248 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
249 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
250
251 if ( isset( $rights['delete'] ) ) {
252 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
253 }
254
255 if ( isset( $rights['block'] ) ) {
256 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
257 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
258 }
259
260 if ( isset( $rights['protect'] ) ) {
261 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
262 }
263
264 return $data;
265 }
266 }