blob: 8bac597b3da558e514f8b0b5bcdfe8f1de4a9a62 [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();
Andrey Andreev878e23f2016-08-10 15:15:49 +030010 $ci->lang = $this->getMockBuilder('CI_Lang')->setMethods(array('load', 'line'))->getMock();
dchill421713d322012-11-24 20:23:21 -050011 $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
Andrey Andreeva8382792016-07-28 16:40:12 +030028 $reflection = new ReflectionClass($upload);
29 $reflection = $reflection->getProperty('_file_name_override');
30 $reflection->setAccessible(TRUE);
31 $this->assertEquals('foo', $reflection->getValue($upload));
Andrey Andreev742826c2014-02-21 15:50:36 +020032
33 $this->assertTrue($upload->file_ext_tolower);
34
35 // reset (defaults to true)
36
37 $upload->initialize(array('file_name' => 'bar'));
38 $this->assertEquals('bar', $upload->file_name);
39 $this->assertFalse($upload->file_ext_tolower);
40
41 // no reset
42
43 $upload->initialize(array('file_ext_tolower' => TRUE), FALSE);
44 $this->assertTrue($upload->file_ext_tolower);
45 $this->assertEquals('bar', $upload->file_name);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010046 }
47
Andrey Andreev742826c2014-02-21 15:50:36 +020048 // --------------------------------------------------------------------
49
50 public function test_do_upload()
51 {
52 $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.");
53 }
54
55 // --------------------------------------------------------------------
56
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010057 function test_data()
58 {
59 $data = array(
60 'file_name' => 'hello.txt',
61 'file_type' => 'text/plain',
62 'file_path' => '/tmp/',
63 'full_path' => '/tmp/hello.txt',
64 'raw_name' => 'hello',
65 'orig_name' => 'hello.txt',
66 'client_name' => '',
67 'file_ext' => '.txt',
68 'file_size' => 100,
69 'is_image' => FALSE,
70 'image_width' => '',
71 'image_height' => '',
72 'image_type' => '',
73 'image_size_str' => ''
74 );
75
76 $this->upload->set_upload_path('/tmp/');
77
78 foreach ($data as $k => $v)
79 {
80 $this->upload->{$k} = $v;
81 }
82
83 $this->assertEquals('hello.txt', $this->upload->data('file_name'));
84 $this->assertEquals($data, $this->upload->data());
85 }
86
87 function test_set_upload_path()
88 {
89 $this->upload->set_upload_path('/tmp/');
90 $this->assertEquals('/tmp/', $this->upload->upload_path);
91
92 $this->upload->set_upload_path('/tmp');
93 $this->assertEquals('/tmp/', $this->upload->upload_path);
94 }
95
96 function test_set_filename()
97 {
dchill427ecc5cd2012-10-12 16:25:51 -040098 $dir = 'uploads';
99 $isnew = 'helloworld.txt';
100 $exists = 'hello-world.txt';
101 $this->ci_vfs_create($exists, 'Hello world.', $this->ci_app_root, $dir);
102 $path = $this->ci_vfs_path($dir.'/', APPPATH);
Alex Bilbie096b8942012-08-30 00:20:36 +0100103 $this->upload->file_ext = '.txt';
104
dchill427ecc5cd2012-10-12 16:25:51 -0400105 $this->assertEquals($isnew, $this->upload->set_filename($path, $isnew));
106 $this->assertEquals('hello-world1.txt', $this->upload->set_filename($path, $exists));
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100107 }
108
109 function test_set_max_filesize()
110 {
111 $this->upload->set_max_filesize(100);
112 $this->assertEquals(100, $this->upload->max_size);
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +0100113 }
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100114
115 function test_set_max_filename()
116 {
117 $this->upload->set_max_filename(100);
118 $this->assertEquals(100, $this->upload->max_filename);
119 }
120
121 function test_set_max_width()
122 {
123 $this->upload->set_max_width(100);
124 $this->assertEquals(100, $this->upload->max_width);
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +0100125 }
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100126
127 function test_set_max_height()
128 {
129 $this->upload->set_max_height(100);
130 $this->assertEquals(100, $this->upload->max_height);
131 }
132
133 function test_set_allowed_types()
134 {
135 $this->upload->set_allowed_types('*');
136 $this->assertEquals('*', $this->upload->allowed_types);
137
138 $this->upload->set_allowed_types('foo|bar');
139 $this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types);
140 }
141
142 function test_set_image_properties()
143 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100144 $this->upload->file_type = 'image/gif';
dchill427ecc5cd2012-10-12 16:25:51 -0400145 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100146
147 $props = array(
148 'image_width' => 170,
149 'image_height' => 73,
150 'image_type' => 'gif',
151 'image_size_str' => 'width="170" height="73"'
152 );
153
154 $this->upload->set_image_properties($this->upload->file_temp);
155
156 $this->assertEquals($props['image_width'], $this->upload->image_width);
157 $this->assertEquals($props['image_height'], $this->upload->image_height);
158 $this->assertEquals($props['image_type'], $this->upload->image_type);
159 $this->assertEquals($props['image_size_str'], $this->upload->image_size_str);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100160 }
161
162 function test_set_xss_clean()
163 {
164 $this->upload->set_xss_clean(TRUE);
165 $this->assertTrue($this->upload->xss_clean);
166
167 $this->upload->set_xss_clean(FALSE);
168 $this->assertFalse($this->upload->xss_clean);
169 }
170
171 function test_is_image()
172 {
173 $this->upload->file_type = 'image/x-png';
174 $this->assertTrue($this->upload->is_image());
175
176 $this->upload->file_type = 'text/plain';
177 $this->assertFalse($this->upload->is_image());
178 }
179
180 function test_is_allowed_filetype()
181 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100182 $this->upload->allowed_types = array('html', 'gif');
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100183
184 $this->upload->file_ext = '.txt';
185 $this->upload->file_type = 'text/plain';
186 $this->assertFalse($this->upload->is_allowed_filetype(FALSE));
187 $this->assertFalse($this->upload->is_allowed_filetype(TRUE));
188
189 $this->upload->file_ext = '.html';
190 $this->upload->file_type = 'text/html';
191 $this->assertTrue($this->upload->is_allowed_filetype(FALSE));
192 $this->assertTrue($this->upload->is_allowed_filetype(TRUE));
193
dchill427ecc5cd2012-10-12 16:25:51 -0400194 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100195 $this->upload->file_ext = '.gif';
196 $this->upload->file_type = 'image/gif';
197 $this->assertTrue($this->upload->is_allowed_filetype());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100198 }
199
200 function test_is_allowed_filesize()
201 {
202 $this->upload->max_size = 100;
203 $this->upload->file_size = 99;
204
205 $this->assertTrue($this->upload->is_allowed_filesize());
206
207 $this->upload->file_size = 101;
208 $this->assertFalse($this->upload->is_allowed_filesize());
209 }
210
211 function test_is_allowed_dimensions()
212 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100213 $this->upload->file_type = 'text/plain';
214 $this->assertTrue($this->upload->is_allowed_dimensions());
215
216 $this->upload->file_type = 'image/gif';
dchill427ecc5cd2012-10-12 16:25:51 -0400217 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100218
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +0100219 $this->upload->max_width = 10;
Alex Bilbie096b8942012-08-30 00:20:36 +0100220 $this->assertFalse($this->upload->is_allowed_dimensions());
221
222 $this->upload->max_width = 170;
223 $this->upload->max_height = 10;
224 $this->assertFalse($this->upload->is_allowed_dimensions());
225
226 $this->upload->max_height = 73;
227 $this->assertTrue($this->upload->is_allowed_dimensions());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100228 }
229
230 function test_validate_upload_path()
231 {
232 $this->upload->upload_path = '';
233 $this->assertFalse($this->upload->validate_upload_path());
234
dchill427ecc5cd2012-10-12 16:25:51 -0400235 $dir = 'uploads';
236 $this->ci_vfs_mkdir($dir);
237 $this->upload->upload_path = $this->ci_vfs_path($dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100238 $this->assertTrue($this->upload->validate_upload_path());
239 }
240
241 function test_get_extension()
242 {
243 $this->assertEquals('.txt', $this->upload->get_extension('hello.txt'));
244 $this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess'));
245 $this->assertEquals('', $this->upload->get_extension('hello'));
246 }
247
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100248 function test_limit_filename_length()
249 {
250 $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10));
251 $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9));
252 }
253
254 function test_do_xss_clean()
255 {
dchill427ecc5cd2012-10-12 16:25:51 -0400256 $dir = 'uploads';
257 $file1 = 'file1.txt';
258 $file2 = 'file2.txt';
259 $file3 = 'file3.txt';
260 $this->ci_vfs_create($file1, 'The billy goat was waiting for them.', $this->ci_vfs_root, $dir);
261 $this->ci_vfs_create($file2, '', $this->ci_vfs_root, $dir);
262 $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 +0100263
dchill427ecc5cd2012-10-12 16:25:51 -0400264 $this->upload->file_temp = $this->ci_vfs_path($file1, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100265 $this->assertTrue($this->upload->do_xss_clean());
266
dchill427ecc5cd2012-10-12 16:25:51 -0400267 $this->upload->file_temp = $this->ci_vfs_path($file2, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100268 $this->assertFalse($this->upload->do_xss_clean());
269
dchill427ecc5cd2012-10-12 16:25:51 -0400270 $this->upload->file_temp = $this->ci_vfs_path($file3, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100271 $this->assertFalse($this->upload->do_xss_clean());
Alex Bilbie096b8942012-08-30 00:20:36 +0100272
dchill427ecc5cd2012-10-12 16:25:51 -0400273 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100274 $this->assertTrue($this->upload->do_xss_clean());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100275 }
276
277 function test_set_error()
278 {
279 $errors = array(
280 'An error!'
281 );
282
283 $another = 'Another error!';
284
285 $this->upload->set_error($errors);
286 $this->assertEquals($errors, $this->upload->error_msg);
287
288 $errors[] = $another;
289 $this->upload->set_error($another);
290 $this->assertEquals($errors, $this->upload->error_msg);
291 }
292
293 function test_display_errors()
294 {
295 $this->upload->error_msg[] = 'Error test';
296 $this->assertEquals('<p>Error test</p>', $this->upload->display_errors());
297 }
298
Andrey Andreev878e23f2016-08-10 15:15:49 +0300299}