David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Form_validation_test extends CI_TestCase { |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 4 | |
| 5 | public function set_up() |
| 6 | { |
| 7 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
| 8 | |
| 9 | // Create a mock loader since load->helper() looks in the wrong directories for unit tests, |
| 10 | // We'll use CI_TestCase->helper() instead |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 11 | $loader = $this->getMock('CI_Loader', array('helper')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 12 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 13 | // Same applies for lang |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 14 | $lang = $this->getMock('CI_Lang', array('load')); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 15 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 16 | $this->ci_set_config('charset', 'UTF-8'); |
| 17 | $utf8 = new Mock_Core_Utf8(); |
| 18 | $security = new Mock_Core_Security(); |
| 19 | $input = new Mock_Core_Input($security, $utf8); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 20 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 21 | $this->ci_instance_var('lang', $lang); |
| 22 | $this->ci_instance_var('load', $loader); |
| 23 | $this->ci_instance_var('input', $input); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 24 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 25 | $this->lang('form_validation'); |
| 26 | $this->helper('form'); |
| 27 | |
| 28 | $this->form_validation = new CI_Form_validation(); |
| 29 | } |
| 30 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 31 | public function test_rule_required() |
| 32 | { |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 33 | $rules = array(array('field' => 'foo', 'label' => 'foo_label', 'rules' => 'required')); |
| 34 | $this->assertTrue($this->run_rules($rules, array('foo' => 'bar'))); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 35 | |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 36 | $this->assertFalse($this->run_rules($rules, array('foo' => ''))); |
| 37 | $this->assertFalse($this->run_rules($rules, array('foo' => ' '))); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | public function test_rule_matches() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 41 | { |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 42 | $rules = array( |
| 43 | array('field' => 'foo', 'label' => 'label', 'rules' => 'required'), |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 44 | array('field' => 'bar', 'label' => 'label2', 'rules' => 'matches[foo]') |
| 45 | ); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 46 | $values_base = array('foo' => 'sample'); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 47 | |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 48 | $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => '')))); |
| 49 | $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => 'sample')))); |
| 50 | |
| 51 | $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => 'Sample')))); |
| 52 | $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => ' sample')))); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public function test_rule_differs() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 56 | { |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 57 | $rules = array( |
| 58 | array('field' => 'foo', 'label' => 'label', 'rules' => 'required'), |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 59 | array('field' => 'bar', 'label' => 'label2', 'rules' => 'differs[foo]') |
| 60 | ); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 61 | $values_base = array('foo' => 'sample'); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 62 | |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 63 | $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => 'does_not_match')))); |
| 64 | $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => 'Sample')))); |
| 65 | $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => ' sample')))); |
| 66 | |
| 67 | $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => 'sample')))); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | public function test_rule_min_length() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 71 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 72 | $this->assertTrue($this->form_validation->min_length('12345', '5')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 73 | $this->assertTrue($this->form_validation->min_length('test', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 74 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 75 | $this->assertFalse($this->form_validation->min_length('123', '4')); |
| 76 | $this->assertFalse($this->form_validation->min_length('should_fail', 'A')); |
| 77 | $this->assertFalse($this->form_validation->min_length('', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | public function test_rule_max_length() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 81 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 82 | $this->assertTrue($this->form_validation->max_length('', '4')); |
| 83 | $this->assertTrue($this->form_validation->max_length('1234', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 84 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 85 | $this->assertFalse($this->form_validation->max_length('12345', '4')); |
| 86 | $this->assertFalse($this->form_validation->max_length('should_fail', 'A')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | public function test_rule_exact_length() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 90 | { |
| 91 | $this->assertTrue($this->form_validation->exact_length('1234', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 92 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 93 | $this->assertFalse($this->form_validation->exact_length('', '3')); |
| 94 | $this->assertFalse($this->form_validation->exact_length('12345', '4')); |
| 95 | $this->assertFalse($this->form_validation->exact_length('123', '4')); |
| 96 | $this->assertFalse($this->form_validation->exact_length('should_fail', 'A')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | public function test_rule_greater_than() |
| 100 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 101 | $this->assertTrue($this->form_validation->greater_than('-10', '-11')); |
| 102 | $this->assertTrue($this->form_validation->greater_than('10', '9')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 103 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 104 | $this->assertFalse($this->form_validation->greater_than('10', '10')); |
| 105 | $this->assertFalse($this->form_validation->greater_than('10', 'a')); |
| 106 | $this->assertFalse($this->form_validation->greater_than('10a', '10')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public function test_rule_greater_than_equal_to() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 110 | { |
| 111 | $this->assertTrue($this->form_validation->greater_than_equal_to('0', '0')); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 112 | $this->assertTrue($this->form_validation->greater_than_equal_to('1', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 113 | |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 114 | $this->assertFalse($this->form_validation->greater_than_equal_to('-1', '0')); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 115 | $this->assertFalse($this->form_validation->greater_than_equal_to('10a', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | public function test_rule_less_than() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 119 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 120 | $this->assertTrue($this->form_validation->less_than('4', '5')); |
| 121 | $this->assertTrue($this->form_validation->less_than('-1', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 122 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 123 | $this->assertFalse($this->form_validation->less_than('4', '4')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 124 | $this->assertFalse($this->form_validation->less_than('10a', '5')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public function test_rule_less_than_equal_to() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 128 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 129 | $this->assertTrue($this->form_validation->less_than_equal_to('-1', '0')); |
| 130 | $this->assertTrue($this->form_validation->less_than_equal_to('-1', '-1')); |
| 131 | $this->assertTrue($this->form_validation->less_than_equal_to('4', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 132 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 133 | $this->assertFalse($this->form_validation->less_than_equal_to('0', '-1')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 134 | $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public function test_rule_in_list() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 138 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 139 | $this->assertTrue($this->form_validation->in_list('red', 'red,Blue,123')); |
| 140 | $this->assertTrue($this->form_validation->in_list('Blue', 'red,Blue,123')); |
| 141 | $this->assertTrue($this->form_validation->in_list('123', 'red,Blue,123')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 142 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 143 | $this->assertFalse($this->form_validation->in_list('Red', 'red,Blue,123')); |
| 144 | $this->assertFalse($this->form_validation->in_list(' red', 'red,Blue,123')); |
| 145 | $this->assertFalse($this->form_validation->in_list('1234', 'red,Blue,123')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | public function test_rule_alpha() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 149 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 150 | $this->assertTrue($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 151 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 152 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); |
| 153 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); |
| 154 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public function test_rule_alpha_numeric() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 158 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 159 | $this->assertTrue($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 160 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 161 | $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); |
| 162 | $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | public function test_rule_alpha_numeric_spaces() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 166 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 167 | $this->assertTrue($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 168 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 169 | $this->assertFalse($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | public function test_rule_alpha_dash() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 173 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 174 | $this->assertTrue($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 175 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 176 | $this->assertFalse($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | public function test_rule_numeric() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 180 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 181 | $this->assertTrue($this->form_validation->numeric('0')); |
| 182 | $this->assertTrue($this->form_validation->numeric('12314')); |
| 183 | $this->assertTrue($this->form_validation->numeric('-42')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 184 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 185 | $this->assertFalse($this->form_validation->numeric('123a')); |
| 186 | $this->assertFalse($this->form_validation->numeric('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | public function test_rule_integer() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 190 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 191 | $this->assertTrue($this->form_validation->integer('0')); |
| 192 | $this->assertTrue($this->form_validation->integer('42')); |
| 193 | $this->assertTrue($this->form_validation->integer('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 194 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 195 | $this->assertFalse($this->form_validation->integer('124a')); |
| 196 | $this->assertFalse($this->form_validation->integer('1.9')); |
| 197 | $this->assertFalse($this->form_validation->integer('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | public function test_rule_decimal() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 201 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 202 | $this->assertTrue($this->form_validation->decimal('1.0')); |
| 203 | $this->assertTrue($this->form_validation->decimal('-0.98')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 204 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 205 | $this->assertFalse($this->form_validation->decimal('0')); |
| 206 | $this->assertFalse($this->form_validation->decimal('1.0a')); |
| 207 | $this->assertFalse($this->form_validation->decimal('-i')); |
| 208 | $this->assertFalse($this->form_validation->decimal('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | public function test_rule_is_natural() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 212 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 213 | $this->assertTrue($this->form_validation->is_natural('0')); |
| 214 | $this->assertTrue($this->form_validation->is_natural('12')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 215 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 216 | $this->assertFalse($this->form_validation->is_natural('42a')); |
| 217 | $this->assertFalse($this->form_validation->is_natural('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | public function test_rule_is_natural_no_zero() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 221 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 222 | $this->assertTrue($this->form_validation->is_natural_no_zero('42')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 223 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 224 | $this->assertFalse($this->form_validation->is_natural_no_zero('0')); |
| 225 | $this->assertFalse($this->form_validation->is_natural_no_zero('42a')); |
| 226 | $this->assertFalse($this->form_validation->is_natural_no_zero('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | public function test_rule_valid_url() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 230 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 231 | $this->assertTrue($this->form_validation->valid_url('www.codeigniter.com')); |
| 232 | $this->assertTrue($this->form_validation->valid_url('http://codeigniter.eu')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 233 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 234 | $this->assertFalse($this->form_validation->valid_url('htt://www.codeIgniter.com')); |
| 235 | $this->assertFalse($this->form_validation->valid_url('')); |
| 236 | $this->assertFalse($this->form_validation->valid_url('code igniter')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | public function test_rule_valid_email() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 240 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 241 | $this->assertTrue($this->form_validation->valid_email('email@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 242 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 243 | $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | public function test_rule_valid_emails() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 247 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 248 | $this->assertTrue($this->form_validation->valid_emails('1@sample.com,2@sample.com')); |
| 249 | $this->assertTrue($this->form_validation->valid_emails('email@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 250 | |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame^] | 251 | $this->assertFalse($this->form_validation->valid_emails('valid_email', '@sample.com')); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 252 | $this->assertFalse($this->form_validation->valid_emails('@sample.com,2@sample.com,validemail@email.ca')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | public function test_rule_valid_ip() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 256 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 257 | $this->assertTrue($this->form_validation->valid_ip('127.0.0.1')); |
| 258 | $this->assertTrue($this->form_validation->valid_ip('127.0.0.1', 'ipv4')); |
| 259 | $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); |
| 260 | $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv6')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 261 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 262 | $this->assertFalse($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv4')); |
| 263 | $this->assertFalse($this->form_validation->valid_ip('127.0.0.1', 'ipv6')); |
| 264 | $this->assertFalse($this->form_validation->valid_ip('H001:0db8:85a3:0000:0000:8a2e:0370:7334')); |
| 265 | $this->assertFalse($this->form_validation->valid_ip('127.0.0.259')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | public function test_rule_valid_base64() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 269 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 270 | $this->assertTrue($this->form_validation->valid_base64(base64_encode('string'))); |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 271 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 272 | $this->assertFalse($this->form_validation->valid_base64('FA08GG')); |
| 273 | } |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 274 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 275 | public function test_set_data() |
| 276 | { |
| 277 | // Reset test environment |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 278 | $_POST = array(); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 279 | $this->form_validation->reset_validation(); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 280 | $data = array('field' => 'some_data'); |
| 281 | $this->form_validation->set_data($data); |
| 282 | $this->form_validation->set_rules('field', 'label', 'required'); |
| 283 | $this->assertTrue($this->form_validation->run()); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 284 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 285 | // Test with empty array |
| 286 | $_POST = array(); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 287 | $this->form_validation->reset_validation(); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 288 | $data = array('field' => 'some_data'); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 289 | $this->form_validation->set_data($data); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 290 | // This should do nothing. Old data will still be used |
| 291 | $this->form_validation->set_data(array()); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 292 | $this->form_validation->set_rules('field', 'label', 'required'); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 293 | $this->assertTrue($this->form_validation->run()); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 294 | } |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 295 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 296 | public function test_set_message() |
| 297 | { |
| 298 | // Reset test environment |
| 299 | $_POST = array(); |
| 300 | $this->form_validation->reset_validation(); |
| 301 | $err_message = 'What a terrible error!'; |
| 302 | $rules = array( |
| 303 | array( |
| 304 | 'field' => 'req_field', |
| 305 | 'label' => 'label', |
| 306 | 'rules' => 'required' |
| 307 | ) |
| 308 | ); |
| 309 | $errorless_data = array('req_field' => 'some text'); |
| 310 | $erroneous_data = array('req_field' => ''); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 311 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 312 | $this->form_validation->set_message('required', $err_message); |
| 313 | $this->form_validation->set_data($erroneous_data); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 314 | $this->form_validation->set_rules($rules); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 315 | $this->form_validation->run(); |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame^] | 316 | $this->assertEquals('<p>' . $err_message . '</p>', $this->form_validation->error('req_field')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 317 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 318 | $this->form_validation->reset_validation(); |
| 319 | $this->form_validation->set_message('required', $err_message); |
| 320 | $this->form_validation->set_data($errorless_data); |
| 321 | $this->form_validation->set_rules($rules); |
| 322 | $this->form_validation->run(); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 323 | $this->assertEquals('', $this->form_validation->error('req_field')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 324 | } |
| 325 | |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame^] | 326 | public function test_set_error_delimiters() |
| 327 | { |
| 328 | $this->form_validation->reset_validation(); |
| 329 | $prefix = '<div class="error">'; |
| 330 | $suffix = '</div>'; |
| 331 | $this->form_validation->set_error_delimiters($prefix, $suffix); |
| 332 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 333 | $_POST = array('foo' => ''); |
| 334 | $this->form_validation->run(); |
| 335 | $error_msg = $this->form_validation->error('foo'); |
| 336 | |
| 337 | $this->assertTrue(strrpos($error_msg, $prefix) === 0); |
| 338 | $this->assertTrue(strrpos($error_msg, $suffix, -strlen($suffix)) === (strlen($error_msg) - strlen($suffix))); |
| 339 | } |
| 340 | |
| 341 | public function test_error_array() |
| 342 | { |
| 343 | $this->form_validation->reset_validation(); |
| 344 | $error_message = 'What a terrible error!'; |
| 345 | $this->form_validation->set_message('required', $error_message); |
| 346 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 347 | $_POST = array('foo' => ''); |
| 348 | $this->form_validation->run(); |
| 349 | $this->assertEquals($error_message, $this->form_validation->error_array()['foo']); |
| 350 | } |
| 351 | |
| 352 | public function test_error_string() |
| 353 | { |
| 354 | $this->form_validation->reset_validation(); |
| 355 | $error_message = 'What a terrible error!'; |
| 356 | $prefix_default = '<foo>'; |
| 357 | $suffix_default = '</foo>'; |
| 358 | $prefix_test = '<bar>'; |
| 359 | $suffix_test = '</bar>'; |
| 360 | $this->form_validation->set_error_delimiters($prefix_default, $suffix_default); |
| 361 | $this->form_validation->set_message('required', $error_message); |
| 362 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 363 | $_POST = array('foo' => ''); |
| 364 | $this->form_validation->run(); |
| 365 | |
| 366 | $this->assertEquals($prefix_default . $error_message . $suffix_default . "\n", $this->form_validation->error_string()); |
| 367 | $this->assertEquals($prefix_test . $error_message . $suffix_default . "\n", $this->form_validation->error_string($prefix_test, '')); |
| 368 | $this->assertEquals($prefix_default . $error_message . $suffix_test . "\n", $this->form_validation->error_string('', $suffix_test)); |
| 369 | $this->assertEquals($prefix_test . $error_message . $suffix_test . "\n", $this->form_validation->error_string($prefix_test, $suffix_test)); |
| 370 | |
| 371 | $this->form_validation->reset_validation(); |
| 372 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 373 | $_POST = array('foo' => 'bar'); |
| 374 | $this->form_validation->run(); |
| 375 | $this->assertEquals('', $this->form_validation->error_string()); |
| 376 | } |
| 377 | |
| 378 | public function test_run() |
| 379 | { |
| 380 | // form_validation->run() is tested in many of the other unit tests |
| 381 | // This test will only test run(group='') when group is not empty |
| 382 | $config = array( |
| 383 | 'pass' => array( |
| 384 | array( |
| 385 | 'field' => 'username', |
| 386 | 'label' => 'user', |
| 387 | 'rules' => 'alpha_numeric' |
| 388 | ) |
| 389 | ), |
| 390 | 'fail' => array( |
| 391 | array( |
| 392 | 'field' => 'username', |
| 393 | 'label' => 'user', |
| 394 | 'rules' => 'alpha' |
| 395 | ) |
| 396 | ) |
| 397 | ); |
| 398 | $_POST = array('username' => 'foo42'); |
| 399 | $form_validation = new CI_Form_validation($config); |
| 400 | $this->assertTrue($form_validation->run('pass')); |
| 401 | |
| 402 | $form_validation = new CI_Form_validation($config); |
| 403 | $this->assertFalse($form_validation->run('fail')); |
| 404 | } |
| 405 | |
| 406 | public function test_has_rule() |
| 407 | { |
| 408 | $this->form_validation->reset_validation(); |
| 409 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 410 | |
| 411 | $this->assertTrue($this->form_validation->has_rule('foo')); |
| 412 | $this->assertFalse($this->form_validation->has_rule('bar')); |
| 413 | } |
| 414 | |
| 415 | public function test_set_value() |
| 416 | { |
| 417 | $this->form_validation->reset_validation(); |
| 418 | $default = 'default'; |
| 419 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 420 | $this->form_validation->set_rules('bar[]', 'label', 'required'); |
| 421 | |
| 422 | // No post data yet: should return the default value provided |
| 423 | $this->assertEquals($default, $this->form_validation->set_value('foo', $default)); |
| 424 | $_POST = array('foo' => 'foo', 'bar' => array('bar1', 'bar2')); |
| 425 | $this->form_validation->run(); |
| 426 | $this->assertEquals('foo', $this->form_validation->set_value('foo', $default)); |
| 427 | $this->assertEquals('bar1', $this->form_validation->set_value('bar[]', $default)); |
| 428 | $this->assertEquals('bar2', $this->form_validation->set_value('bar[]', $default)); |
| 429 | } |
| 430 | |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 431 | /** |
| 432 | * Run rules |
| 433 | * |
| 434 | * Helper method to set rules and run them at once, not |
| 435 | * an actual test case. |
| 436 | */ |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 437 | public function run_rules($rules, $values) |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 438 | { |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 439 | $this->form_validation->reset_validation(); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 440 | $_POST = array(); |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 441 | |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 442 | $this->form_validation->set_rules($rules); |
| 443 | foreach ($values as $field => $value) |
| 444 | { |
| 445 | $_POST[$field] = $value; |
| 446 | } |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 447 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 448 | return $this->form_validation->run(); |
| 449 | } |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame^] | 450 | |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 451 | } |