9 * n.b. Ensure that you can write to the images/ directory as the
10 * user that will run tests.
13 // Note for reviewers: this intentionally duplicates functionality already in "ApiSetup" and so on.
14 // This framework works better IMO and has less strangeness (such as test cases inheriting from "ApiSetup"...)
15 // (and in the case of the other Upload tests, this flat out just actually works... )
17 // TODO: port the other Upload tests, and other API tests to this framework
19 require_once 'ApiTestCaseUpload.php';
24 * Broken test, reports false errors from time to time.
25 * See https://bugzilla.wikimedia.org/26169
27 * This is pretty sucky... needs to be prettified.
29 class ApiUploadTest
extends ApiTestCaseUpload
{
32 * XXX this is a funny way of getting session context
34 public function testLogin() {
35 $user = self
::$users['uploader'];
39 'lgname' => $user->username
,
40 'lgpassword' => $user->password
42 list( $result, , $session ) = $this->doApiRequest( $params );
43 $this->assertArrayHasKey( "login", $result );
44 $this->assertArrayHasKey( "result", $result['login'] );
45 $this->assertEquals( "NeedToken", $result['login']['result'] );
46 $token = $result['login']['token'];
51 'lgname' => $user->username
,
52 'lgpassword' => $user->password
54 list( $result, , $session ) = $this->doApiRequest( $params, $session );
55 $this->assertArrayHasKey( "login", $result );
56 $this->assertArrayHasKey( "result", $result['login'] );
57 $this->assertEquals( "Success", $result['login']['result'] );
58 $this->assertArrayHasKey( 'lgtoken', $result['login'] );
60 $this->assertNotEmpty( $session, 'API Login must return a session' );
68 public function testUploadRequiresToken( $session ) {
71 $this->doApiRequest( array(
74 } catch ( UsageException
$e ) {
76 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
78 $this->assertTrue( $exception, "Got exception" );
84 public function testUploadMissingParams( $session ) {
87 $this->doApiRequestWithToken( array(
89 ), $session, self
::$users['uploader']->user
);
90 } catch ( UsageException
$e ) {
92 $this->assertEquals( "One of the parameters filekey, file, url, statuskey is required",
95 $this->assertTrue( $exception, "Got exception" );
101 public function testUpload( $session ) {
103 $mimeType = 'image/png';
106 $randomImageGenerator = new RandomImageGenerator();
107 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
108 } catch ( Exception
$e ) {
109 $this->markTestIncomplete( $e->getMessage() );
112 $filePath = $filePaths[0];
113 $fileSize = filesize( $filePath );
114 $fileName = basename( $filePath );
116 $this->deleteFileByFileName( $fileName );
117 $this->deleteFileByContent( $filePath );
119 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
120 $this->markTestIncomplete( "Couldn't upload file!\n" );
124 'action' => 'upload',
125 'filename' => $fileName,
126 'file' => 'dummy content',
127 'comment' => 'dummy comment',
128 'text' => "This is the page text for $fileName",
133 list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
134 self
::$users['uploader']->user
);
135 } catch ( UsageException
$e ) {
138 $this->assertTrue( isset( $result['upload'] ) );
139 $this->assertEquals( 'Success', $result['upload']['result'] );
140 $this->assertEquals( $fileSize, (int)$result['upload']['imageinfo']['size'] );
141 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
142 $this->assertFalse( $exception );
145 $this->deleteFileByFilename( $fileName );
152 public function testUploadZeroLength( $session ) {
153 $mimeType = 'image/png';
155 $filePath = tempnam( wfTempDir(), "" );
156 $fileName = "apiTestUploadZeroLength.png";
158 $this->deleteFileByFileName( $fileName );
160 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
161 $this->markTestIncomplete( "Couldn't upload file!\n" );
165 'action' => 'upload',
166 'filename' => $fileName,
167 'file' => 'dummy content',
168 'comment' => 'dummy comment',
169 'text' => "This is the page text for $fileName",
174 $this->doApiRequestWithToken( $params, $session, self
::$users['uploader']->user
);
175 } catch ( UsageException
$e ) {
176 $this->assertContains( 'The file you submitted was empty', $e->getMessage() );
179 $this->assertTrue( $exception );
182 $this->deleteFileByFilename( $fileName );
189 public function testUploadSameFileName( $session ) {
191 $mimeType = 'image/png';
194 $randomImageGenerator = new RandomImageGenerator();
195 $filePaths = $randomImageGenerator->writeImages( 2, $extension, wfTempDir() );
196 } catch ( Exception
$e ) {
197 $this->markTestIncomplete( $e->getMessage() );
200 // we'll reuse this filename
201 $fileName = basename( $filePaths[0] );
203 // clear any other files with the same name
204 $this->deleteFileByFileName( $fileName );
206 // we reuse these params
208 'action' => 'upload',
209 'filename' => $fileName,
210 'file' => 'dummy content',
211 'comment' => 'dummy comment',
212 'text' => "This is the page text for $fileName",
215 // first upload .... should succeed
217 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[0] ) ) {
218 $this->markTestIncomplete( "Couldn't upload file!\n" );
223 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
224 self
::$users['uploader']->user
);
225 } catch ( UsageException
$e ) {
228 $this->assertTrue( isset( $result['upload'] ) );
229 $this->assertEquals( 'Success', $result['upload']['result'] );
230 $this->assertFalse( $exception );
232 // second upload with the same name (but different content)
234 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[1] ) ) {
235 $this->markTestIncomplete( "Couldn't upload file!\n" );
240 list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
241 self
::$users['uploader']->user
); // FIXME: leaks a temporary file
242 } catch ( UsageException
$e ) {
245 $this->assertTrue( isset( $result['upload'] ) );
246 $this->assertEquals( 'Warning', $result['upload']['result'] );
247 $this->assertTrue( isset( $result['upload']['warnings'] ) );
248 $this->assertTrue( isset( $result['upload']['warnings']['exists'] ) );
249 $this->assertFalse( $exception );
252 $this->deleteFileByFilename( $fileName );
253 unlink( $filePaths[0] );
254 unlink( $filePaths[1] );
260 public function testUploadSameContent( $session ) {
262 $mimeType = 'image/png';
265 $randomImageGenerator = new RandomImageGenerator();
266 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
267 } catch ( Exception
$e ) {
268 $this->markTestIncomplete( $e->getMessage() );
271 $fileNames[0] = basename( $filePaths[0] );
272 $fileNames[1] = "SameContentAs" . $fileNames[0];
274 // clear any other files with the same name or content
275 $this->deleteFileByContent( $filePaths[0] );
276 $this->deleteFileByFileName( $fileNames[0] );
277 $this->deleteFileByFileName( $fileNames[1] );
279 // first upload .... should succeed
282 'action' => 'upload',
283 'filename' => $fileNames[0],
284 'file' => 'dummy content',
285 'comment' => 'dummy comment',
286 'text' => "This is the page text for " . $fileNames[0],
289 if ( !$this->fakeUploadFile( 'file', $fileNames[0], $mimeType, $filePaths[0] ) ) {
290 $this->markTestIncomplete( "Couldn't upload file!\n" );
295 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
296 self
::$users['uploader']->user
);
297 } catch ( UsageException
$e ) {
300 $this->assertTrue( isset( $result['upload'] ) );
301 $this->assertEquals( 'Success', $result['upload']['result'] );
302 $this->assertFalse( $exception );
304 // second upload with the same content (but different name)
306 if ( !$this->fakeUploadFile( 'file', $fileNames[1], $mimeType, $filePaths[0] ) ) {
307 $this->markTestIncomplete( "Couldn't upload file!\n" );
311 'action' => 'upload',
312 'filename' => $fileNames[1],
313 'file' => 'dummy content',
314 'comment' => 'dummy comment',
315 'text' => "This is the page text for " . $fileNames[1],
320 list( $result ) = $this->doApiRequestWithToken( $params, $session,
321 self
::$users['uploader']->user
); // FIXME: leaks a temporary file
322 } catch ( UsageException
$e ) {
325 $this->assertTrue( isset( $result['upload'] ) );
326 $this->assertEquals( 'Warning', $result['upload']['result'] );
327 $this->assertTrue( isset( $result['upload']['warnings'] ) );
328 $this->assertTrue( isset( $result['upload']['warnings']['duplicate'] ) );
329 $this->assertFalse( $exception );
332 $this->deleteFileByFilename( $fileNames[0] );
333 $this->deleteFileByFilename( $fileNames[1] );
334 unlink( $filePaths[0] );
340 public function testUploadStash( $session ) {
341 $this->setMwGlobals( array(
342 'wgUser' => self
::$users['uploader']->user
, // @todo FIXME: still used somewhere
346 $mimeType = 'image/png';
349 $randomImageGenerator = new RandomImageGenerator();
350 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
351 } catch ( Exception
$e ) {
352 $this->markTestIncomplete( $e->getMessage() );
355 $filePath = $filePaths[0];
356 $fileSize = filesize( $filePath );
357 $fileName = basename( $filePath );
359 $this->deleteFileByFileName( $fileName );
360 $this->deleteFileByContent( $filePath );
362 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
363 $this->markTestIncomplete( "Couldn't upload file!\n" );
367 'action' => 'upload',
369 'filename' => $fileName,
370 'file' => 'dummy content',
371 'comment' => 'dummy comment',
372 'text' => "This is the page text for $fileName",
377 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
378 self
::$users['uploader']->user
); // FIXME: leaks a temporary file
379 } catch ( UsageException
$e ) {
382 $this->assertFalse( $exception );
383 $this->assertTrue( isset( $result['upload'] ) );
384 $this->assertEquals( 'Success', $result['upload']['result'] );
385 $this->assertEquals( $fileSize, (int)$result['upload']['imageinfo']['size'] );
386 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
387 $this->assertTrue( isset( $result['upload']['filekey'] ) );
388 $this->assertEquals( $result['upload']['sessionkey'], $result['upload']['filekey'] );
389 $filekey = $result['upload']['filekey'];
391 // it should be visible from Special:UploadStash
392 // XXX ...but how to test this, with a fake WebRequest with the session?
394 // now we should try to release the file from stash
396 'action' => 'upload',
397 'filekey' => $filekey,
398 'filename' => $fileName,
399 'comment' => 'dummy comment',
400 'text' => "This is the page text for $fileName, altered",
403 $this->clearFakeUploads();
406 list( $result ) = $this->doApiRequestWithToken( $params, $session,
407 self
::$users['uploader']->user
);
408 } catch ( UsageException
$e ) {
411 $this->assertTrue( isset( $result['upload'] ) );
412 $this->assertEquals( 'Success', $result['upload']['result'] );
413 $this->assertFalse( $exception, "No UsageException exception." );
416 $this->deleteFileByFilename( $fileName );
423 public function testUploadChunks( $session ) {
424 $this->setMwGlobals( array(
425 'wgUser' => self
::$users['uploader']->user
, // @todo FIXME: still used somewhere
428 $chunkSize = 1048576;
429 // Download a large image file
430 // ( using RandomImageGenerator for large files is not stable )
431 $mimeType = 'image/jpeg';
432 $url = 'http://upload.wikimedia.org/wikipedia/commons/e/ed/Oberaargletscher_from_Oberaar%2C_2010_07.JPG';
433 $filePath = wfTempDir() . '/Oberaargletscher_from_Oberaar.jpg';
435 // Only download if the file is not avaliable in the temp location:
436 if ( !is_file( $filePath ) ) {
437 copy( $url, $filePath );
439 } catch ( Exception
$e ) {
440 $this->markTestIncomplete( $e->getMessage() );
443 $fileSize = filesize( $filePath );
444 $fileName = basename( $filePath );
446 $this->deleteFileByFileName( $fileName );
447 $this->deleteFileByContent( $filePath );
449 // Base upload params:
451 'action' => 'upload',
453 'filename' => $fileName,
454 'filesize' => $fileSize,
459 $chunkSessionKey = false;
462 $handle = @fopen
( $filePath, "r" );
463 if ( $handle === false ) {
464 $this->markTestIncomplete( "could not open file: $filePath" );
466 while ( !feof( $handle ) ) {
467 // Get the current chunk
468 $chunkData = @fread
( $handle, $chunkSize );
470 // Upload the current chunk into the $_FILE object:
471 $this->fakeUploadChunk( 'chunk', 'blob', $mimeType, $chunkData );
473 // Check for chunkSessionKey
474 if ( !$chunkSessionKey ) {
475 // Upload fist chunk ( and get the session key )
477 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
478 self
::$users['uploader']->user
);
479 } catch ( UsageException
$e ) {
480 $this->markTestIncomplete( $e->getMessage() );
482 // Make sure we got a valid chunk continue:
483 $this->assertTrue( isset( $result['upload'] ) );
484 $this->assertTrue( isset( $result['upload']['filekey'] ) );
485 // If we don't get a session key mark test incomplete.
486 if ( !isset( $result['upload']['filekey'] ) ) {
487 $this->markTestIncomplete( "no filekey provided" );
489 $chunkSessionKey = $result['upload']['filekey'];
490 $this->assertEquals( 'Continue', $result['upload']['result'] );
491 // First chunk should have chunkSize == offset
492 $this->assertEquals( $chunkSize, $result['upload']['offset'] );
493 $resultOffset = $result['upload']['offset'];
496 // Filekey set to chunk session
497 $params['filekey'] = $chunkSessionKey;
498 // Update the offset ( always add chunkSize for subquent chunks should be in-sync with $result['upload']['offset'] )
499 $params['offset'] +
= $chunkSize;
500 // Make sure param offset is insync with resultOffset:
501 $this->assertEquals( $resultOffset, $params['offset'] );
502 // Upload current chunk
504 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
505 self
::$users['uploader']->user
);
506 } catch ( UsageException
$e ) {
507 $this->markTestIncomplete( $e->getMessage() );
509 // Make sure we got a valid chunk continue:
510 $this->assertTrue( isset( $result['upload'] ) );
511 $this->assertTrue( isset( $result['upload']['filekey'] ) );
513 // Check if we were on the last chunk:
514 if ( $params['offset'] +
$chunkSize >= $fileSize ) {
515 $this->assertEquals( 'Success', $result['upload']['result'] );
518 $this->assertEquals( 'Continue', $result['upload']['result'] );
519 // update $resultOffset
520 $resultOffset = $result['upload']['offset'];
525 // Check that we got a valid file result:
526 wfDebug( __METHOD__
. " hohoh filesize {$fileSize} info {$result['upload']['imageinfo']['size']}\n\n" );
527 $this->assertEquals( $fileSize, $result['upload']['imageinfo']['size'] );
528 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
529 $this->assertTrue( isset( $result['upload']['filekey'] ) );
530 $filekey = $result['upload']['filekey'];
532 // Now we should try to release the file from stash
534 'action' => 'upload',
535 'filekey' => $filekey,
536 'filename' => $fileName,
537 'comment' => 'dummy comment',
538 'text' => "This is the page text for $fileName, altered",
540 $this->clearFakeUploads();
543 list( $result ) = $this->doApiRequestWithToken( $params, $session,
544 self
::$users['uploader']->user
);
545 } catch ( UsageException
$e ) {
548 $this->assertTrue( isset( $result['upload'] ) );
549 $this->assertEquals( 'Success', $result['upload']['result'] );
550 $this->assertFalse( $exception );
553 $this->deleteFileByFilename( $fileName );
554 // don't remove downloaded temporary file for fast subquent tests.
555 //unlink( $filePath );