MediaWiki  REL1_21
UploadFromUrlTest.php
Go to the documentation of this file.
00001 <?php
00002 
00008 class UploadFromUrlTest extends ApiTestCase {
00009 
00010         protected function setUp() {
00011                 global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads;
00012                 parent::setUp();
00013 
00014                 $wgEnableUploads = true;
00015                 $wgAllowCopyUploads = true;
00016                 $wgAllowAsyncCopyUploads = true;
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                 return array( $module->getResultData(), $req );
00034         }
00035 
00039         public function testClearQueue() {
00040                 $job = JobQueueGroup::singleton()->pop();
00041                 while ( $job ) {
00042                         $job = JobQueueGroup::singleton()->pop();
00043                 }
00044                 $this->assertFalse( $job );
00045         }
00046 
00051         public function testLogin() {
00052                 $data = $this->doApiRequest( array(
00053                         'action' => 'login',
00054                         'lgname' => $this->user->userName,
00055                         'lgpassword' => $this->user->passWord ) );
00056                 $this->assertArrayHasKey( "login", $data[0] );
00057                 $this->assertArrayHasKey( "result", $data[0]['login'] );
00058                 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
00059                 $token = $data[0]['login']['token'];
00060 
00061                 $data = $this->doApiRequest( array(
00062                         'action' => 'login',
00063                         "lgtoken" => $token,
00064                         'lgname' => $this->user->userName,
00065                         'lgpassword' => $this->user->passWord ) );
00066 
00067                 $this->assertArrayHasKey( "login", $data[0] );
00068                 $this->assertArrayHasKey( "result", $data[0]['login'] );
00069                 $this->assertEquals( "Success", $data[0]['login']['result'] );
00070                 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
00071 
00072                 return $data;
00073         }
00074 
00079         public function testSetupUrlDownload( $data ) {
00080                 $token = $this->user->getEditToken();
00081                 $exception = false;
00082 
00083                 try {
00084                         $this->doApiRequest( array(
00085                                 'action' => 'upload',
00086                         ) );
00087                 } catch ( UsageException $e ) {
00088                         $exception = true;
00089                         $this->assertEquals( "The token parameter must be set", $e->getMessage() );
00090                 }
00091                 $this->assertTrue( $exception, "Got exception" );
00092 
00093                 $exception = false;
00094                 try {
00095                         $this->doApiRequest( array(
00096                                 'action' => 'upload',
00097                                 'token' => $token,
00098                         ), $data );
00099                 } catch ( UsageException $e ) {
00100                         $exception = true;
00101                         $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
00102                                 $e->getMessage() );
00103                 }
00104                 $this->assertTrue( $exception, "Got exception" );
00105 
00106                 $exception = false;
00107                 try {
00108                         $this->doApiRequest( array(
00109                                 'action' => 'upload',
00110                                 'url' => 'http://www.example.com/test.png',
00111                                 'token' => $token,
00112                         ), $data );
00113                 } catch ( UsageException $e ) {
00114                         $exception = true;
00115                         $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
00116                 }
00117                 $this->assertTrue( $exception, "Got exception" );
00118 
00119                 $this->user->removeGroup( 'sysop' );
00120                 $exception = false;
00121                 try {
00122                         $this->doApiRequest( array(
00123                                 'action' => 'upload',
00124                                 'url' => 'http://www.example.com/test.png',
00125                                 'filename' => 'UploadFromUrlTest.png',
00126                                 'token' => $token,
00127                         ), $data );
00128                 } catch ( UsageException $e ) {
00129                         $exception = true;
00130                         $this->assertEquals( "Permission denied", $e->getMessage() );
00131                 }
00132                 $this->assertTrue( $exception, "Got exception" );
00133 
00134                 $this->user->addGroup( 'sysop' );
00135                 $data = $this->doApiRequest( array(
00136                         'action' => 'upload',
00137                         'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
00138                         'asyncdownload' => 1,
00139                         'filename' => 'UploadFromUrlTest.png',
00140                         'token' => $token,
00141                 ), $data );
00142 
00143                 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
00144 
00145                 $job = JobQueueGroup::singleton()->pop();
00146                 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
00147         }
00148 
00153         public function testAsyncUpload( $data ) {
00154                 $token = $this->user->getEditToken();
00155 
00156                 $this->user->addGroup( 'users' );
00157 
00158                 $data = $this->doAsyncUpload( $token, true );
00159                 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
00160                 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
00161                 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
00162 
00163                 $this->deleteFile( 'UploadFromUrlTest.png' );
00164 
00165                 return $data;
00166         }
00167 
00172         public function testAsyncUploadWarning( $data ) {
00173                 $token = $this->user->getEditToken();
00174 
00175                 $this->user->addGroup( 'users' );
00176 
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                 $data = $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                         $data = $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                 /*
00282                 // Broken until using leavemessage with ignorewarnings is supported
00283                 $job->run();
00284 
00285                 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
00286 
00287                 $talkRev = Revision::newFromTitle( $talk );
00288                 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
00289                 */
00290         }
00291 
00298         private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
00299                 $params = array(
00300                         'action' => 'upload',
00301                         'filename' => 'UploadFromUrlTest.png',
00302                         'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
00303                         'asyncdownload' => 1,
00304                         'token' => $token,
00305                 );
00306                 if ( $ignoreWarnings ) {
00307                         $params['ignorewarnings'] = 1;
00308                 }
00309                 if ( $leaveMessage ) {
00310                         $params['leavemessage'] = 1;
00311                 }
00312 
00313                 $data = $this->doApiRequest( $params );
00314                 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
00315                 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
00316                 $statusKey = $data[0]['upload']['statuskey'];
00317 
00318                 $job = JobQueueGroup::singleton()->pop();
00319                 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
00320 
00321                 $status = $job->run();
00322                 $this->assertTrue( $status );
00323 
00324                 $data = $this->doApiRequest( array(
00325                         'action' => 'upload',
00326                         'statuskey' => $statusKey,
00327                         'token' => $token,
00328                 ) );
00329 
00330                 return $data;
00331         }
00332 
00333 
00337         protected function deleteFile( $name ) {
00338                 $t = Title::newFromText( $name, NS_FILE );
00339                 $this->assertTrue( $t->exists(), "File '$name' exists" );
00340 
00341                 if ( $t->exists() ) {
00342                         $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
00343                         $empty = "";
00344                         FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
00345                         $page = WikiPage::factory( $t );
00346                         $page->doDeleteArticle( "testing" );
00347                 }
00348                 $t = Title::newFromText( $name, NS_FILE );
00349 
00350                 $this->assertFalse( $t->exists(), "File '$name' was deleted" );
00351         }
00352 }