2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @group Upload
|
|
|
|
|
*/
|
2010-12-28 18:17:16 +00:00
|
|
|
class UploadTest extends MediaWikiTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
protected $upload;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
parent::setUp();
|
|
|
|
|
$wgContLang = Language::factory( 'en' );
|
|
|
|
|
$this->upload = new UploadTestHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test various forms of valid and invalid titles that can be supplied.
|
|
|
|
|
*/
|
|
|
|
|
public function testTitleValidation() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Test a valid title */
|
|
|
|
|
$this->assertUploadTitleAndCode( 'ValidTitle.jpg',
|
2010-12-22 00:25:16 +00:00
|
|
|
'ValidTitle.jpg', UploadBase::OK,
|
2010-12-14 16:26:35 +00:00
|
|
|
'upload valid title' );
|
|
|
|
|
|
|
|
|
|
/* A title with a slash */
|
|
|
|
|
$this->assertUploadTitleAndCode( 'A/B.jpg',
|
2010-12-22 00:25:16 +00:00
|
|
|
'B.jpg', UploadBase::OK,
|
2010-12-14 16:26:35 +00:00
|
|
|
'upload title with slash' );
|
|
|
|
|
|
|
|
|
|
/* A title with illegal char */
|
|
|
|
|
$this->assertUploadTitleAndCode( 'A:B.jpg',
|
2010-12-22 00:25:16 +00:00
|
|
|
'A-B.jpg', UploadBase::OK,
|
2010-12-14 16:26:35 +00:00
|
|
|
'upload title with colon' );
|
|
|
|
|
|
|
|
|
|
/* A title without extension */
|
|
|
|
|
$this->assertUploadTitleAndCode( 'A',
|
2010-12-22 00:25:16 +00:00
|
|
|
null, UploadBase::FILETYPE_MISSING,
|
2010-12-14 16:26:35 +00:00
|
|
|
'upload title without extension' );
|
|
|
|
|
|
|
|
|
|
/* A title with no basename */
|
|
|
|
|
$this->assertUploadTitleAndCode( '.jpg',
|
2010-12-22 00:25:16 +00:00
|
|
|
null, UploadBase::MIN_LENGTH_PARTNAME,
|
2010-12-14 16:26:35 +00:00
|
|
|
'upload title without basename' );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Helper function for testTitleValidation. First checks the return code
|
|
|
|
|
* of UploadBase::getTitle() and then the actual returned titl
|
|
|
|
|
*/
|
|
|
|
|
private function assertUploadTitleAndCode( $srcFilename, $dstFilename, $code, $msg ) {
|
|
|
|
|
/* Check the result code */
|
|
|
|
|
$this->assertEquals( $code,
|
|
|
|
|
$this->upload->testTitleValidation( $srcFilename ),
|
|
|
|
|
"$msg code" );
|
|
|
|
|
|
|
|
|
|
/* If we expect a valid title, check the title itself. */
|
2010-12-22 00:25:16 +00:00
|
|
|
if ( $code == UploadBase::OK ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( $dstFilename,
|
|
|
|
|
$this->upload->getTitle()->getText(),
|
|
|
|
|
"$msg text" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test the upload verification functions
|
|
|
|
|
*/
|
|
|
|
|
public function testVerifyUpload() {
|
|
|
|
|
/* Setup with zero file size */
|
|
|
|
|
$this->upload->initializePathInfo( '', '', 0 );
|
|
|
|
|
$result = $this->upload->verifyUpload();
|
2010-12-22 00:25:16 +00:00
|
|
|
$this->assertEquals( UploadBase::EMPTY_FILE,
|
2010-12-14 16:26:35 +00:00
|
|
|
$result['status'],
|
|
|
|
|
'upload empty file' );
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-25 21:26:28 +00:00
|
|
|
// Helper used to create an empty file of size $size.
|
|
|
|
|
private function createFileOfSize( $size ) {
|
|
|
|
|
$filename = '/tmp/mwuploadtest-' . posix_getpid() . '.txt' ;
|
|
|
|
|
|
|
|
|
|
$fh = fopen( $filename, 'w' );
|
|
|
|
|
fseek( $fh, $size-1, SEEK_SET);
|
|
|
|
|
fwrite( $fh, 0x00 );
|
|
|
|
|
fclose( $fh );
|
|
|
|
|
|
|
|
|
|
return $filename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test uploading a 100 bytes file with wgMaxUploadSize = 100
|
|
|
|
|
*
|
|
|
|
|
* This method should be abstracted so we can test different settings.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public function testMaxUploadSize() {
|
|
|
|
|
global $wgMaxUploadSize;
|
|
|
|
|
$savedGlobal = $wgMaxUploadSize; // save global
|
|
|
|
|
global $wgFileExtensions;
|
|
|
|
|
$wgFileExtensions[] = 'txt';
|
|
|
|
|
|
|
|
|
|
$wgMaxUploadSize = 100;
|
|
|
|
|
|
|
|
|
|
$filename = $this->createFileOfSize( $wgMaxUploadSize );
|
|
|
|
|
$this->upload->initializePathInfo( basename($filename), $filename, 100 );
|
|
|
|
|
$result = $this->upload->verifyUpload();
|
|
|
|
|
unlink( $filename );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
array( 'status' => UploadTestHandler::OK ), $result );
|
|
|
|
|
|
|
|
|
|
$wgMaxUploadSize = $savedGlobal; // restore global
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class UploadTestHandler extends UploadBase {
|
|
|
|
|
public function initializeFromRequest( &$request ) { }
|
|
|
|
|
public function testTitleValidation( $name ) {
|
|
|
|
|
$this->mTitle = false;
|
|
|
|
|
$this->mDesiredDestName = $name;
|
|
|
|
|
$this->getTitle();
|
|
|
|
|
return $this->mTitleError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|