7 class UploadFromUrlTest
extends ApiTestCase
{
9 public function setUp() {
10 global $wgEnableUploads, $wgAllowCopyUploads, $wgAllowAsyncCopyUploads;
13 $wgEnableUploads = true;
14 $wgAllowCopyUploads = true;
15 $wgAllowAsyncCopyUploads = true;
18 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
19 $this->deleteFile( 'UploadFromUrlTest.png' );
23 protected function doApiRequest( $params, $unused = null, $appendModule = false, $user = null ) {
24 $sessionId = session_id();
25 session_write_close();
27 $req = new FauxRequest( $params, true, $_SESSION );
28 $module = new ApiMain( $req, true );
31 wfSetupSession( $sessionId );
32 return array( $module->getResultData(), $req );
36 * Ensure that the job queue is empty before continuing
38 public function testClearQueue() {
43 $this->assertFalse( $job );
47 * @todo Document why we test login, since the $wgUser hack used doesn't
50 public function testLogin() {
51 $data = $this->doApiRequest( array(
53 'lgname' => $this->user
->userName
,
54 'lgpassword' => $this->user
->passWord
) );
55 $this->assertArrayHasKey( "login", $data[0] );
56 $this->assertArrayHasKey( "result", $data[0]['login'] );
57 $this->assertEquals( "NeedToken", $data[0]['login']['result'] );
58 $token = $data[0]['login']['token'];
60 $data = $this->doApiRequest( array(
63 'lgname' => $this->user
->userName
,
64 'lgpassword' => $this->user
->passWord
) );
66 $this->assertArrayHasKey( "login", $data[0] );
67 $this->assertArrayHasKey( "result", $data[0]['login'] );
68 $this->assertEquals( "Success", $data[0]['login']['result'] );
69 $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] );
76 * @depends testClearQueue
78 public function testSetupUrlDownload( $data ) {
79 $token = $this->user
->getEditToken();
83 $this->doApiRequest( array(
86 } catch ( UsageException
$e ) {
88 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
90 $this->assertTrue( $exception, "Got exception" );
94 $this->doApiRequest( array(
98 } catch ( UsageException
$e ) {
100 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
103 $this->assertTrue( $exception, "Got exception" );
107 $this->doApiRequest( array(
108 'action' => 'upload',
109 'url' => 'http://www.example.com/test.png',
112 } catch ( UsageException
$e ) {
114 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
116 $this->assertTrue( $exception, "Got exception" );
118 $this->user
->removeGroup( 'sysop' );
121 $this->doApiRequest( array(
122 'action' => 'upload',
123 'url' => 'http://www.example.com/test.png',
124 'filename' => 'UploadFromUrlTest.png',
127 } catch ( UsageException
$e ) {
129 $this->assertEquals( "Permission denied", $e->getMessage() );
131 $this->assertTrue( $exception, "Got exception" );
133 $this->user
->addGroup( 'sysop' );
134 $data = $this->doApiRequest( array(
135 'action' => 'upload',
136 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
137 'asyncdownload' => 1,
138 'filename' => 'UploadFromUrlTest.png',
142 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
145 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
150 * @depends testClearQueue
152 public function testAsyncUpload( $data ) {
153 $token = $this->user
->getEditToken();
155 $this->user
->addGroup( 'users' );
157 $data = $this->doAsyncUpload( $token, true );
158 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
159 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
160 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
162 $this->deleteFile( 'UploadFromUrlTest.png' );
169 * @depends testClearQueue
171 public function testAsyncUploadWarning( $data ) {
172 $token = $this->user
->getEditToken();
174 $this->user
->addGroup( 'users' );
177 $data = $this->doAsyncUpload( $token );
179 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
180 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
182 $data = $this->doApiRequest( array(
183 'action' => 'upload',
184 'sessionkey' => $data[0]['upload']['sessionkey'],
185 'filename' => 'UploadFromUrlTest.png',
186 'ignorewarnings' => 1,
189 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
190 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
191 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
193 $this->deleteFile( 'UploadFromUrlTest.png' );
200 * @depends testClearQueue
202 public function testSyncDownload( $data ) {
203 $token = $this->user
->getEditToken();
206 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
208 $this->user
->addGroup( 'users' );
209 $data = $this->doApiRequest( array(
210 'action' => 'upload',
211 'filename' => 'UploadFromUrlTest.png',
212 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
213 'ignorewarnings' => true,
218 $this->assertFalse( $job );
220 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
221 $this->deleteFile( 'UploadFromUrlTest.png' );
226 public function testLeaveMessage() {
227 $token = $this->user
->user
->getEditToken();
229 $talk = $this->user
->user
->getTalkPage();
230 if ( $talk->exists() ) {
231 $page = WikiPage
::factory( $talk );
232 $page->doDeleteArticle( '' );
235 $this->assertFalse( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk does not exist' );
237 $data = $this->doApiRequest( array(
238 'action' => 'upload',
239 'filename' => 'UploadFromUrlTest.png',
240 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
241 'asyncdownload' => 1,
244 'ignorewarnings' => 1,
248 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
251 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
252 $this->assertTrue( (bool)$talk->getArticleID( Title
::GAID_FOR_UPDATE
), 'User talk exists' );
254 $this->deleteFile( 'UploadFromUrlTest.png' );
256 $talkRev = Revision
::newFromTitle( $talk );
257 $talkSize = $talkRev->getSize();
261 $data = $this->doApiRequest( array(
262 'action' => 'upload',
263 'filename' => 'UploadFromUrlTest.png',
264 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
265 'asyncdownload' => 1,
269 } catch ( UsageException
$e ) {
271 $this->assertEquals( 'Using leavemessage without ignorewarnings is not supported', $e->getMessage() );
273 $this->assertTrue( $exception );
276 $this->assertFalse( $job );
281 // Broken until using leavemessage with ignorewarnings is supported
284 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
286 $talkRev = Revision::newFromTitle( $talk );
287 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
292 * Helper function to perform an async upload, execute the job and fetch
295 * @return array The result of action=upload&statuskey=key
297 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
299 'action' => 'upload',
300 'filename' => 'UploadFromUrlTest.png',
301 'url' => 'http://bits.wikimedia.org/skins-1.5/common/images/poweredby_mediawiki_88x31.png',
302 'asyncdownload' => 1,
305 if ( $ignoreWarnings ) {
306 $params['ignorewarnings'] = 1;
308 if ( $leaveMessage ) {
309 $params['leavemessage'] = 1;
312 $data = $this->doApiRequest( $params );
313 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
314 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
315 $statusKey = $data[0]['upload']['statuskey'];
318 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
320 $status = $job->run();
321 $this->assertTrue( $status );
323 $data = $this->doApiRequest( array(
324 'action' => 'upload',
325 'statuskey' => $statusKey,
336 protected function deleteFile( $name ) {
337 $t = Title
::newFromText( $name, NS_FILE
);
338 $this->assertTrue($t->exists(), "File '$name' exists");
340 if ( $t->exists() ) {
341 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
343 FileDeleteForm
::doDelete( $t, $file, $empty, "none", true );
344 $page = WikiPage
::factory( $t );
345 $page->doDeleteArticle( "testing" );
347 $t = Title
::newFromText( $name, NS_FILE
);
349 $this->assertFalse($t->exists(), "File '$name' was deleted");