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