blob: 4fbc11ef00b38524ca76f2098bceaf48e0286666 [file] [log] [blame]
Alex Bilbieba1fbfb2012-08-29 23:23:37 +01001<?php
2
3class Upload_test extends CI_TestCase {
4
Andrey Andreev742826c2014-02-21 15:50:36 +02005 public function set_up()
Alex Bilbieba1fbfb2012-08-29 23:23:37 +01006 {
dchill427ecc5cd2012-10-12 16:25:51 -04007 $ci = $this->ci_instance();
Andrey Andreev10e7a322014-02-20 16:42:16 +02008 $ci->upload = new CI_Upload();
dchill427ecc5cd2012-10-12 16:25:51 -04009 $ci->security = new Mock_Core_Security();
dchill421713d322012-11-24 20:23:21 -050010 $ci->lang = $this->getMock('CI_Lang', array('load', 'line'));
11 $ci->lang->expects($this->any())->method('line')->will($this->returnValue(FALSE));
dchill427ecc5cd2012-10-12 16:25:51 -040012 $this->upload = $ci->upload;
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010013 }
14
Andrey Andreev742826c2014-02-21 15:50:36 +020015 // --------------------------------------------------------------------
16
17 public function test___construct_initialize()
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010018 {
Andrey Andreev742826c2014-02-21 15:50:36 +020019 // via __construct
20
21 $upload = new CI_Upload(
22 array(
23 'file_name' => 'foo',
24 'file_ext_tolower' => TRUE
25 )
26 );
27
28 // ReflectionProperty::setAccessible() is not available in PHP 5.2.x
29 if (is_php('5.3'))
30 {
31 $reflection = new ReflectionClass($upload);
32 $reflection = $reflection->getProperty('_file_name_override');
33 $reflection->setAccessible(TRUE);
34 $this->assertEquals('foo', $reflection->getValue($upload));
35 }
36
37 $this->assertTrue($upload->file_ext_tolower);
38
39 // reset (defaults to true)
40
41 $upload->initialize(array('file_name' => 'bar'));
42 $this->assertEquals('bar', $upload->file_name);
43 $this->assertFalse($upload->file_ext_tolower);
44
45 // no reset
46
47 $upload->initialize(array('file_ext_tolower' => TRUE), FALSE);
48 $this->assertTrue($upload->file_ext_tolower);
49 $this->assertEquals('bar', $upload->file_name);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010050 }
51
Andrey Andreev742826c2014-02-21 15:50:36 +020052 // --------------------------------------------------------------------
53
54 public function test_do_upload()
55 {
56 $this->markTestSkipped("We can't test this at the moment because of the call to is_uploaded_file() in do_upload() which isn't supported by vfsStream.");
57 }
58
59 // --------------------------------------------------------------------
60
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010061 function test_data()
62 {
63 $data = array(
64 'file_name' => 'hello.txt',
65 'file_type' => 'text/plain',
66 'file_path' => '/tmp/',
67 'full_path' => '/tmp/hello.txt',
68 'raw_name' => 'hello',
69 'orig_name' => 'hello.txt',
70 'client_name' => '',
71 'file_ext' => '.txt',
72 'file_size' => 100,
73 'is_image' => FALSE,
74 'image_width' => '',
75 'image_height' => '',
76 'image_type' => '',
77 'image_size_str' => ''
78 );
79
80 $this->upload->set_upload_path('/tmp/');
81
82 foreach ($data as $k => $v)
83 {
84 $this->upload->{$k} = $v;
85 }
86
87 $this->assertEquals('hello.txt', $this->upload->data('file_name'));
88 $this->assertEquals($data, $this->upload->data());
89 }
90
91 function test_set_upload_path()
92 {
93 $this->upload->set_upload_path('/tmp/');
94 $this->assertEquals('/tmp/', $this->upload->upload_path);
95
96 $this->upload->set_upload_path('/tmp');
97 $this->assertEquals('/tmp/', $this->upload->upload_path);
98 }
99
100 function test_set_filename()
101 {
dchill427ecc5cd2012-10-12 16:25:51 -0400102 $dir = 'uploads';
103 $isnew = 'helloworld.txt';
104 $exists = 'hello-world.txt';
105 $this->ci_vfs_create($exists, 'Hello world.', $this->ci_app_root, $dir);
106 $path = $this->ci_vfs_path($dir.'/', APPPATH);
Alex Bilbie096b8942012-08-30 00:20:36 +0100107 $this->upload->file_ext = '.txt';
108
dchill427ecc5cd2012-10-12 16:25:51 -0400109 $this->assertEquals($isnew, $this->upload->set_filename($path, $isnew));
110 $this->assertEquals('hello-world1.txt', $this->upload->set_filename($path, $exists));
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100111 }
112
113 function test_set_max_filesize()
114 {
115 $this->upload->set_max_filesize(100);
116 $this->assertEquals(100, $this->upload->max_size);
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +0100117 }
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100118
119 function test_set_max_filename()
120 {
121 $this->upload->set_max_filename(100);
122 $this->assertEquals(100, $this->upload->max_filename);
123 }
124
125 function test_set_max_width()
126 {
127 $this->upload->set_max_width(100);
128 $this->assertEquals(100, $this->upload->max_width);
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +0100129 }
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100130
131 function test_set_max_height()
132 {
133 $this->upload->set_max_height(100);
134 $this->assertEquals(100, $this->upload->max_height);
135 }
136
137 function test_set_allowed_types()
138 {
139 $this->upload->set_allowed_types('*');
140 $this->assertEquals('*', $this->upload->allowed_types);
141
142 $this->upload->set_allowed_types('foo|bar');
143 $this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types);
144 }
145
146 function test_set_image_properties()
147 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100148 $this->upload->file_type = 'image/gif';
dchill427ecc5cd2012-10-12 16:25:51 -0400149 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100150
151 $props = array(
152 'image_width' => 170,
153 'image_height' => 73,
154 'image_type' => 'gif',
155 'image_size_str' => 'width="170" height="73"'
156 );
157
158 $this->upload->set_image_properties($this->upload->file_temp);
159
160 $this->assertEquals($props['image_width'], $this->upload->image_width);
161 $this->assertEquals($props['image_height'], $this->upload->image_height);
162 $this->assertEquals($props['image_type'], $this->upload->image_type);
163 $this->assertEquals($props['image_size_str'], $this->upload->image_size_str);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100164 }
165
166 function test_set_xss_clean()
167 {
168 $this->upload->set_xss_clean(TRUE);
169 $this->assertTrue($this->upload->xss_clean);
170
171 $this->upload->set_xss_clean(FALSE);
172 $this->assertFalse($this->upload->xss_clean);
173 }
174
175 function test_is_image()
176 {
177 $this->upload->file_type = 'image/x-png';
178 $this->assertTrue($this->upload->is_image());
179
180 $this->upload->file_type = 'text/plain';
181 $this->assertFalse($this->upload->is_image());
182 }
183
184 function test_is_allowed_filetype()
185 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100186 $this->upload->allowed_types = array('html', 'gif');
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100187
188 $this->upload->file_ext = '.txt';
189 $this->upload->file_type = 'text/plain';
190 $this->assertFalse($this->upload->is_allowed_filetype(FALSE));
191 $this->assertFalse($this->upload->is_allowed_filetype(TRUE));
192
193 $this->upload->file_ext = '.html';
194 $this->upload->file_type = 'text/html';
195 $this->assertTrue($this->upload->is_allowed_filetype(FALSE));
196 $this->assertTrue($this->upload->is_allowed_filetype(TRUE));
197
dchill427ecc5cd2012-10-12 16:25:51 -0400198 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100199 $this->upload->file_ext = '.gif';
200 $this->upload->file_type = 'image/gif';
201 $this->assertTrue($this->upload->is_allowed_filetype());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100202 }
203
204 function test_is_allowed_filesize()
205 {
206 $this->upload->max_size = 100;
207 $this->upload->file_size = 99;
208
209 $this->assertTrue($this->upload->is_allowed_filesize());
210
211 $this->upload->file_size = 101;
212 $this->assertFalse($this->upload->is_allowed_filesize());
213 }
214
215 function test_is_allowed_dimensions()
216 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100217 $this->upload->file_type = 'text/plain';
218 $this->assertTrue($this->upload->is_allowed_dimensions());
219
220 $this->upload->file_type = 'image/gif';
dchill427ecc5cd2012-10-12 16:25:51 -0400221 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100222
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +0100223 $this->upload->max_width = 10;
Alex Bilbie096b8942012-08-30 00:20:36 +0100224 $this->assertFalse($this->upload->is_allowed_dimensions());
225
226 $this->upload->max_width = 170;
227 $this->upload->max_height = 10;
228 $this->assertFalse($this->upload->is_allowed_dimensions());
229
230 $this->upload->max_height = 73;
231 $this->assertTrue($this->upload->is_allowed_dimensions());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100232 }
233
234 function test_validate_upload_path()
235 {
236 $this->upload->upload_path = '';
237 $this->assertFalse($this->upload->validate_upload_path());
238
dchill427ecc5cd2012-10-12 16:25:51 -0400239 $dir = 'uploads';
240 $this->ci_vfs_mkdir($dir);
241 $this->upload->upload_path = $this->ci_vfs_path($dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100242 $this->assertTrue($this->upload->validate_upload_path());
243 }
244
245 function test_get_extension()
246 {
247 $this->assertEquals('.txt', $this->upload->get_extension('hello.txt'));
248 $this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess'));
249 $this->assertEquals('', $this->upload->get_extension('hello'));
250 }
251
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100252 function test_limit_filename_length()
253 {
254 $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10));
255 $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9));
256 }
257
258 function test_do_xss_clean()
259 {
dchill427ecc5cd2012-10-12 16:25:51 -0400260 $dir = 'uploads';
261 $file1 = 'file1.txt';
262 $file2 = 'file2.txt';
263 $file3 = 'file3.txt';
264 $this->ci_vfs_create($file1, 'The billy goat was waiting for them.', $this->ci_vfs_root, $dir);
265 $this->ci_vfs_create($file2, '', $this->ci_vfs_root, $dir);
266 $this->ci_vfs_create($file3, '<script type="text/javascript">alert("Boo! said the billy goat")</script>', $this->ci_vfs_root, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100267
dchill427ecc5cd2012-10-12 16:25:51 -0400268 $this->upload->file_temp = $this->ci_vfs_path($file1, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100269 $this->assertTrue($this->upload->do_xss_clean());
270
dchill427ecc5cd2012-10-12 16:25:51 -0400271 $this->upload->file_temp = $this->ci_vfs_path($file2, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100272 $this->assertFalse($this->upload->do_xss_clean());
273
dchill427ecc5cd2012-10-12 16:25:51 -0400274 $this->upload->file_temp = $this->ci_vfs_path($file3, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100275 $this->assertFalse($this->upload->do_xss_clean());
Alex Bilbie096b8942012-08-30 00:20:36 +0100276
dchill427ecc5cd2012-10-12 16:25:51 -0400277 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100278 $this->assertTrue($this->upload->do_xss_clean());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100279 }
280
281 function test_set_error()
282 {
283 $errors = array(
284 'An error!'
285 );
286
287 $another = 'Another error!';
288
289 $this->upload->set_error($errors);
290 $this->assertEquals($errors, $this->upload->error_msg);
291
292 $errors[] = $another;
293 $this->upload->set_error($another);
294 $this->assertEquals($errors, $this->upload->error_msg);
295 }
296
297 function test_display_errors()
298 {
299 $this->upload->error_msg[] = 'Error test';
300 $this->assertEquals('<p>Error test</p>', $this->upload->display_errors());
301 }
302
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100303}