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