Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Upload_test extends CI_TestCase { |
| 4 | |
Andrey Andreev | 742826c | 2014-02-21 15:50:36 +0200 | [diff] [blame] | 5 | public function set_up() |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 6 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 7 | $ci = $this->ci_instance(); |
Andrey Andreev | 10e7a32 | 2014-02-20 16:42:16 +0200 | [diff] [blame] | 8 | $ci->upload = new CI_Upload(); |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 9 | $ci->security = new Mock_Core_Security(); |
Andrey Andreev | 878e23f | 2016-08-10 15:15:49 +0300 | [diff] [blame] | 10 | $ci->lang = $this->getMockBuilder('CI_Lang')->setMethods(array('load', 'line'))->getMock(); |
dchill42 | 1713d32 | 2012-11-24 20:23:21 -0500 | [diff] [blame] | 11 | $ci->lang->expects($this->any())->method('line')->will($this->returnValue(FALSE)); |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 12 | $this->upload = $ci->upload; |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 13 | } |
| 14 | |
Andrey Andreev | 742826c | 2014-02-21 15:50:36 +0200 | [diff] [blame] | 15 | // -------------------------------------------------------------------- |
| 16 | |
| 17 | public function test___construct_initialize() |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 18 | { |
Andrey Andreev | 742826c | 2014-02-21 15:50:36 +0200 | [diff] [blame] | 19 | // via __construct |
| 20 | |
| 21 | $upload = new CI_Upload( |
| 22 | array( |
| 23 | 'file_name' => 'foo', |
| 24 | 'file_ext_tolower' => TRUE |
| 25 | ) |
| 26 | ); |
| 27 | |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 28 | $reflection = new ReflectionClass($upload); |
| 29 | $reflection = $reflection->getProperty('_file_name_override'); |
| 30 | $reflection->setAccessible(TRUE); |
| 31 | $this->assertEquals('foo', $reflection->getValue($upload)); |
Andrey Andreev | 742826c | 2014-02-21 15:50:36 +0200 | [diff] [blame] | 32 | |
| 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 Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Andrey Andreev | 742826c | 2014-02-21 15:50:36 +0200 | [diff] [blame] | 48 | // -------------------------------------------------------------------- |
| 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 Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 57 | 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 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 98 | $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 Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 103 | $this->upload->file_ext = '.txt'; |
| 104 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 105 | $this->assertEquals($isnew, $this->upload->set_filename($path, $isnew)); |
| 106 | $this->assertEquals('hello-world1.txt', $this->upload->set_filename($path, $exists)); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | function test_set_max_filesize() |
| 110 | { |
| 111 | $this->upload->set_max_filesize(100); |
| 112 | $this->assertEquals(100, $this->upload->max_size); |
Alex Bilbie | 4b3cf8d | 2012-10-18 16:44:54 +0100 | [diff] [blame] | 113 | } |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 114 | |
| 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 Bilbie | 4b3cf8d | 2012-10-18 16:44:54 +0100 | [diff] [blame] | 125 | } |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 126 | |
| 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 Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 144 | $this->upload->file_type = 'image/gif'; |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 145 | $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); |
Alex Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 146 | |
| 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 Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 160 | } |
| 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 Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 182 | $this->upload->allowed_types = array('html', 'gif'); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 183 | |
| 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 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 194 | $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); |
Alex Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 195 | $this->upload->file_ext = '.gif'; |
| 196 | $this->upload->file_type = 'image/gif'; |
| 197 | $this->assertTrue($this->upload->is_allowed_filetype()); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 198 | } |
| 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 Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 213 | $this->upload->file_type = 'text/plain'; |
| 214 | $this->assertTrue($this->upload->is_allowed_dimensions()); |
| 215 | |
| 216 | $this->upload->file_type = 'image/gif'; |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 217 | $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); |
Alex Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 218 | |
Alex Bilbie | 4b3cf8d | 2012-10-18 16:44:54 +0100 | [diff] [blame] | 219 | $this->upload->max_width = 10; |
Alex Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 220 | $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 Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | function test_validate_upload_path() |
| 231 | { |
| 232 | $this->upload->upload_path = ''; |
| 233 | $this->assertFalse($this->upload->validate_upload_path()); |
| 234 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 235 | $dir = 'uploads'; |
| 236 | $this->ci_vfs_mkdir($dir); |
| 237 | $this->upload->upload_path = $this->ci_vfs_path($dir); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 238 | $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 Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 248 | 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 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 256 | $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 Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 263 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 264 | $this->upload->file_temp = $this->ci_vfs_path($file1, $dir); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 265 | $this->assertTrue($this->upload->do_xss_clean()); |
| 266 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 267 | $this->upload->file_temp = $this->ci_vfs_path($file2, $dir); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 268 | $this->assertFalse($this->upload->do_xss_clean()); |
| 269 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 270 | $this->upload->file_temp = $this->ci_vfs_path($file3, $dir); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 271 | $this->assertFalse($this->upload->do_xss_clean()); |
Alex Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 272 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 273 | $this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif'); |
Alex Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 274 | $this->assertTrue($this->upload->do_xss_clean()); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 275 | } |
| 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 Andreev | 878e23f | 2016-08-10 15:15:49 +0300 | [diff] [blame] | 299 | } |