Merge "mw.Upload.BookletLayout: Don't explode when the API call fails with 'exception'"
[lhc/web/wiklou.git] / tests / phpunit / includes / upload / UploadFromUrlTest.php
1 <?php
2
3 /**
4 * @group Broken
5 * @group Upload
6 * @group Database
7 *
8 * @covers UploadFromUrl
9 */
10 class UploadFromUrlTest extends ApiTestCase {
11 protected function setUp() {
12 parent::setUp();
13
14 $this->setMwGlobals( array(
15 'wgEnableUploads' => true,
16 'wgAllowCopyUploads' => true,
17 'wgAllowAsyncCopyUploads' => true,
18 ) );
19
20 if ( wfLocalFile( 'UploadFromUrlTest.png' )->exists() ) {
21 $this->deleteFile( 'UploadFromUrlTest.png' );
22 }
23 }
24
25 protected function doApiRequest( array $params, array $unused = null,
26 $appendModule = false, User $user = null
27 ) {
28 global $wgRequest;
29
30 $req = new FauxRequest( $params, true, $wgRequest->getSession() );
31 $module = new ApiMain( $req, true );
32 $module->execute();
33
34 return array(
35 $module->getResult()->getResultData( null, array( 'Strip' => 'all' ) ),
36 $req
37 );
38 }
39
40 /**
41 * Ensure that the job queue is empty before continuing
42 */
43 public function testClearQueue() {
44 $job = JobQueueGroup::singleton()->pop();
45 while ( $job ) {
46 $job = JobQueueGroup::singleton()->pop();
47 }
48 $this->assertFalse( $job );
49 }
50
51 /**
52 * @depends testClearQueue
53 */
54 public function testSetupUrlDownload( $data ) {
55 $token = $this->user->getEditToken();
56 $exception = false;
57
58 try {
59 $this->doApiRequest( array(
60 'action' => 'upload',
61 ) );
62 } catch ( UsageException $e ) {
63 $exception = true;
64 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
65 }
66 $this->assertTrue( $exception, "Got exception" );
67
68 $exception = false;
69 try {
70 $this->doApiRequest( array(
71 'action' => 'upload',
72 'token' => $token,
73 ), $data );
74 } catch ( UsageException $e ) {
75 $exception = true;
76 $this->assertEquals( "One of the parameters sessionkey, file, url, statuskey is required",
77 $e->getMessage() );
78 }
79 $this->assertTrue( $exception, "Got exception" );
80
81 $exception = false;
82 try {
83 $this->doApiRequest( array(
84 'action' => 'upload',
85 'url' => 'http://www.example.com/test.png',
86 'token' => $token,
87 ), $data );
88 } catch ( UsageException $e ) {
89 $exception = true;
90 $this->assertEquals( "The filename parameter must be set", $e->getMessage() );
91 }
92 $this->assertTrue( $exception, "Got exception" );
93
94 $this->user->removeGroup( 'sysop' );
95 $exception = false;
96 try {
97 $this->doApiRequest( array(
98 'action' => 'upload',
99 'url' => 'http://www.example.com/test.png',
100 'filename' => 'UploadFromUrlTest.png',
101 'token' => $token,
102 ), $data );
103 } catch ( UsageException $e ) {
104 $exception = true;
105 $this->assertEquals( "Permission denied", $e->getMessage() );
106 }
107 $this->assertTrue( $exception, "Got exception" );
108
109 $this->user->addGroup( 'sysop' );
110 $data = $this->doApiRequest( array(
111 'action' => 'upload',
112 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
113 'asyncdownload' => 1,
114 'filename' => 'UploadFromUrlTest.png',
115 'token' => $token,
116 ), $data );
117
118 $this->assertEquals( $data[0]['upload']['result'], 'Queued', 'Queued upload' );
119
120 $job = JobQueueGroup::singleton()->pop();
121 $this->assertThat( $job, $this->isInstanceOf( 'UploadFromUrlJob' ), 'Queued upload inserted' );
122 }
123
124 /**
125 * @depends testClearQueue
126 */
127 public function testAsyncUpload( $data ) {
128 $token = $this->user->getEditToken();
129
130 $this->user->addGroup( 'users' );
131
132 $data = $this->doAsyncUpload( $token, true );
133 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
134 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
135 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
136
137 $this->deleteFile( 'UploadFromUrlTest.png' );
138
139 return $data;
140 }
141
142 /**
143 * @depends testClearQueue
144 */
145 public function testAsyncUploadWarning( $data ) {
146 $token = $this->user->getEditToken();
147
148 $this->user->addGroup( 'users' );
149
150 $data = $this->doAsyncUpload( $token );
151
152 $this->assertEquals( $data[0]['upload']['result'], 'Warning' );
153 $this->assertTrue( isset( $data[0]['upload']['sessionkey'] ) );
154
155 $data = $this->doApiRequest( array(
156 'action' => 'upload',
157 'sessionkey' => $data[0]['upload']['sessionkey'],
158 'filename' => 'UploadFromUrlTest.png',
159 'ignorewarnings' => 1,
160 'token' => $token,
161 ) );
162 $this->assertEquals( $data[0]['upload']['result'], 'Success' );
163 $this->assertEquals( $data[0]['upload']['filename'], 'UploadFromUrlTest.png' );
164 $this->assertTrue( wfLocalFile( $data[0]['upload']['filename'] )->exists() );
165
166 $this->deleteFile( 'UploadFromUrlTest.png' );
167
168 return $data;
169 }
170
171 /**
172 * @depends testClearQueue
173 */
174 public function testSyncDownload( $data ) {
175 $token = $this->user->getEditToken();
176
177 $job = JobQueueGroup::singleton()->pop();
178 $this->assertFalse( $job, 'Starting with an empty jobqueue' );
179
180 $this->user->addGroup( 'users' );
181 $data = $this->doApiRequest( array(
182 'action' => 'upload',
183 'filename' => 'UploadFromUrlTest.png',
184 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
185 'ignorewarnings' => true,
186 'token' => $token,
187 ), $data );
188
189 $job = JobQueueGroup::singleton()->pop();
190 $this->assertFalse( $job );
191
192 $this->assertEquals( 'Success', $data[0]['upload']['result'] );
193 $this->deleteFile( 'UploadFromUrlTest.png' );
194
195 return $data;
196 }
197
198 public function testLeaveMessage() {
199 $token = $this->user->user->getEditToken();
200
201 $talk = $this->user->user->getTalkPage();
202 if ( $talk->exists() ) {
203 $page = WikiPage::factory( $talk );
204 $page->doDeleteArticle( '' );
205 }
206
207 $this->assertFalse(
208 (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ),
209 'User talk does not exist'
210 );
211
212 $this->doApiRequest( array(
213 'action' => 'upload',
214 'filename' => 'UploadFromUrlTest.png',
215 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
216 'asyncdownload' => 1,
217 'token' => $token,
218 'leavemessage' => 1,
219 'ignorewarnings' => 1,
220 ) );
221
222 $job = JobQueueGroup::singleton()->pop();
223 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
224 $job->run();
225
226 $this->assertTrue( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
227 $this->assertTrue( (bool)$talk->getArticleID( Title::GAID_FOR_UPDATE ), 'User talk exists' );
228
229 $this->deleteFile( 'UploadFromUrlTest.png' );
230
231 $exception = false;
232 try {
233 $this->doApiRequest( array(
234 'action' => 'upload',
235 'filename' => 'UploadFromUrlTest.png',
236 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
237 'asyncdownload' => 1,
238 'token' => $token,
239 'leavemessage' => 1,
240 ) );
241 } catch ( UsageException $e ) {
242 $exception = true;
243 $this->assertEquals(
244 'Using leavemessage without ignorewarnings is not supported',
245 $e->getMessage()
246 );
247 }
248 $this->assertTrue( $exception );
249
250 $job = JobQueueGroup::singleton()->pop();
251 $this->assertFalse( $job );
252
253 return;
254 /*
255 // Broken until using leavemessage with ignorewarnings is supported
256 $talkRev = Revision::newFromTitle( $talk );
257 $talkSize = $talkRev->getSize();
258
259 $job->run();
260
261 $this->assertFalse( wfLocalFile( 'UploadFromUrlTest.png' )->exists() );
262
263 $talkRev = Revision::newFromTitle( $talk );
264 $this->assertTrue( $talkRev->getSize() > $talkSize, 'New message left' );
265 */
266 }
267
268 /**
269 * Helper function to perform an async upload, execute the job and fetch
270 * the status
271 *
272 * @param string $token
273 * @param bool $ignoreWarnings
274 * @param bool $leaveMessage
275 * @return array The result of action=upload&statuskey=key
276 */
277 private function doAsyncUpload( $token, $ignoreWarnings = false, $leaveMessage = false ) {
278 $params = array(
279 'action' => 'upload',
280 'filename' => 'UploadFromUrlTest.png',
281 'url' => 'http://upload.wikimedia.org/wikipedia/mediawiki/b/bc/Wiki.png',
282 'asyncdownload' => 1,
283 'token' => $token,
284 );
285 if ( $ignoreWarnings ) {
286 $params['ignorewarnings'] = 1;
287 }
288 if ( $leaveMessage ) {
289 $params['leavemessage'] = 1;
290 }
291
292 $data = $this->doApiRequest( $params );
293 $this->assertEquals( $data[0]['upload']['result'], 'Queued' );
294 $this->assertTrue( isset( $data[0]['upload']['statuskey'] ) );
295 $statusKey = $data[0]['upload']['statuskey'];
296
297 $job = JobQueueGroup::singleton()->pop();
298 $this->assertEquals( 'UploadFromUrlJob', get_class( $job ) );
299
300 $status = $job->run();
301 $this->assertTrue( $status );
302
303 $data = $this->doApiRequest( array(
304 'action' => 'upload',
305 'statuskey' => $statusKey,
306 'token' => $token,
307 ) );
308
309 return $data;
310 }
311
312 protected function deleteFile( $name ) {
313 $t = Title::newFromText( $name, NS_FILE );
314 $this->assertTrue( $t->exists(), "File '$name' exists" );
315
316 if ( $t->exists() ) {
317 $file = wfFindFile( $name, array( 'ignoreRedirect' => true ) );
318 $empty = "";
319 FileDeleteForm::doDelete( $t, $file, $empty, "none", true );
320 $page = WikiPage::factory( $t );
321 $page->doDeleteArticle( "testing" );
322 }
323 $t = Title::newFromText( $name, NS_FILE );
324
325 $this->assertFalse( $t->exists(), "File '$name' was deleted" );
326 }
327 }