397c100bc0dbc82af8d6d49861957d97ce6f27b9
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 * @depends testClearQueue
53 public function testSetupUrlDownload( $data ) {
54 $token = $this->user
->getEditToken();
58 $this->doApiRequest( array(
61 } catch ( UsageException
$e ) {
63 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
65 $this->assertTrue( $exception, "Got exception" );
69 $this->doApiRequest( array(
73 } catch ( UsageException
$e ) {
75 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
78 $this->assertTrue( $exception, "Got exception" );
82 $this->doApiRequest( array(
84 'url' => 'http://www.example.com/test.png',
87 } catch ( UsageException
$e ) {
89 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
91 $this->assertTrue( $exception, "Got exception" );
93 $this->user
->removeGroup( 'sysop' );
96 $this->doApiRequest( array(
98 'url' => 'http://www.example.com/test.png',
99 'filename' => 'UploadFromUrlTest.png',
102 } catch ( UsageException
$e ) {
104 $this->assertEquals( "Permission denied", $e->getMessage() );
106 $this->assertTrue( $exception, "Got exception" );
108 $this->user
->addGroup( 'sysop' );
109 $data = $this->doApiRequest( array(
110 'action' => 'upload',
111 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
112 'asyncdownload' => 1,
113 'filename' => 'UploadFromUrlTest.png',
117 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
119 $job = JobQueueGroup
::singleton()->pop();
120 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
124 * @depends testClearQueue
126 public function testAsyncUpload( $data ) {
127 $token = $this->user
->getEditToken();
129 $this->user
->addGroup( 'users' );
131 $data = $this->doAsyncUpload( $token, true );
132 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
133 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
134 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
136 $this->deleteFile( 'UploadFromUrlTest.png' );
142 * @depends testClearQueue
144 public function testAsyncUploadWarning( $data ) {
145 $token = $this->user
->getEditToken();
147 $this->user
->addGroup( 'users' );
149 $data = $this->doAsyncUpload( $token );
151 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
152 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
154 $data = $this->doApiRequest( array(
155 'action' => 'upload',
156 'sessionkey' => $data[0]['upload']['sessionkey'],
157 'filename' => 'UploadFromUrlTest.png',
158 'ignorewarnings' => 1,
161 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
162 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
163 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
165 $this->deleteFile( 'UploadFromUrlTest.png' );
171 * @depends testClearQueue
173 public function testSyncDownload( $data ) {
174 $token = $this->user
->getEditToken();
176 $job = JobQueueGroup
::singleton()->pop();
177 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
179 $this->user
->addGroup( 'users' );
180 $data = $this->doApiRequest( array(
181 'action' => 'upload',
182 'filename' => 'UploadFromUrlTest.png',
183 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
184 'ignorewarnings' => true,
188 $job = JobQueueGroup
::singleton()->pop();
189 $this->assertFalse( $job );
191 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
192 $this->deleteFile( 'UploadFromUrlTest.png' );
197 public function testLeaveMessage() {
198 $token = $this->user
->user
->getEditToken();
200 $talk = $this->user
->user
->getTalkPage();
201 if ( $talk->exists() ) {
202 $page = WikiPage
::factory( $talk );
203 $page->doDeleteArticle( '' );
206 $this->assertFalse( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk does not exist' );
208 $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 'asyncdownload' => 1,
215 'ignorewarnings' => 1,
218 $job = JobQueueGroup
::singleton()->pop();
219 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
222 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
223 $this->assertTrue( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk exists' );
225 $this->deleteFile( 'UploadFromUrlTest.png' );
227 $talkRev = Revision
::newFromTitle( $talk );
228 $talkSize = $talkRev->getSize();
232 $this->doApiRequest( array(
233 'action' => 'upload',
234 'filename' => 'UploadFromUrlTest.png',
235 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
236 'asyncdownload' => 1,
240 } catch ( UsageException
$e ) {
242 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
244 $this->assertTrue( $exception );
246 $job = JobQueueGroup
::singleton()->pop();
247 $this->assertFalse( $job );
251 // Broken until using leavemessage with ignorewarnings is supported
254 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
256 $talkRev = Revision::newFromTitle( $talk );
257 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
262 * Helper function to perform an async upload, execute the job and fetch
265 * @return array The result of action=upload&statuskey=key
267 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
269 'action' => 'upload',
270 'filename' => 'UploadFromUrlTest.png',
271 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
272 'asyncdownload' => 1,
275 if ( $ignoreWarnings ) {
276 $params['ignorewarnings'] = 1;
278 if ( $leaveMessage ) {
279 $params['leavemessage'] = 1;
282 $data = $this->doApiRequest( $params );
283 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
284 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
285 $statusKey = $data[0]['upload']['statuskey'];
287 $job = JobQueueGroup
::singleton()->pop();
288 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
290 $status = $job->run();
291 $this->assertTrue( $status );
293 $data = $this->doApiRequest( array(
294 'action' => 'upload',
295 'statuskey' => $statusKey,
302 protected function deleteFile( $name ) {
303 $t = Title
::newFromText( $name, NS_FILE
);
304 $this->assertTrue( $t->exists(), "File '$name' exists" );
306 if ( $t->exists() ) {
307 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
309 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
310 $page = WikiPage
::factory( $t );
311 $page->doDeleteArticle( "testing" );
313 $t = Title
::newFromText( $name, NS_FILE
);
315 $this->assertFalse( $t->exists(), "File '$name' was deleted" );