blob: ccdc64785c50aa886ca8730621357ca944134b44 [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'));
David Woods64af3bb2015-03-18 10:09:26 -070012 // At current, CI_Form_Validation only calls load->helper("form")
13 // Assert this so that if that changes this fails fast
David Woods317cad92015-03-20 22:32:24 -070014 $loader->method('helper')
David Woods5b884732015-03-18 10:37:35 -070015 ->with($this->equalTo('form'));
David Woods64af3bb2015-03-18 10:09:26 -070016 // Same applies for lang
David Woods5b884732015-03-18 10:37:35 -070017 $lang = $this->getMock('CI_Lang', array('load'));
David Woodsdc1ae6b2015-03-16 23:36:54 -070018
David Woods64af3bb2015-03-18 10:09:26 -070019 $this->ci_set_config('charset', 'UTF-8');
20 $utf8 = new Mock_Core_Utf8();
21 $security = new Mock_Core_Security();
22 $input = new Mock_Core_Input($security, $utf8);
David Woodsdc1ae6b2015-03-16 23:36:54 -070023
David Woods64af3bb2015-03-18 10:09:26 -070024 $this->ci_instance_var('lang', $lang);
25 $this->ci_instance_var('load', $loader);
26 $this->ci_instance_var('input', $input);
David Woodsdc1ae6b2015-03-16 23:36:54 -070027
David Woods64af3bb2015-03-18 10:09:26 -070028 $this->lang('form_validation');
29 $this->helper('form');
30
31 $this->form_validation = new CI_Form_validation();
32 }
33
34 public function test___construct()
35 {
David Woods317cad92015-03-20 22:32:24 -070036 $form_validation = new CI_Form_validation();
37 $this->assertNotNull($form_validation);
38 }
David Woods64af3bb2015-03-18 10:09:26 -070039
40 public function test_rule_required()
41 {
David Woodscf776712015-03-22 15:55:16 -070042 $rules = array(array('field' => 'foo', 'label' => 'foo_label', 'rules' => 'required'));
43 $this->assertTrue($this->run_rules($rules, array('foo' => 'bar')));
David Woods64af3bb2015-03-18 10:09:26 -070044
David Woodscf776712015-03-22 15:55:16 -070045 $this->assertFalse($this->run_rules($rules, array('foo' => '')));
46 $this->assertFalse($this->run_rules($rules, array('foo' => ' ')));
David Woods64af3bb2015-03-18 10:09:26 -070047 }
48
49 public function test_rule_matches()
David Woodscf776712015-03-22 15:55:16 -070050 {
51 $rules = array(
52 array('field' => 'foo', 'label' => 'label', 'rules' => 'required'),
53 array('field' => 'bar', 'label' => 'label2', 'rules' => 'matches[foo]'));
54 $values_base = array('foo' => 'sample');
55
56 $this->assertTrue($this->run_rules(
57 $rules,
58 array_merge($values_base, array('bar' => ''))
59 ));
60 $this->assertTrue($this->run_rules(
61 $rules,
62 array_merge($values_base, array('bar' => 'sample'))
63 ));
David Woods64af3bb2015-03-18 10:09:26 -070064
David Woodscf776712015-03-22 15:55:16 -070065 $this->assertFalse($this->run_rules(
66 $rules,
67 array_merge($values_base, array('bar' => 'Sample'))
68 ));
69 $this->assertFalse($this->run_rules(
70 $rules,
71 array_merge($values_base, array('bar' => ' sample'))
72 ));
David Woods64af3bb2015-03-18 10:09:26 -070073 }
74
75 public function test_rule_differs()
David Woodscf776712015-03-22 15:55:16 -070076 {
77 $rules = array(
78 array('field' => 'foo', 'label' => 'label', 'rules' => 'required'),
79 array('field' => 'bar', 'label' => 'label2', 'rules' => 'differs[foo]'));
80 $values_base = array('foo' => 'sample');
81
82 $this->assertTrue($this->run_rules(
83 $rules,
84 array_merge($values_base, array('bar' => 'does_not_match'))
85 ));
86 $this->assertTrue($this->run_rules(
87 $rules,
88 array_merge($values_base, array('bar' => 'Sample'))
89 ));
90 $this->assertTrue($this->run_rules(
91 $rules,
92 array_merge($values_base, array('bar' => ' sample'))
93 ));
David Woods64af3bb2015-03-18 10:09:26 -070094
David Woodscf776712015-03-22 15:55:16 -070095 $this->assertFalse($this->run_rules(
96 $rules,
97 array_merge($values_base, array('bar' => 'sample'))
98 ));
David Woods64af3bb2015-03-18 10:09:26 -070099 }
100
101 public function test_rule_min_length()
David Woods317cad92015-03-20 22:32:24 -0700102 {
103 $this->assertTrue($this->form_validation->min_length('12345', '5'));
104 $this->assertTrue($this->form_validation->min_length('test', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700105
David Woods317cad92015-03-20 22:32:24 -0700106 $this->assertFalse($this->form_validation->min_length('123', '4'));
107 $this->assertFalse($this->form_validation->min_length('should_fail', 'A'));
108 $this->assertFalse($this->form_validation->min_length('', '4'));
David Woods64af3bb2015-03-18 10:09:26 -0700109 }
110
111 public function test_rule_max_length()
David Woods317cad92015-03-20 22:32:24 -0700112 {
113 $this->assertTrue($this->form_validation->max_length('', '4'));
114 $this->assertTrue($this->form_validation->max_length('1234', '4'));
David Woods64af3bb2015-03-18 10:09:26 -0700115
David Woods317cad92015-03-20 22:32:24 -0700116 $this->assertFalse($this->form_validation->max_length('12345', '4'));
117 $this->assertFalse($this->form_validation->max_length('should_fail', 'A'));
David Woods64af3bb2015-03-18 10:09:26 -0700118 }
119
120 public function test_rule_exact_length()
David Woods317cad92015-03-20 22:32:24 -0700121 {
122 $this->assertTrue($this->form_validation->exact_length('1234', '4'));
David Woods64af3bb2015-03-18 10:09:26 -0700123
David Woods317cad92015-03-20 22:32:24 -0700124 $this->assertFalse($this->form_validation->exact_length('', '3'));
125 $this->assertFalse($this->form_validation->exact_length('12345', '4'));
126 $this->assertFalse($this->form_validation->exact_length('123', '4'));
127 $this->assertFalse($this->form_validation->exact_length('should_fail', 'A'));
David Woods64af3bb2015-03-18 10:09:26 -0700128 }
129
130 public function test_rule_greater_than()
131 {
132 // Empty input should pass any rule unless required is also specified
David Woods317cad92015-03-20 22:32:24 -0700133 $this->assertTrue($this->form_validation->greater_than('-10', '-11'));
134 $this->assertTrue($this->form_validation->greater_than('10', '9'));
David Woods64af3bb2015-03-18 10:09:26 -0700135
David Woods317cad92015-03-20 22:32:24 -0700136 $this->assertFalse($this->form_validation->greater_than('10', '10'));
137 $this->assertFalse($this->form_validation->greater_than('10', 'a'));
138 $this->assertFalse($this->form_validation->greater_than('10a', '10'));
David Woods64af3bb2015-03-18 10:09:26 -0700139 }
140
141 public function test_rule_greater_than_equal_to()
David Woods317cad92015-03-20 22:32:24 -0700142 {
143 $this->assertTrue($this->form_validation->greater_than_equal_to('0', '0'));
144 $this->assertTrue($this->form_validation->greater_than_equal_to('1', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700145
David Woods317cad92015-03-20 22:32:24 -0700146 $this->assertFalse($this->form_validation->greater_than_equal_to('-1', '0'));
147 $this->assertFalse($this->form_validation->greater_than_equal_to('10a', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700148 }
149
150 public function test_rule_less_than()
David Woods317cad92015-03-20 22:32:24 -0700151 {
152 $this->assertTrue($this->form_validation->less_than('4', '5'));
153 $this->assertTrue($this->form_validation->less_than('-1', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700154
David Woods317cad92015-03-20 22:32:24 -0700155 $this->assertFalse($this->form_validation->less_than('4', '4'));
156 $this->assertFalse($this->form_validation->less_than('10a', '5'));
David Woods64af3bb2015-03-18 10:09:26 -0700157 }
158
159 public function test_rule_less_than_equal_to()
David Woods317cad92015-03-20 22:32:24 -0700160 {
161 $this->assertTrue($this->form_validation->less_than_equal_to('-1', '0'));
162 $this->assertTrue($this->form_validation->less_than_equal_to('-1', '-1'));
163 $this->assertTrue($this->form_validation->less_than_equal_to('4', '4'));
David Woods64af3bb2015-03-18 10:09:26 -0700164
David Woods317cad92015-03-20 22:32:24 -0700165 $this->assertFalse($this->form_validation->less_than_equal_to('0', '-1'));
166 $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0'));
David Woods64af3bb2015-03-18 10:09:26 -0700167 }
168
169 public function test_rule_in_list()
David Woods317cad92015-03-20 22:32:24 -0700170 {
171 $this->assertTrue($this->form_validation->in_list('red', 'red,Blue,123'));
172 $this->assertTrue($this->form_validation->in_list('Blue', 'red,Blue,123'));
173 $this->assertTrue($this->form_validation->in_list('123', 'red,Blue,123'));
David Woods64af3bb2015-03-18 10:09:26 -0700174
David Woods317cad92015-03-20 22:32:24 -0700175 $this->assertFalse($this->form_validation->in_list('Red', 'red,Blue,123'));
176 $this->assertFalse($this->form_validation->in_list(' red', 'red,Blue,123'));
177 $this->assertFalse($this->form_validation->in_list('1234', 'red,Blue,123'));
David Woods64af3bb2015-03-18 10:09:26 -0700178 }
179
180 public function test_rule_alpha()
David Woods317cad92015-03-20 22:32:24 -0700181 {
182 $this->assertTrue($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ'));
David Woods64af3bb2015-03-18 10:09:26 -0700183
David Woods317cad92015-03-20 22:32:24 -0700184 $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ '));
185 $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1'));
186 $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*'));
David Woods64af3bb2015-03-18 10:09:26 -0700187 }
188
189 public function test_rule_alpha_numeric()
David Woods317cad92015-03-20 22:32:24 -0700190 {
191 $this->assertTrue($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789'));
David Woods64af3bb2015-03-18 10:09:26 -0700192
David Woods317cad92015-03-20 22:32:24 -0700193 $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ '));
194 $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_'));
David Woods64af3bb2015-03-18 10:09:26 -0700195 }
196
197 public function test_rule_alpha_numeric_spaces()
David Woods317cad92015-03-20 22:32:24 -0700198 {
199 $this->assertTrue($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789'));
David Woods64af3bb2015-03-18 10:09:26 -0700200
David Woods317cad92015-03-20 22:32:24 -0700201 $this->assertFalse($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_'));
David Woods64af3bb2015-03-18 10:09:26 -0700202 }
203
204 public function test_rule_alpha_dash()
David Woods317cad92015-03-20 22:32:24 -0700205 {
206 $this->assertTrue($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_'));
David Woods64af3bb2015-03-18 10:09:26 -0700207
David Woods317cad92015-03-20 22:32:24 -0700208 $this->assertFalse($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ '));
David Woods64af3bb2015-03-18 10:09:26 -0700209 }
210
211 public function test_rule_numeric()
David Woods317cad92015-03-20 22:32:24 -0700212 {
213 $this->assertTrue($this->form_validation->numeric('0'));
214 $this->assertTrue($this->form_validation->numeric('12314'));
215 $this->assertTrue($this->form_validation->numeric('-42'));
David Woods64af3bb2015-03-18 10:09:26 -0700216
David Woods317cad92015-03-20 22:32:24 -0700217 $this->assertFalse($this->form_validation->numeric('123a'));
218 $this->assertFalse($this->form_validation->numeric('--1'));
David Woods64af3bb2015-03-18 10:09:26 -0700219 }
220
221 public function test_rule_integer()
David Woods317cad92015-03-20 22:32:24 -0700222 {
223 $this->assertTrue($this->form_validation->integer('0'));
224 $this->assertTrue($this->form_validation->integer('42'));
225 $this->assertTrue($this->form_validation->integer('-1'));
David Woods64af3bb2015-03-18 10:09:26 -0700226
David Woods317cad92015-03-20 22:32:24 -0700227 $this->assertFalse($this->form_validation->integer('124a'));
228 $this->assertFalse($this->form_validation->integer('1.9'));
229 $this->assertFalse($this->form_validation->integer('--1'));
David Woods64af3bb2015-03-18 10:09:26 -0700230 }
231
232 public function test_rule_decimal()
David Woods317cad92015-03-20 22:32:24 -0700233 {
234 $this->assertTrue($this->form_validation->decimal('1.0'));
235 $this->assertTrue($this->form_validation->decimal('-0.98'));
David Woods64af3bb2015-03-18 10:09:26 -0700236
David Woods317cad92015-03-20 22:32:24 -0700237 $this->assertFalse($this->form_validation->decimal('0'));
238 $this->assertFalse($this->form_validation->decimal('1.0a'));
239 $this->assertFalse($this->form_validation->decimal('-i'));
240 $this->assertFalse($this->form_validation->decimal('--1'));
David Woods64af3bb2015-03-18 10:09:26 -0700241 }
242
243 public function test_rule_is_natural()
David Woods317cad92015-03-20 22:32:24 -0700244 {
245 $this->assertTrue($this->form_validation->is_natural('0'));
246 $this->assertTrue($this->form_validation->is_natural('12'));
David Woods64af3bb2015-03-18 10:09:26 -0700247
David Woods317cad92015-03-20 22:32:24 -0700248 $this->assertFalse($this->form_validation->is_natural('42a'));
249 $this->assertFalse($this->form_validation->is_natural('-1'));
David Woods64af3bb2015-03-18 10:09:26 -0700250 }
251
252 public function test_rule_is_natural_no_zero()
David Woods317cad92015-03-20 22:32:24 -0700253 {
254 $this->assertTrue($this->form_validation->is_natural_no_zero('42'));
David Woods64af3bb2015-03-18 10:09:26 -0700255
David Woods317cad92015-03-20 22:32:24 -0700256 $this->assertFalse($this->form_validation->is_natural_no_zero('0'));
257 $this->assertFalse($this->form_validation->is_natural_no_zero('42a'));
258 $this->assertFalse($this->form_validation->is_natural_no_zero('-1'));
David Woods64af3bb2015-03-18 10:09:26 -0700259 }
260
261 public function test_rule_valid_url()
David Woods317cad92015-03-20 22:32:24 -0700262 {
263 $this->assertTrue($this->form_validation->valid_url('www.codeigniter.com'));
264 $this->assertTrue($this->form_validation->valid_url('http://codeigniter.eu'));
265
266 $this->assertFalse($this->form_validation->valid_url('htt://www.codeIgniter.com'));
267 $this->assertFalse($this->form_validation->valid_url(''));
268 $this->assertFalse($this->form_validation->valid_url('code igniter'));
David Woods64af3bb2015-03-18 10:09:26 -0700269 }
270
271 public function test_rule_valid_email()
David Woods317cad92015-03-20 22:32:24 -0700272 {
273 $this->assertTrue($this->form_validation->valid_email('email@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700274
David Woods317cad92015-03-20 22:32:24 -0700275 $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700276 }
277
278 public function test_rule_valid_emails()
David Woods317cad92015-03-20 22:32:24 -0700279 {
280 $this->assertTrue($this->form_validation->valid_emails('1@sample.com,2@sample.com'));
281 $this->assertTrue($this->form_validation->valid_emails('email@sample.com'));
David Woods64af3bb2015-03-18 10:09:26 -0700282
David Woods317cad92015-03-20 22:32:24 -0700283 $this->assertFalse($this->form_validation->valid_emails('valid_email', '@sample.com'));
284 $this->assertFalse($this->form_validation->valid_emails('@sample.com,2@sample.com,validemail@email.ca'));
David Woods64af3bb2015-03-18 10:09:26 -0700285 }
286
287 public function test_rule_valid_ip()
David Woods317cad92015-03-20 22:32:24 -0700288 {
289 $this->assertTrue($this->form_validation->valid_ip('127.0.0.1'));
290 $this->assertTrue($this->form_validation->valid_ip('127.0.0.1', 'ipv4'));
291 $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
292 $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv6'));
David Woods64af3bb2015-03-18 10:09:26 -0700293
David Woods317cad92015-03-20 22:32:24 -0700294 $this->assertFalse($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv4'));
295 $this->assertFalse($this->form_validation->valid_ip('127.0.0.1', 'ipv6'));
296 $this->assertFalse($this->form_validation->valid_ip('H001:0db8:85a3:0000:0000:8a2e:0370:7334'));
297 $this->assertFalse($this->form_validation->valid_ip('127.0.0.259'));
David Woods64af3bb2015-03-18 10:09:26 -0700298 }
299
300 public function test_rule_valid_base64()
David Woods317cad92015-03-20 22:32:24 -0700301 {
302 $this->assertTrue($this->form_validation->valid_base64(base64_encode('string')));
David Woods5b884732015-03-18 10:37:35 -0700303
David Woods317cad92015-03-20 22:32:24 -0700304 $this->assertFalse($this->form_validation->valid_base64('FA08GG'));
305 }
306
307 public function test_set_data()
308 {
309 // Reset test environment
310 $_POST = array();
311 $this->form_validation->reset_validation();
David Woods317cad92015-03-20 22:32:24 -0700312 $data = array('field' => 'some_data');
313 $this->form_validation->set_data($data);
314 $this->form_validation->set_rules('field', 'label', 'required');
315 $this->assertTrue($this->form_validation->run());
316
317 // Test with empty array
318 $_POST = array();
David Woods317cad92015-03-20 22:32:24 -0700319 $this->form_validation->reset_validation();
David Woodscf776712015-03-22 15:55:16 -0700320 $data = array('field' => 'some_data');
David Woods317cad92015-03-20 22:32:24 -0700321 $this->form_validation->set_data($data);
David Woodscf776712015-03-22 15:55:16 -0700322 // This should do nothing. Old data will still be used
323 $this->form_validation->set_data(array());
David Woods317cad92015-03-20 22:32:24 -0700324 $this->form_validation->set_rules('field', 'label', 'required');
David Woodscf776712015-03-22 15:55:16 -0700325 $this->assertTrue($this->form_validation->run());
David Woods317cad92015-03-20 22:32:24 -0700326 }
327
328 public function test_set_message()
329 {
330 // Reset test environment
331 $_POST = array();
332 $this->form_validation->reset_validation();
333 $err_message = 'What a terrible error!';
334 $rules = array(
335 array(
336 'field' => 'req_field',
337 'label' => 'label',
338 'rules' => 'required'
339 )
340 );
341 $errorless_data = array('req_field' => 'some text');
342 $erroneous_data = array('req_field' => '');
343
344 $this->form_validation->set_message('required', $err_message);
345 $this->form_validation->set_data($erroneous_data);
346 $this->form_validation->set_rules($rules);
347 $this->form_validation->run();
348 $this->assertEquals('<p>'.$err_message.'</p>', $this->form_validation->error('req_field'));
349
350 $this->form_validation->reset_validation();
351 $this->form_validation->set_message('required', $err_message);
352 $this->form_validation->set_data($errorless_data);
353 $this->form_validation->set_rules($rules);
354 $this->form_validation->run();
355 $this->assertEquals('', $this->form_validation->error('req_field'));
David Woods64af3bb2015-03-18 10:09:26 -0700356 }
357
David Woodscf776712015-03-22 15:55:16 -0700358 public function run_rules($rules, $values)
David Woods64af3bb2015-03-18 10:09:26 -0700359 {
David Woodsc7029e22015-03-17 10:52:01 -0700360// $this->markTestSkipped('Not designed to be a unit test');
David Woods64af3bb2015-03-18 10:09:26 -0700361 $this->form_validation->reset_validation();
David Woodscf776712015-03-22 15:55:16 -0700362 $_POST = array();
David Woods5b884732015-03-18 10:37:35 -0700363
David Woodscf776712015-03-22 15:55:16 -0700364 $this->form_validation->set_rules($rules);
365 foreach ($values as $field => $value)
366 {
367 $_POST[$field] = $value;
368 }
369
David Woods64af3bb2015-03-18 10:09:26 -0700370 return $this->form_validation->run();
371 }
372
David Woodsf3ac71e2015-03-16 20:00:21 -0700373}