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