blob: 82794277399df0edbd5fa0c1b5b55433129ecf2e [file] [log] [blame]
Alex Bilbieba1fbfb2012-08-29 23:23:37 +01001<?php
2
3class Upload_test extends CI_TestCase {
4
5 function set_up()
6 {
dchill427ecc5cd2012-10-12 16:25:51 -04007 $ci = $this->ci_instance();
8 $ci->upload = new Mock_Libraries_Upload();
9 $ci->security = new Mock_Core_Security();
10 $ci->lang = new Mock_Core_Lang();
11 $this->upload = $ci->upload;
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010012 }
13
Alex Bilbie096b8942012-08-30 00:20:36 +010014 function test_do_upload()
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010015 {
Alex Bilbie096b8942012-08-30 00:20:36 +010016 $this->markTestIncomplete('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');
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010017 }
18
19 function test_data()
20 {
21 $data = array(
22 'file_name' => 'hello.txt',
23 'file_type' => 'text/plain',
24 'file_path' => '/tmp/',
25 'full_path' => '/tmp/hello.txt',
26 'raw_name' => 'hello',
27 'orig_name' => 'hello.txt',
28 'client_name' => '',
29 'file_ext' => '.txt',
30 'file_size' => 100,
31 'is_image' => FALSE,
32 'image_width' => '',
33 'image_height' => '',
34 'image_type' => '',
35 'image_size_str' => ''
36 );
37
38 $this->upload->set_upload_path('/tmp/');
39
40 foreach ($data as $k => $v)
41 {
42 $this->upload->{$k} = $v;
43 }
44
45 $this->assertEquals('hello.txt', $this->upload->data('file_name'));
46 $this->assertEquals($data, $this->upload->data());
47 }
48
49 function test_set_upload_path()
50 {
51 $this->upload->set_upload_path('/tmp/');
52 $this->assertEquals('/tmp/', $this->upload->upload_path);
53
54 $this->upload->set_upload_path('/tmp');
55 $this->assertEquals('/tmp/', $this->upload->upload_path);
56 }
57
58 function test_set_filename()
59 {
dchill427ecc5cd2012-10-12 16:25:51 -040060 $dir = 'uploads';
61 $isnew = 'helloworld.txt';
62 $exists = 'hello-world.txt';
63 $this->ci_vfs_create($exists, 'Hello world.', $this->ci_app_root, $dir);
64 $path = $this->ci_vfs_path($dir.'/', APPPATH);
Alex Bilbie096b8942012-08-30 00:20:36 +010065 $this->upload->file_ext = '.txt';
66
dchill427ecc5cd2012-10-12 16:25:51 -040067 $this->assertEquals($isnew, $this->upload->set_filename($path, $isnew));
68 $this->assertEquals('hello-world1.txt', $this->upload->set_filename($path, $exists));
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010069 }
70
71 function test_set_max_filesize()
72 {
73 $this->upload->set_max_filesize(100);
74 $this->assertEquals(100, $this->upload->max_size);
75 }
76
77 function test_set_max_filename()
78 {
79 $this->upload->set_max_filename(100);
80 $this->assertEquals(100, $this->upload->max_filename);
81 }
82
83 function test_set_max_width()
84 {
85 $this->upload->set_max_width(100);
86 $this->assertEquals(100, $this->upload->max_width);
87 }
88
89 function test_set_max_height()
90 {
91 $this->upload->set_max_height(100);
92 $this->assertEquals(100, $this->upload->max_height);
93 }
94
95 function test_set_allowed_types()
96 {
97 $this->upload->set_allowed_types('*');
98 $this->assertEquals('*', $this->upload->allowed_types);
99
100 $this->upload->set_allowed_types('foo|bar');
101 $this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types);
102 }
103
104 function test_set_image_properties()
105 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100106 $this->upload->file_type = 'image/gif';
dchill427ecc5cd2012-10-12 16:25:51 -0400107 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100108
109 $props = array(
110 'image_width' => 170,
111 'image_height' => 73,
112 'image_type' => 'gif',
113 'image_size_str' => 'width="170" height="73"'
114 );
115
116 $this->upload->set_image_properties($this->upload->file_temp);
117
118 $this->assertEquals($props['image_width'], $this->upload->image_width);
119 $this->assertEquals($props['image_height'], $this->upload->image_height);
120 $this->assertEquals($props['image_type'], $this->upload->image_type);
121 $this->assertEquals($props['image_size_str'], $this->upload->image_size_str);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100122 }
123
124 function test_set_xss_clean()
125 {
126 $this->upload->set_xss_clean(TRUE);
127 $this->assertTrue($this->upload->xss_clean);
128
129 $this->upload->set_xss_clean(FALSE);
130 $this->assertFalse($this->upload->xss_clean);
131 }
132
133 function test_is_image()
134 {
135 $this->upload->file_type = 'image/x-png';
136 $this->assertTrue($this->upload->is_image());
137
138 $this->upload->file_type = 'text/plain';
139 $this->assertFalse($this->upload->is_image());
140 }
141
142 function test_is_allowed_filetype()
143 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100144 $this->upload->allowed_types = array('html', 'gif');
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100145
146 $this->upload->file_ext = '.txt';
147 $this->upload->file_type = 'text/plain';
148 $this->assertFalse($this->upload->is_allowed_filetype(FALSE));
149 $this->assertFalse($this->upload->is_allowed_filetype(TRUE));
150
151 $this->upload->file_ext = '.html';
152 $this->upload->file_type = 'text/html';
153 $this->assertTrue($this->upload->is_allowed_filetype(FALSE));
154 $this->assertTrue($this->upload->is_allowed_filetype(TRUE));
155
dchill427ecc5cd2012-10-12 16:25:51 -0400156 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100157 $this->upload->file_ext = '.gif';
158 $this->upload->file_type = 'image/gif';
159 $this->assertTrue($this->upload->is_allowed_filetype());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100160 }
161
162 function test_is_allowed_filesize()
163 {
164 $this->upload->max_size = 100;
165 $this->upload->file_size = 99;
166
167 $this->assertTrue($this->upload->is_allowed_filesize());
168
169 $this->upload->file_size = 101;
170 $this->assertFalse($this->upload->is_allowed_filesize());
171 }
172
173 function test_is_allowed_dimensions()
174 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100175 $this->upload->file_type = 'text/plain';
176 $this->assertTrue($this->upload->is_allowed_dimensions());
177
178 $this->upload->file_type = 'image/gif';
dchill427ecc5cd2012-10-12 16:25:51 -0400179 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100180
181 $this->upload->max_width = 10;
182 $this->assertFalse($this->upload->is_allowed_dimensions());
183
184 $this->upload->max_width = 170;
185 $this->upload->max_height = 10;
186 $this->assertFalse($this->upload->is_allowed_dimensions());
187
188 $this->upload->max_height = 73;
189 $this->assertTrue($this->upload->is_allowed_dimensions());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100190 }
191
192 function test_validate_upload_path()
193 {
194 $this->upload->upload_path = '';
195 $this->assertFalse($this->upload->validate_upload_path());
196
dchill427ecc5cd2012-10-12 16:25:51 -0400197 $dir = 'uploads';
198 $this->ci_vfs_mkdir($dir);
199 $this->upload->upload_path = $this->ci_vfs_path($dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100200 $this->assertTrue($this->upload->validate_upload_path());
201 }
202
203 function test_get_extension()
204 {
205 $this->assertEquals('.txt', $this->upload->get_extension('hello.txt'));
206 $this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess'));
207 $this->assertEquals('', $this->upload->get_extension('hello'));
208 }
209
210 function test_clean_file_name()
211 {
212 $this->assertEquals('hello.txt', $this->upload->clean_file_name('hello.txt'));
213 $this->assertEquals('hello.txt', $this->upload->clean_file_name('%253chell>o.txt'));
214 }
215
216 function test_limit_filename_length()
217 {
218 $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10));
219 $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9));
220 }
221
222 function test_do_xss_clean()
223 {
dchill427ecc5cd2012-10-12 16:25:51 -0400224 $dir = 'uploads';
225 $file1 = 'file1.txt';
226 $file2 = 'file2.txt';
227 $file3 = 'file3.txt';
228 $this->ci_vfs_create($file1, 'The billy goat was waiting for them.', $this->ci_vfs_root, $dir);
229 $this->ci_vfs_create($file2, '', $this->ci_vfs_root, $dir);
230 $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 +0100231
dchill427ecc5cd2012-10-12 16:25:51 -0400232 $this->upload->file_temp = $this->ci_vfs_path($file1, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100233 $this->assertTrue($this->upload->do_xss_clean());
234
dchill427ecc5cd2012-10-12 16:25:51 -0400235 $this->upload->file_temp = $this->ci_vfs_path($file2, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100236 $this->assertFalse($this->upload->do_xss_clean());
237
dchill427ecc5cd2012-10-12 16:25:51 -0400238 $this->upload->file_temp = $this->ci_vfs_path($file3, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100239 $this->assertFalse($this->upload->do_xss_clean());
Alex Bilbie096b8942012-08-30 00:20:36 +0100240
dchill427ecc5cd2012-10-12 16:25:51 -0400241 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100242 $this->assertTrue($this->upload->do_xss_clean());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100243 }
244
245 function test_set_error()
246 {
247 $errors = array(
248 'An error!'
249 );
250
251 $another = 'Another error!';
252
253 $this->upload->set_error($errors);
254 $this->assertEquals($errors, $this->upload->error_msg);
255
256 $errors[] = $another;
257 $this->upload->set_error($another);
258 $this->assertEquals($errors, $this->upload->error_msg);
259 }
260
261 function test_display_errors()
262 {
263 $this->upload->error_msg[] = 'Error test';
264 $this->assertEquals('<p>Error test</p>', $this->upload->display_errors());
265 }
266
267 function test_mimes_types()
268 {
269 $this->assertEquals('text/plain', $this->upload->mimes_types('txt'));
270 $this->assertFalse($this->upload->mimes_types('foobar'));
271 }
272
273}