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 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" );
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 );
154 public function testUploadZeroLength( $session ) {
155 $mimeType = 'image/png';
157 $filePath = tempnam( wfTempDir(), "" );
158 $fileName = "apiTestUploadZeroLength.png";
160 $this->deleteFileByFileName( $fileName );
162 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
163 $this->markTestIncomplete( "Couldn't upload file!\n" );
167 'action' => 'upload',
168 'filename' => $fileName,
169 'file' => 'dummy content',
170 'comment' => 'dummy comment',
171 'text' => "This is the page text for $fileName",
176 $this->doApiRequestWithToken( $params, $session, self
::$users['uploader']->user
);
177 } catch ( UsageException
$e ) {
178 $this->assertContains( 'The file you submitted was empty', $e->getMessage() );
181 $this->assertTrue( $exception );
184 $this->deleteFileByFilename( $fileName );
192 public function testUploadSameFileName( $session ) {
194 $mimeType = 'image/png';
197 $randomImageGenerator = new RandomImageGenerator();
198 $filePaths = $randomImageGenerator->writeImages( 2, $extension, wfTempDir() );
199 } catch ( Exception
$e ) {
200 $this->markTestIncomplete( $e->getMessage() );
203 // we'll reuse this filename
204 $fileName = basename( $filePaths[0] );
206 // clear any other files with the same name
207 $this->deleteFileByFileName( $fileName );
209 // we reuse these params
211 'action' => 'upload',
212 'filename' => $fileName,
213 'file' => 'dummy content',
214 'comment' => 'dummy comment',
215 'text' => "This is the page text for $fileName",
218 // first upload .... should succeed
220 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[0] ) ) {
221 $this->markTestIncomplete( "Couldn't upload file!\n" );
226 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
227 self
::$users['uploader']->user
);
228 } catch ( UsageException
$e ) {
231 $this->assertTrue( isset( $result['upload'] ) );
232 $this->assertEquals( 'Success', $result['upload']['result'] );
233 $this->assertFalse( $exception );
235 // second upload with the same name (but different content)
237 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[1] ) ) {
238 $this->markTestIncomplete( "Couldn't upload file!\n" );
243 list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
244 self
::$users['uploader']->user
); // FIXME: leaks a temporary file
245 } catch ( UsageException
$e ) {
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 );
255 $this->deleteFileByFilename( $fileName );
256 unlink( $filePaths[0] );
257 unlink( $filePaths[1] );
264 public function testUploadSameContent( $session ) {
266 $mimeType = 'image/png';
269 $randomImageGenerator = new RandomImageGenerator();
270 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
271 } catch ( Exception
$e ) {
272 $this->markTestIncomplete( $e->getMessage() );
275 $fileNames[0] = basename( $filePaths[0] );
276 $fileNames[1] = "SameContentAs" . $fileNames[0];
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] );
283 // first upload .... should succeed
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],
293 if ( !$this->fakeUploadFile( 'file', $fileNames[0], $mimeType, $filePaths[0] ) ) {
294 $this->markTestIncomplete( "Couldn't upload file!\n" );
299 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
300 self
::$users['uploader']->user
);
301 } catch ( UsageException
$e ) {
304 $this->assertTrue( isset( $result['upload'] ) );
305 $this->assertEquals( 'Success', $result['upload']['result'] );
306 $this->assertFalse( $exception );
308 // second upload with the same content (but different name)
310 if ( !$this->fakeUploadFile( 'file', $fileNames[1], $mimeType, $filePaths[0] ) ) {
311 $this->markTestIncomplete( "Couldn't upload file!\n" );
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],
324 list( $result ) = $this->doApiRequestWithToken( $params, $session,
325 self
::$users['uploader']->user
); // FIXME: leaks a temporary file
326 } catch ( UsageException
$e ) {
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 );
336 $this->deleteFileByFilename( $fileNames[0] );
337 $this->deleteFileByFilename( $fileNames[1] );
338 unlink( $filePaths[0] );
344 public function testUploadStash( $session ) {
345 $this->setMwGlobals( array(
346 'wgUser' => self
::$users['uploader']->user
, // @todo FIXME: still used somewhere
350 $mimeType = 'image/png';
353 $randomImageGenerator = new RandomImageGenerator();
354 $filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
355 } catch ( Exception
$e ) {
356 $this->markTestIncomplete( $e->getMessage() );
359 $filePath = $filePaths[0];
360 $fileSize = filesize( $filePath );
361 $fileName = basename( $filePath );
363 $this->deleteFileByFileName( $fileName );
364 $this->deleteFileByContent( $filePath );
366 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
367 $this->markTestIncomplete( "Couldn't upload file!\n" );
371 'action' => 'upload',
373 'filename' => $fileName,
374 'file' => 'dummy content',
375 'comment' => 'dummy comment',
376 'text' => "This is the page text for $fileName",
381 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
382 self
::$users['uploader']->user
); // FIXME: leaks a temporary file
383 } catch ( UsageException
$e ) {
386 $this->assertFalse( $exception );
387 $this->assertTrue( isset( $result['upload'] ) );
388 $this->assertEquals( 'Success', $result['upload']['result'] );
389 $this->assertEquals( $fileSize, ( int )$result['upload']['imageinfo']['size'] );
390 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
391 $this->assertTrue( isset( $result['upload']['filekey'] ) );
392 $this->assertEquals( $result['upload']['sessionkey'], $result['upload']['filekey'] );
393 $filekey = $result['upload']['filekey'];
395 // it should be visible from Special:UploadStash
396 // XXX ...but how to test this, with a fake WebRequest with the session?
398 // now we should try to release the file from stash
400 'action' => 'upload',
401 'filekey' => $filekey,
402 'filename' => $fileName,
403 'comment' => 'dummy comment',
404 'text' => "This is the page text for $fileName, altered",
407 $this->clearFakeUploads();
410 list( $result ) = $this->doApiRequestWithToken( $params, $session,
411 self
::$users['uploader']->user
);
412 } catch ( UsageException
$e ) {
415 $this->assertTrue( isset( $result['upload'] ) );
416 $this->assertEquals( 'Success', $result['upload']['result'] );
417 $this->assertFalse( $exception, "No UsageException exception." );
420 $this->deleteFileByFilename( $fileName );
427 public function testUploadChunks( $session ) {
428 $this->setMwGlobals( array(
429 'wgUser' => self
::$users['uploader']->user
, // @todo FIXME: still used somewhere
432 $chunkSize = 1048576;
433 // Download a large image file
434 // ( using RandomImageGenerator for large files is not stable )
435 $mimeType = 'image/jpeg';
436 $url = 'http://upload.wikimedia.org/wikipedia/commons/e/ed/Oberaargletscher_from_Oberaar%2C_2010_07.JPG';
437 $filePath = wfTempDir() . '/Oberaargletscher_from_Oberaar.jpg';
439 // Only download if the file is not avaliable in the temp location:
440 if ( !is_file( $filePath ) ) {
441 copy( $url, $filePath );
443 } catch ( Exception
$e ) {
444 $this->markTestIncomplete( $e->getMessage() );
447 $fileSize = filesize( $filePath );
448 $fileName = basename( $filePath );
450 $this->deleteFileByFileName( $fileName );
451 $this->deleteFileByContent( $filePath );
453 // Base upload params:
455 'action' => 'upload',
457 'filename' => $fileName,
458 'filesize' => $fileSize,
463 $chunkSessionKey = false;
466 $handle = @fopen
( $filePath, "r" );
467 if ( $handle === false ) {
468 $this->markTestIncomplete( "could not open file: $filePath" );
470 while ( !feof( $handle ) ) {
471 // Get the current chunk
472 $chunkData = @fread
( $handle, $chunkSize );
474 // Upload the current chunk into the $_FILE object:
475 $this->fakeUploadChunk( 'chunk', 'blob', $mimeType, $chunkData );
477 // Check for chunkSessionKey
478 if ( !$chunkSessionKey ) {
479 // Upload fist chunk ( and get the session key )
481 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
482 self
::$users['uploader']->user
);
483 } catch ( UsageException
$e ) {
484 $this->markTestIncomplete( $e->getMessage() );
486 // Make sure we got a valid chunk continue:
487 $this->assertTrue( isset( $result['upload'] ) );
488 $this->assertTrue( isset( $result['upload']['filekey'] ) );
489 // If we don't get a session key mark test incomplete.
490 if ( !isset( $result['upload']['filekey'] ) ) {
491 $this->markTestIncomplete( "no filekey provided" );
493 $chunkSessionKey = $result['upload']['filekey'];
494 $this->assertEquals( 'Continue', $result['upload']['result'] );
495 // First chunk should have chunkSize == offset
496 $this->assertEquals( $chunkSize, $result['upload']['offset'] );
497 $resultOffset = $result['upload']['offset'];
500 // Filekey set to chunk session
501 $params['filekey'] = $chunkSessionKey;
502 // Update the offset ( always add chunkSize for subquent chunks should be in-sync with $result['upload']['offset'] )
503 $params['offset'] +
= $chunkSize;
504 // Make sure param offset is insync with resultOffset:
505 $this->assertEquals( $resultOffset, $params['offset'] );
506 // Upload current chunk
508 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
509 self
::$users['uploader']->user
);
510 } catch ( UsageException
$e ) {
511 $this->markTestIncomplete( $e->getMessage() );
513 // Make sure we got a valid chunk continue:
514 $this->assertTrue( isset( $result['upload'] ) );
515 $this->assertTrue( isset( $result['upload']['filekey'] ) );
517 // Check if we were on the last chunk:
518 if ( $params['offset'] +
$chunkSize >= $fileSize ) {
519 $this->assertEquals( 'Success', $result['upload']['result'] );
522 $this->assertEquals( 'Continue', $result['upload']['result'] );
523 // update $resultOffset
524 $resultOffset = $result['upload']['offset'];
529 // Check that we got a valid file result:
530 wfDebug( __METHOD__
. " hohoh filesize {$fileSize} info {$result['upload']['imageinfo']['size']}\n\n" );
531 $this->assertEquals( $fileSize, $result['upload']['imageinfo']['size'] );
532 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
533 $this->assertTrue( isset( $result['upload']['filekey'] ) );
534 $filekey = $result['upload']['filekey'];
536 // Now we should try to release the file from stash
538 'action' => 'upload',
539 'filekey' => $filekey,
540 'filename' => $fileName,
541 'comment' => 'dummy comment',
542 'text' => "This is the page text for $fileName, altered",
544 $this->clearFakeUploads();
547 list( $result ) = $this->doApiRequestWithToken( $params, $session,
548 self
::$users['uploader']->user
);
549 } catch ( UsageException
$e ) {
552 $this->assertTrue( isset( $result['upload'] ) );
553 $this->assertEquals( 'Success', $result['upload']['result'] );
554 $this->assertFalse( $exception );
557 $this->deleteFileByFilename( $fileName );
558 // don't remove downloaded temporary file for fast subquent tests.
559 //unlink( $filePath );