blob: 38eb11a347e4d388a8aa598e84e6230d1a074004 [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 Woods5bc1dc52015-03-27 22:41:58 -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();
349 $this->assertEquals($error_message, $this->form_validation->error_array()['foo']);
350 }
351
352 public function test_error_string()
353 {
354 $this->form_validation->reset_validation();
355 $error_message = 'What a terrible error!';
356 $prefix_default = '<foo>';
357 $suffix_default = '</foo>';
358 $prefix_test = '<bar>';
359 $suffix_test = '</bar>';
360 $this->form_validation->set_error_delimiters($prefix_default, $suffix_default);
361 $this->form_validation->set_message('required', $error_message);
362 $this->form_validation->set_rules('foo', 'label', 'required');
363 $_POST = array('foo' => '');
364 $this->form_validation->run();
365
366 $this->assertEquals($prefix_default . $error_message . $suffix_default . "\n", $this->form_validation->error_string());
367 $this->assertEquals($prefix_test . $error_message . $suffix_default . "\n", $this->form_validation->error_string($prefix_test, ''));
368 $this->assertEquals($prefix_default . $error_message . $suffix_test . "\n", $this->form_validation->error_string('', $suffix_test));
369 $this->assertEquals($prefix_test . $error_message . $suffix_test . "\n", $this->form_validation->error_string($prefix_test, $suffix_test));
370
371 $this->form_validation->reset_validation();
372 $this->form_validation->set_rules('foo', 'label', 'required');
373 $_POST = array('foo' => 'bar');
374 $this->form_validation->run();
375 $this->assertEquals('', $this->form_validation->error_string());
376 }
377
378 public function test_run()
379 {
380 // form_validation->run() is tested in many of the other unit tests
381 // This test will only test run(group='') when group is not empty
382 $config = array(
383 'pass' => array(
384 array(
385 'field' => 'username',
386 'label' => 'user',
387 'rules' => 'alpha_numeric'
388 )
389 ),
390 'fail' => array(
391 array(
392 'field' => 'username',
393 'label' => 'user',
394 'rules' => 'alpha'
395 )
396 )
397 );
398 $_POST = array('username' => 'foo42');
399 $form_validation = new CI_Form_validation($config);
400 $this->assertTrue($form_validation->run('pass'));
401
402 $form_validation = new CI_Form_validation($config);
403 $this->assertFalse($form_validation->run('fail'));
404 }
405
406 public function test_has_rule()
407 {
408 $this->form_validation->reset_validation();
409 $this->form_validation->set_rules('foo', 'label', 'required');
410
411 $this->assertTrue($this->form_validation->has_rule('foo'));
412 $this->assertFalse($this->form_validation->has_rule('bar'));
413 }
414
415 public function test_set_value()
416 {
417 $this->form_validation->reset_validation();
418 $default = 'default';
419 $this->form_validation->set_rules('foo', 'label', 'required');
420 $this->form_validation->set_rules('bar[]', 'label', 'required');
421
422 // No post data yet: should return the default value provided
423 $this->assertEquals($default, $this->form_validation->set_value('foo', $default));
424 $_POST = array('foo' => 'foo', 'bar' => array('bar1', 'bar2'));
425 $this->form_validation->run();
426 $this->assertEquals('foo', $this->form_validation->set_value('foo', $default));
427 $this->assertEquals('bar1', $this->form_validation->set_value('bar[]', $default));
428 $this->assertEquals('bar2', $this->form_validation->set_value('bar[]', $default));
429 }
430
David Woods4b02f1d2015-03-29 22:46:14 -0700431 public function test_set_select()
432 {
433 // Test 1: No options selected
434 $this->form_validation->reset_validation();
435 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
436 $_POST = array();
437 $this->form_validation->run();
438
439 $this->assertEquals('', $this->form_validation->set_select('select', 'foo'));
440 // This fails. Default is only used when no rules are defined. Is this really the desired behaviour?
441 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'bar', TRUE));
442
443 // Test 2: 1 option selected
444 $this->form_validation->reset_validation();
445 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
446 $_POST = array('select' => 'foo');
447 $this->form_validation->run();
448
449 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo'));
450 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo', TRUE));
451 $this->assertEquals('', $this->form_validation->set_select('select', 'bar'));
452 $this->assertEquals('', $this->form_validation->set_select('select', 'bar', TRUE));
453
454 // Test 3: Multiple options selected
455 $this->form_validation->reset_validation();
456 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
457 $_POST = array('select' => array('foo', 'bar'));
458 $this->form_validation->run();
459
460 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo'));
461 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo', TRUE));
462 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'bar'));
463 $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'bar', TRUE));
464 $this->assertEquals('', $this->form_validation->set_select('select', 'foobar'));
465 $this->assertEquals('', $this->form_validation->set_select('select', 'foobar', TRUE));
466 }
467
468 public function test_set_radio()
469 {
470 // Test 1: No options selected
471 $this->form_validation->reset_validation();
472 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
473 $_POST = array();
474 $this->form_validation->run();
475
476 $this->assertEquals('', $this->form_validation->set_radio('select', 'foo'));
477 // This fails. Default is only used when no rules are defined. Is this really the desired behaviour?
478 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'bar', TRUE));
479
480 // Test 2: 1 option selected
481 $this->form_validation->reset_validation();
482 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
483 $_POST = array('select' => 'foo');
484 $this->form_validation->run();
485
486 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo'));
487 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo', TRUE));
488 $this->assertEquals('', $this->form_validation->set_radio('select', 'bar'));
489 $this->assertEquals('', $this->form_validation->set_radio('select', 'bar', TRUE));
490
491 // Test 3: Multiple options checked
492 $this->form_validation->reset_validation();
493 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
494 $_POST = array('select' => array('foo', 'bar'));
495 $this->form_validation->run();
496
497 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo'));
498 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo', TRUE));
499 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'bar'));
500 $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'bar', TRUE));
501 $this->assertEquals('', $this->form_validation->set_radio('select', 'foobar'));
502 $this->assertEquals('', $this->form_validation->set_radio('select', 'foobar', TRUE));
503 }
504
505 public function test_set_checkbox()
506 {
507 // Test 1: No options selected
508 $this->form_validation->reset_validation();
509 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
510 $_POST = array();
511 $this->form_validation->run();
512
513 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'foo'));
514 // This fails. Default is only used when no rules are defined. Is this really the desired behaviour?
515 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'bar', TRUE));
516
517 // Test 2: 1 option selected
518 $this->form_validation->reset_validation();
519 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
520 $_POST = array('select' => 'foo');
521 $this->form_validation->run();
522
523 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo'));
524 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo', TRUE));
525 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar'));
526 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar', TRUE));
527
528 // Test 3: Multiple options selected
529 $this->form_validation->reset_validation();
530 $this->form_validation->set_rules('select', 'label', 'alpha_numeric');
531 $_POST = array('select' => array('foo', 'bar'));
532 $this->form_validation->run();
533
534 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo'));
535 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo', TRUE));
536 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'bar'));
537 $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'bar', TRUE));
538 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'foobar'));
539 $this->assertEquals('', $this->form_validation->set_checkbox('select', 'foobar', TRUE));
540 }
541
542 public function test_regex_match()
543 {
544 $regex = '/f[a-zA-Z]+/';
545 $this->assertTrue($this->form_validation->regex_match('foo', $regex));
546 $this->assertFalse($this->form_validation->regex_match('bar', $regex));
547 }
548
549 public function test_prep_for_form()
550 {
551 $this->form_validation->reset_validation();
552 $err_msg_unprepped = '<error =\'foobar\'">';
553 $err_msg_prepped = '&lt;error =&#39;foobar&#39;&quot;&gt;';
554 $this->form_validation->set_rules('foo', 'label', 'required', array('required' => $err_msg_unprepped));
555 $_POST = array('foo' => '');
556 $this->form_validation->run();
557 $err_arr = $this->form_validation->error_array();
558
559 $this->assertEquals('', $this->form_validation->prep_for_form(''));
560 $this->assertEquals(array('foo' => $err_msg_prepped), $this->form_validation->prep_for_form($err_arr));
561 }
562
563 public function test_prep_url()
564 {
565 $this->assertEquals('', $this->form_validation->prep_url(''));
566 $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('codeigniter.com'));
567 $this->assertEquals('https://codeigniter.com', $this->form_validation->prep_url('https://codeigniter.com'));
568 $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('http://codeigniter.com'));
569 $this->assertEquals('http://www.codeigniter.com', $this->form_validation->prep_url('www.codeigniter.com'));
570 }
571
572 public function test_encode_php_tags()
573 {
574 $this->assertEquals("&lt;?php", $this->form_validation->encode_php_tags('<?php'));
575 $this->assertEquals('?&gt;', $this->form_validation->encode_php_tags('?>'));
576 }
577
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200578 /**
579 * Run rules
580 *
581 * Helper method to set rules and run them at once, not
582 * an actual test case.
583 */
David Woodscf776712015-03-22 15:55:16 -0700584 public function run_rules($rules, $values)
David Woods64af3bb2015-03-18 10:09:26 -0700585 {
David Woods64af3bb2015-03-18 10:09:26 -0700586 $this->form_validation->reset_validation();
David Woodscf776712015-03-22 15:55:16 -0700587 $_POST = array();
David Woods5b884732015-03-18 10:37:35 -0700588
David Woodscf776712015-03-22 15:55:16 -0700589 $this->form_validation->set_rules($rules);
590 foreach ($values as $field => $value)
591 {
592 $_POST[$field] = $value;
593 }
Andrey Andreev3e2045b2015-03-26 12:34:38 +0200594
David Woods64af3bb2015-03-18 10:09:26 -0700595 return $this->form_validation->run();
596 }
David Woods5bc1dc52015-03-27 22:41:58 -0700597
David Woodsf3ac71e2015-03-16 20:00:21 -0700598}