summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDavid Woods <d.woods92@gmail.com>2015-03-17 18:52:01 +0100
committerDavid Woods <d.woods92@gmail.com>2015-03-17 18:52:01 +0100
commitc7029e2e5f479a541d951d6f6ebf1b33a82a1632 (patch)
treede3b917beb16788b5ead4b4278f66bb422efeb84 /tests
parentdc1ae6bd92bc0f2c3ee5ce812ec60e580c72501d (diff)
Reformatted unit tests for easier debugging
Signed-off-by: David Woods <d.woods92@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/codeigniter/libraries/Form_validation_test.php379
1 files changed, 265 insertions, 114 deletions
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();
+ }
}