summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/core
diff options
context:
space:
mode:
Diffstat (limited to 'tests/codeigniter/core')
-rw-r--r--tests/codeigniter/core/Config_test.php9
-rw-r--r--tests/codeigniter/core/Input_test.php76
-rw-r--r--tests/codeigniter/core/Loader_test.php36
-rw-r--r--tests/codeigniter/core/Log_test.php12
-rw-r--r--tests/codeigniter/core/Output_test.php2
-rw-r--r--tests/codeigniter/core/Security_test.php9
-rw-r--r--tests/codeigniter/core/URI_test.php9
-rw-r--r--tests/codeigniter/core/Utf8_test.php51
-rw-r--r--tests/codeigniter/core/compat/standard_test.php32
9 files changed, 137 insertions, 99 deletions
diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php
index b5c9e849d..5201d46dc 100644
--- a/tests/codeigniter/core/Config_test.php
+++ b/tests/codeigniter/core/Config_test.php
@@ -152,13 +152,6 @@ class Config_test extends CI_TestCase {
// --------------------------------------------------------------------
- public function test_system_url()
- {
- $this->assertEquals($this->cfg['base_url'].'system/', $this->config->system_url());
- }
-
- // --------------------------------------------------------------------
-
public function test_load()
{
// Test regular load
@@ -237,4 +230,4 @@ class Config_test extends CI_TestCase {
$this->assertNull($this->config->load($file));
}
-} \ No newline at end of file
+}
diff --git a/tests/codeigniter/core/Input_test.php b/tests/codeigniter/core/Input_test.php
index 3cce00414..93d1b7118 100644
--- a/tests/codeigniter/core/Input_test.php
+++ b/tests/codeigniter/core/Input_test.php
@@ -12,12 +12,8 @@ class Input_test extends CI_TestCase {
$this->ci_set_config('global_xss_filtering', FALSE);
$this->ci_set_config('csrf_protection', FALSE);
- $security = new Mock_Core_Security();
-
- $this->ci_set_config('charset', 'UTF-8');
- $utf8 = new Mock_Core_Utf8();
-
- $this->input = new Mock_Core_Input($security, $utf8);
+ $security = new Mock_Core_Security('UTF-8');
+ $this->input = new CI_Input($security);
}
// --------------------------------------------------------------------
@@ -102,6 +98,23 @@ class Input_test extends CI_TestCase {
// --------------------------------------------------------------------
+ public function test_post_get_array_notation()
+ {
+ $_SERVER['REQUEST_METHOD'] = 'POST';
+ $_POST['foo'] = array('bar' => 'baz');
+ $barArray = array('bar' => 'baz');
+
+ $this->assertEquals('baz', $this->input->get_post('foo[bar]'));
+ $this->assertEquals($barArray, $this->input->get_post('foo[]'));
+ $this->assertNull($this->input->get_post('foo[baz]'));
+
+ $this->assertEquals('baz', $this->input->post_get('foo[bar]'));
+ $this->assertEquals($barArray, $this->input->post_get('foo[]'));
+ $this->assertNull($this->input->post_get('foo[baz]'));
+ }
+
+ // --------------------------------------------------------------------
+
public function test_get_post()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
@@ -112,6 +125,23 @@ class Input_test extends CI_TestCase {
// --------------------------------------------------------------------
+ public function test_get_post_array_notation()
+ {
+ $_SERVER['REQUEST_METHOD'] = 'GET';
+ $_GET['foo'] = array('bar' => 'baz');
+ $barArray = array('bar' => 'baz');
+
+ $this->assertEquals('baz', $this->input->get_post('foo[bar]'));
+ $this->assertEquals($barArray, $this->input->get_post('foo[]'));
+ $this->assertNull($this->input->get_post('foo[baz]'));
+
+ $this->assertEquals('baz', $this->input->post_get('foo[bar]'));
+ $this->assertEquals($barArray, $this->input->post_get('foo[]'));
+ $this->assertNull($this->input->post_get('foo[baz]'));
+ }
+
+ // --------------------------------------------------------------------
+
public function test_cookie()
{
$_COOKIE['foo'] = 'bar';
@@ -130,14 +160,17 @@ class Input_test extends CI_TestCase {
public function test_fetch_from_array()
{
+ $reflection = new ReflectionMethod($this->input, '_fetch_from_array');
+ $reflection->setAccessible(TRUE);
+
$data = array(
'foo' => 'bar',
'harm' => 'Hello, i try to <script>alert(\'Hack\');</script> your site',
);
- $foo = $this->input->fetch_from_array($data, 'foo');
- $harm = $this->input->fetch_from_array($data, 'harm');
- $harmless = $this->input->fetch_from_array($data, 'harm', TRUE);
+ $foo = $reflection->invokeArgs($this->input, [&$data, 'foo']);
+ $harm = $reflection->invokeArgs($this->input, [&$data, 'harm']);
+ $harmless = $reflection->invokeArgs($this->input, [&$data, 'harm', TRUE]);
$this->assertEquals('bar', $foo);
$this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $harm);
@@ -225,57 +258,60 @@ class Input_test extends CI_TestCase {
public function test_ip_address()
{
- $this->input->ip_address = '127.0.0.1';
+ $reflection = new ReflectionProperty($this->input, 'ip_address');
+ $reflection->setAccessible(TRUE);
+
+ $reflection->setValue($this->input, '127.0.0.1');
$this->assertEquals('127.0.0.1', $this->input->ip_address());
// 127.0.0.1 is set in our Bootstrap file
- $this->input->ip_address = FALSE;
+ $reflection->setValue($this->input, FALSE);
$this->assertEquals('127.0.0.1', $this->input->ip_address());
// Invalid
$_SERVER['REMOTE_ADDR'] = 'invalid_ip_address';
- $this->input->ip_address = FALSE; // reset cached value
+ $reflection->setValue($this->input, FALSE); // reset cached value
$this->assertEquals('0.0.0.0', $this->input->ip_address());
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Proxy_ips tests
- $this->input->ip_address = FALSE;
+ $reflection->setValue($this->input, FALSE);
$this->ci_set_config('proxy_ips', '127.0.0.3, 127.0.0.4, 127.0.0.2');
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.2';
$this->assertEquals('127.0.0.1', $this->input->ip_address());
// Invalid spoof
- $this->input->ip_address = FALSE;
+ $reflection->setValue($this->input, FALSE);
$this->ci_set_config('proxy_ips', 'invalid_ip_address');
$_SERVER['HTTP_CLIENT_IP'] = 'invalid_ip_address';
$this->assertEquals('127.0.0.1', $this->input->ip_address());
- $this->input->ip_address = FALSE;
+ $reflection->setValue($this->input, FALSE);
$this->ci_set_config('proxy_ips', 'http://foo/bar/baz, 127.0.0.1/1');
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.1';
$this->assertEquals('127.0.0.1', $this->input->ip_address());
- $this->input->ip_address = FALSE;
+ $reflection->setValue($this->input, FALSE);
$this->ci_set_config('proxy_ips', 'http://foo/bar/baz, 127.0.0.2');
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.2';
$_SERVER['REMOTE_ADDR'] = '127.0.0.2';
$this->assertEquals('127.0.0.2', $this->input->ip_address());
- //IPv6
- $this->input->ip_address = FALSE;
+ // IPv6
+ $reflection->setValue($this->input, FALSE);
$this->ci_set_config('proxy_ips', 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329/1, FE80:0000:0000:0000:0202:B3FF:FE1E:8300/2');
$_SERVER['HTTP_CLIENT_IP'] = 'FE80:0000:0000:0000:0202:B3FF:FE1E:8300';
$_SERVER['REMOTE_ADDR'] = 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329';
$this->assertEquals('FE80:0000:0000:0000:0202:B3FF:FE1E:8300', $this->input->ip_address());
- $this->input->ip_address = FALSE;
+ $reflection->setValue($this->input, FALSE);
$this->ci_set_config('proxy_ips', '0::/32');
$_SERVER['HTTP_CLIENT_IP'] = '127.0.0.7';
$_SERVER['REMOTE_ADDR'] = '0000:0000:0000:0000:0000:0000:0000:0001';
$this->assertEquals('127.0.0.7', $this->input->ip_address());
- $this->input->ip_address = FALSE;
+ $reflection->setValue($this->input, FALSE);
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // back to reality
}
diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php
index 6a7aa916a..df9c9f44b 100644
--- a/tests/codeigniter/core/Loader_test.php
+++ b/tests/codeigniter/core/Loader_test.php
@@ -277,6 +277,25 @@ class Loader_test extends CI_TestCase {
// --------------------------------------------------------------------
+ public function test_invalid_model()
+ {
+ $this->ci_set_core_class('model', 'CI_Model');
+
+ // Create model in VFS
+ $model = 'Unit_test_invalid_model';
+ $content = '<?php class '.$model.' {} ';
+ $this->ci_vfs_create($model, $content, $this->ci_app_root, 'models');
+
+ // Test no extending
+ $this->setExpectedException(
+ 'RuntimeException',
+ 'Class '.$model.' doesn\'t extend CI_Model'
+ );
+ $this->load->model($model);
+ }
+
+ // --------------------------------------------------------------------
+
// public function testDatabase()
// {
// $this->assertInstanceOf('CI_Loader', $this->load->database());
@@ -292,12 +311,16 @@ class Loader_test extends CI_TestCase {
$var = 'hello';
$value = 'World!';
$content = 'This is my test page. ';
- $this->ci_vfs_create($view, $content.'<?php echo $'.$var.';', $this->ci_app_root, 'views');
+ $this->ci_vfs_create($view, $content.'<?php echo (isset($'.$var.') ? $'.$var.' : "undefined");', $this->ci_app_root, 'views');
// Test returning view
$out = $this->load->view($view, array($var => $value), TRUE);
$this->assertEquals($content.$value, $out);
+ // Test view with missing parameter in $vars
+ $out = $this->load->view($view, [], TRUE);
+ $this->assertEquals($content.'undefined', $out);
+
// Mock output class
$output = $this->getMockBuilder('CI_Output')->setMethods(array('append_output'))->getMock();
$output->expects($this->once())->method('append_output')->with($content.$value);
@@ -307,6 +330,15 @@ class Loader_test extends CI_TestCase {
$vars = new stdClass();
$vars->$var = $value;
$this->assertInstanceOf('CI_Loader', $this->load->view($view, $vars));
+
+ // Create another view in VFS, nesting the first one without its own $vars
+ $nesting_view = 'unit_test_nesting_view';
+ $nesting_content = 'Here comes a nested view. ';
+ $this->ci_vfs_create($nesting_view, $nesting_content.'<?php $loader->view("'.$view.'");', $this->ci_app_root, 'views');
+
+ // Test $vars inheritance to nested views
+ $out = $this->load->view($nesting_view, array("loader" => $this->load, $var => $value), TRUE);
+ $this->assertEquals($nesting_content.$content.$value, $out);
}
// --------------------------------------------------------------------
@@ -552,7 +584,7 @@ class Loader_test extends CI_TestCase {
$dir = 'testdir';
$path = APPPATH.$dir.'/';
$model = 'Automod';
- $this->ci_vfs_create($model, '<?php class '.$model.' { }', $this->ci_app_root, array($dir, 'models'));
+ $this->ci_vfs_create($model, '<?php class '.$model.' extends CI_Model { }', $this->ci_app_root, array($dir, 'models'));
// Create autoloader config
$cfg = array(
diff --git a/tests/codeigniter/core/Log_test.php b/tests/codeigniter/core/Log_test.php
index 927984385..3715949f6 100644
--- a/tests/codeigniter/core/Log_test.php
+++ b/tests/codeigniter/core/Log_test.php
@@ -9,8 +9,8 @@ class Log_test extends CI_TestCase {
$threshold->setAccessible(TRUE);
$date_fmt = new ReflectionProperty('CI_Log', '_date_fmt');
$date_fmt->setAccessible(TRUE);
- $file_ext = new ReflectionProperty('CI_Log', '_file_ext');
- $file_ext->setAccessible(TRUE);
+ $filename = new ReflectionProperty('CI_Log', '_log_filename');
+ $filename->setAccessible(TRUE);
$file_perms = new ReflectionProperty('CI_Log', '_file_permissions');
$file_perms->setAccessible(TRUE);
$enabled = new ReflectionProperty('CI_Log', '_enabled');
@@ -19,28 +19,28 @@ class Log_test extends CI_TestCase {
$this->ci_set_config('log_path', '/root/');
$this->ci_set_config('log_threshold', 'z');
$this->ci_set_config('log_date_format', 'd.m.Y');
- $this->ci_set_config('log_file_extension', '');
+ $this->ci_set_config('log_filename', '');
$this->ci_set_config('log_file_permissions', '');
$instance = new CI_Log();
$this->assertEquals($path->getValue($instance), '/root/');
$this->assertEquals($threshold->getValue($instance), 1);
$this->assertEquals($date_fmt->getValue($instance), 'd.m.Y');
- $this->assertEquals($file_ext->getValue($instance), 'php');
+ $this->assertEquals($filename->getValue($instance), 'log-'.date('Y-m-d').'.php');
$this->assertEquals($file_perms->getValue($instance), 0644);
$this->assertFalse($enabled->getValue($instance));
$this->ci_set_config('log_path', '');
$this->ci_set_config('log_threshold', '0');
$this->ci_set_config('log_date_format', '');
- $this->ci_set_config('log_file_extension', '.log');
+ $this->ci_set_config('log_filename', 'testname.log');
$this->ci_set_config('log_file_permissions', 0600);
$instance = new CI_Log();
$this->assertEquals($path->getValue($instance), APPPATH.'logs/');
$this->assertEquals($threshold->getValue($instance), 0);
$this->assertEquals($date_fmt->getValue($instance), 'Y-m-d H:i:s');
- $this->assertEquals($file_ext->getValue($instance), 'log');
+ $this->assertEquals($filename->getValue($instance), 'testname.log');
$this->assertEquals($file_perms->getValue($instance), 0600);
$this->assertEquals($enabled->getValue($instance), TRUE);
}
diff --git a/tests/codeigniter/core/Output_test.php b/tests/codeigniter/core/Output_test.php
index 887c077d7..8c99c1fc5 100644
--- a/tests/codeigniter/core/Output_test.php
+++ b/tests/codeigniter/core/Output_test.php
@@ -8,7 +8,7 @@ class Output_test extends CI_TestCase {
public function set_up()
{
$this->_output_data =<<<HTML
- <html>
+ <html lang="en">
<head>
<title>Basic HTML</title>
</head>
diff --git a/tests/codeigniter/core/Security_test.php b/tests/codeigniter/core/Security_test.php
index c650315ce..5132e5887 100644
--- a/tests/codeigniter/core/Security_test.php
+++ b/tests/codeigniter/core/Security_test.php
@@ -12,7 +12,8 @@ class Security_test extends CI_TestCase {
$this->ci_set_config('csrf_token_name', 'ci_csrf_token');
$this->ci_set_config('csrf_cookie_name', 'ci_csrf_cookie');
- $this->security = new Mock_Core_Security();
+ $_SERVER['REQUEST_METHOD'] = 'GET';
+ $this->security = new Mock_Core_Security('UTF-8');
}
// --------------------------------------------------------------------
@@ -308,7 +309,7 @@ class Security_test extends CI_TestCase {
$imgtags = array(
'<img src="smiley.gif" alt="Smiley face" height="42" width="42">',
'<img alt="Smiley face" height="42" width="42" src="smiley.gif">',
- '<img src="http://www.w3schools.com/images/w3schools_green.jpg">',
+ '<img src="https://www.w3schools.com/images/w3schools_green.jpg">',
'<img src="/img/sunset.gif" height="100%" width="100%">',
'<img src="mdn-logo-sm.png" alt="MD Logo" srcset="mdn-logo-HD.png 2x, mdn-logo-small.png 15w, mdn-banner-HD.png 100w 2x" />',
'<img sqrc="/img/sunset.gif" height="100%" width="100%">',
@@ -320,7 +321,7 @@ class Security_test extends CI_TestCase {
$urls = array(
'smiley.gif',
'smiley.gif',
- 'http://www.w3schools.com/images/w3schools_green.jpg',
+ 'https://www.w3schools.com/images/w3schools_green.jpg',
'/img/sunset.gif',
'mdn-logo-sm.png',
'<img sqrc="/img/sunset.gif" height="100%" width="100%">',
@@ -349,7 +350,7 @@ class Security_test extends CI_TestCase {
// leave csrf_cookie_name as blank to test _csrf_set_hash function
$this->ci_set_config('csrf_cookie_name', '');
- $this->security = new Mock_Core_Security();
+ $this->security = new Mock_Core_Security('UTF-8');
$this->assertNotEmpty($this->security->get_csrf_hash());
}
diff --git a/tests/codeigniter/core/URI_test.php b/tests/codeigniter/core/URI_test.php
index 42dff3639..f862c666e 100644
--- a/tests/codeigniter/core/URI_test.php
+++ b/tests/codeigniter/core/URI_test.php
@@ -119,8 +119,13 @@ class URI_test extends CI_TestCase {
*/
// --------------------------------------------------------------------
+ /**
+ * @runInSeparateProcess
+ */
public function test_filter_uri_passing()
{
+ define('UTF8_ENABLED', FALSE);
+
$this->uri->_set_permitted_uri_chars('a-z 0-9~%.:_\-');
$str = 'abc01239~%.:_-';
@@ -129,8 +134,12 @@ class URI_test extends CI_TestCase {
// --------------------------------------------------------------------
+ /**
+ * @runInSeparateProcess
+ */
public function test_filter_uri_throws_error()
{
+ define('UTF8_ENABLED', FALSE);
$this->setExpectedException('RuntimeException');
$this->uri->config->set_item('enable_query_strings', FALSE);
diff --git a/tests/codeigniter/core/Utf8_test.php b/tests/codeigniter/core/Utf8_test.php
index c02c1dd87..8ae51b8af 100644
--- a/tests/codeigniter/core/Utf8_test.php
+++ b/tests/codeigniter/core/Utf8_test.php
@@ -1,31 +1,27 @@
<?php
+/**
+ * @runTestsInSeparateProcesses
+ */
class Utf8_test extends CI_TestCase {
- public function set_up()
+ public function test___constructUTF8_ENABLED()
{
- $this->ci_set_config('charset', 'UTF-8');
- $this->utf8 = new Mock_Core_Utf8();
- $this->ci_instance_var('utf8', $this->utf8);
+ if ( ! defined('PREG_BAD_UTF8_ERROR') OR (ICONV_ENABLED === FALSE && MB_ENABLED === FALSE))
+ {
+ return $this->markTestSkipped('PCRE_UTF8 and/or both ext/mbstring & ext/iconv are unavailable');
+ }
+
+ new CI_Utf8('UTF-8');
+ $this->assertTrue(UTF8_ENABLED);
}
// --------------------------------------------------------------------
- /**
- * __construct() test
- *
- * @covers CI_Utf8::__construct
- */
- public function test___construct()
+ public function test__constructUTF8_DISABLED()
{
- if (defined('PREG_BAD_UTF8_ERROR') && (ICONV_ENABLED === TRUE OR MB_ENABLED === TRUE) && strtoupper(config_item('charset')) === 'UTF-8')
- {
- $this->assertTrue(UTF8_ENABLED);
- }
- else
- {
- $this->assertFalse(UTF8_ENABLED);
- }
+ new CI_Utf8('WINDOWS-1251');
+ $this->assertFalse(UTF8_ENABLED);
}
// --------------------------------------------------------------------
@@ -37,8 +33,9 @@ class Utf8_test extends CI_TestCase {
*/
public function test_is_ascii()
{
- $this->assertTrue($this->utf8->is_ascii('foo bar'));
- $this->assertFalse($this->utf8->is_ascii('тест'));
+ $utf8 = new CI_Utf8('UTF-8');
+ $this->assertTrue($utf8->is_ascii('foo bar'));
+ $this->assertFalse($utf8->is_ascii('тест'));
}
// --------------------------------------------------------------------
@@ -51,21 +48,22 @@ class Utf8_test extends CI_TestCase {
*/
public function test_clean_string()
{
- $this->assertEquals('foo bar', $this->utf8->clean_string('foo bar'));
+ $utf8 = new CI_Utf8('UTF-8');
+ $this->assertEquals('foo bar', $utf8->clean_string('foo bar'));
$illegal_utf8 = "\xc0тест";
if (MB_ENABLED)
{
- $this->assertEquals('тест', $this->utf8->clean_string($illegal_utf8));
+ $this->assertEquals('тест', $utf8->clean_string($illegal_utf8));
}
elseif (ICONV_ENABLED)
{
// This is a known issue, iconv doesn't always work with //IGNORE
- $this->assertContains($this->utf8->clean_string($illegal_utf8), array('тест', ''));
+ $this->assertContains($utf8->clean_string($illegal_utf8), array('тест', ''));
}
else
{
- $this->assertEquals($illegal_utf8, $this->utf8->clean_string($illegal_utf8));
+ $this->assertEquals($illegal_utf8, $utf8->clean_string($illegal_utf8));
}
}
@@ -78,13 +76,14 @@ class Utf8_test extends CI_TestCase {
*/
public function test_convert_to_utf8()
{
+ $utf8 = new CI_Utf8('UTF-8');
if (MB_ENABLED OR ICONV_ENABLED)
{
- $this->assertEquals('тест', $this->utf8->convert_to_utf8('', 'WINDOWS-1251'));
+ $this->assertEquals('тест', $utf8->convert_to_utf8('', 'WINDOWS-1251'));
}
else
{
- $this->assertFalse($this->utf8->convert_to_utf8('', 'WINDOWS-1251'));
+ $this->assertFalse($utf8->convert_to_utf8('', 'WINDOWS-1251'));
}
}
diff --git a/tests/codeigniter/core/compat/standard_test.php b/tests/codeigniter/core/compat/standard_test.php
index a98460129..54424bb9b 100644
--- a/tests/codeigniter/core/compat/standard_test.php
+++ b/tests/codeigniter/core/compat/standard_test.php
@@ -10,11 +10,6 @@ class standard_test extends CI_TestCase {
}
$this->assertTrue(function_exists('array_column'));
-
- if ( ! is_php('5.4'))
- {
- $this->assertTrue(function_exists('hex2bin'));
- }
}
// ------------------------------------------------------------------------
@@ -330,25 +325,6 @@ class standard_test extends CI_TestCase {
array_column($input, 'b', 'a')
);
}
-
- // ------------------------------------------------------------------------
-
- /**
- * hex2bin() tests
- *
- * @depends test_bootstrap
- */
- public function test_hex2bin()
- {
- if (is_php('5.4'))
- {
- return $this->markTestSkipped('hex2bin() is already available on PHP 5.4');
- }
-
- $this->assertEquals("\x03\x04", hex2bin("0304"));
- $this->assertEquals('', hex2bin(''));
- $this->assertEquals("\x01\x02\x03", hex2bin(new FooHex()));
- }
}
// ------------------------------------------------------------------------
@@ -368,11 +344,3 @@ class Bar {
return 'first_name';
}
}
-
-class FooHex {
-
- public function __toString()
- {
- return '010203';
- }
-}