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