(bug 42915) make MovePage aware of whether redirects are supported.
[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 protected function setUp() {
10 global $wgHooks;
11 parent::setUp();
12
13 $this->upload = new UploadTestHandler;
14 $this->hooks = $wgHooks;
15 $wgHooks['InterwikiLoadPrefix'][] = function( $prefix, &$data ) {
16 return false;
17 };
18 }
19
20 protected function tearDown() {
21 global $wgHooks;
22 $wgHooks = $this->hooks;
23 }
24
25
26 /**
27 * First checks the return code
28 * of UploadBase::getTitle() and then the actual returned title
29 *
30 * @dataProvider provideTestTitleValidation
31 */
32 public function testTitleValidation( $srcFilename, $dstFilename, $code, $msg ) {
33 /* Check the result code */
34 $this->assertEquals( $code,
35 $this->upload->testTitleValidation( $srcFilename ),
36 "$msg code" );
37
38 /* If we expect a valid title, check the title itself. */
39 if ( $code == UploadBase::OK ) {
40 $this->assertEquals( $dstFilename,
41 $this->upload->getTitle()->getText(),
42 "$msg text" );
43 }
44 }
45
46 /**
47 * Test various forms of valid and invalid titles that can be supplied.
48 */
49 public static function provideTestTitleValidation() {
50 return array(
51 /* Test a valid title */
52 array( 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase::OK,
53 'upload valid title' ),
54 /* A title with a slash */
55 array( 'A/B.jpg', 'B.jpg', UploadBase::OK,
56 'upload title with slash' ),
57 /* A title with illegal char */
58 array( 'A:B.jpg', 'A-B.jpg', UploadBase::OK,
59 'upload title with colon' ),
60 /* Stripping leading File: prefix */
61 array( 'File:C.jpg', 'C.jpg', UploadBase::OK,
62 'upload title with File prefix' ),
63 /* Test illegal suggested title (r94601) */
64 array( '%281%29.JPG', null, UploadBase::ILLEGAL_FILENAME,
65 'illegal title for upload' ),
66 /* A title without extension */
67 array( 'A', null, UploadBase::FILETYPE_MISSING,
68 'upload title without extension' ),
69 /* A title with no basename */
70 array( '.jpg', null, UploadBase::MIN_LENGTH_PARTNAME,
71 'upload title without basename' ),
72 /* A title that is longer than 255 bytes */
73 array( str_repeat( 'a', 255 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
74 'upload title longer than 255 bytes' ),
75 /* A title that is longer than 240 bytes */
76 array( str_repeat( 'a', 240 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
77 'upload title longer than 240 bytes' ),
78 );
79 }
80
81 /**
82 * Test the upload verification functions
83 */
84 public function testVerifyUpload() {
85 /* Setup with zero file size */
86 $this->upload->initializePathInfo( '', '', 0 );
87 $result = $this->upload->verifyUpload();
88 $this->assertEquals( UploadBase::EMPTY_FILE,
89 $result['status'],
90 'upload empty file' );
91 }
92
93 // Helper used to create an empty file of size $size.
94 private function createFileOfSize( $size ) {
95 $filename = tempnam( wfTempDir(), "mwuploadtest" );
96
97 $fh = fopen( $filename, 'w' );
98 ftruncate( $fh, $size );
99 fclose( $fh );
100
101 return $filename;
102 }
103
104 /**
105 * test uploading a 100 bytes file with $wgMaxUploadSize = 100
106 *
107 * This method should be abstracted so we can test different settings.
108 */
109
110 public function testMaxUploadSize() {
111 global $wgMaxUploadSize;
112 $savedGlobal = $wgMaxUploadSize; // save global
113 global $wgFileExtensions;
114 $wgFileExtensions[] = 'txt';
115
116 $wgMaxUploadSize = 100;
117
118 $filename = $this->createFileOfSize( $wgMaxUploadSize );
119 $this->upload->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
120 $result = $this->upload->verifyUpload();
121 unlink( $filename );
122
123 $this->assertEquals(
124 array( 'status' => UploadBase::OK ), $result );
125
126 $wgMaxUploadSize = $savedGlobal; // restore global
127 }
128 }
129
130 class UploadTestHandler extends UploadBase {
131 public function initializeFromRequest( &$request ) { }
132 public function testTitleValidation( $name ) {
133 $this->mTitle = false;
134 $this->mDesiredDestName = $name;
135 $this->mTitleError = UploadBase::OK;
136 $this->getTitle();
137 return $this->mTitleError;
138 }
139
140
141 }