Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / api / ApiUploadTest.php
blobb4b1bf3f6f9006bf97700fb3ace1d934d7c8125a
1 <?php
2 /**
3 * @group API
4 * @group Database
5 * @group medium
6 */
8 /**
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
14 // "ApiSetup" and so on. This framework works better IMO and has less
15 // strangeness (such as test cases inheriting from "ApiSetup"...) (and in the
16 // 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 /**
21 * @group Database
22 * @group Broken
23 * Broken test, reports false errors from time to time.
24 * See https://bugzilla.wikimedia.org/26169
26 * This is pretty sucky... needs to be prettified.
28 class ApiUploadTest extends ApiTestCaseUpload {
29 /**
30 * Testing login
31 * XXX this is a funny way of getting session context
33 public function testLogin() {
34 $user = self::$users['uploader'];
36 $params = array(
37 'action' => 'login',
38 'lgname' => $user->username,
39 'lgpassword' => $user->password
41 list( $result, , $session ) = $this->doApiRequest( $params );
42 $this->assertArrayHasKey( "login", $result );
43 $this->assertArrayHasKey( "result", $result['login'] );
44 $this->assertEquals( "NeedToken", $result['login']['result'] );
45 $token = $result['login']['token'];
47 $params = array(
48 'action' => 'login',
49 'lgtoken' => $token,
50 'lgname' => $user->username,
51 'lgpassword' => $user->password
53 list( $result, , $session ) = $this->doApiRequest( $params, $session );
54 $this->assertArrayHasKey( "login", $result );
55 $this->assertArrayHasKey( "result", $result['login'] );
56 $this->assertEquals( "Success", $result['login']['result'] );
57 $this->assertArrayHasKey( 'lgtoken', $result['login'] );
59 $this->assertNotEmpty( $session, 'API Login must return a session' );
61 return $session;
64 /**
65 * @depends testLogin
67 public function testUploadRequiresToken( $session ) {
68 $exception = false;
69 try {
70 $this->doApiRequest( array(
71 'action' => 'upload'
72 ) );
73 } catch ( UsageException $e ) {
74 $exception = true;
75 $this->assertEquals( "The token parameter must be set", $e->getMessage() );
77 $this->assertTrue( $exception, "Got exception" );
80 /**
81 * @depends testLogin
83 public function testUploadMissingParams( $session ) {
84 $exception = false;
85 try {
86 $this->doApiRequestWithToken( array(
87 'action' => 'upload',
88 ), $session, self::$users['uploader']->user );
89 } catch ( UsageException $e ) {
90 $exception = true;
91 $this->assertEquals( "One of the parameters filekey, file, url, statuskey is required",
92 $e->getMessage() );
94 $this->assertTrue( $exception, "Got exception" );
97 /**
98 * @depends testLogin
100 public function testUpload( $session ) {
101 $extension = 'png';
102 $mimeType = 'image/png';
104 try {
105 $randomImageGenerator = new RandomImageGenerator();
106 $filePaths = $randomImageGenerator->writeImages( 1, $extension, $this->getNewTempDirectory() );
107 } catch ( Exception $e ) {
108 $this->markTestIncomplete( $e->getMessage() );
111 /** @var array $filePaths */
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" );
123 $params = array(
124 'action' => 'upload',
125 'filename' => $fileName,
126 'file' => 'dummy content',
127 'comment' => 'dummy comment',
128 'text' => "This is the page text for $fileName",
131 $exception = false;
132 try {
133 list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
134 self::$users['uploader']->user );
135 } catch ( UsageException $e ) {
136 $exception = true;
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 );
144 // clean up
145 $this->deleteFileByFilename( $fileName );
149 * @depends testLogin
151 public function testUploadZeroLength( $session ) {
152 $mimeType = 'image/png';
154 $filePath = $this->getNewTempFile();
155 $fileName = "apiTestUploadZeroLength.png";
157 $this->deleteFileByFileName( $fileName );
159 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
160 $this->markTestIncomplete( "Couldn't upload file!\n" );
163 $params = array(
164 'action' => 'upload',
165 'filename' => $fileName,
166 'file' => 'dummy content',
167 'comment' => 'dummy comment',
168 'text' => "This is the page text for $fileName",
171 $exception = false;
172 try {
173 $this->doApiRequestWithToken( $params, $session, self::$users['uploader']->user );
174 } catch ( UsageException $e ) {
175 $this->assertContains( 'The file you submitted was empty', $e->getMessage() );
176 $exception = true;
178 $this->assertTrue( $exception );
180 // clean up
181 $this->deleteFileByFilename( $fileName );
185 * @depends testLogin
187 public function testUploadSameFileName( $session ) {
188 $extension = 'png';
189 $mimeType = 'image/png';
191 try {
192 $randomImageGenerator = new RandomImageGenerator();
193 $filePaths = $randomImageGenerator->writeImages( 2, $extension, $this->getNewTempDirectory() );
194 } catch ( Exception $e ) {
195 $this->markTestIncomplete( $e->getMessage() );
198 // we'll reuse this filename
199 /** @var array $filePaths */
200 $fileName = basename( $filePaths[0] );
202 // clear any other files with the same name
203 $this->deleteFileByFileName( $fileName );
205 // we reuse these params
206 $params = array(
207 'action' => 'upload',
208 'filename' => $fileName,
209 'file' => 'dummy content',
210 'comment' => 'dummy comment',
211 'text' => "This is the page text for $fileName",
214 // first upload .... should succeed
216 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[0] ) ) {
217 $this->markTestIncomplete( "Couldn't upload file!\n" );
220 $exception = false;
221 try {
222 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
223 self::$users['uploader']->user );
224 } catch ( UsageException $e ) {
225 $exception = true;
227 $this->assertTrue( isset( $result['upload'] ) );
228 $this->assertEquals( 'Success', $result['upload']['result'] );
229 $this->assertFalse( $exception );
231 // second upload with the same name (but different content)
233 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[1] ) ) {
234 $this->markTestIncomplete( "Couldn't upload file!\n" );
237 $exception = false;
238 try {
239 list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
240 self::$users['uploader']->user ); // FIXME: leaks a temporary file
241 } catch ( UsageException $e ) {
242 $exception = true;
244 $this->assertTrue( isset( $result['upload'] ) );
245 $this->assertEquals( 'Warning', $result['upload']['result'] );
246 $this->assertTrue( isset( $result['upload']['warnings'] ) );
247 $this->assertTrue( isset( $result['upload']['warnings']['exists'] ) );
248 $this->assertFalse( $exception );
250 // clean up
251 $this->deleteFileByFilename( $fileName );
255 * @depends testLogin
257 public function testUploadSameContent( $session ) {
258 $extension = 'png';
259 $mimeType = 'image/png';
261 try {
262 $randomImageGenerator = new RandomImageGenerator();
263 $filePaths = $randomImageGenerator->writeImages( 1, $extension, $this->getNewTempDirectory() );
264 } catch ( Exception $e ) {
265 $this->markTestIncomplete( $e->getMessage() );
268 /** @var array $filePaths */
269 $fileNames[0] = basename( $filePaths[0] );
270 $fileNames[1] = "SameContentAs" . $fileNames[0];
272 // clear any other files with the same name or content
273 $this->deleteFileByContent( $filePaths[0] );
274 $this->deleteFileByFileName( $fileNames[0] );
275 $this->deleteFileByFileName( $fileNames[1] );
277 // first upload .... should succeed
279 $params = array(
280 'action' => 'upload',
281 'filename' => $fileNames[0],
282 'file' => 'dummy content',
283 'comment' => 'dummy comment',
284 'text' => "This is the page text for " . $fileNames[0],
287 if ( !$this->fakeUploadFile( 'file', $fileNames[0], $mimeType, $filePaths[0] ) ) {
288 $this->markTestIncomplete( "Couldn't upload file!\n" );
291 $exception = false;
292 try {
293 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
294 self::$users['uploader']->user );
295 } catch ( UsageException $e ) {
296 $exception = true;
298 $this->assertTrue( isset( $result['upload'] ) );
299 $this->assertEquals( 'Success', $result['upload']['result'] );
300 $this->assertFalse( $exception );
302 // second upload with the same content (but different name)
304 if ( !$this->fakeUploadFile( 'file', $fileNames[1], $mimeType, $filePaths[0] ) ) {
305 $this->markTestIncomplete( "Couldn't upload file!\n" );
308 $params = array(
309 'action' => 'upload',
310 'filename' => $fileNames[1],
311 'file' => 'dummy content',
312 'comment' => 'dummy comment',
313 'text' => "This is the page text for " . $fileNames[1],
316 $exception = false;
317 try {
318 list( $result ) = $this->doApiRequestWithToken( $params, $session,
319 self::$users['uploader']->user ); // FIXME: leaks a temporary file
320 } catch ( UsageException $e ) {
321 $exception = true;
323 $this->assertTrue( isset( $result['upload'] ) );
324 $this->assertEquals( 'Warning', $result['upload']['result'] );
325 $this->assertTrue( isset( $result['upload']['warnings'] ) );
326 $this->assertTrue( isset( $result['upload']['warnings']['duplicate'] ) );
327 $this->assertFalse( $exception );
329 // clean up
330 $this->deleteFileByFilename( $fileNames[0] );
331 $this->deleteFileByFilename( $fileNames[1] );
335 * @depends testLogin
337 public function testUploadStash( $session ) {
338 $this->setMwGlobals( array(
339 'wgUser' => self::$users['uploader']->user, // @todo FIXME: still used somewhere
340 ) );
342 $extension = 'png';
343 $mimeType = 'image/png';
345 try {
346 $randomImageGenerator = new RandomImageGenerator();
347 $filePaths = $randomImageGenerator->writeImages( 1, $extension, $this->getNewTempDirectory() );
348 } catch ( Exception $e ) {
349 $this->markTestIncomplete( $e->getMessage() );
352 /** @var array $filePaths */
353 $filePath = $filePaths[0];
354 $fileSize = filesize( $filePath );
355 $fileName = basename( $filePath );
357 $this->deleteFileByFileName( $fileName );
358 $this->deleteFileByContent( $filePath );
360 if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
361 $this->markTestIncomplete( "Couldn't upload file!\n" );
364 $params = array(
365 'action' => 'upload',
366 'stash' => 1,
367 'filename' => $fileName,
368 'file' => 'dummy content',
369 'comment' => 'dummy comment',
370 'text' => "This is the page text for $fileName",
373 $exception = false;
374 try {
375 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
376 self::$users['uploader']->user ); // FIXME: leaks a temporary file
377 } catch ( UsageException $e ) {
378 $exception = true;
380 $this->assertFalse( $exception );
381 $this->assertTrue( isset( $result['upload'] ) );
382 $this->assertEquals( 'Success', $result['upload']['result'] );
383 $this->assertEquals( $fileSize, (int)$result['upload']['imageinfo']['size'] );
384 $this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
385 $this->assertTrue( isset( $result['upload']['filekey'] ) );
386 $this->assertEquals( $result['upload']['sessionkey'], $result['upload']['filekey'] );
387 $filekey = $result['upload']['filekey'];
389 // it should be visible from Special:UploadStash
390 // XXX ...but how to test this, with a fake WebRequest with the session?
392 // now we should try to release the file from stash
393 $params = array(
394 'action' => 'upload',
395 'filekey' => $filekey,
396 'filename' => $fileName,
397 'comment' => 'dummy comment',
398 'text' => "This is the page text for $fileName, altered",
401 $this->clearFakeUploads();
402 $exception = false;
403 try {
404 list( $result ) = $this->doApiRequestWithToken( $params, $session,
405 self::$users['uploader']->user );
406 } catch ( UsageException $e ) {
407 $exception = true;
409 $this->assertTrue( isset( $result['upload'] ) );
410 $this->assertEquals( 'Success', $result['upload']['result'] );
411 $this->assertFalse( $exception, "No UsageException exception." );
413 // clean up
414 $this->deleteFileByFilename( $fileName );
418 * @depends testLogin
420 public function testUploadChunks( $session ) {
421 $this->setMwGlobals( array(
422 // @todo FIXME: still used somewhere
423 'wgUser' => self::$users['uploader']->user,
424 ) );
426 $chunkSize = 1048576;
427 // Download a large image file
428 // (using RandomImageGenerator for large files is not stable)
429 // @todo Don't download files from wikimedia.org
430 $mimeType = 'image/jpeg';
431 $url = 'http://upload.wikimedia.org/wikipedia/commons/'
432 . 'e/ed/Oberaargletscher_from_Oberaar%2C_2010_07.JPG';
433 $filePath = $this->getNewTempDirectory() . '/Oberaargletscher_from_Oberaar.jpg';
434 try {
435 copy( $url, $filePath );
436 } catch ( Exception $e ) {
437 $this->markTestIncomplete( $e->getMessage() );
440 $fileSize = filesize( $filePath );
441 $fileName = basename( $filePath );
443 $this->deleteFileByFileName( $fileName );
444 $this->deleteFileByContent( $filePath );
446 // Base upload params:
447 $params = array(
448 'action' => 'upload',
449 'stash' => 1,
450 'filename' => $fileName,
451 'filesize' => $fileSize,
452 'offset' => 0,
455 // Upload chunks
456 $chunkSessionKey = false;
457 $resultOffset = 0;
458 // Open the file:
459 wfSuppressWarnings();
460 $handle = fopen( $filePath, "r" );
461 wfRestoreWarnings();
463 if ( $handle === false ) {
464 $this->markTestIncomplete( "could not open file: $filePath" );
467 while ( !feof( $handle ) ) {
468 // Get the current chunk
469 wfSuppressWarnings();
470 $chunkData = fread( $handle, $chunkSize );
471 wfRestoreWarnings();
473 // Upload the current chunk into the $_FILE object:
474 $this->fakeUploadChunk( 'chunk', 'blob', $mimeType, $chunkData );
476 // Check for chunkSessionKey
477 if ( !$chunkSessionKey ) {
478 // Upload fist chunk ( and get the session key )
479 try {
480 list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
481 self::$users['uploader']->user );
482 } catch ( UsageException $e ) {
483 $this->markTestIncomplete( $e->getMessage() );
485 // Make sure we got a valid chunk continue:
486 $this->assertTrue( isset( $result['upload'] ) );
487 $this->assertTrue( isset( $result['upload']['filekey'] ) );
488 // If we don't get a session key mark test incomplete.
489 if ( !isset( $result['upload']['filekey'] ) ) {
490 $this->markTestIncomplete( "no filekey provided" );
492 $chunkSessionKey = $result['upload']['filekey'];
493 $this->assertEquals( 'Continue', $result['upload']['result'] );
494 // First chunk should have chunkSize == offset
495 $this->assertEquals( $chunkSize, $result['upload']['offset'] );
496 $resultOffset = $result['upload']['offset'];
497 continue;
499 // Filekey set to chunk session
500 $params['filekey'] = $chunkSessionKey;
501 // Update the offset ( always add chunkSize for subquent chunks
502 // 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
507 try {
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'] );
520 break;
521 } else {
522 $this->assertEquals( 'Continue', $result['upload']['result'] );
523 // update $resultOffset
524 $resultOffset = $result['upload']['offset'];
527 fclose( $handle );
529 // Check that we got a valid file result:
530 wfDebug( __METHOD__
531 . " 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'];
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",
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;
553 $this->assertTrue( isset( $result['upload'] ) );
554 $this->assertEquals( 'Success', $result['upload']['result'] );
555 $this->assertFalse( $exception );
557 // clean up
558 $this->deleteFileByFilename( $fileName );