8 * @covers UploadFromUrl
10 class UploadFromUrlTest
extends ApiTestCase
{
11 protected function setUp() {
14 $this->setMwGlobals( [
15 'wgEnableUploads' => true,
16 'wgAllowCopyUploads' => true,
19 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
20 $this->deleteFile( 'UploadFromUrlTest.png' );
24 protected function doApiRequest( array $params, array $unused = null,
25 $appendModule = false, User
$user = null
29 $req = new FauxRequest( $params, true, $wgRequest->getSession() );
30 $module = new ApiMain( $req, true );
34 $module->getResult()->getResultData( null, [ 'Strip' => 'all' ] ),
40 * Ensure that the job queue is empty before continuing
42 public function testClearQueue() {
43 $job = JobQueueGroup
::singleton()->pop();
45 $job = JobQueueGroup
::singleton()->pop();
47 $this->assertFalse( $job );
51 * @depends testClearQueue
53 public function testSetupUrlDownload( $data ) {
54 $token = $this->user
->getEditToken();
58 $this->doApiRequest( [
61 } catch ( ApiUsageException
$e ) {
63 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
65 $this->assertTrue( $exception, "Got exception" );
69 $this->doApiRequest( [
73 } catch ( ApiUsageException
$e ) {
75 $this->assertEquals( "One of the parameters sessionkey, file, url is required",
78 $this->assertTrue( $exception, "Got exception" );
82 $this->doApiRequest( [
84 'url' => 'http://www.example.com/test.png',
87 } catch ( ApiUsageException
$e ) {
89 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
91 $this->assertTrue( $exception, "Got exception" );
93 $this->user
->removeGroup( 'sysop' );
96 $this->doApiRequest( [
98 'url' => 'http://www.example.com/test.png',
99 'filename' => 'UploadFromUrlTest.png',
102 } catch ( ApiUsageException
$e ) {
104 $this->assertEquals( "Permission denied", $e->getMessage() );
106 $this->assertTrue( $exception, "Got exception" );
110 * @depends testClearQueue
112 public function testSyncDownload( $data ) {
113 $token = $this->user
->getEditToken();
115 $job = JobQueueGroup
::singleton()->pop();
116 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
118 $this->user
->addGroup( 'users' );
119 $data = $this->doApiRequest( [
120 'action' => 'upload',
121 'filename' => 'UploadFromUrlTest.png',
122 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
123 'ignorewarnings' => true,
127 $job = JobQueueGroup
::singleton()->pop();
128 $this->assertFalse( $job );
130 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
131 $this->deleteFile( 'UploadFromUrlTest.png' );
136 protected function deleteFile( $name ) {
137 $t = Title
::newFromText( $name, NS_FILE
);
138 $this->assertTrue( $t->exists(), "File '$name' exists" );
140 if ( $t->exists() ) {
141 $file = wfFindFile( $name, [ 'ignoreRedirect' => true ] );
143 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
144 $page = WikiPage
::factory( $t );
145 $page->doDeleteArticle( "testing" );
147 $t = Title
::newFromText( $name, NS_FILE
);
149 $this->assertFalse( $t->exists(), "File '$name' was deleted" );