8 class UploadFromUrlTest
extends ApiTestCase
{
10 protected function setUp() {
13 $this->setMwGlobals( array(
14 'wgEnableUploads' => true,
15 'wgAllowCopyUploads' => true,
16 'wgAllowAsyncCopyUploads' => true,
20 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
21 $this->deleteFile( 'UploadFromUrlTest.png' );
25 protected function doApiRequest( array $params, array $unused = null, $appendModule = false, User
$user = null ) {
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 $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' );
179 $data = $this->doAsyncUpload( $token );
181 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
182 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
184 $data = $this->doApiRequest( array(
185 'action' => 'upload',
186 'sessionkey' => $data[0]['upload']['sessionkey'],
187 'filename' => 'UploadFromUrlTest.png',
188 'ignorewarnings' => 1,
191 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
192 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
193 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
195 $this->deleteFile( 'UploadFromUrlTest.png' );
202 * @depends testClearQueue
204 public function testSyncDownload( $data ) {
205 $token = $this->user
->getEditToken();
207 $job = JobQueueGroup
::singleton()->pop();
208 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
210 $this->user
->addGroup( 'users' );
211 $data = $this->doApiRequest( array(
212 'action' => 'upload',
213 'filename' => 'UploadFromUrlTest.png',
214 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
215 'ignorewarnings' => true,
219 $job = JobQueueGroup
::singleton()->pop();
220 $this->assertFalse( $job );
222 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
223 $this->deleteFile( 'UploadFromUrlTest.png' );
228 public function testLeaveMessage() {
229 $token = $this->user
->user
->getEditToken();
231 $talk = $this->user
->user
->getTalkPage();
232 if ( $talk->exists() ) {
233 $page = WikiPage
::factory( $talk );
234 $page->doDeleteArticle( '' );
237 $this->assertFalse( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk does not exist' );
239 $data = $this->doApiRequest( array(
240 'action' => 'upload',
241 'filename' => 'UploadFromUrlTest.png',
242 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
243 'asyncdownload' => 1,
246 'ignorewarnings' => 1,
249 $job = JobQueueGroup
::singleton()->pop();
250 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
253 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
254 $this->assertTrue( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk exists' );
256 $this->deleteFile( 'UploadFromUrlTest.png' );
258 $talkRev = Revision
::newFromTitle( $talk );
259 $talkSize = $talkRev->getSize();
263 $data = $this->doApiRequest( array(
264 'action' => 'upload',
265 'filename' => 'UploadFromUrlTest.png',
266 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
267 'asyncdownload' => 1,
271 } catch ( UsageException
$e ) {
273 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
275 $this->assertTrue( $exception );
277 $job = JobQueueGroup
::singleton()->pop();
278 $this->assertFalse( $job );
283 // Broken until using leavemessage with ignorewarnings is supported
286 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
288 $talkRev = Revision::newFromTitle( $talk );
289 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
294 * Helper function to perform an async upload, execute the job and fetch
297 * @return array The result of action=upload&statuskey=key
299 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
301 'action' => 'upload',
302 'filename' => 'UploadFromUrlTest.png',
303 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
304 'asyncdownload' => 1,
307 if ( $ignoreWarnings ) {
308 $params['ignorewarnings'] = 1;
310 if ( $leaveMessage ) {
311 $params['leavemessage'] = 1;
314 $data = $this->doApiRequest( $params );
315 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
316 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
317 $statusKey = $data[0]['upload']['statuskey'];
319 $job = JobQueueGroup
::singleton()->pop();
320 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
322 $status = $job->run();
323 $this->assertTrue( $status );
325 $data = $this->doApiRequest( array(
326 'action' => 'upload',
327 'statuskey' => $statusKey,
338 protected function deleteFile( $name ) {
339 $t = Title
::newFromText( $name, NS_FILE
);
340 $this->assertTrue( $t->exists(), "File '$name' exists" );
342 if ( $t->exists() ) {
343 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
345 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
346 $page = WikiPage
::factory( $t );
347 $page->doDeleteArticle( "testing" );
349 $t = Title
::newFromText( $name, NS_FILE
);
351 $this->assertFalse( $t->exists(), "File '$name' was deleted" );