5 class UploadTest
extends MediaWikiTestCase
{
12 $wgContLang = Language
::factory( 'en' );
13 $this->upload
= new UploadTestHandler
;
17 * Test various forms of valid and invalid titles that can be supplied.
19 public function testTitleValidation() {
22 /* Test a valid title */
23 $this->assertUploadTitleAndCode( 'ValidTitle.jpg',
24 'ValidTitle.jpg', UploadBase
::OK
,
25 'upload valid title' );
27 /* A title with a slash */
28 $this->assertUploadTitleAndCode( 'A/B.jpg',
29 'B.jpg', UploadBase
::OK
,
30 'upload title with slash' );
32 /* A title with illegal char */
33 $this->assertUploadTitleAndCode( 'A:B.jpg',
34 'A-B.jpg', UploadBase
::OK
,
35 'upload title with colon' );
37 /* A title without extension */
38 $this->assertUploadTitleAndCode( 'A',
39 null, UploadBase
::FILETYPE_MISSING
,
40 'upload title without extension' );
42 /* A title with no basename */
43 $this->assertUploadTitleAndCode( '.jpg',
44 null, UploadBase
::MIN_LENGTH_PARTNAME
,
45 'upload title without basename' );
49 * Helper function for testTitleValidation. First checks the return code
50 * of UploadBase::getTitle() and then the actual returned titl
52 private function assertUploadTitleAndCode( $srcFilename, $dstFilename, $code, $msg ) {
53 /* Check the result code */
54 $this->assertEquals( $code,
55 $this->upload
->testTitleValidation( $srcFilename ),
58 /* If we expect a valid title, check the title itself. */
59 if ( $code == UploadBase
::OK
) {
60 $this->assertEquals( $dstFilename,
61 $this->upload
->getTitle()->getText(),
67 * Test the upload verification functions
69 public function testVerifyUpload() {
70 /* Setup with zero file size */
71 $this->upload
->initializePathInfo( '', '', 0 );
72 $result = $this->upload
->verifyUpload();
73 $this->assertEquals( UploadBase
::EMPTY_FILE
,
75 'upload empty file' );
78 // Helper used to create an empty file of size $size.
79 private function createFileOfSize( $size ) {
80 $filename = tempnam( wfTempDir(), "mwuploadtest" );
82 $fh = fopen( $filename, 'w' );
83 ftruncate( $fh, $size );
90 * test uploading a 100 bytes file with wgMaxUploadSize = 100
92 * This method should be abstracted so we can test different settings.
95 public function testMaxUploadSize() {
96 global $wgMaxUploadSize;
97 $savedGlobal = $wgMaxUploadSize; // save global
98 global $wgFileExtensions;
99 $wgFileExtensions[] = 'txt';
101 $wgMaxUploadSize = 100;
103 $filename = $this->createFileOfSize( $wgMaxUploadSize );
104 $this->upload
->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
105 $result = $this->upload
->verifyUpload();
109 array( 'status' => UploadBase
::OK
), $result );
111 $wgMaxUploadSize = $savedGlobal; // restore global
115 class UploadTestHandler
extends UploadBase
{
116 public function initializeFromRequest( &$request ) { }
117 public function testTitleValidation( $name ) {
118 $this->mTitle
= false;
119 $this->mDesiredDestName
= $name;
121 return $this->mTitleError
;