MediaWiki  REL1_19
UploadTest.php
Go to the documentation of this file.
00001 <?php
00005 class UploadTest extends MediaWikiTestCase {
00006         protected $upload;
00007 
00008 
00009         function setUp() {
00010                 global $wgHooks;
00011                 parent::setUp();
00012 
00013                 $this->upload = new UploadTestHandler;
00014                 $this->hooks = $wgHooks;
00015                 $wgHooks['InterwikiLoadPrefix'][] = 'MediaWikiTestCase::disableInterwikis';
00016         }
00017 
00018         function tearDown() {
00019                 global $wgHooks;
00020                 $wgHooks = $this->hooks;
00021         }
00022 
00023 
00030         public function testTitleValidation( $srcFilename, $dstFilename, $code, $msg ) {
00031                 /* Check the result code */
00032                 $this->assertEquals( $code,
00033                         $this->upload->testTitleValidation( $srcFilename ),
00034                         "$msg code" );
00035 
00036                 /* If we expect a valid title, check the title itself. */
00037                 if ( $code == UploadBase::OK ) {
00038                         $this->assertEquals( $dstFilename,
00039                                 $this->upload->getTitle()->getText(),
00040                                 "$msg text" );
00041                 }
00042         }
00043         
00047         public function dataTestTitleValidation() {
00048                 return array(
00049                         /* Test a valid title */
00050                         array( 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase::OK, 
00051                                 'upload valid title' ),
00052                         /* A title with a slash */
00053                         array( 'A/B.jpg', 'B.jpg', UploadBase::OK, 
00054                                 'upload title with slash' ),
00055                         /* A title with illegal char */
00056                         array( 'A:B.jpg', 'A-B.jpg', UploadBase::OK, 
00057                                 'upload title with colon' ),
00058                         /* Stripping leading File: prefix */
00059                         array( 'File:C.jpg', 'C.jpg', UploadBase::OK, 
00060                                 'upload title with File prefix' ),
00061                         /* Test illegal suggested title (r94601) */
00062                         array( '%281%29.JPG', null, UploadBase::ILLEGAL_FILENAME, 
00063                                 'illegal title for upload' ),
00064                         /* A title without extension */
00065                         array( 'A', null, UploadBase::FILETYPE_MISSING, 
00066                                 'upload title without extension' ),
00067                         /* A title with no basename */
00068                         array( '.jpg', null, UploadBase::MIN_LENGTH_PARTNAME, 
00069                                 'upload title without basename' ),
00070                         /* A title that is longer than 255 bytes */
00071                         array( str_repeat( 'a', 255 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG, 
00072                                 'upload title longer than 255 bytes' ),
00073                         /* A title that is longer than 240 bytes */
00074                         array( str_repeat( 'a', 240 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG, 
00075                                 'upload title longer than 240 bytes' ),
00076                 );
00077         }
00078 
00082         public function testVerifyUpload() {
00083                 /* Setup with zero file size */
00084                 $this->upload->initializePathInfo( '', '', 0 );
00085                 $result = $this->upload->verifyUpload();
00086                 $this->assertEquals( UploadBase::EMPTY_FILE,
00087                         $result['status'],
00088                         'upload empty file' );
00089         }
00090 
00091         // Helper used to create an empty file of size $size.
00092         private function createFileOfSize( $size ) {
00093                 $filename = tempnam( wfTempDir(), "mwuploadtest" );
00094 
00095                 $fh = fopen( $filename, 'w' );
00096                 ftruncate( $fh, $size );
00097                 fclose( $fh );
00098 
00099                 return $filename;
00100         }
00101 
00108         public function testMaxUploadSize() {
00109                 global $wgMaxUploadSize;
00110                 $savedGlobal = $wgMaxUploadSize; // save global
00111                 global $wgFileExtensions;
00112                 $wgFileExtensions[] = 'txt';
00113 
00114                 $wgMaxUploadSize = 100;
00115 
00116                 $filename = $this->createFileOfSize( $wgMaxUploadSize );
00117                 $this->upload->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
00118                 $result = $this->upload->verifyUpload();
00119                 unlink( $filename );
00120 
00121                 $this->assertEquals(
00122                         array( 'status' => UploadBase::OK ), $result );
00123 
00124                 $wgMaxUploadSize = $savedGlobal;  // restore global
00125         }
00126 
00127 }
00128 
00129 class UploadTestHandler extends UploadBase {
00130                 public function initializeFromRequest( &$request ) { }
00131                 public function testTitleValidation( $name ) {
00132                         $this->mTitle = false;
00133                         $this->mDesiredDestName = $name;
00134                         $this->mTitleError = UploadBase::OK;
00135                         $this->getTitle();
00136                         return $this->mTitleError;
00137                 }
00138 
00139 }