3 require_once dirname( __FILE__
) . '/api/ApiSetup.php';
9 class UploadFromUrlTest
extends ApiTestSetup
{
11 public function setUp() {
12 global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads;
15 $wgEnableUploads = true;
16 $wgAllowCopyUploads = true;
17 $wgAllowAsyncCopyUploads = true;
20 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
21 $this->deleteFile( 'UploadFromUrlTest.png' );
25 protected function doApiRequest( $params, $unused = null, $appendModule = false ) {
26 $sessionId = session_id();
27 session_write_close();
29 $req = new FauxRequest( $params, true, $_SESSION );
30 $module = new ApiMain( $req, true );
33 wfSetupSession( $sessionId );
34 return array( $module->getResultData(), $req );
38 * Ensure that the job queue is empty before continuing
40 public function testClearQueue() {
41 while ( $job = Job
::pop() ) { }
42 $this->assertFalse( $job );
46 * @todo Document why we test login, since the $wgUser hack used doesn't
49 public function testLogin() {
50 $data = $this->doApiRequest( array(
52 'lgname' => $this->user
->userName
,
53 'lgpassword' => $this->user
->passWord
) );
54 $this->assertArrayHasKey( "login", $data[0] );
55 $this->assertArrayHasKey( "result", $data[0]['login'] );
56 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
57 $token = $data[0]['login']['token'];
59 $data = $this->doApiRequest( array(
62 'lgname' => $this->user
->userName
,
63 'lgpassword' => $this->user
->passWord
) );
65 $this->assertArrayHasKey( "login", $data[0] );
66 $this->assertArrayHasKey( "result", $data[0]['login'] );
67 $this->assertEquals( "Success", $data[0]['login']['result'] );
68 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
75 * @depends testClearQueue
77 public function testSetupUrlDownload( $data ) {
78 $token = $this->user
->editToken();
82 $this->doApiRequest( array(
85 } catch ( UsageException
$e ) {
87 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
89 $this->assertTrue( $exception, "Got exception" );
93 $this->doApiRequest( array(
97 } catch ( UsageException
$e ) {
99 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
102 $this->assertTrue( $exception, "Got exception" );
106 $this->doApiRequest( array(
107 'action' => 'upload',
108 'url' => 'http://www.example.com/test.png',
111 } catch ( UsageException
$e ) {
113 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
115 $this->assertTrue( $exception, "Got exception" );
117 $this->user
->removeGroup( 'sysop' );
120 $this->doApiRequest( array(
121 'action' => 'upload',
122 'url' => 'http://www.example.com/test.png',
123 'filename' => 'UploadFromUrlTest.png',
126 } catch ( UsageException
$e ) {
128 $this->assertEquals( "Permission denied", $e->getMessage() );
130 $this->assertTrue( $exception, "Got exception" );
132 $this->user
->addGroup( 'sysop' );
133 $data = $this->doApiRequest( array(
134 'action' => 'upload',
135 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
136 'asyncdownload' => 1,
137 'filename' => 'UploadFromUrlTest.png',
141 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
144 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
149 * @depends testClearQueue
151 public function testAsyncUpload( $data ) {
152 $token = $this->user
->editToken();
154 $this->user
->addGroup( 'users' );
156 $data = $this->doAsyncUpload( $token, true );
157 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
158 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
159 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
161 $this->deleteFile( 'UploadFromUrlTest.png' );
168 * @depends testClearQueue
170 public function testAsyncUploadWarning( $data ) {
171 $token = $this->user
->editToken();
173 $this->user
->addGroup( 'users' );
176 $data = $this->doAsyncUpload( $token );
178 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
179 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
181 $data = $this->doApiRequest( array(
182 'action' => 'upload',
183 'sessionkey' => $data[0]['upload']['sessionkey'],
184 'filename' => 'UploadFromUrlTest.png',
185 'ignorewarnings' => 1,
188 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
189 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
190 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
192 $this->deleteFile( 'UploadFromUrlTest.png' );
199 * @depends testClearQueue
201 public function testSyncDownload( $data ) {
202 $token = $this->user
->editToken();
205 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
207 $this->user
->addGroup( 'users' );
208 $data = $this->doApiRequest( array(
209 'action' => 'upload',
210 'filename' => 'UploadFromUrlTest.png',
211 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
212 'ignorewarnings' => true,
217 $this->assertFalse( $job );
219 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
220 $this->deleteFile( 'UploadFromUrlTest.png' );
225 public function testLeaveMessage() {
226 $token = $this->user
->user
->editToken();
228 $talk = $this->user
->user
->getTalkPage();
229 if ( $talk->exists() ) {
230 $a = new Article( $talk );
231 $a->doDeleteArticle( '' );
234 $this->assertFalse( (bool)$talk->getArticleId( Title
::GAID_FOR_UPDATE
), 'User talk does not exist' );
236 $data = $this->doApiRequest( array(
237 'action' => 'upload',
238 'filename' => 'UploadFromUrlTest.png',
239 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
240 'asyncdownload' => 1,
243 'ignorewarnings' => 1,
247 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
250 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
251 $this->assertTrue( (bool)$talk->getArticleId( Title
::GAID_FOR_UPDATE
), 'User talk exists' );
253 $this->deleteFile( 'UploadFromUrlTest.png' );
255 $talkRev = Revision
::newFromTitle( $talk );
256 $talkSize = $talkRev->getSize();
260 $data = $this->doApiRequest( array(
261 'action' => 'upload',
262 'filename' => 'UploadFromUrlTest.png',
263 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
264 'asyncdownload' => 1,
268 } catch ( UsageException
$e ) {
270 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
272 $this->assertTrue( $exception );
275 $this->assertFalse( $job );
280 // Broken until using leavemessage with ignorewarnings is supported
283 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
285 $talkRev = Revision::newFromTitle( $talk );
286 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
291 * Helper function to perform an async upload, execute the job and fetch
294 * @return array The result of action=upload&statuskey=key
296 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
298 'action' => 'upload',
299 'filename' => 'UploadFromUrlTest.png',
300 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
301 'asyncdownload' => 1,
304 if ( $ignoreWarnings ) {
305 $params['ignorewarnings'] = 1;
307 if ( $leaveMessage ) {
308 $params['leavemessage'] = 1;
311 $data = $this->doApiRequest( $params );
312 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
313 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
314 $statusKey = $data[0]['upload']['statuskey'];
317 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
319 $status = $job->run();
320 $this->assertTrue( $status );
322 $data = $this->doApiRequest( array(
323 'action' => 'upload',
324 '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 $a = new Article ( $t );
344 $a->doDeleteArticle( "testing" );
346 $t = Title
::newFromText( $name, NS_FILE
);
348 $this->assertFalse($t->exists(), "File '$name' was deleted");