summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--system/libraries/Form_validation.php16
-rw-r--r--tests/codeigniter/libraries/Form_validation_test.php136
2 files changed, 139 insertions, 13 deletions
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php
index 05de59628..32ea4b1b4 100644
--- a/system/libraries/Form_validation.php
+++ b/system/libraries/Form_validation.php
@@ -1091,14 +1091,12 @@ class CI_Form_validation {
* Match one field to another
*
* @param string $str string to compare against
- * @param string $field
+ * @param string $param The string desired
* @return bool
*/
- public function matches($str, $field)
+ public function matches($str, $param)
{
- return isset($this->_field_data[$field], $this->_field_data[$field]['postdata'])
- ? ($str === $this->_field_data[$field]['postdata'])
- : FALSE;
+ return ($str === $param);
}
// --------------------------------------------------------------------
@@ -1107,12 +1105,12 @@ class CI_Form_validation {
* Differs from another field
*
* @param string
- * @param string field
+ * @param string param is the value provided in the form
* @return bool
*/
- public function differs($str, $field)
+ public function differs($str, $param)
{
- return ! (isset($this->_field_data[$field]) && $this->_field_data[$field]['postdata'] === $str);
+ return ($str !== $param);
}
// --------------------------------------------------------------------
@@ -1493,7 +1491,7 @@ class CI_Form_validation {
*/
public function valid_base64($str)
{
- return (base64_encode(base64_decode($str)) === $str);
+ return (base64_decode($str, true) !== false);
}
// --------------------------------------------------------------------
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();
+ }
+ }
+ }
+
}