8 * @covers UploadFromUrl
10 class UploadFromUrlTest
extends ApiTestCase
{
11 protected function setUp() {
14 $this->setMwGlobals( array(
15 'wgEnableUploads' => true,
16 'wgAllowCopyUploads' => true,
17 'wgAllowAsyncCopyUploads' => true,
21 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
22 $this->deleteFile( 'UploadFromUrlTest.png' );
26 protected function doApiRequest( array $params, array $unused = null,
27 $appendModule = false, User
$user = null
29 $sessionId = session_id();
30 session_write_close();
32 $req = new FauxRequest( $params, true, $_SESSION );
33 $module = new ApiMain( $req, true );
36 wfSetupSession( $sessionId );
38 return array( $module->getResultData(), $req );
42 * Ensure that the job queue is empty before continuing
44 public function testClearQueue() {
45 $job = JobQueueGroup
::singleton()->pop();
47 $job = JobQueueGroup
::singleton()->pop();
49 $this->assertFalse( $job );
53 * @depends testClearQueue
55 public function testSetupUrlDownload( $data ) {
56 $token = $this->user
->getEditToken();
60 $this->doApiRequest( array(
63 } catch ( UsageException
$e ) {
65 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
67 $this->assertTrue( $exception, "Got exception" );
71 $this->doApiRequest( array(
75 } catch ( UsageException
$e ) {
77 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
80 $this->assertTrue( $exception, "Got exception" );
84 $this->doApiRequest( array(
86 'url' => 'http://www.example.com/test.png',
89 } catch ( UsageException
$e ) {
91 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
93 $this->assertTrue( $exception, "Got exception" );
95 $this->user
->removeGroup( 'sysop' );
98 $this->doApiRequest( array(
100 'url' => 'http://www.example.com/test.png',
101 'filename' => 'UploadFromUrlTest.png',
104 } catch ( UsageException
$e ) {
106 $this->assertEquals( "Permission denied", $e->getMessage() );
108 $this->assertTrue( $exception, "Got exception" );
110 $this->user
->addGroup( 'sysop' );
111 $data = $this->doApiRequest( array(
112 'action' => 'upload',
113 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
114 'asyncdownload' => 1,
115 'filename' => 'UploadFromUrlTest.png',
119 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
121 $job = JobQueueGroup
::singleton()->pop();
122 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
126 * @depends testClearQueue
128 public function testAsyncUpload( $data ) {
129 $token = $this->user
->getEditToken();
131 $this->user
->addGroup( 'users' );
133 $data = $this->doAsyncUpload( $token, true );
134 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
135 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
136 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
138 $this->deleteFile( 'UploadFromUrlTest.png' );
144 * @depends testClearQueue
146 public function testAsyncUploadWarning( $data ) {
147 $token = $this->user
->getEditToken();
149 $this->user
->addGroup( 'users' );
151 $data = $this->doAsyncUpload( $token );
153 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
154 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
156 $data = $this->doApiRequest( array(
157 'action' => 'upload',
158 'sessionkey' => $data[0]['upload']['sessionkey'],
159 'filename' => 'UploadFromUrlTest.png',
160 'ignorewarnings' => 1,
163 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
164 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
165 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
167 $this->deleteFile( 'UploadFromUrlTest.png' );
173 * @depends testClearQueue
175 public function testSyncDownload( $data ) {
176 $token = $this->user
->getEditToken();
178 $job = JobQueueGroup
::singleton()->pop();
179 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
181 $this->user
->addGroup( 'users' );
182 $data = $this->doApiRequest( array(
183 'action' => 'upload',
184 'filename' => 'UploadFromUrlTest.png',
185 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
186 'ignorewarnings' => true,
190 $job = JobQueueGroup
::singleton()->pop();
191 $this->assertFalse( $job );
193 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
194 $this->deleteFile( 'UploadFromUrlTest.png' );
199 public function testLeaveMessage() {
200 $token = $this->user
->user
->getEditToken();
202 $talk = $this->user
->user
->getTalkPage();
203 if ( $talk->exists() ) {
204 $page = WikiPage
::factory( $talk );
205 $page->doDeleteArticle( '' );
209 (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
),
210 'User talk does not exist'
213 $this->doApiRequest( array(
214 'action' => 'upload',
215 'filename' => 'UploadFromUrlTest.png',
216 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
217 'asyncdownload' => 1,
220 'ignorewarnings' => 1,
223 $job = JobQueueGroup
::singleton()->pop();
224 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
227 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
228 $this->assertTrue( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk exists' );
230 $this->deleteFile( 'UploadFromUrlTest.png' );
234 $this->doApiRequest( array(
235 'action' => 'upload',
236 'filename' => 'UploadFromUrlTest.png',
237 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
238 'asyncdownload' => 1,
242 } catch ( UsageException
$e ) {
245 'Using leavemessage without ignorewarnings is not supported',
249 $this->assertTrue( $exception );
251 $job = JobQueueGroup
::singleton()->pop();
252 $this->assertFalse( $job );
256 // Broken until using leavemessage with ignorewarnings is supported
257 $talkRev = Revision::newFromTitle( $talk );
258 $talkSize = $talkRev->getSize();
262 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
264 $talkRev = Revision::newFromTitle( $talk );
265 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
270 * Helper function to perform an async upload, execute the job and fetch
273 * @param string $token
274 * @param bool $ignoreWarnings
275 * @param bool $leaveMessage
276 * @return array The result of action=upload&statuskey=key
278 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
280 'action' => 'upload',
281 'filename' => 'UploadFromUrlTest.png',
282 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
283 'asyncdownload' => 1,
286 if ( $ignoreWarnings ) {
287 $params['ignorewarnings'] = 1;
289 if ( $leaveMessage ) {
290 $params['leavemessage'] = 1;
293 $data = $this->doApiRequest( $params );
294 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
295 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
296 $statusKey = $data[0]['upload']['statuskey'];
298 $job = JobQueueGroup
::singleton()->pop();
299 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
301 $status = $job->run();
302 $this->assertTrue( $status );
304 $data = $this->doApiRequest( array(
305 'action' => 'upload',
306 'statuskey' => $statusKey,
313 protected function deleteFile( $name ) {
314 $t = Title
::newFromText( $name, NS_FILE
);
315 $this->assertTrue( $t->exists(), "File '$name' exists" );
317 if ( $t->exists() ) {
318 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
320 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
321 $page = WikiPage
::factory( $t );
322 $page->doDeleteArticle( "testing" );
324 $t = Title
::newFromText( $name, NS_FILE
);
326 $this->assertFalse( $t->exists(), "File '$name' was deleted" );