5 class UploadTest
extends MediaWikiTestCase
{
13 $this->upload
= new UploadTestHandler
;
14 $this->hooks
= $wgHooks;
15 $wgHooks['InterwikiLoadPrefix'][] = 'MediaWikiTestCase::disableInterwikis';
20 $wgHooks = $this->hooks
;
24 * Test various forms of valid and invalid titles that can be supplied.
26 public function testTitleValidation() {
29 /* Test a valid title */
30 $this->assertUploadTitleAndCode( 'ValidTitle.jpg',
31 'ValidTitle.jpg', UploadBase
::OK
,
32 'upload valid title' );
34 /* A title with a slash */
35 $this->assertUploadTitleAndCode( 'A/B.jpg',
36 'B.jpg', UploadBase
::OK
,
37 'upload title with slash' );
39 /* A title with illegal char */
40 $this->assertUploadTitleAndCode( 'A:B.jpg',
41 'A-B.jpg', UploadBase
::OK
,
42 'upload title with colon' );
44 /* Stripping leading File: prefix */
45 $this->assertUploadTitleAndCode( 'File:C.jpg',
46 'C.jpg', UploadBase
::OK
,
47 'upload title with File prefix' );
49 /* Test illegal suggested title (r94601) */
50 $this->assertUploadTitleAndCode( '%281%29.JPG',
51 null, UploadBase
::ILLEGAL_FILENAME
,
52 'illegal title for upload' );
54 /* A title without extension */
55 $this->assertUploadTitleAndCode( 'A',
56 null, UploadBase
::FILETYPE_MISSING
,
57 'upload title without extension' );
59 /* A title with no basename */
60 $this->assertUploadTitleAndCode( '.jpg',
61 null, UploadBase
::MIN_LENGTH_PARTNAME
,
62 'upload title without basename' );
64 /* A title that is longer than 255 bytes */
65 $this->assertUploadTitleAndCode( str_repeat( 'a', 255 ) . '.jpg',
66 null, UploadBase
::ILLEGAL_FILENAME
,
67 'upload title longer than 255 bytes' );
69 /* A title that is longer than 240 bytes */
70 $this->assertUploadTitleAndCode( str_repeat( 'a', 240 ) . '.jpg',
71 null, UploadBase
::ILLEGAL_FILENAME
,
72 'upload title longer than 240 bytes' );
76 * Helper function for testTitleValidation. First checks the return code
77 * of UploadBase::getTitle() and then the actual returned titl
79 private function assertUploadTitleAndCode( $srcFilename, $dstFilename, $code, $msg ) {
80 /* Check the result code */
81 $this->assertEquals( $code,
82 $this->upload
->testTitleValidation( $srcFilename ),
85 /* If we expect a valid title, check the title itself. */
86 if ( $code == UploadBase
::OK
) {
87 $this->assertEquals( $dstFilename,
88 $this->upload
->getTitle()->getText(),
94 * Test the upload verification functions
96 public function testVerifyUpload() {
97 /* Setup with zero file size */
98 $this->upload
->initializePathInfo( '', '', 0 );
99 $result = $this->upload
->verifyUpload();
100 $this->assertEquals( UploadBase
::EMPTY_FILE
,
102 'upload empty file' );
105 // Helper used to create an empty file of size $size.
106 private function createFileOfSize( $size ) {
107 $filename = tempnam( wfTempDir(), "mwuploadtest" );
109 $fh = fopen( $filename, 'w' );
110 ftruncate( $fh, $size );
117 * test uploading a 100 bytes file with wgMaxUploadSize = 100
119 * This method should be abstracted so we can test different settings.
122 public function testMaxUploadSize() {
123 global $wgMaxUploadSize;
124 $savedGlobal = $wgMaxUploadSize; // save global
125 global $wgFileExtensions;
126 $wgFileExtensions[] = 'txt';
128 $wgMaxUploadSize = 100;
130 $filename = $this->createFileOfSize( $wgMaxUploadSize );
131 $this->upload
->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
132 $result = $this->upload
->verifyUpload();
136 array( 'status' => UploadBase
::OK
), $result );
138 $wgMaxUploadSize = $savedGlobal; // restore global
142 class UploadTestHandler
extends UploadBase
{
143 public function initializeFromRequest( &$request ) { }
144 public function testTitleValidation( $name ) {
145 $this->mTitle
= false;
146 $this->mDesiredDestName
= $name;
147 $this->mTitleError
= UploadBase
::OK
;
149 return $this->mTitleError
;