Merge "(bug 42211) Fix the display of some user rights log entries"
[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 $api = new ApiMain(
47 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
48 );
49 $api->execute();
50 $api->getPrinter()->setBufferResult( true );
51 $api->printResult( false );
52 $resp = $api->getPrinter()->getBuffer();
53
54 libxml_use_internal_errors( true );
55 $sxe = simplexml_load_string( $resp );
56 $this->assertNotInternalType( "bool", $sxe );
57 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
58 }
59
60 /**
61 * Test result of attempted login with an empty username
62 */
63 function testApiLoginNoName() {
64 $data = $this->doApiRequest( array( 'action' => 'login',
65 'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
66 ) );
67 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
68 }
69
70 function testApiLoginBadPass() {
71 global $wgServer;
72
73 $user = self::$users['sysop'];
74 $user->user->logOut();
75
76 if ( !isset( $wgServer ) ) {
77 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
78 }
79 $ret = $this->doApiRequest( array(
80 "action" => "login",
81 "lgname" => $user->username,
82 "lgpassword" => "bad",
83 )
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( array(
95 "action" => "login",
96 "lgtoken" => $token,
97 "lgname" => $user->username,
98 "lgpassword" => "badnowayinhell",
99 ), $ret[2]
100 );
101
102 $result = $ret[0];
103
104 $this->assertNotInternalType( "bool", $result );
105 $a = $result["login"]["result"];
106
107 $this->assertEquals( "WrongPass", $a );
108 }
109
110 function testApiLoginGoodPass() {
111 global $wgServer;
112
113 if ( !isset( $wgServer ) ) {
114 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
115 }
116
117 $user = self::$users['sysop'];
118 $user->user->logOut();
119
120 $ret = $this->doApiRequest( array(
121 "action" => "login",
122 "lgname" => $user->username,
123 "lgpassword" => $user->password,
124 )
125 );
126
127 $result = $ret[0];
128 $this->assertNotInternalType( "bool", $result );
129 $this->assertNotInternalType( "null", $result["login"] );
130
131 $a = $result["login"]["result"];
132 $this->assertEquals( "NeedToken", $a );
133 $token = $result["login"]["token"];
134
135 $ret = $this->doApiRequest( array(
136 "action" => "login",
137 "lgtoken" => $token,
138 "lgname" => $user->username,
139 "lgpassword" => $user->password,
140 ), $ret[2]
141 );
142
143 $result = $ret[0];
144
145 $this->assertNotInternalType( "bool", $result );
146 $a = $result["login"]["result"];
147
148 $this->assertEquals( "Success", $a );
149 }
150
151 /**
152 * @group Broken
153 */
154 function testApiGotCookie() {
155 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
156
157 global $wgServer, $wgScriptPath;
158
159 if ( !isset( $wgServer ) ) {
160 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
161 }
162 $user = self::$users['sysop'];
163
164 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
165 array( "method" => "POST",
166 "postData" => array(
167 "lgname" => $user->username,
168 "lgpassword" => $user->password ) ) );
169 $req->execute();
170
171 libxml_use_internal_errors( true );
172 $sxe = simplexml_load_string( $req->getContent() );
173 $this->assertNotInternalType( "bool", $sxe );
174 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
175 $this->assertNotInternalType( "null", $sxe->login[0] );
176
177 $a = $sxe->login[0]->attributes()->result[0];
178 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
179 $token = (string)$sxe->login[0]->attributes()->token;
180
181 $req->setData( array(
182 "lgtoken" => $token,
183 "lgname" => $user->username,
184 "lgpassword" => $user->password ) );
185 $req->execute();
186
187 $cj = $req->getCookieJar();
188 $serverName = parse_url( $wgServer, PHP_URL_HOST );
189 $this->assertNotEquals( false, $serverName );
190 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
191 $this->assertNotEquals( '', $serializedCookie );
192 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
193
194 return $cj;
195 }
196
197 function testRunLogin() {
198 $sysopUser = self::$users['sysop'];
199 $data = $this->doApiRequest( array(
200 'action' => 'login',
201 'lgname' => $sysopUser->username,
202 'lgpassword' => $sysopUser->password ) );
203
204 $this->assertArrayHasKey( "login", $data[0] );
205 $this->assertArrayHasKey( "result", $data[0]['login'] );
206 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
207 $token = $data[0]['login']['token'];
208
209 $data = $this->doApiRequest( array(
210 'action' => 'login',
211 "lgtoken" => $token,
212 "lgname" => $sysopUser->username,
213 "lgpassword" => $sysopUser->password ), $data[2] );
214
215 $this->assertArrayHasKey( "login", $data[0] );
216 $this->assertArrayHasKey( "result", $data[0]['login'] );
217 $this->assertEquals( "Success", $data[0]['login']['result'] );
218 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
219
220 return $data;
221 }
222
223 function testGettingToken() {
224 foreach ( self::$users as $user ) {
225 $this->runTokenTest( $user );
226 }
227 }
228
229 function runTokenTest( $user ) {
230 $data = $this->getTokenList( $user );
231
232 $this->assertArrayHasKey( 'query', $data[0] );
233 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
234 $keys = array_keys( $data[0]['query']['pages'] );
235 $key = array_pop( $keys );
236
237 $rights = $user->user->getRights();
238
239 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
240 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
241 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
242
243 if ( isset( $rights['delete'] ) ) {
244 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
245 }
246
247 if ( isset( $rights['block'] ) ) {
248 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
249 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
250 }
251
252 if ( isset( $rights['protect'] ) ) {
253 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
254 }
255
256 return $data;
257 }
258 }