* upgrade patches for oracle 1.17->1.19
[mediawiki.git] / tests / phpunit / includes / api / ApiTest.php
blobd7133da30dc28e81bcd1278e53fc2e0c40df7e95
1 <?php
3 class MockApi extends ApiBase {
4 public function execute() { }
5 public function getVersion() { }
7 public function __construct() { }
9 public function getAllowedParams() {
10 return array(
11 'filename' => null,
12 'enablechunks' => false,
13 'sessionkey' => null,
18 /**
19 * @group Database
20 * @group Destructive
22 class ApiTest extends ApiTestSetup {
24 function testRequireOnlyOneParameterDefault() {
25 $mock = new MockApi();
27 $this->assertEquals(
28 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
29 "enablechunks" => false ), "filename", "enablechunks" ) );
32 /**
33 * @expectedException UsageException
35 function testRequireOnlyOneParameterZero() {
36 $mock = new MockApi();
38 $this->assertEquals(
39 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
40 "enablechunks" => 0 ), "filename", "enablechunks" ) );
43 /**
44 * @expectedException UsageException
46 function testRequireOnlyOneParameterTrue() {
47 $mock = new MockApi();
49 $this->assertEquals(
50 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
51 "enablechunks" => true ), "filename", "enablechunks" ) );
54 /**
55 * Test that the API will accept a FauxRequest and execute. The help action
56 * (default) throws a UsageException. Just validate we're getting proper XML
58 * @expectedException UsageException
60 function testApi() {
62 $api = new ApiMain(
63 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
65 $api->execute();
66 $api->getPrinter()->setBufferResult( true );
67 $api->printResult( false );
68 $resp = $api->getPrinter()->getBuffer();
70 libxml_use_internal_errors( true );
71 $sxe = simplexml_load_string( $resp );
72 $this->assertNotInternalType( "bool", $sxe );
73 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
76 /**
77 * Test result of attempted login with an empty username
79 function testApiLoginNoName() {
80 $data = $this->doApiRequest( array( 'action' => 'login',
81 'lgname' => '', 'lgpassword' => $this->user->password,
82 ) );
83 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
86 function testApiLoginBadPass() {
87 global $wgServer;
89 $user = $this->user;
91 if ( !isset( $wgServer ) ) {
92 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
94 $ret = $this->doApiRequest( array(
95 "action" => "login",
96 "lgname" => $user->userName,
97 "lgpassword" => "bad",
101 $result = $ret[0];
103 $this->assertNotInternalType( "bool", $result );
104 $a = $result["login"]["result"];
105 $this->assertEquals( "NeedToken", $a );
107 $token = $result["login"]["token"];
109 $ret = $this->doApiRequest( array(
110 "action" => "login",
111 "lgtoken" => $token,
112 "lgname" => $user->userName,
113 "lgpassword" => "bad",
117 $result = $ret[0];
119 $this->assertNotInternalType( "bool", $result );
120 $a = $result["login"]["result"];
122 $this->assertEquals( "WrongPass", $a );
125 function testApiLoginGoodPass() {
126 global $wgServer;
128 if ( !isset( $wgServer ) ) {
129 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
132 $user = $this->user;
134 $ret = $this->doApiRequest( array(
135 "action" => "login",
136 "lgname" => $user->userName,
137 "lgpassword" => $user->password,
141 $result = $ret[0];
142 $this->assertNotInternalType( "bool", $result );
143 $this->assertNotInternalType( "null", $result["login"] );
145 $a = $result["login"]["result"];
146 $this->assertEquals( "NeedToken", $a );
147 $token = $result["login"]["token"];
149 $ret = $this->doApiRequest( array(
150 "action" => "login",
151 "lgtoken" => $token,
152 "lgname" => $user->userName,
153 "lgpassword" => $user->password,
157 $result = $ret[0];
159 $this->assertNotInternalType( "bool", $result );
160 $a = $result["login"]["result"];
162 $this->assertEquals( "Success", $a );
165 function testApiGotCookie() {
166 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
168 global $wgServer, $wgScriptPath;
170 if ( !isset( $wgServer ) ) {
171 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
173 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
174 array( "method" => "POST",
175 "postData" => array(
176 "lgname" => $this->user->userName,
177 "lgpassword" => $this->user->password ) ) );
178 $req->execute();
180 libxml_use_internal_errors( true );
181 $sxe = simplexml_load_string( $req->getContent() );
182 $this->assertNotInternalType( "bool", $sxe );
183 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
184 $this->assertNotInternalType( "null", $sxe->login[0] );
186 $a = $sxe->login[0]->attributes()->result[0];
187 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
188 $token = (string)$sxe->login[0]->attributes()->token;
190 $req->setData( array(
191 "lgtoken" => $token,
192 "lgname" => $this->user->userName,
193 "lgpassword" => $this->user->password ) );
194 $req->execute();
196 $cj = $req->getCookieJar();
197 $serverName = parse_url( $wgServer, PHP_URL_HOST );
198 $this->assertNotEquals( false, $serverName );
199 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
200 $this->assertNotEquals( '', $serializedCookie );
201 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $this->user->userName . '; .*Token=/', $serializedCookie );
203 return $cj;
207 * @depends testApiGotCookie
209 function testApiListPages( CookieJar $cj ) {
210 $this->markTestIncomplete( "Not done with this yet" );
211 global $wgServer;
213 if ( $wgServer == "http://localhost" ) {
214 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
216 $req = MWHttpRequest::factory( self::$apiUrl . "?action=query&format=xml&prop=revisions&" .
217 "titles=Main%20Page&rvprop=timestamp|user|comment|content" );
218 $req->setCookieJar( $cj );
219 $req->execute();
220 libxml_use_internal_errors( true );
221 $sxe = simplexml_load_string( $req->getContent() );
222 $this->assertNotInternalType( "bool", $sxe );
223 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
224 $a = $sxe->query[0]->pages[0]->page[0]->attributes();
227 function testRunLogin() {
228 $data = $this->doApiRequest( array(
229 'action' => 'login',
230 'lgname' => $this->sysopUser->userName,
231 'lgpassword' => $this->sysopUser->password ) );
233 $this->assertArrayHasKey( "login", $data[0] );
234 $this->assertArrayHasKey( "result", $data[0]['login'] );
235 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
236 $token = $data[0]['login']['token'];
238 $data = $this->doApiRequest( array(
239 'action' => 'login',
240 "lgtoken" => $token,
241 "lgname" => $this->sysopUser->userName,
242 "lgpassword" => $this->sysopUser->password ), $data );
244 $this->assertArrayHasKey( "login", $data[0] );
245 $this->assertArrayHasKey( "result", $data[0]['login'] );
246 $this->assertEquals( "Success", $data[0]['login']['result'] );
247 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
249 return $data;
252 function testGettingToken() {
253 foreach ( array( $this->user, $this->sysopUser ) as $user ) {
254 $this->runTokenTest( $user );
258 function runTokenTest( $user ) {
260 $data = $this->getTokenList( $user );
262 $this->assertArrayHasKey( 'query', $data[0] );
263 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
264 $keys = array_keys( $data[0]['query']['pages'] );
265 $key = array_pop( $keys );
267 $rights = $user->user->getRights();
269 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
270 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
271 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
273 if ( isset( $rights['delete'] ) ) {
274 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
277 if ( isset( $rights['block'] ) ) {
278 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
279 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
282 if ( isset( $rights['protect'] ) ) {
283 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
286 return $data;