summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter
diff options
context:
space:
mode:
authorDavid Woods <d.woods92@gmail.com>2015-03-21 06:32:24 +0100
committerDavid Woods <d.woods92@gmail.com>2015-03-21 06:32:24 +0100
commit317cad99f23cc80577039a5b709b39cad72690c5 (patch)
tree66da1a113983170f52bf4e89d218b3f216e68bd2 /tests/codeigniter
parent5b88473a716a3c69e72c67f1cfe26452db9e1172 (diff)
Changed scenario based tests to unit tests
Added tests for set_data() & set_message() Signed-off-by: David Woods <d.woods92@gmail.com>
Diffstat (limited to 'tests/codeigniter')
-rw-r--r--tests/codeigniter/libraries/Form_validation_test.php323
1 files changed, 175 insertions, 148 deletions
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('<p>'.$err_message.'</p>', $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)