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