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