summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/upload/UploadTest.php
blob: 69c29032941fe0ac943f43189c7f48cea9b2034a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
 * @group Upload
 */
class UploadTest extends MediaWikiTestCase {
	protected $upload;


	function setUp() {
		global $wgHooks;
		parent::setUp();

		$this->upload = new UploadTestHandler;
		$this->hooks = $wgHooks;
		$wgHooks['InterwikiLoadPrefix'][] = 'MediaWikiTestCase::disableInterwikis';
	}

	function tearDown() {
		global $wgHooks;
		$wgHooks = $this->hooks;
	}

	/**
	 * Test various forms of valid and invalid titles that can be supplied.
	 */
	public function testTitleValidation() {


		/* Test a valid title */
		$this->assertUploadTitleAndCode( 'ValidTitle.jpg',
			'ValidTitle.jpg', UploadBase::OK,
			'upload valid title' );

		/* A title with a slash */
		$this->assertUploadTitleAndCode( 'A/B.jpg',
			'B.jpg', UploadBase::OK,
			'upload title with slash' );

		/* A title with illegal char */
		$this->assertUploadTitleAndCode( 'A:B.jpg',
			'A-B.jpg', UploadBase::OK,
			'upload title with colon' );

		/* Stripping leading File: prefix */
		$this->assertUploadTitleAndCode( 'File:C.jpg',
			'C.jpg', UploadBase::OK,
			'upload title with File prefix' );

		/* Test illegal suggested title (r94601) */
		$this->assertUploadTitleAndCode( '%281%29.JPG',
			null, UploadBase::ILLEGAL_FILENAME,
			'illegal title for upload' );

		/* A title without extension */
		$this->assertUploadTitleAndCode( 'A',
			null, UploadBase::FILETYPE_MISSING,
			'upload title without extension' );

		/* A title with no basename */
		$this->assertUploadTitleAndCode( '.jpg',
			null, UploadBase::MIN_LENGTH_PARTNAME,
			'upload title without basename' );

	}
	/**
	 * Helper function for testTitleValidation. First checks the return code
	 * of UploadBase::getTitle() and then the actual returned titl
	 */
	private function assertUploadTitleAndCode( $srcFilename, $dstFilename, $code, $msg ) {
		/* Check the result code */
		$this->assertEquals( $code,
			$this->upload->testTitleValidation( $srcFilename ),
			"$msg code" );

		/* If we expect a valid title, check the title itself. */
		if ( $code == UploadBase::OK ) {
			$this->assertEquals( $dstFilename,
				$this->upload->getTitle()->getText(),
				"$msg text" );
		}
	}

	/**
	 * Test the upload verification functions
	 */
	public function testVerifyUpload() {
		/* Setup with zero file size */
		$this->upload->initializePathInfo( '', '', 0 );
		$result = $this->upload->verifyUpload();
		$this->assertEquals( UploadBase::EMPTY_FILE,
			$result['status'],
			'upload empty file' );
	}

	// Helper used to create an empty file of size $size.
	private function createFileOfSize( $size ) {
		$filename = tempnam( wfTempDir(), "mwuploadtest" );

		$fh = fopen( $filename, 'w' );
		ftruncate( $fh, $size );
		fclose( $fh );

		return $filename;
	}

	/**
	 * test uploading a 100 bytes file with wgMaxUploadSize = 100
	 *
	 * This method should be abstracted so we can test different settings.
	 */

	public function testMaxUploadSize() {
		global $wgMaxUploadSize;
		$savedGlobal = $wgMaxUploadSize; // save global
		global $wgFileExtensions;
		$wgFileExtensions[] = 'txt';

		$wgMaxUploadSize = 100;

		$filename = $this->createFileOfSize( $wgMaxUploadSize );
		$this->upload->initializePathInfo( basename($filename) . '.txt', $filename, 100 );
		$result = $this->upload->verifyUpload();
		unlink( $filename );

		$this->assertEquals(
			array( 'status' => UploadBase::OK ), $result );

		$wgMaxUploadSize = $savedGlobal;  // restore global
	}
}

class UploadTestHandler extends UploadBase {
		public function initializeFromRequest( &$request ) { }
		public function testTitleValidation( $name ) {
			$this->mTitle = false;
			$this->mDesiredDestName = $name;
			$this->getTitle();
			return $this->mTitleError;
		}


}