summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/libraries/Upload_test.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/codeigniter/libraries/Upload_test.php')
-rw-r--r--tests/codeigniter/libraries/Upload_test.php270
1 files changed, 270 insertions, 0 deletions
diff --git a/tests/codeigniter/libraries/Upload_test.php b/tests/codeigniter/libraries/Upload_test.php
new file mode 100644
index 000000000..d79a3ffc9
--- /dev/null
+++ b/tests/codeigniter/libraries/Upload_test.php
@@ -0,0 +1,270 @@
+<?php
+
+class Upload_test extends CI_TestCase {
+
+ function set_up()
+ {
+ $obj = new stdClass;
+ $obj->upload = new Mock_Libraries_Upload();
+ $obj->security = new Mock_Core_Security();
+ $obj->lang = new Mock_Core_Lang();
+
+ $this->ci_instance($obj);
+ $this->upload = $obj->upload;
+
+ vfsStreamWrapper::register();
+ vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir'));
+
+ $this->_test_dir = vfsStreamWrapper::getRoot();
+ }
+
+ function test_do_upload()
+ {
+ $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');
+ }
+
+ function test_data()
+ {
+ $data = array(
+ 'file_name' => 'hello.txt',
+ 'file_type' => 'text/plain',
+ 'file_path' => '/tmp/',
+ 'full_path' => '/tmp/hello.txt',
+ 'raw_name' => 'hello',
+ 'orig_name' => 'hello.txt',
+ 'client_name' => '',
+ 'file_ext' => '.txt',
+ 'file_size' => 100,
+ 'is_image' => FALSE,
+ 'image_width' => '',
+ 'image_height' => '',
+ 'image_type' => '',
+ 'image_size_str' => ''
+ );
+
+ $this->upload->set_upload_path('/tmp/');
+
+ foreach ($data as $k => $v)
+ {
+ $this->upload->{$k} = $v;
+ }
+
+ $this->assertEquals('hello.txt', $this->upload->data('file_name'));
+ $this->assertEquals($data, $this->upload->data());
+ }
+
+ function test_set_upload_path()
+ {
+ $this->upload->set_upload_path('/tmp/');
+ $this->assertEquals('/tmp/', $this->upload->upload_path);
+
+ $this->upload->set_upload_path('/tmp');
+ $this->assertEquals('/tmp/', $this->upload->upload_path);
+ }
+
+ function test_set_filename()
+ {
+ $file1 = vfsStream::newFile('hello-world.txt')->withContent('Hello world.')->at($this->_test_dir);
+ $this->upload->file_ext = '.txt';
+
+ $this->assertEquals('helloworld.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'helloworld.txt'));
+ $this->assertEquals('hello-world1.txt', $this->upload->set_filename(vfsStream::url('testDir').'/', 'hello-world.txt'));
+ }
+
+ function test_set_max_filesize()
+ {
+ $this->upload->set_max_filesize(100);
+ $this->assertEquals(100, $this->upload->max_size);
+ }
+
+ function test_set_max_filename()
+ {
+ $this->upload->set_max_filename(100);
+ $this->assertEquals(100, $this->upload->max_filename);
+ }
+
+ function test_set_max_width()
+ {
+ $this->upload->set_max_width(100);
+ $this->assertEquals(100, $this->upload->max_width);
+ }
+
+ function test_set_max_height()
+ {
+ $this->upload->set_max_height(100);
+ $this->assertEquals(100, $this->upload->max_height);
+ }
+
+ function test_set_allowed_types()
+ {
+ $this->upload->set_allowed_types('*');
+ $this->assertEquals('*', $this->upload->allowed_types);
+
+ $this->upload->set_allowed_types('foo|bar');
+ $this->assertEquals(array('foo', 'bar'), $this->upload->allowed_types);
+ }
+
+ function test_set_image_properties()
+ {
+ $this->upload->file_type = 'image/gif';
+ $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
+
+ $props = array(
+ 'image_width' => 170,
+ 'image_height' => 73,
+ 'image_type' => 'gif',
+ 'image_size_str' => 'width="170" height="73"'
+ );
+
+ $this->upload->set_image_properties($this->upload->file_temp);
+
+ $this->assertEquals($props['image_width'], $this->upload->image_width);
+ $this->assertEquals($props['image_height'], $this->upload->image_height);
+ $this->assertEquals($props['image_type'], $this->upload->image_type);
+ $this->assertEquals($props['image_size_str'], $this->upload->image_size_str);
+ }
+
+ function test_set_xss_clean()
+ {
+ $this->upload->set_xss_clean(TRUE);
+ $this->assertTrue($this->upload->xss_clean);
+
+ $this->upload->set_xss_clean(FALSE);
+ $this->assertFalse($this->upload->xss_clean);
+ }
+
+ function test_is_image()
+ {
+ $this->upload->file_type = 'image/x-png';
+ $this->assertTrue($this->upload->is_image());
+
+ $this->upload->file_type = 'text/plain';
+ $this->assertFalse($this->upload->is_image());
+ }
+
+ function test_is_allowed_filetype()
+ {
+ $this->upload->allowed_types = array('html', 'gif');
+
+ $this->upload->file_ext = '.txt';
+ $this->upload->file_type = 'text/plain';
+ $this->assertFalse($this->upload->is_allowed_filetype(FALSE));
+ $this->assertFalse($this->upload->is_allowed_filetype(TRUE));
+
+ $this->upload->file_ext = '.html';
+ $this->upload->file_type = 'text/html';
+ $this->assertTrue($this->upload->is_allowed_filetype(FALSE));
+ $this->assertTrue($this->upload->is_allowed_filetype(TRUE));
+
+ $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
+ $this->upload->file_ext = '.gif';
+ $this->upload->file_type = 'image/gif';
+ $this->assertTrue($this->upload->is_allowed_filetype());
+ }
+
+ function test_is_allowed_filesize()
+ {
+ $this->upload->max_size = 100;
+ $this->upload->file_size = 99;
+
+ $this->assertTrue($this->upload->is_allowed_filesize());
+
+ $this->upload->file_size = 101;
+ $this->assertFalse($this->upload->is_allowed_filesize());
+ }
+
+ function test_is_allowed_dimensions()
+ {
+ $this->upload->file_type = 'text/plain';
+ $this->assertTrue($this->upload->is_allowed_dimensions());
+
+ $this->upload->file_type = 'image/gif';
+ $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
+
+ $this->upload->max_width = 10;
+ $this->assertFalse($this->upload->is_allowed_dimensions());
+
+ $this->upload->max_width = 170;
+ $this->upload->max_height = 10;
+ $this->assertFalse($this->upload->is_allowed_dimensions());
+
+ $this->upload->max_height = 73;
+ $this->assertTrue($this->upload->is_allowed_dimensions());
+ }
+
+ function test_validate_upload_path()
+ {
+ $this->upload->upload_path = '';
+ $this->assertFalse($this->upload->validate_upload_path());
+
+ $this->upload->upload_path = vfsStream::url('testDir');
+ $this->assertTrue($this->upload->validate_upload_path());
+ }
+
+ function test_get_extension()
+ {
+ $this->assertEquals('.txt', $this->upload->get_extension('hello.txt'));
+ $this->assertEquals('.htaccess', $this->upload->get_extension('.htaccess'));
+ $this->assertEquals('', $this->upload->get_extension('hello'));
+ }
+
+ function test_clean_file_name()
+ {
+ $this->assertEquals('hello.txt', $this->upload->clean_file_name('hello.txt'));
+ $this->assertEquals('hello.txt', $this->upload->clean_file_name('%253chell>o.txt'));
+ }
+
+ function test_limit_filename_length()
+ {
+ $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello.txt', 10));
+ $this->assertEquals('hello.txt', $this->upload->limit_filename_length('hello-world.txt', 9));
+ }
+
+ function test_do_xss_clean()
+ {
+ $file1 = vfsStream::newFile('file1.txt')->withContent('The billy goat was waiting for them.')->at($this->_test_dir);
+ $file2 = vfsStream::newFile('file2.txt')->at($this->_test_dir);
+ $file3 = vfsStream::newFile('file3.txt')->withContent('<script type="text/javascript">alert("Boo! said the billy goat")</script>')->at($this->_test_dir);
+
+ $this->upload->file_temp = vfsStream::url('file1.txt');
+ $this->assertTrue($this->upload->do_xss_clean());
+
+ $this->upload->file_temp = vfsStream::url('file2.txt');
+ $this->assertFalse($this->upload->do_xss_clean());
+
+ $this->upload->file_temp = vfsStream::url('file3.txt');
+ $this->assertFalse($this->upload->do_xss_clean());
+
+ $this->upload->file_temp = 'tests/mocks/uploads/ci_logo.gif';
+ $this->assertTrue($this->upload->do_xss_clean());
+ }
+
+ function test_set_error()
+ {
+ $errors = array(
+ 'An error!'
+ );
+
+ $another = 'Another error!';
+
+ $this->upload->set_error($errors);
+ $this->assertEquals($errors, $this->upload->error_msg);
+
+ $errors[] = $another;
+ $this->upload->set_error($another);
+ $this->assertEquals($errors, $this->upload->error_msg);
+ }
+
+ function test_display_errors()
+ {
+ $this->upload->error_msg[] = 'Error test';
+ $this->assertEquals('<p>Error test</p>', $this->upload->display_errors());
+ }
+
+ function test_mimes_types()
+ {
+ $this->assertEquals('text/plain', $this->upload->mimes_types('txt'));
+ $this->assertFalse($this->upload->mimes_types('foobar'));
+ }
+
+} \ No newline at end of file