summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/libraries/Upload_test.php
blob: a953d83298b4aab543573d69ed96588f30b7163f (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php

class Upload_test extends CI_TestCase {

	function set_up()
	{
		$ci = $this->ci_instance();
		$ci->upload = new CI_Upload();
		$ci->security = new Mock_Core_Security();
		$ci->lang = $this->getMock('CI_Lang', array('load', 'line'));
		$ci->lang->expects($this->any())->method('line')->will($this->returnValue(FALSE));
		$this->upload = $ci->upload;
	}

	function test_do_upload()
	{
		$this->markTestSkipped('We can\'t really test this at the moment because of the call to `is_uploaded_file` in do_upload which isn\'t supported by vfsStream');
	}

	function test_data()
	{
		$data = array(
				'file_name'		=> 'hello.txt',
				'file_type'		=> 'text/plain',
				'file_path'		=> '/tmp/',
				'full_path'		=> '/tmp/hello.txt',
				'raw_name'		=> 'hello',
				'orig_name'		=> 'hello.txt',
				'client_name'		=> '',
				'file_ext'		=> '.txt',
				'file_size'		=> 100,
				'is_image'		=> FALSE,
				'image_width'		=> '',
				'image_height'		=> '',
				'image_type'		=> '',
				'image_size_str'	=> ''
			);

		$this->upload->set_upload_path('/tmp/');

		foreach ($data as $k => $v)
		{
			$this->upload->{$k}	= $v;
		}

		$this->assertEquals('hello.txt', $this->upload->data('file_name'));
		$this->assertEquals($data, $this->upload->data());
	}

	function test_set_upload_path()
	{
		$this->upload->set_upload_path('/tmp/');
		$this->assertEquals('/tmp/', $this->upload->upload_path);

		$this->upload->set_upload_path('/tmp');
		$this->assertEquals('/tmp/', $this->upload->upload_path);
	}

	function test_set_filename()
	{
		$dir = 'uploads';
		$isnew = 'helloworld.txt';
		$exists = 'hello-world.txt';
		$this->ci_vfs_create($exists, 'Hello world.', $this->ci_app_root, $dir);
		$path = $this->ci_vfs_path($dir.'/', APPPATH);
		$this->upload->file_ext = '.txt';

		$this->assertEquals($isnew, $this->upload->set_filename($path, $isnew));
		$this->assertEquals('hello-world1.txt', $this->upload->set_filename($path, $exists));
	}

	function test_set_max_filesize()
	{
		$this->upload->set_max_filesize(100);
		$this->assertEquals(100, $this->upload->max_size);
	}

	function test_set_max_filename()
	{
		$this->upload->set_max_filename(100);
		$this->assertEquals(100, $this->upload->max_filename);
	}

	function test_set_max_width()
	{
		$this->upload->set_max_width(100);
		$this->assertEquals(100, $this->upload->max_width);
	}

	function test_set_max_height()
	{
		$this->upload->set_max_height(100);
		$this->assertEquals(100, $this->upload->max_height);
	}

	function test_set_allowed_types()
	{
		$this->upload->set_allowed_types('*');
		$this->assertEquals('*', $this->upload->allowed_types);

		$this->upload->set_allowed_types('foo|bar');
		$this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types);
	}

	function test_set_image_properties()
	{
		$this->upload->file_type = 'image/gif';
		$this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');

		$props = array(
			'image_width'	=>	170,
			'image_height'	=>	73,
			'image_type'	=>	'gif',
			'image_size_str'	=>	'width="170" height="73"'
		);

		$this->upload->set_image_properties($this->upload->file_temp);

		$this->assertEquals($props['image_width'], $this->upload->image_width);
		$this->assertEquals($props['image_height'], $this->upload->image_height);
		$this->assertEquals($props['image_type'], $this->upload->image_type);
		$this->assertEquals($props['image_size_str'], $this->upload->image_size_str);
	}

	function test_set_xss_clean()
	{
		$this->upload->set_xss_clean(TRUE);
		$this->assertTrue($this->upload->xss_clean);

		$this->upload->set_xss_clean(FALSE);
		$this->assertFalse($this->upload->xss_clean);
	}

	function test_is_image()
	{
		$this->upload->file_type = 'image/x-png';
		$this->assertTrue($this->upload->is_image());

		$this->upload->file_type = 'text/plain';
		$this->assertFalse($this->upload->is_image());
	}

	function test_is_allowed_filetype()
	{
		$this->upload->allowed_types = array('html', 'gif');

		$this->upload->file_ext = '.txt';
		$this->upload->file_type = 'text/plain';
		$this->assertFalse($this->upload->is_allowed_filetype(FALSE));
		$this->assertFalse($this->upload->is_allowed_filetype(TRUE));

		$this->upload->file_ext = '.html';
		$this->upload->file_type = 'text/html';
		$this->assertTrue($this->upload->is_allowed_filetype(FALSE));
		$this->assertTrue($this->upload->is_allowed_filetype(TRUE));

		$this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
		$this->upload->file_ext = '.gif';
		$this->upload->file_type = 'image/gif';
		$this->assertTrue($this->upload->is_allowed_filetype());
	}

	function test_is_allowed_filesize()
	{
		$this->upload->max_size = 100;
		$this->upload->file_size = 99;

		$this->assertTrue($this->upload->is_allowed_filesize());

		$this->upload->file_size = 101;
		$this->assertFalse($this->upload->is_allowed_filesize());
	}

	function test_is_allowed_dimensions()
	{
		$this->upload->file_type = 'text/plain';
		$this->assertTrue($this->upload->is_allowed_dimensions());

		$this->upload->file_type = 'image/gif';
		$this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');

		$this->upload->max_width = 10;
		$this->assertFalse($this->upload->is_allowed_dimensions());

		$this->upload->max_width = 170;
		$this->upload->max_height = 10;
		$this->assertFalse($this->upload->is_allowed_dimensions());

		$this->upload->max_height = 73;
		$this->assertTrue($this->upload->is_allowed_dimensions());
	}

	function test_validate_upload_path()
	{
		$this->upload->upload_path = '';
		$this->assertFalse($this->upload->validate_upload_path());

		$dir = 'uploads';
		$this->ci_vfs_mkdir($dir);
		$this->upload->upload_path = $this->ci_vfs_path($dir);
		$this->assertTrue($this->upload->validate_upload_path());
	}

	function test_get_extension()
	{
		$this->assertEquals('.txt', $this->upload->get_extension('hello.txt'));
		$this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess'));
		$this->assertEquals('', $this->upload->get_extension('hello'));
	}

	function test_limit_filename_length()
	{
		$this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10));
		$this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9));
	}

	function test_do_xss_clean()
	{
		$dir = 'uploads';
		$file1 = 'file1.txt';
		$file2 = 'file2.txt';
		$file3 = 'file3.txt';
		$this->ci_vfs_create($file1, 'The billy goat was waiting for them.', $this->ci_vfs_root, $dir);
		$this->ci_vfs_create($file2, '', $this->ci_vfs_root, $dir);
		$this->ci_vfs_create($file3, '<script type="text/javascript">alert("Boo! said the billy goat")</script>', $this->ci_vfs_root, $dir);

		$this->upload->file_temp = $this->ci_vfs_path($file1, $dir);
		$this->assertTrue($this->upload->do_xss_clean());

		$this->upload->file_temp = $this->ci_vfs_path($file2, $dir);
		$this->assertFalse($this->upload->do_xss_clean());

		$this->upload->file_temp = $this->ci_vfs_path($file3, $dir);
		$this->assertFalse($this->upload->do_xss_clean());

		$this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
		$this->assertTrue($this->upload->do_xss_clean());
	}

	function test_set_error()
	{
		$errors = array(
			'An error!'
		);

		$another = 'Another error!';

		$this->upload->set_error($errors);
		$this->assertEquals($errors, $this->upload->error_msg);

		$errors[] = $another;
		$this->upload->set_error($another);
		$this->assertEquals($errors, $this->upload->error_msg);
	}

	function test_display_errors()
	{
		$this->upload->error_msg[] = 'Error test';
		$this->assertEquals('<p>Error test</p>', $this->upload->display_errors());
	}

	function test_mimes_types()
	{
		$this->assertEquals('text/plain', $this->upload->mimes_types('txt'));
		$this->assertFalse($this->upload->mimes_types('foobar'));
	}

}