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