summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/libraries/Form_validation_test.php
diff options
context:
space:
mode:
authorDavid Woods <d.woods92@gmail.com>2015-03-28 06:41:58 +0100
committerDavid Woods <d.woods92@gmail.com>2015-03-28 06:41:58 +0100
commit5bc1dc5257577f2afd562f5b039ad95d57befdb1 (patch)
tree9e2774ac78d898870b6d820047520be052770853 /tests/codeigniter/libraries/Form_validation_test.php
parent7abc08acbeec7437b72d44e5e1a3500f7f6ac766 (diff)
Added more units tests for Form_validation
Up to 65% coverage Signed-off-by: David Woods <d.woods92@gmail.com>
Diffstat (limited to 'tests/codeigniter/libraries/Form_validation_test.php')
-rw-r--r--tests/codeigniter/libraries/Form_validation_test.php110
1 files changed, 108 insertions, 2 deletions
diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php
index 1bbd1758b..19e7beeee 100644
--- a/tests/codeigniter/libraries/Form_validation_test.php
+++ b/tests/codeigniter/libraries/Form_validation_test.php
@@ -248,7 +248,7 @@ class Form_validation_test extends CI_TestCase {
$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->form_validation->valid_emails('valid_email', '@sample.com'));
+ $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'));
}
@@ -313,7 +313,7 @@ class Form_validation_test extends CI_TestCase {
$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->assertEquals('<p>' . $err_message . '</p>', $this->form_validation->error('req_field'));
$this->form_validation->reset_validation();
$this->form_validation->set_message('required', $err_message);
@@ -323,6 +323,111 @@ class Form_validation_test extends CI_TestCase {
$this->assertEquals('', $this->form_validation->error('req_field'));
}
+ public function test_set_error_delimiters()
+ {
+ $this->form_validation->reset_validation();
+ $prefix = '<div class="error">';
+ $suffix = '</div>';
+ $this->form_validation->set_error_delimiters($prefix, $suffix);
+ $this->form_validation->set_rules('foo', 'label', 'required');
+ $_POST = array('foo' => '');
+ $this->form_validation->run();
+ $error_msg = $this->form_validation->error('foo');
+
+ $this->assertTrue(strrpos($error_msg, $prefix) === 0);
+ $this->assertTrue(strrpos($error_msg, $suffix, -strlen($suffix)) === (strlen($error_msg) - strlen($suffix)));
+ }
+
+ public function test_error_array()
+ {
+ $this->form_validation->reset_validation();
+ $error_message = 'What a terrible error!';
+ $this->form_validation->set_message('required', $error_message);
+ $this->form_validation->set_rules('foo', 'label', 'required');
+ $_POST = array('foo' => '');
+ $this->form_validation->run();
+ $this->assertEquals($error_message, $this->form_validation->error_array()['foo']);
+ }
+
+ public function test_error_string()
+ {
+ $this->form_validation->reset_validation();
+ $error_message = 'What a terrible error!';
+ $prefix_default = '<foo>';
+ $suffix_default = '</foo>';
+ $prefix_test = '<bar>';
+ $suffix_test = '</bar>';
+ $this->form_validation->set_error_delimiters($prefix_default, $suffix_default);
+ $this->form_validation->set_message('required', $error_message);
+ $this->form_validation->set_rules('foo', 'label', 'required');
+ $_POST = array('foo' => '');
+ $this->form_validation->run();
+
+ $this->assertEquals($prefix_default . $error_message . $suffix_default . "\n", $this->form_validation->error_string());
+ $this->assertEquals($prefix_test . $error_message . $suffix_default . "\n", $this->form_validation->error_string($prefix_test, ''));
+ $this->assertEquals($prefix_default . $error_message . $suffix_test . "\n", $this->form_validation->error_string('', $suffix_test));
+ $this->assertEquals($prefix_test . $error_message . $suffix_test . "\n", $this->form_validation->error_string($prefix_test, $suffix_test));
+
+ $this->form_validation->reset_validation();
+ $this->form_validation->set_rules('foo', 'label', 'required');
+ $_POST = array('foo' => 'bar');
+ $this->form_validation->run();
+ $this->assertEquals('', $this->form_validation->error_string());
+ }
+
+ public function test_run()
+ {
+ // form_validation->run() is tested in many of the other unit tests
+ // This test will only test run(group='') when group is not empty
+ $config = array(
+ 'pass' => array(
+ array(
+ 'field' => 'username',
+ 'label' => 'user',
+ 'rules' => 'alpha_numeric'
+ )
+ ),
+ 'fail' => array(
+ array(
+ 'field' => 'username',
+ 'label' => 'user',
+ 'rules' => 'alpha'
+ )
+ )
+ );
+ $_POST = array('username' => 'foo42');
+ $form_validation = new CI_Form_validation($config);
+ $this->assertTrue($form_validation->run('pass'));
+
+ $form_validation = new CI_Form_validation($config);
+ $this->assertFalse($form_validation->run('fail'));
+ }
+
+ public function test_has_rule()
+ {
+ $this->form_validation->reset_validation();
+ $this->form_validation->set_rules('foo', 'label', 'required');
+
+ $this->assertTrue($this->form_validation->has_rule('foo'));
+ $this->assertFalse($this->form_validation->has_rule('bar'));
+ }
+
+ public function test_set_value()
+ {
+ $this->form_validation->reset_validation();
+ $default = 'default';
+ $this->form_validation->set_rules('foo', 'label', 'required');
+ $this->form_validation->set_rules('bar[]', 'label', 'required');
+
+ // No post data yet: should return the default value provided
+ $this->assertEquals($default, $this->form_validation->set_value('foo', $default));
+ $_POST = array('foo' => 'foo', 'bar' => array('bar1', 'bar2'));
+ $this->form_validation->run();
+ $this->assertEquals('foo', $this->form_validation->set_value('foo', $default));
+ $this->assertEquals('bar1', $this->form_validation->set_value('bar[]', $default));
+ $this->assertEquals('bar2', $this->form_validation->set_value('bar[]', $default));
+ }
+
/**
* Run rules
*
@@ -342,4 +447,5 @@ class Form_validation_test extends CI_TestCase {
return $this->form_validation->run();
}
+
}