5 class UploadTest
extends MediaWikiTestCase
{
13 $this->upload
= new UploadTestHandler
;
14 $this->hooks
= $wgHooks;
15 $wgHooks['InterwikiLoadPrefix'][] = function( $prefix, &$data ) {
22 $wgHooks = $this->hooks
;
27 * First checks the return code
28 * of UploadBase::getTitle() and then the actual returned title
30 * @dataProvider dataTestTitleValidation
32 public function testTitleValidation( $srcFilename, $dstFilename, $code, $msg ) {
33 /* Check the result code */
34 $this->assertEquals( $code,
35 $this->upload
->testTitleValidation( $srcFilename ),
38 /* If we expect a valid title, check the title itself. */
39 if ( $code == UploadBase
::OK
) {
40 $this->assertEquals( $dstFilename,
41 $this->upload
->getTitle()->getText(),
47 * Test various forms of valid and invalid titles that can be supplied.
49 public function dataTestTitleValidation() {
51 /* Test a valid title */
52 array( 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase
::OK
,
53 'upload valid title' ),
54 /* A title with a slash */
55 array( 'A/B.jpg', 'B.jpg', UploadBase
::OK
,
56 'upload title with slash' ),
57 /* A title with illegal char */
58 array( 'A:B.jpg', 'A-B.jpg', UploadBase
::OK
,
59 'upload title with colon' ),
60 /* Stripping leading File: prefix */
61 array( 'File:C.jpg', 'C.jpg', UploadBase
::OK
,
62 'upload title with File prefix' ),
63 /* Test illegal suggested title (r94601) */
64 array( '%281%29.JPG', null, UploadBase
::ILLEGAL_FILENAME
,
65 'illegal title for upload' ),
66 /* A title without extension */
67 array( 'A', null, UploadBase
::FILETYPE_MISSING
,
68 'upload title without extension' ),
69 /* A title with no basename */
70 array( '.jpg', null, UploadBase
::MIN_LENGTH_PARTNAME
,
71 'upload title without basename' ),
72 /* A title that is longer than 255 bytes */
73 array( str_repeat( 'a', 255 ) . '.jpg', null, UploadBase
::FILENAME_TOO_LONG
,
74 'upload title longer than 255 bytes' ),
75 /* A title that is longer than 240 bytes */
76 array( str_repeat( 'a', 240 ) . '.jpg', null, UploadBase
::FILENAME_TOO_LONG
,
77 'upload title longer than 240 bytes' ),
82 * Test the upload verification functions
84 public function testVerifyUpload() {
85 /* Setup with zero file size */
86 $this->upload
->initializePathInfo( '', '', 0 );
87 $result = $this->upload
->verifyUpload();
88 $this->assertEquals( UploadBase
::EMPTY_FILE
,
90 'upload empty file' );
93 // Helper used to create an empty file of size $size.
94 private function createFileOfSize( $size ) {
95 $filename = tempnam( wfTempDir(), "mwuploadtest" );
97 $fh = fopen( $filename, 'w' );
98 ftruncate( $fh, $size );
105 * test uploading a 100 bytes file with $wgMaxUploadSize = 100
107 * This method should be abstracted so we can test different settings.
110 public function testMaxUploadSize() {
111 global $wgMaxUploadSize;
112 $savedGlobal = $wgMaxUploadSize; // save global
113 global $wgFileExtensions;
114 $wgFileExtensions[] = 'txt';
116 $wgMaxUploadSize = 100;
118 $filename = $this->createFileOfSize( $wgMaxUploadSize );
119 $this->upload
->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
120 $result = $this->upload
->verifyUpload();
124 array( 'status' => UploadBase
::OK
), $result );
126 $wgMaxUploadSize = $savedGlobal; // restore global
130 class UploadTestHandler
extends UploadBase
{
131 public function initializeFromRequest( &$request ) { }
132 public function testTitleValidation( $name ) {
133 $this->mTitle
= false;
134 $this->mDesiredDestName
= $name;
135 $this->mTitleError
= UploadBase
::OK
;
137 return $this->mTitleError
;