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