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