diff options
Diffstat (limited to 'tests/codeigniter/core')
-rw-r--r-- | tests/codeigniter/core/Benchmark_test.php | 43 | ||||
-rw-r--r-- | tests/codeigniter/core/Common_test.php | 47 | ||||
-rw-r--r-- | tests/codeigniter/core/Config_test.php | 208 | ||||
-rw-r--r-- | tests/codeigniter/core/Input_test.php | 141 | ||||
-rw-r--r-- | tests/codeigniter/core/Lang_test.php | 19 | ||||
-rw-r--r-- | tests/codeigniter/core/Loader_test.php | 555 | ||||
-rw-r--r-- | tests/codeigniter/core/Output_test.php | 37 | ||||
-rw-r--r-- | tests/codeigniter/core/Security_test.php | 106 | ||||
-rw-r--r-- | tests/codeigniter/core/URI_test.php | 232 |
9 files changed, 1075 insertions, 313 deletions
diff --git a/tests/codeigniter/core/Benchmark_test.php b/tests/codeigniter/core/Benchmark_test.php new file mode 100644 index 000000000..a239ba51d --- /dev/null +++ b/tests/codeigniter/core/Benchmark_test.php @@ -0,0 +1,43 @@ +<?php + +class Benchmark_test extends CI_TestCase { + + public function set_up() + { + $this->benchmark = new Mock_Core_Benchmark(); + } + + // -------------------------------------------------------------------- + + public function test_mark() + { + $this->assertEmpty($this->benchmark->marker); + + $this->benchmark->mark('code_start'); + + $this->assertEquals(1, count($this->benchmark->marker)); + $this->assertArrayHasKey('code_start', $this->benchmark->marker); + } + + // -------------------------------------------------------------------- + + public function test_elapsed_time() + { + $this->assertEquals('{elapsed_time}', $this->benchmark->elapsed_time()); + $this->assertEmpty($this->benchmark->elapsed_time('undefined_point')); + + $this->benchmark->mark('code_start'); + sleep(1); + $this->benchmark->mark('code_end'); + + $this->assertEquals('1.0', $this->benchmark->elapsed_time('code_start', 'code_end', 1)); + } + + // -------------------------------------------------------------------- + + public function test_memory_usage() + { + $this->assertEquals('{memory_usage}', $this->benchmark->memory_usage()); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Common_test.php b/tests/codeigniter/core/Common_test.php index dded2e824..999b49cb3 100644 --- a/tests/codeigniter/core/Common_test.php +++ b/tests/codeigniter/core/Common_test.php @@ -1,13 +1,52 @@ <?php class Common_test extends CI_TestCase { - - // ------------------------------------------------------------------------ - + public function test_is_php() { $this->assertEquals(TRUE, is_php('1.2.0')); $this->assertEquals(FALSE, is_php('9999.9.9')); } - + + // ------------------------------------------------------------------------ + + public function test_stringify_attributes() + { + $this->assertEquals(' class="foo" id="bar"', _stringify_attributes(array('class' => 'foo', 'id' => 'bar'))); + + $atts = new stdClass; + $atts->class = 'foo'; + $atts->id = 'bar'; + $this->assertEquals(' class="foo" id="bar"', _stringify_attributes($atts)); + + $atts = new stdClass; + $this->assertEquals('', _stringify_attributes($atts)); + + $this->assertEquals(' class="foo" id="bar"', _stringify_attributes('class="foo" id="bar"')); + + $this->assertEquals('', _stringify_attributes(array())); + } + + // ------------------------------------------------------------------------ + + public function test_stringify_js_attributes() + { + $this->assertEquals('width=800,height=600', _stringify_attributes(array('width' => '800', 'height' => '600'), TRUE)); + + $atts = new stdClass; + $atts->width = 800; + $atts->height = 600; + $this->assertEquals('width=800,height=600', _stringify_attributes($atts, TRUE)); + } + + // ------------------------------------------------------------------------ + + public function test_html_escape() + { + $this->assertEquals( + html_escape('Here is a string containing "quoted" text.'), + 'Here is a string containing "quoted" text.' + ); + } + }
\ No newline at end of file diff --git a/tests/codeigniter/core/Config_test.php b/tests/codeigniter/core/Config_test.php index 30f0cc61d..be426d070 100644 --- a/tests/codeigniter/core/Config_test.php +++ b/tests/codeigniter/core/Config_test.php @@ -5,89 +5,229 @@ class Config_test extends CI_TestCase { public function set_up() { $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/', + 'base_url' => 'http://example.com/', 'subclass_prefix' => 'MY_' - )); + ); + $this->ci_set_config($this->cfg); - $this->config = new $cls; + $this->config = new $cls; } - + // -------------------------------------------------------------------- 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')); - + // Index $this->assertFalse($this->config->item('no_good_item', 'bad_index')); $this->assertFalse($this->config->item('no_good_item', 'default')); } - + // -------------------------------------------------------------------- - + public function test_set_item() { $this->assertFalse($this->config->item('not_yet_set')); - + $this->config->set_item('not_yet_set', 'is set'); - $this->assertEquals('is set', $this->config->item('not_yet_set')); } // -------------------------------------------------------------------- - + public function test_slash_item() { // 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('MY_/', $this->config->slash_item('subclass_prefix')); + $this->assertEquals($this->cfg['base_url'], $this->config->slash_item('base_url')); + $this->assertEquals($this->cfg['subclass_prefix'].'/', $this->config->slash_item('subclass_prefix')); + } + + // -------------------------------------------------------------------- + + public function test_base_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; + + // 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() { - $this->assertEquals('http://example.com/index.php', $this->config->site_url()); - - $base_url = $this->config->item('base_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->config->set_item('base_url', $base_url); + $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', $old_base); + + $this->assertEquals($base_url.$index_page.'?'.$uri, $this->config->site_url($uri)); - $this->assertEquals('http://example.com/index.php?test', $this->config->site_url('test')); - // back to home base - $this->config->set_item('enable_query_strings', $q_string); + $this->config->set_item('enable_query_strings', $q_string); } // -------------------------------------------------------------------- - + 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() + { + // 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($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 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)); + } + + // -------------------------------------------------------------------- + + public function test_load_nonexistent() + { + // Test graceful fail of nonexistent file + $this->assertFalse($this->config->load('not_config_file', FALSE, TRUE)); + + // Test regular fail + $file = 'absentia'; + $this->setExpectedException( + 'RuntimeException', + 'CI Error: The configuration file '.$file.'.php does not exist.' + ); + $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 new file mode 100644 index 000000000..ca1c6dfd7 --- /dev/null +++ b/tests/codeigniter/core/Input_test.php @@ -0,0 +1,141 @@ +<?php + +class Input_test extends CI_TestCase { + + public function set_up() + { + // Set server variable to GET as default, since this will leave unset in STDIN env + $_SERVER['REQUEST_METHOD'] = 'GET'; + + // Set config for Input class + $this->ci_set_config('allow_get_array', TRUE); + $this->ci_set_config('global_xss_filtering', FALSE); + $this->ci_set_config('csrf_protection', FALSE); + + $security = new Mock_Core_Security(); + $utf8 = new Mock_Core_Utf8(); + + $this->input = new Mock_Core_Input($security, $utf8); + } + + // -------------------------------------------------------------------- + + public function test_get_not_exists() + { + $this->assertTrue($this->input->get() === array()); + $this->assertTrue($this->input->get('foo') === NULL); + } + + // -------------------------------------------------------------------- + + public function test_get_exist() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_GET['foo'] = 'bar'; + + $this->assertArrayHasKey('foo', $this->input->get()); + $this->assertEquals('bar', $this->input->get('foo')); + } + + // -------------------------------------------------------------------- + + public function test_get_exist_with_xss_clean() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_GET['harm'] = "Hello, i try to <script>alert('Hack');</script> your site"; + + $this->assertArrayHasKey('harm', $this->input->get()); + $this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $this->input->get('harm')); + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $this->input->get('harm', TRUE)); + } + + // -------------------------------------------------------------------- + + public function test_post_not_exists() + { + $this->assertTrue($this->input->post() === array()); + $this->assertTrue($this->input->post('foo') === NULL); + } + + // -------------------------------------------------------------------- + + public function test_post_exist() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['foo'] = 'bar'; + + $this->assertArrayHasKey('foo', $this->input->post()); + $this->assertEquals('bar', $this->input->post('foo')); + } + + // -------------------------------------------------------------------- + + public function test_post_exist_with_xss_clean() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['harm'] = "Hello, i try to <script>alert('Hack');</script> your site"; + + $this->assertArrayHasKey('harm', $this->input->post()); + $this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $this->input->post('harm')); + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $this->input->post('harm', TRUE)); + } + + // -------------------------------------------------------------------- + + public function test_get_post() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST['foo'] = 'bar'; + + $this->assertEquals('bar', $this->input->get_post('foo')); + } + + // -------------------------------------------------------------------- + + public function test_cookie() + { + $_COOKIE['foo'] = 'bar'; + + $this->assertEquals('bar', $this->input->cookie('foo')); + } + + // -------------------------------------------------------------------- + + public function test_server() + { + $this->assertEquals('GET', $this->input->server('REQUEST_METHOD')); + } + + // -------------------------------------------------------------------- + + public function test_fetch_from_array() + { + $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); + + $this->assertEquals('bar', $foo); + $this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $harm); + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $harmless); + } + + // -------------------------------------------------------------------- + + public function test_valid_ip() + { + $ip_v4 = '192.18.0.1'; + $this->assertTrue($this->input->valid_ip($ip_v4)); + + $ip_v6 = array('2001:0db8:0000:85a3:0000:0000:ac1f:8001', '2001:db8:0:85a3:0:0:ac1f:8001', '2001:db8:0:85a3::ac1f:8001'); + foreach ($ip_v6 as $ip) + { + $this->assertTrue($this->input->valid_ip($ip)); + } + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Lang_test.php b/tests/codeigniter/core/Lang_test.php index a414f0ace..3364362e0 100644 --- a/tests/codeigniter/core/Lang_test.php +++ b/tests/codeigniter/core/Lang_test.php @@ -1,9 +1,9 @@ <?php class Lang_test extends CI_TestCase { - + protected $lang; - + public function set_up() { $loader_cls = $this->ci_core_class('load'); @@ -12,20 +12,23 @@ class Lang_test extends CI_TestCase { $cls = $this->ci_core_class('lang'); $this->lang = new $cls; } - + // -------------------------------------------------------------------- - + public function test_load() { + $this->ci_vfs_clone('system/language/english/profiler_lang.php'); $this->assertTrue($this->lang->load('profiler', 'english')); + $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string')); } - + // -------------------------------------------------------------------- - public function test_line() + public function test_load_with_unspecified_language() { - $this->assertTrue($this->lang->load('profiler', 'english')); + $this->ci_vfs_clone('system/language/english/profiler_lang.php'); + $this->assertTrue($this->lang->load('profiler')); $this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string')); } - + }
\ No newline at end of file diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index 43008651e..ecc5ca933 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -1,146 +1,286 @@ <?php class Loader_test extends CI_TestCase { - + private $ci_obj; - + public function set_up() { // Instantiate a new loader - $this->load = new Mock_Core_Loader(); - - // mock up a ci instance - $this->ci_obj = new StdClass; - - // Fix get_instance() - $this->ci_instance($this->ci_obj); + $loader = $this->ci_core_class('loader'); + $this->load = new $loader(); + + // Get CI instance + $this->ci_obj = $this->ci_instance(); + + // Set subclass prefix + $this->prefix = 'MY_'; + $this->ci_set_config('subclass_prefix', $this->prefix); } // -------------------------------------------------------------------- - + public function test_library() { - $this->_setup_config_mock(); - + // Create library in VFS + $lib = 'unit_test_lib'; + $class = 'CI_'.ucfirst($lib); + $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_base_root, 'libraries'); + + // Test is_loaded fail + $this->assertFalse($this->load->is_loaded($lib)); + // Test loading as an array. - $this->assertNull($this->load->library(array('table'))); - $this->assertTrue(class_exists('CI_Table'), 'Table class exists'); - $this->assertAttributeInstanceOf('CI_Table', 'table', $this->ci_obj); - + $this->assertNull($this->load->library(array($lib))); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $lib, $this->ci_obj); + // Test no lib given - $this->assertEquals(FALSE, $this->load->library()); - + $this->assertNull($this->load->library()); + // Test a string given to params - $this->assertEquals(NULL, $this->load->library('table', ' ')); + $this->assertNull($this->load->library($lib, ' ')); + + // Create library w/o class + $lib = 'bad_test_lib'; + $this->ci_vfs_create($lib, '', $this->ci_base_root, 'libraries'); + + // Test non-existent class + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Non-existent class: '.$lib + ); + $this->assertNull($this->load->library($lib)); } // -------------------------------------------------------------------- - public function test_load_library_in_application_dir() + public function test_library_extension() { - $this->_setup_config_mock(); - - $content = '<?php class Super_test_library {} '; - - $model = vfsStream::newFile('Super_test_library.php')->withContent($content) - ->at($this->load->libs_dir); - - $this->assertNull($this->load->library('super_test_library')); - - // Was the model class instantiated. - $this->assertTrue(class_exists('Super_test_library')); + // Create library and extension in VFS + $name = 'ext_test_lib'; + $lib = ucfirst($name); + $class = 'CI_'.$lib; + $ext = $this->prefix.$lib; + $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_base_root, 'libraries'); + $this->ci_vfs_create($ext, '<?php class '.$ext.' extends '.$class.' { }', $this->ci_app_root, 'libraries'); + + // Test loading with extension + $this->assertNull($this->load->library($lib)); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertTrue(class_exists($ext), $ext.' does not exist'); + $this->assertAttributeInstanceOf($class, $name, $this->ci_obj); + $this->assertAttributeInstanceOf($ext, $name, $this->ci_obj); + + // Test reloading with object name + $obj = 'exttest'; + $this->assertNull($this->load->library($lib, NULL, $obj)); + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); + $this->assertAttributeInstanceOf($ext, $obj, $this->ci_obj); + + // Test reloading + unset($this->ci_obj->$name); + $this->assertNull($this->load->library($lib)); + $this->assertObjectNotHasAttribute($name, $this->ci_obj); + + // Create baseless library + $name = 'ext_baseless_lib'; + $lib = ucfirst($name); + $class = $this->prefix.$lib; + $this->ci_vfs_create($class, '<?php class '.$class.' { }', $this->ci_app_root, 'libraries'); + + // Test missing base class + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested class: '.$lib + ); + $this->assertNull($this->load->library($lib)); } - + // -------------------------------------------------------------------- - - private function _setup_config_mock() + + public function test_library_config() { - // Mock up a config object until we - // figure out how to test the library configs - $config = $this->getMock('CI_Config', NULL, array(), '', FALSE); - $config->expects($this->any()) - ->method('load') - ->will($this->returnValue(TRUE)); - - // Add the mock to our stdClass - $this->ci_instance_var('config', $config); + // Create library in VFS + $lib = 'unit_test_config_lib'; + $class = 'CI_'.ucfirst($lib); + $content = '<?php class '.$class.' { public function __construct($params) { $this->config = $params; } }'; + $this->ci_vfs_create($lib, $content, $this->ci_base_root, 'libraries'); + + // Create config file + $cfg = array( + 'foo' => 'bar', + 'bar' => 'baz', + 'baz' => false + ); + $this->ci_vfs_create($lib, '<?php $config = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config'); + + // Test object name and config + $obj = 'testy'; + $this->assertNull($this->load->library($lib, NULL, $obj)); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); + $this->assertEquals($cfg, $this->ci_obj->$obj->config); + + // Test is_loaded + $this->assertEquals($obj, $this->load->is_loaded($lib)); } // -------------------------------------------------------------------- - public function test_non_existent_model() + public function test_load_library_in_application_dir() { - $this->setExpectedException( - 'RuntimeException', - 'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php' - ); - - $this->load->model('ci_test_nonexistent_model.php'); + // Create library in VFS + $lib = 'super_test_library'; + $class = ucfirst($lib); + $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_app_root, 'libraries'); + + // Load library + $this->assertNull($this->load->library($lib)); + + // Was the model class instantiated. + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $lib, $this->ci_obj); + } + + // -------------------------------------------------------------------- + + public function test_driver() + { + // Create driver in VFS + $driver = 'unit_test_driver'; + $dir = ucfirst($driver); + $class = 'CI_'.$dir; + $content = '<?php class '.$class.' { } '; + $this->ci_vfs_create($driver, $content, $this->ci_base_root, 'libraries/'.$dir); + + // Test loading as an array. + $this->assertNull($this->load->driver(array($driver))); + $this->assertTrue(class_exists($class), $class.' does not exist'); + $this->assertAttributeInstanceOf($class, $driver, $this->ci_obj); + + // Test loading as a library with a name + $obj = 'testdrive'; + $this->assertNull($this->load->library($driver, NULL, $obj)); + $this->assertAttributeInstanceOf($class, $obj, $this->ci_obj); + + // Test no driver given + $this->assertFalse($this->load->driver()); + + // Test a string given to params + $this->assertNull($this->load->driver($driver, ' ')); } // -------------------------------------------------------------------- - - /** - * @coverts CI_Loader::model - */ + public function test_models() { $this->ci_set_core_class('model', 'CI_Model'); - - $content = '<?php class Unit_test_model extends CI_Model {} '; - - $model = vfsStream::newFile('unit_test_model.php')->withContent($content) - ->at($this->load->models_dir); - - $this->assertNull($this->load->model('unit_test_model')); - + + // Create model in VFS + $model = 'unit_test_model'; + $class = ucfirst($model); + $content = '<?php class '.$class.' extends CI_Model {} '; + $this->ci_vfs_create($model, $content, $this->ci_app_root, 'models'); + + // Load model + $this->assertNull($this->load->model($model)); + // Was the model class instantiated. - $this->assertTrue(class_exists('Unit_test_model')); - + $this->assertTrue(class_exists($class)); + // Test no model given - $this->assertNull($this->load->model('')); + $this->assertNull($this->load->model('')); + } + + // -------------------------------------------------------------------- + + public function test_model_subdir() + { + // Make sure base class is loaded - we'll test _ci_include later + $this->ci_core_class('model'); + + // Create modelin VFS + $model = 'test_sub_model'; + $base = 'CI_Model'; + $class = ucfirst($model); + $subdir = 'cars'; + $this->ci_vfs_create($model, '<?php class '.$class.' extends '.$base.' { }', $this->ci_app_root, + array('models', $subdir)); + + // Load model + $name = 'testors'; + $this->assertNull($this->load->model($subdir.'/'.$model, $name)); + + // Was the model class instantiated? + $this->assertTrue(class_exists($class)); + $this->assertObjectHasAttribute($name, $this->ci_obj); + $this->assertAttributeInstanceOf($base, $name, $this->ci_obj); + $this->assertAttributeInstanceOf($class, $name, $this->ci_obj); + + // Test name conflict + $obj = 'conflict'; + $this->ci_obj->$obj = new StdClass(); + $this->setExpectedException( + 'RuntimeException', + 'CI Error: The model name you are loading is the name of a resource that is already being used: '.$obj + ); + $this->load->model('not_real', $obj); + } + + // -------------------------------------------------------------------- + + public function test_non_existent_model() + { + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to locate the model you have specified: ci_test_nonexistent_model.php' + ); + + $this->load->model('ci_test_nonexistent_model.php'); } // -------------------------------------------------------------------- - + // public function testDatabase() // { - // $this->assertEquals(NULL, $this->load->database()); - // $this->assertEquals(NULL, $this->load->dbutil()); + // $this->assertNull($this->load->database()); + // $this->assertNull($this->load->dbutil()); // } // -------------------------------------------------------------------- - - /** - * @coverts CI_Loader::view - */ + public function test_load_view() { - $this->ci_set_core_class('output', 'CI_Output'); - - $content = 'This is my test page. <?php echo $hello; ?>'; - $view = vfsStream::newFile('unit_test_view.php')->withContent($content) - ->at($this->load->views_dir); - - // Use the optional return parameter in this test, so the view is not - // run through the output class. - $this->assertEquals('This is my test page. World!', - $this->load->view('unit_test_view', array('hello' => "World!"), TRUE)); - + // Create view in VFS + $view = 'unit_test_view'; + $var = 'hello'; + $value = 'World!'; + $content = 'This is my test page. '; + $this->ci_vfs_create($view, $content.'<?php echo $'.$var.';', $this->ci_app_root, 'views'); + + // Test returning view + $out = $this->load->view($view, array($var => $value), TRUE); + $this->assertEquals($content.$value, $out); + + // Mock output class + $output = $this->getMock('CI_Output', array('append_output')); + $output->expects($this->once())->method('append_output')->with($content.$value); + $this->ci_instance_var('output', $output); + + // Test view output + $this->assertNull($this->load->view($view, array($var => $value))); } // -------------------------------------------------------------------- - - /** - * @coverts CI_Loader::view - */ + public function test_non_existent_view() { $this->setExpectedException( 'RuntimeException', 'CI Error: Unable to load the requested file: ci_test_nonexistent_view.php' - ); - + ); + $this->load->view('ci_test_nonexistent_view', array('foo' => 'bar')); } @@ -148,88 +288,233 @@ class Loader_test extends CI_TestCase { public function test_file() { + // Create view in VFS + $dir = 'views'; + $file = 'ci_test_mock_file'; $content = 'Here is a test file, which we will load now.'; - $file = vfsStream::newFile('ci_test_mock_file.php')->withContent($content) - ->at($this->load->views_dir); - + $this->ci_vfs_create($file, $content, $this->ci_app_root, $dir); + // Just like load->view(), take the output class out of the mix here. - $load = $this->load->file(vfsStream::url('application').'/views/ci_test_mock_file.php', - TRUE); - - $this->assertEquals($content, $load); - + $out = $this->load->file(APPPATH.$dir.'/'.$file.'.php', TRUE); + $this->assertEquals($content, $out); + + // Test non-existent file $this->setExpectedException( 'RuntimeException', 'CI Error: Unable to load the requested file: ci_test_file_not_exists' - ); - + ); + $this->load->file('ci_test_file_not_exists', TRUE); - } // -------------------------------------------------------------------- - + public function test_vars() { - $vars = array( - 'foo' => 'bar' - ); - - $this->assertNull($this->load->vars($vars)); - $this->assertNull($this->load->vars('foo', 'bar')); + $key1 = 'foo'; + $val1 = 'bar'; + $key2 = 'boo'; + $val2 = 'hoo'; + $this->assertNull($this->load->vars(array($key1 => $val1))); + $this->assertNull($this->load->vars($key2, $val2)); + $this->assertEquals($val1, $this->load->get_var($key1)); + $this->assertEquals(array($key1 => $val1, $key2 => $val2), $this->load->get_vars()); } // -------------------------------------------------------------------- - + public function test_helper() { - $this->assertEquals(NULL, $this->load->helper('array')); - + // Create helper in VFS + $helper = 'test'; + $func = '_my_helper_test_func'; + $content = '<?php function '.$func.'() { return true; } '; + $this->ci_vfs_create($helper.'_helper', $content, $this->ci_base_root, 'helpers'); + + // Create helper extension + $exfunc = '_my_extension_func'; + $content = '<?php function '.$exfunc.'() { return true; } '; + $this->ci_vfs_create($this->prefix.$helper.'_helper', $content, $this->ci_app_root, 'helpers'); + + // Load helper + $this->assertNull($this->load->helper($helper)); + $this->assertTrue(function_exists($func), $func.' does not exist'); + $this->assertTrue(function_exists($exfunc), $exfunc.' does not exist'); + + // Create baseless extension + $ext = 'bad_ext'; + $this->ci_vfs_create($this->prefix.$ext.'_helper', '', $this->ci_app_root, 'helpers'); + + // Test bad extension + $this->setExpectedException( + 'RuntimeException', + 'CI Error: Unable to load the requested file: helpers/'.$ext.'_helper.php' + ); + $this->load->helper($ext); + } + + // -------------------------------------------------------------------- + + public function test_non_existent_helper() + { $this->setExpectedException( 'RuntimeException', 'CI Error: Unable to load the requested file: helpers/bad_helper.php' - ); - + ); $this->load->helper('bad'); } - + // -------------------------------------------------------------------- public function test_loading_multiple_helpers() { - $this->assertEquals(NULL, $this->load->helpers(array('file', 'array', 'string'))); + // Create helpers in VFS + $helpers = array(); + $funcs = array(); + $files = array(); + for ($i = 1; $i <= 3; ++$i) { + $helper = 'test'.$i; + $helpers[] = $helper; + $func = '_my_helper_test_func'.$i; + $funcs[] = $func; + $files[$helper.'_helper'] = '<?php function '.$func.'() { return true; } '; + } + $this->ci_vfs_create($files, NULL, $this->ci_base_root, 'helpers'); + + // Load helpers + $this->assertNull($this->load->helpers($helpers)); + + // Verify helper existence + foreach ($funcs as $func) { + $this->assertTrue(function_exists($func), $func.' does not exist'); + } } - - // -------------------------------------------------------------------- - - // public function testLanguage() - // { - // $this->assertEquals(NULL, $this->load->language('test')); - // } // -------------------------------------------------------------------- - public function test_load_config() + public function test_language() { - $this->_setup_config_mock(); - - $this->assertNull($this->load->config('config', FALSE)); + // Mock lang class and test load call + $file = 'test'; + $lang = $this->getMock('CI_Lang', array('load')); + $lang->expects($this->once())->method('load')->with($file); + $this->ci_instance_var('lang', $lang); + $this->assertNull($this->load->language($file)); } - + // -------------------------------------------------------------------- - public function test_load_bad_config() + public function test_packages() { - $this->_setup_config_mock(); - + // Create model in VFS package path + $dir = 'third-party'; + $lib = 'unit_test_package'; + $class = 'CI_'.ucfirst($lib); + $this->ci_vfs_create($lib, '<?php class '.$class.' { }', $this->ci_app_root, array($dir, 'libraries')); + + // Get paths + $paths = $this->load->get_package_paths(TRUE); + + // Add path and verify + $path = APPPATH.$dir.'/'; + $this->assertNull($this->load->add_package_path($path)); + $this->assertContains($path, $this->load->get_package_paths(TRUE)); + + // Test successful load + $this->assertNull($this->load->library($lib)); + $this->assertTrue(class_exists($class), $class.' does not exist'); + + // Add another path + $path2 = APPPATH.'another/'; + $this->assertNull($this->load->add_package_path($path2)); + $this->assertContains($path2, $this->load->get_package_paths(TRUE)); + + // Remove last path + $this->assertNull($this->load->remove_package_path()); + $this->assertNotContains($path2, $this->load->get_package_paths(TRUE)); + + // Remove path and verify restored paths + $this->assertNull($this->load->remove_package_path($path)); + $this->assertEquals($paths, $this->load->get_package_paths(TRUE)); + + // Test failed load without path $this->setExpectedException( 'RuntimeException', - 'CI Error: The configuration file foobar.php does not exist.' - ); - - $this->load->config('foobar', FALSE); + 'CI Error: Unable to load the requested class: '.$lib + ); + $this->load->library($lib); + } + + // -------------------------------------------------------------------- + + public function test_load_config() + { + $cfg = 'someconfig'; + $this->assertTrue($this->load->config($cfg, FALSE)); + $this->assertContains($cfg, $this->ci_obj->config->loaded); } // -------------------------------------------------------------------- - -} + + public function test_initialize() + { + // Create helper in VFS + $helper = 'autohelp'; + $hlp_func = '_autohelp_test_func'; + $content = '<?php function '.$hlp_func.'() { return true; }'; + $this->ci_vfs_create($helper.'_helper', $content, $this->ci_app_root, 'helpers'); + + // Create library in VFS + $lib = 'autolib'; + $lib_class = 'CI_'.ucfirst($lib); + $this->ci_vfs_create($lib, '<?php class '.$lib_class.' { }', $this->ci_base_root, 'libraries'); + + // Create driver in VFS + $drv = 'autodrv'; + $subdir = ucfirst($drv); + $drv_class = 'CI_'.$subdir; + $this->ci_vfs_create($drv, '<?php class '.$drv_class.' { }', $this->ci_base_root, array('libraries', $subdir)); + + // Create model in VFS package path + $dir = 'testdir'; + $path = APPPATH.$dir.'/'; + $model = 'automod'; + $mod_class = ucfirst($model); + $this->ci_vfs_create($model, '<?php class '.$mod_class.' { }', $this->ci_app_root, array($dir, 'models')); + + // Create autoloader config + $cfg = array( + 'packages' => array($path), + 'helper' => array($helper), + 'libraries' => array($lib), + 'drivers' => array($drv), + 'model' => array($model), + 'config' => array('config1', 'config2') + ); + $this->ci_vfs_create('autoload', '<?php $autoload = '.var_export($cfg, TRUE).';', $this->ci_app_root, 'config'); + + $this->load->initialize(); + + // Verify path + $this->assertContains($path, $this->load->get_package_paths()); + + // Verify helper + $this->assertTrue(function_exists($hlp_func), $hlp_func.' does not exist'); + + // Verify library + $this->assertTrue(class_exists($lib_class), $lib_class.' does not exist'); + $this->assertAttributeInstanceOf($lib_class, $lib, $this->ci_obj); + + // Verify driver + $this->assertTrue(class_exists($drv_class), $drv_class.' does not exist'); + $this->assertAttributeInstanceOf($drv_class, $drv, $this->ci_obj); + + // Verify model + $this->assertTrue(class_exists($mod_class), $mod_class.' does not exist'); + $this->assertAttributeInstanceOf($mod_class, $model, $this->ci_obj); + + // Verify config calls + $this->assertEquals($cfg['config'], $this->ci_obj->config->loaded); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Output_test.php b/tests/codeigniter/core/Output_test.php new file mode 100644 index 000000000..d8252403d --- /dev/null +++ b/tests/codeigniter/core/Output_test.php @@ -0,0 +1,37 @@ +<?php + +class Output_test extends CI_TestCase { + + public $output; + + public function set_up() + { + $this->ci_set_config('charset', 'UTF-8'); + $output = $this->ci_core_class('output'); + $this->output = new $output(); + } + + // -------------------------------------------------------------------- + + public function test_get_content_type() + { + $this->assertEquals('text/html', $this->output->get_content_type()); + } + + // -------------------------------------------------------------------- + + public function test_get_header() + { + $this->assertNull($this->output->get_header('Non-Existent-Header')); + + // TODO: Find a way to test header() values as well. Currently, + // PHPUnit prevents this by not using output buffering. + + $this->output->set_content_type('text/plain', 'WINDOWS-1251'); + $this->assertEquals( + 'text/plain; charset=windows-1251', // Character set is converted to lowercase + $this->output->get_header('content-type') // Case-insensitive comparison + ); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/Security_test.php b/tests/codeigniter/core/Security_test.php new file mode 100644 index 000000000..3f6e3b07a --- /dev/null +++ b/tests/codeigniter/core/Security_test.php @@ -0,0 +1,106 @@ +<?php + +class Security_test extends CI_TestCase { + + public function set_up() + { + // Set cookie for security test + $_COOKIE['ci_csrf_cookie'] = md5(uniqid(rand(), TRUE)); + + // Set config for Security class + $this->ci_set_config('csrf_protection', TRUE); + $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(); + } + + // -------------------------------------------------------------------- + + public function test_csrf_verify() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + + $this->assertInstanceOf('CI_Security', $this->security->csrf_verify()); + } + + // -------------------------------------------------------------------- + + public function test_csrf_verify_invalid() + { + // Without issuing $_POST[csrf_token_name], this request will triggering CSRF error + $_SERVER['REQUEST_METHOD'] = 'POST'; + + $this->setExpectedException('RuntimeException', 'CI Error: The action you have requested is not allowed'); + + $this->security->csrf_verify(); + } + + // -------------------------------------------------------------------- + + public function test_csrf_verify_valid() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST[$this->security->csrf_token_name] = $this->security->csrf_hash; + + $this->assertInstanceOf('CI_Security', $this->security->csrf_verify()); + } + + // -------------------------------------------------------------------- + + public function test_get_csrf_hash() + { + $this->assertEquals($this->security->csrf_hash, $this->security->get_csrf_hash()); + } + + // -------------------------------------------------------------------- + + public function test_get_csrf_token_name() + { + $this->assertEquals('ci_csrf_token', $this->security->get_csrf_token_name()); + } + + // -------------------------------------------------------------------- + + public function test_xss_clean() + { + $harm_string = "Hello, i try to <script>alert('Hack');</script> your site"; + + $harmless_string = $this->security->xss_clean($harm_string); + + $this->assertEquals("Hello, i try to [removed]alert('Hack');[removed] your site", $harmless_string); + } + + // -------------------------------------------------------------------- + + public function test_xss_hash() + { + $this->assertEmpty($this->security->xss_hash); + + // Perform hash + $this->security->xss_hash(); + + $this->assertTrue(preg_match('#^[0-9a-f]{32}$#iS', $this->security->xss_hash) === 1); + } + + // -------------------------------------------------------------------- + + public function test_entity_decode() + { + $encoded = '<div>Hello <b>Booya</b></div>'; + $decoded = $this->security->entity_decode($encoded); + + $this->assertEquals('<div>Hello <b>Booya</b></div>', $decoded); + } + + // -------------------------------------------------------------------- + + public function test_sanitize_filename() + { + $filename = './<!--foo-->'; + $safe_filename = $this->security->sanitize_filename($filename); + + $this->assertEquals('foo', $safe_filename); + } + +}
\ No newline at end of file diff --git a/tests/codeigniter/core/URI_test.php b/tests/codeigniter/core/URI_test.php index e340ddf73..e2deabe51 100644 --- a/tests/codeigniter/core/URI_test.php +++ b/tests/codeigniter/core/URI_test.php @@ -1,7 +1,7 @@ <?php class URI_test extends CI_TestCase { - + public function set_up() { $this->uri = new Mock_Core_URI(); @@ -9,23 +9,21 @@ class URI_test extends CI_TestCase { // -------------------------------------------------------------------- + /* As of the following commit, _set_uri_string() is a protected method: + + https://github.com/EllisLab/CodeIgniter/commit/d461934184d95b0cfb2feec93f27b621ef72a5c2 + public function test_set_uri_string() { // Slashes get killed $this->uri->_set_uri_string('/'); - - $a = ''; - $b =& $this->uri->uri_string; - - $this->assertEquals($a, $b); - + $this->assertEquals('', $this->uri->uri_string); + $this->uri->_set_uri_string('nice/uri'); - - $a = 'nice/uri'; - - $this->assertEquals($a, $b); + $this->assertEquals('nice/uri', $this->uri->uri_string); } - + */ + // -------------------------------------------------------------------- public function test_fetch_uri_string() @@ -34,75 +32,61 @@ class URI_test extends CI_TestCase { // uri_protocol: AUTO $this->uri->config->set_item('uri_protocol', 'AUTO'); - + // Test a variety of request uris $requests = array( '/index.php/controller/method' => 'controller/method', '/index.php?/controller/method' => 'controller/method', '/index.php?/controller/method/?var=foo' => 'controller/method' ); - - foreach($requests as $request => $expected) + + foreach ($requests as $request => $expected) { $_SERVER['SCRIPT_NAME'] = '/index.php'; $_SERVER['REQUEST_URI'] = $request; - + $this->uri->_fetch_uri_string(); - $this->assertEquals($expected, $this->uri->uri_string ); + $this->assertEquals($expected, $this->uri->uri_string); } - + // Test a subfolder $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; $_SERVER['REQUEST_URI'] = '/subfolder/index.php/controller/method'; - + $this->uri->_fetch_uri_string(); - - $a = 'controller/method'; - $b = $this->uri->uri_string; - - $this->assertEquals($a, $b); - + $this->assertEquals('controller/method', $this->uri->uri_string); + // death to request uri unset($_SERVER['REQUEST_URI']); - + // life to path info $_SERVER['PATH_INFO'] = '/controller/method/'; - + $this->uri->_fetch_uri_string(); - - $a = '/controller/method/'; - $b =& $this->uri->uri_string; + $this->assertEquals('controller/method', $this->uri->uri_string); - $this->assertEquals($a, $b); - // death to path info // At this point your server must be seriously drunk unset($_SERVER['PATH_INFO']); - + $_SERVER['QUERY_STRING'] = '/controller/method/'; - + $this->uri->_fetch_uri_string(); + $this->assertEquals('controller/method', $this->uri->uri_string); - $a = '/controller/method/'; - $b = $this->uri->uri_string; - - $this->assertEquals($a, $b); - // At this point your server is a labotomy victim - unset($_SERVER['QUERY_STRING']); - + $_GET['/controller/method/'] = ''; - + $this->uri->_fetch_uri_string(); - $this->assertEquals($a, $b); + $this->assertEquals('controller/method', $this->uri->uri_string); // Test coverage implies that these will work // uri_protocol: REQUEST_URI // uri_protocol: CLI - } - + // -------------------------------------------------------------------- public function test_explode_segments() @@ -113,18 +97,15 @@ class URI_test extends CI_TestCase { '/test2/uri2' => array('test2', 'uri2'), '//test3/test3///' => array('test3', 'test3') ); - - foreach($uris as $uri => $a) + + foreach ($uris as $uri => $a) { $this->uri->segments = array(); $this->uri->uri_string = $uri; $this->uri->_explode_segments(); - - $b = $this->uri->segments; - - $this->assertEquals($a, $b); + + $this->assertEquals($a, $this->uri->segments); } - } // -------------------------------------------------------------------- @@ -133,7 +114,7 @@ class URI_test extends CI_TestCase { { $this->uri->config->set_item('enable_query_strings', FALSE); $this->uri->config->set_item('permitted_uri_chars', 'a-z 0-9~%.:_\-'); - + $str_in = 'abc01239~%.:_-'; $str = $this->uri->_filter_uri($str_in); @@ -145,52 +126,52 @@ class URI_test extends CI_TestCase { public function test_filter_uri_escaping() { // ensure escaping even if dodgey characters are permitted - + $this->uri->config->set_item('enable_query_strings', FALSE); $this->uri->config->set_item('permitted_uri_chars', 'a-z 0-9~%.:_\-()$'); $str = $this->uri->_filter_uri('$destroy_app(foo)'); - + $this->assertEquals($str, '$destroy_app(foo)'); } // -------------------------------------------------------------------- - public function test_filter_uri_throws_error() - { + public function test_filter_uri_throws_error() + { $this->setExpectedException('RuntimeException'); - + $this->uri->config->set_item('enable_query_strings', FALSE); $this->uri->config->set_item('permitted_uri_chars', 'a-z 0-9~%.:_\-'); $this->uri->_filter_uri('$this()'); - } + } // -------------------------------------------------------------------- public function test_remove_url_suffix() { $this->uri->config->set_item('url_suffix', '.html'); - + $this->uri->uri_string = 'controller/method/index.html'; $this->uri->_remove_url_suffix(); - + $this->assertEquals($this->uri->uri_string, 'controller/method/index'); - + $this->uri->uri_string = 'controller/method/index.htmlify.html'; $this->uri->_remove_url_suffix(); - + $this->assertEquals($this->uri->uri_string, 'controller/method/index.htmlify'); } // -------------------------------------------------------------------- - + public function test_segment() { $this->uri->segments = array(1 => 'controller'); $this->assertEquals($this->uri->segment(1), 'controller'); $this->assertEquals($this->uri->segment(2, 'default'), 'default'); } - + // -------------------------------------------------------------------- public function test_rsegment() @@ -205,32 +186,33 @@ class URI_test extends CI_TestCase { public function test_uri_to_assoc() { $this->uri->segments = array('a', '1', 'b', '2', 'c', '3'); - - $a = array('a' => '1', 'b' => '2', 'c' => '3'); - $b = $this->uri->uri_to_assoc(1); - $this->assertEquals($a, $b); - - $a = array('b' => '2', 'c' => '3'); - $b = $this->uri->uri_to_assoc(3); - $this->assertEquals($a, $b); - - + + $this->assertEquals( + array('a' => '1', 'b' => '2', 'c' => '3'), + $this->uri->uri_to_assoc(1) + ); + + $this->assertEquals( + array('b' => '2', 'c' => '3'), + $this->uri->uri_to_assoc(3) + ); + $this->uri->keyval = array(); // reset cache - $this->uri->segments = array('a', '1', 'b', '2', 'c'); - - $a = array('a' => '1', 'b' => '2', 'c' => FALSE); - $b = $this->uri->uri_to_assoc(1); - $this->assertEquals($a, $b); - + + $this->assertEquals( + array('a' => '1', 'b' => '2', 'c' => FALSE), + $this->uri->uri_to_assoc(1) + ); + $this->uri->keyval = array(); // reset cache - $this->uri->segments = array('a', '1'); - + // test default - $a = array('a' => '1', 'b' => FALSE); - $b = $this->uri->uri_to_assoc(1, array('a', 'b')); - $this->assertEquals($a, $b); + $this->assertEquals( + array('a' => '1', 'b' => FALSE), + $this->uri->uri_to_assoc(1, array('a', 'b')) + ); } // -------------------------------------------------------------------- @@ -238,33 +220,33 @@ class URI_test extends CI_TestCase { public function test_ruri_to_assoc() { $this->uri->rsegments = array('x', '1', 'y', '2', 'z', '3'); - - $a = array('x' => '1', 'y' => '2', 'z' => '3'); - $b = $this->uri->ruri_to_assoc(1); - $this->assertEquals($a, $b); - - $a = array('y' => '2', 'z' => '3'); - $b = $this->uri->ruri_to_assoc(3); - $this->assertEquals($a, $b); - - + + $this->assertEquals( + array('x' => '1', 'y' => '2', 'z' => '3'), + $this->uri->ruri_to_assoc(1) + ); + + $this->assertEquals( + array('y' => '2', 'z' => '3'), + $this->uri->ruri_to_assoc(3) + ); + $this->uri->keyval = array(); // reset cache - $this->uri->rsegments = array('x', '1', 'y', '2', 'z'); - - $a = array('x' => '1', 'y' => '2', 'z' => FALSE); - $b = $this->uri->ruri_to_assoc(1); - $this->assertEquals($a, $b); - + + $this->assertEquals( + array('x' => '1', 'y' => '2', 'z' => FALSE), + $this->uri->ruri_to_assoc(1) + ); + $this->uri->keyval = array(); // reset cache - $this->uri->rsegments = array('x', '1'); - - // test default - $a = array('x' => '1', 'y' => FALSE); - $b = $this->uri->ruri_to_assoc(1, array('x', 'y')); - $this->assertEquals($a, $b); + // test default + $this->assertEquals( + array('x' => '1', 'y' => FALSE), + $this->uri->ruri_to_assoc(1, array('x', 'y')) + ); } // -------------------------------------------------------------------- @@ -272,11 +254,7 @@ class URI_test extends CI_TestCase { public function test_assoc_to_uri() { $this->uri->config->set_item('uri_string_slashes', 'none'); - - $arr = array('a' => 1, 'b' => 2); - $a = 'a/1/b/2'; - $b = $this->uri->assoc_to_uri($arr); - $this->assertEquals($a, $b); + $this->assertEquals('a/1/b/2', $this->uri->assoc_to_uri(array('a' => '1', 'b' => '2'))); } // -------------------------------------------------------------------- @@ -286,28 +264,18 @@ class URI_test extends CI_TestCase { $this->uri->segments[1] = 'segment'; $this->uri->rsegments[1] = 'segment'; - $a = '/segment/'; - $b = $this->uri->slash_segment(1, 'both'); - $this->assertEquals($a, $b); - $b = $this->uri->slash_rsegment(1, 'both'); - $this->assertEquals($a, $b); - + $this->assertEquals('/segment/', $this->uri->slash_segment(1, 'both')); + $this->assertEquals('/segment/', $this->uri->slash_rsegment(1, 'both')); + $a = '/segment'; - $b = $this->uri->slash_segment(1, 'leading'); - $this->assertEquals($a, $b); - $b = $this->uri->slash_rsegment(1, 'leading'); - $this->assertEquals($a, $b); - - $a = 'segment/'; - $b = $this->uri->slash_segment(1, 'trailing'); - $this->assertEquals($a, $b); - $b = $this->uri->slash_rsegment(1, 'trailing'); - $this->assertEquals($a, $b); - } + $this->assertEquals('/segment', $this->uri->slash_segment(1, 'leading')); + $this->assertEquals('/segment', $this->uri->slash_rsegment(1, 'leading')); + $this->assertEquals('segment/', $this->uri->slash_segment(1, 'trailing')); + $this->assertEquals('segment/', $this->uri->slash_rsegment(1, 'trailing')); + } } -// END URI_test Class /* End of file URI_test.php */ -/* Location: ./tests/core/URI_test.php */ +/* Location: ./tests/core/URI_test.php */
\ No newline at end of file |