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