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,
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
30 $req = new FauxRequest( $params, true, $wgRequest->getSession() );
31 $module = new ApiMain( $req, true );
35 $module->getResult()->getResultData( null, array( 'Strip' => 'all' ) ),
41 * Ensure that the job queue is empty before continuing
43 public function testClearQueue() {
44 $job = JobQueueGroup
::singleton()->pop();
46 $job = JobQueueGroup
::singleton()->pop();
48 $this->assertFalse( $job );
52 * @depends testClearQueue
54 public function testSetupUrlDownload( $data ) {
55 $token = $this->user
->getEditToken();
59 $this->doApiRequest( array(
62 } catch ( UsageException
$e ) {
64 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
66 $this->assertTrue( $exception, "Got exception" );
70 $this->doApiRequest( array(
74 } catch ( UsageException
$e ) {
76 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
79 $this->assertTrue( $exception, "Got exception" );
83 $this->doApiRequest( array(
85 'url' => 'http://www.example.com/test.png',
88 } catch ( UsageException
$e ) {
90 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
92 $this->assertTrue( $exception, "Got exception" );
94 $this->user
->removeGroup( 'sysop' );
97 $this->doApiRequest( array(
99 'url' => 'http://www.example.com/test.png',
100 'filename' => 'UploadFromUrlTest.png',
103 } catch ( UsageException
$e ) {
105 $this->assertEquals( "Permission denied", $e->getMessage() );
107 $this->assertTrue( $exception, "Got exception" );
109 $this->user
->addGroup( 'sysop' );
110 $data = $this->doApiRequest( array(
111 'action' => 'upload',
112 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
113 'asyncdownload' => 1,
114 'filename' => 'UploadFromUrlTest.png',
118 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
120 $job = JobQueueGroup
::singleton()->pop();
121 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
125 * @depends testClearQueue
127 public function testAsyncUpload( $data ) {
128 $token = $this->user
->getEditToken();
130 $this->user
->addGroup( 'users' );
132 $data = $this->doAsyncUpload( $token, true );
133 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
134 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
135 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
137 $this->deleteFile( 'UploadFromUrlTest.png' );
143 * @depends testClearQueue
145 public function testAsyncUploadWarning( $data ) {
146 $token = $this->user
->getEditToken();
148 $this->user
->addGroup( 'users' );
150 $data = $this->doAsyncUpload( $token );
152 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
153 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
155 $data = $this->doApiRequest( array(
156 'action' => 'upload',
157 'sessionkey' => $data[0]['upload']['sessionkey'],
158 'filename' => 'UploadFromUrlTest.png',
159 'ignorewarnings' => 1,
162 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
163 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
164 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
166 $this->deleteFile( 'UploadFromUrlTest.png' );
172 * @depends testClearQueue
174 public function testSyncDownload( $data ) {
175 $token = $this->user
->getEditToken();
177 $job = JobQueueGroup
::singleton()->pop();
178 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
180 $this->user
->addGroup( 'users' );
181 $data = $this->doApiRequest( array(
182 'action' => 'upload',
183 'filename' => 'UploadFromUrlTest.png',
184 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
185 'ignorewarnings' => true,
189 $job = JobQueueGroup
::singleton()->pop();
190 $this->assertFalse( $job );
192 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
193 $this->deleteFile( 'UploadFromUrlTest.png' );
198 public function testLeaveMessage() {
199 $token = $this->user
->user
->getEditToken();
201 $talk = $this->user
->user
->getTalkPage();
202 if ( $talk->exists() ) {
203 $page = WikiPage
::factory( $talk );
204 $page->doDeleteArticle( '' );
208 (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
),
209 'User talk does not exist'
212 $this->doApiRequest( array(
213 'action' => 'upload',
214 'filename' => 'UploadFromUrlTest.png',
215 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
216 'asyncdownload' => 1,
219 'ignorewarnings' => 1,
222 $job = JobQueueGroup
::singleton()->pop();
223 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
226 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
227 $this->assertTrue( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk exists' );
229 $this->deleteFile( 'UploadFromUrlTest.png' );
233 $this->doApiRequest( array(
234 'action' => 'upload',
235 'filename' => 'UploadFromUrlTest.png',
236 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
237 'asyncdownload' => 1,
241 } catch ( UsageException
$e ) {
244 'Using leavemessage without ignorewarnings is not supported',
248 $this->assertTrue( $exception );
250 $job = JobQueueGroup
::singleton()->pop();
251 $this->assertFalse( $job );
255 // Broken until using leavemessage with ignorewarnings is supported
256 $talkRev = Revision::newFromTitle( $talk );
257 $talkSize = $talkRev->getSize();
261 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
263 $talkRev = Revision::newFromTitle( $talk );
264 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
269 * Helper function to perform an async upload, execute the job and fetch
272 * @param string $token
273 * @param bool $ignoreWarnings
274 * @param bool $leaveMessage
275 * @return array The result of action=upload&statuskey=key
277 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
279 'action' => 'upload',
280 'filename' => 'UploadFromUrlTest.png',
281 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
282 'asyncdownload' => 1,
285 if ( $ignoreWarnings ) {
286 $params['ignorewarnings'] = 1;
288 if ( $leaveMessage ) {
289 $params['leavemessage'] = 1;
292 $data = $this->doApiRequest( $params );
293 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
294 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
295 $statusKey = $data[0]['upload']['statuskey'];
297 $job = JobQueueGroup
::singleton()->pop();
298 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
300 $status = $job->run();
301 $this->assertTrue( $status );
303 $data = $this->doApiRequest( array(
304 'action' => 'upload',
305 'statuskey' => $statusKey,
312 protected function deleteFile( $name ) {
313 $t = Title
::newFromText( $name, NS_FILE
);
314 $this->assertTrue( $t->exists(), "File '$name' exists" );
316 if ( $t->exists() ) {
317 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
319 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
320 $page = WikiPage
::factory( $t );
321 $page->doDeleteArticle( "testing" );
323 $t = Title
::newFromText( $name, NS_FILE
);
325 $this->assertFalse( $t->exists(), "File '$name' was deleted" );