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, $appendModule = false, User
$user = null ) {
27 $sessionId = session_id();
28 session_write_close();
30 $req = new FauxRequest( $params, true, $_SESSION );
31 $module = new ApiMain( $req, true );
34 wfSetupSession( $sessionId );
36 return array( $module->getResultData(), $req );
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 * @todo Document why we test login, since the $wgUser hack used doesn't
56 public function testLogin() {
57 $data = $this->doApiRequest( array(
59 'lgname' => $this->user
->userName
,
60 'lgpassword' => $this->user
->passWord
) );
61 $this->assertArrayHasKey( "login", $data[0] );
62 $this->assertArrayHasKey( "result", $data[0]['login'] );
63 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
64 $token = $data[0]['login']['token'];
66 $data = $this->doApiRequest( array(
69 'lgname' => $this->user
->userName
,
70 'lgpassword' => $this->user
->passWord
) );
72 $this->assertArrayHasKey( "login", $data[0] );
73 $this->assertArrayHasKey( "result", $data[0]['login'] );
74 $this->assertEquals( "Success", $data[0]['login']['result'] );
75 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
82 * @depends testClearQueue
84 public function testSetupUrlDownload( $data ) {
85 $token = $this->user
->getEditToken();
89 $this->doApiRequest( array(
92 } catch ( UsageException
$e ) {
94 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
96 $this->assertTrue( $exception, "Got exception" );
100 $this->doApiRequest( array(
101 'action' => 'upload',
104 } catch ( UsageException
$e ) {
106 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
109 $this->assertTrue( $exception, "Got exception" );
113 $this->doApiRequest( array(
114 'action' => 'upload',
115 'url' => 'http://www.example.com/test.png',
118 } catch ( UsageException
$e ) {
120 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
122 $this->assertTrue( $exception, "Got exception" );
124 $this->user
->removeGroup( 'sysop' );
127 $this->doApiRequest( array(
128 'action' => 'upload',
129 'url' => 'http://www.example.com/test.png',
130 'filename' => 'UploadFromUrlTest.png',
133 } catch ( UsageException
$e ) {
135 $this->assertEquals( "Permission denied", $e->getMessage() );
137 $this->assertTrue( $exception, "Got exception" );
139 $this->user
->addGroup( 'sysop' );
140 $data = $this->doApiRequest( array(
141 'action' => 'upload',
142 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
143 'asyncdownload' => 1,
144 'filename' => 'UploadFromUrlTest.png',
148 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
150 $job = JobQueueGroup
::singleton()->pop();
151 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
156 * @depends testClearQueue
158 public function testAsyncUpload( $data ) {
159 $token = $this->user
->getEditToken();
161 $this->user
->addGroup( 'users' );
163 $data = $this->doAsyncUpload( $token, true );
164 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
165 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
166 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
168 $this->deleteFile( 'UploadFromUrlTest.png' );
175 * @depends testClearQueue
177 public function testAsyncUploadWarning( $data ) {
178 $token = $this->user
->getEditToken();
180 $this->user
->addGroup( 'users' );
182 $data = $this->doAsyncUpload( $token );
184 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
185 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
187 $data = $this->doApiRequest( array(
188 'action' => 'upload',
189 'sessionkey' => $data[0]['upload']['sessionkey'],
190 'filename' => 'UploadFromUrlTest.png',
191 'ignorewarnings' => 1,
194 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
195 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
196 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
198 $this->deleteFile( 'UploadFromUrlTest.png' );
205 * @depends testClearQueue
207 public function testSyncDownload( $data ) {
208 $token = $this->user
->getEditToken();
210 $job = JobQueueGroup
::singleton()->pop();
211 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
213 $this->user
->addGroup( 'users' );
214 $data = $this->doApiRequest( array(
215 'action' => 'upload',
216 'filename' => 'UploadFromUrlTest.png',
217 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
218 'ignorewarnings' => true,
222 $job = JobQueueGroup
::singleton()->pop();
223 $this->assertFalse( $job );
225 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
226 $this->deleteFile( 'UploadFromUrlTest.png' );
231 public function testLeaveMessage() {
232 $token = $this->user
->user
->getEditToken();
234 $talk = $this->user
->user
->getTalkPage();
235 if ( $talk->exists() ) {
236 $page = WikiPage
::factory( $talk );
237 $page->doDeleteArticle( '' );
240 $this->assertFalse( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk does not exist' );
242 $this->doApiRequest( array(
243 'action' => 'upload',
244 'filename' => 'UploadFromUrlTest.png',
245 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
246 'asyncdownload' => 1,
249 'ignorewarnings' => 1,
252 $job = JobQueueGroup
::singleton()->pop();
253 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
256 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
257 $this->assertTrue( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk exists' );
259 $this->deleteFile( 'UploadFromUrlTest.png' );
261 $talkRev = Revision
::newFromTitle( $talk );
262 $talkSize = $talkRev->getSize();
266 $this->doApiRequest( array(
267 'action' => 'upload',
268 'filename' => 'UploadFromUrlTest.png',
269 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
270 'asyncdownload' => 1,
274 } catch ( UsageException
$e ) {
276 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
278 $this->assertTrue( $exception );
280 $job = JobQueueGroup
::singleton()->pop();
281 $this->assertFalse( $job );
285 // Broken until using leavemessage with ignorewarnings is supported
288 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
290 $talkRev = Revision::newFromTitle( $talk );
291 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
296 * Helper function to perform an async upload, execute the job and fetch
299 * @return array The result of action=upload&statuskey=key
301 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
303 'action' => 'upload',
304 'filename' => 'UploadFromUrlTest.png',
305 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
306 'asyncdownload' => 1,
309 if ( $ignoreWarnings ) {
310 $params['ignorewarnings'] = 1;
312 if ( $leaveMessage ) {
313 $params['leavemessage'] = 1;
316 $data = $this->doApiRequest( $params );
317 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
318 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
319 $statusKey = $data[0]['upload']['statuskey'];
321 $job = JobQueueGroup
::singleton()->pop();
322 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
324 $status = $job->run();
325 $this->assertTrue( $status );
327 $data = $this->doApiRequest( array(
328 'action' => 'upload',
329 'statuskey' => $statusKey,
339 protected function deleteFile( $name ) {
340 $t = Title
::newFromText( $name, NS_FILE
);
341 $this->assertTrue( $t->exists(), "File '$name' exists" );
343 if ( $t->exists() ) {
344 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
346 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
347 $page = WikiPage
::factory( $t );
348 $page->doDeleteArticle( "testing" );
350 $t = Title
::newFromText( $name, NS_FILE
);
352 $this->assertFalse( $t->exists(), "File '$name' was deleted" );