MediaWiki  REL1_20
ApiTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class ApiTest extends ApiTestCase {
00008 
00009         function testRequireOnlyOneParameterDefault() {
00010                 $mock = new MockApi();
00011 
00012                 $this->assertEquals(
00013                         null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
00014                                                                                                            "enablechunks" => false ), "filename", "enablechunks" ) );
00015         }
00016 
00020         function testRequireOnlyOneParameterZero() {
00021                 $mock = new MockApi();
00022 
00023                 $this->assertEquals(
00024                         null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
00025                                                                                                            "enablechunks" => 0 ), "filename", "enablechunks" ) );
00026         }
00027 
00031         function testRequireOnlyOneParameterTrue() {
00032                 $mock = new MockApi();
00033 
00034                 $this->assertEquals(
00035                         null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
00036                                                                                                            "enablechunks" => true ), "filename", "enablechunks" ) );
00037         }
00038 
00045         function testApi() {
00046         
00047                 $api = new ApiMain(
00048                         new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) )
00049                 );
00050                 $api->execute();
00051                 $api->getPrinter()->setBufferResult( true );
00052                 $api->printResult( false );
00053                 $resp = $api->getPrinter()->getBuffer();
00054 
00055                 libxml_use_internal_errors( true );
00056                 $sxe = simplexml_load_string( $resp );
00057                 $this->assertNotInternalType( "bool", $sxe );
00058                 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00059         }
00060 
00064         function testApiLoginNoName() {
00065                 $data = $this->doApiRequest( array( 'action' => 'login',
00066                         'lgname' => '', 'lgpassword' => self::$users['sysop']->password,
00067                 ) );
00068                 $this->assertEquals( 'NoName', $data[0]['login']['result'] );
00069         }
00070 
00071         function testApiLoginBadPass() {
00072                 global $wgServer;
00073 
00074                 $user = self::$users['sysop'];
00075                 $user->user->logOut();
00076 
00077                 if ( !isset( $wgServer ) ) {
00078                         $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00079                 }
00080                 $ret = $this->doApiRequest( array(
00081                         "action" => "login",
00082                         "lgname" => $user->username,
00083                         "lgpassword" => "bad",
00084                         )
00085                 );
00086 
00087                 $result = $ret[0];
00088 
00089                 $this->assertNotInternalType( "bool", $result );
00090                 $a = $result["login"]["result"];
00091                 $this->assertEquals( "NeedToken", $a );
00092 
00093                 $token = $result["login"]["token"];
00094 
00095                 $ret = $this->doApiRequest( array(
00096                         "action" => "login",
00097                         "lgtoken" => $token,
00098                         "lgname" => $user->username,
00099                         "lgpassword" => "badnowayinhell",
00100                         ), $ret[2]
00101                 );
00102 
00103                 $result = $ret[0];
00104 
00105                 $this->assertNotInternalType( "bool", $result );
00106                 $a = $result["login"]["result"];
00107 
00108                 $this->assertEquals( "WrongPass", $a );
00109         }
00110 
00111         function testApiLoginGoodPass() {
00112                 global $wgServer;
00113 
00114                 if ( !isset( $wgServer ) ) {
00115                         $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00116                 }
00117 
00118                 $user = self::$users['sysop'];
00119                 $user->user->logOut();
00120 
00121                 $ret = $this->doApiRequest( array(
00122                         "action" => "login",
00123                         "lgname" => $user->username,
00124                         "lgpassword" => $user->password,
00125                         )
00126                 );
00127 
00128                 $result = $ret[0];
00129                 $this->assertNotInternalType( "bool", $result );
00130                 $this->assertNotInternalType( "null", $result["login"] );
00131 
00132                 $a = $result["login"]["result"];
00133                 $this->assertEquals( "NeedToken", $a );
00134                 $token = $result["login"]["token"];
00135 
00136                 $ret = $this->doApiRequest( array(
00137                         "action" => "login",
00138                         "lgtoken" => $token,
00139                         "lgname" => $user->username,
00140                         "lgpassword" => $user->password,
00141                         ), $ret[2]
00142                 );
00143 
00144                 $result = $ret[0];
00145 
00146                 $this->assertNotInternalType( "bool", $result );
00147                 $a = $result["login"]["result"];
00148 
00149                 $this->assertEquals( "Success", $a );
00150         }
00151 
00155         function testApiGotCookie() {
00156                 $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies"  );
00157 
00158                 global $wgServer, $wgScriptPath;
00159 
00160                 if ( !isset( $wgServer ) ) {
00161                         $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00162                 }
00163                 $user = self::$users['sysop'];
00164 
00165                 $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
00166                         array( "method" => "POST",
00167                                 "postData" => array(
00168                                 "lgname" => $user->username,
00169                                 "lgpassword" => $user->password ) ) );
00170                 $req->execute();
00171 
00172                 libxml_use_internal_errors( true );
00173                 $sxe = simplexml_load_string( $req->getContent() );
00174                 $this->assertNotInternalType( "bool", $sxe );
00175                 $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00176                 $this->assertNotInternalType( "null", $sxe->login[0] );
00177 
00178                 $a = $sxe->login[0]->attributes()->result[0];
00179                 $this->assertEquals( ' result="NeedToken"', $a->asXML() );
00180                 $token = (string)$sxe->login[0]->attributes()->token;
00181 
00182                 $req->setData( array(
00183                         "lgtoken" => $token,
00184                         "lgname" => $user->username,
00185                         "lgpassword" => $user->password ) );
00186                 $req->execute();
00187 
00188                 $cj = $req->getCookieJar();
00189                 $serverName = parse_url( $wgServer, PHP_URL_HOST );
00190                 $this->assertNotEquals( false, $serverName );
00191                 $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
00192                 $this->assertNotEquals( '', $serializedCookie );
00193                 $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
00194 
00195                 return $cj;
00196         }
00197 
00201         function testApiListPages() {
00202                 global $wgServer;
00203                 if ( !isset( $wgServer ) ) {
00204                         $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00205                 }
00206 
00207                 $ret = $this->doApiRequest( array(
00208                         'action' => 'query',
00209                         'prop'   => 'revisions',
00210                         'titles' => 'Main Page',
00211                         'rvprop' => 'timestamp|user|comment|content',
00212                 ) );
00213 
00214                 $result = $ret[0]['query']['pages'];
00215                 $this->markTestIncomplete( "Somebody needs to finish loving me" );
00216         }
00217         
00218         function testRunLogin() {
00219                 $sysopUser = self::$users['sysop'];
00220                 $data = $this->doApiRequest( array(
00221                         'action' => 'login',
00222                         'lgname' => $sysopUser->username,
00223                         'lgpassword' => $sysopUser->password ) );
00224 
00225                 $this->assertArrayHasKey( "login", $data[0] );
00226                 $this->assertArrayHasKey( "result", $data[0]['login'] );
00227                 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
00228                 $token = $data[0]['login']['token'];
00229 
00230                 $data = $this->doApiRequest( array(
00231                         'action' => 'login',
00232                         "lgtoken" => $token,
00233                         "lgname" => $sysopUser->username,
00234                         "lgpassword" => $sysopUser->password ), $data[2] );
00235 
00236                 $this->assertArrayHasKey( "login", $data[0] );
00237                 $this->assertArrayHasKey( "result", $data[0]['login'] );
00238                 $this->assertEquals( "Success", $data[0]['login']['result'] );
00239                 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
00240                 
00241                 return $data;
00242         }
00243         
00244         function testGettingToken() {
00245                 foreach ( self::$users as $user ) {
00246                         $this->runTokenTest( $user );
00247                 }
00248         }
00249 
00250         function runTokenTest( $user ) {
00251                 
00252                 $data = $this->getTokenList( $user );
00253 
00254                 $this->assertArrayHasKey( 'query', $data[0] );
00255                 $this->assertArrayHasKey( 'pages', $data[0]['query'] );
00256                 $keys = array_keys( $data[0]['query']['pages'] );
00257                 $key = array_pop( $keys );
00258 
00259                 $rights = $user->user->getRights();
00260 
00261                 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] );
00262                 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] );
00263                 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] );
00264 
00265                 if ( isset( $rights['delete'] ) ) {
00266                         $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] );
00267                 }
00268 
00269                 if ( isset( $rights['block'] ) ) {
00270                         $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] );
00271                         $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] );
00272                 }
00273 
00274                 if ( isset( $rights['protect'] ) ) {
00275                         $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] );
00276                 }
00277 
00278                 return $data;
00279         }
00280 }