diff options
Diffstat (limited to 'tests/codeigniter/libraries')
-rw-r--r-- | tests/codeigniter/libraries/Form_validation_test.php | 154 |
1 files changed, 63 insertions, 91 deletions
diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index ccdc64785..1bbd1758b 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -9,10 +9,7 @@ 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')); - // At current, CI_Form_Validation only calls load->helper("form") - // Assert this so that if that changes this fails fast - $loader->method('helper') - ->with($this->equalTo('form')); + // Same applies for lang $lang = $this->getMock('CI_Lang', array('load')); @@ -31,12 +28,6 @@ class Form_validation_test extends CI_TestCase { $this->form_validation = new CI_Form_validation(); } - public function test___construct() - { - $form_validation = new CI_Form_validation(); - $this->assertNotNull($form_validation); - } - public function test_rule_required() { $rules = array(array('field' => 'foo', 'label' => 'foo_label', 'rules' => 'required')); @@ -47,61 +38,39 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_matches() - { + { $rules = array( array('field' => 'foo', 'label' => 'label', 'rules' => 'required'), - array('field' => 'bar', 'label' => 'label2', 'rules' => 'matches[foo]')); + 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')) - )); + + $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() - { + { $rules = array( array('field' => 'foo', 'label' => 'label', 'rules' => 'required'), - array('field' => 'bar', 'label' => 'label2', 'rules' => 'differs[foo]')); + 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')) - )); + + $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() - { + { $this->assertTrue($this->form_validation->min_length('12345', '5')); - $this->assertTrue($this->form_validation->min_length('test', '0')); + $this->assertTrue($this->form_validation->min_length('test', '0')); $this->assertFalse($this->form_validation->min_length('123', '4')); $this->assertFalse($this->form_validation->min_length('should_fail', 'A')); @@ -109,7 +78,7 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_max_length() - { + { $this->assertTrue($this->form_validation->max_length('', '4')); $this->assertTrue($this->form_validation->max_length('1234', '4')); @@ -118,8 +87,8 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_exact_length() - { - $this->assertTrue($this->form_validation->exact_length('1234', '4')); + { + $this->assertTrue($this->form_validation->exact_length('1234', '4')); $this->assertFalse($this->form_validation->exact_length('', '3')); $this->assertFalse($this->form_validation->exact_length('12345', '4')); @@ -129,7 +98,6 @@ class Form_validation_test extends CI_TestCase { public function test_rule_greater_than() { - // Empty input should pass any rule unless required is also specified $this->assertTrue($this->form_validation->greater_than('-10', '-11')); $this->assertTrue($this->form_validation->greater_than('10', '9')); @@ -139,35 +107,35 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_greater_than_equal_to() - { - $this->assertTrue($this->form_validation->greater_than_equal_to('0', '0')); + { + $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->form_validation->greater_than_equal_to('-1', '0')); + $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() - { + { $this->assertTrue($this->form_validation->less_than('4', '5')); $this->assertTrue($this->form_validation->less_than('-1', '0')); $this->assertFalse($this->form_validation->less_than('4', '4')); - $this->assertFalse($this->form_validation->less_than('10a', '5')); + $this->assertFalse($this->form_validation->less_than('10a', '5')); } public function test_rule_less_than_equal_to() - { + { $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->form_validation->less_than_equal_to('0', '-1')); - $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0')); + $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0')); } public function test_rule_in_list() - { + { $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')); @@ -178,7 +146,7 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_alpha() - { + { $this->assertTrue($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); @@ -187,7 +155,7 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_alpha_numeric() - { + { $this->assertTrue($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); @@ -195,21 +163,21 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_alpha_numeric_spaces() - { + { $this->assertTrue($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); $this->assertFalse($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); } public function test_rule_alpha_dash() - { + { $this->assertTrue($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); $this->assertFalse($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); } public function test_rule_numeric() - { + { $this->assertTrue($this->form_validation->numeric('0')); $this->assertTrue($this->form_validation->numeric('12314')); $this->assertTrue($this->form_validation->numeric('-42')); @@ -219,7 +187,7 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_integer() - { + { $this->assertTrue($this->form_validation->integer('0')); $this->assertTrue($this->form_validation->integer('42')); $this->assertTrue($this->form_validation->integer('-1')); @@ -230,7 +198,7 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_decimal() - { + { $this->assertTrue($this->form_validation->decimal('1.0')); $this->assertTrue($this->form_validation->decimal('-0.98')); @@ -241,7 +209,7 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_is_natural() - { + { $this->assertTrue($this->form_validation->is_natural('0')); $this->assertTrue($this->form_validation->is_natural('12')); @@ -250,7 +218,7 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_is_natural_no_zero() - { + { $this->assertTrue($this->form_validation->is_natural_no_zero('42')); $this->assertFalse($this->form_validation->is_natural_no_zero('0')); @@ -259,24 +227,24 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_valid_url() - { + { $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() - { + { $this->assertTrue($this->form_validation->valid_email('email@sample.com')); $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com')); } public function test_rule_valid_emails() - { + { $this->assertTrue($this->form_validation->valid_emails('1@sample.com,2@sample.com')); $this->assertTrue($this->form_validation->valid_emails('email@sample.com')); @@ -285,7 +253,7 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_valid_ip() - { + { $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')); @@ -298,22 +266,22 @@ class Form_validation_test extends CI_TestCase { } public function test_rule_valid_base64() - { + { $this->assertTrue($this->form_validation->valid_base64(base64_encode('string'))); $this->assertFalse($this->form_validation->valid_base64('FA08GG')); } - + public function test_set_data() { // Reset test environment - $_POST = array(); + $_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(); $this->form_validation->reset_validation(); @@ -322,9 +290,9 @@ class Form_validation_test extends CI_TestCase { // 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->assertTrue($this->form_validation->run()); + $this->assertTrue($this->form_validation->run()); } - + public function test_set_message() { // Reset test environment @@ -340,24 +308,29 @@ class Form_validation_test extends CI_TestCase { ); $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->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')); + $this->assertEquals('', $this->form_validation->error('req_field')); } + /** + * Run rules + * + * Helper method to set rules and run them at once, not + * an actual test case. + */ public function run_rules($rules, $values) { -// $this->markTestSkipped('Not designed to be a unit test'); $this->form_validation->reset_validation(); $_POST = array(); @@ -366,8 +339,7 @@ class Form_validation_test extends CI_TestCase { { $_POST[$field] = $value; } - + return $this->form_validation->run(); } - } |