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