2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Broken
|
2011-03-06 09:01:19 +00:00
|
|
|
* @group Upload
|
2012-12-09 09:27:56 +00:00
|
|
|
* @group Database
|
2013-10-24 20:30:43 +00:00
|
|
|
*
|
|
|
|
|
* @covers UploadFromUrl
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2011-07-01 16:34:02 +00:00
|
|
|
class UploadFromUrlTest extends ApiTestCase {
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2010-12-14 16:26:35 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2013-03-21 19:35:44 +00:00
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgEnableUploads' => true,
|
|
|
|
|
'wgAllowCopyUploads' => true,
|
|
|
|
|
'wgAllowAsyncCopyUploads' => true,
|
|
|
|
|
) );
|
2010-12-14 16:26:35 +00:00
|
|
|
wfSetupSession();
|
|
|
|
|
|
|
|
|
|
if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
|
|
|
|
|
$this->deleteFile( 'UploadFromUrlTest.png' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-24 17:52:34 +00:00
|
|
|
protected function doApiRequest( array $params, array $unused = null,
|
|
|
|
|
$appendModule = false, User $user = null
|
|
|
|
|
) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$sessionId = session_id();
|
|
|
|
|
session_write_close();
|
|
|
|
|
|
|
|
|
|
$req = new FauxRequest( $params, true, $_SESSION );
|
|
|
|
|
$module = new ApiMain( $req, true );
|
|
|
|
|
$module->execute();
|
|
|
|
|
|
|
|
|
|
wfSetupSession( $sessionId );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
return array( $module->getResultData(), $req );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Ensure that the job queue is empty before continuing
|
|
|
|
|
*/
|
|
|
|
|
public function testClearQueue() {
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2011-10-16 03:27:12 +00:00
|
|
|
while ( $job ) {
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2011-10-16 03:27:12 +00:00
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalse( $job );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testClearQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testSetupUrlDownload( $data ) {
|
2012-01-07 23:26:35 +00:00
|
|
|
$token = $this->user->getEditToken();
|
2010-12-14 16:26:35 +00:00
|
|
|
$exception = false;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$this->doApiRequest( array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
) );
|
|
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
$this->assertEquals( "The token parameter must be set", $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
|
|
|
|
$this->doApiRequest( array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'token' => $token,
|
|
|
|
|
), $data );
|
|
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
$this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
|
|
|
|
|
$e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
|
|
|
|
$this->doApiRequest( array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'url' => 'http://www.example.com/test.png',
|
|
|
|
|
'token' => $token,
|
|
|
|
|
), $data );
|
|
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
$this->assertEquals( "The filename parameter must be set", $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$this->user->removeGroup( 'sysop' );
|
2010-12-14 16:26:35 +00:00
|
|
|
$exception = false;
|
|
|
|
|
try {
|
|
|
|
|
$this->doApiRequest( array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'url' => 'http://www.example.com/test.png',
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
|
|
|
|
'token' => $token,
|
|
|
|
|
), $data );
|
|
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
$this->assertEquals( "Permission denied", $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$this->user->addGroup( 'sysop' );
|
2010-12-14 16:26:35 +00:00
|
|
|
$data = $this->doApiRequest( array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
|
|
|
|
|
'asyncdownload' => 1,
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
|
|
|
|
'token' => $token,
|
|
|
|
|
), $data );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
|
|
|
|
|
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testClearQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testAsyncUpload( $data ) {
|
2012-01-07 23:26:35 +00:00
|
|
|
$token = $this->user->getEditToken();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$this->user->addGroup( 'users' );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$data = $this->doAsyncUpload( $token, true );
|
|
|
|
|
$this->assertEquals( $data[0]['upload']['result'], 'Success' );
|
|
|
|
|
$this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
|
|
|
|
|
$this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
|
|
|
|
|
|
|
|
|
|
$this->deleteFile( 'UploadFromUrlTest.png' );
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testClearQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testAsyncUploadWarning( $data ) {
|
2012-01-07 23:26:35 +00:00
|
|
|
$token = $this->user->getEditToken();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$this->user->addGroup( 'users' );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$data = $this->doAsyncUpload( $token );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $data[0]['upload']['result'], 'Warning' );
|
|
|
|
|
$this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
|
|
|
|
|
|
|
|
|
|
$data = $this->doApiRequest( array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'sessionkey' => $data[0]['upload']['sessionkey'],
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
|
|
|
|
'ignorewarnings' => 1,
|
|
|
|
|
'token' => $token,
|
|
|
|
|
) );
|
|
|
|
|
$this->assertEquals( $data[0]['upload']['result'], 'Success' );
|
|
|
|
|
$this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
|
|
|
|
|
$this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
|
|
|
|
|
|
|
|
|
|
$this->deleteFile( 'UploadFromUrlTest.png' );
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testClearQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testSyncDownload( $data ) {
|
2012-01-07 23:26:35 +00:00
|
|
|
$token = $this->user->getEditToken();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalse( $job, 'Starting with an empty jobqueue' );
|
|
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$this->user->addGroup( 'users' );
|
2010-12-14 16:26:35 +00:00
|
|
|
$data = $this->doApiRequest( array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
|
|
|
|
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
|
|
|
|
|
'ignorewarnings' => true,
|
|
|
|
|
'token' => $token,
|
|
|
|
|
), $data );
|
|
|
|
|
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalse( $job );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 'Success', $data[0]['upload']['result'] );
|
|
|
|
|
$this->deleteFile( 'UploadFromUrlTest.png' );
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testLeaveMessage() {
|
2012-01-07 23:26:35 +00:00
|
|
|
$token = $this->user->user->getEditToken();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2011-03-06 09:01:19 +00:00
|
|
|
$talk = $this->user->user->getTalkPage();
|
2010-12-14 16:26:35 +00:00
|
|
|
if ( $talk->exists() ) {
|
2012-02-13 16:38:37 +00:00
|
|
|
$page = WikiPage::factory( $talk );
|
|
|
|
|
$page->doDeleteArticle( '' );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-24 17:52:34 +00:00
|
|
|
$this->assertFalse(
|
|
|
|
|
(bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ),
|
|
|
|
|
'User talk does not exist'
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2013-04-26 07:48:46 +00:00
|
|
|
$this->doApiRequest( array(
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
|
|
|
|
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
|
|
|
|
|
'asyncdownload' => 1,
|
|
|
|
|
'token' => $token,
|
|
|
|
|
'leavemessage' => 1,
|
|
|
|
|
'ignorewarnings' => 1,
|
|
|
|
|
) );
|
|
|
|
|
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
|
|
|
|
|
$job->run();
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
|
2012-03-11 18:54:55 +00:00
|
|
|
$this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$this->deleteFile( 'UploadFromUrlTest.png' );
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2013-04-26 07:48:46 +00:00
|
|
|
$this->doApiRequest( array(
|
2010-12-14 16:26:35 +00:00
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
|
|
|
|
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
|
|
|
|
|
'asyncdownload' => 1,
|
|
|
|
|
'token' => $token,
|
|
|
|
|
'leavemessage' => 1,
|
|
|
|
|
) );
|
|
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
2014-04-24 17:52:34 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'Using leavemessage without ignorewarnings is not supported',
|
|
|
|
|
$e->getMessage()
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception );
|
|
|
|
|
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalse( $job );
|
|
|
|
|
|
|
|
|
|
return;
|
2011-12-21 22:48:00 +00:00
|
|
|
/*
|
2010-12-14 16:26:35 +00:00
|
|
|
// Broken until using leavemessage with ignorewarnings is supported
|
2014-01-25 13:26:03 +00:00
|
|
|
$talkRev = Revision::newFromTitle( $talk );
|
|
|
|
|
$talkSize = $talkRev->getSize();
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$job->run();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
|
|
|
|
|
|
|
|
|
|
$talkRev = Revision::newFromTitle( $talk );
|
|
|
|
|
$this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to perform an async upload, execute the job and fetch
|
|
|
|
|
* the status
|
|
|
|
|
*
|
2014-08-25 16:50:35 +00:00
|
|
|
* @param string $token
|
|
|
|
|
* @param bool $ignoreWarnings
|
|
|
|
|
* @param bool $leaveMessage
|
2010-12-14 16:26:35 +00:00
|
|
|
* @return array The result of action=upload&statuskey=key
|
|
|
|
|
*/
|
|
|
|
|
private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => 'UploadFromUrlTest.png',
|
|
|
|
|
'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
|
|
|
|
|
'asyncdownload' => 1,
|
|
|
|
|
'token' => $token,
|
|
|
|
|
);
|
|
|
|
|
if ( $ignoreWarnings ) {
|
|
|
|
|
$params['ignorewarnings'] = 1;
|
|
|
|
|
}
|
|
|
|
|
if ( $leaveMessage ) {
|
|
|
|
|
$params['leavemessage'] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $this->doApiRequest( $params );
|
|
|
|
|
$this->assertEquals( $data[0]['upload']['result'], 'Queued' );
|
|
|
|
|
$this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
|
|
|
|
|
$statusKey = $data[0]['upload']['statuskey'];
|
|
|
|
|
|
2012-12-07 19:30:45 +00:00
|
|
|
$job = JobQueueGroup::singleton()->pop();
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
|
|
|
|
|
|
|
|
|
|
$status = $job->run();
|
|
|
|
|
$this->assertTrue( $status );
|
|
|
|
|
|
|
|
|
|
$data = $this->doApiRequest( array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'statuskey' => $statusKey,
|
|
|
|
|
'token' => $token,
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function deleteFile( $name ) {
|
|
|
|
|
$t = Title::newFromText( $name, NS_FILE );
|
2013-02-15 10:35:55 +00:00
|
|
|
$this->assertTrue( $t->exists(), "File '$name' exists" );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
if ( $t->exists() ) {
|
|
|
|
|
$file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
|
|
|
|
|
$empty = "";
|
|
|
|
|
FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
|
2012-02-13 16:38:37 +00:00
|
|
|
$page = WikiPage::factory( $t );
|
|
|
|
|
$page->doDeleteArticle( "testing" );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
$t = Title::newFromText( $name, NS_FILE );
|
|
|
|
|
|
2013-02-15 10:35:55 +00:00
|
|
|
$this->assertFalse( $t->exists(), "File '$name' was deleted" );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2013-02-15 10:35:55 +00:00
|
|
|
}
|