Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Upload_test extends CI_TestCase { |
| 4 | |
| 5 | function set_up() |
| 6 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 7 | $ci = $this->ci_instance(); |
| 8 | $ci->upload = new Mock_Libraries_Upload(); |
| 9 | $ci->security = new Mock_Core_Security(); |
dchill42 | 1713d32 | 2012-11-24 20:23:21 -0500 | [diff] [blame] | 10 | $ci->lang = $this->getMock('CI_Lang', array('load', 'line')); |
| 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 | |
Alex Bilbie | 4b3cf8d | 2012-10-18 16:44:54 +0100 | [diff] [blame] | 15 | function test_do_upload() |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 16 | { |
Alex Bilbie | 4b3cf8d | 2012-10-18 16:44:54 +0100 | [diff] [blame] | 17 | $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 Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 18 | } |
| 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 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 61 | $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 Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 66 | $this->upload->file_ext = '.txt'; |
| 67 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 68 | $this->assertEquals($isnew, $this->upload->set_filename($path, $isnew)); |
| 69 | $this->assertEquals('hello-world1.txt', $this->upload->set_filename($path, $exists)); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | function test_set_max_filesize() |
| 73 | { |
| 74 | $this->upload->set_max_filesize(100); |
| 75 | $this->assertEquals(100, $this->upload->max_size); |
Alex Bilbie | 4b3cf8d | 2012-10-18 16:44:54 +0100 | [diff] [blame] | 76 | } |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 77 | |
| 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 Bilbie | 4b3cf8d | 2012-10-18 16:44:54 +0100 | [diff] [blame] | 88 | } |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 89 | |
| 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 Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 107 | $this->upload->file_type = 'image/gif'; |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 108 | $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] | 109 | |
| 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 Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 123 | } |
| 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 Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 145 | $this->upload->allowed_types = array('html', 'gif'); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 146 | |
| 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 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 157 | $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] | 158 | $this->upload->file_ext = '.gif'; |
| 159 | $this->upload->file_type = 'image/gif'; |
| 160 | $this->assertTrue($this->upload->is_allowed_filetype()); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 161 | } |
| 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 Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 176 | $this->upload->file_type = 'text/plain'; |
| 177 | $this->assertTrue($this->upload->is_allowed_dimensions()); |
| 178 | |
| 179 | $this->upload->file_type = 'image/gif'; |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 180 | $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] | 181 | |
Alex Bilbie | 4b3cf8d | 2012-10-18 16:44:54 +0100 | [diff] [blame] | 182 | $this->upload->max_width = 10; |
Alex Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 183 | $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 Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | function test_validate_upload_path() |
| 194 | { |
| 195 | $this->upload->upload_path = ''; |
| 196 | $this->assertFalse($this->upload->validate_upload_path()); |
| 197 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 198 | $dir = 'uploads'; |
| 199 | $this->ci_vfs_mkdir($dir); |
| 200 | $this->upload->upload_path = $this->ci_vfs_path($dir); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 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 | |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 211 | function test_limit_filename_length() |
| 212 | { |
| 213 | $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10)); |
| 214 | $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9)); |
| 215 | } |
| 216 | |
| 217 | function test_do_xss_clean() |
| 218 | { |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 219 | $dir = 'uploads'; |
| 220 | $file1 = 'file1.txt'; |
| 221 | $file2 = 'file2.txt'; |
| 222 | $file3 = 'file3.txt'; |
| 223 | $this->ci_vfs_create($file1, 'The billy goat was waiting for them.', $this->ci_vfs_root, $dir); |
| 224 | $this->ci_vfs_create($file2, '', $this->ci_vfs_root, $dir); |
| 225 | $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] | 226 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 227 | $this->upload->file_temp = $this->ci_vfs_path($file1, $dir); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 228 | $this->assertTrue($this->upload->do_xss_clean()); |
| 229 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 230 | $this->upload->file_temp = $this->ci_vfs_path($file2, $dir); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 231 | $this->assertFalse($this->upload->do_xss_clean()); |
| 232 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 233 | $this->upload->file_temp = $this->ci_vfs_path($file3, $dir); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 234 | $this->assertFalse($this->upload->do_xss_clean()); |
Alex Bilbie | 096b894 | 2012-08-30 00:20:36 +0100 | [diff] [blame] | 235 | |
dchill42 | 7ecc5cd | 2012-10-12 16:25:51 -0400 | [diff] [blame] | 236 | $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] | 237 | $this->assertTrue($this->upload->do_xss_clean()); |
Alex Bilbie | ba1fbfb | 2012-08-29 23:23:37 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | function test_set_error() |
| 241 | { |
| 242 | $errors = array( |
| 243 | 'An error!' |
| 244 | ); |
| 245 | |
| 246 | $another = 'Another error!'; |
| 247 | |
| 248 | $this->upload->set_error($errors); |
| 249 | $this->assertEquals($errors, $this->upload->error_msg); |
| 250 | |
| 251 | $errors[] = $another; |
| 252 | $this->upload->set_error($another); |
| 253 | $this->assertEquals($errors, $this->upload->error_msg); |
| 254 | } |
| 255 | |
| 256 | function test_display_errors() |
| 257 | { |
| 258 | $this->upload->error_msg[] = 'Error test'; |
| 259 | $this->assertEquals('<p>Error test</p>', $this->upload->display_errors()); |
| 260 | } |
| 261 | |
| 262 | function test_mimes_types() |
| 263 | { |
| 264 | $this->assertEquals('text/plain', $this->upload->mimes_types('txt')); |
| 265 | $this->assertFalse($this->upload->mimes_types('foobar')); |
| 266 | } |
| 267 | |
| 268 | } |