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;
|
|
|
|
|
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2011-06-22 21:02:07 +00:00
|
|
|
global $wgHooks;
|
2010-12-14 16:26:35 +00:00
|
|
|
parent::setUp();
|
2011-06-22 21:02:07 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->upload = new UploadTestHandler;
|
2011-06-22 21:32:42 +00:00
|
|
|
$this->hooks = $wgHooks;
|
2012-08-12 19:47:06 +00:00
|
|
|
$wgHooks['InterwikiLoadPrefix'][] = function( $prefix, &$data ) {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2011-06-22 21:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function tearDown() {
|
2011-06-22 21:02:07 +00:00
|
|
|
global $wgHooks;
|
|
|
|
|
$wgHooks = $this->hooks;
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-17 04:05:11 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
2011-09-29 19:08:08 +00:00
|
|
|
* First checks the return code
|
|
|
|
|
* of UploadBase::getTitle() and then the actual returned title
|
|
|
|
|
*
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideTestTitleValidation
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2011-09-29 19:08:08 +00:00
|
|
|
public function testTitleValidation( $srcFilename, $dstFilename, $code, $msg ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
/* 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" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-29 19:08:08 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test various forms of valid and invalid titles that can be supplied.
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideTestTitleValidation() {
|
2011-09-29 19:08:08 +00:00
|
|
|
return array(
|
|
|
|
|
/* Test a valid title */
|
|
|
|
|
array( 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase::OK,
|
|
|
|
|
'upload valid title' ),
|
|
|
|
|
/* A title with a slash */
|
|
|
|
|
array( 'A/B.jpg', 'B.jpg', UploadBase::OK,
|
|
|
|
|
'upload title with slash' ),
|
|
|
|
|
/* A title with illegal char */
|
|
|
|
|
array( 'A:B.jpg', 'A-B.jpg', UploadBase::OK,
|
|
|
|
|
'upload title with colon' ),
|
|
|
|
|
/* Stripping leading File: prefix */
|
|
|
|
|
array( 'File:C.jpg', 'C.jpg', UploadBase::OK,
|
|
|
|
|
'upload title with File prefix' ),
|
|
|
|
|
/* Test illegal suggested title (r94601) */
|
|
|
|
|
array( '%281%29.JPG', null, UploadBase::ILLEGAL_FILENAME,
|
|
|
|
|
'illegal title for upload' ),
|
|
|
|
|
/* A title without extension */
|
|
|
|
|
array( 'A', null, UploadBase::FILETYPE_MISSING,
|
|
|
|
|
'upload title without extension' ),
|
|
|
|
|
/* A title with no basename */
|
|
|
|
|
array( '.jpg', null, UploadBase::MIN_LENGTH_PARTNAME,
|
|
|
|
|
'upload title without basename' ),
|
|
|
|
|
/* A title that is longer than 255 bytes */
|
2011-10-07 18:32:08 +00:00
|
|
|
array( str_repeat( 'a', 255 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
|
2011-09-29 19:08:08 +00:00
|
|
|
'upload title longer than 255 bytes' ),
|
|
|
|
|
/* A title that is longer than 240 bytes */
|
2011-10-07 18:32:08 +00:00
|
|
|
array( str_repeat( 'a', 240 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
|
2011-09-29 19:08:08 +00:00
|
|
|
'upload title longer than 240 bytes' ),
|
|
|
|
|
);
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 ) {
|
2011-02-12 15:18:59 +00:00
|
|
|
$filename = tempnam( wfTempDir(), "mwuploadtest" );
|
2011-01-25 21:26:28 +00:00
|
|
|
|
|
|
|
|
$fh = fopen( $filename, 'w' );
|
2011-02-10 10:48:02 +00:00
|
|
|
ftruncate( $fh, $size );
|
2011-01-25 21:26:28 +00:00
|
|
|
fclose( $fh );
|
|
|
|
|
|
|
|
|
|
return $filename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-31 21:25:00 +00:00
|
|
|
* test uploading a 100 bytes file with $wgMaxUploadSize = 100
|
2011-01-25 21:26:28 +00:00
|
|
|
*
|
|
|
|
|
* 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 );
|
2011-02-12 15:18:59 +00:00
|
|
|
$this->upload->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
|
2011-01-25 21:26:28 +00:00
|
|
|
$result = $this->upload->verifyUpload();
|
|
|
|
|
unlink( $filename );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2011-02-27 21:11:45 +00:00
|
|
|
array( 'status' => UploadBase::OK ), $result );
|
2011-01-25 21:26:28 +00:00
|
|
|
|
|
|
|
|
$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;
|
2011-09-29 19:00:45 +00:00
|
|
|
$this->mTitleError = UploadBase::OK;
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->getTitle();
|
|
|
|
|
return $this->mTitleError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|