blob: 1bd8f14301a8c535e9972b7e5f6bba90074aaa76 [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();
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
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +010015 function test_do_upload()
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010016 {
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +010017 $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');
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010018 }
19
20 function test_data()
21 {
22 $data = array(
23 'file_name' => 'hello.txt',
24 'file_type' => 'text/plain',
25 'file_path' => '/tmp/',
26 'full_path' => '/tmp/hello.txt',
27 'raw_name' => 'hello',
28 'orig_name' => 'hello.txt',
29 'client_name' => '',
30 'file_ext' => '.txt',
31 'file_size' => 100,
32 'is_image' => FALSE,
33 'image_width' => '',
34 'image_height' => '',
35 'image_type' => '',
36 'image_size_str' => ''
37 );
38
39 $this->upload->set_upload_path('/tmp/');
40
41 foreach ($data as $k => $v)
42 {
43 $this->upload->{$k} = $v;
44 }
45
46 $this->assertEquals('hello.txt', $this->upload->data('file_name'));
47 $this->assertEquals($data, $this->upload->data());
48 }
49
50 function test_set_upload_path()
51 {
52 $this->upload->set_upload_path('/tmp/');
53 $this->assertEquals('/tmp/', $this->upload->upload_path);
54
55 $this->upload->set_upload_path('/tmp');
56 $this->assertEquals('/tmp/', $this->upload->upload_path);
57 }
58
59 function test_set_filename()
60 {
dchill427ecc5cd2012-10-12 16:25:51 -040061 $dir = 'uploads';
62 $isnew = 'helloworld.txt';
63 $exists = 'hello-world.txt';
64 $this->ci_vfs_create($exists, 'Hello world.', $this->ci_app_root, $dir);
65 $path = $this->ci_vfs_path($dir.'/', APPPATH);
Alex Bilbie096b8942012-08-30 00:20:36 +010066 $this->upload->file_ext = '.txt';
67
dchill427ecc5cd2012-10-12 16:25:51 -040068 $this->assertEquals($isnew, $this->upload->set_filename($path, $isnew));
69 $this->assertEquals('hello-world1.txt', $this->upload->set_filename($path, $exists));
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010070 }
71
72 function test_set_max_filesize()
73 {
74 $this->upload->set_max_filesize(100);
75 $this->assertEquals(100, $this->upload->max_size);
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +010076 }
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010077
78 function test_set_max_filename()
79 {
80 $this->upload->set_max_filename(100);
81 $this->assertEquals(100, $this->upload->max_filename);
82 }
83
84 function test_set_max_width()
85 {
86 $this->upload->set_max_width(100);
87 $this->assertEquals(100, $this->upload->max_width);
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +010088 }
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010089
90 function test_set_max_height()
91 {
92 $this->upload->set_max_height(100);
93 $this->assertEquals(100, $this->upload->max_height);
94 }
95
96 function test_set_allowed_types()
97 {
98 $this->upload->set_allowed_types('*');
99 $this->assertEquals('*', $this->upload->allowed_types);
100
101 $this->upload->set_allowed_types('foo|bar');
102 $this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types);
103 }
104
105 function test_set_image_properties()
106 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100107 $this->upload->file_type = 'image/gif';
dchill427ecc5cd2012-10-12 16:25:51 -0400108 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100109
110 $props = array(
111 'image_width' => 170,
112 'image_height' => 73,
113 'image_type' => 'gif',
114 'image_size_str' => 'width="170" height="73"'
115 );
116
117 $this->upload->set_image_properties($this->upload->file_temp);
118
119 $this->assertEquals($props['image_width'], $this->upload->image_width);
120 $this->assertEquals($props['image_height'], $this->upload->image_height);
121 $this->assertEquals($props['image_type'], $this->upload->image_type);
122 $this->assertEquals($props['image_size_str'], $this->upload->image_size_str);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100123 }
124
125 function test_set_xss_clean()
126 {
127 $this->upload->set_xss_clean(TRUE);
128 $this->assertTrue($this->upload->xss_clean);
129
130 $this->upload->set_xss_clean(FALSE);
131 $this->assertFalse($this->upload->xss_clean);
132 }
133
134 function test_is_image()
135 {
136 $this->upload->file_type = 'image/x-png';
137 $this->assertTrue($this->upload->is_image());
138
139 $this->upload->file_type = 'text/plain';
140 $this->assertFalse($this->upload->is_image());
141 }
142
143 function test_is_allowed_filetype()
144 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100145 $this->upload->allowed_types = array('html', 'gif');
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100146
147 $this->upload->file_ext = '.txt';
148 $this->upload->file_type = 'text/plain';
149 $this->assertFalse($this->upload->is_allowed_filetype(FALSE));
150 $this->assertFalse($this->upload->is_allowed_filetype(TRUE));
151
152 $this->upload->file_ext = '.html';
153 $this->upload->file_type = 'text/html';
154 $this->assertTrue($this->upload->is_allowed_filetype(FALSE));
155 $this->assertTrue($this->upload->is_allowed_filetype(TRUE));
156
dchill427ecc5cd2012-10-12 16:25:51 -0400157 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100158 $this->upload->file_ext = '.gif';
159 $this->upload->file_type = 'image/gif';
160 $this->assertTrue($this->upload->is_allowed_filetype());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100161 }
162
163 function test_is_allowed_filesize()
164 {
165 $this->upload->max_size = 100;
166 $this->upload->file_size = 99;
167
168 $this->assertTrue($this->upload->is_allowed_filesize());
169
170 $this->upload->file_size = 101;
171 $this->assertFalse($this->upload->is_allowed_filesize());
172 }
173
174 function test_is_allowed_dimensions()
175 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100176 $this->upload->file_type = 'text/plain';
177 $this->assertTrue($this->upload->is_allowed_dimensions());
178
179 $this->upload->file_type = 'image/gif';
dchill427ecc5cd2012-10-12 16:25:51 -0400180 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100181
Alex Bilbie4b3cf8d2012-10-18 16:44:54 +0100182 $this->upload->max_width = 10;
Alex Bilbie096b8942012-08-30 00:20:36 +0100183 $this->assertFalse($this->upload->is_allowed_dimensions());
184
185 $this->upload->max_width = 170;
186 $this->upload->max_height = 10;
187 $this->assertFalse($this->upload->is_allowed_dimensions());
188
189 $this->upload->max_height = 73;
190 $this->assertTrue($this->upload->is_allowed_dimensions());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100191 }
192
193 function test_validate_upload_path()
194 {
195 $this->upload->upload_path = '';
196 $this->assertFalse($this->upload->validate_upload_path());
197
dchill427ecc5cd2012-10-12 16:25:51 -0400198 $dir = 'uploads';
199 $this->ci_vfs_mkdir($dir);
200 $this->upload->upload_path = $this->ci_vfs_path($dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100201 $this->assertTrue($this->upload->validate_upload_path());
202 }
203
204 function test_get_extension()
205 {
206 $this->assertEquals('.txt', $this->upload->get_extension('hello.txt'));
207 $this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess'));
208 $this->assertEquals('', $this->upload->get_extension('hello'));
209 }
210
211 function test_clean_file_name()
212 {
213 $this->assertEquals('hello.txt', $this->upload->clean_file_name('hello.txt'));
214 $this->assertEquals('hello.txt', $this->upload->clean_file_name('%253chell>o.txt'));
215 }
216
217 function test_limit_filename_length()
218 {
219 $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10));
220 $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9));
221 }
222
223 function test_do_xss_clean()
224 {
dchill427ecc5cd2012-10-12 16:25:51 -0400225 $dir = 'uploads';
226 $file1 = 'file1.txt';
227 $file2 = 'file2.txt';
228 $file3 = 'file3.txt';
229 $this->ci_vfs_create($file1, 'The billy goat was waiting for them.', $this->ci_vfs_root, $dir);
230 $this->ci_vfs_create($file2, '', $this->ci_vfs_root, $dir);
231 $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 +0100232
dchill427ecc5cd2012-10-12 16:25:51 -0400233 $this->upload->file_temp = $this->ci_vfs_path($file1, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100234 $this->assertTrue($this->upload->do_xss_clean());
235
dchill427ecc5cd2012-10-12 16:25:51 -0400236 $this->upload->file_temp = $this->ci_vfs_path($file2, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100237 $this->assertFalse($this->upload->do_xss_clean());
238
dchill427ecc5cd2012-10-12 16:25:51 -0400239 $this->upload->file_temp = $this->ci_vfs_path($file3, $dir);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100240 $this->assertFalse($this->upload->do_xss_clean());
Alex Bilbie096b8942012-08-30 00:20:36 +0100241
dchill427ecc5cd2012-10-12 16:25:51 -0400242 $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
Alex Bilbie096b8942012-08-30 00:20:36 +0100243 $this->assertTrue($this->upload->do_xss_clean());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100244 }
245
246 function test_set_error()
247 {
248 $errors = array(
249 'An error!'
250 );
251
252 $another = 'Another error!';
253
254 $this->upload->set_error($errors);
255 $this->assertEquals($errors, $this->upload->error_msg);
256
257 $errors[] = $another;
258 $this->upload->set_error($another);
259 $this->assertEquals($errors, $this->upload->error_msg);
260 }
261
262 function test_display_errors()
263 {
264 $this->upload->error_msg[] = 'Error test';
265 $this->assertEquals('<p>Error test</p>', $this->upload->display_errors());
266 }
267
268 function test_mimes_types()
269 {
270 $this->assertEquals('text/plain', $this->upload->mimes_types('txt'));
271 $this->assertFalse($this->upload->mimes_types('foobar'));
272 }
273
274}