summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter
diff options
context:
space:
mode:
Diffstat (limited to 'tests/codeigniter')
-rw-r--r--tests/codeigniter/core/Config_test.php197
-rw-r--r--tests/codeigniter/helpers/captcha_helper_test.php10
-rw-r--r--tests/codeigniter/helpers/cookie_helper_test.php59
-rw-r--r--tests/codeigniter/helpers/download_helper_test.php10
-rw-r--r--tests/codeigniter/helpers/language_helper_test.php14
-rw-r--r--tests/codeigniter/helpers/security_helper_test.php64
-rw-r--r--tests/codeigniter/helpers/url_helper_test.php2
-rw-r--r--tests/codeigniter/libraries/Upload_test.php10
8 files changed, 322 insertions, 44 deletions
diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php
index 80e0862ff..d652a625e 100644
--- a/tests/codeigniter/core/Config_test.php
+++ b/tests/codeigniter/core/Config_test.php
@@ -7,11 +7,12 @@ class Config_test extends CI_TestCase {
$cls =& $this->ci_core_class('cfg');
// set predictable config values
- $this->ci_set_config(array(
+ $this->cfg = array(
'index_page' => 'index.php',
'base_url' => 'http://example.com/',
'subclass_prefix' => 'MY_'
- ));
+ );
+ $this->ci_set_config($this->cfg);
$this->config = new $cls;
}
@@ -20,7 +21,7 @@ class Config_test extends CI_TestCase {
public function test_item()
{
- $this->assertEquals('http://example.com/', $this->config->item('base_url'));
+ $this->assertEquals($this->cfg['base_url'], $this->config->item('base_url'));
// Bad Config value
$this->assertFalse($this->config->item('no_good_item'));
@@ -48,36 +49,103 @@ class Config_test extends CI_TestCase {
// Bad Config value
$this->assertFalse($this->config->slash_item('no_good_item'));
- $this->assertEquals('http://example.com/', $this->config->slash_item('base_url'));
+ $this->assertEquals($this->cfg['base_url'], $this->config->slash_item('base_url'));
- $this->assertEquals('MY_/', $this->config->slash_item('subclass_prefix'));
+ $this->assertEquals($this->cfg['subclass_prefix'].'/', $this->config->slash_item('subclass_prefix'));
}
// --------------------------------------------------------------------
- public function test_site_url()
+ public function test_base_url()
{
- $this->assertEquals('http://example.com/index.php', $this->config->site_url());
+ // Test regular base URL
+ $base_url = $this->cfg['base_url'];
+ $this->assertEquals($base_url, $this->config->base_url());
+
+ // Test with URI
+ $uri = 'test';
+ $this->assertEquals($base_url.$uri, $this->config->base_url($uri));
+
+ // Clear base_url
+ $this->ci_set_config('base_url', '');
+
+ // Rerun constructor
+ $cls =& $this->ci_core_class('cfg');
+ $this->config = new $cls;
- $base_url = $this->config->item('base_url');
+ // Test default base
+ $this->assertEquals('http://localhost/', $this->config->base_url());
+ // Capture server vars
+ $old_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : NULL;
+ $old_script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL;
+ $old_https = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : NULL;
+
+ // Setup server vars for detection
+ $host = 'test.com';
+ $path = '/path/';
+ $script = 'base_test.php';
+ $_SERVER['HTTP_HOST'] = $host;
+ $_SERVER['SCRIPT_NAME'] = $path.$script;
+
+ // Rerun constructor
+ $this->config = new $cls;
+
+ // Test plain detected
+ $this->assertEquals('http://'.$host.$path, $this->config->base_url());
+
+ // Rerun constructor
+ $_SERVER['HTTPS'] = 'on';
+ $this->config = new $cls;
+
+ // Test secure detected
+ $this->assertEquals('https://'.$host.$path, $this->config->base_url());
+
+ // Restore server vars
+ if ($old_host === NULL) unset($_SERVER['HTTP_HOST']);
+ else $_SERVER['HTTP_HOST'] = $old_host;
+ if ($old_script === NULL) unset($_SERVER['SCRIPT_NAME']);
+ else $_SERVER['SCRIPT_NAME'] = $old_script;
+ if ($old_https === NULL) unset($_SERVER['HTTPS']);
+ else $_SERVER['HTTPS'] = $old_https;
+ }
+
+ // --------------------------------------------------------------------
+
+ public function test_site_url()
+ {
+ $base_url = $this->cfg['base_url'];
+ $index_page = $this->cfg['index_page'];
+ $this->assertEquals($base_url.$index_page, $this->config->site_url());
+
+ $old_base = $this->config->item('base_url');
$this->config->set_item('base_url', '');
$q_string = $this->config->item('enable_query_strings');
-
$this->config->set_item('enable_query_strings', FALSE);
- $this->assertEquals('index.php/test', $this->config->site_url('test'));
- $this->assertEquals('index.php/test/1', $this->config->site_url(array('test', '1')));
+ $uri= 'test';
+ $uri2 = '1';
+ $this->assertEquals($index_page.'/'.$uri, $this->config->site_url($uri));
+ $this->assertEquals($index_page.'/'.$uri.'/'.$uri2, $this->config->site_url(array($uri, $uri2)));
+
+ $suffix = 'ing';
+ $this->config->set_item('url_suffix', $suffix);
+
+ $arg = 'pass';
+ $this->assertEquals($index_page.'/'.$uri.$suffix, $this->config->site_url($uri));
+ $this->assertEquals($index_page.'/'.$uri.$suffix.'?'.$arg, $this->config->site_url($uri.'?'.$arg));
+
+ $this->config->set_item('url_suffix', FALSE);
$this->config->set_item('enable_query_strings', TRUE);
- $this->assertEquals('index.php?test', $this->config->site_url('test'));
- $this->assertEquals('index.php?0=test&1=1', $this->config->site_url(array('test', '1')));
+ $this->assertEquals($index_page.'?'.$uri, $this->config->site_url($uri));
+ $this->assertEquals($index_page.'?0='.$uri.'&1='.$uri2, $this->config->site_url(array($uri, $uri2)));
- $this->config->set_item('base_url', $base_url);
+ $this->config->set_item('base_url', $old_base);
- $this->assertEquals('http://example.com/index.php?test', $this->config->site_url('test'));
+ $this->assertEquals($base_url.$index_page.'?'.$uri, $this->config->site_url($uri));
// back to home base
$this->config->set_item('enable_query_strings', $q_string);
@@ -87,49 +155,100 @@ class Config_test extends CI_TestCase {
public function test_system_url()
{
- $this->assertEquals('http://example.com/system/', $this->config->system_url());
+ $this->assertEquals($this->cfg['base_url'].'system/', $this->config->system_url());
}
// --------------------------------------------------------------------
public function test_load()
{
- // Create config files in VFS
- $file1 = 'test.php';
- $file2 = 'secttest';
- $key1 = 'testconfig';
- $val1 = 'my_value';
- $cfg1 = array(
- $key1 => $val1
- );
- $cfg2 = array(
+ // Test regular load
+ $file = 'test.php';
+ $key = 'testconfig';
+ $val = 'my_value';
+ $cfg = array($key => $val);
+ $this->ci_vfs_create($file, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
+ $this->assertTrue($this->config->load($file));
+ $this->assertEquals($val, $this->config->item($key));
+
+ // Test reload - value should not change
+ $val2 = 'new_value';
+ $cfg = array($key => $val2);
+ $this->ci_vfs_create($file, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
+ $this->assertTrue($this->config->load($file));
+ $this->assertEquals($val, $this->config->item($key));
+
+ // Test section load
+ $file = 'secttest';
+ $cfg = array(
'one' => 'prime',
'two' => 2,
'three' => true
);
- $this->ci_vfs_create(array(
- $file1 => '<?php $config = '.var_export($cfg1, TRUE).';',
- $file2.'.php' => '<?php $config = '.var_export($cfg2, TRUE).';'
- ), '', $this->ci_app_root, 'config');
+ $this->ci_vfs_create($file.'.php', '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config');
+ $this->assertTrue($this->config->load($file, TRUE));
+ $this->assertEquals($cfg, $this->config->item($file));
- // Test regular load
- $this->assertTrue($this->config->load($file1));
- $this->assertEquals($val1, $this->config->item($key1));
+ // Test section merge
+ $cfg2 = array(
+ 'three' => 'tres',
+ 'number' => 42,
+ 'letter' => 'Z'
+ );
+ $pkg_dir = 'package';
+ $this->ci_vfs_create($file.'.php', '<?php $config = '.var_export($cfg2, TRUE).';', $this->ci_app_root,
+ array($pkg_dir, 'config'));
+ array_push($this->config->_config_paths, $this->ci_vfs_path($pkg_dir.'/', APPPATH));
+ $this->assertTrue($this->config->load($file, TRUE));
+ $this->assertEquals(array_merge($cfg, $cfg2), $this->config->item($file));
+ array_pop($this->config->_config_paths);
+
+ // Test graceful fail of invalid file
+ $file = 'badfile';
+ $this->ci_vfs_create($file, '', $this->ci_app_root, 'config');
+ $this->assertFalse($this->config->load($file, FALSE, TRUE));
+
+ // Test regular fail of invalid file
+ $this->setExpectedException(
+ 'RuntimeException',
+ 'CI Error: Your '.$this->ci_vfs_path('config/'.$file.'.php', APPPATH).
+ ' file does not appear to contain a valid configuration array.'
+ );
+ $this->assertNull($this->config->load($file));
+ }
- // Test section load
- $this->assertTrue($this->config->load($file2, TRUE));
- $this->assertEquals($cfg2, $this->config->item($file2));
+ // --------------------------------------------------------------------
- // Test graceful fail
+ public function test_load_nonexistent()
+ {
+ // Test graceful fail of nonexistent file
$this->assertFalse($this->config->load('not_config_file', FALSE, TRUE));
// Test regular fail
- $file3 = 'absentia';
+ $file = 'absentia';
$this->setExpectedException(
'RuntimeException',
- 'CI Error: The configuration file '.$file3.'.php does not exist.'
+ 'CI Error: The configuration file '.$file.'.php does not exist.'
+ );
+ $this->assertNull($this->config->load($file));
+ }
+
+ // --------------------------------------------------------------------
+
+ public function test_assign_to_config()
+ {
+ $key1 = 'test';
+ $key2 = '1';
+ $val1 = 'foo';
+ $val2 = 'bar';
+ $cfg = array(
+ $key1 => $val1,
+ $key2 => $val2
);
- $this->assertNull($this->config->load($file3));
+
+ $this->assertNull($this->config->_assign_to_config($cfg));
+ $this->assertEquals($val1, $this->config->item($key1));
+ $this->assertEquals($val2, $this->config->item($key2));
}
} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/captcha_helper_test.php b/tests/codeigniter/helpers/captcha_helper_test.php
new file mode 100644
index 000000000..fc86305e3
--- /dev/null
+++ b/tests/codeigniter/helpers/captcha_helper_test.php
@@ -0,0 +1,10 @@
+<?php
+
+class Captcha_helper_test extends CI_TestCase {
+
+ public function test_create_captcha()
+ {
+ $this->markTestSkipped('Cant easily test');
+ }
+
+} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/cookie_helper_test.php b/tests/codeigniter/helpers/cookie_helper_test.php
new file mode 100644
index 000000000..fba68f20f
--- /dev/null
+++ b/tests/codeigniter/helpers/cookie_helper_test.php
@@ -0,0 +1,59 @@
+<?php
+
+class Cookie_helper_test extends CI_TestCase {
+
+ public function set_up()
+ {
+ $this->helper('cookie');
+ }
+
+ // ------------------------------------------------------------------------
+
+ function test_set_cookie()
+ {
+ /*$input_cls = $this->ci_core_class('input');
+ $this->ci_instance_var('input', new $input_cls);
+
+ $this->assertTrue(set_cookie(
+ 'my_cookie',
+ 'foobar'
+ ));*/
+
+ $this->markTestSkipped('Need to find a way to overcome a headers already set exception');
+ }
+
+ // ------------------------------------------------------------------------
+
+ function test_get_cookie()
+ {
+ $_COOKIE['foo'] = 'bar';
+
+ $security = new Mock_Core_Security();
+ $utf8 = new Mock_Core_Utf8();
+ $input_cls = $this->ci_core_class('input');
+ $this->ci_instance_var('input', new Mock_Core_Input($security, $utf8));
+
+ $this->assertEquals('bar', get_cookie('foo', FALSE));
+ $this->assertEquals('bar', get_cookie('foo', TRUE));
+
+ $_COOKIE['bar'] = "Hello, i try to <script>alert('Hack');</script> your site";
+
+ $this->assertEquals("Hello, i try to [removed]alert&#40;'Hack'&#41;;[removed] your site", get_cookie('bar', TRUE));
+ $this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", get_cookie('bar', FALSE));
+ }
+
+ // ------------------------------------------------------------------------
+
+ function test_delete_cookie()
+ {
+ /*$input_cls = $this->ci_core_class('input');
+ $this->ci_instance_var('input', new $input_cls);
+
+ $this->assertTrue(delete_cookie(
+ 'my_cookie'
+ ));*/
+
+ $this->markTestSkipped('Need to find a way to overcome a headers already set exception');
+ }
+
+} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/download_helper_test.php b/tests/codeigniter/helpers/download_helper_test.php
new file mode 100644
index 000000000..d2b42e46b
--- /dev/null
+++ b/tests/codeigniter/helpers/download_helper_test.php
@@ -0,0 +1,10 @@
+<?php
+
+class Download_helper_test extends CI_TestCase {
+
+ public function test_force_download()
+ {
+ $this->markTestSkipped('Cant easily test');
+ }
+
+} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/language_helper_test.php b/tests/codeigniter/helpers/language_helper_test.php
new file mode 100644
index 000000000..06932b9fd
--- /dev/null
+++ b/tests/codeigniter/helpers/language_helper_test.php
@@ -0,0 +1,14 @@
+<?php
+
+class Language_helper_test extends CI_TestCase {
+
+ public function test_lang()
+ {
+ $this->helper('language');
+ $this->ci_instance_var('lang', new Mock_Core_Lang());
+
+ $this->assertFalse(lang(1));
+ $this->assertEquals('<label for="foo"></label>', lang(1, 'foo'));
+ }
+
+} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/security_helper_test.php b/tests/codeigniter/helpers/security_helper_test.php
new file mode 100644
index 000000000..effd3ec02
--- /dev/null
+++ b/tests/codeigniter/helpers/security_helper_test.php
@@ -0,0 +1,64 @@
+<?php
+
+class Security_helper_tests extends CI_TestCase {
+
+ function setUp()
+ {
+ $this->helper('security');
+ $obj = new stdClass;
+ $obj->security = new Mock_Core_Security();
+ $this->ci_instance($obj);
+ }
+
+ function test_xss_clean()
+ {
+ $this->assertEquals('foo', xss_clean('foo'));
+
+ $this->assertEquals("Hello, i try to [removed]alert&#40;'Hack'&#41;;[removed] your site", xss_clean("Hello, i try to <script>alert('Hack');</script> your site"));
+ }
+
+ function test_sanitize_filename()
+ {
+ $this->assertEquals('hello.doc', sanitize_filename('hello.doc'));
+
+ $filename = './<!--foo-->';
+ $this->assertEquals('foo', sanitize_filename($filename));
+ }
+
+ function test_do_hash()
+ {
+ $md5 = md5('foo');
+ $sha1 = sha1('foo');
+
+ $algos = hash_algos();
+ $algo_results = array();
+ foreach ($algos as $k => $v)
+ {
+ $algo_results[$v] = hash($v, 'foo');
+ }
+
+ $this->assertEquals($sha1, do_hash('foo'));
+ $this->assertEquals($sha1, do_hash('foo', 'sha1'));
+ $this->assertEquals($md5, do_hash('foo', 'md5'));
+ $this->assertEquals($md5, do_hash('foo', 'foobar'));
+
+ // Test each algorithm available to PHP
+ foreach ($algo_results as $algo => $result)
+ {
+ $this->assertEquals($result, do_hash('foo', $algo));
+ }
+ }
+
+ function test_strip_image_tags()
+ {
+ $this->assertEquals('http://example.com/spacer.gif', strip_image_tags('http://example.com/spacer.gif'));
+
+ $this->assertEquals('http://example.com/spacer.gif', strip_image_tags('<img src="http://example.com/spacer.gif" alt="Who needs CSS when you have a spacer.gif?" />'));
+ }
+
+ function test_encode_php_tags()
+ {
+ $this->assertEquals('&lt;? echo $foo; ?&gt;', encode_php_tags('<? echo $foo; ?>'));
+ }
+
+} \ No newline at end of file
diff --git a/tests/codeigniter/helpers/url_helper_test.php b/tests/codeigniter/helpers/url_helper_test.php
index c81c5f1b8..5fc364238 100644
--- a/tests/codeigniter/helpers/url_helper_test.php
+++ b/tests/codeigniter/helpers/url_helper_test.php
@@ -51,6 +51,8 @@ class Url_helper_test extends CI_TestCase {
'www.codeigniter.com test' => '<a href="http://www.codeigniter.com">http://www.codeigniter.com</a> test',
'This is my noreply@codeigniter.com test' => 'This is my noreply@codeigniter.com test',
'<br />www.google.com' => '<br /><a href="http://www.google.com">http://www.google.com</a>',
+ 'Download CodeIgniter at www.codeigniter.com. Period test.' => 'Download CodeIgniter at <a href="http://www.codeigniter.com">http://www.codeigniter.com</a>. Period test.',
+ 'Download CodeIgniter at www.codeigniter.com, comma test' => 'Download CodeIgniter at <a href="http://www.codeigniter.com">http://www.codeigniter.com</a>, comma test'
);
foreach ($strings as $in => $out)
diff --git a/tests/codeigniter/libraries/Upload_test.php b/tests/codeigniter/libraries/Upload_test.php
index 827942773..546cebc59 100644
--- a/tests/codeigniter/libraries/Upload_test.php
+++ b/tests/codeigniter/libraries/Upload_test.php
@@ -11,9 +11,9 @@ class Upload_test extends CI_TestCase {
$this->upload = $ci->upload;
}
- function test_do_upload()
+ 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');
+ $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');
}
function test_data()
@@ -72,7 +72,7 @@ class Upload_test extends CI_TestCase {
{
$this->upload->set_max_filesize(100);
$this->assertEquals(100, $this->upload->max_size);
- }
+ }
function test_set_max_filename()
{
@@ -84,7 +84,7 @@ class Upload_test extends CI_TestCase {
{
$this->upload->set_max_width(100);
$this->assertEquals(100, $this->upload->max_width);
- }
+ }
function test_set_max_height()
{
@@ -178,7 +178,7 @@ class Upload_test extends CI_TestCase {
$this->upload->file_type = 'image/gif';
$this->upload->file_temp = realpath(PROJECT_BASE.'tests/mocks/uploads/ci_logo.gif');
- $this->upload->max_width = 10;
+ $this->upload->max_width = 10;
$this->assertFalse($this->upload->is_allowed_dimensions());
$this->upload->max_width = 170;