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 |
Andrey Andreev | 878e23f | 2016-08-10 15:15:49 +0300 | [diff] [blame] | 11 | $loader = $this->getMockBuilder('CI_Loader')->setMethods(array('helper'))->getMock(); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 12 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 13 | // Same applies for lang |
Andrey Andreev | 878e23f | 2016-08-10 15:15:49 +0300 | [diff] [blame] | 14 | $lang = $this->getMockBuilder('CI_Lang')->setMethods(array('load'))->getMock(); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 15 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 16 | $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 Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 20 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 21 | $this->ci_instance_var('lang', $lang); |
| 22 | $this->ci_instance_var('load', $loader); |
| 23 | $this->ci_instance_var('input', $input); |
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->lang('form_validation'); |
| 26 | $this->helper('form'); |
| 27 | |
| 28 | $this->form_validation = new CI_Form_validation(); |
| 29 | } |
| 30 | |
Andrey Andreev | 7243d0b | 2016-03-12 11:40:34 +0200 | [diff] [blame] | 31 | public function test_empty_array_input() |
| 32 | { |
| 33 | $this->assertFalse( |
| 34 | $this->run_rules( |
| 35 | array(array('field' => 'foo', 'label' => 'Foo Label', 'rules' => 'required')), |
| 36 | array('foo' => array()) |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 41 | public function test_rule_required() |
| 42 | { |
Andrey Andreev | 9e78be0 | 2016-05-25 10:54:36 +0300 | [diff] [blame] | 43 | $rules = array(array('field' => 'foo', 'label' => 'Foo', 'rules' => 'is_numeric')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 44 | |
Andrey Andreev | 9e78be0 | 2016-05-25 10:54:36 +0300 | [diff] [blame] | 45 | // Empty, not required |
| 46 | $this->assertTrue($this->run_rules($rules, array('foo' => ''))); |
| 47 | |
| 48 | |
| 49 | // Not required, but also not empty |
| 50 | $this->assertTrue($this->run_rules($rules, array('foo' => '123'))); |
| 51 | $this->assertFalse($this->run_rules($rules, array('foo' => 'bar'))); |
| 52 | |
| 53 | // Required variations |
| 54 | $rules[0]['rules'] .= '|required'; |
| 55 | $this->assertTrue($this->run_rules($rules, array('foo' => '123'))); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 56 | $this->assertFalse($this->run_rules($rules, array('foo' => ''))); |
| 57 | $this->assertFalse($this->run_rules($rules, array('foo' => ' '))); |
Andrey Andreev | 9e78be0 | 2016-05-25 10:54:36 +0300 | [diff] [blame] | 58 | $this->assertFalse($this->run_rules($rules, array('foo' => 'bar'))); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public function test_rule_matches() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 62 | { |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 63 | $rules = array( |
| 64 | array('field' => 'foo', 'label' => 'label', 'rules' => 'required'), |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 65 | array('field' => 'bar', 'label' => 'label2', 'rules' => 'matches[foo]') |
| 66 | ); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 67 | $values_base = array('foo' => 'sample'); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 68 | |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 69 | $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => 'sample')))); |
| 70 | |
Andrey Andreev | 0fae625 | 2016-05-17 13:46:55 +0300 | [diff] [blame] | 71 | $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => '')))); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 72 | $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => 'Sample')))); |
| 73 | $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => ' sample')))); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public function test_rule_differs() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 77 | { |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 78 | $rules = array( |
| 79 | array('field' => 'foo', 'label' => 'label', 'rules' => 'required'), |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 80 | array('field' => 'bar', 'label' => 'label2', 'rules' => 'differs[foo]') |
| 81 | ); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 82 | $values_base = array('foo' => 'sample'); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 83 | |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 84 | $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => 'does_not_match')))); |
| 85 | $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => 'Sample')))); |
| 86 | $this->assertTrue($this->run_rules($rules, array_merge($values_base, array('bar' => ' sample')))); |
| 87 | |
| 88 | $this->assertFalse($this->run_rules($rules, array_merge($values_base, array('bar' => 'sample')))); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | public function test_rule_min_length() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 92 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 93 | $this->assertTrue($this->form_validation->min_length('12345', '5')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 94 | $this->assertTrue($this->form_validation->min_length('test', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 95 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 96 | $this->assertFalse($this->form_validation->min_length('123', '4')); |
| 97 | $this->assertFalse($this->form_validation->min_length('should_fail', 'A')); |
| 98 | $this->assertFalse($this->form_validation->min_length('', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | public function test_rule_max_length() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 102 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 103 | $this->assertTrue($this->form_validation->max_length('', '4')); |
| 104 | $this->assertTrue($this->form_validation->max_length('1234', '4')); |
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->max_length('12345', '4')); |
| 107 | $this->assertFalse($this->form_validation->max_length('should_fail', 'A')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | public function test_rule_exact_length() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 111 | { |
| 112 | $this->assertTrue($this->form_validation->exact_length('1234', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 113 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 114 | $this->assertFalse($this->form_validation->exact_length('', '3')); |
| 115 | $this->assertFalse($this->form_validation->exact_length('12345', '4')); |
| 116 | $this->assertFalse($this->form_validation->exact_length('123', '4')); |
| 117 | $this->assertFalse($this->form_validation->exact_length('should_fail', 'A')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public function test_rule_greater_than() |
| 121 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 122 | $this->assertTrue($this->form_validation->greater_than('-10', '-11')); |
| 123 | $this->assertTrue($this->form_validation->greater_than('10', '9')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 124 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 125 | $this->assertFalse($this->form_validation->greater_than('10', '10')); |
| 126 | $this->assertFalse($this->form_validation->greater_than('10', 'a')); |
| 127 | $this->assertFalse($this->form_validation->greater_than('10a', '10')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | public function test_rule_greater_than_equal_to() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 131 | { |
| 132 | $this->assertTrue($this->form_validation->greater_than_equal_to('0', '0')); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 133 | $this->assertTrue($this->form_validation->greater_than_equal_to('1', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 134 | |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 135 | $this->assertFalse($this->form_validation->greater_than_equal_to('-1', '0')); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 136 | $this->assertFalse($this->form_validation->greater_than_equal_to('10a', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | public function test_rule_less_than() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 140 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 141 | $this->assertTrue($this->form_validation->less_than('4', '5')); |
| 142 | $this->assertTrue($this->form_validation->less_than('-1', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 143 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 144 | $this->assertFalse($this->form_validation->less_than('4', '4')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 145 | $this->assertFalse($this->form_validation->less_than('10a', '5')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | public function test_rule_less_than_equal_to() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 149 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 150 | $this->assertTrue($this->form_validation->less_than_equal_to('-1', '0')); |
| 151 | $this->assertTrue($this->form_validation->less_than_equal_to('-1', '-1')); |
| 152 | $this->assertTrue($this->form_validation->less_than_equal_to('4', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 153 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 154 | $this->assertFalse($this->form_validation->less_than_equal_to('0', '-1')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 155 | $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | public function test_rule_in_list() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 159 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 160 | $this->assertTrue($this->form_validation->in_list('red', 'red,Blue,123')); |
| 161 | $this->assertTrue($this->form_validation->in_list('Blue', 'red,Blue,123')); |
| 162 | $this->assertTrue($this->form_validation->in_list('123', 'red,Blue,123')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 163 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 164 | $this->assertFalse($this->form_validation->in_list('Red', 'red,Blue,123')); |
| 165 | $this->assertFalse($this->form_validation->in_list(' red', 'red,Blue,123')); |
| 166 | $this->assertFalse($this->form_validation->in_list('1234', 'red,Blue,123')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | public function test_rule_alpha() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 170 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 171 | $this->assertTrue($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 172 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 173 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); |
| 174 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); |
| 175 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | public function test_rule_alpha_numeric() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 179 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 180 | $this->assertTrue($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 181 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 182 | $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); |
| 183 | $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | public function test_rule_alpha_numeric_spaces() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 187 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 188 | $this->assertTrue($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 189 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 190 | $this->assertFalse($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | public function test_rule_alpha_dash() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 194 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 195 | $this->assertTrue($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 196 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 197 | $this->assertFalse($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | public function test_rule_numeric() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 201 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 202 | $this->assertTrue($this->form_validation->numeric('0')); |
| 203 | $this->assertTrue($this->form_validation->numeric('12314')); |
| 204 | $this->assertTrue($this->form_validation->numeric('-42')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 205 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 206 | $this->assertFalse($this->form_validation->numeric('123a')); |
| 207 | $this->assertFalse($this->form_validation->numeric('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | public function test_rule_integer() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 211 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 212 | $this->assertTrue($this->form_validation->integer('0')); |
| 213 | $this->assertTrue($this->form_validation->integer('42')); |
| 214 | $this->assertTrue($this->form_validation->integer('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 215 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 216 | $this->assertFalse($this->form_validation->integer('124a')); |
| 217 | $this->assertFalse($this->form_validation->integer('1.9')); |
| 218 | $this->assertFalse($this->form_validation->integer('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | public function test_rule_decimal() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 222 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 223 | $this->assertTrue($this->form_validation->decimal('1.0')); |
| 224 | $this->assertTrue($this->form_validation->decimal('-0.98')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 225 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 226 | $this->assertFalse($this->form_validation->decimal('0')); |
| 227 | $this->assertFalse($this->form_validation->decimal('1.0a')); |
| 228 | $this->assertFalse($this->form_validation->decimal('-i')); |
| 229 | $this->assertFalse($this->form_validation->decimal('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | public function test_rule_is_natural() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 233 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 234 | $this->assertTrue($this->form_validation->is_natural('0')); |
| 235 | $this->assertTrue($this->form_validation->is_natural('12')); |
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->is_natural('42a')); |
| 238 | $this->assertFalse($this->form_validation->is_natural('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | public function test_rule_is_natural_no_zero() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 242 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 243 | $this->assertTrue($this->form_validation->is_natural_no_zero('42')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 244 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 245 | $this->assertFalse($this->form_validation->is_natural_no_zero('0')); |
| 246 | $this->assertFalse($this->form_validation->is_natural_no_zero('42a')); |
| 247 | $this->assertFalse($this->form_validation->is_natural_no_zero('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | public function test_rule_valid_url() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 251 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 252 | $this->assertTrue($this->form_validation->valid_url('www.codeigniter.com')); |
Andrey Andreev | 700dbad | 2016-01-11 12:51:46 +0200 | [diff] [blame] | 253 | $this->assertTrue($this->form_validation->valid_url('http://codeigniter.com')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 254 | |
Andrey Andreev | 391d339 | 2016-01-30 22:43:41 +0200 | [diff] [blame] | 255 | // https://bugs.php.net/bug.php?id=51192 |
| 256 | $this->assertTrue($this->form_validation->valid_url('http://accept-dashes.tld')); |
| 257 | $this->assertFalse($this->form_validation->valid_url('http://reject_underscores.tld')); |
| 258 | |
| 259 | // https://github.com/bcit-ci/CodeIgniter/issues/4415 |
| 260 | $this->assertTrue($this->form_validation->valid_url('http://[::1]/ipv6')); |
| 261 | |
Andrey Andreev | 9180a12 | 2016-08-10 15:23:42 +0300 | [diff] [blame] | 262 | // URI scheme case-sensitivity: https://github.com/bcit-ci/CodeIgniter/pull/4758 |
| 263 | $this->assertTrue($this->form_validation->valid_url('HtTp://127.0.0.1/')); |
| 264 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 265 | $this->assertFalse($this->form_validation->valid_url('htt://www.codeIgniter.com')); |
| 266 | $this->assertFalse($this->form_validation->valid_url('')); |
| 267 | $this->assertFalse($this->form_validation->valid_url('code igniter')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | public function test_rule_valid_email() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 271 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 272 | $this->assertTrue($this->form_validation->valid_email('email@sample.com')); |
Andrey Andreev | ed1a045 | 2017-06-19 08:25:23 +0300 | [diff] [blame] | 273 | $this->assertFalse($this->form_validation->valid_email('email@sample.com foo bar')); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 274 | $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | public function test_rule_valid_emails() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 278 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 279 | $this->assertTrue($this->form_validation->valid_emails('1@sample.com,2@sample.com')); |
| 280 | $this->assertTrue($this->form_validation->valid_emails('email@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 281 | |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 282 | $this->assertFalse($this->form_validation->valid_emails('valid_email', '@sample.com')); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 283 | $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] | 284 | } |
| 285 | |
| 286 | public function test_rule_valid_ip() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 287 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 288 | $this->assertTrue($this->form_validation->valid_ip('127.0.0.1')); |
| 289 | $this->assertTrue($this->form_validation->valid_ip('127.0.0.1', 'ipv4')); |
| 290 | $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); |
| 291 | $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] | 292 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 293 | $this->assertFalse($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv4')); |
| 294 | $this->assertFalse($this->form_validation->valid_ip('127.0.0.1', 'ipv6')); |
| 295 | $this->assertFalse($this->form_validation->valid_ip('H001:0db8:85a3:0000:0000:8a2e:0370:7334')); |
| 296 | $this->assertFalse($this->form_validation->valid_ip('127.0.0.259')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | public function test_rule_valid_base64() |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 300 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 301 | $this->assertTrue($this->form_validation->valid_base64(base64_encode('string'))); |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 302 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 303 | $this->assertFalse($this->form_validation->valid_base64('FA08GG')); |
| 304 | } |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 305 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 306 | public function test_set_data() |
| 307 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 308 | $data = array('field' => 'some_data'); |
| 309 | $this->form_validation->set_data($data); |
| 310 | $this->form_validation->set_rules('field', 'label', 'required'); |
| 311 | $this->assertTrue($this->form_validation->run()); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 312 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 313 | // Test with empty array |
| 314 | $_POST = array(); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 315 | $this->form_validation->reset_validation(); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 316 | $data = array('field' => 'some_data'); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 317 | $this->form_validation->set_data($data); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 318 | // This should do nothing. Old data will still be used |
| 319 | $this->form_validation->set_data(array()); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 320 | $this->form_validation->set_rules('field', 'label', 'required'); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 321 | $this->assertTrue($this->form_validation->run()); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 322 | } |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 323 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 324 | public function test_set_message() |
| 325 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 326 | $err_message = 'What a terrible error!'; |
| 327 | $rules = array( |
| 328 | array( |
| 329 | 'field' => 'req_field', |
| 330 | 'label' => 'label', |
| 331 | 'rules' => 'required' |
| 332 | ) |
| 333 | ); |
| 334 | $errorless_data = array('req_field' => 'some text'); |
| 335 | $erroneous_data = array('req_field' => ''); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 336 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 337 | $this->form_validation->set_message('required', $err_message); |
| 338 | $this->form_validation->set_data($erroneous_data); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 339 | $this->form_validation->set_rules($rules); |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 340 | $this->form_validation->run(); |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 341 | $this->assertEquals('<p>'.$err_message.'</p>', $this->form_validation->error('req_field')); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 342 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame] | 343 | $this->form_validation->reset_validation(); |
| 344 | $this->form_validation->set_message('required', $err_message); |
| 345 | $this->form_validation->set_data($errorless_data); |
| 346 | $this->form_validation->set_rules($rules); |
| 347 | $this->form_validation->run(); |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 348 | $this->assertEquals('', $this->form_validation->error('req_field')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 349 | } |
| 350 | |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 351 | public function test_set_error_delimiters() |
| 352 | { |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 353 | $prefix = '<div class="error">'; |
| 354 | $suffix = '</div>'; |
| 355 | $this->form_validation->set_error_delimiters($prefix, $suffix); |
| 356 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 357 | $_POST = array('foo' => ''); |
| 358 | $this->form_validation->run(); |
| 359 | $error_msg = $this->form_validation->error('foo'); |
| 360 | |
Andrey Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame^] | 361 | $this->assertStringStartsWith($prefix, $error_msg); |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 362 | $this->assertTrue(strrpos($error_msg, $suffix, -strlen($suffix)) === (strlen($error_msg) - strlen($suffix))); |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 363 | |
| 364 | $_POST = array(); |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | public function test_error_array() |
| 368 | { |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 369 | $error_message = 'What a terrible error!'; |
| 370 | $this->form_validation->set_message('required', $error_message); |
| 371 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 372 | $_POST = array('foo' => ''); |
| 373 | $this->form_validation->run(); |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 374 | $error_array = $this->form_validation->error_array(); |
| 375 | $this->assertEquals($error_message, $error_array['foo']); |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 376 | |
| 377 | $_POST = array(); |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | public function test_error_string() |
| 381 | { |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 382 | $error_message = 'What a terrible error!'; |
| 383 | $prefix_default = '<foo>'; |
| 384 | $suffix_default = '</foo>'; |
| 385 | $prefix_test = '<bar>'; |
| 386 | $suffix_test = '</bar>'; |
| 387 | $this->form_validation->set_error_delimiters($prefix_default, $suffix_default); |
| 388 | $this->form_validation->set_message('required', $error_message); |
| 389 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 390 | $_POST = array('foo' => ''); |
| 391 | $this->form_validation->run(); |
| 392 | |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 393 | $this->assertEquals($prefix_default.$error_message.$suffix_default."\n", $this->form_validation->error_string()); |
| 394 | $this->assertEquals($prefix_test.$error_message.$suffix_default."\n", $this->form_validation->error_string($prefix_test, '')); |
| 395 | $this->assertEquals($prefix_default.$error_message.$suffix_test."\n", $this->form_validation->error_string('', $suffix_test)); |
| 396 | $this->assertEquals($prefix_test.$error_message.$suffix_test."\n", $this->form_validation->error_string($prefix_test, $suffix_test)); |
| 397 | |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 398 | $this->form_validation->reset_validation(); |
| 399 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 400 | $_POST = array('foo' => 'bar'); |
| 401 | $this->form_validation->run(); |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 402 | $this->assertEquals('', $this->form_validation->error_string()); |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 403 | |
| 404 | $_POST = array(); |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | public function test_run() |
| 408 | { |
| 409 | // form_validation->run() is tested in many of the other unit tests |
| 410 | // This test will only test run(group='') when group is not empty |
| 411 | $config = array( |
| 412 | 'pass' => array( |
| 413 | array( |
| 414 | 'field' => 'username', |
| 415 | 'label' => 'user', |
| 416 | 'rules' => 'alpha_numeric' |
| 417 | ) |
| 418 | ), |
| 419 | 'fail' => array( |
| 420 | array( |
| 421 | 'field' => 'username', |
| 422 | 'label' => 'user', |
| 423 | 'rules' => 'alpha' |
| 424 | ) |
| 425 | ) |
| 426 | ); |
| 427 | $_POST = array('username' => 'foo42'); |
| 428 | $form_validation = new CI_Form_validation($config); |
| 429 | $this->assertTrue($form_validation->run('pass')); |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 430 | |
| 431 | $form_validation = new CI_Form_validation($config); |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 432 | $this->assertFalse($form_validation->run('fail')); |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 433 | |
| 434 | $_POST = array(); |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 435 | } |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 436 | |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 437 | public function test_has_rule() |
| 438 | { |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 439 | $this->form_validation->set_rules('foo', 'label', 'required'); |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 440 | |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 441 | $this->assertTrue($this->form_validation->has_rule('foo')); |
| 442 | $this->assertFalse($this->form_validation->has_rule('bar')); |
| 443 | } |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 444 | |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 445 | public function test_set_value() |
| 446 | { |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 447 | $default = 'default'; |
| 448 | $this->form_validation->set_rules('foo', 'label', 'required'); |
| 449 | $this->form_validation->set_rules('bar[]', 'label', 'required'); |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 450 | |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 451 | // No post data yet: should return the default value provided |
| 452 | $this->assertEquals($default, $this->form_validation->set_value('foo', $default)); |
| 453 | $_POST = array('foo' => 'foo', 'bar' => array('bar1', 'bar2')); |
| 454 | $this->form_validation->run(); |
| 455 | $this->assertEquals('foo', $this->form_validation->set_value('foo', $default)); |
| 456 | $this->assertEquals('bar1', $this->form_validation->set_value('bar[]', $default)); |
| 457 | $this->assertEquals('bar2', $this->form_validation->set_value('bar[]', $default)); |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 458 | |
| 459 | $_POST = array(); |
David Woods | 5bc1dc5 | 2015-03-27 22:41:58 -0700 | [diff] [blame] | 460 | } |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 461 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 462 | public function test_set_select() |
| 463 | { |
| 464 | // Test 1: No options selected |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 465 | $this->form_validation->run(); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 466 | |
| 467 | $this->assertEquals('', $this->form_validation->set_select('select', 'foo')); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 468 | $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'bar', TRUE)); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 469 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 470 | // Test 2: 1 option selected |
| 471 | $this->form_validation->reset_validation(); |
| 472 | $this->form_validation->set_rules('select', 'label', 'alpha_numeric'); |
| 473 | $_POST = array('select' => 'foo'); |
| 474 | $this->form_validation->run(); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 475 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 476 | $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo')); |
| 477 | $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select', 'foo', TRUE)); |
| 478 | $this->assertEquals('', $this->form_validation->set_select('select', 'bar')); |
| 479 | $this->assertEquals('', $this->form_validation->set_select('select', 'bar', TRUE)); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 480 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 481 | // Test 3: Multiple options selected |
| 482 | $this->form_validation->reset_validation(); |
David Woods | 29704f8 | 2015-03-30 10:37:57 -0700 | [diff] [blame] | 483 | $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric'); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 484 | $_POST = array('select' => array('foo', 'bar')); |
| 485 | $this->form_validation->run(); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 486 | |
David Woods | 29704f8 | 2015-03-30 10:37:57 -0700 | [diff] [blame] | 487 | $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'foo')); |
| 488 | $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'foo', TRUE)); |
| 489 | $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'bar')); |
| 490 | $this->assertEquals(' selected="selected"', $this->form_validation->set_select('select[]', 'bar', TRUE)); |
| 491 | $this->assertEquals('', $this->form_validation->set_select('select[]', 'foobar')); |
| 492 | $this->assertEquals('', $this->form_validation->set_select('select[]', 'foobar', TRUE)); |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 493 | |
| 494 | $_POST = array(); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 495 | } |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 496 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 497 | public function test_set_radio() |
| 498 | { |
| 499 | // Test 1: No options selected |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 500 | $this->form_validation->run(); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 501 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 502 | $this->assertEquals('', $this->form_validation->set_radio('select', 'foo')); |
David Woods | 29704f8 | 2015-03-30 10:37:57 -0700 | [diff] [blame] | 503 | // Default should only work when no rules are set |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 504 | $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'bar', TRUE)); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 505 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 506 | // Test 2: 1 option selected |
| 507 | $this->form_validation->reset_validation(); |
| 508 | $this->form_validation->set_rules('select', 'label', 'alpha_numeric'); |
| 509 | $_POST = array('select' => 'foo'); |
| 510 | $this->form_validation->run(); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 511 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 512 | $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo')); |
| 513 | $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select', 'foo', TRUE)); |
| 514 | $this->assertEquals('', $this->form_validation->set_radio('select', 'bar')); |
| 515 | $this->assertEquals('', $this->form_validation->set_radio('select', 'bar', TRUE)); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 516 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 517 | // Test 3: Multiple options checked |
| 518 | $this->form_validation->reset_validation(); |
David Woods | 29704f8 | 2015-03-30 10:37:57 -0700 | [diff] [blame] | 519 | $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric'); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 520 | $_POST = array('select' => array('foo', 'bar')); |
| 521 | $this->form_validation->run(); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 522 | |
David Woods | 29704f8 | 2015-03-30 10:37:57 -0700 | [diff] [blame] | 523 | $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'foo')); |
| 524 | $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'foo', TRUE)); |
| 525 | $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'bar')); |
| 526 | $this->assertEquals(' checked="checked"', $this->form_validation->set_radio('select[]', 'bar', TRUE)); |
| 527 | $this->assertEquals('', $this->form_validation->set_radio('select[]', 'foobar')); |
| 528 | $this->assertEquals('', $this->form_validation->set_radio('select[]', 'foobar', TRUE)); |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 529 | |
| 530 | $_POST = array(); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 531 | } |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 532 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 533 | public function test_set_checkbox() |
| 534 | { |
| 535 | // Test 1: No options selected |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 536 | $this->form_validation->run(); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 537 | |
| 538 | $this->assertEquals('', $this->form_validation->set_checkbox('select', 'foo')); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 539 | $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'bar', TRUE)); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 540 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 541 | // Test 2: 1 option selected |
| 542 | $this->form_validation->reset_validation(); |
| 543 | $this->form_validation->set_rules('select', 'label', 'alpha_numeric'); |
| 544 | $_POST = array('select' => 'foo'); |
| 545 | $this->form_validation->run(); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 546 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 547 | $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo')); |
| 548 | $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select', 'foo', TRUE)); |
| 549 | $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar')); |
| 550 | $this->assertEquals('', $this->form_validation->set_checkbox('select', 'bar', TRUE)); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 551 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 552 | // Test 3: Multiple options selected |
| 553 | $this->form_validation->reset_validation(); |
David Woods | 29704f8 | 2015-03-30 10:37:57 -0700 | [diff] [blame] | 554 | $this->form_validation->set_rules('select[]', 'label', 'alpha_numeric'); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 555 | $_POST = array('select' => array('foo', 'bar')); |
| 556 | $this->form_validation->run(); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 557 | |
David Woods | 29704f8 | 2015-03-30 10:37:57 -0700 | [diff] [blame] | 558 | $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'foo')); |
| 559 | $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'foo', TRUE)); |
| 560 | $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'bar')); |
| 561 | $this->assertEquals(' checked="checked"', $this->form_validation->set_checkbox('select[]', 'bar', TRUE)); |
| 562 | $this->assertEquals('', $this->form_validation->set_checkbox('select[]', 'foobar')); |
| 563 | $this->assertEquals('', $this->form_validation->set_checkbox('select[]', 'foobar', TRUE)); |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 564 | |
| 565 | $_POST = array(); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 566 | } |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 567 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 568 | public function test_regex_match() |
| 569 | { |
| 570 | $regex = '/f[a-zA-Z]+/'; |
| 571 | $this->assertTrue($this->form_validation->regex_match('foo', $regex)); |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 572 | $this->assertFalse($this->form_validation->regex_match('bar', $regex)); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 573 | } |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 574 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 575 | public function test_prep_for_form() |
| 576 | { |
| 577 | $this->form_validation->reset_validation(); |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 578 | $error_msg_unprepped = '<error =\'foobar\'">'; |
| 579 | $error_msg_prepped = '<error ='foobar'">'; |
| 580 | $this->form_validation->set_rules('foo', 'label', 'required', array('required' => $error_msg_unprepped)); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 581 | $_POST = array('foo' => ''); |
| 582 | $this->form_validation->run(); |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 583 | $error_arr = $this->form_validation->error_array(); |
| 584 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 585 | $this->assertEquals('', $this->form_validation->prep_for_form('')); |
David Woods | c6ac559 | 2015-03-31 20:19:39 -0700 | [diff] [blame] | 586 | $this->assertEquals(array('foo' => $error_msg_prepped), $this->form_validation->prep_for_form($error_arr)); |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 587 | } |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 588 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 589 | public function test_prep_url() |
| 590 | { |
| 591 | $this->assertEquals('', $this->form_validation->prep_url('')); |
| 592 | $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('codeigniter.com')); |
| 593 | $this->assertEquals('https://codeigniter.com', $this->form_validation->prep_url('https://codeigniter.com')); |
| 594 | $this->assertEquals('http://codeigniter.com', $this->form_validation->prep_url('http://codeigniter.com')); |
| 595 | $this->assertEquals('http://www.codeigniter.com', $this->form_validation->prep_url('www.codeigniter.com')); |
| 596 | } |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 597 | |
David Woods | 4b02f1d | 2015-03-29 22:46:14 -0700 | [diff] [blame] | 598 | public function test_encode_php_tags() |
| 599 | { |
| 600 | $this->assertEquals("<?php", $this->form_validation->encode_php_tags('<?php')); |
| 601 | $this->assertEquals('?>', $this->form_validation->encode_php_tags('?>')); |
| 602 | } |
Andrey Andreev | 2e9ae00 | 2015-04-01 14:45:16 +0300 | [diff] [blame] | 603 | |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 604 | /** |
| 605 | * Run rules |
| 606 | * |
| 607 | * Helper method to set rules and run them at once, not |
| 608 | * an actual test case. |
| 609 | */ |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 610 | public function run_rules($rules, $values) |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 611 | { |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 612 | $this->form_validation->reset_validation(); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 613 | $_POST = array(); |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 614 | $this->form_validation->set_rules($rules); |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 615 | |
David Woods | cf77671 | 2015-03-22 15:55:16 -0700 | [diff] [blame] | 616 | foreach ($values as $field => $value) |
| 617 | { |
| 618 | $_POST[$field] = $value; |
| 619 | } |
Andrey Andreev | 3e2045b | 2015-03-26 12:34:38 +0200 | [diff] [blame] | 620 | |
Andrey Andreev | c54fd91 | 2017-10-12 12:56:09 +0300 | [diff] [blame] | 621 | $valid = $this->form_validation->run(); |
| 622 | $_POST = array(); |
| 623 | |
| 624 | return $valid; |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 625 | } |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 626 | } |