From ba1fbfb44612cc7f06cb67f4ab4e2589c4961501 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Wed, 29 Aug 2012 23:23:37 +0100 Subject: Added initial upload library tests --- tests/codeigniter/libraries/Upload_test.php | 231 ++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 tests/codeigniter/libraries/Upload_test.php (limited to 'tests') diff --git a/tests/codeigniter/libraries/Upload_test.php b/tests/codeigniter/libraries/Upload_test.php new file mode 100644 index 000000000..021af28a7 --- /dev/null +++ b/tests/codeigniter/libraries/Upload_test.php @@ -0,0 +1,231 @@ +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('TODO'); + } + + 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() + { + $this->markTestIncomplete('TODO'); + } + + 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->markTestIncomplete('TODO'); + } + + 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'); + + $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->markTestIncomplete('Image tests'); + } + + 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->markTestIncomplete('TODO'); + } + + 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('')->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()); + } + + 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('

Error test

', $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 -- cgit v1.2.3-24-g4f1b