2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-02 13:33:43 +00:00
|
|
|
* @group API
|
2010-12-14 16:26:35 +00:00
|
|
|
* @group Database
|
2013-11-23 21:53:23 +00:00
|
|
|
* @group medium
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* n.b. Ensure that you can write to the images/ directory as the
|
|
|
|
|
* user that will run tests.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Note for reviewers: this intentionally duplicates functionality already in "ApiSetup" and so on.
|
|
|
|
|
// This framework works better IMO and has less strangeness (such as test cases inheriting from "ApiSetup"...)
|
|
|
|
|
// (and in the case of the other Upload tests, this flat out just actually works... )
|
|
|
|
|
|
|
|
|
|
// TODO: port the other Upload tests, and other API tests to this framework
|
|
|
|
|
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once 'ApiTestCaseUpload.php';
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Database
|
2012-02-06 21:39:56 +00:00
|
|
|
* @group Broken
|
|
|
|
|
* Broken test, reports false errors from time to time.
|
|
|
|
|
* See https://bugzilla.wikimedia.org/26169
|
2011-01-02 05:52:00 +00:00
|
|
|
*
|
|
|
|
|
* This is pretty sucky... needs to be prettified.
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2011-04-06 19:50:54 +00:00
|
|
|
class ApiUploadTest extends ApiTestCaseUpload {
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
|
|
|
|
* Testing login
|
|
|
|
|
* XXX this is a funny way of getting session context
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testLogin() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$user = self::$users['uploader'];
|
|
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'login',
|
|
|
|
|
'lgname' => $user->username,
|
|
|
|
|
'lgpassword' => $user->password
|
|
|
|
|
);
|
2011-03-02 12:52:47 +00:00
|
|
|
list( $result, , $session ) = $this->doApiRequest( $params );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertArrayHasKey( "login", $result );
|
|
|
|
|
$this->assertArrayHasKey( "result", $result['login'] );
|
|
|
|
|
$this->assertEquals( "NeedToken", $result['login']['result'] );
|
|
|
|
|
$token = $result['login']['token'];
|
|
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'login',
|
|
|
|
|
'lgtoken' => $token,
|
|
|
|
|
'lgname' => $user->username,
|
|
|
|
|
'lgpassword' => $user->password
|
|
|
|
|
);
|
2011-03-02 12:52:47 +00:00
|
|
|
list( $result, , $session ) = $this->doApiRequest( $params, $session );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertArrayHasKey( "login", $result );
|
|
|
|
|
$this->assertArrayHasKey( "result", $result['login'] );
|
|
|
|
|
$this->assertEquals( "Success", $result['login']['result'] );
|
|
|
|
|
$this->assertArrayHasKey( 'lgtoken', $result['login'] );
|
|
|
|
|
|
2011-10-27 10:28:25 +00:00
|
|
|
$this->assertNotEmpty( $session, 'API Login must return a session' );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2013-04-26 12:00:22 +00:00
|
|
|
return $session;
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testLogin
|
|
|
|
|
*/
|
|
|
|
|
public function testUploadRequiresToken( $session ) {
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
|
|
|
|
$this->doApiRequest( array(
|
|
|
|
|
'action' => 'upload'
|
|
|
|
|
) );
|
|
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
$this->assertEquals( "The token parameter must be set", $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testLogin
|
|
|
|
|
*/
|
|
|
|
|
public function testUploadMissingParams( $session ) {
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
|
|
|
|
$this->doApiRequestWithToken( array(
|
|
|
|
|
'action' => 'upload',
|
2011-10-27 18:46:40 +00:00
|
|
|
), $session, self::$users['uploader']->user );
|
2010-12-14 16:26:35 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
2011-07-12 21:11:43 +00:00
|
|
|
$this->assertEquals( "One of the parameters filekey, file, url, statuskey is required",
|
2010-12-14 16:26:35 +00:00
|
|
|
$e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testLogin
|
|
|
|
|
*/
|
|
|
|
|
public function testUpload( $session ) {
|
|
|
|
|
$extension = 'png';
|
|
|
|
|
$mimeType = 'image/png';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$randomImageGenerator = new RandomImageGenerator();
|
2011-08-04 22:33:04 +00:00
|
|
|
$filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
|
2013-02-14 11:56:23 +00:00
|
|
|
} catch ( Exception $e ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-15 14:32:12 +00:00
|
|
|
/** @var array $filePaths */
|
2010-12-14 16:26:35 +00:00
|
|
|
$filePath = $filePaths[0];
|
|
|
|
|
$fileSize = filesize( $filePath );
|
|
|
|
|
$fileName = basename( $filePath );
|
|
|
|
|
|
|
|
|
|
$this->deleteFileByFileName( $fileName );
|
|
|
|
|
$this->deleteFileByContent( $filePath );
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( "Couldn't upload file!\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => $fileName,
|
|
|
|
|
'file' => 'dummy content',
|
|
|
|
|
'comment' => 'dummy comment',
|
2013-02-14 11:56:23 +00:00
|
|
|
'text' => "This is the page text for $fileName",
|
2010-12-14 16:26:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2011-10-27 18:46:40 +00:00
|
|
|
list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
|
|
|
|
|
self::$users['uploader']->user );
|
2010-12-14 16:26:35 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertEquals( 'Success', $result['upload']['result'] );
|
2013-08-31 16:17:28 +00:00
|
|
|
$this->assertEquals( $fileSize, (int)$result['upload']['imageinfo']['size'] );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
|
|
|
|
|
$this->assertFalse( $exception );
|
|
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
$this->deleteFileByFilename( $fileName );
|
|
|
|
|
unlink( $filePath );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testLogin
|
|
|
|
|
*/
|
|
|
|
|
public function testUploadZeroLength( $session ) {
|
|
|
|
|
$mimeType = 'image/png';
|
|
|
|
|
|
|
|
|
|
$filePath = tempnam( wfTempDir(), "" );
|
|
|
|
|
$fileName = "apiTestUploadZeroLength.png";
|
|
|
|
|
|
|
|
|
|
$this->deleteFileByFileName( $fileName );
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( "Couldn't upload file!\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => $fileName,
|
|
|
|
|
'file' => 'dummy content',
|
|
|
|
|
'comment' => 'dummy comment',
|
2013-02-14 11:56:23 +00:00
|
|
|
'text' => "This is the page text for $fileName",
|
2010-12-14 16:26:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2011-10-27 18:46:40 +00:00
|
|
|
$this->doApiRequestWithToken( $params, $session, self::$users['uploader']->user );
|
2010-12-14 16:26:35 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$this->assertContains( 'The file you submitted was empty', $e->getMessage() );
|
|
|
|
|
$exception = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( $exception );
|
|
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
$this->deleteFileByFilename( $fileName );
|
|
|
|
|
unlink( $filePath );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testLogin
|
|
|
|
|
*/
|
|
|
|
|
public function testUploadSameFileName( $session ) {
|
|
|
|
|
$extension = 'png';
|
|
|
|
|
$mimeType = 'image/png';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$randomImageGenerator = new RandomImageGenerator();
|
2011-08-04 22:33:04 +00:00
|
|
|
$filePaths = $randomImageGenerator->writeImages( 2, $extension, wfTempDir() );
|
2013-02-14 11:56:23 +00:00
|
|
|
} catch ( Exception $e ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we'll reuse this filename
|
2013-11-15 14:32:12 +00:00
|
|
|
/** @var array $filePaths */
|
2010-12-14 16:26:35 +00:00
|
|
|
$fileName = basename( $filePaths[0] );
|
|
|
|
|
|
|
|
|
|
// clear any other files with the same name
|
|
|
|
|
$this->deleteFileByFileName( $fileName );
|
|
|
|
|
|
|
|
|
|
// we reuse these params
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => $fileName,
|
|
|
|
|
'file' => 'dummy content',
|
|
|
|
|
'comment' => 'dummy comment',
|
2013-02-14 11:56:23 +00:00
|
|
|
'text' => "This is the page text for $fileName",
|
2010-12-14 16:26:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// first upload .... should succeed
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[0] ) ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( "Couldn't upload file!\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2011-10-27 18:46:40 +00:00
|
|
|
list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
|
|
|
|
|
self::$users['uploader']->user );
|
2010-12-14 16:26:35 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertEquals( 'Success', $result['upload']['result'] );
|
|
|
|
|
$this->assertFalse( $exception );
|
|
|
|
|
|
|
|
|
|
// second upload with the same name (but different content)
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePaths[1] ) ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( "Couldn't upload file!\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2011-10-27 18:46:40 +00:00
|
|
|
list( $result, , ) = $this->doApiRequestWithToken( $params, $session,
|
2011-11-08 22:27:25 +00:00
|
|
|
self::$users['uploader']->user ); // FIXME: leaks a temporary file
|
2010-12-14 16:26:35 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertEquals( 'Warning', $result['upload']['result'] );
|
|
|
|
|
$this->assertTrue( isset( $result['upload']['warnings'] ) );
|
|
|
|
|
$this->assertTrue( isset( $result['upload']['warnings']['exists'] ) );
|
|
|
|
|
$this->assertFalse( $exception );
|
|
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
$this->deleteFileByFilename( $fileName );
|
|
|
|
|
unlink( $filePaths[0] );
|
|
|
|
|
unlink( $filePaths[1] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testLogin
|
|
|
|
|
*/
|
|
|
|
|
public function testUploadSameContent( $session ) {
|
|
|
|
|
$extension = 'png';
|
|
|
|
|
$mimeType = 'image/png';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$randomImageGenerator = new RandomImageGenerator();
|
2011-08-04 22:33:04 +00:00
|
|
|
$filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
|
2013-02-14 11:56:23 +00:00
|
|
|
} catch ( Exception $e ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( $e->getMessage() );
|
|
|
|
|
}
|
2011-08-04 22:33:04 +00:00
|
|
|
|
2013-11-15 14:32:12 +00:00
|
|
|
/** @var array $filePaths */
|
2010-12-14 16:26:35 +00:00
|
|
|
$fileNames[0] = basename( $filePaths[0] );
|
|
|
|
|
$fileNames[1] = "SameContentAs" . $fileNames[0];
|
|
|
|
|
|
|
|
|
|
// clear any other files with the same name or content
|
|
|
|
|
$this->deleteFileByContent( $filePaths[0] );
|
|
|
|
|
$this->deleteFileByFileName( $fileNames[0] );
|
|
|
|
|
$this->deleteFileByFileName( $fileNames[1] );
|
|
|
|
|
|
|
|
|
|
// first upload .... should succeed
|
|
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => $fileNames[0],
|
|
|
|
|
'file' => 'dummy content',
|
|
|
|
|
'comment' => 'dummy comment',
|
2013-02-14 11:56:23 +00:00
|
|
|
'text' => "This is the page text for " . $fileNames[0],
|
2010-12-14 16:26:35 +00:00
|
|
|
);
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !$this->fakeUploadFile( 'file', $fileNames[0], $mimeType, $filePaths[0] ) ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( "Couldn't upload file!\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2013-04-26 07:48:46 +00:00
|
|
|
list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
|
2011-10-27 18:46:40 +00:00
|
|
|
self::$users['uploader']->user );
|
2010-12-14 16:26:35 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertEquals( 'Success', $result['upload']['result'] );
|
|
|
|
|
$this->assertFalse( $exception );
|
|
|
|
|
|
|
|
|
|
// second upload with the same content (but different name)
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !$this->fakeUploadFile( 'file', $fileNames[1], $mimeType, $filePaths[0] ) ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( "Couldn't upload file!\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'filename' => $fileNames[1],
|
|
|
|
|
'file' => 'dummy content',
|
|
|
|
|
'comment' => 'dummy comment',
|
2013-02-14 11:56:23 +00:00
|
|
|
'text' => "This is the page text for " . $fileNames[1],
|
2010-12-14 16:26:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2013-04-26 07:48:46 +00:00
|
|
|
list( $result ) = $this->doApiRequestWithToken( $params, $session,
|
2011-11-08 22:27:25 +00:00
|
|
|
self::$users['uploader']->user ); // FIXME: leaks a temporary file
|
2010-12-14 16:26:35 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertEquals( 'Warning', $result['upload']['result'] );
|
|
|
|
|
$this->assertTrue( isset( $result['upload']['warnings'] ) );
|
|
|
|
|
$this->assertTrue( isset( $result['upload']['warnings']['duplicate'] ) );
|
|
|
|
|
$this->assertFalse( $exception );
|
|
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
$this->deleteFileByFilename( $fileNames[0] );
|
|
|
|
|
$this->deleteFileByFilename( $fileNames[1] );
|
|
|
|
|
unlink( $filePaths[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @depends testLogin
|
|
|
|
|
*/
|
|
|
|
|
public function testUploadStash( $session ) {
|
2013-02-08 17:57:16 +00:00
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgUser' => self::$users['uploader']->user, // @todo FIXME: still used somewhere
|
|
|
|
|
) );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$extension = 'png';
|
|
|
|
|
$mimeType = 'image/png';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$randomImageGenerator = new RandomImageGenerator();
|
2011-08-04 22:33:04 +00:00
|
|
|
$filePaths = $randomImageGenerator->writeImages( 1, $extension, wfTempDir() );
|
2013-02-14 11:56:23 +00:00
|
|
|
} catch ( Exception $e ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-15 14:32:12 +00:00
|
|
|
/** @var array $filePaths */
|
2010-12-14 16:26:35 +00:00
|
|
|
$filePath = $filePaths[0];
|
|
|
|
|
$fileSize = filesize( $filePath );
|
|
|
|
|
$fileName = basename( $filePath );
|
|
|
|
|
|
|
|
|
|
$this->deleteFileByFileName( $fileName );
|
|
|
|
|
$this->deleteFileByContent( $filePath );
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, $filePath ) ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->markTestIncomplete( "Couldn't upload file!\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
2013-02-14 11:56:23 +00:00
|
|
|
'stash' => 1,
|
2010-12-14 16:26:35 +00:00
|
|
|
'filename' => $fileName,
|
|
|
|
|
'file' => 'dummy content',
|
|
|
|
|
'comment' => 'dummy comment',
|
2013-02-14 11:56:23 +00:00
|
|
|
'text' => "This is the page text for $fileName",
|
2010-12-14 16:26:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2013-04-26 07:48:46 +00:00
|
|
|
list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
|
2011-11-08 22:27:25 +00:00
|
|
|
self::$users['uploader']->user ); // FIXME: leaks a temporary file
|
2010-12-14 16:26:35 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertFalse( $exception );
|
|
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertEquals( 'Success', $result['upload']['result'] );
|
2013-08-31 16:17:28 +00:00
|
|
|
$this->assertEquals( $fileSize, (int)$result['upload']['imageinfo']['size'] );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
|
2011-07-12 21:11:43 +00:00
|
|
|
$this->assertTrue( isset( $result['upload']['filekey'] ) );
|
|
|
|
|
$this->assertEquals( $result['upload']['sessionkey'], $result['upload']['filekey'] );
|
|
|
|
|
$filekey = $result['upload']['filekey'];
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
// it should be visible from Special:UploadStash
|
|
|
|
|
// XXX ...but how to test this, with a fake WebRequest with the session?
|
|
|
|
|
|
|
|
|
|
// now we should try to release the file from stash
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
2011-07-12 21:11:43 +00:00
|
|
|
'filekey' => $filekey,
|
2010-12-14 16:26:35 +00:00
|
|
|
'filename' => $fileName,
|
|
|
|
|
'comment' => 'dummy comment',
|
2013-02-14 11:56:23 +00:00
|
|
|
'text' => "This is the page text for $fileName, altered",
|
2010-12-14 16:26:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->clearFakeUploads();
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2013-04-26 07:48:46 +00:00
|
|
|
list( $result ) = $this->doApiRequestWithToken( $params, $session,
|
2011-10-27 18:46:40 +00:00
|
|
|
self::$users['uploader']->user );
|
2010-12-14 16:26:35 +00:00
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertEquals( 'Success', $result['upload']['result'] );
|
2011-12-20 03:52:06 +00:00
|
|
|
$this->assertFalse( $exception, "No UsageException exception." );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
$this->deleteFileByFilename( $fileName );
|
|
|
|
|
unlink( $filePath );
|
|
|
|
|
}
|
2013-02-14 11:56:23 +00:00
|
|
|
|
2011-12-27 05:06:41 +00:00
|
|
|
/**
|
|
|
|
|
* @depends testLogin
|
|
|
|
|
*/
|
|
|
|
|
public function testUploadChunks( $session ) {
|
2013-02-08 17:57:16 +00:00
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgUser' => self::$users['uploader']->user, // @todo FIXME: still used somewhere
|
|
|
|
|
) );
|
|
|
|
|
|
2011-12-27 05:06:41 +00:00
|
|
|
$chunkSize = 1048576;
|
|
|
|
|
// Download a large image file
|
|
|
|
|
// ( using RandomImageGenerator for large files is not stable )
|
|
|
|
|
$mimeType = 'image/jpeg';
|
|
|
|
|
$url = 'http://upload.wikimedia.org/wikipedia/commons/e/ed/Oberaargletscher_from_Oberaar%2C_2010_07.JPG';
|
|
|
|
|
$filePath = wfTempDir() . '/Oberaargletscher_from_Oberaar.jpg';
|
|
|
|
|
try {
|
|
|
|
|
// Only download if the file is not avaliable in the temp location:
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !is_file( $filePath ) ) {
|
|
|
|
|
copy( $url, $filePath );
|
2011-12-27 05:06:41 +00:00
|
|
|
}
|
2013-02-14 11:56:23 +00:00
|
|
|
} catch ( Exception $e ) {
|
2011-12-27 05:06:41 +00:00
|
|
|
$this->markTestIncomplete( $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fileSize = filesize( $filePath );
|
|
|
|
|
$fileName = basename( $filePath );
|
|
|
|
|
|
|
|
|
|
$this->deleteFileByFileName( $fileName );
|
|
|
|
|
$this->deleteFileByContent( $filePath );
|
|
|
|
|
|
2013-08-24 15:06:25 +00:00
|
|
|
// Base upload params:
|
2011-12-27 05:06:41 +00:00
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
2013-02-14 11:56:23 +00:00
|
|
|
'stash' => 1,
|
2011-12-27 05:06:41 +00:00
|
|
|
'filename' => $fileName,
|
|
|
|
|
'filesize' => $fileSize,
|
|
|
|
|
'offset' => 0,
|
|
|
|
|
);
|
2013-02-14 11:56:23 +00:00
|
|
|
|
2011-12-27 05:06:41 +00:00
|
|
|
// Upload chunks
|
|
|
|
|
$chunkSessionKey = false;
|
|
|
|
|
$resultOffset = 0;
|
2013-08-24 15:06:25 +00:00
|
|
|
// Open the file:
|
2013-02-14 11:56:23 +00:00
|
|
|
$handle = @fopen( $filePath, "r" );
|
|
|
|
|
if ( $handle === false ) {
|
2011-12-27 05:06:41 +00:00
|
|
|
$this->markTestIncomplete( "could not open file: $filePath" );
|
|
|
|
|
}
|
2013-02-14 11:56:23 +00:00
|
|
|
while ( !feof( $handle ) ) {
|
2011-12-27 05:06:41 +00:00
|
|
|
// Get the current chunk
|
|
|
|
|
$chunkData = @fread( $handle, $chunkSize );
|
|
|
|
|
|
|
|
|
|
// Upload the current chunk into the $_FILE object:
|
|
|
|
|
$this->fakeUploadChunk( 'chunk', 'blob', $mimeType, $chunkData );
|
2013-02-14 11:56:23 +00:00
|
|
|
|
2011-12-27 05:06:41 +00:00
|
|
|
// Check for chunkSessionKey
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !$chunkSessionKey ) {
|
2011-12-27 05:06:41 +00:00
|
|
|
// Upload fist chunk ( and get the session key )
|
|
|
|
|
try {
|
2013-04-26 07:48:46 +00:00
|
|
|
list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
|
2011-12-27 05:06:41 +00:00
|
|
|
self::$users['uploader']->user );
|
|
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$this->markTestIncomplete( $e->getMessage() );
|
|
|
|
|
}
|
2013-08-24 15:06:25 +00:00
|
|
|
// Make sure we got a valid chunk continue:
|
2011-12-27 05:06:41 +00:00
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertTrue( isset( $result['upload']['filekey'] ) );
|
2013-08-24 15:06:25 +00:00
|
|
|
// If we don't get a session key mark test incomplete.
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( !isset( $result['upload']['filekey'] ) ) {
|
2011-12-27 05:06:41 +00:00
|
|
|
$this->markTestIncomplete( "no filekey provided" );
|
|
|
|
|
}
|
|
|
|
|
$chunkSessionKey = $result['upload']['filekey'];
|
|
|
|
|
$this->assertEquals( 'Continue', $result['upload']['result'] );
|
|
|
|
|
// First chunk should have chunkSize == offset
|
|
|
|
|
$this->assertEquals( $chunkSize, $result['upload']['offset'] );
|
|
|
|
|
$resultOffset = $result['upload']['offset'];
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// Filekey set to chunk session
|
|
|
|
|
$params['filekey'] = $chunkSessionKey;
|
|
|
|
|
// Update the offset ( always add chunkSize for subquent chunks should be in-sync with $result['upload']['offset'] )
|
|
|
|
|
$params['offset'] += $chunkSize;
|
|
|
|
|
// Make sure param offset is insync with resultOffset:
|
|
|
|
|
$this->assertEquals( $resultOffset, $params['offset'] );
|
|
|
|
|
// Upload current chunk
|
|
|
|
|
try {
|
2013-04-26 07:48:46 +00:00
|
|
|
list( $result, , $session ) = $this->doApiRequestWithToken( $params, $session,
|
2011-12-27 05:06:41 +00:00
|
|
|
self::$users['uploader']->user );
|
|
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$this->markTestIncomplete( $e->getMessage() );
|
|
|
|
|
}
|
2013-08-24 15:06:25 +00:00
|
|
|
// Make sure we got a valid chunk continue:
|
2011-12-27 05:06:41 +00:00
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertTrue( isset( $result['upload']['filekey'] ) );
|
2013-02-14 11:56:23 +00:00
|
|
|
|
2013-08-24 15:06:25 +00:00
|
|
|
// Check if we were on the last chunk:
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( $params['offset'] + $chunkSize >= $fileSize ) {
|
2011-12-27 05:06:41 +00:00
|
|
|
$this->assertEquals( 'Success', $result['upload']['result'] );
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals( 'Continue', $result['upload']['result'] );
|
|
|
|
|
// update $resultOffset
|
|
|
|
|
$resultOffset = $result['upload']['offset'];
|
|
|
|
|
}
|
2013-02-14 11:56:23 +00:00
|
|
|
}
|
|
|
|
|
fclose( $handle );
|
|
|
|
|
|
2011-12-27 05:06:41 +00:00
|
|
|
// Check that we got a valid file result:
|
2013-02-14 11:56:23 +00:00
|
|
|
wfDebug( __METHOD__ . " hohoh filesize {$fileSize} info {$result['upload']['imageinfo']['size']}\n\n" );
|
2011-12-27 05:06:41 +00:00
|
|
|
$this->assertEquals( $fileSize, $result['upload']['imageinfo']['size'] );
|
|
|
|
|
$this->assertEquals( $mimeType, $result['upload']['imageinfo']['mime'] );
|
|
|
|
|
$this->assertTrue( isset( $result['upload']['filekey'] ) );
|
|
|
|
|
$filekey = $result['upload']['filekey'];
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2011-12-27 05:06:41 +00:00
|
|
|
// Now we should try to release the file from stash
|
|
|
|
|
$params = array(
|
|
|
|
|
'action' => 'upload',
|
|
|
|
|
'filekey' => $filekey,
|
|
|
|
|
'filename' => $fileName,
|
|
|
|
|
'comment' => 'dummy comment',
|
2013-02-14 11:56:23 +00:00
|
|
|
'text' => "This is the page text for $fileName, altered",
|
2011-12-27 05:06:41 +00:00
|
|
|
);
|
|
|
|
|
$this->clearFakeUploads();
|
|
|
|
|
$exception = false;
|
|
|
|
|
try {
|
2013-04-26 07:48:46 +00:00
|
|
|
list( $result ) = $this->doApiRequestWithToken( $params, $session,
|
2011-12-27 05:06:41 +00:00
|
|
|
self::$users['uploader']->user );
|
|
|
|
|
} catch ( UsageException $e ) {
|
|
|
|
|
$exception = true;
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( isset( $result['upload'] ) );
|
|
|
|
|
$this->assertEquals( 'Success', $result['upload']['result'] );
|
|
|
|
|
$this->assertFalse( $exception );
|
|
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
$this->deleteFileByFilename( $fileName );
|
2013-08-24 15:06:25 +00:00
|
|
|
// don't remove downloaded temporary file for fast subquent tests.
|
2011-12-27 05:06:41 +00:00
|
|
|
//unlink( $filePath );
|
|
|
|
|
}
|
|
|
|
|
}
|