Merge "Use 'pagetitle' in content language"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiUploadTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 */
8
9 /**
10 * n.b. Ensure that you can write to the images/ directory as the
11 * user that will run tests.
12 */
13
14 // Note for reviewers: this intentionally duplicates functionality already in "ApiSetup" and so on.
15 // This framework works better IMO and has less strangeness (such as test cases inheriting from "ApiSetup"...)
16 // (and in the case of the other Upload tests, this flat out just actually works... )
17
18 // TODO: port the other Upload tests, and other API tests to this framework
19
20 require_once 'ApiTestCaseUpload.php';
21
22 /**
23 * @group Database
24 * @group Broken
25 * Broken test, reports false errors from time to time.
26 * See https://bugzilla.wikimedia.org/26169
27 *
28 * This is pretty sucky... needs to be prettified.
29 */
30 class ApiUploadTest extends ApiTestCaseUpload {
31 /**
32 * Testing login
33 * XXX this is a funny way of getting session context
34 */
35 public function testLogin() {
36 $user = self::$users['uploader'];
37
38 $params = array(
39 'action' => 'login',
40 'lgname' => $user->username,
41 'lgpassword' => $user->password
42 );
43 list( $result, , $session ) = $this->doApiRequest( $params );
44 $this->assertArrayHasKey( "login", $result );
45 $this->assertArrayHasKey( "result", $result['login'] );
46 $this->assertEquals( "NeedToken", $result['login']['result'] );
47 $token = $result['login']['token'];
48
49 $params = array(
50 'action' => 'login',
51 'lgtoken' => $token,
52 'lgname' => $user->username,
53 'lgpassword' => $user->password
54 );
55 list( $result, , $session ) = $this->doApiRequest( $params, $session );
56 $this->assertArrayHasKey( "login", $result );
57 $this->assertArrayHasKey( "result", $result['login'] );
58 $this->assertEquals( "Success", $result['login']['result'] );
59 $this->assertArrayHasKey( 'lgtoken', $result['login'] );
60
61 $this->assertNotEmpty( $session, 'API Login must return a session' );
62
63 return $session;
64 }
65
66 /**
67 * @depends testLogin
68 */
69 public function testUploadRequiresToken( $session ) {
70 $exception = false;
71 try {
72 $this->doApiRequest( array(
73 'action' => 'upload'
74 ) );
75 } catch ( UsageException $e ) {
76 $exception = true;
77 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
78 }
79 $this->assertTrue( $exception, "Got exception" );
80 }
81
82 /**
83 * @depends testLogin
84 */
85 public function testUploadMissingParams( $session ) {
86 $exception = false;
87 try {
88 $this->doApiRequestWithToken( array(
89 'action' => 'upload',
90 ), $session, self::$users['uploader']->user );
91 } catch ( UsageException $e ) {
92 $exception = true;
93 $this->assertEquals( "One of the parameters filekey, file, url, statuskey is required",
94 $e->getMessage() );
95 }
96 $this->assertTrue( $exception, "Got exception" );
97 }
98
99 /**
100 * @depends testLogin
101 */
102 public function testUpload( $session ) {
103 $extension = 'png';
104 $mimeType = 'image/png';
105
106 try {
107 $randomImageGenerator = new RandomImageGenerator();
108 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
109 } catch ( Exception $e ) {
110 $this->markTestIncomplete( $e->getMessage() );
111 }
112
113 /** @var array $filePaths */
114 $filePath = $filePaths[0];
115 $fileSize = filesize( $filePath );
116 $fileName = basename( $filePath );
117
118 $this->deleteFileByFileName( $fileName );
119 $this->deleteFileByContent( $filePath );
120
121 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
122 $this->markTestIncomplete( "Couldn't upload file!\n" );
123 }
124
125 $params = array(
126 'action' => 'upload',
127 'filename' => $fileName,
128 'file' => 'dummy content',
129 'comment' => 'dummy comment',
130 'text' => "This is the page text for $fileName",
131 );
132
133 $exception = false;
134 try {
135 list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
136 self::$users['uploader']->user );
137 } catch ( UsageException $e ) {
138 $exception = true;
139 }
140 $this->assertTrue( isset( $result['upload'] ) );
141 $this->assertEquals( 'Success', $result['upload']['result'] );
142 $this->assertEquals( $fileSize, (int)$result['upload']['imageinfo']['size'] );
143 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
144 $this->assertFalse( $exception );
145
146 // clean up
147 $this->deleteFileByFilename( $fileName );
148 unlink( $filePath );
149 }
150
151 /**
152 * @depends testLogin
153 */
154 public function testUploadZeroLength( $session ) {
155 $mimeType = 'image/png';
156
157 $filePath = tempnam( wfTempDir(), "" );
158 $fileName = "apiTestUploadZeroLength.png";
159
160 $this->deleteFileByFileName( $fileName );
161
162 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
163 $this->markTestIncomplete( "Couldn't upload file!\n" );
164 }
165
166 $params = array(
167 'action' => 'upload',
168 'filename' => $fileName,
169 'file' => 'dummy content',
170 'comment' => 'dummy comment',
171 'text' => "This is the page text for $fileName",
172 );
173
174 $exception = false;
175 try {
176 $this->doApiRequestWithToken( $params, $session, self::$users['uploader']->user );
177 } catch ( UsageException $e ) {
178 $this->assertContains( 'The file you submitted was empty', $e->getMessage() );
179 $exception = true;
180 }
181 $this->assertTrue( $exception );
182
183 // clean up
184 $this->deleteFileByFilename( $fileName );
185 unlink( $filePath );
186 }
187
188 /**
189 * @depends testLogin
190 */
191 public function testUploadSameFileName( $session ) {
192 $extension = 'png';
193 $mimeType = 'image/png';
194
195 try {
196 $randomImageGenerator = new RandomImageGenerator();
197 $filePaths = $randomImageGenerator->writeImages( 2, $extension, wfTempDir() );
198 } catch ( Exception $e ) {
199 $this->markTestIncomplete( $e->getMessage() );
200 }
201
202 // we'll reuse this filename
203 /** @var array $filePaths */
204 $fileName = basename( $filePaths[0] );
205
206 // clear any other files with the same name
207 $this->deleteFileByFileName( $fileName );
208
209 // we reuse these params
210 $params = array(
211 'action' => 'upload',
212 'filename' => $fileName,
213 'file' => 'dummy content',
214 'comment' => 'dummy comment',
215 'text' => "This is the page text for $fileName",
216 );
217
218 // first upload .... should succeed
219
220 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[0] ) ) {
221 $this->markTestIncomplete( "Couldn't upload file!\n" );
222 }
223
224 $exception = false;
225 try {
226 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
227 self::$users['uploader']->user );
228 } catch ( UsageException $e ) {
229 $exception = true;
230 }
231 $this->assertTrue( isset( $result['upload'] ) );
232 $this->assertEquals( 'Success', $result['upload']['result'] );
233 $this->assertFalse( $exception );
234
235 // second upload with the same name (but different content)
236
237 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[1] ) ) {
238 $this->markTestIncomplete( "Couldn't upload file!\n" );
239 }
240
241 $exception = false;
242 try {
243 list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
244 self::$users['uploader']->user ); // FIXME: leaks a temporary file
245 } catch ( UsageException $e ) {
246 $exception = true;
247 }
248 $this->assertTrue( isset( $result['upload'] ) );
249 $this->assertEquals( 'Warning', $result['upload']['result'] );
250 $this->assertTrue( isset( $result['upload']['warnings'] ) );
251 $this->assertTrue( isset( $result['upload']['warnings']['exists'] ) );
252 $this->assertFalse( $exception );
253
254 // clean up
255 $this->deleteFileByFilename( $fileName );
256 unlink( $filePaths[0] );
257 unlink( $filePaths[1] );
258 }
259
260 /**
261 * @depends testLogin
262 */
263 public function testUploadSameContent( $session ) {
264 $extension = 'png';
265 $mimeType = 'image/png';
266
267 try {
268 $randomImageGenerator = new RandomImageGenerator();
269 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
270 } catch ( Exception $e ) {
271 $this->markTestIncomplete( $e->getMessage() );
272 }
273
274 /** @var array $filePaths */
275 $fileNames[0] = basename( $filePaths[0] );
276 $fileNames[1] = "SameContentAs" . $fileNames[0];
277
278 // clear any other files with the same name or content
279 $this->deleteFileByContent( $filePaths[0] );
280 $this->deleteFileByFileName( $fileNames[0] );
281 $this->deleteFileByFileName( $fileNames[1] );
282
283 // first upload .... should succeed
284
285 $params = array(
286 'action' => 'upload',
287 'filename' => $fileNames[0],
288 'file' => 'dummy content',
289 'comment' => 'dummy comment',
290 'text' => "This is the page text for " . $fileNames[0],
291 );
292
293 if ( !$this->fakeUploadFile( 'file', $fileNames[0], $mimeType, $filePaths[0] ) ) {
294 $this->markTestIncomplete( "Couldn't upload file!\n" );
295 }
296
297 $exception = false;
298 try {
299 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
300 self::$users['uploader']->user );
301 } catch ( UsageException $e ) {
302 $exception = true;
303 }
304 $this->assertTrue( isset( $result['upload'] ) );
305 $this->assertEquals( 'Success', $result['upload']['result'] );
306 $this->assertFalse( $exception );
307
308 // second upload with the same content (but different name)
309
310 if ( !$this->fakeUploadFile( 'file', $fileNames[1], $mimeType, $filePaths[0] ) ) {
311 $this->markTestIncomplete( "Couldn't upload file!\n" );
312 }
313
314 $params = array(
315 'action' => 'upload',
316 'filename' => $fileNames[1],
317 'file' => 'dummy content',
318 'comment' => 'dummy comment',
319 'text' => "This is the page text for " . $fileNames[1],
320 );
321
322 $exception = false;
323 try {
324 list( $result ) = $this->doApiRequestWithToken( $params, $session,
325 self::$users['uploader']->user ); // FIXME: leaks a temporary file
326 } catch ( UsageException $e ) {
327 $exception = true;
328 }
329 $this->assertTrue( isset( $result['upload'] ) );
330 $this->assertEquals( 'Warning', $result['upload']['result'] );
331 $this->assertTrue( isset( $result['upload']['warnings'] ) );
332 $this->assertTrue( isset( $result['upload']['warnings']['duplicate'] ) );
333 $this->assertFalse( $exception );
334
335 // clean up
336 $this->deleteFileByFilename( $fileNames[0] );
337 $this->deleteFileByFilename( $fileNames[1] );
338 unlink( $filePaths[0] );
339 }
340
341 /**
342 * @depends testLogin
343 */
344 public function testUploadStash( $session ) {
345 $this->setMwGlobals( array(
346 'wgUser' => self::$users['uploader']->user, // @todo FIXME: still used somewhere
347 ) );
348
349 $extension = 'png';
350 $mimeType = 'image/png';
351
352 try {
353 $randomImageGenerator = new RandomImageGenerator();
354 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
355 } catch ( Exception $e ) {
356 $this->markTestIncomplete( $e->getMessage() );
357 }
358
359 /** @var array $filePaths */
360 $filePath = $filePaths[0];
361 $fileSize = filesize( $filePath );
362 $fileName = basename( $filePath );
363
364 $this->deleteFileByFileName( $fileName );
365 $this->deleteFileByContent( $filePath );
366
367 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
368 $this->markTestIncomplete( "Couldn't upload file!\n" );
369 }
370
371 $params = array(
372 'action' => 'upload',
373 'stash' => 1,
374 'filename' => $fileName,
375 'file' => 'dummy content',
376 'comment' => 'dummy comment',
377 'text' => "This is the page text for $fileName",
378 );
379
380 $exception = false;
381 try {
382 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
383 self::$users['uploader']->user ); // FIXME: leaks a temporary file
384 } catch ( UsageException $e ) {
385 $exception = true;
386 }
387 $this->assertFalse( $exception );
388 $this->assertTrue( isset( $result['upload'] ) );
389 $this->assertEquals( 'Success', $result['upload']['result'] );
390 $this->assertEquals( $fileSize, (int)$result['upload']['imageinfo']['size'] );
391 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
392 $this->assertTrue( isset( $result['upload']['filekey'] ) );
393 $this->assertEquals( $result['upload']['sessionkey'], $result['upload']['filekey'] );
394 $filekey = $result['upload']['filekey'];
395
396 // it should be visible from Special:UploadStash
397 // XXX ...but how to test this, with a fake WebRequest with the session?
398
399 // now we should try to release the file from stash
400 $params = array(
401 'action' => 'upload',
402 'filekey' => $filekey,
403 'filename' => $fileName,
404 'comment' => 'dummy comment',
405 'text' => "This is the page text for $fileName, altered",
406 );
407
408 $this->clearFakeUploads();
409 $exception = false;
410 try {
411 list( $result ) = $this->doApiRequestWithToken( $params, $session,
412 self::$users['uploader']->user );
413 } catch ( UsageException $e ) {
414 $exception = true;
415 }
416 $this->assertTrue( isset( $result['upload'] ) );
417 $this->assertEquals( 'Success', $result['upload']['result'] );
418 $this->assertFalse( $exception, "No UsageException exception." );
419
420 // clean up
421 $this->deleteFileByFilename( $fileName );
422 unlink( $filePath );
423 }
424
425 /**
426 * @depends testLogin
427 */
428 public function testUploadChunks( $session ) {
429 $this->setMwGlobals( array(
430 'wgUser' => self::$users['uploader']->user, // @todo FIXME: still used somewhere
431 ) );
432
433 $chunkSize = 1048576;
434 // Download a large image file
435 // ( using RandomImageGenerator for large files is not stable )
436 $mimeType = 'image/jpeg';
437 $url = 'http://upload.wikimedia.org/wikipedia/commons/e/ed/Oberaargletscher_from_Oberaar%2C_2010_07.JPG';
438 $filePath = wfTempDir() . '/Oberaargletscher_from_Oberaar.jpg';
439 try {
440 // Only download if the file is not avaliable in the temp location:
441 if ( !is_file( $filePath ) ) {
442 copy( $url, $filePath );
443 }
444 } catch ( Exception $e ) {
445 $this->markTestIncomplete( $e->getMessage() );
446 }
447
448 $fileSize = filesize( $filePath );
449 $fileName = basename( $filePath );
450
451 $this->deleteFileByFileName( $fileName );
452 $this->deleteFileByContent( $filePath );
453
454 // Base upload params:
455 $params = array(
456 'action' => 'upload',
457 'stash' => 1,
458 'filename' => $fileName,
459 'filesize' => $fileSize,
460 'offset' => 0,
461 );
462
463 // Upload chunks
464 $chunkSessionKey = false;
465 $resultOffset = 0;
466 // Open the file:
467 $handle = @fopen( $filePath, "r" );
468 if ( $handle === false ) {
469 $this->markTestIncomplete( "could not open file: $filePath" );
470 }
471 while ( !feof( $handle ) ) {
472 // Get the current chunk
473 $chunkData = @fread( $handle, $chunkSize );
474
475 // Upload the current chunk into the $_FILE object:
476 $this->fakeUploadChunk( 'chunk', 'blob', $mimeType, $chunkData );
477
478 // Check for chunkSessionKey
479 if ( !$chunkSessionKey ) {
480 // Upload fist chunk ( and get the session key )
481 try {
482 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
483 self::$users['uploader']->user );
484 } catch ( UsageException $e ) {
485 $this->markTestIncomplete( $e->getMessage() );
486 }
487 // Make sure we got a valid chunk continue:
488 $this->assertTrue( isset( $result['upload'] ) );
489 $this->assertTrue( isset( $result['upload']['filekey'] ) );
490 // If we don't get a session key mark test incomplete.
491 if ( !isset( $result['upload']['filekey'] ) ) {
492 $this->markTestIncomplete( "no filekey provided" );
493 }
494 $chunkSessionKey = $result['upload']['filekey'];
495 $this->assertEquals( 'Continue', $result['upload']['result'] );
496 // First chunk should have chunkSize == offset
497 $this->assertEquals( $chunkSize, $result['upload']['offset'] );
498 $resultOffset = $result['upload']['offset'];
499 continue;
500 }
501 // Filekey set to chunk session
502 $params['filekey'] = $chunkSessionKey;
503 // Update the offset ( always add chunkSize for subquent chunks should be in-sync with $result['upload']['offset'] )
504 $params['offset'] += $chunkSize;
505 // Make sure param offset is insync with resultOffset:
506 $this->assertEquals( $resultOffset, $params['offset'] );
507 // Upload current chunk
508 try {
509 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
510 self::$users['uploader']->user );
511 } catch ( UsageException $e ) {
512 $this->markTestIncomplete( $e->getMessage() );
513 }
514 // Make sure we got a valid chunk continue:
515 $this->assertTrue( isset( $result['upload'] ) );
516 $this->assertTrue( isset( $result['upload']['filekey'] ) );
517
518 // Check if we were on the last chunk:
519 if ( $params['offset'] + $chunkSize >= $fileSize ) {
520 $this->assertEquals( 'Success', $result['upload']['result'] );
521 break;
522 } else {
523 $this->assertEquals( 'Continue', $result['upload']['result'] );
524 // update $resultOffset
525 $resultOffset = $result['upload']['offset'];
526 }
527 }
528 fclose( $handle );
529
530 // Check that we got a valid file result:
531 wfDebug( __METHOD__ . " hohoh filesize {$fileSize} info {$result['upload']['imageinfo']['size']}\n\n" );
532 $this->assertEquals( $fileSize, $result['upload']['imageinfo']['size'] );
533 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
534 $this->assertTrue( isset( $result['upload']['filekey'] ) );
535 $filekey = $result['upload']['filekey'];
536
537 // Now we should try to release the file from stash
538 $params = array(
539 'action' => 'upload',
540 'filekey' => $filekey,
541 'filename' => $fileName,
542 'comment' => 'dummy comment',
543 'text' => "This is the page text for $fileName, altered",
544 );
545 $this->clearFakeUploads();
546 $exception = false;
547 try {
548 list( $result ) = $this->doApiRequestWithToken( $params, $session,
549 self::$users['uploader']->user );
550 } catch ( UsageException $e ) {
551 $exception = true;
552 }
553 $this->assertTrue( isset( $result['upload'] ) );
554 $this->assertEquals( 'Success', $result['upload']['result'] );
555 $this->assertFalse( $exception );
556
557 // clean up
558 $this->deleteFileByFilename( $fileName );
559 // don't remove downloaded temporary file for fast subquent tests.
560 //unlink( $filePath );
561 }
562 }