Merge "Clarify userrights-conflict"
[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 $result = $ret[0];
87
88 $this->assertNotInternalType( "bool", $result );
89 $a = $result["login"]["result"];
90 $this->assertEquals( "NeedToken", $a );
91
92 $token = $result["login"]["token"];
93
94 $ret = $this->doApiRequest(
95 array(
96 "action" => "login",
97 "lgtoken" => $token,
98 "lgname" => $user->username,
99 "lgpassword" => "badnowayinhell",
100 ),
101 $ret[2]
102 );
103
104 $result = $ret[0];
105
106 $this->assertNotInternalType( "bool", $result );
107 $a = $result["login"]["result"];
108
109 $this->assertEquals( "WrongPass", $a );
110 }
111
112 function testApiLoginGoodPass() {
113 global $wgServer;
114
115 if ( !isset( $wgServer ) ) {
116 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
117 }
118
119 $user = self::$users['sysop'];
120 $user->user->logOut();
121
122 $ret = $this->doApiRequest( array(
123 "action" => "login",
124 "lgname" => $user->username,
125 "lgpassword" => $user->password,
126 )
127 );
128
129 $result = $ret[0];
130 $this->assertNotInternalType( "bool", $result );
131 $this->assertNotInternalType( "null", $result["login"] );
132
133 $a = $result["login"]["result"];
134 $this->assertEquals( "NeedToken", $a );
135 $token = $result["login"]["token"];
136
137 $ret = $this->doApiRequest(
138 array(
139 "action" => "login",
140 "lgtoken" => $token,
141 "lgname" => $user->username,
142 "lgpassword" => $user->password,
143 ),
144 $ret[2]
145 );
146
147 $result = $ret[0];
148
149 $this->assertNotInternalType( "bool", $result );
150 $a = $result["login"]["result"];
151
152 $this->assertEquals( "Success", $a );
153 }
154
155 /**
156 * @group Broken
157 */
158 function testApiGotCookie() {
159 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
160
161 global $wgServer, $wgScriptPath;
162
163 if ( !isset( $wgServer ) ) {
164 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
165 }
166 $user = self::$users['sysop'];
167
168 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
169 array( "method" => "POST",
170 "postData" => array(
171 "lgname" => $user->username,
172 "lgpassword" => $user->password
173 )
174 )
175 );
176 $req->execute();
177
178 libxml_use_internal_errors( true );
179 $sxe = simplexml_load_string( $req->getContent() );
180 $this->assertNotInternalType( "bool", $sxe );
181 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
182 $this->assertNotInternalType( "null", $sxe->login[0] );
183
184 $a = $sxe->login[0]->attributes()->result[0];
185 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
186 $token = (string)$sxe->login[0]->attributes()->token;
187
188 $req->setData( array(
189 "lgtoken" => $token,
190 "lgname" => $user->username,
191 "lgpassword" => $user->password ) );
192 $req->execute();
193
194 $cj = $req->getCookieJar();
195 $serverName = parse_url( $wgServer, PHP_URL_HOST );
196 $this->assertNotEquals( false, $serverName );
197 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
198 $this->assertNotEquals( '', $serializedCookie );
199 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
200
201 return $cj;
202 }
203
204 function testRunLogin() {
205 $sysopUser = self::$users['sysop'];
206 $data = $this->doApiRequest( array(
207 'action' => 'login',
208 'lgname' => $sysopUser->username,
209 'lgpassword' => $sysopUser->password ) );
210
211 $this->assertArrayHasKey( "login", $data[0] );
212 $this->assertArrayHasKey( "result", $data[0]['login'] );
213 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
214 $token = $data[0]['login']['token'];
215
216 $data = $this->doApiRequest( array(
217 'action' => 'login',
218 "lgtoken" => $token,
219 "lgname" => $sysopUser->username,
220 "lgpassword" => $sysopUser->password ), $data[2] );
221
222 $this->assertArrayHasKey( "login", $data[0] );
223 $this->assertArrayHasKey( "result", $data[0]['login'] );
224 $this->assertEquals( "Success", $data[0]['login']['result'] );
225 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
226
227 return $data;
228 }
229
230 function testGettingToken() {
231 foreach ( self::$users as $user ) {
232 $this->runTokenTest( $user );
233 }
234 }
235
236 function runTokenTest( $user ) {
237 $tokens = $this->getTokenList( $user );
238
239 $rights = $user->user->getRights();
240
241 $this->assertArrayHasKey( 'edittoken', $tokens );
242 $this->assertArrayHasKey( 'movetoken', $tokens );
243
244 if ( isset( $rights['delete'] ) ) {
245 $this->assertArrayHasKey( 'deletetoken', $tokens );
246 }
247
248 if ( isset( $rights['block'] ) ) {
249 $this->assertArrayHasKey( 'blocktoken', $tokens );
250 $this->assertArrayHasKey( 'unblocktoken', $tokens );
251 }
252
253 if ( isset( $rights['protect'] ) ) {
254 $this->assertArrayHasKey( 'protecttoken', $tokens );
255 }
256
257 return $tokens;
258 }
259 }