Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / upload / UploadFromUrlTest.php
blobec56b63e5ce9dcd0daa7e62ef87a182ab2cf4aa1
1 <?php
3 /**
4 * @group Broken
5 * @group Upload
6 * @group Database
8 * @covers UploadFromUrl
9 */
10 class UploadFromUrlTest extends ApiTestCase {
11 protected function setUp() {
12 parent::setUp();
14 $this->setMwGlobals( array(
15 'wgEnableUploads' => true,
16 'wgAllowCopyUploads' => true,
17 'wgAllowAsyncCopyUploads' => true,
18 ) );
19 wfSetupSession();
21 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
22 $this->deleteFile( 'UploadFromUrlTest.png' );
26 protected function doApiRequest( array $params, array $unused = null,
27 $appendModule = false, User $user = null
28 ) {
29 $sessionId = session_id();
30 session_write_close();
32 $req = new FauxRequest( $params, true, $_SESSION );
33 $module = new ApiMain( $req, true );
34 $module->execute();
36 wfSetupSession( $sessionId );
38 return array( $module->getResultData(), $req );
41 /**
42 * Ensure that the job queue is empty before continuing
44 public function testClearQueue() {
45 $job = JobQueueGroup::singleton()->pop();
46 while ( $job ) {
47 $job = JobQueueGroup::singleton()->pop();
49 $this->assertFalse( $job );
52 /**
53 * @depends testClearQueue
55 public function testSetupUrlDownload( $data ) {
56 $token = $this->user->getEditToken();
57 $exception = false;
59 try {
60 $this->doApiRequest( array(
61 'action' => 'upload',
62 ) );
63 } catch ( UsageException $e ) {
64 $exception = true;
65 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
67 $this->assertTrue( $exception, "Got exception" );
69 $exception = false;
70 try {
71 $this->doApiRequest( array(
72 'action' => 'upload',
73 'token' => $token,
74 ), $data );
75 } catch ( UsageException $e ) {
76 $exception = true;
77 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
78 $e->getMessage() );
80 $this->assertTrue( $exception, "Got exception" );
82 $exception = false;
83 try {
84 $this->doApiRequest( array(
85 'action' => 'upload',
86 'url' => 'http://www.example.com/test.png',
87 'token' => $token,
88 ), $data );
89 } catch ( UsageException $e ) {
90 $exception = true;
91 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
93 $this->assertTrue( $exception, "Got exception" );
95 $this->user->removeGroup( 'sysop' );
96 $exception = false;
97 try {
98 $this->doApiRequest( array(
99 'action' => 'upload',
100 'url' => 'http://www.example.com/test.png',
101 'filename' => 'UploadFromUrlTest.png',
102 'token' => $token,
103 ), $data );
104 } catch ( UsageException $e ) {
105 $exception = true;
106 $this->assertEquals( "Permission denied", $e->getMessage() );
108 $this->assertTrue( $exception, "Got exception" );
110 $this->user->addGroup( 'sysop' );
111 $data = $this->doApiRequest( array(
112 'action' => 'upload',
113 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
114 'asyncdownload' => 1,
115 'filename' => 'UploadFromUrlTest.png',
116 'token' => $token,
117 ), $data );
119 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
121 $job = JobQueueGroup::singleton()->pop();
122 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
126 * @depends testClearQueue
128 public function testAsyncUpload( $data ) {
129 $token = $this->user->getEditToken();
131 $this->user->addGroup( 'users' );
133 $data = $this->doAsyncUpload( $token, true );
134 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
135 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
136 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
138 $this->deleteFile( 'UploadFromUrlTest.png' );
140 return $data;
144 * @depends testClearQueue
146 public function testAsyncUploadWarning( $data ) {
147 $token = $this->user->getEditToken();
149 $this->user->addGroup( 'users' );
151 $data = $this->doAsyncUpload( $token );
153 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
154 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
156 $data = $this->doApiRequest( array(
157 'action' => 'upload',
158 'sessionkey' => $data[0]['upload']['sessionkey'],
159 'filename' => 'UploadFromUrlTest.png',
160 'ignorewarnings' => 1,
161 'token' => $token,
162 ) );
163 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
164 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
165 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
167 $this->deleteFile( 'UploadFromUrlTest.png' );
169 return $data;
173 * @depends testClearQueue
175 public function testSyncDownload( $data ) {
176 $token = $this->user->getEditToken();
178 $job = JobQueueGroup::singleton()->pop();
179 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
181 $this->user->addGroup( 'users' );
182 $data = $this->doApiRequest( array(
183 'action' => 'upload',
184 'filename' => 'UploadFromUrlTest.png',
185 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
186 'ignorewarnings' => true,
187 'token' => $token,
188 ), $data );
190 $job = JobQueueGroup::singleton()->pop();
191 $this->assertFalse( $job );
193 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
194 $this->deleteFile( 'UploadFromUrlTest.png' );
196 return $data;
199 public function testLeaveMessage() {
200 $token = $this->user->user->getEditToken();
202 $talk = $this->user->user->getTalkPage();
203 if ( $talk->exists() ) {
204 $page = WikiPage::factory( $talk );
205 $page->doDeleteArticle( '' );
208 $this->assertFalse(
209 (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ),
210 'User talk does not exist'
213 $this->doApiRequest( array(
214 'action' => 'upload',
215 'filename' => 'UploadFromUrlTest.png',
216 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
217 'asyncdownload' => 1,
218 'token' => $token,
219 'leavemessage' => 1,
220 'ignorewarnings' => 1,
221 ) );
223 $job = JobQueueGroup::singleton()->pop();
224 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
225 $job->run();
227 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
228 $this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' );
230 $this->deleteFile( 'UploadFromUrlTest.png' );
232 $exception = false;
233 try {
234 $this->doApiRequest( array(
235 'action' => 'upload',
236 'filename' => 'UploadFromUrlTest.png',
237 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
238 'asyncdownload' => 1,
239 'token' => $token,
240 'leavemessage' => 1,
241 ) );
242 } catch ( UsageException $e ) {
243 $exception = true;
244 $this->assertEquals(
245 'Using leavemessage without ignorewarnings is not supported',
246 $e->getMessage()
249 $this->assertTrue( $exception );
251 $job = JobQueueGroup::singleton()->pop();
252 $this->assertFalse( $job );
254 return;
256 // Broken until using leavemessage with ignorewarnings is supported
257 $talkRev = Revision::newFromTitle( $talk );
258 $talkSize = $talkRev->getSize();
260 $job->run();
262 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
264 $talkRev = Revision::newFromTitle( $talk );
265 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
270 * Helper function to perform an async upload, execute the job and fetch
271 * the status
273 * @param string $token
274 * @param bool $ignoreWarnings
275 * @param bool $leaveMessage
276 * @return array The result of action=upload&statuskey=key
278 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
279 $params = array(
280 'action' => 'upload',
281 'filename' => 'UploadFromUrlTest.png',
282 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
283 'asyncdownload' => 1,
284 'token' => $token,
286 if ( $ignoreWarnings ) {
287 $params['ignorewarnings'] = 1;
289 if ( $leaveMessage ) {
290 $params['leavemessage'] = 1;
293 $data = $this->doApiRequest( $params );
294 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
295 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
296 $statusKey = $data[0]['upload']['statuskey'];
298 $job = JobQueueGroup::singleton()->pop();
299 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
301 $status = $job->run();
302 $this->assertTrue( $status );
304 $data = $this->doApiRequest( array(
305 'action' => 'upload',
306 'statuskey' => $statusKey,
307 'token' => $token,
308 ) );
310 return $data;
313 protected function deleteFile( $name ) {
314 $t = Title::newFromText( $name, NS_FILE );
315 $this->assertTrue( $t->exists(), "File '$name' exists" );
317 if ( $t->exists() ) {
318 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
319 $empty = "";
320 FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
321 $page = WikiPage::factory( $t );
322 $page->doDeleteArticle( "testing" );
324 $t = Title::newFromText( $name, NS_FILE );
326 $this->assertFalse( $t->exists(), "File '$name' was deleted" );