David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Form_validation_test extends CI_TestCase { |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame^] | 4 | |
| 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 |
| 11 | $loader = $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 | $loader->expects($this->once()) |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 15 | ->method('helper') |
| 16 | ->with($this->equalTo('form')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame^] | 17 | // Same applies for lang |
| 18 | $lang = $this->getMock('CI_Lang', array('load')); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 19 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame^] | 20 | $this->ci_set_config('charset', 'UTF-8'); |
| 21 | $utf8 = new Mock_Core_Utf8(); |
| 22 | $security = new Mock_Core_Security(); |
| 23 | $input = new Mock_Core_Input($security, $utf8); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 24 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame^] | 25 | $this->ci_instance_var('lang', $lang); |
| 26 | $this->ci_instance_var('load', $loader); |
| 27 | $this->ci_instance_var('input', $input); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 28 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame^] | 29 | $this->lang('form_validation'); |
| 30 | $this->helper('form'); |
| 31 | |
| 32 | $this->form_validation = new CI_Form_validation(); |
| 33 | } |
| 34 | |
| 35 | public function test___construct() |
| 36 | { |
| 37 | $this->assertNotNull($this->form_validation); |
| 38 | } |
| 39 | |
| 40 | public function test_rule_required() |
| 41 | { |
| 42 | $this->assertTrue($this->run_rule('required', ' someValue')); |
| 43 | |
| 44 | $this->assertFalse($this->run_rule('required', '')); |
| 45 | $this->assertFalse($this->run_rule('required', ' ')); |
| 46 | } |
| 47 | |
| 48 | public function test_rule_matches() |
| 49 | { |
| 50 | // Empty input should pass any rule unless required is also specified |
| 51 | $_POST['to_match'] = 'sample'; |
| 52 | $this->assertTrue($this->run_rule('matches[to_match]', '')); |
| 53 | $_POST['to_match'] = 'sample'; |
| 54 | $this->assertTrue($this->run_rule('matches[to_match]', 'sample')); |
| 55 | |
| 56 | $_POST['to_match'] = 'sample'; |
| 57 | $this->assertFalse($this->run_rule('matches[to_match]', 'Sample')); |
| 58 | $_POST['to_match'] = 'sample'; |
| 59 | $this->assertFalse($this->run_rule('matches[to_match]', ' sample')); |
| 60 | } |
| 61 | |
| 62 | public function test_rule_differs() |
| 63 | { |
| 64 | // Empty input should pass any rule unless required is also specified |
| 65 | $_POST['to_differ'] = 'sample'; |
| 66 | $this->assertTrue($this->run_rule('differs[to_differ]', '')); |
| 67 | $_POST['to_differ'] = 'sample'; |
| 68 | $this->assertTrue($this->run_rule('differs[to_differ]', 'Sample')); |
| 69 | $_POST['to_differ'] = 'sample'; |
| 70 | $this->assertTrue($this->run_rule('differs[to_differ]', ' sample')); |
| 71 | |
| 72 | $_POST['to_differ'] = 'sample'; |
| 73 | $this->assertFalse($this->run_rule('differs[to_differ]', 'sample')); |
| 74 | } |
| 75 | |
| 76 | public function test_rule_min_length() |
| 77 | { |
| 78 | // Empty input should pass any rule unless required is also specified |
| 79 | $this->assertTrue($this->run_rule('min_length[34]', '')); |
| 80 | $this->assertTrue($this->run_rule('min_length[2]', '12')); |
| 81 | $this->assertTrue($this->run_rule('min_length[2]', ' 2')); |
| 82 | |
| 83 | $this->assertFalse($this->run_rule('min_length[2]', '1')); |
| 84 | $this->assertFalse($this->run_rule('min_length[4]|required', '')); |
| 85 | } |
| 86 | |
| 87 | public function test_rule_max_length() |
| 88 | { |
| 89 | // Empty input should pass any rule unless required is also specified |
| 90 | $this->assertTrue($this->run_rule('max_length[4]', '')); |
| 91 | $this->assertTrue($this->run_rule('max_length[4]', '1234')); |
| 92 | |
| 93 | $this->assertFalse($this->run_rule('max_length[4]', '12345')); |
| 94 | } |
| 95 | |
| 96 | public function test_rule_exact_length() |
| 97 | { |
| 98 | // Empty input should pass any rule unless required is also specified |
| 99 | $this->assertTrue($this->run_rule('exact_length[4]', '')); |
| 100 | $this->assertTrue($this->run_rule('exact_length[4]', '1234')); |
| 101 | |
| 102 | $this->assertFalse($this->run_rule('exact_length[4]', '123')); |
| 103 | $this->assertFalse($this->run_rule('exact_length[4]', '12345')); |
| 104 | } |
| 105 | |
| 106 | public function test_rule_greater_than() |
| 107 | { |
| 108 | // Empty input should pass any rule unless required is also specified |
| 109 | $this->assertTrue($this->run_rule('greater_than[-10]', '')); |
| 110 | $this->assertTrue($this->run_rule('greater_than[-10]', '-9')); |
| 111 | |
| 112 | $this->assertFalse($this->run_rule('greater_than[-10]', '-99')); |
| 113 | $this->assertFalse($this->run_rule('greater_than[-10]', 'a')); |
| 114 | } |
| 115 | |
| 116 | public function test_rule_greater_than_equal_to() |
| 117 | { |
| 118 | // Empty input should pass any rule unless required is also specified |
| 119 | $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '')); |
| 120 | $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '0')); |
| 121 | $this->assertTrue($this->run_rule('greater_than_equal_to[0]', '1')); |
| 122 | |
| 123 | $this->assertFalse($this->run_rule('greater_than_equal_to[0]', '-1')); |
| 124 | $this->assertFalse($this->run_rule('greater_than_equal_to[0]', 'a')); |
| 125 | } |
| 126 | |
| 127 | public function test_rule_less_than() |
| 128 | { |
| 129 | // Empty input should pass any rule unless required is also specified |
| 130 | $this->assertTrue($this->run_rule('less_than[0]', '')); |
| 131 | $this->assertTrue($this->run_rule('less_than[0]', '-1')); |
| 132 | |
| 133 | $this->assertFalse($this->run_rule('less_than[0]', '0')); |
| 134 | $this->assertFalse($this->run_rule('less_than[0]', 'a')); |
| 135 | } |
| 136 | |
| 137 | public function test_rule_less_than_equal_to() |
| 138 | { |
| 139 | // Empty input should pass any rule unless required is also specified |
| 140 | $this->assertTrue($this->run_rule('less_than_equal_to[0]', '')); |
| 141 | $this->assertTrue($this->run_rule('less_than_equal_to[0]', '-1')); |
| 142 | $this->assertTrue($this->run_rule('less_than_equal_to[0]', '0')); |
| 143 | |
| 144 | $this->assertFalse($this->run_rule('less_than_equal_to[0]', '1')); |
| 145 | $this->assertFalse($this->run_rule('less_than_equal_to[0]', 'a')); |
| 146 | } |
| 147 | |
| 148 | public function test_rule_in_list() |
| 149 | { |
| 150 | // Empty input should pass any rule unless required is also specified |
| 151 | $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '')); |
| 152 | $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'red')); |
| 153 | $this->assertTrue($this->run_rule('in_list[red,Blue,123]', 'Blue')); |
| 154 | $this->assertTrue($this->run_rule('in_list[red,Blue,123]', '123')); |
| 155 | |
| 156 | $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'Red')); |
| 157 | $this->assertFalse($this->run_rule('in_list[red,Blue,123]', 'blue')); |
| 158 | $this->assertFalse($this->run_rule('in_list[red,Blue,123]', ' red')); |
| 159 | } |
| 160 | |
| 161 | public function test_rule_alpha() |
| 162 | { |
| 163 | // Empty input should pass any rule unless required is also specified |
| 164 | $this->assertTrue($this->run_rule('alpha', '')); |
| 165 | $this->assertTrue($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); |
| 166 | |
| 167 | $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); |
| 168 | $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); |
| 169 | $this->assertFalse($this->run_rule('alpha', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); |
| 170 | } |
| 171 | |
| 172 | public function test_rule_alpha_numeric() |
| 173 | { |
| 174 | // Empty input should pass any rule unless required is also specified |
| 175 | $this->assertTrue($this->run_rule('alpha_numeric', '')); |
| 176 | $this->assertTrue($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
| 177 | |
| 178 | $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); |
| 179 | $this->assertFalse($this->run_rule('alpha_numeric', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
| 180 | } |
| 181 | |
| 182 | public function test_rule_alpha_numeric_spaces() |
| 183 | { |
| 184 | // Empty input should pass any rule unless required is also specified |
| 185 | $this->assertTrue($this->run_rule('alpha_numeric_spaces', '')); |
| 186 | $this->assertTrue($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
| 187 | |
| 188 | $this->assertFalse($this->run_rule('alpha_numeric_spaces', ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
| 189 | } |
| 190 | |
| 191 | public function test_rule_alpha_dash() |
| 192 | { |
| 193 | // Empty input should pass any rule unless required is also specified |
| 194 | $this->assertTrue($this->run_rule('alpha_dash', '')); |
| 195 | $this->assertTrue($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); |
| 196 | |
| 197 | $this->assertFalse($this->run_rule('alpha_dash', 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); |
| 198 | } |
| 199 | |
| 200 | public function test_rule_numeric() |
| 201 | { |
| 202 | // Empty input should pass any rule unless required is also specified |
| 203 | $this->assertTrue($this->run_rule('numeric', '')); |
| 204 | $this->assertTrue($this->run_rule('numeric', '0')); |
| 205 | $this->assertTrue($this->run_rule('numeric', '12314')); |
| 206 | $this->assertTrue($this->run_rule('numeric', '-42')); |
| 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', 'code igniter')); |
| 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', base64_encode('string'))); |
| 301 | |
| 302 | $this->assertTrue($this->run_rule('valid_base64', "FA08GG")); |
| 303 | } |
| 304 | |
| 305 | public function run_rule($rule, $test_value) |
| 306 | { |
David Woods | c7029e2 | 2015-03-17 10:52:01 -0700 | [diff] [blame] | 307 | // $this->markTestSkipped('Not designed to be a unit test'); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame^] | 308 | // Reset the _$POST array |
| 309 | $_POST = array(); |
| 310 | $this->form_validation->reset_validation(); |
David Woods | c7029e2 | 2015-03-17 10:52:01 -0700 | [diff] [blame] | 311 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame^] | 312 | $this->form_validation->set_rules('field', 'name', $rule); |
| 313 | $_POST['field'] = $test_value; |
| 314 | return $this->form_validation->run(); |
| 315 | } |
| 316 | |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 317 | } |