wiki.techinc.nl/tests/phpunit/includes/upload/UploadTest.php
Timo Tijhof 181c7cdc8e Clean and repair many phpunit tests (+ fix implied configuration)
This commit depends on the introduction of
MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4.

Various tests already set their globals, but forgot to restore
them afterwards, or forgot to call the parent setUp, tearDown...

Either way they won't have to anymore with setMwGlobals.

Consistent use of function characteristics:
* protected function setUp
* protected function tearDown
* public static function (provide..)

(Matching the function signature with PHPUnit/Framework/TestCase.php)

Replaces:
 * public function (setUp|tearDown)\(
 * protected function $1(

 * \tfunction (setUp|tearDown)\(
 * \tprotected function $1(

 * \tfunction (data|provide)\(
 * \tpublic static function $1\(

Also renamed a few "data#", "provider#" and "provides#" functions
to "provide#" for consistency. This also removes confusion where
the /media tests had a few private methods called dataFile(),
which were sometimes expected to be data providers.

Fixes:

TimestampTest often failed due to a previous test setting a
different language (it tests "1 hour ago" so need to make sure
it is set to English).

MWNamespaceTest became a lot cleaner now that it executes with
a known context. Though the now-redundant code that was removed
didn't work anyway because wgContentNamespaces isn't keyed by
namespace id, it had them was values...

FileBackendTest:
* Fixed: "PHP Fatal: Using $this when not in object context"

HttpTest
* Added comment about:
  "PHP Fatal: Call to protected MWHttpRequest::__construct()"
  (too much unrelated code to fix in this commit)

ExternalStoreTest
* Add an assertTrue as well, without it the test is useless
  because regardless of whether wgExternalStores is true or false
  it only uses it if it is an array.

Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-09 03:01:51 +02:00

141 lines
3.9 KiB
PHP

<?php
/**
* @group Upload
*/
class UploadTest extends MediaWikiTestCase {
protected $upload;
protected function setUp() {
global $wgHooks;
parent::setUp();
$this->upload = new UploadTestHandler;
$this->hooks = $wgHooks;
$wgHooks['InterwikiLoadPrefix'][] = function( $prefix, &$data ) {
return false;
};
}
protected function tearDown() {
global $wgHooks;
$wgHooks = $this->hooks;
}
/**
* First checks the return code
* of UploadBase::getTitle() and then the actual returned title
*
* @dataProvider provideTestTitleValidation
*/
public function testTitleValidation( $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. */
if ( $code == UploadBase::OK ) {
$this->assertEquals( $dstFilename,
$this->upload->getTitle()->getText(),
"$msg text" );
}
}
/**
* Test various forms of valid and invalid titles that can be supplied.
*/
public static function provideTestTitleValidation() {
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 */
array( str_repeat( 'a', 255 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
'upload title longer than 255 bytes' ),
/* A title that is longer than 240 bytes */
array( str_repeat( 'a', 240 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
'upload title longer than 240 bytes' ),
);
}
/**
* Test the upload verification functions
*/
public function testVerifyUpload() {
/* Setup with zero file size */
$this->upload->initializePathInfo( '', '', 0 );
$result = $this->upload->verifyUpload();
$this->assertEquals( UploadBase::EMPTY_FILE,
$result['status'],
'upload empty file' );
}
// Helper used to create an empty file of size $size.
private function createFileOfSize( $size ) {
$filename = tempnam( wfTempDir(), "mwuploadtest" );
$fh = fopen( $filename, 'w' );
ftruncate( $fh, $size );
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) . '.txt', $filename, 100 );
$result = $this->upload->verifyUpload();
unlink( $filename );
$this->assertEquals(
array( 'status' => UploadBase::OK ), $result );
$wgMaxUploadSize = $savedGlobal; // restore global
}
}
class UploadTestHandler extends UploadBase {
public function initializeFromRequest( &$request ) { }
public function testTitleValidation( $name ) {
$this->mTitle = false;
$this->mDesiredDestName = $name;
$this->mTitleError = UploadBase::OK;
$this->getTitle();
return $this->mTitleError;
}
}