8 class UploadFromUrlTest
extends ApiTestCase
{
9 protected function setUp() {
12 $this->setMwGlobals( array(
13 'wgEnableUploads' => true,
14 'wgAllowCopyUploads' => true,
15 'wgAllowAsyncCopyUploads' => true,
19 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
20 $this->deleteFile( 'UploadFromUrlTest.png' );
24 protected function doApiRequest( array $params, array $unused = null, $appendModule = false, User
$user = null ) {
25 $sessionId = session_id();
26 session_write_close();
28 $req = new FauxRequest( $params, true, $_SESSION );
29 $module = new ApiMain( $req, true );
32 wfSetupSession( $sessionId );
34 return array( $module->getResultData(), $req );
38 * Ensure that the job queue is empty before continuing
40 public function testClearQueue() {
41 $job = JobQueueGroup
::singleton()->pop();
43 $job = JobQueueGroup
::singleton()->pop();
45 $this->assertFalse( $job );
49 * @todo Document why we test login, since the $wgUser hack used doesn't
52 public function testLogin() {
53 $data = $this->doApiRequest( array(
55 'lgname' => $this->user
->userName
,
56 'lgpassword' => $this->user
->passWord
) );
57 $this->assertArrayHasKey( "login", $data[0] );
58 $this->assertArrayHasKey( "result", $data[0]['login'] );
59 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
60 $token = $data[0]['login']['token'];
62 $data = $this->doApiRequest( array(
65 'lgname' => $this->user
->userName
,
66 'lgpassword' => $this->user
->passWord
) );
68 $this->assertArrayHasKey( "login", $data[0] );
69 $this->assertArrayHasKey( "result", $data[0]['login'] );
70 $this->assertEquals( "Success", $data[0]['login']['result'] );
71 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
78 * @depends testClearQueue
80 public function testSetupUrlDownload( $data ) {
81 $token = $this->user
->getEditToken();
85 $this->doApiRequest( array(
88 } catch ( UsageException
$e ) {
90 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
92 $this->assertTrue( $exception, "Got exception" );
96 $this->doApiRequest( array(
100 } catch ( UsageException
$e ) {
102 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
105 $this->assertTrue( $exception, "Got exception" );
109 $this->doApiRequest( array(
110 'action' => 'upload',
111 'url' => 'http://www.example.com/test.png',
114 } catch ( UsageException
$e ) {
116 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
118 $this->assertTrue( $exception, "Got exception" );
120 $this->user
->removeGroup( 'sysop' );
123 $this->doApiRequest( array(
124 'action' => 'upload',
125 'url' => 'http://www.example.com/test.png',
126 'filename' => 'UploadFromUrlTest.png',
129 } catch ( UsageException
$e ) {
131 $this->assertEquals( "Permission denied", $e->getMessage() );
133 $this->assertTrue( $exception, "Got exception" );
135 $this->user
->addGroup( 'sysop' );
136 $data = $this->doApiRequest( array(
137 'action' => 'upload',
138 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
139 'asyncdownload' => 1,
140 'filename' => 'UploadFromUrlTest.png',
144 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
146 $job = JobQueueGroup
::singleton()->pop();
147 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
152 * @depends testClearQueue
154 public function testAsyncUpload( $data ) {
155 $token = $this->user
->getEditToken();
157 $this->user
->addGroup( 'users' );
159 $data = $this->doAsyncUpload( $token, true );
160 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
161 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
162 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
164 $this->deleteFile( 'UploadFromUrlTest.png' );
171 * @depends testClearQueue
173 public function testAsyncUploadWarning( $data ) {
174 $token = $this->user
->getEditToken();
176 $this->user
->addGroup( 'users' );
178 $data = $this->doAsyncUpload( $token );
180 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
181 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
183 $data = $this->doApiRequest( array(
184 'action' => 'upload',
185 'sessionkey' => $data[0]['upload']['sessionkey'],
186 'filename' => 'UploadFromUrlTest.png',
187 'ignorewarnings' => 1,
190 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
191 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
192 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
194 $this->deleteFile( 'UploadFromUrlTest.png' );
201 * @depends testClearQueue
203 public function testSyncDownload( $data ) {
204 $token = $this->user
->getEditToken();
206 $job = JobQueueGroup
::singleton()->pop();
207 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
209 $this->user
->addGroup( 'users' );
210 $data = $this->doApiRequest( array(
211 'action' => 'upload',
212 'filename' => 'UploadFromUrlTest.png',
213 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
214 'ignorewarnings' => true,
218 $job = JobQueueGroup
::singleton()->pop();
219 $this->assertFalse( $job );
221 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
222 $this->deleteFile( 'UploadFromUrlTest.png' );
227 public function testLeaveMessage() {
228 $token = $this->user
->user
->getEditToken();
230 $talk = $this->user
->user
->getTalkPage();
231 if ( $talk->exists() ) {
232 $page = WikiPage
::factory( $talk );
233 $page->doDeleteArticle( '' );
236 $this->assertFalse( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk does not exist' );
238 $this->doApiRequest( array(
239 'action' => 'upload',
240 'filename' => 'UploadFromUrlTest.png',
241 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
242 'asyncdownload' => 1,
245 'ignorewarnings' => 1,
248 $job = JobQueueGroup
::singleton()->pop();
249 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
252 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
253 $this->assertTrue( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk exists' );
255 $this->deleteFile( 'UploadFromUrlTest.png' );
257 $talkRev = Revision
::newFromTitle( $talk );
258 $talkSize = $talkRev->getSize();
262 $this->doApiRequest( array(
263 'action' => 'upload',
264 'filename' => 'UploadFromUrlTest.png',
265 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
266 'asyncdownload' => 1,
270 } catch ( UsageException
$e ) {
272 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
274 $this->assertTrue( $exception );
276 $job = JobQueueGroup
::singleton()->pop();
277 $this->assertFalse( $job );
281 // Broken until using leavemessage with ignorewarnings is supported
284 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
286 $talkRev = Revision::newFromTitle( $talk );
287 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
292 * Helper function to perform an async upload, execute the job and fetch
295 * @return array The result of action=upload&statuskey=key
297 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
299 'action' => 'upload',
300 'filename' => 'UploadFromUrlTest.png',
301 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
302 'asyncdownload' => 1,
305 if ( $ignoreWarnings ) {
306 $params['ignorewarnings'] = 1;
308 if ( $leaveMessage ) {
309 $params['leavemessage'] = 1;
312 $data = $this->doApiRequest( $params );
313 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
314 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
315 $statusKey = $data[0]['upload']['statuskey'];
317 $job = JobQueueGroup
::singleton()->pop();
318 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
320 $status = $job->run();
321 $this->assertTrue( $status );
323 $data = $this->doApiRequest( array(
324 'action' => 'upload',
325 'statuskey' => $statusKey,
335 protected function deleteFile( $name ) {
336 $t = Title
::newFromText( $name, NS_FILE
);
337 $this->assertTrue( $t->exists(), "File '$name' exists" );
339 if ( $t->exists() ) {
340 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
342 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
343 $page = WikiPage
::factory( $t );
344 $page->doDeleteArticle( "testing" );
346 $t = Title
::newFromText( $name, NS_FILE
);
348 $this->assertFalse( $t->exists(), "File '$name' was deleted" );