3 class MockApi
extends ApiBase
{
4 public function execute() { }
5 public function getVersion() { }
7 public function __construct() { }
9 public function getAllowedParams() {
12 'enablechunks' => false,
22 class ApiTest
extends ApiTestSetup
{
24 function testRequireOnlyOneParameterDefault() {
25 $mock = new MockApi();
28 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
29 "enablechunks" => false ), "filename", "enablechunks" ) );
33 * @expectedException UsageException
35 function testRequireOnlyOneParameterZero() {
36 $mock = new MockApi();
39 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
40 "enablechunks" => 0 ), "filename", "enablechunks" ) );
44 * @expectedException UsageException
46 function testRequireOnlyOneParameterTrue() {
47 $mock = new MockApi();
50 null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
51 "enablechunks" => true ), "filename", "enablechunks" ) );
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
63 new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
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" ) );
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
,
83 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
86 function testApiLoginBadPass() {
91 if ( !isset( $wgServer ) ) {
92 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
94 $ret = $this->doApiRequest( array(
96 "lgname" => $user->userName
,
97 "lgpassword" => "bad",
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(
112 "lgname" => $user->userName
,
113 "lgpassword" => "bad",
119 $this->assertNotInternalType( "bool", $result );
120 $a = $result["login"]["result"];
122 $this->assertEquals( "WrongPass", $a );
125 function testApiLoginGoodPass() {
128 if ( !isset( $wgServer ) ) {
129 $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
134 $ret = $this->doApiRequest( array(
136 "lgname" => $user->userName
,
137 "lgpassword" => $user->password
,
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(
152 "lgname" => $user->userName
,
153 "lgpassword" => $user->password
,
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",
176 "lgname" => $this->user
->userName
,
177 "lgpassword" => $this->user
->password
) ) );
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(
192 "lgname" => $this->user
->userName
,
193 "lgpassword" => $this->user
->password
) );
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 );
207 * @depends testApiGotCookie
209 function testApiListPages( CookieJar
$cj ) {
210 $this->markTestIncomplete( "Not done with this yet" );
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 );
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(
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(
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'] );
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] );