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