MediaWiki
REL1_22
|
00001 <?php 00002 00008 class UploadFromUrlTest extends ApiTestCase { 00009 protected function setUp() { 00010 parent::setUp(); 00011 00012 $this->setMwGlobals( array( 00013 'wgEnableUploads' => true, 00014 'wgAllowCopyUploads' => true, 00015 'wgAllowAsyncCopyUploads' => true, 00016 ) ); 00017 wfSetupSession(); 00018 00019 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) { 00020 $this->deleteFile( 'UploadFromUrlTest.png' ); 00021 } 00022 } 00023 00024 protected function doApiRequest( array $params, array $unused = null, $appendModule = false, User $user = null ) { 00025 $sessionId = session_id(); 00026 session_write_close(); 00027 00028 $req = new FauxRequest( $params, true, $_SESSION ); 00029 $module = new ApiMain( $req, true ); 00030 $module->execute(); 00031 00032 wfSetupSession( $sessionId ); 00033 00034 return array( $module->getResultData(), $req ); 00035 } 00036 00040 public function testClearQueue() { 00041 $job = JobQueueGroup::singleton()->pop(); 00042 while ( $job ) { 00043 $job = JobQueueGroup::singleton()->pop(); 00044 } 00045 $this->assertFalse( $job ); 00046 } 00047 00052 public function testLogin() { 00053 $data = $this->doApiRequest( array( 00054 'action' => 'login', 00055 'lgname' => $this->user->userName, 00056 'lgpassword' => $this->user->passWord ) ); 00057 $this->assertArrayHasKey( "login", $data[0] ); 00058 $this->assertArrayHasKey( "result", $data[0]['login'] ); 00059 $this->assertEquals( "NeedToken", $data[0]['login']['result'] ); 00060 $token = $data[0]['login']['token']; 00061 00062 $data = $this->doApiRequest( array( 00063 'action' => 'login', 00064 "lgtoken" => $token, 00065 'lgname' => $this->user->userName, 00066 'lgpassword' => $this->user->passWord ) ); 00067 00068 $this->assertArrayHasKey( "login", $data[0] ); 00069 $this->assertArrayHasKey( "result", $data[0]['login'] ); 00070 $this->assertEquals( "Success", $data[0]['login']['result'] ); 00071 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] ); 00072 00073 return $data; 00074 } 00075 00080 public function testSetupUrlDownload( $data ) { 00081 $token = $this->user->getEditToken(); 00082 $exception = false; 00083 00084 try { 00085 $this->doApiRequest( array( 00086 'action' => 'upload', 00087 ) ); 00088 } catch ( UsageException $e ) { 00089 $exception = true; 00090 $this->assertEquals( "The token parameter must be set", $e->getMessage() ); 00091 } 00092 $this->assertTrue( $exception, "Got exception" ); 00093 00094 $exception = false; 00095 try { 00096 $this->doApiRequest( array( 00097 'action' => 'upload', 00098 'token' => $token, 00099 ), $data ); 00100 } catch ( UsageException $e ) { 00101 $exception = true; 00102 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required", 00103 $e->getMessage() ); 00104 } 00105 $this->assertTrue( $exception, "Got exception" ); 00106 00107 $exception = false; 00108 try { 00109 $this->doApiRequest( array( 00110 'action' => 'upload', 00111 'url' => 'http://www.example.com/test.png', 00112 'token' => $token, 00113 ), $data ); 00114 } catch ( UsageException $e ) { 00115 $exception = true; 00116 $this->assertEquals( "The filename parameter must be set", $e->getMessage() ); 00117 } 00118 $this->assertTrue( $exception, "Got exception" ); 00119 00120 $this->user->removeGroup( 'sysop' ); 00121 $exception = false; 00122 try { 00123 $this->doApiRequest( array( 00124 'action' => 'upload', 00125 'url' => 'http://www.example.com/test.png', 00126 'filename' => 'UploadFromUrlTest.png', 00127 'token' => $token, 00128 ), $data ); 00129 } catch ( UsageException $e ) { 00130 $exception = true; 00131 $this->assertEquals( "Permission denied", $e->getMessage() ); 00132 } 00133 $this->assertTrue( $exception, "Got exception" ); 00134 00135 $this->user->addGroup( 'sysop' ); 00136 $data = $this->doApiRequest( array( 00137 'action' => 'upload', 00138 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png', 00139 'asyncdownload' => 1, 00140 'filename' => 'UploadFromUrlTest.png', 00141 'token' => $token, 00142 ), $data ); 00143 00144 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' ); 00145 00146 $job = JobQueueGroup::singleton()->pop(); 00147 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' ); 00148 } 00149 00154 public function testAsyncUpload( $data ) { 00155 $token = $this->user->getEditToken(); 00156 00157 $this->user->addGroup( 'users' ); 00158 00159 $data = $this->doAsyncUpload( $token, true ); 00160 $this->assertEquals( $data[0]['upload']['result'], 'Success' ); 00161 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' ); 00162 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() ); 00163 00164 $this->deleteFile( 'UploadFromUrlTest.png' ); 00165 00166 return $data; 00167 } 00168 00173 public function testAsyncUploadWarning( $data ) { 00174 $token = $this->user->getEditToken(); 00175 00176 $this->user->addGroup( 'users' ); 00177 00178 $data = $this->doAsyncUpload( $token ); 00179 00180 $this->assertEquals( $data[0]['upload']['result'], 'Warning' ); 00181 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) ); 00182 00183 $data = $this->doApiRequest( array( 00184 'action' => 'upload', 00185 'sessionkey' => $data[0]['upload']['sessionkey'], 00186 'filename' => 'UploadFromUrlTest.png', 00187 'ignorewarnings' => 1, 00188 'token' => $token, 00189 ) ); 00190 $this->assertEquals( $data[0]['upload']['result'], 'Success' ); 00191 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' ); 00192 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() ); 00193 00194 $this->deleteFile( 'UploadFromUrlTest.png' ); 00195 00196 return $data; 00197 } 00198 00203 public function testSyncDownload( $data ) { 00204 $token = $this->user->getEditToken(); 00205 00206 $job = JobQueueGroup::singleton()->pop(); 00207 $this->assertFalse( $job, 'Starting with an empty jobqueue' ); 00208 00209 $this->user->addGroup( 'users' ); 00210 $data = $this->doApiRequest( array( 00211 'action' => 'upload', 00212 'filename' => 'UploadFromUrlTest.png', 00213 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png', 00214 'ignorewarnings' => true, 00215 'token' => $token, 00216 ), $data ); 00217 00218 $job = JobQueueGroup::singleton()->pop(); 00219 $this->assertFalse( $job ); 00220 00221 $this->assertEquals( 'Success', $data[0]['upload']['result'] ); 00222 $this->deleteFile( 'UploadFromUrlTest.png' ); 00223 00224 return $data; 00225 } 00226 00227 public function testLeaveMessage() { 00228 $token = $this->user->user->getEditToken(); 00229 00230 $talk = $this->user->user->getTalkPage(); 00231 if ( $talk->exists() ) { 00232 $page = WikiPage::factory( $talk ); 00233 $page->doDeleteArticle( '' ); 00234 } 00235 00236 $this->assertFalse( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk does not exist' ); 00237 00238 $this->doApiRequest( array( 00239 'action' => 'upload', 00240 'filename' => 'UploadFromUrlTest.png', 00241 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png', 00242 'asyncdownload' => 1, 00243 'token' => $token, 00244 'leavemessage' => 1, 00245 'ignorewarnings' => 1, 00246 ) ); 00247 00248 $job = JobQueueGroup::singleton()->pop(); 00249 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) ); 00250 $job->run(); 00251 00252 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ); 00253 $this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' ); 00254 00255 $this->deleteFile( 'UploadFromUrlTest.png' ); 00256 00257 $talkRev = Revision::newFromTitle( $talk ); 00258 $talkSize = $talkRev->getSize(); 00259 00260 $exception = false; 00261 try { 00262 $this->doApiRequest( array( 00263 'action' => 'upload', 00264 'filename' => 'UploadFromUrlTest.png', 00265 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png', 00266 'asyncdownload' => 1, 00267 'token' => $token, 00268 'leavemessage' => 1, 00269 ) ); 00270 } catch ( UsageException $e ) { 00271 $exception = true; 00272 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() ); 00273 } 00274 $this->assertTrue( $exception ); 00275 00276 $job = JobQueueGroup::singleton()->pop(); 00277 $this->assertFalse( $job ); 00278 00279 return; 00280 /* 00281 // Broken until using leavemessage with ignorewarnings is supported 00282 $job->run(); 00283 00284 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ); 00285 00286 $talkRev = Revision::newFromTitle( $talk ); 00287 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' ); 00288 */ 00289 } 00290 00297 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) { 00298 $params = array( 00299 'action' => 'upload', 00300 'filename' => 'UploadFromUrlTest.png', 00301 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png', 00302 'asyncdownload' => 1, 00303 'token' => $token, 00304 ); 00305 if ( $ignoreWarnings ) { 00306 $params['ignorewarnings'] = 1; 00307 } 00308 if ( $leaveMessage ) { 00309 $params['leavemessage'] = 1; 00310 } 00311 00312 $data = $this->doApiRequest( $params ); 00313 $this->assertEquals( $data[0]['upload']['result'], 'Queued' ); 00314 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) ); 00315 $statusKey = $data[0]['upload']['statuskey']; 00316 00317 $job = JobQueueGroup::singleton()->pop(); 00318 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) ); 00319 00320 $status = $job->run(); 00321 $this->assertTrue( $status ); 00322 00323 $data = $this->doApiRequest( array( 00324 'action' => 'upload', 00325 'statuskey' => $statusKey, 00326 'token' => $token, 00327 ) ); 00328 00329 return $data; 00330 } 00331 00335 protected function deleteFile( $name ) { 00336 $t = Title::newFromText( $name, NS_FILE ); 00337 $this->assertTrue( $t->exists(), "File '$name' exists" ); 00338 00339 if ( $t->exists() ) { 00340 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) ); 00341 $empty = ""; 00342 FileDeleteForm::doDelete( $t, $file, $empty, "none", true ); 00343 $page = WikiPage::factory( $t ); 00344 $page->doDeleteArticle( "testing" ); 00345 } 00346 $t = Title::newFromText( $name, NS_FILE ); 00347 00348 $this->assertFalse( $t->exists(), "File '$name' was deleted" ); 00349 } 00350 }