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