MediaWiki  REL1_22
ApiTest.php
Go to the documentation of this file.
00001 <?php
00002 
00008 class ApiTest extends ApiTestCase {
00009 
00010     public function testRequireOnlyOneParameterDefault() {
00011         $mock = new MockApi();
00012 
00013         $this->assertEquals(
00014             null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
00015             "enablechunks" => false ), "filename", "enablechunks" ) );
00016     }
00017 
00021     public function testRequireOnlyOneParameterZero() {
00022         $mock = new MockApi();
00023 
00024         $this->assertEquals(
00025             null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
00026             "enablechunks" => 0 ), "filename", "enablechunks" ) );
00027     }
00028 
00032     public function testRequireOnlyOneParameterTrue() {
00033         $mock = new MockApi();
00034 
00035         $this->assertEquals(
00036             null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt",
00037             "enablechunks" => true ), "filename", "enablechunks" ) );
00038     }
00039 
00046     public function testApi() {
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     public 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     public 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         $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(
00095             array(
00096                 "action" => "login",
00097                 "lgtoken" => $token,
00098                 "lgname" => $user->username,
00099                 "lgpassword" => "badnowayinhell",
00100             ),
00101             $ret[2]
00102         );
00103 
00104         $result = $ret[0];
00105 
00106         $this->assertNotInternalType( "bool", $result );
00107         $a = $result["login"]["result"];
00108 
00109         $this->assertEquals( "WrongPass", $a );
00110     }
00111 
00112     public function testApiLoginGoodPass() {
00113         global $wgServer;
00114 
00115         if ( !isset( $wgServer ) ) {
00116             $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00117         }
00118 
00119         $user = self::$users['sysop'];
00120         $user->user->logOut();
00121 
00122         $ret = $this->doApiRequest( array(
00123                 "action" => "login",
00124                 "lgname" => $user->username,
00125                 "lgpassword" => $user->password,
00126             )
00127         );
00128 
00129         $result = $ret[0];
00130         $this->assertNotInternalType( "bool", $result );
00131         $this->assertNotInternalType( "null", $result["login"] );
00132 
00133         $a = $result["login"]["result"];
00134         $this->assertEquals( "NeedToken", $a );
00135         $token = $result["login"]["token"];
00136 
00137         $ret = $this->doApiRequest(
00138             array(
00139                 "action" => "login",
00140                 "lgtoken" => $token,
00141                 "lgname" => $user->username,
00142                 "lgpassword" => $user->password,
00143             ),
00144             $ret[2]
00145         );
00146 
00147         $result = $ret[0];
00148 
00149         $this->assertNotInternalType( "bool", $result );
00150         $a = $result["login"]["result"];
00151 
00152         $this->assertEquals( "Success", $a );
00153     }
00154 
00158     public function testApiGotCookie() {
00159         $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" );
00160 
00161         global $wgServer, $wgScriptPath;
00162 
00163         if ( !isset( $wgServer ) ) {
00164             $this->markTestIncomplete( 'This test needs $wgServer to be set in LocalSettings.php' );
00165         }
00166         $user = self::$users['sysop'];
00167 
00168         $req = MWHttpRequest::factory( self::$apiUrl . "?action=login&format=xml",
00169             array( "method" => "POST",
00170                 "postData" => array(
00171                     "lgname" => $user->username,
00172                     "lgpassword" => $user->password
00173                 )
00174             )
00175         );
00176         $req->execute();
00177 
00178         libxml_use_internal_errors( true );
00179         $sxe = simplexml_load_string( $req->getContent() );
00180         $this->assertNotInternalType( "bool", $sxe );
00181         $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) );
00182         $this->assertNotInternalType( "null", $sxe->login[0] );
00183 
00184         $a = $sxe->login[0]->attributes()->result[0];
00185         $this->assertEquals( ' result="NeedToken"', $a->asXML() );
00186         $token = (string)$sxe->login[0]->attributes()->token;
00187 
00188         $req->setData( array(
00189             "lgtoken" => $token,
00190             "lgname" => $user->username,
00191             "lgpassword" => $user->password ) );
00192         $req->execute();
00193 
00194         $cj = $req->getCookieJar();
00195         $serverName = parse_url( $wgServer, PHP_URL_HOST );
00196         $this->assertNotEquals( false, $serverName );
00197         $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName );
00198         $this->assertNotEquals( '', $serializedCookie );
00199         $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie );
00200 
00201         return $cj;
00202     }
00203 
00204     public function testRunLogin() {
00205         $sysopUser = self::$users['sysop'];
00206         $data = $this->doApiRequest( array(
00207             'action' => 'login',
00208             'lgname' => $sysopUser->username,
00209             'lgpassword' => $sysopUser->password ) );
00210 
00211         $this->assertArrayHasKey( "login", $data[0] );
00212         $this->assertArrayHasKey( "result", $data[0]['login'] );
00213         $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
00214         $token = $data[0]['login']['token'];
00215 
00216         $data = $this->doApiRequest( array(
00217             'action' => 'login',
00218             "lgtoken" => $token,
00219             "lgname" => $sysopUser->username,
00220             "lgpassword" => $sysopUser->password ), $data[2] );
00221 
00222         $this->assertArrayHasKey( "login", $data[0] );
00223         $this->assertArrayHasKey( "result", $data[0]['login'] );
00224         $this->assertEquals( "Success", $data[0]['login']['result'] );
00225         $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
00226 
00227         return $data;
00228     }
00229 
00230     public function testGettingToken() {
00231         foreach ( self::$users as $user ) {
00232             $this->runTokenTest( $user );
00233         }
00234     }
00235 
00236     function runTokenTest( $user ) {
00237         $tokens = $this->getTokenList( $user );
00238 
00239         $rights = $user->user->getRights();
00240 
00241         $this->assertArrayHasKey( 'edittoken', $tokens );
00242         $this->assertArrayHasKey( 'movetoken', $tokens );
00243 
00244         if ( isset( $rights['delete'] ) ) {
00245             $this->assertArrayHasKey( 'deletetoken', $tokens );
00246         }
00247 
00248         if ( isset( $rights['block'] ) ) {
00249             $this->assertArrayHasKey( 'blocktoken', $tokens );
00250             $this->assertArrayHasKey( 'unblocktoken', $tokens );
00251         }
00252 
00253         if ( isset( $rights['protect'] ) ) {
00254             $this->assertArrayHasKey( 'protecttoken', $tokens );
00255         }
00256 
00257         return $tokens;
00258     }
00259 }