MediaWiki
master
|
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 00198 function testRunLogin() { 00199 $sysopUser = self::$users['sysop']; 00200 $data = $this->doApiRequest( array( 00201 'action' => 'login', 00202 'lgname' => $sysopUser->username, 00203 'lgpassword' => $sysopUser->password ) ); 00204 00205 $this->assertArrayHasKey( "login", $data[0] ); 00206 $this->assertArrayHasKey( "result", $data[0]['login'] ); 00207 $this->assertEquals( "NeedToken", $data[0]['login']['result'] ); 00208 $token = $data[0]['login']['token']; 00209 00210 $data = $this->doApiRequest( array( 00211 'action' => 'login', 00212 "lgtoken" => $token, 00213 "lgname" => $sysopUser->username, 00214 "lgpassword" => $sysopUser->password ), $data[2] ); 00215 00216 $this->assertArrayHasKey( "login", $data[0] ); 00217 $this->assertArrayHasKey( "result", $data[0]['login'] ); 00218 $this->assertEquals( "Success", $data[0]['login']['result'] ); 00219 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] ); 00220 00221 return $data; 00222 } 00223 00224 function testGettingToken() { 00225 foreach ( self::$users as $user ) { 00226 $this->runTokenTest( $user ); 00227 } 00228 } 00229 00230 function runTokenTest( $user ) { 00231 00232 $data = $this->getTokenList( $user ); 00233 00234 $this->assertArrayHasKey( 'query', $data[0] ); 00235 $this->assertArrayHasKey( 'pages', $data[0]['query'] ); 00236 $keys = array_keys( $data[0]['query']['pages'] ); 00237 $key = array_pop( $keys ); 00238 00239 $rights = $user->user->getRights(); 00240 00241 $this->assertArrayHasKey( $key, $data[0]['query']['pages'] ); 00242 $this->assertArrayHasKey( 'edittoken', $data[0]['query']['pages'][$key] ); 00243 $this->assertArrayHasKey( 'movetoken', $data[0]['query']['pages'][$key] ); 00244 00245 if ( isset( $rights['delete'] ) ) { 00246 $this->assertArrayHasKey( 'deletetoken', $data[0]['query']['pages'][$key] ); 00247 } 00248 00249 if ( isset( $rights['block'] ) ) { 00250 $this->assertArrayHasKey( 'blocktoken', $data[0]['query']['pages'][$key] ); 00251 $this->assertArrayHasKey( 'unblocktoken', $data[0]['query']['pages'][$key] ); 00252 } 00253 00254 if ( isset( $rights['protect'] ) ) { 00255 $this->assertArrayHasKey( 'protecttoken', $data[0]['query']['pages'][$key] ); 00256 } 00257 00258 return $data; 00259 } 00260 }