blob: 0815300e6be4634198770bf5872a1aa58183d57a [file] [log] [blame]
David Woodsf3ac71e2015-03-16 20:00:21 -07001<?php
2
3class Form_validation_test extends CI_TestCase {
David Woods64af3bb2015-03-18 10:09:26 -07004
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
Andrey Andreev878e23f2016-08-10 15:15:49 +030011 $loader = $this->getMockBuilder('CI_Loader')->setMethods(array('helper'))->getMock();
Andrey Andreev3e2045b2015-03-26 12:34:38 +020012
David Woods64af3bb2015-03-18 10:09:26 -070013 // Same applies for lang
Andrey Andreev878e23f2016-08-10 15:15:49 +030014 $lang = $this->getMockBuilder('CI_Lang')->setMethods(array('load'))->getMock();
David Woodsdc1ae6b2015-03-16 23:36:54 -070015
David Woods64af3bb2015-03-18 10:09:26 -070016 $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 Woodsdc1ae6b2015-03-16 23:36:54 -070020
David Woods64af3bb2015-03-18 10:09:26 -070021 $this->ci_instance_var('lang', $lang);
22 $this->ci_instance_var('load', $loader);
23 $this->ci_instance_var('input', $input);
David Woodsdc1ae6b2015-03-16 23:36:54 -070024
David Woods64af3bb2015-03-18 10:09:26 -070025 $this->lang('form_validation');
26 $this->helper('form');
27
28 $this->form_validation = new CI_Form_validation();
29 }
30
Andrey Andreev7243d0b2016-03-12 11:40:34 +020031 public function test_empty_array_input()
32 {
33 $this->assertFalse(
34 $this->run_rules(
35 array(array('field' => 'foo', 'label' => 'Foo Label', 'rules' => 'required')),
36 array('foo' => array())
37 )
38 );
39 }
40
David Woods64af3bb2015-03-18 10:09:26 -070041 public function test_rule_required()
42 {
Andrey Andreev9e78be02016-05-25 10:54:36 +030043 $rules = array(array('field' => 'foo', 'label' => 'Foo', 'rules' => 'is_numeric'));
David Woods64af3bb2015-03-18 10:09:26 -070044
Andrey Andreev9e78be02016-05-25 10:54:36 +030045 // Empty, not required
46 $this->assertTrue($this->run_rules($rules, array('foo' => '')));
47
48
49 // Not required, but also not empty
50 $this->assertTrue($this->run_rules($rules, array('foo' => '123')));
51 $this->assertFalse($this->run_rules($rules, array('foo' => 'bar')));
52
53 // Required variations
54 $rules[0]['rules'] .= '|required';
55 $this->assertTrue($this->run_rules($rules, array('foo' => '123')));
David Woodscf776712015-03-22 15:55:16 -070056 $this->assertFalse($this->run_rules($rules, array('foo' => '')));
57 $this->assertFalse($this->run_rules($rules, array('foo' => ' ')));
Andrey Andreev9e78be02016-05-25 10:54:36 +030058 $this->assertFalse($this->run_rules($rules, array('foo' => 'bar')));
David Woods64af3bb2015-03-18 10:09:26 -070059 }
60
61 public function test_rule_matches()
Andrey Andreev3e2045b2015-03-26 12:34:38 +020062 {
David Woodscf776712015-03-22 15:55:16 -070063 $rules = array(
64 array('field' => 'foo', 'label' => 'label', 'rules' => 'required'),
Andrey Andreev3e2045b2015-03-26 12:34:38 +020065 array('field' => 'bar', 'label' => 'label2', 'rules' => 'matches[foo]')
66 );
David Woodscf776712015-03-22 15:55:16 -070067 $values_base = array('foo' => 'sample');
David Woods64af3bb2015-03-18 10:09:26 -070068
Andrey Andreev3e2045b2015-03-26 12:34:38 +020069 $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => 'sample'))));
70
Andrey Andreev0fae6252016-05-17 13:46:55 +030071 $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => ''))));
Andrey Andreev3e2045b2015-03-26 12:34:38 +020072 $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => 'Sample'))));
73 $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => ' sample'))));
David Woods64af3bb2015-03-18 10:09:26 -070074 }
75
76 public function test_rule_differs()
Andrey Andreev3e2045b2015-03-26 12:34:38 +020077 {
David Woodscf776712015-03-22 15:55:16 -070078 $rules = array(
79 array('field' => 'foo', 'label' => 'label', 'rules' => 'required'),
Andrey Andreev3e2045b2015-03-26 12:34:38 +020080 array('field' => 'bar', 'label' => 'label2', 'rules' => 'differs[foo]')
81 );
David Woodscf776712015-03-22 15:55:16 -070082 $values_base = array('foo' => 'sample');
David Woods64af3bb2015-03-18 10:09:26 -070083
Andrey Andreev3e2045b2015-03-26 12:34:38 +020084 $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => 'does_not_match'))));
85 $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => 'Sample'))));
86 $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => ' sample'))));
87
88 $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => 'sample'))));
David Woods64af3bb2015-03-18 10:09:26 -070089 }
90
91 public function test_rule_min_length()
Andrey Andreev3e2045b2015-03-26 12:34:38 +020092 {
David Woods317cad92015-03-20 22:32:24 -070093 $this->assertTrue($this->form_validation->min_length('12345', '5'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +020094 $this->assertTrue($this->form_validation->min_length('test', '0'));
David Woods64af3bb2015-03-18 10:09:26 -070095
David Woods317cad92015-03-20 22:32:24 -070096 $this->assertFalse($this->form_validation->min_length('123', '4'));
97 $this->assertFalse($this->form_validation->min_length('should_fail', 'A'));
98 $this->assertFalse($this->form_validation->min_length('', '4'));
David Woods64af3bb2015-03-18 10:09:26 -070099 }
100
101 public function test_rule_max_length()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200102 {
David Woods317cad92015-03-20 22:32:24 -0700103 $this->assertTrue($this->form_validation->max_length('', '4'));
104 $this->assertTrue($this->form_validation->max_length('1234', '4'));
David Woods64af3bb2015-03-18 10:09:26 -0700105
David Woods317cad92015-03-20 22:32:24 -0700106 $this->assertFalse($this->form_validation->max_length('12345', '4'));
107 $this->assertFalse($this->form_validation->max_length('should_fail', 'A'));
David Woods64af3bb2015-03-18 10:09:26 -0700108 }
109
110 public function test_rule_exact_length()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200111 {
112 $this->assertTrue($this->form_validation->exact_length('1234', '4'));
David Woods64af3bb2015-03-18 10:09:26 -0700113
David Woods317cad92015-03-20 22:32:24 -0700114 $this->assertFalse($this->form_validation->exact_length('', '3'));
115 $this->assertFalse($this->form_validation->exact_length('12345', '4'));
116 $this->assertFalse($this->form_validation->exact_length('123', '4'));
117 $this->assertFalse($this->form_validation->exact_length('should_fail', 'A'));
David Woods64af3bb2015-03-18 10:09:26 -0700118 }
119
120 public function test_rule_greater_than()
121 {
David Woods317cad92015-03-20 22:32:24 -0700122 $this->assertTrue($this->form_validation->greater_than('-10', '-11'));
123 $this->assertTrue($this->form_validation->greater_than('10', '9'));
David Woods64af3bb2015-03-18 10:09:26 -0700124
David Woods317cad92015-03-20 22:32:24 -0700125 $this->assertFalse($this->form_validation->greater_than('10', '10'));
126 $this->assertFalse($this->form_validation->greater_than('10', 'a'));
127 $this->assertFalse($this->form_validation->greater_than('10a', '10'));
David Woods64af3bb2015-03-18 10:09:26 -0700128 }
129
130 public function test_rule_greater_than_equal_to()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200131 {
132 $this->assertTrue($this->form_validation->greater_than_equal_to('0', '0'));
David Woods317cad92015-03-20 22:32:24 -0700133 $this->assertTrue($this->form_validation->greater_than_equal_to('1', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700134
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200135 $this->assertFalse($this->form_validation->greater_than_equal_to('-1', '0'));
David Woods317cad92015-03-20 22:32:24 -0700136 $this->assertFalse($this->form_validation->greater_than_equal_to('10a', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700137 }
138
139 public function test_rule_less_than()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200140 {
David Woods317cad92015-03-20 22:32:24 -0700141 $this->assertTrue($this->form_validation->less_than('4', '5'));
142 $this->assertTrue($this->form_validation->less_than('-1', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700143
David Woods317cad92015-03-20 22:32:24 -0700144 $this->assertFalse($this->form_validation->less_than('4', '4'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200145 $this->assertFalse($this->form_validation->less_than('10a', '5'));
David Woods64af3bb2015-03-18 10:09:26 -0700146 }
147
148 public function test_rule_less_than_equal_to()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200149 {
David Woods317cad92015-03-20 22:32:24 -0700150 $this->assertTrue($this->form_validation->less_than_equal_to('-1', '0'));
151 $this->assertTrue($this->form_validation->less_than_equal_to('-1', '-1'));
152 $this->assertTrue($this->form_validation->less_than_equal_to('4', '4'));
David Woods64af3bb2015-03-18 10:09:26 -0700153
David Woods317cad92015-03-20 22:32:24 -0700154 $this->assertFalse($this->form_validation->less_than_equal_to('0', '-1'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200155 $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700156 }
157
158 public function test_rule_in_list()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200159 {
David Woods317cad92015-03-20 22:32:24 -0700160 $this->assertTrue($this->form_validation->in_list('red', 'red,Blue,123'));
161 $this->assertTrue($this->form_validation->in_list('Blue', 'red,Blue,123'));
162 $this->assertTrue($this->form_validation->in_list('123', 'red,Blue,123'));
David Woods64af3bb2015-03-18 10:09:26 -0700163
David Woods317cad92015-03-20 22:32:24 -0700164 $this->assertFalse($this->form_validation->in_list('Red', 'red,Blue,123'));
165 $this->assertFalse($this->form_validation->in_list(' red', 'red,Blue,123'));
166 $this->assertFalse($this->form_validation->in_list('1234', 'red,Blue,123'));
David Woods64af3bb2015-03-18 10:09:26 -0700167 }
168
169 public function test_rule_alpha()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200170 {
David Woods317cad92015-03-20 22:32:24 -0700171 $this->assertTrue($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ'));
David Woods64af3bb2015-03-18 10:09:26 -0700172
David Woods317cad92015-03-20 22:32:24 -0700173 $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ '));
174 $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1'));
175 $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*'));
David Woods64af3bb2015-03-18 10:09:26 -0700176 }
177
178 public function test_rule_alpha_numeric()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200179 {
David Woods317cad92015-03-20 22:32:24 -0700180 $this->assertTrue($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789'));
David Woods64af3bb2015-03-18 10:09:26 -0700181
David Woods317cad92015-03-20 22:32:24 -0700182 $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ '));
183 $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_'));
David Woods64af3bb2015-03-18 10:09:26 -0700184 }
185
186 public function test_rule_alpha_numeric_spaces()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200187 {
David Woods317cad92015-03-20 22:32:24 -0700188 $this->assertTrue($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789'));
David Woods64af3bb2015-03-18 10:09:26 -0700189
David Woods317cad92015-03-20 22:32:24 -0700190 $this->assertFalse($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_'));
David Woods64af3bb2015-03-18 10:09:26 -0700191 }
192
193 public function test_rule_alpha_dash()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200194 {
David Woods317cad92015-03-20 22:32:24 -0700195 $this->assertTrue($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_'));
David Woods64af3bb2015-03-18 10:09:26 -0700196
David Woods317cad92015-03-20 22:32:24 -0700197 $this->assertFalse($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ '));
David Woods64af3bb2015-03-18 10:09:26 -0700198 }
199
200 public function test_rule_numeric()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200201 {
David Woods317cad92015-03-20 22:32:24 -0700202 $this->assertTrue($this->form_validation->numeric('0'));
203 $this->assertTrue($this->form_validation->numeric('12314'));
204 $this->assertTrue($this->form_validation->numeric('-42'));
David Woods64af3bb2015-03-18 10:09:26 -0700205
David Woods317cad92015-03-20 22:32:24 -0700206 $this->assertFalse($this->form_validation->numeric('123a'));
207 $this->assertFalse($this->form_validation->numeric('--1'));
David Woods64af3bb2015-03-18 10:09:26 -0700208 }
209
210 public function test_rule_integer()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200211 {
David Woods317cad92015-03-20 22:32:24 -0700212 $this->assertTrue($this->form_validation->integer('0'));
213 $this->assertTrue($this->form_validation->integer('42'));
214 $this->assertTrue($this->form_validation->integer('-1'));
David Woods64af3bb2015-03-18 10:09:26 -0700215
David Woods317cad92015-03-20 22:32:24 -0700216 $this->assertFalse($this->form_validation->integer('124a'));
217 $this->assertFalse($this->form_validation->integer('1.9'));
218 $this->assertFalse($this->form_validation->integer('--1'));
David Woods64af3bb2015-03-18 10:09:26 -0700219 }
220
221 public function test_rule_decimal()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200222 {
David Woods317cad92015-03-20 22:32:24 -0700223 $this->assertTrue($this->form_validation->decimal('1.0'));
224 $this->assertTrue($this->form_validation->decimal('-0.98'));
David Woods64af3bb2015-03-18 10:09:26 -0700225
David Woods317cad92015-03-20 22:32:24 -0700226 $this->assertFalse($this->form_validation->decimal('0'));
227 $this->assertFalse($this->form_validation->decimal('1.0a'));
228 $this->assertFalse($this->form_validation->decimal('-i'));
229 $this->assertFalse($this->form_validation->decimal('--1'));
David Woods64af3bb2015-03-18 10:09:26 -0700230 }
231
232 public function test_rule_is_natural()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200233 {
David Woods317cad92015-03-20 22:32:24 -0700234 $this->assertTrue($this->form_validation->is_natural('0'));
235 $this->assertTrue($this->form_validation->is_natural('12'));
David Woods64af3bb2015-03-18 10:09:26 -0700236
David Woods317cad92015-03-20 22:32:24 -0700237 $this->assertFalse($this->form_validation->is_natural('42a'));
238 $this->assertFalse($this->form_validation->is_natural('-1'));
David Woods64af3bb2015-03-18 10:09:26 -0700239 }
240
241 public function test_rule_is_natural_no_zero()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200242 {
David Woods317cad92015-03-20 22:32:24 -0700243 $this->assertTrue($this->form_validation->is_natural_no_zero('42'));
David Woods64af3bb2015-03-18 10:09:26 -0700244
David Woods317cad92015-03-20 22:32:24 -0700245 $this->assertFalse($this->form_validation->is_natural_no_zero('0'));
246 $this->assertFalse($this->form_validation->is_natural_no_zero('42a'));
247 $this->assertFalse($this->form_validation->is_natural_no_zero('-1'));
David Woods64af3bb2015-03-18 10:09:26 -0700248 }
249
250 public function test_rule_valid_url()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200251 {
David Woods317cad92015-03-20 22:32:24 -0700252 $this->assertTrue($this->form_validation->valid_url('www.codeigniter.com'));
Andrey Andreev700dbad2016-01-11 12:51:46 +0200253 $this->assertTrue($this->form_validation->valid_url('http://codeigniter.com'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200254
Andrey Andreev391d3392016-01-30 22:43:41 +0200255 // https://bugs.php.net/bug.php?id=51192
256 $this->assertTrue($this->form_validation->valid_url('http://accept-dashes.tld'));
257 $this->assertFalse($this->form_validation->valid_url('http://reject_underscores.tld'));
258
259 // https://github.com/bcit-ci/CodeIgniter/issues/4415
260 $this->assertTrue($this->form_validation->valid_url('http://[::1]/ipv6'));
261
Andrey Andreev9180a122016-08-10 15:23:42 +0300262 // URI scheme case-sensitivity: https://github.com/bcit-ci/CodeIgniter/pull/4758
263 $this->assertTrue($this->form_validation->valid_url('HtTp://127.0.0.1/'));
264
David Woods317cad92015-03-20 22:32:24 -0700265 $this->assertFalse($this->form_validation->valid_url('htt://www.codeIgniter.com'));
266 $this->assertFalse($this->form_validation->valid_url(''));
267 $this->assertFalse($this->form_validation->valid_url('code igniter'));
David Woods64af3bb2015-03-18 10:09:26 -0700268 }
269
270 public function test_rule_valid_email()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200271 {
David Woods317cad92015-03-20 22:32:24 -0700272 $this->assertTrue($this->form_validation->valid_email('email@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700273
David Woods317cad92015-03-20 22:32:24 -0700274 $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700275 }
276
277 public function test_rule_valid_emails()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200278 {
David Woods317cad92015-03-20 22:32:24 -0700279 $this->assertTrue($this->form_validation->valid_emails('1@sample.com,2@sample.com'));
280 $this->assertTrue($this->form_validation->valid_emails('email@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700281
David Woods5bc1dc52015-03-27 22:41:58 -0700282 $this->assertFalse($this->form_validation->valid_emails('valid_email', '@sample.com'));
David Woods317cad92015-03-20 22:32:24 -0700283 $this->assertFalse($this->form_validation->valid_emails('@sample.com,2@sample.com,validemail@email.ca'));
David Woods64af3bb2015-03-18 10:09:26 -0700284 }
285
286 public function test_rule_valid_ip()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200287 {
David Woods317cad92015-03-20 22:32:24 -0700288 $this->assertTrue($this->form_validation->valid_ip('127.0.0.1'));
289 $this->assertTrue($this->form_validation->valid_ip('127.0.0.1', 'ipv4'));
290 $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
291 $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv6'));
David Woods64af3bb2015-03-18 10:09:26 -0700292
David Woods317cad92015-03-20 22:32:24 -0700293 $this->assertFalse($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv4'));
294 $this->assertFalse($this->form_validation->valid_ip('127.0.0.1', 'ipv6'));
295 $this->assertFalse($this->form_validation->valid_ip('H001:0db8:85a3:0000:0000:8a2e:0370:7334'));
296 $this->assertFalse($this->form_validation->valid_ip('127.0.0.259'));
David Woods64af3bb2015-03-18 10:09:26 -0700297 }
298
299 public function test_rule_valid_base64()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200300 {
David Woods317cad92015-03-20 22:32:24 -0700301 $this->assertTrue($this->form_validation->valid_base64(base64_encode('string')));
David Woods5b884732015-03-18 10:37:35 -0700302
David Woods317cad92015-03-20 22:32:24 -0700303 $this->assertFalse($this->form_validation->valid_base64('FA08GG'));
304 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200305
David Woods317cad92015-03-20 22:32:24 -0700306 public function test_set_data()
307 {
308 // Reset test environment
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200309 $_POST = array();
David Woods317cad92015-03-20 22:32:24 -0700310 $this->form_validation->reset_validation();
David Woods317cad92015-03-20 22:32:24 -0700311 $data = array('field' => 'some_data');
312 $this->form_validation->set_data($data);
313 $this->form_validation->set_rules('field', 'label', 'required');
314 $this->assertTrue($this->form_validation->run());
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200315
David Woods317cad92015-03-20 22:32:24 -0700316 // Test with empty array
317 $_POST = array();
David Woods317cad92015-03-20 22:32:24 -0700318 $this->form_validation->reset_validation();
David Woodscf776712015-03-22 15:55:16 -0700319 $data = array('field' => 'some_data');
David Woods317cad92015-03-20 22:32:24 -0700320 $this->form_validation->set_data($data);
David Woodscf776712015-03-22 15:55:16 -0700321 // This should do nothing. Old data will still be used
322 $this->form_validation->set_data(array());
David Woods317cad92015-03-20 22:32:24 -0700323 $this->form_validation->set_rules('field', 'label', 'required');
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200324 $this->assertTrue($this->form_validation->run());
David Woods317cad92015-03-20 22:32:24 -0700325 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200326
David Woods317cad92015-03-20 22:32:24 -0700327 public function test_set_message()
328 {
329 // Reset test environment
330 $_POST = array();
331 $this->form_validation->reset_validation();
332 $err_message = 'What a terrible error!';
333 $rules = array(
334 array(
335 'field' => 'req_field',
336 'label' => 'label',
337 'rules' => 'required'
338 )
339 );
340 $errorless_data = array('req_field' => 'some text');
341 $erroneous_data = array('req_field' => '');
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200342
David Woods317cad92015-03-20 22:32:24 -0700343 $this->form_validation->set_message('required', $err_message);
344 $this->form_validation->set_data($erroneous_data);
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200345 $this->form_validation->set_rules($rules);
David Woods317cad92015-03-20 22:32:24 -0700346 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700347 $this->assertEquals('<p>'.$err_message.'</p>', $this->form_validation->error('req_field'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200348
David Woods317cad92015-03-20 22:32:24 -0700349 $this->form_validation->reset_validation();
350 $this->form_validation->set_message('required', $err_message);
351 $this->form_validation->set_data($errorless_data);
352 $this->form_validation->set_rules($rules);
353 $this->form_validation->run();
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200354 $this->assertEquals('', $this->form_validation->error('req_field'));
David Woods64af3bb2015-03-18 10:09:26 -0700355 }
356
David Woods5bc1dc52015-03-27 22:41:58 -0700357 public function test_set_error_delimiters()
358 {
359 $this->form_validation->reset_validation();
360 $prefix = '<div class="error">';
361 $suffix = '</div>';
362 $this->form_validation->set_error_delimiters($prefix, $suffix);
363 $this->form_validation->set_rules('foo', 'label', 'required');
364 $_POST = array('foo' => '');
365 $this->form_validation->run();
366 $error_msg = $this->form_validation->error('foo');
367
368 $this->assertTrue(strrpos($error_msg, $prefix) === 0);
369 $this->assertTrue(strrpos($error_msg, $suffix, -strlen($suffix)) === (strlen($error_msg) - strlen($suffix)));
370 }
371
372 public function test_error_array()
373 {
374 $this->form_validation->reset_validation();
375 $error_message = 'What a terrible error!';
376 $this->form_validation->set_message('required', $error_message);
377 $this->form_validation->set_rules('foo', 'label', 'required');
378 $_POST = array('foo' => '');
379 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700380 $error_array = $this->form_validation->error_array();
381 $this->assertEquals($error_message, $error_array['foo']);
David Woods5bc1dc52015-03-27 22:41:58 -0700382 }
383
384 public function test_error_string()
385 {
386 $this->form_validation->reset_validation();
387 $error_message = 'What a terrible error!';
388 $prefix_default = '<foo>';
389 $suffix_default = '</foo>';
390 $prefix_test = '<bar>';
391 $suffix_test = '</bar>';
392 $this->form_validation->set_error_delimiters($prefix_default, $suffix_default);
393 $this->form_validation->set_message('required', $error_message);
394 $this->form_validation->set_rules('foo', 'label', 'required');
395 $_POST = array('foo' => '');
396 $this->form_validation->run();
397
David Woodsc6ac5592015-03-31 20:19:39 -0700398 $this->assertEquals($prefix_default.$error_message.$suffix_default."\n", $this->form_validation->error_string());
399 $this->assertEquals($prefix_test.$error_message.$suffix_default."\n", $this->form_validation->error_string($prefix_test, ''));
400 $this->assertEquals($prefix_default.$error_message.$suffix_test."\n", $this->form_validation->error_string('', $suffix_test));
401 $this->assertEquals($prefix_test.$error_message.$suffix_test."\n", $this->form_validation->error_string($prefix_test, $suffix_test));
402
David Woods5bc1dc52015-03-27 22:41:58 -0700403 $this->form_validation->reset_validation();
404 $this->form_validation->set_rules('foo', 'label', 'required');
405 $_POST = array('foo' => 'bar');
406 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700407 $this->assertEquals('', $this->form_validation->error_string());
David Woods5bc1dc52015-03-27 22:41:58 -0700408 }
409
410 public function test_run()
411 {
412 // form_validation->run() is tested in many of the other unit tests
413 // This test will only test run(group='') when group is not empty
414 $config = array(
415 'pass' => array(
416 array(
417 'field' => 'username',
418 'label' => 'user',
419 'rules' => 'alpha_numeric'
420 )
421 ),
422 'fail' => array(
423 array(
424 'field' => 'username',
425 'label' => 'user',
426 'rules' => 'alpha'
427 )
428 )
429 );
430 $_POST = array('username' => 'foo42');
431 $form_validation = new CI_Form_validation($config);
432 $this->assertTrue($form_validation->run('pass'));
David Woodsc6ac5592015-03-31 20:19:39 -0700433
434 $form_validation = new CI_Form_validation($config);
David Woods5bc1dc52015-03-27 22:41:58 -0700435 $this->assertFalse($form_validation->run('fail'));
436 }
David Woodsc6ac5592015-03-31 20:19:39 -0700437
David Woods5bc1dc52015-03-27 22:41:58 -0700438 public function test_has_rule()
439 {
440 $this->form_validation->reset_validation();
441 $this->form_validation->set_rules('foo', 'label', 'required');
David Woodsc6ac5592015-03-31 20:19:39 -0700442
David Woods5bc1dc52015-03-27 22:41:58 -0700443 $this->assertTrue($this->form_validation->has_rule('foo'));
444 $this->assertFalse($this->form_validation->has_rule('bar'));
445 }
David Woodsc6ac5592015-03-31 20:19:39 -0700446
David Woods5bc1dc52015-03-27 22:41:58 -0700447 public function test_set_value()
448 {
449 $this->form_validation->reset_validation();
450 $default = 'default';
451 $this->form_validation->set_rules('foo', 'label', 'required');
452 $this->form_validation->set_rules('bar[]', 'label', 'required');
David Woodsc6ac5592015-03-31 20:19:39 -0700453
David Woods5bc1dc52015-03-27 22:41:58 -0700454 // No post data yet: should return the default value provided
455 $this->assertEquals($default, $this->form_validation->set_value('foo', $default));
456 $_POST = array('foo' => 'foo', 'bar' => array('bar1', 'bar2'));
457 $this->form_validation->run();
458 $this->assertEquals('foo', $this->form_validation->set_value('foo', $default));
459 $this->assertEquals('bar1', $this->form_validation->set_value('bar[]', $default));
460 $this->assertEquals('bar2', $this->form_validation->set_value('bar[]', $default));
461 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300462
David Woods4b02f1d2015-03-29 22:46:14 -0700463 public function test_set_select()
464 {
465 // Test 1: No options selected
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300466 $this->form_validation->reset_validation();
David Woods4b02f1d2015-03-29 22:46:14 -0700467 $_POST = array();
468 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300469
470 $this->assertEquals('', $this->form_validation->set_select('select', 'foo'));
David Woods4b02f1d2015-03-29 22:46:14 -0700471 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300472
David Woods4b02f1d2015-03-29 22:46:14 -0700473 // Test 2: 1 option selected
474 $this->form_validation->reset_validation();
475 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
476 $_POST = array('select' => 'foo');
477 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300478
David Woods4b02f1d2015-03-29 22:46:14 -0700479 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo'));
480 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo', TRUE));
481 $this->assertEquals('', $this->form_validation->set_select('select', 'bar'));
482 $this->assertEquals('', $this->form_validation->set_select('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300483
David Woods4b02f1d2015-03-29 22:46:14 -0700484 // Test 3: Multiple options selected
485 $this->form_validation->reset_validation();
David Woods29704f82015-03-30 10:37:57 -0700486 $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric');
David Woods4b02f1d2015-03-29 22:46:14 -0700487 $_POST = array('select' => array('foo', 'bar'));
488 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300489
David Woods29704f82015-03-30 10:37:57 -0700490 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'foo'));
491 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'foo', TRUE));
492 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'bar'));
493 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'bar', TRUE));
494 $this->assertEquals('', $this->form_validation->set_select('select[]', 'foobar'));
495 $this->assertEquals('', $this->form_validation->set_select('select[]', 'foobar', TRUE));
David Woods4b02f1d2015-03-29 22:46:14 -0700496 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300497
David Woods4b02f1d2015-03-29 22:46:14 -0700498 public function test_set_radio()
499 {
500 // Test 1: No options selected
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300501 $this->form_validation->reset_validation();
David Woods4b02f1d2015-03-29 22:46:14 -0700502 $_POST = array();
503 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300504
David Woods4b02f1d2015-03-29 22:46:14 -0700505 $this->assertEquals('', $this->form_validation->set_radio('select', 'foo'));
David Woods29704f82015-03-30 10:37:57 -0700506 // Default should only work when no rules are set
David Woods4b02f1d2015-03-29 22:46:14 -0700507 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300508
David Woods4b02f1d2015-03-29 22:46:14 -0700509 // Test 2: 1 option selected
510 $this->form_validation->reset_validation();
511 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
512 $_POST = array('select' => 'foo');
513 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300514
David Woods4b02f1d2015-03-29 22:46:14 -0700515 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo'));
516 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo', TRUE));
517 $this->assertEquals('', $this->form_validation->set_radio('select', 'bar'));
518 $this->assertEquals('', $this->form_validation->set_radio('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300519
David Woods4b02f1d2015-03-29 22:46:14 -0700520 // Test 3: Multiple options checked
521 $this->form_validation->reset_validation();
David Woods29704f82015-03-30 10:37:57 -0700522 $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric');
David Woods4b02f1d2015-03-29 22:46:14 -0700523 $_POST = array('select' => array('foo', 'bar'));
524 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300525
David Woods29704f82015-03-30 10:37:57 -0700526 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'foo'));
527 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'foo', TRUE));
528 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'bar'));
529 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'bar', TRUE));
530 $this->assertEquals('', $this->form_validation->set_radio('select[]', 'foobar'));
531 $this->assertEquals('', $this->form_validation->set_radio('select[]', 'foobar', TRUE));
David Woods4b02f1d2015-03-29 22:46:14 -0700532 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300533
David Woods4b02f1d2015-03-29 22:46:14 -0700534 public function test_set_checkbox()
535 {
536 // Test 1: No options selected
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300537 $this->form_validation->reset_validation();
David Woods4b02f1d2015-03-29 22:46:14 -0700538 $_POST = array();
539 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300540
541 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'foo'));
David Woods4b02f1d2015-03-29 22:46:14 -0700542 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300543
David Woods4b02f1d2015-03-29 22:46:14 -0700544 // Test 2: 1 option selected
545 $this->form_validation->reset_validation();
546 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
547 $_POST = array('select' => 'foo');
548 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300549
David Woods4b02f1d2015-03-29 22:46:14 -0700550 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo'));
551 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo', TRUE));
552 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar'));
553 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300554
David Woods4b02f1d2015-03-29 22:46:14 -0700555 // Test 3: Multiple options selected
556 $this->form_validation->reset_validation();
David Woods29704f82015-03-30 10:37:57 -0700557 $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric');
David Woods4b02f1d2015-03-29 22:46:14 -0700558 $_POST = array('select' => array('foo', 'bar'));
559 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300560
David Woods29704f82015-03-30 10:37:57 -0700561 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'foo'));
562 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'foo', TRUE));
563 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'bar'));
564 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'bar', TRUE));
565 $this->assertEquals('', $this->form_validation->set_checkbox('select[]', 'foobar'));
566 $this->assertEquals('', $this->form_validation->set_checkbox('select[]', 'foobar', TRUE));
David Woods4b02f1d2015-03-29 22:46:14 -0700567 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300568
David Woods4b02f1d2015-03-29 22:46:14 -0700569 public function test_regex_match()
570 {
571 $regex = '/f[a-zA-Z]+/';
572 $this->assertTrue($this->form_validation->regex_match('foo', $regex));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300573 $this->assertFalse($this->form_validation->regex_match('bar', $regex));
David Woods4b02f1d2015-03-29 22:46:14 -0700574 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300575
David Woods4b02f1d2015-03-29 22:46:14 -0700576 public function test_prep_for_form()
577 {
578 $this->form_validation->reset_validation();
David Woodsc6ac5592015-03-31 20:19:39 -0700579 $error_msg_unprepped = '<error =\'foobar\'">';
580 $error_msg_prepped = '&lt;error =&#39;foobar&#39;&quot;&gt;';
581 $this->form_validation->set_rules('foo', 'label', 'required', array('required' => $error_msg_unprepped));
David Woods4b02f1d2015-03-29 22:46:14 -0700582 $_POST = array('foo' => '');
583 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700584 $error_arr = $this->form_validation->error_array();
585
David Woods4b02f1d2015-03-29 22:46:14 -0700586 $this->assertEquals('', $this->form_validation->prep_for_form(''));
David Woodsc6ac5592015-03-31 20:19:39 -0700587 $this->assertEquals(array('foo' => $error_msg_prepped), $this->form_validation->prep_for_form($error_arr));
David Woods4b02f1d2015-03-29 22:46:14 -0700588 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300589
David Woods4b02f1d2015-03-29 22:46:14 -0700590 public function test_prep_url()
591 {
592 $this->assertEquals('', $this->form_validation->prep_url(''));
593 $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('codeigniter.com'));
594 $this->assertEquals('https://codeigniter.com', $this->form_validation->prep_url('https://codeigniter.com'));
595 $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('http://codeigniter.com'));
596 $this->assertEquals('http://www.codeigniter.com', $this->form_validation->prep_url('www.codeigniter.com'));
597 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300598
David Woods4b02f1d2015-03-29 22:46:14 -0700599 public function test_encode_php_tags()
600 {
601 $this->assertEquals("&lt;?php", $this->form_validation->encode_php_tags('<?php'));
602 $this->assertEquals('?&gt;', $this->form_validation->encode_php_tags('?>'));
603 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300604
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200605 /**
606 * Run rules
607 *
608 * Helper method to set rules and run them at once, not
609 * an actual test case.
610 */
David Woodscf776712015-03-22 15:55:16 -0700611 public function run_rules($rules, $values)
David Woods64af3bb2015-03-18 10:09:26 -0700612 {
David Woods64af3bb2015-03-18 10:09:26 -0700613 $this->form_validation->reset_validation();
David Woodscf776712015-03-22 15:55:16 -0700614 $_POST = array();
David Woods5b884732015-03-18 10:37:35 -0700615
David Woodscf776712015-03-22 15:55:16 -0700616 $this->form_validation->set_rules($rules);
617 foreach ($values as $field => $value)
618 {
619 $_POST[$field] = $value;
620 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200621
David Woods64af3bb2015-03-18 10:09:26 -0700622 return $this->form_validation->run();
623 }
David Woodsf3ac71e2015-03-16 20:00:21 -0700624}