blob: bfc2083fde7794d56dc1ad841839949fd76a95be [file] [log] [blame]
David Woodsf3ac71e2015-03-16 20:00:21 -07001<?php
2
3class Form_validation_test extends CI_TestCase {
4
5 public function set_up()
6 {
David Woodsc7029e22015-03-17 10:52:01 -07007 $_SERVER['REQUEST_METHOD'] = 'POST';
8
David Woodsf3ac71e2015-03-16 20:00:21 -07009 // Create a mock loader since load->helper() looks in the wrong directories for unit tests,
10 // We'll use CI_TestCase->helper() instead
11 $ldr = $this->getMock('CI_Loader', array('helper'));
12 // At current, CI_Form_Validation only calls load->helper("form")
13 // Assert this so that if that changes this fails fast
14 $ldr->expects($this->once())
15 ->method('helper')
16 ->with($this->equalTo('form'));
17
David Woodsdc1ae6b2015-03-16 23:36:54 -070018 // Same applies for lang
19 $lang = $this->getMock('CI_Lang', array('load'));
20
21 // Setup CI_Input
David Woodsdc1ae6b2015-03-16 23:36:54 -070022 // Set config for Input class
23 $this->ci_set_config('allow_get_array', TRUE);
24 $this->ci_set_config('global_xss_filtering', FALSE);
25 $this->ci_set_config('csrf_protection', FALSE);
26
27 $security = new Mock_Core_Security();
28
29 $this->ci_set_config('charset', 'UTF-8');
30 $utf8 = new Mock_Core_Utf8();
31
32 $inp = new Mock_Core_Input($security, $utf8);
33
34 $this->ci_instance_var('lang', $lang);
David Woodsf3ac71e2015-03-16 20:00:21 -070035 $this->ci_instance_var('load', $ldr);
David Woodsdc1ae6b2015-03-16 23:36:54 -070036 $this->ci_instance_var('input', $inp);
37
38 $this->lang('form_validation');
David Woodsf3ac71e2015-03-16 20:00:21 -070039 $this->helper('form');
David Woodsc7029e22015-03-17 10:52:01 -070040
41 $this->form_validation = new CI_Form_validation();
David Woodsf3ac71e2015-03-16 20:00:21 -070042 }
43
44 public function test___construct()
45 {
David Woodsf3ac71e2015-03-16 20:00:21 -070046 $this->assertNotNull($this->form_validation);
47 }
48
David Woodsc7029e22015-03-17 10:52:01 -070049 public function test_rule_required()
David Woodsf3ac71e2015-03-16 20:00:21 -070050 {
David Woodsc7029e22015-03-17 10:52:01 -070051 $this->assertTrue($this->run_rule('required', ' someValue'));
David Woodsf3ac71e2015-03-16 20:00:21 -070052
David Woodsc7029e22015-03-17 10:52:01 -070053 $this->assertFalse($this->run_rule('required', ''));
54 $this->assertFalse($this->run_rule('required', ' '));
David Woodsf3ac71e2015-03-16 20:00:21 -070055 }
56
David Woodsc7029e22015-03-17 10:52:01 -070057 public function test_rule_matches()
David Woodsdc1ae6b2015-03-16 23:36:54 -070058 {
David Woodsc7029e22015-03-17 10:52:01 -070059 // Empty input should pass any rule unless required is also specified
60 $this->assertTrue($this->run_rule('matches[sample]', ''));
61 $this->assertTrue($this->run_rule('matches[s]', 's'));
David Woodsdc1ae6b2015-03-16 23:36:54 -070062
David Woodsc7029e22015-03-17 10:52:01 -070063 $this->assertFalse($this->run_rule('matches[Sample]', 'sample'));
64 $this->assertFalse($this->run_rule('matches[sample]', ' sample'));
David Woodsdc1ae6b2015-03-16 23:36:54 -070065 }
David Woodsc7029e22015-03-17 10:52:01 -070066
67 public function test_rule_differs()
68 {
69 // Empty input should pass any rule unless required is also specified
70 $this->assertTrue($this->run_rule('differs[sample]', ''));
71 $this->assertTrue($this->run_rule('differs[sample]', 'Sample'));
72 $this->assertTrue($this->run_rule('differs[sample]', ' sample'));
73
74 $this->assertFalse($this->run_rule('differs[sample]', 'sample'));
75 }
76
77 public function test_rule_min_length()
78 {
79 // Empty input should pass any rule unless required is also specified
80 $this->assertTrue($this->run_rule('min_length[34]', ''));
81 $this->assertTrue($this->run_rule('min_length[2]', '12'));
82 $this->assertTrue($this->run_rule('min_length[2]', ' 2'));
83
84 $this->assertFalse($this->run_rule('min_length[2]', '1'));
85 $this->assertFalse($this->run_rule('min_length[4]|required', ''));
86 }
87
88 public function test_rule_max_length()
89 {
90 // Empty input should pass any rule unless required is also specified
91 $this->assertTrue($this->run_rule('max_length[4]', ''));
92 $this->assertTrue($this->run_rule('max_length[4]', '1234'));
93
94 $this->assertFalse($this->run_rule('max_length[4]', '12345'));
95 }
96
97 public function test_rule_exact_length()
98 {
99 // Empty input should pass any rule unless required is also specified
100 $this->assertTrue($this->run_rule('exact_length[4]', ''));
101 $this->assertTrue($this->run_rule('exact_length[4]', '1234'));
102
103 $this->assertFalse($this->run_rule('exact_length[4]', '123'));
104 $this->assertFalse($this->run_rule('exact_length[4]', '12345'));
105 }
106
107 public function test_rule_greater_than()
108 {
109 // Empty input should pass any rule unless required is also specified
110 $this->assertTrue($this->run_rule('greater_than[-10]', ''));
111 $this->assertTrue($this->run_rule('greater_than[-10]', '-9'));
112
113 $this->assertFalse($this->run_rule('greater_than[-10]', '-99'));
114 $this->assertFalse($this->run_rule('greater_than[-10]', 'a'));
115 }
116
117 public function test_rule_greater_than_equal_to()
118 {
119 // Empty input should pass any rule unless required is also specified
120 $this->assertTrue($this->run_rule('greater_than_equal_to[0]', ''));
121 $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '0'));
122 $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '1'));
123
124 $this->assertFalse($this->run_rule('greater_than_equal_to[0]', '-1'));
125 $this->assertFalse($this->run_rule('greater_than_equal_to[0]', 'a'));
126 }
127
128 public function test_rule_less_than()
129 {
130 // Empty input should pass any rule unless required is also specified
131 $this->assertTrue($this->run_rule('less_than[0]', ''));
132 $this->assertTrue($this->run_rule('less_than[0]', '-1'));
133
134 $this->assertFalse($this->run_rule('less_than[0]', '0'));
135 $this->assertFalse($this->run_rule('less_than[0]', 'a'));
136 }
137
138 public function test_rule_less_than_equal_to()
139 {
140 // Empty input should pass any rule unless required is also specified
141 $this->assertTrue($this->run_rule('less_than_equal_to[0]', ''));
142 $this->assertTrue($this->run_rule('less_than_equal_to[0]', '-1'));
143 $this->assertTrue($this->run_rule('less_than_equal_to[0]', '0'));
144
145 $this->assertFalse($this->run_rule('less_than_equal_to[0]', '1'));
146 $this->assertFalse($this->run_rule('less_than_equal_to[0]', 'a'));
147 }
148
149 public function test_rule_in_list()
150 {
151 // Empty input should pass any rule unless required is also specified
152 $this->assertTrue($this->run_rule('in_list[red,Blue,123]', ''));
153 $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'red'));
154 $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'Blue'));
155 $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '123'));
156
157 $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'Red'));
158 $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'blue'));
159 $this->assertFalse($this->run_rule('in_list[red,Blue,123]', ' red'));
160 }
161
162 public function test_rule_alpha()
163 {
164 // Empty input should pass any rule unless required is also specified
165 $this->assertTrue($this->run_rule('alpha', ''));
166 $this->assertTrue($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ'));
167
168 $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ '));
169 $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1'));
170 $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*'));
171 }
172
173 public function test_rule_alpha_numeric()
174 {
175 // Empty input should pass any rule unless required is also specified
176 $this->assertTrue($this->run_rule('alpha_numeric', ''));
177 $this->assertTrue($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789'));
178
179 $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ '));
180 $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_'));
181 }
182
183 public function test_rule_alpha_numeric_spaces()
184 {
185 // Empty input should pass any rule unless required is also specified
186 $this->assertTrue($this->run_rule('alpha_numeric_spaces', ''));
187 $this->assertTrue($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789'));
188
189 $this->assertFalse($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_'));
190 }
191
192 public function test_rule_alpha_dash()
193 {
194 // Empty input should pass any rule unless required is also specified
195 $this->assertTrue($this->run_rule('alpha_dash', ''));
196 $this->assertTrue($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_'));
197
198 $this->assertFalse($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ '));
199 }
200
201 public function test_rule_numeric()
202 {
203 // Empty input should pass any rule unless required is also specified
204 $this->assertTrue($this->run_rule('numeric', ''));
205 $this->assertTrue($this->run_rule('numeric', '0'));
206 $this->assertTrue($this->run_rule('numeric', '12314'));
207
208 $this->assertFalse($this->run_rule('numeric', '123a'));
209 }
210
211 public function test_rule_integer()
212 {
213 // Empty input should pass any rule unless required is also specified
214 $this->assertTrue($this->run_rule('integer', ''));
215 $this->assertTrue($this->run_rule('integer', '0'));
216 $this->assertTrue($this->run_rule('integer', '42'));
217
218 $this->assertFalse($this->run_rule('integer', '124a'));
219 $this->assertFalse($this->run_rule('integer', '1.9'));
220 }
221
222 public function test_rule_decimal()
223 {
224 // Empty input should pass any rule unless required is also specified
225 $this->assertTrue($this->run_rule('decimal', ''));
226 $this->assertTrue($this->run_rule('decimal', '1.0'));
227 $this->assertTrue($this->run_rule('decimal', '0.98'));
228
229 $this->assertFalse($this->run_rule('decimal', '1.0a'));
230 $this->assertFalse($this->run_rule('decimal', '-i'));
231 }
232
233 public function test_rule_is_natural()
234 {
235 // Empty input should pass any rule unless required is also specified
236 $this->assertTrue($this->run_rule('is_natural', ''));
237 $this->assertTrue($this->run_rule('is_natural', '0'));
238 $this->assertTrue($this->run_rule('is_natural', '12'));
239
240 $this->assertFalse($this->run_rule('is_natural', '42a'));
241 }
242
243 public function test_rule_is_natural_no_zero()
244 {
245 // Empty input should pass any rule unless required is also specified
246 $this->assertTrue($this->run_rule('is_natural_no_zero', ''));
247 $this->assertTrue($this->run_rule('is_natural_no_zero', '42'));
248
249 $this->assertFalse($this->run_rule('is_natural_no_zero', '0'));
250 $this->assertFalse($this->run_rule('is_natural_no_zero', '42a'));
251 }
252
253 public function test_rule_valid_url()
254 {
255 // Empty input should pass any rule unless required is also specified
256 $this->assertTrue($this->run_rule('valid_url', ''));
257 $this->assertTrue($this->run_rule('valid_url', 'www.codeigniter.com'));
258 $this->assertTrue($this->run_rule('valid_url', 'http://codeigniter.eu'));
259
260 $this->assertFalse($this->run_rule('valid_url', 'codeigniter.c'));
261 }
262
263 public function test_rule_valid_email()
264 {
265 // Empty input should pass any rule unless required is also specified
266 $this->assertTrue($this->run_rule('valid_email', ''));
267 $this->assertTrue($this->run_rule('valid_email', 'email@sample.com'));
268
269 $this->assertFalse($this->run_rule('valid_email', '@sample.com'));
270 }
271
272 public function test_rule_valid_emails()
273 {
274 // Empty input should pass any rule unless required is also specified
275 $this->assertTrue($this->run_rule('valid_emails', ''));
276 $this->assertTrue($this->run_rule('valid_emails', '1@sample.com,2@sample.com'));
277
278 $this->assertFalse($this->run_rule('valid_emails', '@sample.com,2@sample.com,validemail@email.ca'));
279 }
280
281 public function test_rule_valid_ip()
282 {
283 // Empty input should pass any rule unless required is also specified
284 $this->assertTrue($this->run_rule('valid_ip', ''));
285 $this->assertTrue($this->run_rule('valid_ip', '127.0.0.1'));
286 $this->assertTrue($this->run_rule('valid_ip[ipv4]', '127.0.0.1'));
287 $this->assertTrue($this->run_rule('valid_ip', '2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
288 $this->assertTrue($this->run_rule('valid_ip[ipv6]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
289
290 $this->assertFalse($this->run_rule('valid_ip[ipv4]', '2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
291 $this->assertFalse($this->run_rule('valid_ip[ipv6]', '127.0.0.1'));
292 $this->assertFalse($this->run_rule('valid_ip', 'H001:0db8:85a3:0000:0000:8a2e:0370:7334'));
293 $this->assertFalse($this->run_rule('valid_ip', '127.0.0.259'));
294 }
295
296 public function test_rule_valid_base64()
297 {
298 // Empty input should pass any rule unless required is also specified
299 $this->assertTrue($this->run_rule('valid_base64', ''));
300 $this->assertTrue($this->run_rule('valid_base64', 'string'));
301 }
302
303 public function run_rule($rule, $test_value)
304 {
305// $this->markTestSkipped('Not designed to be a unit test');
306 // Reset the _$POST array
307 $_POST = array();
308 $this->form_validation->reset_validation();
309
310 $this->form_validation->set_rules('field', 'name', $rule);
311 $_POST['field'] = $test_value;
312 return $this->form_validation->run();
313 }
David Woodsf3ac71e2015-03-16 20:00:21 -0700314}