8 class UploadFromUrlTest
extends ApiTestCase
{
10 protected function setUp() {
11 global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads;
14 $wgEnableUploads = true;
15 $wgAllowCopyUploads = true;
16 $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 );
33 return array( $module->getResultData(), $req );
37 * Ensure that the job queue is empty before continuing
39 public function testClearQueue() {
40 $job = JobQueueGroup
::singleton()->pop();
42 $job = JobQueueGroup
::singleton()->pop();
44 $this->assertFalse( $job );
48 * @todo Document why we test login, since the $wgUser hack used doesn't
51 public function testLogin() {
52 $data = $this->doApiRequest( array(
54 'lgname' => $this->user
->userName
,
55 'lgpassword' => $this->user
->passWord
) );
56 $this->assertArrayHasKey( "login", $data[0] );
57 $this->assertArrayHasKey( "result", $data[0]['login'] );
58 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
59 $token = $data[0]['login']['token'];
61 $data = $this->doApiRequest( array(
64 'lgname' => $this->user
->userName
,
65 'lgpassword' => $this->user
->passWord
) );
67 $this->assertArrayHasKey( "login", $data[0] );
68 $this->assertArrayHasKey( "result", $data[0]['login'] );
69 $this->assertEquals( "Success", $data[0]['login']['result'] );
70 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
77 * @depends testClearQueue
79 public function testSetupUrlDownload( $data ) {
80 $token = $this->user
->getEditToken();
84 $this->doApiRequest( array(
87 } catch ( UsageException
$e ) {
89 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
91 $this->assertTrue( $exception, "Got exception" );
95 $this->doApiRequest( array(
99 } catch ( UsageException
$e ) {
101 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
104 $this->assertTrue( $exception, "Got exception" );
108 $this->doApiRequest( array(
109 'action' => 'upload',
110 'url' => 'http://www.example.com/test.png',
113 } catch ( UsageException
$e ) {
115 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
117 $this->assertTrue( $exception, "Got exception" );
119 $this->user
->removeGroup( 'sysop' );
122 $this->doApiRequest( array(
123 'action' => 'upload',
124 'url' => 'http://www.example.com/test.png',
125 'filename' => 'UploadFromUrlTest.png',
128 } catch ( UsageException
$e ) {
130 $this->assertEquals( "Permission denied", $e->getMessage() );
132 $this->assertTrue( $exception, "Got exception" );
134 $this->user
->addGroup( 'sysop' );
135 $data = $this->doApiRequest( array(
136 'action' => 'upload',
137 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
138 'asyncdownload' => 1,
139 'filename' => 'UploadFromUrlTest.png',
143 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
145 $job = JobQueueGroup
::singleton()->pop();
146 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
151 * @depends testClearQueue
153 public function testAsyncUpload( $data ) {
154 $token = $this->user
->getEditToken();
156 $this->user
->addGroup( 'users' );
158 $data = $this->doAsyncUpload( $token, true );
159 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
160 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
161 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
163 $this->deleteFile( 'UploadFromUrlTest.png' );
170 * @depends testClearQueue
172 public function testAsyncUploadWarning( $data ) {
173 $token = $this->user
->getEditToken();
175 $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 $data = $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 $data = $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 );
282 // Broken until using leavemessage with ignorewarnings is supported
285 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
287 $talkRev = Revision::newFromTitle( $talk );
288 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
293 * Helper function to perform an async upload, execute the job and fetch
296 * @return array The result of action=upload&statuskey=key
298 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
300 'action' => 'upload',
301 'filename' => 'UploadFromUrlTest.png',
302 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
303 'asyncdownload' => 1,
306 if ( $ignoreWarnings ) {
307 $params['ignorewarnings'] = 1;
309 if ( $leaveMessage ) {
310 $params['leavemessage'] = 1;
313 $data = $this->doApiRequest( $params );
314 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
315 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
316 $statusKey = $data[0]['upload']['statuskey'];
318 $job = JobQueueGroup
::singleton()->pop();
319 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
321 $status = $job->run();
322 $this->assertTrue( $status );
324 $data = $this->doApiRequest( array(
325 'action' => 'upload',
326 'statuskey' => $statusKey,
337 protected function deleteFile( $name ) {
338 $t = Title
::newFromText( $name, NS_FILE
);
339 $this->assertTrue($t->exists(), "File '$name' exists");
341 if ( $t->exists() ) {
342 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
344 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
345 $page = WikiPage
::factory( $t );
346 $page->doDeleteArticle( "testing" );
348 $t = Title
::newFromText( $name, NS_FILE
);
350 $this->assertFalse($t->exists(), "File '$name' was deleted");