blob: f455b9146a67471fcb52a6f061dd58668707487f [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
David Woods5b884732015-03-18 10:37:35 -070011 $loader = $this->getMock('CI_Loader', array('helper'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +020012
David Woods64af3bb2015-03-18 10:09:26 -070013 // Same applies for lang
David Woods5b884732015-03-18 10:37:35 -070014 $lang = $this->getMock('CI_Lang', array('load'));
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
David Woods64af3bb2015-03-18 10:09:26 -070031 public function test_rule_required()
32 {
David Woodscf776712015-03-22 15:55:16 -070033 $rules = array(array('field' => 'foo', 'label' => 'foo_label', 'rules' => 'required'));
34 $this->assertTrue($this->run_rules($rules, array('foo' => 'bar')));
David Woods64af3bb2015-03-18 10:09:26 -070035
David Woodscf776712015-03-22 15:55:16 -070036 $this->assertFalse($this->run_rules($rules, array('foo' => '')));
37 $this->assertFalse($this->run_rules($rules, array('foo' => ' ')));
David Woods64af3bb2015-03-18 10:09:26 -070038 }
39
40 public function test_rule_matches()
Andrey Andreev3e2045b2015-03-26 12:34:38 +020041 {
David Woodscf776712015-03-22 15:55:16 -070042 $rules = array(
43 array('field' => 'foo', 'label' => 'label', 'rules' => 'required'),
Andrey Andreev3e2045b2015-03-26 12:34:38 +020044 array('field' => 'bar', 'label' => 'label2', 'rules' => 'matches[foo]')
45 );
David Woodscf776712015-03-22 15:55:16 -070046 $values_base = array('foo' => 'sample');
David Woods64af3bb2015-03-18 10:09:26 -070047
Andrey Andreev3e2045b2015-03-26 12:34:38 +020048 $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 Woods64af3bb2015-03-18 10:09:26 -070053 }
54
55 public function test_rule_differs()
Andrey Andreev3e2045b2015-03-26 12:34:38 +020056 {
David Woodscf776712015-03-22 15:55:16 -070057 $rules = array(
58 array('field' => 'foo', 'label' => 'label', 'rules' => 'required'),
Andrey Andreev3e2045b2015-03-26 12:34:38 +020059 array('field' => 'bar', 'label' => 'label2', 'rules' => 'differs[foo]')
60 );
David Woodscf776712015-03-22 15:55:16 -070061 $values_base = array('foo' => 'sample');
David Woods64af3bb2015-03-18 10:09:26 -070062
Andrey Andreev3e2045b2015-03-26 12:34:38 +020063 $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 Woods64af3bb2015-03-18 10:09:26 -070068 }
69
70 public function test_rule_min_length()
Andrey Andreev3e2045b2015-03-26 12:34:38 +020071 {
David Woods317cad92015-03-20 22:32:24 -070072 $this->assertTrue($this->form_validation->min_length('12345', '5'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +020073 $this->assertTrue($this->form_validation->min_length('test', '0'));
David Woods64af3bb2015-03-18 10:09:26 -070074
David Woods317cad92015-03-20 22:32:24 -070075 $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 Woods64af3bb2015-03-18 10:09:26 -070078 }
79
80 public function test_rule_max_length()
Andrey Andreev3e2045b2015-03-26 12:34:38 +020081 {
David Woods317cad92015-03-20 22:32:24 -070082 $this->assertTrue($this->form_validation->max_length('', '4'));
83 $this->assertTrue($this->form_validation->max_length('1234', '4'));
David Woods64af3bb2015-03-18 10:09:26 -070084
David Woods317cad92015-03-20 22:32:24 -070085 $this->assertFalse($this->form_validation->max_length('12345', '4'));
86 $this->assertFalse($this->form_validation->max_length('should_fail', 'A'));
David Woods64af3bb2015-03-18 10:09:26 -070087 }
88
89 public function test_rule_exact_length()
Andrey Andreev3e2045b2015-03-26 12:34:38 +020090 {
91 $this->assertTrue($this->form_validation->exact_length('1234', '4'));
David Woods64af3bb2015-03-18 10:09:26 -070092
David Woods317cad92015-03-20 22:32:24 -070093 $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 Woods64af3bb2015-03-18 10:09:26 -070097 }
98
99 public function test_rule_greater_than()
100 {
David Woods317cad92015-03-20 22:32:24 -0700101 $this->assertTrue($this->form_validation->greater_than('-10', '-11'));
102 $this->assertTrue($this->form_validation->greater_than('10', '9'));
David Woods64af3bb2015-03-18 10:09:26 -0700103
David Woods317cad92015-03-20 22:32:24 -0700104 $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 Woods64af3bb2015-03-18 10:09:26 -0700107 }
108
109 public function test_rule_greater_than_equal_to()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200110 {
111 $this->assertTrue($this->form_validation->greater_than_equal_to('0', '0'));
David Woods317cad92015-03-20 22:32:24 -0700112 $this->assertTrue($this->form_validation->greater_than_equal_to('1', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700113
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200114 $this->assertFalse($this->form_validation->greater_than_equal_to('-1', '0'));
David Woods317cad92015-03-20 22:32:24 -0700115 $this->assertFalse($this->form_validation->greater_than_equal_to('10a', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700116 }
117
118 public function test_rule_less_than()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200119 {
David Woods317cad92015-03-20 22:32:24 -0700120 $this->assertTrue($this->form_validation->less_than('4', '5'));
121 $this->assertTrue($this->form_validation->less_than('-1', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700122
David Woods317cad92015-03-20 22:32:24 -0700123 $this->assertFalse($this->form_validation->less_than('4', '4'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200124 $this->assertFalse($this->form_validation->less_than('10a', '5'));
David Woods64af3bb2015-03-18 10:09:26 -0700125 }
126
127 public function test_rule_less_than_equal_to()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200128 {
David Woods317cad92015-03-20 22:32:24 -0700129 $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 Woods64af3bb2015-03-18 10:09:26 -0700132
David Woods317cad92015-03-20 22:32:24 -0700133 $this->assertFalse($this->form_validation->less_than_equal_to('0', '-1'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200134 $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700135 }
136
137 public function test_rule_in_list()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200138 {
David Woods317cad92015-03-20 22:32:24 -0700139 $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 Woods64af3bb2015-03-18 10:09:26 -0700142
David Woods317cad92015-03-20 22:32:24 -0700143 $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 Woods64af3bb2015-03-18 10:09:26 -0700146 }
147
148 public function test_rule_alpha()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200149 {
David Woods317cad92015-03-20 22:32:24 -0700150 $this->assertTrue($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ'));
David Woods64af3bb2015-03-18 10:09:26 -0700151
David Woods317cad92015-03-20 22:32:24 -0700152 $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ '));
153 $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1'));
154 $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*'));
David Woods64af3bb2015-03-18 10:09:26 -0700155 }
156
157 public function test_rule_alpha_numeric()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200158 {
David Woods317cad92015-03-20 22:32:24 -0700159 $this->assertTrue($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789'));
David Woods64af3bb2015-03-18 10:09:26 -0700160
David Woods317cad92015-03-20 22:32:24 -0700161 $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ '));
162 $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_'));
David Woods64af3bb2015-03-18 10:09:26 -0700163 }
164
165 public function test_rule_alpha_numeric_spaces()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200166 {
David Woods317cad92015-03-20 22:32:24 -0700167 $this->assertTrue($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789'));
David Woods64af3bb2015-03-18 10:09:26 -0700168
David Woods317cad92015-03-20 22:32:24 -0700169 $this->assertFalse($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_'));
David Woods64af3bb2015-03-18 10:09:26 -0700170 }
171
172 public function test_rule_alpha_dash()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200173 {
David Woods317cad92015-03-20 22:32:24 -0700174 $this->assertTrue($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_'));
David Woods64af3bb2015-03-18 10:09:26 -0700175
David Woods317cad92015-03-20 22:32:24 -0700176 $this->assertFalse($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ '));
David Woods64af3bb2015-03-18 10:09:26 -0700177 }
178
179 public function test_rule_numeric()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200180 {
David Woods317cad92015-03-20 22:32:24 -0700181 $this->assertTrue($this->form_validation->numeric('0'));
182 $this->assertTrue($this->form_validation->numeric('12314'));
183 $this->assertTrue($this->form_validation->numeric('-42'));
David Woods64af3bb2015-03-18 10:09:26 -0700184
David Woods317cad92015-03-20 22:32:24 -0700185 $this->assertFalse($this->form_validation->numeric('123a'));
186 $this->assertFalse($this->form_validation->numeric('--1'));
David Woods64af3bb2015-03-18 10:09:26 -0700187 }
188
189 public function test_rule_integer()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200190 {
David Woods317cad92015-03-20 22:32:24 -0700191 $this->assertTrue($this->form_validation->integer('0'));
192 $this->assertTrue($this->form_validation->integer('42'));
193 $this->assertTrue($this->form_validation->integer('-1'));
David Woods64af3bb2015-03-18 10:09:26 -0700194
David Woods317cad92015-03-20 22:32:24 -0700195 $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 Woods64af3bb2015-03-18 10:09:26 -0700198 }
199
200 public function test_rule_decimal()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200201 {
David Woods317cad92015-03-20 22:32:24 -0700202 $this->assertTrue($this->form_validation->decimal('1.0'));
203 $this->assertTrue($this->form_validation->decimal('-0.98'));
David Woods64af3bb2015-03-18 10:09:26 -0700204
David Woods317cad92015-03-20 22:32:24 -0700205 $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 Woods64af3bb2015-03-18 10:09:26 -0700209 }
210
211 public function test_rule_is_natural()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200212 {
David Woods317cad92015-03-20 22:32:24 -0700213 $this->assertTrue($this->form_validation->is_natural('0'));
214 $this->assertTrue($this->form_validation->is_natural('12'));
David Woods64af3bb2015-03-18 10:09:26 -0700215
David Woods317cad92015-03-20 22:32:24 -0700216 $this->assertFalse($this->form_validation->is_natural('42a'));
217 $this->assertFalse($this->form_validation->is_natural('-1'));
David Woods64af3bb2015-03-18 10:09:26 -0700218 }
219
220 public function test_rule_is_natural_no_zero()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200221 {
David Woods317cad92015-03-20 22:32:24 -0700222 $this->assertTrue($this->form_validation->is_natural_no_zero('42'));
David Woods64af3bb2015-03-18 10:09:26 -0700223
David Woods317cad92015-03-20 22:32:24 -0700224 $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 Woods64af3bb2015-03-18 10:09:26 -0700227 }
228
229 public function test_rule_valid_url()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200230 {
David Woods317cad92015-03-20 22:32:24 -0700231 $this->assertTrue($this->form_validation->valid_url('www.codeigniter.com'));
Andrey Andreev700dbad2016-01-11 12:51:46 +0200232 $this->assertTrue($this->form_validation->valid_url('http://codeigniter.com'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200233
Andrey Andreev391d3392016-01-30 22:43:41 +0200234 // https://bugs.php.net/bug.php?id=51192
235 $this->assertTrue($this->form_validation->valid_url('http://accept-dashes.tld'));
236 $this->assertFalse($this->form_validation->valid_url('http://reject_underscores.tld'));
237
238 // https://github.com/bcit-ci/CodeIgniter/issues/4415
239 $this->assertTrue($this->form_validation->valid_url('http://[::1]/ipv6'));
240
David Woods317cad92015-03-20 22:32:24 -0700241 $this->assertFalse($this->form_validation->valid_url('htt://www.codeIgniter.com'));
242 $this->assertFalse($this->form_validation->valid_url(''));
243 $this->assertFalse($this->form_validation->valid_url('code igniter'));
David Woods64af3bb2015-03-18 10:09:26 -0700244 }
245
246 public function test_rule_valid_email()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200247 {
David Woods317cad92015-03-20 22:32:24 -0700248 $this->assertTrue($this->form_validation->valid_email('email@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700249
David Woods317cad92015-03-20 22:32:24 -0700250 $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700251 }
252
253 public function test_rule_valid_emails()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200254 {
David Woods317cad92015-03-20 22:32:24 -0700255 $this->assertTrue($this->form_validation->valid_emails('1@sample.com,2@sample.com'));
256 $this->assertTrue($this->form_validation->valid_emails('email@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700257
David Woods5bc1dc52015-03-27 22:41:58 -0700258 $this->assertFalse($this->form_validation->valid_emails('valid_email', '@sample.com'));
David Woods317cad92015-03-20 22:32:24 -0700259 $this->assertFalse($this->form_validation->valid_emails('@sample.com,2@sample.com,validemail@email.ca'));
David Woods64af3bb2015-03-18 10:09:26 -0700260 }
261
262 public function test_rule_valid_ip()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200263 {
David Woods317cad92015-03-20 22:32:24 -0700264 $this->assertTrue($this->form_validation->valid_ip('127.0.0.1'));
265 $this->assertTrue($this->form_validation->valid_ip('127.0.0.1', 'ipv4'));
266 $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
267 $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv6'));
David Woods64af3bb2015-03-18 10:09:26 -0700268
David Woods317cad92015-03-20 22:32:24 -0700269 $this->assertFalse($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv4'));
270 $this->assertFalse($this->form_validation->valid_ip('127.0.0.1', 'ipv6'));
271 $this->assertFalse($this->form_validation->valid_ip('H001:0db8:85a3:0000:0000:8a2e:0370:7334'));
272 $this->assertFalse($this->form_validation->valid_ip('127.0.0.259'));
David Woods64af3bb2015-03-18 10:09:26 -0700273 }
274
275 public function test_rule_valid_base64()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200276 {
David Woods317cad92015-03-20 22:32:24 -0700277 $this->assertTrue($this->form_validation->valid_base64(base64_encode('string')));
David Woods5b884732015-03-18 10:37:35 -0700278
David Woods317cad92015-03-20 22:32:24 -0700279 $this->assertFalse($this->form_validation->valid_base64('FA08GG'));
280 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200281
David Woods317cad92015-03-20 22:32:24 -0700282 public function test_set_data()
283 {
284 // Reset test environment
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200285 $_POST = array();
David Woods317cad92015-03-20 22:32:24 -0700286 $this->form_validation->reset_validation();
David Woods317cad92015-03-20 22:32:24 -0700287 $data = array('field' => 'some_data');
288 $this->form_validation->set_data($data);
289 $this->form_validation->set_rules('field', 'label', 'required');
290 $this->assertTrue($this->form_validation->run());
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200291
David Woods317cad92015-03-20 22:32:24 -0700292 // Test with empty array
293 $_POST = array();
David Woods317cad92015-03-20 22:32:24 -0700294 $this->form_validation->reset_validation();
David Woodscf776712015-03-22 15:55:16 -0700295 $data = array('field' => 'some_data');
David Woods317cad92015-03-20 22:32:24 -0700296 $this->form_validation->set_data($data);
David Woodscf776712015-03-22 15:55:16 -0700297 // This should do nothing. Old data will still be used
298 $this->form_validation->set_data(array());
David Woods317cad92015-03-20 22:32:24 -0700299 $this->form_validation->set_rules('field', 'label', 'required');
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200300 $this->assertTrue($this->form_validation->run());
David Woods317cad92015-03-20 22:32:24 -0700301 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200302
David Woods317cad92015-03-20 22:32:24 -0700303 public function test_set_message()
304 {
305 // Reset test environment
306 $_POST = array();
307 $this->form_validation->reset_validation();
308 $err_message = 'What a terrible error!';
309 $rules = array(
310 array(
311 'field' => 'req_field',
312 'label' => 'label',
313 'rules' => 'required'
314 )
315 );
316 $errorless_data = array('req_field' => 'some text');
317 $erroneous_data = array('req_field' => '');
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200318
David Woods317cad92015-03-20 22:32:24 -0700319 $this->form_validation->set_message('required', $err_message);
320 $this->form_validation->set_data($erroneous_data);
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200321 $this->form_validation->set_rules($rules);
David Woods317cad92015-03-20 22:32:24 -0700322 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700323 $this->assertEquals('<p>'.$err_message.'</p>', $this->form_validation->error('req_field'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200324
David Woods317cad92015-03-20 22:32:24 -0700325 $this->form_validation->reset_validation();
326 $this->form_validation->set_message('required', $err_message);
327 $this->form_validation->set_data($errorless_data);
328 $this->form_validation->set_rules($rules);
329 $this->form_validation->run();
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200330 $this->assertEquals('', $this->form_validation->error('req_field'));
David Woods64af3bb2015-03-18 10:09:26 -0700331 }
332
David Woods5bc1dc52015-03-27 22:41:58 -0700333 public function test_set_error_delimiters()
334 {
335 $this->form_validation->reset_validation();
336 $prefix = '<div class="error">';
337 $suffix = '</div>';
338 $this->form_validation->set_error_delimiters($prefix, $suffix);
339 $this->form_validation->set_rules('foo', 'label', 'required');
340 $_POST = array('foo' => '');
341 $this->form_validation->run();
342 $error_msg = $this->form_validation->error('foo');
343
344 $this->assertTrue(strrpos($error_msg, $prefix) === 0);
345 $this->assertTrue(strrpos($error_msg, $suffix, -strlen($suffix)) === (strlen($error_msg) - strlen($suffix)));
346 }
347
348 public function test_error_array()
349 {
350 $this->form_validation->reset_validation();
351 $error_message = 'What a terrible error!';
352 $this->form_validation->set_message('required', $error_message);
353 $this->form_validation->set_rules('foo', 'label', 'required');
354 $_POST = array('foo' => '');
355 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700356 $error_array = $this->form_validation->error_array();
357 $this->assertEquals($error_message, $error_array['foo']);
David Woods5bc1dc52015-03-27 22:41:58 -0700358 }
359
360 public function test_error_string()
361 {
362 $this->form_validation->reset_validation();
363 $error_message = 'What a terrible error!';
364 $prefix_default = '<foo>';
365 $suffix_default = '</foo>';
366 $prefix_test = '<bar>';
367 $suffix_test = '</bar>';
368 $this->form_validation->set_error_delimiters($prefix_default, $suffix_default);
369 $this->form_validation->set_message('required', $error_message);
370 $this->form_validation->set_rules('foo', 'label', 'required');
371 $_POST = array('foo' => '');
372 $this->form_validation->run();
373
David Woodsc6ac5592015-03-31 20:19:39 -0700374 $this->assertEquals($prefix_default.$error_message.$suffix_default."\n", $this->form_validation->error_string());
375 $this->assertEquals($prefix_test.$error_message.$suffix_default."\n", $this->form_validation->error_string($prefix_test, ''));
376 $this->assertEquals($prefix_default.$error_message.$suffix_test."\n", $this->form_validation->error_string('', $suffix_test));
377 $this->assertEquals($prefix_test.$error_message.$suffix_test."\n", $this->form_validation->error_string($prefix_test, $suffix_test));
378
David Woods5bc1dc52015-03-27 22:41:58 -0700379 $this->form_validation->reset_validation();
380 $this->form_validation->set_rules('foo', 'label', 'required');
381 $_POST = array('foo' => 'bar');
382 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700383 $this->assertEquals('', $this->form_validation->error_string());
David Woods5bc1dc52015-03-27 22:41:58 -0700384 }
385
386 public function test_run()
387 {
388 // form_validation->run() is tested in many of the other unit tests
389 // This test will only test run(group='') when group is not empty
390 $config = array(
391 'pass' => array(
392 array(
393 'field' => 'username',
394 'label' => 'user',
395 'rules' => 'alpha_numeric'
396 )
397 ),
398 'fail' => array(
399 array(
400 'field' => 'username',
401 'label' => 'user',
402 'rules' => 'alpha'
403 )
404 )
405 );
406 $_POST = array('username' => 'foo42');
407 $form_validation = new CI_Form_validation($config);
408 $this->assertTrue($form_validation->run('pass'));
David Woodsc6ac5592015-03-31 20:19:39 -0700409
410 $form_validation = new CI_Form_validation($config);
David Woods5bc1dc52015-03-27 22:41:58 -0700411 $this->assertFalse($form_validation->run('fail'));
412 }
David Woodsc6ac5592015-03-31 20:19:39 -0700413
David Woods5bc1dc52015-03-27 22:41:58 -0700414 public function test_has_rule()
415 {
416 $this->form_validation->reset_validation();
417 $this->form_validation->set_rules('foo', 'label', 'required');
David Woodsc6ac5592015-03-31 20:19:39 -0700418
David Woods5bc1dc52015-03-27 22:41:58 -0700419 $this->assertTrue($this->form_validation->has_rule('foo'));
420 $this->assertFalse($this->form_validation->has_rule('bar'));
421 }
David Woodsc6ac5592015-03-31 20:19:39 -0700422
David Woods5bc1dc52015-03-27 22:41:58 -0700423 public function test_set_value()
424 {
425 $this->form_validation->reset_validation();
426 $default = 'default';
427 $this->form_validation->set_rules('foo', 'label', 'required');
428 $this->form_validation->set_rules('bar[]', 'label', 'required');
David Woodsc6ac5592015-03-31 20:19:39 -0700429
David Woods5bc1dc52015-03-27 22:41:58 -0700430 // No post data yet: should return the default value provided
431 $this->assertEquals($default, $this->form_validation->set_value('foo', $default));
432 $_POST = array('foo' => 'foo', 'bar' => array('bar1', 'bar2'));
433 $this->form_validation->run();
434 $this->assertEquals('foo', $this->form_validation->set_value('foo', $default));
435 $this->assertEquals('bar1', $this->form_validation->set_value('bar[]', $default));
436 $this->assertEquals('bar2', $this->form_validation->set_value('bar[]', $default));
437 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300438
David Woods4b02f1d2015-03-29 22:46:14 -0700439 public function test_set_select()
440 {
441 // Test 1: No options selected
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300442 $this->form_validation->reset_validation();
David Woods4b02f1d2015-03-29 22:46:14 -0700443 $_POST = array();
444 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300445
446 $this->assertEquals('', $this->form_validation->set_select('select', 'foo'));
David Woods4b02f1d2015-03-29 22:46:14 -0700447 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300448
David Woods4b02f1d2015-03-29 22:46:14 -0700449 // Test 2: 1 option selected
450 $this->form_validation->reset_validation();
451 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
452 $_POST = array('select' => 'foo');
453 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300454
David Woods4b02f1d2015-03-29 22:46:14 -0700455 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo'));
456 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo', TRUE));
457 $this->assertEquals('', $this->form_validation->set_select('select', 'bar'));
458 $this->assertEquals('', $this->form_validation->set_select('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300459
David Woods4b02f1d2015-03-29 22:46:14 -0700460 // Test 3: Multiple options selected
461 $this->form_validation->reset_validation();
David Woods29704f82015-03-30 10:37:57 -0700462 $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric');
David Woods4b02f1d2015-03-29 22:46:14 -0700463 $_POST = array('select' => array('foo', 'bar'));
464 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300465
David Woods29704f82015-03-30 10:37:57 -0700466 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'foo'));
467 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'foo', TRUE));
468 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'bar'));
469 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'bar', TRUE));
470 $this->assertEquals('', $this->form_validation->set_select('select[]', 'foobar'));
471 $this->assertEquals('', $this->form_validation->set_select('select[]', 'foobar', TRUE));
David Woods4b02f1d2015-03-29 22:46:14 -0700472 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300473
David Woods4b02f1d2015-03-29 22:46:14 -0700474 public function test_set_radio()
475 {
476 // Test 1: No options selected
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300477 $this->form_validation->reset_validation();
David Woods4b02f1d2015-03-29 22:46:14 -0700478 $_POST = array();
479 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300480
David Woods4b02f1d2015-03-29 22:46:14 -0700481 $this->assertEquals('', $this->form_validation->set_radio('select', 'foo'));
David Woods29704f82015-03-30 10:37:57 -0700482 // Default should only work when no rules are set
David Woods4b02f1d2015-03-29 22:46:14 -0700483 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300484
David Woods4b02f1d2015-03-29 22:46:14 -0700485 // Test 2: 1 option selected
486 $this->form_validation->reset_validation();
487 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
488 $_POST = array('select' => 'foo');
489 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300490
David Woods4b02f1d2015-03-29 22:46:14 -0700491 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo'));
492 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo', TRUE));
493 $this->assertEquals('', $this->form_validation->set_radio('select', 'bar'));
494 $this->assertEquals('', $this->form_validation->set_radio('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300495
David Woods4b02f1d2015-03-29 22:46:14 -0700496 // Test 3: Multiple options checked
497 $this->form_validation->reset_validation();
David Woods29704f82015-03-30 10:37:57 -0700498 $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric');
David Woods4b02f1d2015-03-29 22:46:14 -0700499 $_POST = array('select' => array('foo', 'bar'));
500 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300501
David Woods29704f82015-03-30 10:37:57 -0700502 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'foo'));
503 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'foo', TRUE));
504 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'bar'));
505 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'bar', TRUE));
506 $this->assertEquals('', $this->form_validation->set_radio('select[]', 'foobar'));
507 $this->assertEquals('', $this->form_validation->set_radio('select[]', 'foobar', TRUE));
David Woods4b02f1d2015-03-29 22:46:14 -0700508 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300509
David Woods4b02f1d2015-03-29 22:46:14 -0700510 public function test_set_checkbox()
511 {
512 // Test 1: No options selected
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300513 $this->form_validation->reset_validation();
David Woods4b02f1d2015-03-29 22:46:14 -0700514 $_POST = array();
515 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300516
517 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'foo'));
David Woods4b02f1d2015-03-29 22:46:14 -0700518 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300519
David Woods4b02f1d2015-03-29 22:46:14 -0700520 // Test 2: 1 option selected
521 $this->form_validation->reset_validation();
522 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
523 $_POST = array('select' => 'foo');
524 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300525
David Woods4b02f1d2015-03-29 22:46:14 -0700526 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo'));
527 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo', TRUE));
528 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar'));
529 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300530
David Woods4b02f1d2015-03-29 22:46:14 -0700531 // Test 3: Multiple options selected
532 $this->form_validation->reset_validation();
David Woods29704f82015-03-30 10:37:57 -0700533 $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric');
David Woods4b02f1d2015-03-29 22:46:14 -0700534 $_POST = array('select' => array('foo', 'bar'));
535 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300536
David Woods29704f82015-03-30 10:37:57 -0700537 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'foo'));
538 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'foo', TRUE));
539 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'bar'));
540 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'bar', TRUE));
541 $this->assertEquals('', $this->form_validation->set_checkbox('select[]', 'foobar'));
542 $this->assertEquals('', $this->form_validation->set_checkbox('select[]', 'foobar', TRUE));
David Woods4b02f1d2015-03-29 22:46:14 -0700543 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300544
David Woods4b02f1d2015-03-29 22:46:14 -0700545 public function test_regex_match()
546 {
547 $regex = '/f[a-zA-Z]+/';
548 $this->assertTrue($this->form_validation->regex_match('foo', $regex));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300549 $this->assertFalse($this->form_validation->regex_match('bar', $regex));
David Woods4b02f1d2015-03-29 22:46:14 -0700550 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300551
David Woods4b02f1d2015-03-29 22:46:14 -0700552 public function test_prep_for_form()
553 {
554 $this->form_validation->reset_validation();
David Woodsc6ac5592015-03-31 20:19:39 -0700555 $error_msg_unprepped = '<error =\'foobar\'">';
556 $error_msg_prepped = '&lt;error =&#39;foobar&#39;&quot;&gt;';
557 $this->form_validation->set_rules('foo', 'label', 'required', array('required' => $error_msg_unprepped));
David Woods4b02f1d2015-03-29 22:46:14 -0700558 $_POST = array('foo' => '');
559 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700560 $error_arr = $this->form_validation->error_array();
561
David Woods4b02f1d2015-03-29 22:46:14 -0700562 $this->assertEquals('', $this->form_validation->prep_for_form(''));
David Woodsc6ac5592015-03-31 20:19:39 -0700563 $this->assertEquals(array('foo' => $error_msg_prepped), $this->form_validation->prep_for_form($error_arr));
David Woods4b02f1d2015-03-29 22:46:14 -0700564 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300565
David Woods4b02f1d2015-03-29 22:46:14 -0700566 public function test_prep_url()
567 {
568 $this->assertEquals('', $this->form_validation->prep_url(''));
569 $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('codeigniter.com'));
570 $this->assertEquals('https://codeigniter.com', $this->form_validation->prep_url('https://codeigniter.com'));
571 $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('http://codeigniter.com'));
572 $this->assertEquals('http://www.codeigniter.com', $this->form_validation->prep_url('www.codeigniter.com'));
573 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300574
David Woods4b02f1d2015-03-29 22:46:14 -0700575 public function test_encode_php_tags()
576 {
577 $this->assertEquals("&lt;?php", $this->form_validation->encode_php_tags('<?php'));
578 $this->assertEquals('?&gt;', $this->form_validation->encode_php_tags('?>'));
579 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300580
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200581 /**
582 * Run rules
583 *
584 * Helper method to set rules and run them at once, not
585 * an actual test case.
586 */
David Woodscf776712015-03-22 15:55:16 -0700587 public function run_rules($rules, $values)
David Woods64af3bb2015-03-18 10:09:26 -0700588 {
David Woods64af3bb2015-03-18 10:09:26 -0700589 $this->form_validation->reset_validation();
David Woodscf776712015-03-22 15:55:16 -0700590 $_POST = array();
David Woods5b884732015-03-18 10:37:35 -0700591
David Woodscf776712015-03-22 15:55:16 -0700592 $this->form_validation->set_rules($rules);
593 foreach ($values as $field => $value)
594 {
595 $_POST[$field] = $value;
596 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200597
David Woods64af3bb2015-03-18 10:09:26 -0700598 return $this->form_validation->run();
599 }
David Woodsf3ac71e2015-03-16 20:00:21 -0700600}