10 * n.b. Ensure that you can write to the images/ directory as the
11 * user that will run tests.
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... )
18 // TODO: port the other Upload tests, and other API tests to this framework
20 require_once 'ApiTestCaseUpload.php';
25 * Broken test, reports false errors from time to time.
26 * See https://bugzilla.wikimedia.org/26169
28 * This is pretty sucky... needs to be prettified.
30 class ApiUploadTest
extends ApiTestCaseUpload
{
33 * XXX this is a funny way of getting session context
35 public function testLogin() {
36 $user = self
::$users['uploader'];
40 'lgname' => $user->username
,
41 'lgpassword' => $user->password
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'];
52 'lgname' => $user->username
,
53 'lgpassword' => $user->password
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'] );
61 $this->assertNotEmpty( $session, 'API Login must return a session' );
69 public function testUploadRequiresToken( $session ) {
72 $this->doApiRequest( array(
75 } catch ( UsageException
$e ) {
77 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
79 $this->assertTrue( $exception, "Got exception" );
85 public function testUploadMissingParams( $session ) {
88 $this->doApiRequestWithToken( array(
90 ), $session, self
::$users['uploader']->user
);
91 } catch ( UsageException
$e ) {
93 $this->assertEquals( "One of the parameters filekey, file, url, statuskey is required",
96 $this->assertTrue( $exception, "Got exception" );
102 public function testUpload( $session ) {
104 $mimeType = 'image/png';
107 $randomImageGenerator = new RandomImageGenerator();
108 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
109 } catch ( Exception
$e ) {
110 $this->markTestIncomplete( $e->getMessage() );
113 $filePath = $filePaths[0];
114 $fileSize = filesize( $filePath );
115 $fileName = basename( $filePath );
117 $this->deleteFileByFileName( $fileName );
118 $this->deleteFileByContent( $filePath );
120 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
121 $this->markTestIncomplete( "Couldn't upload file!\n" );
125 'action' => 'upload',
126 'filename' => $fileName,
127 'file' => 'dummy content',
128 'comment' => 'dummy comment',
129 'text' => "This is the page text for $fileName",
134 list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
135 self
::$users['uploader']->user
);
136 } catch ( UsageException
$e ) {
139 $this->assertTrue( isset( $result['upload'] ) );
140 $this->assertEquals( 'Success', $result['upload']['result'] );
141 $this->assertEquals( $fileSize, (int)$result['upload']['imageinfo']['size'] );
142 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
143 $this->assertFalse( $exception );
146 $this->deleteFileByFilename( $fileName );
153 public function testUploadZeroLength( $session ) {
154 $mimeType = 'image/png';
156 $filePath = tempnam( wfTempDir(), "" );
157 $fileName = "apiTestUploadZeroLength.png";
159 $this->deleteFileByFileName( $fileName );
161 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
162 $this->markTestIncomplete( "Couldn't upload file!\n" );
166 'action' => 'upload',
167 'filename' => $fileName,
168 'file' => 'dummy content',
169 'comment' => 'dummy comment',
170 'text' => "This is the page text for $fileName",
175 $this->doApiRequestWithToken( $params, $session, self
::$users['uploader']->user
);
176 } catch ( UsageException
$e ) {
177 $this->assertContains( 'The file you submitted was empty', $e->getMessage() );
180 $this->assertTrue( $exception );
183 $this->deleteFileByFilename( $fileName );
190 public function testUploadSameFileName( $session ) {
192 $mimeType = 'image/png';
195 $randomImageGenerator = new RandomImageGenerator();
196 $filePaths = $randomImageGenerator->writeImages( 2, $extension, wfTempDir() );
197 } catch ( Exception
$e ) {
198 $this->markTestIncomplete( $e->getMessage() );
201 // we'll reuse this filename
202 $fileName = basename( $filePaths[0] );
204 // clear any other files with the same name
205 $this->deleteFileByFileName( $fileName );
207 // we reuse these params
209 'action' => 'upload',
210 'filename' => $fileName,
211 'file' => 'dummy content',
212 'comment' => 'dummy comment',
213 'text' => "This is the page text for $fileName",
216 // first upload .... should succeed
218 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[0] ) ) {
219 $this->markTestIncomplete( "Couldn't upload file!\n" );
224 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
225 self
::$users['uploader']->user
);
226 } catch ( UsageException
$e ) {
229 $this->assertTrue( isset( $result['upload'] ) );
230 $this->assertEquals( 'Success', $result['upload']['result'] );
231 $this->assertFalse( $exception );
233 // second upload with the same name (but different content)
235 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[1] ) ) {
236 $this->markTestIncomplete( "Couldn't upload file!\n" );
241 list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
242 self
::$users['uploader']->user
); // FIXME: leaks a temporary file
243 } catch ( UsageException
$e ) {
246 $this->assertTrue( isset( $result['upload'] ) );
247 $this->assertEquals( 'Warning', $result['upload']['result'] );
248 $this->assertTrue( isset( $result['upload']['warnings'] ) );
249 $this->assertTrue( isset( $result['upload']['warnings']['exists'] ) );
250 $this->assertFalse( $exception );
253 $this->deleteFileByFilename( $fileName );
254 unlink( $filePaths[0] );
255 unlink( $filePaths[1] );
261 public function testUploadSameContent( $session ) {
263 $mimeType = 'image/png';
266 $randomImageGenerator = new RandomImageGenerator();
267 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
268 } catch ( Exception
$e ) {
269 $this->markTestIncomplete( $e->getMessage() );
272 $fileNames[0] = basename( $filePaths[0] );
273 $fileNames[1] = "SameContentAs" . $fileNames[0];
275 // clear any other files with the same name or content
276 $this->deleteFileByContent( $filePaths[0] );
277 $this->deleteFileByFileName( $fileNames[0] );
278 $this->deleteFileByFileName( $fileNames[1] );
280 // first upload .... should succeed
283 'action' => 'upload',
284 'filename' => $fileNames[0],
285 'file' => 'dummy content',
286 'comment' => 'dummy comment',
287 'text' => "This is the page text for " . $fileNames[0],
290 if ( !$this->fakeUploadFile( 'file', $fileNames[0], $mimeType, $filePaths[0] ) ) {
291 $this->markTestIncomplete( "Couldn't upload file!\n" );
296 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
297 self
::$users['uploader']->user
);
298 } catch ( UsageException
$e ) {
301 $this->assertTrue( isset( $result['upload'] ) );
302 $this->assertEquals( 'Success', $result['upload']['result'] );
303 $this->assertFalse( $exception );
305 // second upload with the same content (but different name)
307 if ( !$this->fakeUploadFile( 'file', $fileNames[1], $mimeType, $filePaths[0] ) ) {
308 $this->markTestIncomplete( "Couldn't upload file!\n" );
312 'action' => 'upload',
313 'filename' => $fileNames[1],
314 'file' => 'dummy content',
315 'comment' => 'dummy comment',
316 'text' => "This is the page text for " . $fileNames[1],
321 list( $result ) = $this->doApiRequestWithToken( $params, $session,
322 self
::$users['uploader']->user
); // FIXME: leaks a temporary file
323 } catch ( UsageException
$e ) {
326 $this->assertTrue( isset( $result['upload'] ) );
327 $this->assertEquals( 'Warning', $result['upload']['result'] );
328 $this->assertTrue( isset( $result['upload']['warnings'] ) );
329 $this->assertTrue( isset( $result['upload']['warnings']['duplicate'] ) );
330 $this->assertFalse( $exception );
333 $this->deleteFileByFilename( $fileNames[0] );
334 $this->deleteFileByFilename( $fileNames[1] );
335 unlink( $filePaths[0] );
341 public function testUploadStash( $session ) {
342 $this->setMwGlobals( array(
343 'wgUser' => self
::$users['uploader']->user
, // @todo FIXME: still used somewhere
347 $mimeType = 'image/png';
350 $randomImageGenerator = new RandomImageGenerator();
351 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
352 } catch ( Exception
$e ) {
353 $this->markTestIncomplete( $e->getMessage() );
356 $filePath = $filePaths[0];
357 $fileSize = filesize( $filePath );
358 $fileName = basename( $filePath );
360 $this->deleteFileByFileName( $fileName );
361 $this->deleteFileByContent( $filePath );
363 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
364 $this->markTestIncomplete( "Couldn't upload file!\n" );
368 'action' => 'upload',
370 'filename' => $fileName,
371 'file' => 'dummy content',
372 'comment' => 'dummy comment',
373 'text' => "This is the page text for $fileName",
378 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
379 self
::$users['uploader']->user
); // FIXME: leaks a temporary file
380 } catch ( UsageException
$e ) {
383 $this->assertFalse( $exception );
384 $this->assertTrue( isset( $result['upload'] ) );
385 $this->assertEquals( 'Success', $result['upload']['result'] );
386 $this->assertEquals( $fileSize, (int)$result['upload']['imageinfo']['size'] );
387 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
388 $this->assertTrue( isset( $result['upload']['filekey'] ) );
389 $this->assertEquals( $result['upload']['sessionkey'], $result['upload']['filekey'] );
390 $filekey = $result['upload']['filekey'];
392 // it should be visible from Special:UploadStash
393 // XXX ...but how to test this, with a fake WebRequest with the session?
395 // now we should try to release the file from stash
397 'action' => 'upload',
398 'filekey' => $filekey,
399 'filename' => $fileName,
400 'comment' => 'dummy comment',
401 'text' => "This is the page text for $fileName, altered",
404 $this->clearFakeUploads();
407 list( $result ) = $this->doApiRequestWithToken( $params, $session,
408 self
::$users['uploader']->user
);
409 } catch ( UsageException
$e ) {
412 $this->assertTrue( isset( $result['upload'] ) );
413 $this->assertEquals( 'Success', $result['upload']['result'] );
414 $this->assertFalse( $exception, "No UsageException exception." );
417 $this->deleteFileByFilename( $fileName );
424 public function testUploadChunks( $session ) {
425 $this->setMwGlobals( array(
426 'wgUser' => self
::$users['uploader']->user
, // @todo FIXME: still used somewhere
429 $chunkSize = 1048576;
430 // Download a large image file
431 // ( using RandomImageGenerator for large files is not stable )
432 $mimeType = 'image/jpeg';
433 $url = 'http://upload.wikimedia.org/wikipedia/commons/e/ed/Oberaargletscher_from_Oberaar%2C_2010_07.JPG';
434 $filePath = wfTempDir() . '/Oberaargletscher_from_Oberaar.jpg';
436 // Only download if the file is not avaliable in the temp location:
437 if ( !is_file( $filePath ) ) {
438 copy( $url, $filePath );
440 } catch ( Exception
$e ) {
441 $this->markTestIncomplete( $e->getMessage() );
444 $fileSize = filesize( $filePath );
445 $fileName = basename( $filePath );
447 $this->deleteFileByFileName( $fileName );
448 $this->deleteFileByContent( $filePath );
450 // Base upload params:
452 'action' => 'upload',
454 'filename' => $fileName,
455 'filesize' => $fileSize,
460 $chunkSessionKey = false;
463 $handle = @fopen
( $filePath, "r" );
464 if ( $handle === false ) {
465 $this->markTestIncomplete( "could not open file: $filePath" );
467 while ( !feof( $handle ) ) {
468 // Get the current chunk
469 $chunkData = @fread
( $handle, $chunkSize );
471 // Upload the current chunk into the $_FILE object:
472 $this->fakeUploadChunk( 'chunk', 'blob', $mimeType, $chunkData );
474 // Check for chunkSessionKey
475 if ( !$chunkSessionKey ) {
476 // Upload fist chunk ( and get the session key )
478 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
479 self
::$users['uploader']->user
);
480 } catch ( UsageException
$e ) {
481 $this->markTestIncomplete( $e->getMessage() );
483 // Make sure we got a valid chunk continue:
484 $this->assertTrue( isset( $result['upload'] ) );
485 $this->assertTrue( isset( $result['upload']['filekey'] ) );
486 // If we don't get a session key mark test incomplete.
487 if ( !isset( $result['upload']['filekey'] ) ) {
488 $this->markTestIncomplete( "no filekey provided" );
490 $chunkSessionKey = $result['upload']['filekey'];
491 $this->assertEquals( 'Continue', $result['upload']['result'] );
492 // First chunk should have chunkSize == offset
493 $this->assertEquals( $chunkSize, $result['upload']['offset'] );
494 $resultOffset = $result['upload']['offset'];
497 // Filekey set to chunk session
498 $params['filekey'] = $chunkSessionKey;
499 // Update the offset ( always add chunkSize for subquent chunks should be in-sync with $result['upload']['offset'] )
500 $params['offset'] +
= $chunkSize;
501 // Make sure param offset is insync with resultOffset:
502 $this->assertEquals( $resultOffset, $params['offset'] );
503 // Upload current chunk
505 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
506 self
::$users['uploader']->user
);
507 } catch ( UsageException
$e ) {
508 $this->markTestIncomplete( $e->getMessage() );
510 // Make sure we got a valid chunk continue:
511 $this->assertTrue( isset( $result['upload'] ) );
512 $this->assertTrue( isset( $result['upload']['filekey'] ) );
514 // Check if we were on the last chunk:
515 if ( $params['offset'] +
$chunkSize >= $fileSize ) {
516 $this->assertEquals( 'Success', $result['upload']['result'] );
519 $this->assertEquals( 'Continue', $result['upload']['result'] );
520 // update $resultOffset
521 $resultOffset = $result['upload']['offset'];
526 // Check that we got a valid file result:
527 wfDebug( __METHOD__
. " hohoh filesize {$fileSize} info {$result['upload']['imageinfo']['size']}\n\n" );
528 $this->assertEquals( $fileSize, $result['upload']['imageinfo']['size'] );
529 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
530 $this->assertTrue( isset( $result['upload']['filekey'] ) );
531 $filekey = $result['upload']['filekey'];
533 // Now we should try to release the file from stash
535 'action' => 'upload',
536 'filekey' => $filekey,
537 'filename' => $fileName,
538 'comment' => 'dummy comment',
539 'text' => "This is the page text for $fileName, altered",
541 $this->clearFakeUploads();
544 list( $result ) = $this->doApiRequestWithToken( $params, $session,
545 self
::$users['uploader']->user
);
546 } catch ( UsageException
$e ) {
549 $this->assertTrue( isset( $result['upload'] ) );
550 $this->assertEquals( 'Success', $result['upload']['result'] );
551 $this->assertFalse( $exception );
554 $this->deleteFileByFilename( $fileName );
555 // don't remove downloaded temporary file for fast subquent tests.
556 //unlink( $filePath );