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 |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 11 | $loader = $this->getMock('CI_Loader', array('helper')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 12 | // At current, CI_Form_Validation only calls load->helper("form") |
| 13 | // Assert this so that if that changes this fails fast |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 14 | $loader->method('helper') |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 15 | ->with($this->equalTo('form')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 16 | // Same applies for lang |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 17 | $lang = $this->getMock('CI_Lang', array('load')); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 18 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 19 | $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 Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 23 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 24 | $this->ci_instance_var('lang', $lang); |
| 25 | $this->ci_instance_var('load', $loader); |
| 26 | $this->ci_instance_var('input', $input); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 27 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 28 | $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 Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 36 | $form_validation = new CI_Form_validation(); |
| 37 | $this->assertNotNull($form_validation); |
| 38 | } |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 39 | |
| 40 | public function test_rule_required() |
| 41 | { |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 42 | $rules = array(array('field' => 'foo', 'label' => 'foo_label', 'rules' => 'required')); |
| 43 | $this->assertTrue($this->run_rules($rules, array('foo' => 'bar'))); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 44 | |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 45 | $this->assertFalse($this->run_rules($rules, array('foo' => ''))); |
| 46 | $this->assertFalse($this->run_rules($rules, array('foo' => ' '))); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | public function test_rule_matches() |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 50 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 64 | |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 65 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | public function test_rule_differs() |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 76 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 94 | |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 95 | $this->assertFalse($this->run_rules( |
| 96 | $rules, |
| 97 | array_merge($values_base, array('bar' => 'sample')) |
| 98 | )); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | public function test_rule_min_length() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 102 | { |
| 103 | $this->assertTrue($this->form_validation->min_length('12345', '5')); |
| 104 | $this->assertTrue($this->form_validation->min_length('test', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 105 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 106 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | public function test_rule_max_length() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 112 | { |
| 113 | $this->assertTrue($this->form_validation->max_length('', '4')); |
| 114 | $this->assertTrue($this->form_validation->max_length('1234', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 115 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 116 | $this->assertFalse($this->form_validation->max_length('12345', '4')); |
| 117 | $this->assertFalse($this->form_validation->max_length('should_fail', 'A')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public function test_rule_exact_length() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 121 | { |
| 122 | $this->assertTrue($this->form_validation->exact_length('1234', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 123 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 124 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | public function test_rule_greater_than() |
| 131 | { |
| 132 | // Empty input should pass any rule unless required is also specified |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 133 | $this->assertTrue($this->form_validation->greater_than('-10', '-11')); |
| 134 | $this->assertTrue($this->form_validation->greater_than('10', '9')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 135 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 136 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | public function test_rule_greater_than_equal_to() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 142 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 145 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 146 | $this->assertFalse($this->form_validation->greater_than_equal_to('-1', '0')); |
| 147 | $this->assertFalse($this->form_validation->greater_than_equal_to('10a', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | public function test_rule_less_than() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 151 | { |
| 152 | $this->assertTrue($this->form_validation->less_than('4', '5')); |
| 153 | $this->assertTrue($this->form_validation->less_than('-1', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 154 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 155 | $this->assertFalse($this->form_validation->less_than('4', '4')); |
| 156 | $this->assertFalse($this->form_validation->less_than('10a', '5')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | public function test_rule_less_than_equal_to() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 160 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 164 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 165 | $this->assertFalse($this->form_validation->less_than_equal_to('0', '-1')); |
| 166 | $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | public function test_rule_in_list() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 170 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 174 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 175 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | public function test_rule_alpha() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 181 | { |
| 182 | $this->assertTrue($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 183 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 184 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); |
| 185 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); |
| 186 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | public function test_rule_alpha_numeric() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 190 | { |
| 191 | $this->assertTrue($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 192 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 193 | $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); |
| 194 | $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | public function test_rule_alpha_numeric_spaces() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 198 | { |
| 199 | $this->assertTrue($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 200 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 201 | $this->assertFalse($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | public function test_rule_alpha_dash() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 205 | { |
| 206 | $this->assertTrue($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 207 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 208 | $this->assertFalse($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | public function test_rule_numeric() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 212 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 216 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 217 | $this->assertFalse($this->form_validation->numeric('123a')); |
| 218 | $this->assertFalse($this->form_validation->numeric('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | public function test_rule_integer() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 222 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 226 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 227 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | public function test_rule_decimal() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 233 | { |
| 234 | $this->assertTrue($this->form_validation->decimal('1.0')); |
| 235 | $this->assertTrue($this->form_validation->decimal('-0.98')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 236 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 237 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | public function test_rule_is_natural() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 244 | { |
| 245 | $this->assertTrue($this->form_validation->is_natural('0')); |
| 246 | $this->assertTrue($this->form_validation->is_natural('12')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 247 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 248 | $this->assertFalse($this->form_validation->is_natural('42a')); |
| 249 | $this->assertFalse($this->form_validation->is_natural('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | public function test_rule_is_natural_no_zero() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 253 | { |
| 254 | $this->assertTrue($this->form_validation->is_natural_no_zero('42')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 255 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 256 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | public function test_rule_valid_url() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 262 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | public function test_rule_valid_email() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 272 | { |
| 273 | $this->assertTrue($this->form_validation->valid_email('email@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 274 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 275 | $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | public function test_rule_valid_emails() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 279 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 282 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 283 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | public function test_rule_valid_ip() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 288 | { |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 293 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 294 | $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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | public function test_rule_valid_base64() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 301 | { |
| 302 | $this->assertTrue($this->form_validation->valid_base64(base64_encode('string'))); |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 303 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 304 | $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 Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 312 | $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 Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 319 | $this->form_validation->reset_validation(); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 320 | $data = array('field' => 'some_data'); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 321 | $this->form_validation->set_data($data); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 322 | // This should do nothing. Old data will still be used |
| 323 | $this->form_validation->set_data(array()); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 324 | $this->form_validation->set_rules('field', 'label', 'required'); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 325 | $this->assertTrue($this->form_validation->run()); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 326 | } |
| 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 Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 356 | } |
| 357 | |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 358 | public function run_rules($rules, $values) |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 359 | { |
David Woods | c7029e2 | 2015-03-17 10:52:01 -0700 | [diff] [blame] | 360 | // $this->markTestSkipped('Not designed to be a unit test'); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 361 | $this->form_validation->reset_validation(); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 362 | $_POST = array(); |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 363 | |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame^] | 364 | $this->form_validation->set_rules($rules); |
| 365 | foreach ($values as $field => $value) |
| 366 | { |
| 367 | $_POST[$field] = $value; |
| 368 | } |
| 369 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 370 | return $this->form_validation->run(); |
| 371 | } |
| 372 | |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 373 | } |