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 | { |
| 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 | |
| 21 | function test_do_upload() |
| 22 | { |
| 23 | $this->markTestIncomplete('TODO'); |
| 24 | } |
| 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 | { |
| 67 | $this->markTestIncomplete('TODO'); |
| 68 | } |
| 69 | |
| 70 | function test_set_max_filesize() |
| 71 | { |
| 72 | $this->upload->set_max_filesize(100); |
| 73 | $this->assertEquals(100, $this->upload->max_size); |
| 74 | } |
| 75 | |
| 76 | function test_set_max_filename() |
| 77 | { |
| 78 | $this->upload->set_max_filename(100); |
| 79 | $this->assertEquals(100, $this->upload->max_filename); |
| 80 | } |
| 81 | |
| 82 | function test_set_max_width() |
| 83 | { |
| 84 | $this->upload->set_max_width(100); |
| 85 | $this->assertEquals(100, $this->upload->max_width); |
| 86 | } |
| 87 | |
| 88 | function test_set_max_height() |
| 89 | { |
| 90 | $this->upload->set_max_height(100); |
| 91 | $this->assertEquals(100, $this->upload->max_height); |
| 92 | } |
| 93 | |
| 94 | function test_set_allowed_types() |
| 95 | { |
| 96 | $this->upload->set_allowed_types('*'); |
| 97 | $this->assertEquals('*', $this->upload->allowed_types); |
| 98 | |
| 99 | $this->upload->set_allowed_types('foo|bar'); |
| 100 | $this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types); |
| 101 | } |
| 102 | |
| 103 | function test_set_image_properties() |
| 104 | { |
| 105 | $this->markTestIncomplete('TODO'); |
| 106 | } |
| 107 | |
| 108 | function test_set_xss_clean() |
| 109 | { |
| 110 | $this->upload->set_xss_clean(TRUE); |
| 111 | $this->assertTrue($this->upload->xss_clean); |
| 112 | |
| 113 | $this->upload->set_xss_clean(FALSE); |
| 114 | $this->assertFalse($this->upload->xss_clean); |
| 115 | } |
| 116 | |
| 117 | function test_is_image() |
| 118 | { |
| 119 | $this->upload->file_type = 'image/x-png'; |
| 120 | $this->assertTrue($this->upload->is_image()); |
| 121 | |
| 122 | $this->upload->file_type = 'text/plain'; |
| 123 | $this->assertFalse($this->upload->is_image()); |
| 124 | } |
| 125 | |
| 126 | function test_is_allowed_filetype() |
| 127 | { |
| 128 | $this->upload->allowed_types = array('html'); |
| 129 | |
| 130 | $this->upload->file_ext = '.txt'; |
| 131 | $this->upload->file_type = 'text/plain'; |
| 132 | $this->assertFalse($this->upload->is_allowed_filetype(FALSE)); |
| 133 | $this->assertFalse($this->upload->is_allowed_filetype(TRUE)); |
| 134 | |
| 135 | $this->upload->file_ext = '.html'; |
| 136 | $this->upload->file_type = 'text/html'; |
| 137 | $this->assertTrue($this->upload->is_allowed_filetype(FALSE)); |
| 138 | $this->assertTrue($this->upload->is_allowed_filetype(TRUE)); |
| 139 | |
| 140 | $this->markTestIncomplete('Image tests'); |
| 141 | } |
| 142 | |
| 143 | function test_is_allowed_filesize() |
| 144 | { |
| 145 | $this->upload->max_size = 100; |
| 146 | $this->upload->file_size = 99; |
| 147 | |
| 148 | $this->assertTrue($this->upload->is_allowed_filesize()); |
| 149 | |
| 150 | $this->upload->file_size = 101; |
| 151 | $this->assertFalse($this->upload->is_allowed_filesize()); |
| 152 | } |
| 153 | |
| 154 | function test_is_allowed_dimensions() |
| 155 | { |
| 156 | $this->markTestIncomplete('TODO'); |
| 157 | } |
| 158 | |
| 159 | function test_validate_upload_path() |
| 160 | { |
| 161 | $this->upload->upload_path = ''; |
| 162 | $this->assertFalse($this->upload->validate_upload_path()); |
| 163 | |
| 164 | $this->upload->upload_path = vfsStream::url('testDir'); |
| 165 | $this->assertTrue($this->upload->validate_upload_path()); |
| 166 | } |
| 167 | |
| 168 | function test_get_extension() |
| 169 | { |
| 170 | $this->assertEquals('.txt', $this->upload->get_extension('hello.txt')); |
| 171 | $this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess')); |
| 172 | $this->assertEquals('', $this->upload->get_extension('hello')); |
| 173 | } |
| 174 | |
| 175 | function test_clean_file_name() |
| 176 | { |
| 177 | $this->assertEquals('hello.txt', $this->upload->clean_file_name('hello.txt')); |
| 178 | $this->assertEquals('hello.txt', $this->upload->clean_file_name('%253chell>o.txt')); |
| 179 | } |
| 180 | |
| 181 | function test_limit_filename_length() |
| 182 | { |
| 183 | $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10)); |
| 184 | $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9)); |
| 185 | } |
| 186 | |
| 187 | function test_do_xss_clean() |
| 188 | { |
| 189 | $file1 = vfsStream::newFile('file1.txt')->withContent('The billy goat was waiting for them.')->at($this->_test_dir); |
| 190 | $file2 = vfsStream::newFile('file2.txt')->at($this->_test_dir); |
| 191 | $file3 = vfsStream::newFile('file3.txt')->withContent('<script type="text/javascript">alert("Boo! said the billy goat")</script>')->at($this->_test_dir); |
| 192 | |
| 193 | $this->upload->file_temp = vfsStream::url('file1.txt'); |
| 194 | $this->assertTrue($this->upload->do_xss_clean()); |
| 195 | |
| 196 | $this->upload->file_temp = vfsStream::url('file2.txt'); |
| 197 | $this->assertFalse($this->upload->do_xss_clean()); |
| 198 | |
| 199 | $this->upload->file_temp = vfsStream::url('file3.txt'); |
| 200 | $this->assertFalse($this->upload->do_xss_clean()); |
| 201 | } |
| 202 | |
| 203 | function test_set_error() |
| 204 | { |
| 205 | $errors = array( |
| 206 | 'An error!' |
| 207 | ); |
| 208 | |
| 209 | $another = 'Another error!'; |
| 210 | |
| 211 | $this->upload->set_error($errors); |
| 212 | $this->assertEquals($errors, $this->upload->error_msg); |
| 213 | |
| 214 | $errors[] = $another; |
| 215 | $this->upload->set_error($another); |
| 216 | $this->assertEquals($errors, $this->upload->error_msg); |
| 217 | } |
| 218 | |
| 219 | function test_display_errors() |
| 220 | { |
| 221 | $this->upload->error_msg[] = 'Error test'; |
| 222 | $this->assertEquals('<p>Error test</p>', $this->upload->display_errors()); |
| 223 | } |
| 224 | |
| 225 | function test_mimes_types() |
| 226 | { |
| 227 | $this->assertEquals('text/plain', $this->upload->mimes_types('txt')); |
| 228 | $this->assertFalse($this->upload->mimes_types('foobar')); |
| 229 | } |
| 230 | |
| 231 | } |