(bug 30202) Restrict file names on upload to 240 bytes, because wfTimestamp( TS_MW...
[lhc/web/wiklou.git] / tests / phpunit / includes / upload / UploadTest.php
1 <?php
2 /**
3 * @group Upload
4 */
5 class UploadTest extends MediaWikiTestCase {
6 protected $upload;
7
8
9 function setUp() {
10 global $wgHooks;
11 parent::setUp();
12
13 $this->upload = new UploadTestHandler;
14 $this->hooks = $wgHooks;
15 $wgHooks['InterwikiLoadPrefix'][] = 'MediaWikiTestCase::disableInterwikis';
16 }
17
18 function tearDown() {
19 global $wgHooks;
20 $wgHooks = $this->hooks;
21 }
22
23 /**
24 * Test various forms of valid and invalid titles that can be supplied.
25 */
26 public function testTitleValidation() {
27
28
29 /* Test a valid title */
30 $this->assertUploadTitleAndCode( 'ValidTitle.jpg',
31 'ValidTitle.jpg', UploadBase::OK,
32 'upload valid title' );
33
34 /* A title with a slash */
35 $this->assertUploadTitleAndCode( 'A/B.jpg',
36 'B.jpg', UploadBase::OK,
37 'upload title with slash' );
38
39 /* A title with illegal char */
40 $this->assertUploadTitleAndCode( 'A:B.jpg',
41 'A-B.jpg', UploadBase::OK,
42 'upload title with colon' );
43
44 /* Stripping leading File: prefix */
45 $this->assertUploadTitleAndCode( 'File:C.jpg',
46 'C.jpg', UploadBase::OK,
47 'upload title with File prefix' );
48
49 /* Test illegal suggested title (r94601) */
50 $this->assertUploadTitleAndCode( '%281%29.JPG',
51 null, UploadBase::ILLEGAL_FILENAME,
52 'illegal title for upload' );
53
54 /* A title without extension */
55 $this->assertUploadTitleAndCode( 'A',
56 null, UploadBase::FILETYPE_MISSING,
57 'upload title without extension' );
58
59 /* A title with no basename */
60 $this->assertUploadTitleAndCode( '.jpg',
61 null, UploadBase::MIN_LENGTH_PARTNAME,
62 'upload title without basename' );
63
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' );
68
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' );
73
74 }
75 /**
76 * Helper function for testTitleValidation. First checks the return code
77 * of UploadBase::getTitle() and then the actual returned titl
78 */
79 private function assertUploadTitleAndCode( $srcFilename, $dstFilename, $code, $msg ) {
80 /* Check the result code */
81 $this->assertEquals( $code,
82 $this->upload->testTitleValidation( $srcFilename ),
83 "$msg code" );
84
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(),
89 "$msg text" );
90 }
91 }
92
93 /**
94 * Test the upload verification functions
95 */
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,
101 $result['status'],
102 'upload empty file' );
103 }
104
105 // Helper used to create an empty file of size $size.
106 private function createFileOfSize( $size ) {
107 $filename = tempnam( wfTempDir(), "mwuploadtest" );
108
109 $fh = fopen( $filename, 'w' );
110 ftruncate( $fh, $size );
111 fclose( $fh );
112
113 return $filename;
114 }
115
116 /**
117 * test uploading a 100 bytes file with wgMaxUploadSize = 100
118 *
119 * This method should be abstracted so we can test different settings.
120 */
121
122 public function testMaxUploadSize() {
123 global $wgMaxUploadSize;
124 $savedGlobal = $wgMaxUploadSize; // save global
125 global $wgFileExtensions;
126 $wgFileExtensions[] = 'txt';
127
128 $wgMaxUploadSize = 100;
129
130 $filename = $this->createFileOfSize( $wgMaxUploadSize );
131 $this->upload->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
132 $result = $this->upload->verifyUpload();
133 unlink( $filename );
134
135 $this->assertEquals(
136 array( 'status' => UploadBase::OK ), $result );
137
138 $wgMaxUploadSize = $savedGlobal; // restore global
139 }
140 }
141
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;
148 $this->getTitle();
149 return $this->mTitleError;
150 }
151
152
153 }