From 30d5324617ae136c7a91badb6ed8f7de418fd7f5 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 16 Jan 2014 14:41:46 +0200 Subject: URI Routing overhaul - Allow multiple levels of controller directories (supersedes PRs #390, #2439) - Add support for per-directory 'defaul_controller' and '404_override' (resolves issue #2611; supersedes PR #939) - Fixed a bug where default_controller was called instead of triggering 404 if the current route is inside a directory - Removed a few calls from CI_Router to CI_URI that made a necessity for otherwise internal CI_URI methods to be public: - Removed CI_URI::_fetch_uri_string() and moved its logic into CI_URI::__construct() - Removed CI_URI::_remove_url_suffix, CI_URI::_explode_segments() and moved their logic into CI_URI::_set_uri_string() - Removed CI_URI::_reindex_segments() altogether ( doesn't need further manipulation, while is public anyway and can be properly (and more effectively) replaced on the spot) --- tests/codeigniter/core/URI_test.php | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/URI_test.php b/tests/codeigniter/core/URI_test.php index 99d79bbd2..6589c1f5a 100644 --- a/tests/codeigniter/core/URI_test.php +++ b/tests/codeigniter/core/URI_test.php @@ -26,6 +26,10 @@ class URI_test extends CI_TestCase { // -------------------------------------------------------------------- + /* + + This has been moved to the constructor + public function test_fetch_uri_string() { define('SELF', 'index.php'); @@ -86,9 +90,14 @@ class URI_test extends CI_TestCase { // uri_protocol: REQUEST_URI // uri_protocol: CLI } + */ // -------------------------------------------------------------------- + /* + + This has been moved into _set_uri_string() + public function test_explode_segments() { // Let's test the function's ability to clean up this mess @@ -107,7 +116,7 @@ class URI_test extends CI_TestCase { $this->assertEquals($a, $this->uri->segments); } } - + */ // -------------------------------------------------------------------- public function test_filter_uri() @@ -145,23 +154,6 @@ class URI_test extends CI_TestCase { // -------------------------------------------------------------------- - 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'); -- cgit v1.2.3-24-g4f1b From 279459e42b3a7244ab5f6db8950ed47bf80590ad Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 16 Jan 2014 18:09:09 +0200 Subject: Add some unit tests for CI_Input --- tests/codeigniter/core/Input_test.php | 47 ++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Input_test.php b/tests/codeigniter/core/Input_test.php index 0a98e556c..95833fc91 100644 --- a/tests/codeigniter/core/Input_test.php +++ b/tests/codeigniter/core/Input_test.php @@ -138,13 +138,24 @@ class Input_test extends CI_TestCase { public function test_valid_ip() { - $ip_v4 = '192.18.0.1'; - $this->assertTrue($this->input->valid_ip($ip_v4)); + $this->assertTrue($this->input->valid_ip('192.18.0.1')); + $this->assertTrue($this->input->valid_ip('192.18.0.1', 'ipv4')); + $this->assertFalse($this->input->valid_ip('555.0.0.0')); + $this->assertFalse($this->input->valid_ip('2001:db8:0:85a3::ac1f:8001', 'ipv4')); + + // v6 tests + $this->assertFalse($this->input->valid_ip('192.18.0.1', 'ipv6')); + + $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' + ); - $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)); + $this->assertTrue($this->input->valid_ip($ip, 'ipv6')); } } @@ -171,4 +182,34 @@ class Input_test extends CI_TestCase { $this->assertTrue($this->input->is_ajax_request()); } + // -------------------------------------------------------------------- + + public function test_input_stream() + { + $this->markTestSkipped('TODO: Find a way to test input://'); + } + + // -------------------------------------------------------------------- + + public function test_set_cookie() + { + $this->markTestSkipped('TODO: Find a way to test HTTP headers'); + } + + public function test_ip_address() + { + // 127.0.0.1 is set in our Bootstrap file + $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 + $this->assertEquals('0.0.0.0', $this->input->ip_address()); + + // TODO: Add proxy_ips tests + + // Back to reality + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; // back to reality + } + } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 291676be2ca85581b48dfdc40889085f1928fabf Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 16 Jan 2014 18:39:52 +0200 Subject: Add a unit test for CI_Utf8::_is_ascii() --- tests/codeigniter/core/Utf8_test.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Utf8_test.php b/tests/codeigniter/core/Utf8_test.php index caa7b6986..71299134e 100644 --- a/tests/codeigniter/core/Utf8_test.php +++ b/tests/codeigniter/core/Utf8_test.php @@ -11,10 +11,15 @@ class Utf8_test extends CI_TestCase { public function test_convert_to_utf8() { - $this->assertEquals( - $this->utf8->convert_to_utf8('๒ๅ๑๒', 'WINDOWS-1251'), - 'ั‚ะตัั‚' - ); + $this->assertEquals('ั‚ะตัั‚', $this->utf8->convert_to_utf8('๒ๅ๑๒', 'WINDOWS-1251')); + } + + // -------------------------------------------------------------------- + + public function test_is_ascii() + { + $this->assertTrue($this->utf8->is_ascii_test('foo bar')); + $this->assertFalse($this->utf8->is_ascii_test('ั‚ะตัั‚')); } } \ No newline at end of file -- cgit v1.2.3-24-g4f1b From e33e9c1e5f0b600c0b14558fb74cf61258f7d684 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 17 Jan 2014 12:53:47 +0200 Subject: Add CI_Model unit test --- tests/codeigniter/core/Model_test.php | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/codeigniter/core/Model_test.php (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Model_test.php b/tests/codeigniter/core/Model_test.php new file mode 100644 index 000000000..80dc97b3b --- /dev/null +++ b/tests/codeigniter/core/Model_test.php @@ -0,0 +1,37 @@ +ci_core_class('loader'); + $this->load = new $loader(); + $this->ci_obj = $this->ci_instance(); + $this->ci_set_core_class('model', 'CI_Model'); + + $model_code =<<ci_vfs_create('Test_model', $model_code, $this->ci_app_root, 'models'); + $this->load->model('test_model'); + } + + // -------------------------------------------------------------------- + + public function test__get() + { + $this->assertEquals('foo', $this->ci_obj->test_model->property); + + $this->ci_obj->controller_property = 'bar'; + $this->assertEquals('bar', $this->ci_obj->test_model->controller_property); + } + +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 6672b82087e8737b074f1b849aa77c9c761e2079 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 17 Jan 2014 13:19:33 +0200 Subject: Unit tests: Full code coverage of Benchmark class --- tests/codeigniter/core/Benchmark_test.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/core/Benchmark_test.php b/tests/codeigniter/core/Benchmark_test.php index aff736a40..7fd0727e2 100644 --- a/tests/codeigniter/core/Benchmark_test.php +++ b/tests/codeigniter/core/Benchmark_test.php @@ -27,10 +27,34 @@ class Benchmark_test extends CI_TestCase { $this->assertEmpty($this->benchmark->elapsed_time('undefined_point')); $this->benchmark->mark('code_start'); - sleep(1); $this->benchmark->mark('code_end'); + // Override values, because time isn't testable, but make sure the markers were set + if (isset($this->benchmark->marker['code_start']) && is_float($this->benchmark->marker['code_start'])) + { + $this->benchmark->marker['code_start'] = 1389956144.1944; + } + + if (isset($this->benchmark->marker['code_end']) && is_float($this->benchmark->marker['code_end'])) + { + $this->benchmark->marker['code_end'] = 1389956145.1946; + } + $this->assertEquals('1', $this->benchmark->elapsed_time('code_start', 'code_end', 0)); + $this->assertEquals('1.0', $this->benchmark->elapsed_time('code_start', 'code_end', 1)); + $this->assertEquals('1.00', $this->benchmark->elapsed_time('code_start', 'code_end', 2)); + $this->assertEquals('1.000', $this->benchmark->elapsed_time('code_start', 'code_end', 3)); + $this->assertEquals('1.0002', $this->benchmark->elapsed_time('code_start', 'code_end', 4)); + $this->assertEquals('1.0002', $this->benchmark->elapsed_time('code_start', 'code_end')); + + // Test with non-existing 2nd marker, but again - we need to override the value + $this->benchmark->elapsed_time('code_start', 'code_end2'); + if (isset($this->benchmark->marker['code_end2']) && is_float($this->benchmark->marker['code_end2'])) + { + $this->benchmark->marker['code_end2'] = 1389956146.2046; + } + + $this->assertEquals('2.0102', $this->benchmark->elapsed_time('code_start', 'code_end2')); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From a9938a09e8214b778b8ab11f60501661bb2ac623 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 17 Jan 2014 14:55:56 +0200 Subject: Minor changes related to CI_User_agent Fixed a bug where both accept_charset() and accept_lang() improperly parsed headers if they contained spaces between data separators (which is valid). Also made is_referral() testable by replacing its static cache var with a class property and added some more unit tests for the library as a whole. --- tests/codeigniter/libraries/Parser_test.php | 14 ++----- tests/codeigniter/libraries/Useragent_test.php | 57 ++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 19 deletions(-) (limited to 'tests/codeigniter') diff --git a/tests/codeigniter/libraries/Parser_test.php b/tests/codeigniter/libraries/Parser_test.php index 6e5c192dd..3755cf1a0 100644 --- a/tests/codeigniter/libraries/Parser_test.php +++ b/tests/codeigniter/libraries/Parser_test.php @@ -33,7 +33,7 @@ class Parser_test extends CI_TestCase { // -------------------------------------------------------------------- - public function test_parse_simple_string() + public function test_parse_string() { $data = array( 'title' => 'Page Title', @@ -69,11 +69,7 @@ class Parser_test extends CI_TestCase { { $data = array( 'title' => 'Super Heroes', - 'powers' => array( - array( - 'invisibility' => 'yes', - 'flying' => 'no'), - ) + 'powers' => array(array('invisibility' => 'yes', 'flying' => 'no')) ); $template = "{title}\n{powers}{invisibility}\n{flying}{/powers}\nsecond:{powers} {invisibility} {flying}{/powers}"; @@ -87,11 +83,7 @@ class Parser_test extends CI_TestCase { { $data = array( 'title' => 'Super Heroes', - 'powers' => array( - array( - 'invisibility' => 'yes', - 'flying' => 'no'), - ) + 'powers' => array(array('invisibility' => 'yes', 'flying' => 'no')) ); $template = "{title}\n{powers}{invisibility}\n{flying}"; diff --git a/tests/codeigniter/libraries/Useragent_test.php b/tests/codeigniter/libraries/Useragent_test.php index e3726554e..aed38b8c2 100644 --- a/tests/codeigniter/libraries/Useragent_test.php +++ b/tests/codeigniter/libraries/Useragent_test.php @@ -11,9 +11,7 @@ class UserAgent_test extends CI_TestCase { $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; $this->ci_vfs_clone('application/config/user_agents.php'); - $this->agent = new Mock_Libraries_UserAgent(); - $this->ci_instance_var('agent', $this->agent); } @@ -40,12 +38,27 @@ class UserAgent_test extends CI_TestCase { // -------------------------------------------------------------------- - public function test_util_is_functions() + public function test_is_functions() { $this->assertTrue($this->agent->is_browser()); + $this->assertTrue($this->agent->is_browser('Safari')); + $this->assertFalse($this->agent->is_browser('Firefox')); $this->assertFalse($this->agent->is_robot()); $this->assertFalse($this->agent->is_mobile()); + } + + // -------------------------------------------------------------------- + + public function test_referrer() + { + $_SERVER['HTTP_REFERER'] = 'http://codeigniter.com/user_guide/'; + $this->assertTrue($this->agent->is_referral()); + $this->assertEquals('http://codeigniter.com/user_guide/', $this->agent->referrer()); + + $this->agent->referer = NULL; + unset($_SERVER['HTTP_REFERER']); $this->assertFalse($this->agent->is_referral()); + $this->assertEquals('', $this->agent->referrer()); } // -------------------------------------------------------------------- @@ -63,7 +76,6 @@ class UserAgent_test extends CI_TestCase { $this->assertEquals('Safari', $this->agent->browser()); $this->assertEquals('533.20.27', $this->agent->version()); $this->assertEquals('', $this->agent->robot()); - $this->assertEquals('', $this->agent->referrer()); } // -------------------------------------------------------------------- @@ -71,14 +83,43 @@ class UserAgent_test extends CI_TestCase { public function test_charsets() { $_SERVER['HTTP_ACCEPT_CHARSET'] = 'utf8'; + $this->agent->charsets = array(); + $this->agent->charsets(); + $this->assertTrue($this->agent->accept_charset('utf8')); + $this->assertFalse($this->agent->accept_charset('foo')); + $this->assertEquals('utf8', $this->agent->charsets[0]); + + $_SERVER['HTTP_ACCEPT_CHARSET'] = ''; + $this->agent->charsets = array(); + $this->assertFalse($this->agent->accept_charset()); + $this->assertEquals('Undefined', $this->agent->charsets[0]); - $charsets = $this->agent->charsets(); - - $this->assertEquals('utf8', $charsets[0]); + $_SERVER['HTTP_ACCEPT_CHARSET'] = 'iso-8859-5, unicode-1-1; q=0.8'; + $this->agent->charsets = array(); + $this->assertTrue($this->agent->accept_charset('iso-8859-5')); + $this->assertTrue($this->agent->accept_charset('unicode-1-1')); + $this->assertFalse($this->agent->accept_charset('foo')); + $this->assertEquals('iso-8859-5', $this->agent->charsets[0]); + $this->assertEquals('unicode-1-1', $this->agent->charsets[1]); unset($_SERVER['HTTP_ACCEPT_CHARSET']); + } - $this->assertFalse($this->agent->accept_charset()); + public function test_parse() + { + $new_agent = 'Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0'; + $this->agent->parse($new_agent); + + $this->assertEquals('Android', $this->agent->platform()); + $this->assertEquals('Firefox', $this->agent->browser()); + $this->assertEquals('13.0', $this->agent->version()); + $this->assertEquals('', $this->agent->robot()); + $this->assertEquals('Android', $this->agent->mobile()); + $this->assertEquals($new_agent, $this->agent->agent_string()); + $this->assertTrue($this->agent->is_browser()); + $this->assertFalse($this->agent->is_robot()); + $this->assertTrue($this->agent->is_mobile()); + $this->assertTrue($this->agent->is_mobile('android')); } } \ No newline at end of file -- cgit v1.2.3-24-g4f1b