blob: 26d82ec93952b70376cfc73bda45363275170fb4 [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'));
232 $this->assertTrue($this->form_validation->valid_url('http://codeigniter.eu'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200233
David Woods317cad92015-03-20 22:32:24 -0700234 $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 Woods64af3bb2015-03-18 10:09:26 -0700237 }
238
239 public function test_rule_valid_email()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200240 {
David Woods317cad92015-03-20 22:32:24 -0700241 $this->assertTrue($this->form_validation->valid_email('email@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700242
David Woods317cad92015-03-20 22:32:24 -0700243 $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700244 }
245
246 public function test_rule_valid_emails()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200247 {
David Woods317cad92015-03-20 22:32:24 -0700248 $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 Woods64af3bb2015-03-18 10:09:26 -0700250
David Woods5bc1dc52015-03-27 22:41:58 -0700251 $this->assertFalse($this->form_validation->valid_emails('valid_email', '@sample.com'));
David Woods317cad92015-03-20 22:32:24 -0700252 $this->assertFalse($this->form_validation->valid_emails('@sample.com,2@sample.com,validemail@email.ca'));
David Woods64af3bb2015-03-18 10:09:26 -0700253 }
254
255 public function test_rule_valid_ip()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200256 {
David Woods317cad92015-03-20 22:32:24 -0700257 $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 Woods64af3bb2015-03-18 10:09:26 -0700261
David Woods317cad92015-03-20 22:32:24 -0700262 $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 Woods64af3bb2015-03-18 10:09:26 -0700266 }
267
268 public function test_rule_valid_base64()
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200269 {
David Woods317cad92015-03-20 22:32:24 -0700270 $this->assertTrue($this->form_validation->valid_base64(base64_encode('string')));
David Woods5b884732015-03-18 10:37:35 -0700271
David Woods317cad92015-03-20 22:32:24 -0700272 $this->assertFalse($this->form_validation->valid_base64('FA08GG'));
273 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200274
David Woods317cad92015-03-20 22:32:24 -0700275 public function test_set_data()
276 {
277 // Reset test environment
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200278 $_POST = array();
David Woods317cad92015-03-20 22:32:24 -0700279 $this->form_validation->reset_validation();
David Woods317cad92015-03-20 22:32:24 -0700280 $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 Andreev3e2045b2015-03-26 12:34:38 +0200284
David Woods317cad92015-03-20 22:32:24 -0700285 // Test with empty array
286 $_POST = array();
David Woods317cad92015-03-20 22:32:24 -0700287 $this->form_validation->reset_validation();
David Woodscf776712015-03-22 15:55:16 -0700288 $data = array('field' => 'some_data');
David Woods317cad92015-03-20 22:32:24 -0700289 $this->form_validation->set_data($data);
David Woodscf776712015-03-22 15:55:16 -0700290 // This should do nothing. Old data will still be used
291 $this->form_validation->set_data(array());
David Woods317cad92015-03-20 22:32:24 -0700292 $this->form_validation->set_rules('field', 'label', 'required');
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200293 $this->assertTrue($this->form_validation->run());
David Woods317cad92015-03-20 22:32:24 -0700294 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200295
David Woods317cad92015-03-20 22:32:24 -0700296 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 Andreev3e2045b2015-03-26 12:34:38 +0200311
David Woods317cad92015-03-20 22:32:24 -0700312 $this->form_validation->set_message('required', $err_message);
313 $this->form_validation->set_data($erroneous_data);
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200314 $this->form_validation->set_rules($rules);
David Woods317cad92015-03-20 22:32:24 -0700315 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700316 $this->assertEquals('<p>'.$err_message.'</p>', $this->form_validation->error('req_field'));
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200317
David Woods317cad92015-03-20 22:32:24 -0700318 $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 Andreev3e2045b2015-03-26 12:34:38 +0200323 $this->assertEquals('', $this->form_validation->error('req_field'));
David Woods64af3bb2015-03-18 10:09:26 -0700324 }
325
David Woods5bc1dc52015-03-27 22:41:58 -0700326 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();
David Woodsc6ac5592015-03-31 20:19:39 -0700349 $error_array = $this->form_validation->error_array();
350 $this->assertEquals($error_message, $error_array['foo']);
David Woods5bc1dc52015-03-27 22:41:58 -0700351 }
352
353 public function test_error_string()
354 {
355 $this->form_validation->reset_validation();
356 $error_message = 'What a terrible error!';
357 $prefix_default = '<foo>';
358 $suffix_default = '</foo>';
359 $prefix_test = '<bar>';
360 $suffix_test = '</bar>';
361 $this->form_validation->set_error_delimiters($prefix_default, $suffix_default);
362 $this->form_validation->set_message('required', $error_message);
363 $this->form_validation->set_rules('foo', 'label', 'required');
364 $_POST = array('foo' => '');
365 $this->form_validation->run();
366
David Woodsc6ac5592015-03-31 20:19:39 -0700367 $this->assertEquals($prefix_default.$error_message.$suffix_default."\n", $this->form_validation->error_string());
368 $this->assertEquals($prefix_test.$error_message.$suffix_default."\n", $this->form_validation->error_string($prefix_test, ''));
369 $this->assertEquals($prefix_default.$error_message.$suffix_test."\n", $this->form_validation->error_string('', $suffix_test));
370 $this->assertEquals($prefix_test.$error_message.$suffix_test."\n", $this->form_validation->error_string($prefix_test, $suffix_test));
371
David Woods5bc1dc52015-03-27 22:41:58 -0700372 $this->form_validation->reset_validation();
373 $this->form_validation->set_rules('foo', 'label', 'required');
374 $_POST = array('foo' => 'bar');
375 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700376 $this->assertEquals('', $this->form_validation->error_string());
David Woods5bc1dc52015-03-27 22:41:58 -0700377 }
378
379 public function test_run()
380 {
381 // form_validation->run() is tested in many of the other unit tests
382 // This test will only test run(group='') when group is not empty
383 $config = array(
384 'pass' => array(
385 array(
386 'field' => 'username',
387 'label' => 'user',
388 'rules' => 'alpha_numeric'
389 )
390 ),
391 'fail' => array(
392 array(
393 'field' => 'username',
394 'label' => 'user',
395 'rules' => 'alpha'
396 )
397 )
398 );
399 $_POST = array('username' => 'foo42');
400 $form_validation = new CI_Form_validation($config);
401 $this->assertTrue($form_validation->run('pass'));
David Woodsc6ac5592015-03-31 20:19:39 -0700402
403 $form_validation = new CI_Form_validation($config);
David Woods5bc1dc52015-03-27 22:41:58 -0700404 $this->assertFalse($form_validation->run('fail'));
405 }
David Woodsc6ac5592015-03-31 20:19:39 -0700406
David Woods5bc1dc52015-03-27 22:41:58 -0700407 public function test_has_rule()
408 {
409 $this->form_validation->reset_validation();
410 $this->form_validation->set_rules('foo', 'label', 'required');
David Woodsc6ac5592015-03-31 20:19:39 -0700411
David Woods5bc1dc52015-03-27 22:41:58 -0700412 $this->assertTrue($this->form_validation->has_rule('foo'));
413 $this->assertFalse($this->form_validation->has_rule('bar'));
414 }
David Woodsc6ac5592015-03-31 20:19:39 -0700415
David Woods5bc1dc52015-03-27 22:41:58 -0700416 public function test_set_value()
417 {
418 $this->form_validation->reset_validation();
419 $default = 'default';
420 $this->form_validation->set_rules('foo', 'label', 'required');
421 $this->form_validation->set_rules('bar[]', 'label', 'required');
David Woodsc6ac5592015-03-31 20:19:39 -0700422
David Woods5bc1dc52015-03-27 22:41:58 -0700423 // No post data yet: should return the default value provided
424 $this->assertEquals($default, $this->form_validation->set_value('foo', $default));
425 $_POST = array('foo' => 'foo', 'bar' => array('bar1', 'bar2'));
426 $this->form_validation->run();
427 $this->assertEquals('foo', $this->form_validation->set_value('foo', $default));
428 $this->assertEquals('bar1', $this->form_validation->set_value('bar[]', $default));
429 $this->assertEquals('bar2', $this->form_validation->set_value('bar[]', $default));
430 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300431
David Woods4b02f1d2015-03-29 22:46:14 -0700432 public function test_set_select()
433 {
434 // Test 1: No options selected
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300435 $this->form_validation->reset_validation();
David Woods4b02f1d2015-03-29 22:46:14 -0700436 $_POST = array();
437 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300438
439 $this->assertEquals('', $this->form_validation->set_select('select', 'foo'));
David Woods4b02f1d2015-03-29 22:46:14 -0700440 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300441
David Woods4b02f1d2015-03-29 22:46:14 -0700442 // Test 2: 1 option selected
443 $this->form_validation->reset_validation();
444 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
445 $_POST = array('select' => 'foo');
446 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300447
David Woods4b02f1d2015-03-29 22:46:14 -0700448 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo'));
449 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo', TRUE));
450 $this->assertEquals('', $this->form_validation->set_select('select', 'bar'));
451 $this->assertEquals('', $this->form_validation->set_select('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300452
David Woods4b02f1d2015-03-29 22:46:14 -0700453 // Test 3: Multiple options selected
454 $this->form_validation->reset_validation();
David Woods29704f82015-03-30 10:37:57 -0700455 $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric');
David Woods4b02f1d2015-03-29 22:46:14 -0700456 $_POST = array('select' => array('foo', 'bar'));
457 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300458
David Woods29704f82015-03-30 10:37:57 -0700459 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'foo'));
460 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'foo', TRUE));
461 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'bar'));
462 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'bar', TRUE));
463 $this->assertEquals('', $this->form_validation->set_select('select[]', 'foobar'));
464 $this->assertEquals('', $this->form_validation->set_select('select[]', 'foobar', TRUE));
David Woods4b02f1d2015-03-29 22:46:14 -0700465 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300466
David Woods4b02f1d2015-03-29 22:46:14 -0700467 public function test_set_radio()
468 {
469 // Test 1: No options selected
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300470 $this->form_validation->reset_validation();
David Woods4b02f1d2015-03-29 22:46:14 -0700471 $_POST = array();
472 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300473
David Woods4b02f1d2015-03-29 22:46:14 -0700474 $this->assertEquals('', $this->form_validation->set_radio('select', 'foo'));
David Woods29704f82015-03-30 10:37:57 -0700475 // Default should only work when no rules are set
David Woods4b02f1d2015-03-29 22:46:14 -0700476 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300477
David Woods4b02f1d2015-03-29 22:46:14 -0700478 // Test 2: 1 option selected
479 $this->form_validation->reset_validation();
480 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
481 $_POST = array('select' => 'foo');
482 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300483
David Woods4b02f1d2015-03-29 22:46:14 -0700484 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo'));
485 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo', TRUE));
486 $this->assertEquals('', $this->form_validation->set_radio('select', 'bar'));
487 $this->assertEquals('', $this->form_validation->set_radio('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300488
David Woods4b02f1d2015-03-29 22:46:14 -0700489 // Test 3: Multiple options checked
490 $this->form_validation->reset_validation();
David Woods29704f82015-03-30 10:37:57 -0700491 $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric');
David Woods4b02f1d2015-03-29 22:46:14 -0700492 $_POST = array('select' => array('foo', 'bar'));
493 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300494
David Woods29704f82015-03-30 10:37:57 -0700495 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'foo'));
496 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'foo', TRUE));
497 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'bar'));
498 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'bar', TRUE));
499 $this->assertEquals('', $this->form_validation->set_radio('select[]', 'foobar'));
500 $this->assertEquals('', $this->form_validation->set_radio('select[]', 'foobar', TRUE));
David Woods4b02f1d2015-03-29 22:46:14 -0700501 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300502
David Woods4b02f1d2015-03-29 22:46:14 -0700503 public function test_set_checkbox()
504 {
505 // Test 1: No options selected
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300506 $this->form_validation->reset_validation();
David Woods4b02f1d2015-03-29 22:46:14 -0700507 $_POST = array();
508 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300509
510 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'foo'));
David Woods4b02f1d2015-03-29 22:46:14 -0700511 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300512
David Woods4b02f1d2015-03-29 22:46:14 -0700513 // Test 2: 1 option selected
514 $this->form_validation->reset_validation();
515 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
516 $_POST = array('select' => 'foo');
517 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300518
David Woods4b02f1d2015-03-29 22:46:14 -0700519 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo'));
520 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo', TRUE));
521 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar'));
522 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar', TRUE));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300523
David Woods4b02f1d2015-03-29 22:46:14 -0700524 // Test 3: Multiple options selected
525 $this->form_validation->reset_validation();
David Woods29704f82015-03-30 10:37:57 -0700526 $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric');
David Woods4b02f1d2015-03-29 22:46:14 -0700527 $_POST = array('select' => array('foo', 'bar'));
528 $this->form_validation->run();
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300529
David Woods29704f82015-03-30 10:37:57 -0700530 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'foo'));
531 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'foo', TRUE));
532 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'bar'));
533 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'bar', TRUE));
534 $this->assertEquals('', $this->form_validation->set_checkbox('select[]', 'foobar'));
535 $this->assertEquals('', $this->form_validation->set_checkbox('select[]', 'foobar', TRUE));
David Woods4b02f1d2015-03-29 22:46:14 -0700536 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300537
David Woods4b02f1d2015-03-29 22:46:14 -0700538 public function test_regex_match()
539 {
540 $regex = '/f[a-zA-Z]+/';
541 $this->assertTrue($this->form_validation->regex_match('foo', $regex));
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300542 $this->assertFalse($this->form_validation->regex_match('bar', $regex));
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_prep_for_form()
546 {
547 $this->form_validation->reset_validation();
David Woodsc6ac5592015-03-31 20:19:39 -0700548 $error_msg_unprepped = '<error =\'foobar\'">';
549 $error_msg_prepped = '&lt;error =&#39;foobar&#39;&quot;&gt;';
550 $this->form_validation->set_rules('foo', 'label', 'required', array('required' => $error_msg_unprepped));
David Woods4b02f1d2015-03-29 22:46:14 -0700551 $_POST = array('foo' => '');
552 $this->form_validation->run();
David Woodsc6ac5592015-03-31 20:19:39 -0700553 $error_arr = $this->form_validation->error_array();
554
David Woods4b02f1d2015-03-29 22:46:14 -0700555 $this->assertEquals('', $this->form_validation->prep_for_form(''));
David Woodsc6ac5592015-03-31 20:19:39 -0700556 $this->assertEquals(array('foo' => $error_msg_prepped), $this->form_validation->prep_for_form($error_arr));
David Woods4b02f1d2015-03-29 22:46:14 -0700557 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300558
David Woods4b02f1d2015-03-29 22:46:14 -0700559 public function test_prep_url()
560 {
561 $this->assertEquals('', $this->form_validation->prep_url(''));
562 $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('codeigniter.com'));
563 $this->assertEquals('https://codeigniter.com', $this->form_validation->prep_url('https://codeigniter.com'));
564 $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('http://codeigniter.com'));
565 $this->assertEquals('http://www.codeigniter.com', $this->form_validation->prep_url('www.codeigniter.com'));
566 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300567
David Woods4b02f1d2015-03-29 22:46:14 -0700568 public function test_encode_php_tags()
569 {
570 $this->assertEquals("&lt;?php", $this->form_validation->encode_php_tags('<?php'));
571 $this->assertEquals('?&gt;', $this->form_validation->encode_php_tags('?>'));
572 }
Andrey Andreev2e9ae002015-04-01 14:45:16 +0300573
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200574 /**
575 * Run rules
576 *
577 * Helper method to set rules and run them at once, not
578 * an actual test case.
579 */
David Woodscf776712015-03-22 15:55:16 -0700580 public function run_rules($rules, $values)
David Woods64af3bb2015-03-18 10:09:26 -0700581 {
David Woods64af3bb2015-03-18 10:09:26 -0700582 $this->form_validation->reset_validation();
David Woodscf776712015-03-22 15:55:16 -0700583 $_POST = array();
David Woods5b884732015-03-18 10:37:35 -0700584
David Woodscf776712015-03-22 15:55:16 -0700585 $this->form_validation->set_rules($rules);
586 foreach ($values as $field => $value)
587 {
588 $_POST[$field] = $value;
589 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200590
David Woods64af3bb2015-03-18 10:09:26 -0700591 return $this->form_validation->run();
592 }
David Woodsf3ac71e2015-03-16 20:00:21 -0700593}