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
;
31 * First checks the return code
32 * of UploadBase::getTitle() and then the actual returned title
34 * @dataProvider provideTestTitleValidation
35 * @covers UploadBase::getTitle
37 public function testTitleValidation( $srcFilename, $dstFilename, $code, $msg ) {
38 /* Check the result code */
39 $this->assertEquals( $code,
40 $this->upload
->testTitleValidation( $srcFilename ),
43 /* If we expect a valid title, check the title itself. */
44 if ( $code == UploadBase
::OK
) {
45 $this->assertEquals( $dstFilename,
46 $this->upload
->getTitle()->getText(),
52 * Test various forms of valid and invalid titles that can be supplied.
54 public static function provideTestTitleValidation() {
56 /* Test a valid title */
57 array( 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase
::OK
,
58 'upload valid title' ),
59 /* A title with a slash */
60 array( 'A/B.jpg', 'B.jpg', UploadBase
::OK
,
61 'upload title with slash' ),
62 /* A title with illegal char */
63 array( 'A:B.jpg', 'A-B.jpg', UploadBase
::OK
,
64 'upload title with colon' ),
65 /* Stripping leading File: prefix */
66 array( 'File:C.jpg', 'C.jpg', UploadBase
::OK
,
67 'upload title with File prefix' ),
68 /* Test illegal suggested title (r94601) */
69 array( '%281%29.JPG', null, UploadBase
::ILLEGAL_FILENAME
,
70 'illegal title for upload' ),
71 /* A title without extension */
72 array( 'A', null, UploadBase
::FILETYPE_MISSING
,
73 'upload title without extension' ),
74 /* A title with no basename */
75 array( '.jpg', null, UploadBase
::MIN_LENGTH_PARTNAME
,
76 'upload title without basename' ),
77 /* A title that is longer than 255 bytes */
78 array( str_repeat( 'a', 255 ) . '.jpg', null, UploadBase
::FILENAME_TOO_LONG
,
79 'upload title longer than 255 bytes' ),
80 /* A title that is longer than 240 bytes */
81 array( str_repeat( 'a', 240 ) . '.jpg', null, UploadBase
::FILENAME_TOO_LONG
,
82 'upload title longer than 240 bytes' ),
87 * Test the upload verification functions
88 * @covers UploadBase::verifyUpload
90 public function testVerifyUpload() {
91 /* Setup with zero file size */
92 $this->upload
->initializePathInfo( '', '', 0 );
93 $result = $this->upload
->verifyUpload();
94 $this->assertEquals( UploadBase
::EMPTY_FILE
,
96 'upload empty file' );
99 // Helper used to create an empty file of size $size.
100 private function createFileOfSize( $size ) {
101 $filename = tempnam( wfTempDir(), "mwuploadtest" );
103 $fh = fopen( $filename, 'w' );
104 ftruncate( $fh, $size );
111 * test uploading a 100 bytes file with $wgMaxUploadSize = 100
113 * This method should be abstracted so we can test different settings.
115 public function testMaxUploadSize() {
116 global $wgMaxUploadSize;
117 $savedGlobal = $wgMaxUploadSize; // save global
118 global $wgFileExtensions;
119 $wgFileExtensions[] = 'txt';
121 $wgMaxUploadSize = 100;
123 $filename = $this->createFileOfSize( $wgMaxUploadSize );
124 $this->upload
->initializePathInfo( basename( $filename ) . '.txt', $filename, 100 );
125 $result = $this->upload
->verifyUpload();
129 array( 'status' => UploadBase
::OK
), $result );
131 $wgMaxUploadSize = $savedGlobal; // restore global
135 class UploadTestHandler
extends UploadBase
{
136 public function initializeFromRequest( &$request ) {
139 public function testTitleValidation( $name ) {
140 $this->mTitle
= false;
141 $this->mDesiredDestName
= $name;
142 $this->mTitleError
= UploadBase
::OK
;
145 return $this->mTitleError
;