7834d9bebad7c0d97a19b78231d69b6beedfbe71
8 * @covers UploadFromUrl
10 class UploadFromUrlTest
extends ApiTestCase
{
11 protected function setUp() {
14 $this->setMwGlobals( array(
15 'wgEnableUploads' => true,
16 'wgAllowCopyUploads' => true,
20 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
21 $this->deleteFile( 'UploadFromUrlTest.png' );
25 protected function doApiRequest( array $params, array $unused = null,
26 $appendModule = false, User
$user = null
28 $sessionId = session_id();
29 session_write_close();
31 $req = new FauxRequest( $params, true, $_SESSION );
32 $module = new ApiMain( $req, true );
35 wfSetupSession( $sessionId );
38 $module->getResult()->getResultData( null, array( 'Strip' => 'all' ) ),
44 * Ensure that the job queue is empty before continuing
46 public function testClearQueue() {
47 $job = JobQueueGroup
::singleton()->pop();
49 $job = JobQueueGroup
::singleton()->pop();
51 $this->assertFalse( $job );
55 * @depends testClearQueue
57 public function testSetupUrlDownload( $data ) {
58 $token = $this->user
->getEditToken();
62 $this->doApiRequest( array(
65 } catch ( UsageException
$e ) {
67 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
69 $this->assertTrue( $exception, "Got exception" );
73 $this->doApiRequest( array(
77 } catch ( UsageException
$e ) {
79 $this->assertEquals( "One of the parameters sessionkey, file, url is required",
82 $this->assertTrue( $exception, "Got exception" );
86 $this->doApiRequest( array(
88 'url' => 'http://www.example.com/test.png',
91 } catch ( UsageException
$e ) {
93 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
95 $this->assertTrue( $exception, "Got exception" );
97 $this->user
->removeGroup( 'sysop' );
100 $this->doApiRequest( array(
101 'action' => 'upload',
102 'url' => 'http://www.example.com/test.png',
103 'filename' => 'UploadFromUrlTest.png',
106 } catch ( UsageException
$e ) {
108 $this->assertEquals( "Permission denied", $e->getMessage() );
110 $this->assertTrue( $exception, "Got exception" );
114 * @depends testClearQueue
116 public function testSyncDownload( $data ) {
117 $token = $this->user
->getEditToken();
119 $job = JobQueueGroup
::singleton()->pop();
120 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
122 $this->user
->addGroup( 'users' );
123 $data = $this->doApiRequest( array(
124 'action' => 'upload',
125 'filename' => 'UploadFromUrlTest.png',
126 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
127 'ignorewarnings' => true,
131 $job = JobQueueGroup
::singleton()->pop();
132 $this->assertFalse( $job );
134 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
135 $this->deleteFile( 'UploadFromUrlTest.png' );
140 protected function deleteFile( $name ) {
141 $t = Title
::newFromText( $name, NS_FILE
);
142 $this->assertTrue( $t->exists(), "File '$name' exists" );
144 if ( $t->exists() ) {
145 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
147 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
148 $page = WikiPage
::factory( $t );
149 $page->doDeleteArticle( "testing" );
151 $t = Title
::newFromText( $name, NS_FILE
);
153 $this->assertFalse( $t->exists(), "File '$name' was deleted" );