From f3ac71ea74351dc075d95867612da46834eccdf3 Mon Sep 17 00:00:00 2001 From: David Woods Date: Mon, 16 Mar 2015 20:00:21 -0700 Subject: Created setup and construct for Form_Validation unit test Signed-off-by: David Woods --- .../codeigniter/libraries/Form_validation_test.php | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/codeigniter/libraries/Form_validation_test.php (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php new file mode 100644 index 000000000..2a433af3d --- /dev/null +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -0,0 +1,35 @@ +helper() looks in the wrong directories for unit tests, + // We'll use CI_TestCase->helper() instead + $ldr = $this->getMock('CI_Loader', array('helper')); + // At current, CI_Form_Validation only calls load->helper("form") + // Assert this so that if that changes this fails fast + $ldr->expects($this->once()) + ->method('helper') + ->with($this->equalTo('form')); + + $this->ci_instance_var('load', $ldr); + $this->helper('form'); + + } + + public function test___construct() + { + $this->form_validation = new CI_Form_validation(); + + $this->assertNotNull($this->form_validation); + } + + public function test__construct_rules() + { + + } + + public function test_ + +} -- cgit v1.2.3-24-g4f1b From dc1ae6bd92bc0f2c3ee5ce812ec60e580c72501d Mon Sep 17 00:00:00 2001 From: David Woods Date: Mon, 16 Mar 2015 23:36:54 -0700 Subject: Fixed bugs in form_validation for methods matches, differs, and valid_base64. Implemented tests for valid and invalid inputs for all basic rules available for form_validation. The invalid input data currently doesn't pass all tests. Signed-off-by: David Woods --- .../codeigniter/libraries/Form_validation_test.php | 136 ++++++++++++++++++++- 1 file changed, 132 insertions(+), 4 deletions(-) (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index 2a433af3d..aefc9a2c7 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -13,9 +13,31 @@ class Form_validation_test extends CI_TestCase { ->method('helper') ->with($this->equalTo('form')); + // Same applies for lang + $lang = $this->getMock('CI_Lang', array('load')); + + // Setup CI_Input + // 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(); + + $this->ci_set_config('charset', 'UTF-8'); + $utf8 = new Mock_Core_Utf8(); + + $inp = new Mock_Core_Input($security, $utf8); + + $this->ci_instance_var('lang', $lang); $this->ci_instance_var('load', $ldr); + $this->ci_instance_var('input', $inp); + + $this->lang('form_validation'); $this->helper('form'); - } public function test___construct() @@ -25,11 +47,117 @@ class Form_validation_test extends CI_TestCase { $this->assertNotNull($this->form_validation); } - public function test__construct_rules() + public function test_rules_valid() { + $this->form_validation = new CI_Form_validation(); + $valid_posts = array( + 'required' => array('required',' !'), + 'matches[sample]' => 'sample', + 'differs[sample]' => 'differ', + 'min_length[4]' => array('is_more_than_4', '1234', ' 1'), + 'max_length[8]' => array('less_8', '12345678'), + 'exact_length[5]' => '12345', + 'greater_than[-5]' => array('-4','0','123124451'), + 'greater_than_equal_to[8]' => array('8', '99'), + 'less_than[0]' => '-1', + 'less_than_equal_to[5]' => array('-5', '5'), + 'in_list[red,blue,green]' => array('red', 'blue','green'), + 'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ', + 'alpha_numeric' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789', + 'alpha_numeric_spaces' => ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789', + 'alpha_dash' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_', + 'numeric' => '0123456789', + 'integer' => array('0', '-1231', '987234'), + 'decimal' => array('0.123', '1.0'), + 'is_natural' => '0', + 'is_natural_no_zero' => '1', + 'valid_url' => array('www.codeigniter.com','http://codeigniter.eu'), + 'valid_email' => 'email@sample.com', + 'valid_emails' => '1@sample.com,2@sample.com', + 'valid_ip[ipv4]' => '127.0.0.1', + 'valid_ip[ipv6]' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + 'valid_base64' => 'string' + ); + + // Loop through each rule and test + foreach ($valid_posts as $rule => $value) { + // Reset $_POST + $_POST = array(); + + if (is_array($value)) { + foreach($value as $item) { +// printf("%s => %s\n", $rule, $item); + $this->form_validation->set_rules('field', 'name', $rule); + $_POST['field'] = $item; + $this->assertTrue($this->form_validation->run()); + $this->form_validation->reset_validation(); + } + } + else { +// printf("%s => %s\n", $rule, $value); + $this->form_validation->set_rules('field', 'name', $rule); + $_POST['field'] = $value; + $this->assertTrue($this->form_validation->run()); + $this->form_validation->reset_validation(); + } + } } - public function test_ - + public function test_rules_invalid() + { + $this->form_validation = new CI_Form_validation(); + + $invalid_posts = array( + 'required' => array('',' '), + 'matches[sample]' => 'Sample', + 'differ[sample]' => 'sample', + 'min_length[4]' => array('123', ''), + 'max_length[8]' => array('more_than_8', '12345678 '), + 'exact_length[5]' => ' 12345', + 'greater_than[-5]' => array('-5, -12415'), + 'greater_than_equal_to[8]' => array('7', '0'), + 'less_than[0]' => '0', + 'less_than_equal_to[5]' => array('6', '98234'), + 'in_list[red,blue,green]' => array(' red', 'Blue','failz'), + 'alpha' => array('*', ' a', '1'), + 'alpha_numeric' => array('a1 ', '*', '1-'), + 'alpha_numeric_spaces' => array('a1*', ' ~'), + 'alpha_dash' => array('a b', '*'), + 'numeric' => array('a', ''), + 'integer' => array('0.123', '1a', ''), + 'decimal' => array('1', 'a',''), + 'is_natural' => array('1.2','aA',''), + 'is_natural_no_zero' => array('0','1.2',''), + 'valid_url' => array('codeigniter.com','nosite', ''), + 'valid_email' => '@sample.com', + 'valid_emails' => '@sample.com,2@sample.com,validemail@email.ca', + 'valid_ip[ipv4]' => '257.0.0.1', + 'valid_ip[ipv6]' => 'A001:0db8:85a3:0000:0000:8a2e:0370:7334', + ); + + // Loop through each rule and test + foreach ($invalid_posts as $rule => $value) { + // Reset $_POST + $_POST = array(); + + if (is_array($value)) { + foreach($value as $item) { + printf("%s => %s\n", $rule, $item); + $this->form_validation->set_rules('field', 'name', $rule); + $_POST['field'] = $item; + $this->assertFalse($this->form_validation->run()); + $this->form_validation->reset_validation(); + } + } + else { + printf("%s => %s\n", $rule, $value); + $this->form_validation->set_rules('field', 'name', $rule); + $_POST['field'] = $value; + $this->assertFalse($this->form_validation->run()); + $this->form_validation->reset_validation(); + } + } + } + } -- cgit v1.2.3-24-g4f1b From c7029e2e5f479a541d951d6f6ebf1b33a82a1632 Mon Sep 17 00:00:00 2001 From: David Woods Date: Tue, 17 Mar 2015 10:52:01 -0700 Subject: Reformatted unit tests for easier debugging Signed-off-by: David Woods --- .../codeigniter/libraries/Form_validation_test.php | 379 ++++++++++++++------- 1 file changed, 265 insertions(+), 114 deletions(-) (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index aefc9a2c7..bfc2083fd 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -4,6 +4,8 @@ class Form_validation_test extends CI_TestCase { public function set_up() { + $_SERVER['REQUEST_METHOD'] = 'POST'; + // Create a mock loader since load->helper() looks in the wrong directories for unit tests, // We'll use CI_TestCase->helper() instead $ldr = $this->getMock('CI_Loader', array('helper')); @@ -17,9 +19,6 @@ class Form_validation_test extends CI_TestCase { $lang = $this->getMock('CI_Lang', array('load')); // Setup CI_Input - // 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); @@ -38,126 +37,278 @@ class Form_validation_test extends CI_TestCase { $this->lang('form_validation'); $this->helper('form'); + + $this->form_validation = new CI_Form_validation(); } public function test___construct() { - $this->form_validation = new CI_Form_validation(); - $this->assertNotNull($this->form_validation); } - public function test_rules_valid() + public function test_rule_required() { - $this->form_validation = new CI_Form_validation(); + $this->assertTrue($this->run_rule('required', ' someValue')); - $valid_posts = array( - 'required' => array('required',' !'), - 'matches[sample]' => 'sample', - 'differs[sample]' => 'differ', - 'min_length[4]' => array('is_more_than_4', '1234', ' 1'), - 'max_length[8]' => array('less_8', '12345678'), - 'exact_length[5]' => '12345', - 'greater_than[-5]' => array('-4','0','123124451'), - 'greater_than_equal_to[8]' => array('8', '99'), - 'less_than[0]' => '-1', - 'less_than_equal_to[5]' => array('-5', '5'), - 'in_list[red,blue,green]' => array('red', 'blue','green'), - 'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ', - 'alpha_numeric' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789', - 'alpha_numeric_spaces' => ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789', - 'alpha_dash' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_', - 'numeric' => '0123456789', - 'integer' => array('0', '-1231', '987234'), - 'decimal' => array('0.123', '1.0'), - 'is_natural' => '0', - 'is_natural_no_zero' => '1', - 'valid_url' => array('www.codeigniter.com','http://codeigniter.eu'), - 'valid_email' => 'email@sample.com', - 'valid_emails' => '1@sample.com,2@sample.com', - 'valid_ip[ipv4]' => '127.0.0.1', - 'valid_ip[ipv6]' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334', - 'valid_base64' => 'string' - ); - - // Loop through each rule and test - foreach ($valid_posts as $rule => $value) { - // Reset $_POST - $_POST = array(); - - if (is_array($value)) { - foreach($value as $item) { -// printf("%s => %s\n", $rule, $item); - $this->form_validation->set_rules('field', 'name', $rule); - $_POST['field'] = $item; - $this->assertTrue($this->form_validation->run()); - $this->form_validation->reset_validation(); - } - } - else { -// printf("%s => %s\n", $rule, $value); - $this->form_validation->set_rules('field', 'name', $rule); - $_POST['field'] = $value; - $this->assertTrue($this->form_validation->run()); - $this->form_validation->reset_validation(); - } - } - } - - public function test_rules_invalid() + $this->assertFalse($this->run_rule('required', '')); + $this->assertFalse($this->run_rule('required', ' ')); + } + + public function test_rule_matches() { - $this->form_validation = new CI_Form_validation(); + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('matches[sample]', '')); + $this->assertTrue($this->run_rule('matches[s]', 's')); + + $this->assertFalse($this->run_rule('matches[Sample]', 'sample')); + $this->assertFalse($this->run_rule('matches[sample]', ' sample')); + } + + public function test_rule_differs() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('differs[sample]', '')); + $this->assertTrue($this->run_rule('differs[sample]', 'Sample')); + $this->assertTrue($this->run_rule('differs[sample]', ' sample')); + + $this->assertFalse($this->run_rule('differs[sample]', 'sample')); + } + + public function test_rule_min_length() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('min_length[34]', '')); + $this->assertTrue($this->run_rule('min_length[2]', '12')); + $this->assertTrue($this->run_rule('min_length[2]', ' 2')); + + $this->assertFalse($this->run_rule('min_length[2]', '1')); + $this->assertFalse($this->run_rule('min_length[4]|required', '')); + } + + public function test_rule_max_length() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('max_length[4]', '')); + $this->assertTrue($this->run_rule('max_length[4]', '1234')); + + $this->assertFalse($this->run_rule('max_length[4]', '12345')); + } + + public function test_rule_exact_length() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('exact_length[4]', '')); + $this->assertTrue($this->run_rule('exact_length[4]', '1234')); + + $this->assertFalse($this->run_rule('exact_length[4]', '123')); + $this->assertFalse($this->run_rule('exact_length[4]', '12345')); + } + + public function test_rule_greater_than() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('greater_than[-10]', '')); + $this->assertTrue($this->run_rule('greater_than[-10]', '-9')); + + $this->assertFalse($this->run_rule('greater_than[-10]', '-99')); + $this->assertFalse($this->run_rule('greater_than[-10]', 'a')); + } + + public function test_rule_greater_than_equal_to() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '')); + $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '0')); + $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '1')); + + $this->assertFalse($this->run_rule('greater_than_equal_to[0]', '-1')); + $this->assertFalse($this->run_rule('greater_than_equal_to[0]', 'a')); + } + + public function test_rule_less_than() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('less_than[0]', '')); + $this->assertTrue($this->run_rule('less_than[0]', '-1')); + + $this->assertFalse($this->run_rule('less_than[0]', '0')); + $this->assertFalse($this->run_rule('less_than[0]', 'a')); + } + + public function test_rule_less_than_equal_to() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('less_than_equal_to[0]', '')); + $this->assertTrue($this->run_rule('less_than_equal_to[0]', '-1')); + $this->assertTrue($this->run_rule('less_than_equal_to[0]', '0')); + + $this->assertFalse($this->run_rule('less_than_equal_to[0]', '1')); + $this->assertFalse($this->run_rule('less_than_equal_to[0]', 'a')); + } + + public function test_rule_in_list() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '')); + $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'red')); + $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'Blue')); + $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '123')); + + $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'Red')); + $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'blue')); + $this->assertFalse($this->run_rule('in_list[red,Blue,123]', ' red')); + } + + public function test_rule_alpha() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('alpha', '')); + $this->assertTrue($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); + + $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); + $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); + $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); + } + + public function test_rule_alpha_numeric() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('alpha_numeric', '')); + $this->assertTrue($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); + + $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); + $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); + } + + public function test_rule_alpha_numeric_spaces() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('alpha_numeric_spaces', '')); + $this->assertTrue($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); + + $this->assertFalse($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); + } + + public function test_rule_alpha_dash() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('alpha_dash', '')); + $this->assertTrue($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); + + $this->assertFalse($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); + } + + public function test_rule_numeric() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('numeric', '')); + $this->assertTrue($this->run_rule('numeric', '0')); + $this->assertTrue($this->run_rule('numeric', '12314')); + + $this->assertFalse($this->run_rule('numeric', '123a')); + } + + public function test_rule_integer() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('integer', '')); + $this->assertTrue($this->run_rule('integer', '0')); + $this->assertTrue($this->run_rule('integer', '42')); + + $this->assertFalse($this->run_rule('integer', '124a')); + $this->assertFalse($this->run_rule('integer', '1.9')); + } + + public function test_rule_decimal() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('decimal', '')); + $this->assertTrue($this->run_rule('decimal', '1.0')); + $this->assertTrue($this->run_rule('decimal', '0.98')); + + $this->assertFalse($this->run_rule('decimal', '1.0a')); + $this->assertFalse($this->run_rule('decimal', '-i')); + } + + public function test_rule_is_natural() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('is_natural', '')); + $this->assertTrue($this->run_rule('is_natural', '0')); + $this->assertTrue($this->run_rule('is_natural', '12')); + + $this->assertFalse($this->run_rule('is_natural', '42a')); + } + + public function test_rule_is_natural_no_zero() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('is_natural_no_zero', '')); + $this->assertTrue($this->run_rule('is_natural_no_zero', '42')); + + $this->assertFalse($this->run_rule('is_natural_no_zero', '0')); + $this->assertFalse($this->run_rule('is_natural_no_zero', '42a')); + } + + public function test_rule_valid_url() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_url', '')); + $this->assertTrue($this->run_rule('valid_url', 'www.codeigniter.com')); + $this->assertTrue($this->run_rule('valid_url', 'http://codeigniter.eu')); + + $this->assertFalse($this->run_rule('valid_url', 'codeigniter.c')); + } + + public function test_rule_valid_email() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_email', '')); + $this->assertTrue($this->run_rule('valid_email', 'email@sample.com')); + + $this->assertFalse($this->run_rule('valid_email', '@sample.com')); + } + + public function test_rule_valid_emails() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_emails', '')); + $this->assertTrue($this->run_rule('valid_emails', '1@sample.com,2@sample.com')); - $invalid_posts = array( - 'required' => array('',' '), - 'matches[sample]' => 'Sample', - 'differ[sample]' => 'sample', - 'min_length[4]' => array('123', ''), - 'max_length[8]' => array('more_than_8', '12345678 '), - 'exact_length[5]' => ' 12345', - 'greater_than[-5]' => array('-5, -12415'), - 'greater_than_equal_to[8]' => array('7', '0'), - 'less_than[0]' => '0', - 'less_than_equal_to[5]' => array('6', '98234'), - 'in_list[red,blue,green]' => array(' red', 'Blue','failz'), - 'alpha' => array('*', ' a', '1'), - 'alpha_numeric' => array('a1 ', '*', '1-'), - 'alpha_numeric_spaces' => array('a1*', ' ~'), - 'alpha_dash' => array('a b', '*'), - 'numeric' => array('a', ''), - 'integer' => array('0.123', '1a', ''), - 'decimal' => array('1', 'a',''), - 'is_natural' => array('1.2','aA',''), - 'is_natural_no_zero' => array('0','1.2',''), - 'valid_url' => array('codeigniter.com','nosite', ''), - 'valid_email' => '@sample.com', - 'valid_emails' => '@sample.com,2@sample.com,validemail@email.ca', - 'valid_ip[ipv4]' => '257.0.0.1', - 'valid_ip[ipv6]' => 'A001:0db8:85a3:0000:0000:8a2e:0370:7334', - ); - - // Loop through each rule and test - foreach ($invalid_posts as $rule => $value) { - // Reset $_POST - $_POST = array(); - - if (is_array($value)) { - foreach($value as $item) { - printf("%s => %s\n", $rule, $item); - $this->form_validation->set_rules('field', 'name', $rule); - $_POST['field'] = $item; - $this->assertFalse($this->form_validation->run()); - $this->form_validation->reset_validation(); - } - } - else { - printf("%s => %s\n", $rule, $value); - $this->form_validation->set_rules('field', 'name', $rule); - $_POST['field'] = $value; - $this->assertFalse($this->form_validation->run()); - $this->form_validation->reset_validation(); - } - } - } - + $this->assertFalse($this->run_rule('valid_emails', '@sample.com,2@sample.com,validemail@email.ca')); + } + + public function test_rule_valid_ip() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_ip', '')); + $this->assertTrue($this->run_rule('valid_ip', '127.0.0.1')); + $this->assertTrue($this->run_rule('valid_ip[ipv4]', '127.0.0.1')); + $this->assertTrue($this->run_rule('valid_ip', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); + $this->assertTrue($this->run_rule('valid_ip[ipv6]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); + + $this->assertFalse($this->run_rule('valid_ip[ipv4]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); + $this->assertFalse($this->run_rule('valid_ip[ipv6]', '127.0.0.1')); + $this->assertFalse($this->run_rule('valid_ip', 'H001:0db8:85a3:0000:0000:8a2e:0370:7334')); + $this->assertFalse($this->run_rule('valid_ip', '127.0.0.259')); + } + + public function test_rule_valid_base64() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_base64', '')); + $this->assertTrue($this->run_rule('valid_base64', 'string')); + } + + public function run_rule($rule, $test_value) + { +// $this->markTestSkipped('Not designed to be a unit test'); + // Reset the _$POST array + $_POST = array(); + $this->form_validation->reset_validation(); + + $this->form_validation->set_rules('field', 'name', $rule); + $_POST['field'] = $test_value; + return $this->form_validation->run(); + } } -- cgit v1.2.3-24-g4f1b From ce4237a6c48fc2538fcb149730c4f62a02dce849 Mon Sep 17 00:00:00 2001 From: David Woods Date: Tue, 17 Mar 2015 11:28:07 -0700 Subject: Corrected an invalid test case Signed-off-by: David Woods --- tests/codeigniter/libraries/Form_validation_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index bfc2083fd..5b2e9de70 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -256,8 +256,8 @@ class Form_validation_test extends CI_TestCase { $this->assertTrue($this->run_rule('valid_url', '')); $this->assertTrue($this->run_rule('valid_url', 'www.codeigniter.com')); $this->assertTrue($this->run_rule('valid_url', 'http://codeigniter.eu')); - - $this->assertFalse($this->run_rule('valid_url', 'codeigniter.c')); + + $this->assertFalse($this->run_rule('valid_url', 'codeigniter')); } public function test_rule_valid_email() -- cgit v1.2.3-24-g4f1b From 64af3bb57e3d84ec9dd79b8e7f82eba57c46c296 Mon Sep 17 00:00:00 2001 From: David Woods Date: Wed, 18 Mar 2015 10:09:26 -0700 Subject: Corrected match, differs, base64, and valid_url test cases. Also changed spaces to tabs Signed-off-by: David Woods --- .../codeigniter/libraries/Form_validation_test.php | 617 +++++++++++---------- 1 file changed, 310 insertions(+), 307 deletions(-) (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index 5b2e9de70..571e2cfce 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -1,314 +1,317 @@ helper() looks in the wrong directories for unit tests, - // We'll use CI_TestCase->helper() instead - $ldr = $this->getMock('CI_Loader', array('helper')); - // At current, CI_Form_Validation only calls load->helper("form") - // Assert this so that if that changes this fails fast - $ldr->expects($this->once()) + + public function set_up() + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + + // Create a mock loader since load->helper() looks in the wrong directories for unit tests, + // We'll use CI_TestCase->helper() instead + $loader = $this->getMock('CI_Loader', array('helper')); + // At current, CI_Form_Validation only calls load->helper("form") + // Assert this so that if that changes this fails fast + $loader->expects($this->once()) ->method('helper') ->with($this->equalTo('form')); - - // Same applies for lang - $lang = $this->getMock('CI_Lang', array('load')); - - // Setup CI_Input - // 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(); - - $this->ci_set_config('charset', 'UTF-8'); - $utf8 = new Mock_Core_Utf8(); - - $inp = new Mock_Core_Input($security, $utf8); - - $this->ci_instance_var('lang', $lang); - $this->ci_instance_var('load', $ldr); - $this->ci_instance_var('input', $inp); - - $this->lang('form_validation'); - $this->helper('form'); - - $this->form_validation = new CI_Form_validation(); - } - - public function test___construct() - { - $this->assertNotNull($this->form_validation); - } - - public function test_rule_required() - { - $this->assertTrue($this->run_rule('required', ' someValue')); - - $this->assertFalse($this->run_rule('required', '')); - $this->assertFalse($this->run_rule('required', ' ')); - } - - public function test_rule_matches() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('matches[sample]', '')); - $this->assertTrue($this->run_rule('matches[s]', 's')); - - $this->assertFalse($this->run_rule('matches[Sample]', 'sample')); - $this->assertFalse($this->run_rule('matches[sample]', ' sample')); - } - - public function test_rule_differs() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('differs[sample]', '')); - $this->assertTrue($this->run_rule('differs[sample]', 'Sample')); - $this->assertTrue($this->run_rule('differs[sample]', ' sample')); - - $this->assertFalse($this->run_rule('differs[sample]', 'sample')); - } - - public function test_rule_min_length() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('min_length[34]', '')); - $this->assertTrue($this->run_rule('min_length[2]', '12')); - $this->assertTrue($this->run_rule('min_length[2]', ' 2')); - - $this->assertFalse($this->run_rule('min_length[2]', '1')); - $this->assertFalse($this->run_rule('min_length[4]|required', '')); - } - - public function test_rule_max_length() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('max_length[4]', '')); - $this->assertTrue($this->run_rule('max_length[4]', '1234')); - - $this->assertFalse($this->run_rule('max_length[4]', '12345')); - } - - public function test_rule_exact_length() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('exact_length[4]', '')); - $this->assertTrue($this->run_rule('exact_length[4]', '1234')); - - $this->assertFalse($this->run_rule('exact_length[4]', '123')); - $this->assertFalse($this->run_rule('exact_length[4]', '12345')); - } - - public function test_rule_greater_than() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('greater_than[-10]', '')); - $this->assertTrue($this->run_rule('greater_than[-10]', '-9')); - - $this->assertFalse($this->run_rule('greater_than[-10]', '-99')); - $this->assertFalse($this->run_rule('greater_than[-10]', 'a')); - } - - public function test_rule_greater_than_equal_to() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '')); - $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '0')); - $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '1')); - - $this->assertFalse($this->run_rule('greater_than_equal_to[0]', '-1')); - $this->assertFalse($this->run_rule('greater_than_equal_to[0]', 'a')); - } - - public function test_rule_less_than() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('less_than[0]', '')); - $this->assertTrue($this->run_rule('less_than[0]', '-1')); - - $this->assertFalse($this->run_rule('less_than[0]', '0')); - $this->assertFalse($this->run_rule('less_than[0]', 'a')); - } - - public function test_rule_less_than_equal_to() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('less_than_equal_to[0]', '')); - $this->assertTrue($this->run_rule('less_than_equal_to[0]', '-1')); - $this->assertTrue($this->run_rule('less_than_equal_to[0]', '0')); - - $this->assertFalse($this->run_rule('less_than_equal_to[0]', '1')); - $this->assertFalse($this->run_rule('less_than_equal_to[0]', 'a')); - } - - public function test_rule_in_list() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '')); - $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'red')); - $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'Blue')); - $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '123')); - - $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'Red')); - $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'blue')); - $this->assertFalse($this->run_rule('in_list[red,Blue,123]', ' red')); - } - - public function test_rule_alpha() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('alpha', '')); - $this->assertTrue($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); - - $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); - $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); - $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); - } - - public function test_rule_alpha_numeric() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('alpha_numeric', '')); - $this->assertTrue($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); - - $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); - $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); - } - - public function test_rule_alpha_numeric_spaces() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('alpha_numeric_spaces', '')); - $this->assertTrue($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); - - $this->assertFalse($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); - } - - public function test_rule_alpha_dash() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('alpha_dash', '')); - $this->assertTrue($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); - - $this->assertFalse($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); - } - - public function test_rule_numeric() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('numeric', '')); - $this->assertTrue($this->run_rule('numeric', '0')); - $this->assertTrue($this->run_rule('numeric', '12314')); - - $this->assertFalse($this->run_rule('numeric', '123a')); - } - - public function test_rule_integer() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('integer', '')); - $this->assertTrue($this->run_rule('integer', '0')); - $this->assertTrue($this->run_rule('integer', '42')); - - $this->assertFalse($this->run_rule('integer', '124a')); - $this->assertFalse($this->run_rule('integer', '1.9')); - } - - public function test_rule_decimal() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('decimal', '')); - $this->assertTrue($this->run_rule('decimal', '1.0')); - $this->assertTrue($this->run_rule('decimal', '0.98')); - - $this->assertFalse($this->run_rule('decimal', '1.0a')); - $this->assertFalse($this->run_rule('decimal', '-i')); - } - - public function test_rule_is_natural() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('is_natural', '')); - $this->assertTrue($this->run_rule('is_natural', '0')); - $this->assertTrue($this->run_rule('is_natural', '12')); - - $this->assertFalse($this->run_rule('is_natural', '42a')); - } - - public function test_rule_is_natural_no_zero() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('is_natural_no_zero', '')); - $this->assertTrue($this->run_rule('is_natural_no_zero', '42')); - - $this->assertFalse($this->run_rule('is_natural_no_zero', '0')); - $this->assertFalse($this->run_rule('is_natural_no_zero', '42a')); - } - - public function test_rule_valid_url() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_url', '')); - $this->assertTrue($this->run_rule('valid_url', 'www.codeigniter.com')); - $this->assertTrue($this->run_rule('valid_url', 'http://codeigniter.eu')); - - $this->assertFalse($this->run_rule('valid_url', 'codeigniter')); - } - - public function test_rule_valid_email() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_email', '')); - $this->assertTrue($this->run_rule('valid_email', 'email@sample.com')); - - $this->assertFalse($this->run_rule('valid_email', '@sample.com')); - } - - public function test_rule_valid_emails() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_emails', '')); - $this->assertTrue($this->run_rule('valid_emails', '1@sample.com,2@sample.com')); - - $this->assertFalse($this->run_rule('valid_emails', '@sample.com,2@sample.com,validemail@email.ca')); - } - - public function test_rule_valid_ip() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_ip', '')); - $this->assertTrue($this->run_rule('valid_ip', '127.0.0.1')); - $this->assertTrue($this->run_rule('valid_ip[ipv4]', '127.0.0.1')); - $this->assertTrue($this->run_rule('valid_ip', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); - $this->assertTrue($this->run_rule('valid_ip[ipv6]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); - - $this->assertFalse($this->run_rule('valid_ip[ipv4]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); - $this->assertFalse($this->run_rule('valid_ip[ipv6]', '127.0.0.1')); - $this->assertFalse($this->run_rule('valid_ip', 'H001:0db8:85a3:0000:0000:8a2e:0370:7334')); - $this->assertFalse($this->run_rule('valid_ip', '127.0.0.259')); - } - - public function test_rule_valid_base64() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_base64', '')); - $this->assertTrue($this->run_rule('valid_base64', 'string')); - } - - public function run_rule($rule, $test_value) - { + // Same applies for lang + $lang = $this->getMock('CI_Lang', array('load')); + + $this->ci_set_config('charset', 'UTF-8'); + $utf8 = new Mock_Core_Utf8(); + $security = new Mock_Core_Security(); + $input = new Mock_Core_Input($security, $utf8); + + $this->ci_instance_var('lang', $lang); + $this->ci_instance_var('load', $loader); + $this->ci_instance_var('input', $input); + + $this->lang('form_validation'); + $this->helper('form'); + + $this->form_validation = new CI_Form_validation(); + } + + public function test___construct() + { + $this->assertNotNull($this->form_validation); + } + + public function test_rule_required() + { + $this->assertTrue($this->run_rule('required', ' someValue')); + + $this->assertFalse($this->run_rule('required', '')); + $this->assertFalse($this->run_rule('required', ' ')); + } + + public function test_rule_matches() + { + // Empty input should pass any rule unless required is also specified + $_POST['to_match'] = 'sample'; + $this->assertTrue($this->run_rule('matches[to_match]', '')); + $_POST['to_match'] = 'sample'; + $this->assertTrue($this->run_rule('matches[to_match]', 'sample')); + + $_POST['to_match'] = 'sample'; + $this->assertFalse($this->run_rule('matches[to_match]', 'Sample')); + $_POST['to_match'] = 'sample'; + $this->assertFalse($this->run_rule('matches[to_match]', ' sample')); + } + + public function test_rule_differs() + { + // Empty input should pass any rule unless required is also specified + $_POST['to_differ'] = 'sample'; + $this->assertTrue($this->run_rule('differs[to_differ]', '')); + $_POST['to_differ'] = 'sample'; + $this->assertTrue($this->run_rule('differs[to_differ]', 'Sample')); + $_POST['to_differ'] = 'sample'; + $this->assertTrue($this->run_rule('differs[to_differ]', ' sample')); + + $_POST['to_differ'] = 'sample'; + $this->assertFalse($this->run_rule('differs[to_differ]', 'sample')); + } + + public function test_rule_min_length() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('min_length[34]', '')); + $this->assertTrue($this->run_rule('min_length[2]', '12')); + $this->assertTrue($this->run_rule('min_length[2]', ' 2')); + + $this->assertFalse($this->run_rule('min_length[2]', '1')); + $this->assertFalse($this->run_rule('min_length[4]|required', '')); + } + + public function test_rule_max_length() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('max_length[4]', '')); + $this->assertTrue($this->run_rule('max_length[4]', '1234')); + + $this->assertFalse($this->run_rule('max_length[4]', '12345')); + } + + public function test_rule_exact_length() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('exact_length[4]', '')); + $this->assertTrue($this->run_rule('exact_length[4]', '1234')); + + $this->assertFalse($this->run_rule('exact_length[4]', '123')); + $this->assertFalse($this->run_rule('exact_length[4]', '12345')); + } + + public function test_rule_greater_than() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('greater_than[-10]', '')); + $this->assertTrue($this->run_rule('greater_than[-10]', '-9')); + + $this->assertFalse($this->run_rule('greater_than[-10]', '-99')); + $this->assertFalse($this->run_rule('greater_than[-10]', 'a')); + } + + public function test_rule_greater_than_equal_to() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '')); + $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '0')); + $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '1')); + + $this->assertFalse($this->run_rule('greater_than_equal_to[0]', '-1')); + $this->assertFalse($this->run_rule('greater_than_equal_to[0]', 'a')); + } + + public function test_rule_less_than() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('less_than[0]', '')); + $this->assertTrue($this->run_rule('less_than[0]', '-1')); + + $this->assertFalse($this->run_rule('less_than[0]', '0')); + $this->assertFalse($this->run_rule('less_than[0]', 'a')); + } + + public function test_rule_less_than_equal_to() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('less_than_equal_to[0]', '')); + $this->assertTrue($this->run_rule('less_than_equal_to[0]', '-1')); + $this->assertTrue($this->run_rule('less_than_equal_to[0]', '0')); + + $this->assertFalse($this->run_rule('less_than_equal_to[0]', '1')); + $this->assertFalse($this->run_rule('less_than_equal_to[0]', 'a')); + } + + public function test_rule_in_list() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '')); + $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'red')); + $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'Blue')); + $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '123')); + + $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'Red')); + $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'blue')); + $this->assertFalse($this->run_rule('in_list[red,Blue,123]', ' red')); + } + + public function test_rule_alpha() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('alpha', '')); + $this->assertTrue($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); + + $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); + $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); + $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); + } + + public function test_rule_alpha_numeric() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('alpha_numeric', '')); + $this->assertTrue($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); + + $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); + $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); + } + + public function test_rule_alpha_numeric_spaces() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('alpha_numeric_spaces', '')); + $this->assertTrue($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); + + $this->assertFalse($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); + } + + public function test_rule_alpha_dash() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('alpha_dash', '')); + $this->assertTrue($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); + + $this->assertFalse($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); + } + + public function test_rule_numeric() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('numeric', '')); + $this->assertTrue($this->run_rule('numeric', '0')); + $this->assertTrue($this->run_rule('numeric', '12314')); + $this->assertTrue($this->run_rule('numeric', '-42')); + + $this->assertFalse($this->run_rule('numeric', '123a')); + } + + public function test_rule_integer() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('integer', '')); + $this->assertTrue($this->run_rule('integer', '0')); + $this->assertTrue($this->run_rule('integer', '42')); + + $this->assertFalse($this->run_rule('integer', '124a')); + $this->assertFalse($this->run_rule('integer', '1.9')); + } + + public function test_rule_decimal() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('decimal', '')); + $this->assertTrue($this->run_rule('decimal', '1.0')); + $this->assertTrue($this->run_rule('decimal', '0.98')); + + $this->assertFalse($this->run_rule('decimal', '1.0a')); + $this->assertFalse($this->run_rule('decimal', '-i')); + } + + public function test_rule_is_natural() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('is_natural', '')); + $this->assertTrue($this->run_rule('is_natural', '0')); + $this->assertTrue($this->run_rule('is_natural', '12')); + + $this->assertFalse($this->run_rule('is_natural', '42a')); + } + + public function test_rule_is_natural_no_zero() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('is_natural_no_zero', '')); + $this->assertTrue($this->run_rule('is_natural_no_zero', '42')); + + $this->assertFalse($this->run_rule('is_natural_no_zero', '0')); + $this->assertFalse($this->run_rule('is_natural_no_zero', '42a')); + } + + public function test_rule_valid_url() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_url', '')); + $this->assertTrue($this->run_rule('valid_url', 'www.codeigniter.com')); + $this->assertTrue($this->run_rule('valid_url', 'http://codeigniter.eu')); + + $this->assertFalse($this->run_rule('valid_url', 'code igniter')); + } + + public function test_rule_valid_email() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_email', '')); + $this->assertTrue($this->run_rule('valid_email', 'email@sample.com')); + + $this->assertFalse($this->run_rule('valid_email', '@sample.com')); + } + + public function test_rule_valid_emails() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_emails', '')); + $this->assertTrue($this->run_rule('valid_emails', '1@sample.com,2@sample.com')); + + $this->assertFalse($this->run_rule('valid_emails', '@sample.com,2@sample.com,validemail@email.ca')); + } + + public function test_rule_valid_ip() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_ip', '')); + $this->assertTrue($this->run_rule('valid_ip', '127.0.0.1')); + $this->assertTrue($this->run_rule('valid_ip[ipv4]', '127.0.0.1')); + $this->assertTrue($this->run_rule('valid_ip', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); + $this->assertTrue($this->run_rule('valid_ip[ipv6]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); + + $this->assertFalse($this->run_rule('valid_ip[ipv4]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); + $this->assertFalse($this->run_rule('valid_ip[ipv6]', '127.0.0.1')); + $this->assertFalse($this->run_rule('valid_ip', 'H001:0db8:85a3:0000:0000:8a2e:0370:7334')); + $this->assertFalse($this->run_rule('valid_ip', '127.0.0.259')); + } + + public function test_rule_valid_base64() + { + // Empty input should pass any rule unless required is also specified + $this->assertTrue($this->run_rule('valid_base64', '')); + $this->assertTrue($this->run_rule('valid_base64', base64_encode('string'))); + + $this->assertTrue($this->run_rule('valid_base64', "FA08GG")); + } + + public function run_rule($rule, $test_value) + { // $this->markTestSkipped('Not designed to be a unit test'); - // Reset the _$POST array - $_POST = array(); - $this->form_validation->reset_validation(); - - $this->form_validation->set_rules('field', 'name', $rule); - $_POST['field'] = $test_value; - return $this->form_validation->run(); - } + // Reset the _$POST array + $_POST = array(); + $this->form_validation->reset_validation(); + + $this->form_validation->set_rules('field', 'name', $rule); + $_POST['field'] = $test_value; + return $this->form_validation->run(); + } + } -- cgit v1.2.3-24-g4f1b From 63c288cdd7dd66c653da1092ac07a035b01ef560 Mon Sep 17 00:00:00 2001 From: David Woods Date: Wed, 18 Mar 2015 10:13:34 -0700 Subject: Corrected incorrect assertion in test_rule_valid_base64 Signed-off-by: David Woods --- tests/codeigniter/libraries/Form_validation_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index 571e2cfce..9c21b9cfb 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -299,7 +299,7 @@ class Form_validation_test extends CI_TestCase { $this->assertTrue($this->run_rule('valid_base64', '')); $this->assertTrue($this->run_rule('valid_base64', base64_encode('string'))); - $this->assertTrue($this->run_rule('valid_base64', "FA08GG")); + $this->assertFalse($this->run_rule('valid_base64', "FA08GG")); } public function run_rule($rule, $test_value) -- cgit v1.2.3-24-g4f1b From 70e220ad2423f2f701b0db12686dad2e7e9a6458 Mon Sep 17 00:00:00 2001 From: David Woods Date: Wed, 18 Mar 2015 10:29:20 -0700 Subject: Fixed bug of clearing POST array before every test. Signed-off-by: David Woods --- .../codeigniter/libraries/Form_validation_test.php | 26 ++++++++++++---------- 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index 9c21b9cfb..3dff2f374 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -49,28 +49,28 @@ class Form_validation_test extends CI_TestCase { { // Empty input should pass any rule unless required is also specified $_POST['to_match'] = 'sample'; - $this->assertTrue($this->run_rule('matches[to_match]', '')); + $this->assertTrue($this->run_rule('matches[to_match]', '', FALSE)); $_POST['to_match'] = 'sample'; - $this->assertTrue($this->run_rule('matches[to_match]', 'sample')); + $this->assertTrue($this->run_rule('matches[to_match]', 'sample', FALSE)); $_POST['to_match'] = 'sample'; - $this->assertFalse($this->run_rule('matches[to_match]', 'Sample')); + $this->assertFalse($this->run_rule('matches[to_match]', 'Sample', FALSE)); $_POST['to_match'] = 'sample'; - $this->assertFalse($this->run_rule('matches[to_match]', ' sample')); + $this->assertFalse($this->run_rule('matches[to_match]', ' sample', FALSE)); } public function test_rule_differs() { // Empty input should pass any rule unless required is also specified $_POST['to_differ'] = 'sample'; - $this->assertTrue($this->run_rule('differs[to_differ]', '')); + $this->assertTrue($this->run_rule('differs[to_differ]', '', FALSE)); $_POST['to_differ'] = 'sample'; - $this->assertTrue($this->run_rule('differs[to_differ]', 'Sample')); + $this->assertTrue($this->run_rule('differs[to_differ]', 'Sample', FALSE)); $_POST['to_differ'] = 'sample'; - $this->assertTrue($this->run_rule('differs[to_differ]', ' sample')); + $this->assertTrue($this->run_rule('differs[to_differ]', ' sample', FALSE)); $_POST['to_differ'] = 'sample'; - $this->assertFalse($this->run_rule('differs[to_differ]', 'sample')); + $this->assertFalse($this->run_rule('differs[to_differ]', 'sample', FALSE)); } public function test_rule_min_length() @@ -302,13 +302,15 @@ class Form_validation_test extends CI_TestCase { $this->assertFalse($this->run_rule('valid_base64', "FA08GG")); } - public function run_rule($rule, $test_value) + public function run_rule($rule, $test_value, $reset_post = TRUE) { // $this->markTestSkipped('Not designed to be a unit test'); - // Reset the _$POST array - $_POST = array(); $this->form_validation->reset_validation(); - + if ($reset_post === TRUE) + { + $_POST = array(); + } + $this->form_validation->set_rules('field', 'name', $rule); $_POST['field'] = $test_value; return $this->form_validation->run(); -- cgit v1.2.3-24-g4f1b From 5b88473a716a3c69e72c67f1cfe26452db9e1172 Mon Sep 17 00:00:00 2001 From: David Woods Date: Wed, 18 Mar 2015 10:37:35 -0700 Subject: Switched spaces to tabs on the few lines that were missed from previous refactors Signed-off-by: David Woods --- tests/codeigniter/libraries/Form_validation_test.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index 3dff2f374..18b7c83d6 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -8,14 +8,14 @@ class Form_validation_test extends CI_TestCase { // Create a mock loader since load->helper() looks in the wrong directories for unit tests, // We'll use CI_TestCase->helper() instead - $loader = $this->getMock('CI_Loader', array('helper')); + $loader = $this->getMock('CI_Loader', array('helper')); // At current, CI_Form_Validation only calls load->helper("form") // Assert this so that if that changes this fails fast - $loader->expects($this->once()) - ->method('helper') - ->with($this->equalTo('form')); + $loader->expects($this->once()) + ->method('helper') + ->with($this->equalTo('form')); // Same applies for lang - $lang = $this->getMock('CI_Lang', array('load')); + $lang = $this->getMock('CI_Lang', array('load')); $this->ci_set_config('charset', 'UTF-8'); $utf8 = new Mock_Core_Utf8(); @@ -298,7 +298,7 @@ class Form_validation_test extends CI_TestCase { // Empty input should pass any rule unless required is also specified $this->assertTrue($this->run_rule('valid_base64', '')); $this->assertTrue($this->run_rule('valid_base64', base64_encode('string'))); - + $this->assertFalse($this->run_rule('valid_base64', "FA08GG")); } @@ -310,7 +310,7 @@ class Form_validation_test extends CI_TestCase { { $_POST = array(); } - + $this->form_validation->set_rules('field', 'name', $rule); $_POST['field'] = $test_value; return $this->form_validation->run(); -- cgit v1.2.3-24-g4f1b From 317cad99f23cc80577039a5b709b39cad72690c5 Mon Sep 17 00:00:00 2001 From: David Woods Date: Fri, 20 Mar 2015 22:32:24 -0700 Subject: Changed scenario based tests to unit tests Added tests for set_data() & set_message() Signed-off-by: David Woods --- .../codeigniter/libraries/Form_validation_test.php | 323 +++++++++++---------- 1 file changed, 175 insertions(+), 148 deletions(-) (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index 18b7c83d6..088a0ce3e 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -11,8 +11,7 @@ class Form_validation_test extends CI_TestCase { $loader = $this->getMock('CI_Loader', array('helper')); // At current, CI_Form_Validation only calls load->helper("form") // Assert this so that if that changes this fails fast - $loader->expects($this->once()) - ->method('helper') + $loader->method('helper') ->with($this->equalTo('form')); // Same applies for lang $lang = $this->getMock('CI_Lang', array('load')); @@ -34,8 +33,9 @@ class Form_validation_test extends CI_TestCase { public function test___construct() { - $this->assertNotNull($this->form_validation); - } + $form_validation = new CI_Form_validation(); + $this->assertNotNull($form_validation); + } public function test_rule_required() { @@ -74,232 +74,259 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_min_length() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('min_length[34]', '')); - $this->assertTrue($this->run_rule('min_length[2]', '12')); - $this->assertTrue($this->run_rule('min_length[2]', ' 2')); + { + $this->assertTrue($this->form_validation->min_length('12345', '5')); + $this->assertTrue($this->form_validation->min_length('test', '0')); - $this->assertFalse($this->run_rule('min_length[2]', '1')); - $this->assertFalse($this->run_rule('min_length[4]|required', '')); + $this->assertFalse($this->form_validation->min_length('123', '4')); + $this->assertFalse($this->form_validation->min_length('should_fail', 'A')); + $this->assertFalse($this->form_validation->min_length('', '4')); } public function test_rule_max_length() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('max_length[4]', '')); - $this->assertTrue($this->run_rule('max_length[4]', '1234')); + { + $this->assertTrue($this->form_validation->max_length('', '4')); + $this->assertTrue($this->form_validation->max_length('1234', '4')); - $this->assertFalse($this->run_rule('max_length[4]', '12345')); + $this->assertFalse($this->form_validation->max_length('12345', '4')); + $this->assertFalse($this->form_validation->max_length('should_fail', 'A')); } public function test_rule_exact_length() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('exact_length[4]', '')); - $this->assertTrue($this->run_rule('exact_length[4]', '1234')); + { + $this->assertTrue($this->form_validation->exact_length('1234', '4')); - $this->assertFalse($this->run_rule('exact_length[4]', '123')); - $this->assertFalse($this->run_rule('exact_length[4]', '12345')); + $this->assertFalse($this->form_validation->exact_length('', '3')); + $this->assertFalse($this->form_validation->exact_length('12345', '4')); + $this->assertFalse($this->form_validation->exact_length('123', '4')); + $this->assertFalse($this->form_validation->exact_length('should_fail', 'A')); } public function test_rule_greater_than() { // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('greater_than[-10]', '')); - $this->assertTrue($this->run_rule('greater_than[-10]', '-9')); + $this->assertTrue($this->form_validation->greater_than('-10', '-11')); + $this->assertTrue($this->form_validation->greater_than('10', '9')); - $this->assertFalse($this->run_rule('greater_than[-10]', '-99')); - $this->assertFalse($this->run_rule('greater_than[-10]', 'a')); + $this->assertFalse($this->form_validation->greater_than('10', '10')); + $this->assertFalse($this->form_validation->greater_than('10', 'a')); + $this->assertFalse($this->form_validation->greater_than('10a', '10')); } public function test_rule_greater_than_equal_to() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '')); - $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '0')); - $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '1')); + { + $this->assertTrue($this->form_validation->greater_than_equal_to('0', '0')); + $this->assertTrue($this->form_validation->greater_than_equal_to('1', '0')); - $this->assertFalse($this->run_rule('greater_than_equal_to[0]', '-1')); - $this->assertFalse($this->run_rule('greater_than_equal_to[0]', 'a')); + $this->assertFalse($this->form_validation->greater_than_equal_to('-1', '0')); + $this->assertFalse($this->form_validation->greater_than_equal_to('10a', '0')); } public function test_rule_less_than() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('less_than[0]', '')); - $this->assertTrue($this->run_rule('less_than[0]', '-1')); + { + $this->assertTrue($this->form_validation->less_than('4', '5')); + $this->assertTrue($this->form_validation->less_than('-1', '0')); - $this->assertFalse($this->run_rule('less_than[0]', '0')); - $this->assertFalse($this->run_rule('less_than[0]', 'a')); + $this->assertFalse($this->form_validation->less_than('4', '4')); + $this->assertFalse($this->form_validation->less_than('10a', '5')); } public function test_rule_less_than_equal_to() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('less_than_equal_to[0]', '')); - $this->assertTrue($this->run_rule('less_than_equal_to[0]', '-1')); - $this->assertTrue($this->run_rule('less_than_equal_to[0]', '0')); + { + $this->assertTrue($this->form_validation->less_than_equal_to('-1', '0')); + $this->assertTrue($this->form_validation->less_than_equal_to('-1', '-1')); + $this->assertTrue($this->form_validation->less_than_equal_to('4', '4')); - $this->assertFalse($this->run_rule('less_than_equal_to[0]', '1')); - $this->assertFalse($this->run_rule('less_than_equal_to[0]', 'a')); + $this->assertFalse($this->form_validation->less_than_equal_to('0', '-1')); + $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0')); } public function test_rule_in_list() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '')); - $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'red')); - $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'Blue')); - $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '123')); - - $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'Red')); - $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'blue')); - $this->assertFalse($this->run_rule('in_list[red,Blue,123]', ' red')); + { + $this->assertTrue($this->form_validation->in_list('red', 'red,Blue,123')); + $this->assertTrue($this->form_validation->in_list('Blue', 'red,Blue,123')); + $this->assertTrue($this->form_validation->in_list('123', 'red,Blue,123')); + + $this->assertFalse($this->form_validation->in_list('Red', 'red,Blue,123')); + $this->assertFalse($this->form_validation->in_list(' red', 'red,Blue,123')); + $this->assertFalse($this->form_validation->in_list('1234', 'red,Blue,123')); } public function test_rule_alpha() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('alpha', '')); - $this->assertTrue($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); + { + $this->assertTrue($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); - $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); - $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); - $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); + $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); + $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); + $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); } public function test_rule_alpha_numeric() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('alpha_numeric', '')); - $this->assertTrue($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); + { + $this->assertTrue($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); - $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); - $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); + $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); + $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); } public function test_rule_alpha_numeric_spaces() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('alpha_numeric_spaces', '')); - $this->assertTrue($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); + { + $this->assertTrue($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); - $this->assertFalse($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); + $this->assertFalse($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); } public function test_rule_alpha_dash() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('alpha_dash', '')); - $this->assertTrue($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); + { + $this->assertTrue($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); - $this->assertFalse($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); + $this->assertFalse($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); } public function test_rule_numeric() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('numeric', '')); - $this->assertTrue($this->run_rule('numeric', '0')); - $this->assertTrue($this->run_rule('numeric', '12314')); - $this->assertTrue($this->run_rule('numeric', '-42')); + { + $this->assertTrue($this->form_validation->numeric('0')); + $this->assertTrue($this->form_validation->numeric('12314')); + $this->assertTrue($this->form_validation->numeric('-42')); - $this->assertFalse($this->run_rule('numeric', '123a')); + $this->assertFalse($this->form_validation->numeric('123a')); + $this->assertFalse($this->form_validation->numeric('--1')); } public function test_rule_integer() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('integer', '')); - $this->assertTrue($this->run_rule('integer', '0')); - $this->assertTrue($this->run_rule('integer', '42')); - - $this->assertFalse($this->run_rule('integer', '124a')); - $this->assertFalse($this->run_rule('integer', '1.9')); + { + $this->assertTrue($this->form_validation->integer('0')); + $this->assertTrue($this->form_validation->integer('42')); + $this->assertTrue($this->form_validation->integer('-1')); + + $this->assertFalse($this->form_validation->integer('124a')); + $this->assertFalse($this->form_validation->integer('1.9')); + $this->assertFalse($this->form_validation->integer('--1')); } public function test_rule_decimal() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('decimal', '')); - $this->assertTrue($this->run_rule('decimal', '1.0')); - $this->assertTrue($this->run_rule('decimal', '0.98')); - - $this->assertFalse($this->run_rule('decimal', '1.0a')); - $this->assertFalse($this->run_rule('decimal', '-i')); + { + $this->assertTrue($this->form_validation->decimal('1.0')); + $this->assertTrue($this->form_validation->decimal('-0.98')); + + $this->assertFalse($this->form_validation->decimal('0')); + $this->assertFalse($this->form_validation->decimal('1.0a')); + $this->assertFalse($this->form_validation->decimal('-i')); + $this->assertFalse($this->form_validation->decimal('--1')); } public function test_rule_is_natural() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('is_natural', '')); - $this->assertTrue($this->run_rule('is_natural', '0')); - $this->assertTrue($this->run_rule('is_natural', '12')); + { + $this->assertTrue($this->form_validation->is_natural('0')); + $this->assertTrue($this->form_validation->is_natural('12')); - $this->assertFalse($this->run_rule('is_natural', '42a')); + $this->assertFalse($this->form_validation->is_natural('42a')); + $this->assertFalse($this->form_validation->is_natural('-1')); } public function test_rule_is_natural_no_zero() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('is_natural_no_zero', '')); - $this->assertTrue($this->run_rule('is_natural_no_zero', '42')); + { + $this->assertTrue($this->form_validation->is_natural_no_zero('42')); - $this->assertFalse($this->run_rule('is_natural_no_zero', '0')); - $this->assertFalse($this->run_rule('is_natural_no_zero', '42a')); + $this->assertFalse($this->form_validation->is_natural_no_zero('0')); + $this->assertFalse($this->form_validation->is_natural_no_zero('42a')); + $this->assertFalse($this->form_validation->is_natural_no_zero('-1')); } public function test_rule_valid_url() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_url', '')); - $this->assertTrue($this->run_rule('valid_url', 'www.codeigniter.com')); - $this->assertTrue($this->run_rule('valid_url', 'http://codeigniter.eu')); - - $this->assertFalse($this->run_rule('valid_url', 'code igniter')); + { + $this->assertTrue($this->form_validation->valid_url('www.codeigniter.com')); + $this->assertTrue($this->form_validation->valid_url('http://codeigniter.eu')); + + $this->assertFalse($this->form_validation->valid_url('htt://www.codeIgniter.com')); + $this->assertFalse($this->form_validation->valid_url('')); + $this->assertFalse($this->form_validation->valid_url('code igniter')); } public function test_rule_valid_email() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_email', '')); - $this->assertTrue($this->run_rule('valid_email', 'email@sample.com')); + { + $this->assertTrue($this->form_validation->valid_email('email@sample.com')); - $this->assertFalse($this->run_rule('valid_email', '@sample.com')); + $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com')); } public function test_rule_valid_emails() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_emails', '')); - $this->assertTrue($this->run_rule('valid_emails', '1@sample.com,2@sample.com')); + { + $this->assertTrue($this->form_validation->valid_emails('1@sample.com,2@sample.com')); + $this->assertTrue($this->form_validation->valid_emails('email@sample.com')); - $this->assertFalse($this->run_rule('valid_emails', '@sample.com,2@sample.com,validemail@email.ca')); + $this->assertFalse($this->form_validation->valid_emails('valid_email', '@sample.com')); + $this->assertFalse($this->form_validation->valid_emails('@sample.com,2@sample.com,validemail@email.ca')); } public function test_rule_valid_ip() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_ip', '')); - $this->assertTrue($this->run_rule('valid_ip', '127.0.0.1')); - $this->assertTrue($this->run_rule('valid_ip[ipv4]', '127.0.0.1')); - $this->assertTrue($this->run_rule('valid_ip', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); - $this->assertTrue($this->run_rule('valid_ip[ipv6]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); - - $this->assertFalse($this->run_rule('valid_ip[ipv4]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334')); - $this->assertFalse($this->run_rule('valid_ip[ipv6]', '127.0.0.1')); - $this->assertFalse($this->run_rule('valid_ip', 'H001:0db8:85a3:0000:0000:8a2e:0370:7334')); - $this->assertFalse($this->run_rule('valid_ip', '127.0.0.259')); + { + $this->assertTrue($this->form_validation->valid_ip('127.0.0.1')); + $this->assertTrue($this->form_validation->valid_ip('127.0.0.1', 'ipv4')); + $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); + $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv6')); + + $this->assertFalse($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv4')); + $this->assertFalse($this->form_validation->valid_ip('127.0.0.1', 'ipv6')); + $this->assertFalse($this->form_validation->valid_ip('H001:0db8:85a3:0000:0000:8a2e:0370:7334')); + $this->assertFalse($this->form_validation->valid_ip('127.0.0.259')); } public function test_rule_valid_base64() - { - // Empty input should pass any rule unless required is also specified - $this->assertTrue($this->run_rule('valid_base64', '')); - $this->assertTrue($this->run_rule('valid_base64', base64_encode('string'))); + { + $this->assertTrue($this->form_validation->valid_base64(base64_encode('string'))); - $this->assertFalse($this->run_rule('valid_base64', "FA08GG")); + $this->assertFalse($this->form_validation->valid_base64('FA08GG')); + } + + public function test_set_data() + { + // Reset test environment + $_POST = array(); + $this->form_validation->reset_validation(); + + $data = array('field' => 'some_data'); + $this->form_validation->set_data($data); + $this->form_validation->set_rules('field', 'label', 'required'); + $this->assertTrue($this->form_validation->run()); + + // Test with empty array + $_POST = array(); + $data = array(); + $this->form_validation->reset_validation(); + $this->form_validation->set_data($data); + $this->form_validation->set_rules('field', 'label', 'required'); + $this->assertFalse($this->form_validation->run()); + } + + public function test_set_message() + { + // Reset test environment + $_POST = array(); + $this->form_validation->reset_validation(); + $err_message = 'What a terrible error!'; + $rules = array( + array( + 'field' => 'req_field', + 'label' => 'label', + 'rules' => 'required' + ) + ); + $errorless_data = array('req_field' => 'some text'); + $erroneous_data = array('req_field' => ''); + + $this->form_validation->set_message('required', $err_message); + $this->form_validation->set_data($erroneous_data); + $this->form_validation->set_rules($rules); + $this->form_validation->run(); + $this->assertEquals('

'.$err_message.'

', $this->form_validation->error('req_field')); + + $this->form_validation->reset_validation(); + $this->form_validation->set_message('required', $err_message); + $this->form_validation->set_data($errorless_data); + $this->form_validation->set_rules($rules); + $this->form_validation->run(); + $this->assertEquals('', $this->form_validation->error('req_field')); } public function run_rule($rule, $test_value, $reset_post = TRUE) -- cgit v1.2.3-24-g4f1b From cf77671e18cc40a38f86e216c294e719c73f08b9 Mon Sep 17 00:00:00 2001 From: David Woods Date: Sun, 22 Mar 2015 15:55:16 -0700 Subject: Corrected invalid matches, differs, and set_data test cases Signed-off-by: David Woods --- .../codeigniter/libraries/Form_validation_test.php | 95 ++++++++++++++-------- 1 file changed, 61 insertions(+), 34 deletions(-) (limited to 'tests/codeigniter/libraries/Form_validation_test.php') diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index 088a0ce3e..ccdc64785 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -39,38 +39,63 @@ class Form_validation_test extends CI_TestCase { public function test_rule_required() { - $this->assertTrue($this->run_rule('required', ' someValue')); + $rules = array(array('field' => 'foo', 'label' => 'foo_label', 'rules' => 'required')); + $this->assertTrue($this->run_rules($rules, array('foo' => 'bar'))); - $this->assertFalse($this->run_rule('required', '')); - $this->assertFalse($this->run_rule('required', ' ')); + $this->assertFalse($this->run_rules($rules, array('foo' => ''))); + $this->assertFalse($this->run_rules($rules, array('foo' => ' '))); } public function test_rule_matches() - { - // Empty input should pass any rule unless required is also specified - $_POST['to_match'] = 'sample'; - $this->assertTrue($this->run_rule('matches[to_match]', '', FALSE)); - $_POST['to_match'] = 'sample'; - $this->assertTrue($this->run_rule('matches[to_match]', 'sample', FALSE)); - - $_POST['to_match'] = 'sample'; - $this->assertFalse($this->run_rule('matches[to_match]', 'Sample', FALSE)); - $_POST['to_match'] = 'sample'; - $this->assertFalse($this->run_rule('matches[to_match]', ' sample', FALSE)); + { + $rules = array( + array('field' => 'foo', 'label' => 'label', 'rules' => 'required'), + array('field' => 'bar', 'label' => 'label2', 'rules' => 'matches[foo]')); + $values_base = array('foo' => 'sample'); + + $this->assertTrue($this->run_rules( + $rules, + array_merge($values_base, array('bar' => '')) + )); + $this->assertTrue($this->run_rules( + $rules, + array_merge($values_base, array('bar' => 'sample')) + )); + + $this->assertFalse($this->run_rules( + $rules, + array_merge($values_base, array('bar' => 'Sample')) + )); + $this->assertFalse($this->run_rules( + $rules, + array_merge($values_base, array('bar' => ' sample')) + )); } public function test_rule_differs() - { - // Empty input should pass any rule unless required is also specified - $_POST['to_differ'] = 'sample'; - $this->assertTrue($this->run_rule('differs[to_differ]', '', FALSE)); - $_POST['to_differ'] = 'sample'; - $this->assertTrue($this->run_rule('differs[to_differ]', 'Sample', FALSE)); - $_POST['to_differ'] = 'sample'; - $this->assertTrue($this->run_rule('differs[to_differ]', ' sample', FALSE)); - - $_POST['to_differ'] = 'sample'; - $this->assertFalse($this->run_rule('differs[to_differ]', 'sample', FALSE)); + { + $rules = array( + array('field' => 'foo', 'label' => 'label', 'rules' => 'required'), + array('field' => 'bar', 'label' => 'label2', 'rules' => 'differs[foo]')); + $values_base = array('foo' => 'sample'); + + $this->assertTrue($this->run_rules( + $rules, + array_merge($values_base, array('bar' => 'does_not_match')) + )); + $this->assertTrue($this->run_rules( + $rules, + array_merge($values_base, array('bar' => 'Sample')) + )); + $this->assertTrue($this->run_rules( + $rules, + array_merge($values_base, array('bar' => ' sample')) + )); + + $this->assertFalse($this->run_rules( + $rules, + array_merge($values_base, array('bar' => 'sample')) + )); } public function test_rule_min_length() @@ -284,7 +309,6 @@ class Form_validation_test extends CI_TestCase { // Reset test environment $_POST = array(); $this->form_validation->reset_validation(); - $data = array('field' => 'some_data'); $this->form_validation->set_data($data); $this->form_validation->set_rules('field', 'label', 'required'); @@ -292,11 +316,13 @@ class Form_validation_test extends CI_TestCase { // Test with empty array $_POST = array(); - $data = array(); $this->form_validation->reset_validation(); + $data = array('field' => 'some_data'); $this->form_validation->set_data($data); + // This should do nothing. Old data will still be used + $this->form_validation->set_data(array()); $this->form_validation->set_rules('field', 'label', 'required'); - $this->assertFalse($this->form_validation->run()); + $this->assertTrue($this->form_validation->run()); } public function test_set_message() @@ -329,17 +355,18 @@ class Form_validation_test extends CI_TestCase { $this->assertEquals('', $this->form_validation->error('req_field')); } - public function run_rule($rule, $test_value, $reset_post = TRUE) + public function run_rules($rules, $values) { // $this->markTestSkipped('Not designed to be a unit test'); $this->form_validation->reset_validation(); - if ($reset_post === TRUE) + $_POST = array(); + + $this->form_validation->set_rules($rules); + foreach ($values as $field => $value) { - $_POST = array(); + $_POST[$field] = $value; } - - $this->form_validation->set_rules('field', 'name', $rule); - $_POST['field'] = $test_value; + return $this->form_validation->run(); } -- cgit v1.2.3-24-g4f1b