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 /* A title without extension */
45 $this->assertUploadTitleAndCode( 'A',
46 null, UploadBase
::FILETYPE_MISSING
,
47 'upload title without extension' );
49 /* A title with no basename */
50 $this->assertUploadTitleAndCode( '.jpg',
51 null, UploadBase
::MIN_LENGTH_PARTNAME
,
52 'upload title without basename' );
56 * Helper function for testTitleValidation. First checks the return code
57 * of UploadBase::getTitle() and then the actual returned titl
59 private function assertUploadTitleAndCode( $srcFilename, $dstFilename, $code, $msg ) {
60 /* Check the result code */
61 $this->assertEquals( $code,
62 $this->upload
->testTitleValidation( $srcFilename ),
65 /* If we expect a valid title, check the title itself. */
66 if ( $code == UploadBase
::OK
) {
67 $this->assertEquals( $dstFilename,
68 $this->upload
->getTitle()->getText(),
74 * Test the upload verification functions
76 public function testVerifyUpload() {
77 /* Setup with zero file size */
78 $this->upload
->initializePathInfo( '', '', 0 );
79 $result = $this->upload
->verifyUpload();
80 $this->assertEquals( UploadBase
::EMPTY_FILE
,
82 'upload empty file' );
85 // Helper used to create an empty file of size $size.
86 private function createFileOfSize( $size ) {
87 $filename = tempnam( wfTempDir(), "mwuploadtest" );
89 $fh = fopen( $filename, 'w' );
90 ftruncate( $fh, $size );
97 * test uploading a 100 bytes file with wgMaxUploadSize = 100
99 * This method should be abstracted so we can test different settings.
102 public function testMaxUploadSize() {
103 global $wgMaxUploadSize;
104 $savedGlobal = $wgMaxUploadSize; // save global
105 global $wgFileExtensions;
106 $wgFileExtensions[] = 'txt';
108 $wgMaxUploadSize = 100;
110 $filename = $this->createFileOfSize( $wgMaxUploadSize );
111 $this->upload
->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
112 $result = $this->upload
->verifyUpload();
116 array( 'status' => UploadBase
::OK
), $result );
118 $wgMaxUploadSize = $savedGlobal; // restore global
122 class UploadTestHandler
extends UploadBase
{
123 public function initializeFromRequest( &$request ) { }
124 public function testTitleValidation( $name ) {
125 $this->mTitle
= false;
126 $this->mDesiredDestName
= $name;
128 return $this->mTitleError
;