6 class UploadBaseTest
extends MediaWikiTestCase
{
8 /** @var UploadTestHandler */
11 protected function setUp() {
15 $this->upload
= new UploadTestHandler
;
16 $this->hooks
= $wgHooks;
17 $wgHooks['InterwikiLoadPrefix'][] = function ( $prefix, &$data ) {
22 protected function tearDown() {
24 $wgHooks = $this->hooks
;
30 * First checks the return code
31 * of UploadBase::getTitle() and then the actual returned title
33 * @dataProvider provideTestTitleValidation
34 * @covers UploadBase::getTitle
36 public function testTitleValidation( $srcFilename, $dstFilename, $code, $msg ) {
37 /* Check the result code */
38 $this->assertEquals( $code,
39 $this->upload
->testTitleValidation( $srcFilename ),
42 /* If we expect a valid title, check the title itself. */
43 if ( $code == UploadBase
::OK
) {
44 $this->assertEquals( $dstFilename,
45 $this->upload
->getTitle()->getText(),
51 * Test various forms of valid and invalid titles that can be supplied.
53 public static function provideTestTitleValidation() {
55 /* Test a valid title */
56 array( 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase
::OK
,
57 'upload valid title' ),
58 /* A title with a slash */
59 array( 'A/B.jpg', 'B.jpg', UploadBase
::OK
,
60 'upload title with slash' ),
61 /* A title with illegal char */
62 array( 'A:B.jpg', 'A-B.jpg', UploadBase
::OK
,
63 'upload title with colon' ),
64 /* Stripping leading File: prefix */
65 array( 'File:C.jpg', 'C.jpg', UploadBase
::OK
,
66 'upload title with File prefix' ),
67 /* Test illegal suggested title (r94601) */
68 array( '%281%29.JPG', null, UploadBase
::ILLEGAL_FILENAME
,
69 'illegal title for upload' ),
70 /* A title without extension */
71 array( 'A', null, UploadBase
::FILETYPE_MISSING
,
72 'upload title without extension' ),
73 /* A title with no basename */
74 array( '.jpg', null, UploadBase
::MIN_LENGTH_PARTNAME
,
75 'upload title without basename' ),
76 /* A title that is longer than 255 bytes */
77 array( str_repeat( 'a', 255 ) . '.jpg', null, UploadBase
::FILENAME_TOO_LONG
,
78 'upload title longer than 255 bytes' ),
79 /* A title that is longer than 240 bytes */
80 array( str_repeat( 'a', 240 ) . '.jpg', null, UploadBase
::FILENAME_TOO_LONG
,
81 'upload title longer than 240 bytes' ),
86 * Test the upload verification functions
87 * @covers UploadBase::verifyUpload
89 public function testVerifyUpload() {
90 /* Setup with zero file size */
91 $this->upload
->initializePathInfo( '', '', 0 );
92 $result = $this->upload
->verifyUpload();
93 $this->assertEquals( UploadBase
::EMPTY_FILE
,
95 'upload empty file' );
98 // Helper used to create an empty file of size $size.
99 private function createFileOfSize( $size ) {
100 $filename = tempnam( wfTempDir(), "mwuploadtest" );
102 $fh = fopen( $filename, 'w' );
103 ftruncate( $fh, $size );
110 * test uploading a 100 bytes file with $wgMaxUploadSize = 100
112 * This method should be abstracted so we can test different settings.
114 public function testMaxUploadSize() {
115 global $wgMaxUploadSize;
116 $savedGlobal = $wgMaxUploadSize; // save global
117 global $wgFileExtensions;
118 $wgFileExtensions[] = 'txt';
120 $wgMaxUploadSize = 100;
122 $filename = $this->createFileOfSize( $wgMaxUploadSize );
123 $this->upload
->initializePathInfo( basename( $filename ) . '.txt', $filename, 100 );
124 $result = $this->upload
->verifyUpload();
128 array( 'status' => UploadBase
::OK
), $result );
130 $wgMaxUploadSize = $savedGlobal; // restore global
134 class UploadTestHandler
extends UploadBase
{
135 public function initializeFromRequest( &$request ) {
138 public function testTitleValidation( $name ) {
139 $this->mTitle
= false;
140 $this->mDesiredDestName
= $name;
141 $this->mTitleError
= UploadBase
::OK
;
144 return $this->mTitleError
;