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