blob: d79a3ffc90dbe61bc910d26a54dc36a6498bcfad [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 {
7 $obj = new stdClass;
8 $obj->upload = new Mock_Libraries_Upload();
9 $obj->security = new Mock_Core_Security();
10 $obj->lang = new Mock_Core_Lang();
11
12 $this->ci_instance($obj);
13 $this->upload = $obj->upload;
14
15 vfsStreamWrapper::register();
16 vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir'));
17
18 $this->_test_dir = vfsStreamWrapper::getRoot();
19 }
20
Alex Bilbie096b8942012-08-30 00:20:36 +010021 function test_do_upload()
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010022 {
Alex Bilbie096b8942012-08-30 00:20:36 +010023 $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 +010024 }
25
26 function test_data()
27 {
28 $data = array(
29 'file_name' => 'hello.txt',
30 'file_type' => 'text/plain',
31 'file_path' => '/tmp/',
32 'full_path' => '/tmp/hello.txt',
33 'raw_name' => 'hello',
34 'orig_name' => 'hello.txt',
35 'client_name' => '',
36 'file_ext' => '.txt',
37 'file_size' => 100,
38 'is_image' => FALSE,
39 'image_width' => '',
40 'image_height' => '',
41 'image_type' => '',
42 'image_size_str' => ''
43 );
44
45 $this->upload->set_upload_path('/tmp/');
46
47 foreach ($data as $k => $v)
48 {
49 $this->upload->{$k} = $v;
50 }
51
52 $this->assertEquals('hello.txt', $this->upload->data('file_name'));
53 $this->assertEquals($data, $this->upload->data());
54 }
55
56 function test_set_upload_path()
57 {
58 $this->upload->set_upload_path('/tmp/');
59 $this->assertEquals('/tmp/', $this->upload->upload_path);
60
61 $this->upload->set_upload_path('/tmp');
62 $this->assertEquals('/tmp/', $this->upload->upload_path);
63 }
64
65 function test_set_filename()
66 {
Alex Bilbie096b8942012-08-30 00:20:36 +010067 $file1 = vfsStream::newFile('hello-world.txt')->withContent('Hello world.')->at($this->_test_dir);
68 $this->upload->file_ext = '.txt';
69
70 $this->assertEquals('helloworld.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'helloworld.txt'));
71 $this->assertEquals('hello-world1.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'hello-world.txt'));
Alex Bilbieba1fbfb2012-08-29 23:23:37 +010072 }
73
74 function test_set_max_filesize()
75 {
76 $this->upload->set_max_filesize(100);
77 $this->assertEquals(100, $this->upload->max_size);
78 }
79
80 function test_set_max_filename()
81 {
82 $this->upload->set_max_filename(100);
83 $this->assertEquals(100, $this->upload->max_filename);
84 }
85
86 function test_set_max_width()
87 {
88 $this->upload->set_max_width(100);
89 $this->assertEquals(100, $this->upload->max_width);
90 }
91
92 function test_set_max_height()
93 {
94 $this->upload->set_max_height(100);
95 $this->assertEquals(100, $this->upload->max_height);
96 }
97
98 function test_set_allowed_types()
99 {
100 $this->upload->set_allowed_types('*');
101 $this->assertEquals('*', $this->upload->allowed_types);
102
103 $this->upload->set_allowed_types('foo|bar');
104 $this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types);
105 }
106
107 function test_set_image_properties()
108 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100109 $this->upload->file_type = 'image/gif';
110 $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
111
112 $props = array(
113 'image_width' => 170,
114 'image_height' => 73,
115 'image_type' => 'gif',
116 'image_size_str' => 'width="170" height="73"'
117 );
118
119 $this->upload->set_image_properties($this->upload->file_temp);
120
121 $this->assertEquals($props['image_width'], $this->upload->image_width);
122 $this->assertEquals($props['image_height'], $this->upload->image_height);
123 $this->assertEquals($props['image_type'], $this->upload->image_type);
124 $this->assertEquals($props['image_size_str'], $this->upload->image_size_str);
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100125 }
126
127 function test_set_xss_clean()
128 {
129 $this->upload->set_xss_clean(TRUE);
130 $this->assertTrue($this->upload->xss_clean);
131
132 $this->upload->set_xss_clean(FALSE);
133 $this->assertFalse($this->upload->xss_clean);
134 }
135
136 function test_is_image()
137 {
138 $this->upload->file_type = 'image/x-png';
139 $this->assertTrue($this->upload->is_image());
140
141 $this->upload->file_type = 'text/plain';
142 $this->assertFalse($this->upload->is_image());
143 }
144
145 function test_is_allowed_filetype()
146 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100147 $this->upload->allowed_types = array('html', 'gif');
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100148
149 $this->upload->file_ext = '.txt';
150 $this->upload->file_type = 'text/plain';
151 $this->assertFalse($this->upload->is_allowed_filetype(FALSE));
152 $this->assertFalse($this->upload->is_allowed_filetype(TRUE));
153
154 $this->upload->file_ext = '.html';
155 $this->upload->file_type = 'text/html';
156 $this->assertTrue($this->upload->is_allowed_filetype(FALSE));
157 $this->assertTrue($this->upload->is_allowed_filetype(TRUE));
158
Alex Bilbie096b8942012-08-30 00:20:36 +0100159 $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
160 $this->upload->file_ext = '.gif';
161 $this->upload->file_type = 'image/gif';
162 $this->assertTrue($this->upload->is_allowed_filetype());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100163 }
164
165 function test_is_allowed_filesize()
166 {
167 $this->upload->max_size = 100;
168 $this->upload->file_size = 99;
169
170 $this->assertTrue($this->upload->is_allowed_filesize());
171
172 $this->upload->file_size = 101;
173 $this->assertFalse($this->upload->is_allowed_filesize());
174 }
175
176 function test_is_allowed_dimensions()
177 {
Alex Bilbie096b8942012-08-30 00:20:36 +0100178 $this->upload->file_type = 'text/plain';
179 $this->assertTrue($this->upload->is_allowed_dimensions());
180
181 $this->upload->file_type = 'image/gif';
182 $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
183
184 $this->upload->max_width = 10;
185 $this->assertFalse($this->upload->is_allowed_dimensions());
186
187 $this->upload->max_width = 170;
188 $this->upload->max_height = 10;
189 $this->assertFalse($this->upload->is_allowed_dimensions());
190
191 $this->upload->max_height = 73;
192 $this->assertTrue($this->upload->is_allowed_dimensions());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100193 }
194
195 function test_validate_upload_path()
196 {
197 $this->upload->upload_path = '';
198 $this->assertFalse($this->upload->validate_upload_path());
199
200 $this->upload->upload_path = vfsStream::url('testDir');
201 $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 {
225 $file1 = vfsStream::newFile('file1.txt')->withContent('The billy goat was waiting for them.')->at($this->_test_dir);
226 $file2 = vfsStream::newFile('file2.txt')->at($this->_test_dir);
227 $file3 = vfsStream::newFile('file3.txt')->withContent('<script type="text/javascript">alert("Boo! said the billy goat")</script>')->at($this->_test_dir);
228
229 $this->upload->file_temp = vfsStream::url('file1.txt');
230 $this->assertTrue($this->upload->do_xss_clean());
231
232 $this->upload->file_temp = vfsStream::url('file2.txt');
233 $this->assertFalse($this->upload->do_xss_clean());
234
235 $this->upload->file_temp = vfsStream::url('file3.txt');
236 $this->assertFalse($this->upload->do_xss_clean());
Alex Bilbie096b8942012-08-30 00:20:36 +0100237
238 $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
239 $this->assertTrue($this->upload->do_xss_clean());
Alex Bilbieba1fbfb2012-08-29 23:23:37 +0100240 }
241
242 function test_set_error()
243 {
244 $errors = array(
245 'An error!'
246 );
247
248 $another = 'Another error!';
249
250 $this->upload->set_error($errors);
251 $this->assertEquals($errors, $this->upload->error_msg);
252
253 $errors[] = $another;
254 $this->upload->set_error($another);
255 $this->assertEquals($errors, $this->upload->error_msg);
256 }
257
258 function test_display_errors()
259 {
260 $this->upload->error_msg[] = 'Error test';
261 $this->assertEquals('<p>Error test</p>', $this->upload->display_errors());
262 }
263
264 function test_mimes_types()
265 {
266 $this->assertEquals('text/plain', $this->upload->mimes_types('txt'));
267 $this->assertFalse($this->upload->mimes_types('foobar'));
268 }
269
270}