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