David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Form_validation_test extends CI_TestCase { |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 4 | |
| 5 | public function set_up() |
| 6 | { |
| 7 | $_SERVER['REQUEST_METHOD'] = 'POST'; |
| 8 | |
| 9 | // Create a mock loader since load->helper() looks in the wrong directories for unit tests, |
| 10 | // We'll use CI_TestCase->helper() instead |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 11 | $loader = $this->getMock('CI_Loader', array('helper')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 12 | // At current, CI_Form_Validation only calls load->helper("form") |
| 13 | // Assert this so that if that changes this fails fast |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 14 | $loader->method('helper') |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 15 | ->with($this->equalTo('form')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 16 | // Same applies for lang |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 17 | $lang = $this->getMock('CI_Lang', array('load')); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 18 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 19 | $this->ci_set_config('charset', 'UTF-8'); |
| 20 | $utf8 = new Mock_Core_Utf8(); |
| 21 | $security = new Mock_Core_Security(); |
| 22 | $input = new Mock_Core_Input($security, $utf8); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 23 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 24 | $this->ci_instance_var('lang', $lang); |
| 25 | $this->ci_instance_var('load', $loader); |
| 26 | $this->ci_instance_var('input', $input); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame] | 27 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 28 | $this->lang('form_validation'); |
| 29 | $this->helper('form'); |
| 30 | |
| 31 | $this->form_validation = new CI_Form_validation(); |
| 32 | } |
| 33 | |
| 34 | public function test___construct() |
| 35 | { |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 36 | $form_validation = new CI_Form_validation(); |
| 37 | $this->assertNotNull($form_validation); |
| 38 | } |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 39 | |
| 40 | public function test_rule_required() |
| 41 | { |
| 42 | $this->assertTrue($this->run_rule('required', ' someValue')); |
| 43 | |
| 44 | $this->assertFalse($this->run_rule('required', '')); |
| 45 | $this->assertFalse($this->run_rule('required', ' ')); |
| 46 | } |
| 47 | |
| 48 | public function test_rule_matches() |
| 49 | { |
| 50 | // Empty input should pass any rule unless required is also specified |
| 51 | $_POST['to_match'] = 'sample'; |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 52 | $this->assertTrue($this->run_rule('matches[to_match]', '', FALSE)); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 53 | $_POST['to_match'] = 'sample'; |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 54 | $this->assertTrue($this->run_rule('matches[to_match]', 'sample', FALSE)); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 55 | |
| 56 | $_POST['to_match'] = 'sample'; |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 57 | $this->assertFalse($this->run_rule('matches[to_match]', 'Sample', FALSE)); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 58 | $_POST['to_match'] = 'sample'; |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 59 | $this->assertFalse($this->run_rule('matches[to_match]', ' sample', FALSE)); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public function test_rule_differs() |
| 63 | { |
| 64 | // Empty input should pass any rule unless required is also specified |
| 65 | $_POST['to_differ'] = 'sample'; |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 66 | $this->assertTrue($this->run_rule('differs[to_differ]', '', FALSE)); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 67 | $_POST['to_differ'] = 'sample'; |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 68 | $this->assertTrue($this->run_rule('differs[to_differ]', 'Sample', FALSE)); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 69 | $_POST['to_differ'] = 'sample'; |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 70 | $this->assertTrue($this->run_rule('differs[to_differ]', ' sample', FALSE)); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 71 | |
| 72 | $_POST['to_differ'] = 'sample'; |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 73 | $this->assertFalse($this->run_rule('differs[to_differ]', 'sample', FALSE)); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public function test_rule_min_length() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 77 | { |
| 78 | $this->assertTrue($this->form_validation->min_length('12345', '5')); |
| 79 | $this->assertTrue($this->form_validation->min_length('test', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 80 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 81 | $this->assertFalse($this->form_validation->min_length('123', '4')); |
| 82 | $this->assertFalse($this->form_validation->min_length('should_fail', 'A')); |
| 83 | $this->assertFalse($this->form_validation->min_length('', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public function test_rule_max_length() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 87 | { |
| 88 | $this->assertTrue($this->form_validation->max_length('', '4')); |
| 89 | $this->assertTrue($this->form_validation->max_length('1234', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 90 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 91 | $this->assertFalse($this->form_validation->max_length('12345', '4')); |
| 92 | $this->assertFalse($this->form_validation->max_length('should_fail', 'A')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | public function test_rule_exact_length() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 96 | { |
| 97 | $this->assertTrue($this->form_validation->exact_length('1234', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 98 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 99 | $this->assertFalse($this->form_validation->exact_length('', '3')); |
| 100 | $this->assertFalse($this->form_validation->exact_length('12345', '4')); |
| 101 | $this->assertFalse($this->form_validation->exact_length('123', '4')); |
| 102 | $this->assertFalse($this->form_validation->exact_length('should_fail', 'A')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | public function test_rule_greater_than() |
| 106 | { |
| 107 | // Empty input should pass any rule unless required is also specified |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 108 | $this->assertTrue($this->form_validation->greater_than('-10', '-11')); |
| 109 | $this->assertTrue($this->form_validation->greater_than('10', '9')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 110 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 111 | $this->assertFalse($this->form_validation->greater_than('10', '10')); |
| 112 | $this->assertFalse($this->form_validation->greater_than('10', 'a')); |
| 113 | $this->assertFalse($this->form_validation->greater_than('10a', '10')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public function test_rule_greater_than_equal_to() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 117 | { |
| 118 | $this->assertTrue($this->form_validation->greater_than_equal_to('0', '0')); |
| 119 | $this->assertTrue($this->form_validation->greater_than_equal_to('1', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 120 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 121 | $this->assertFalse($this->form_validation->greater_than_equal_to('-1', '0')); |
| 122 | $this->assertFalse($this->form_validation->greater_than_equal_to('10a', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | public function test_rule_less_than() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 126 | { |
| 127 | $this->assertTrue($this->form_validation->less_than('4', '5')); |
| 128 | $this->assertTrue($this->form_validation->less_than('-1', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 129 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 130 | $this->assertFalse($this->form_validation->less_than('4', '4')); |
| 131 | $this->assertFalse($this->form_validation->less_than('10a', '5')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | public function test_rule_less_than_equal_to() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 135 | { |
| 136 | $this->assertTrue($this->form_validation->less_than_equal_to('-1', '0')); |
| 137 | $this->assertTrue($this->form_validation->less_than_equal_to('-1', '-1')); |
| 138 | $this->assertTrue($this->form_validation->less_than_equal_to('4', '4')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 139 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 140 | $this->assertFalse($this->form_validation->less_than_equal_to('0', '-1')); |
| 141 | $this->assertFalse($this->form_validation->less_than_equal_to('10a', '0')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | public function test_rule_in_list() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 145 | { |
| 146 | $this->assertTrue($this->form_validation->in_list('red', 'red,Blue,123')); |
| 147 | $this->assertTrue($this->form_validation->in_list('Blue', 'red,Blue,123')); |
| 148 | $this->assertTrue($this->form_validation->in_list('123', 'red,Blue,123')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 149 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 150 | $this->assertFalse($this->form_validation->in_list('Red', 'red,Blue,123')); |
| 151 | $this->assertFalse($this->form_validation->in_list(' red', 'red,Blue,123')); |
| 152 | $this->assertFalse($this->form_validation->in_list('1234', 'red,Blue,123')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public function test_rule_alpha() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 156 | { |
| 157 | $this->assertTrue($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 158 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 159 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ')); |
| 160 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1')); |
| 161 | $this->assertFalse($this->form_validation->alpha('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | public function test_rule_alpha_numeric() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 165 | { |
| 166 | $this->assertTrue($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 167 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 168 | $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ')); |
| 169 | $this->assertFalse($this->form_validation->alpha_numeric('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | public function test_rule_alpha_numeric_spaces() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 173 | { |
| 174 | $this->assertTrue($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 175 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 176 | $this->assertFalse($this->form_validation->alpha_numeric_spaces(' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | public function test_rule_alpha_dash() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 180 | { |
| 181 | $this->assertTrue($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 182 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 183 | $this->assertFalse($this->form_validation->alpha_dash('abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_\ ')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | public function test_rule_numeric() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 187 | { |
| 188 | $this->assertTrue($this->form_validation->numeric('0')); |
| 189 | $this->assertTrue($this->form_validation->numeric('12314')); |
| 190 | $this->assertTrue($this->form_validation->numeric('-42')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 191 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 192 | $this->assertFalse($this->form_validation->numeric('123a')); |
| 193 | $this->assertFalse($this->form_validation->numeric('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | public function test_rule_integer() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 197 | { |
| 198 | $this->assertTrue($this->form_validation->integer('0')); |
| 199 | $this->assertTrue($this->form_validation->integer('42')); |
| 200 | $this->assertTrue($this->form_validation->integer('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 201 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 202 | $this->assertFalse($this->form_validation->integer('124a')); |
| 203 | $this->assertFalse($this->form_validation->integer('1.9')); |
| 204 | $this->assertFalse($this->form_validation->integer('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | public function test_rule_decimal() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 208 | { |
| 209 | $this->assertTrue($this->form_validation->decimal('1.0')); |
| 210 | $this->assertTrue($this->form_validation->decimal('-0.98')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 211 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 212 | $this->assertFalse($this->form_validation->decimal('0')); |
| 213 | $this->assertFalse($this->form_validation->decimal('1.0a')); |
| 214 | $this->assertFalse($this->form_validation->decimal('-i')); |
| 215 | $this->assertFalse($this->form_validation->decimal('--1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | public function test_rule_is_natural() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 219 | { |
| 220 | $this->assertTrue($this->form_validation->is_natural('0')); |
| 221 | $this->assertTrue($this->form_validation->is_natural('12')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 222 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 223 | $this->assertFalse($this->form_validation->is_natural('42a')); |
| 224 | $this->assertFalse($this->form_validation->is_natural('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | public function test_rule_is_natural_no_zero() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 228 | { |
| 229 | $this->assertTrue($this->form_validation->is_natural_no_zero('42')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 230 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 231 | $this->assertFalse($this->form_validation->is_natural_no_zero('0')); |
| 232 | $this->assertFalse($this->form_validation->is_natural_no_zero('42a')); |
| 233 | $this->assertFalse($this->form_validation->is_natural_no_zero('-1')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | public function test_rule_valid_url() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 237 | { |
| 238 | $this->assertTrue($this->form_validation->valid_url('www.codeigniter.com')); |
| 239 | $this->assertTrue($this->form_validation->valid_url('http://codeigniter.eu')); |
| 240 | |
| 241 | $this->assertFalse($this->form_validation->valid_url('htt://www.codeIgniter.com')); |
| 242 | $this->assertFalse($this->form_validation->valid_url('')); |
| 243 | $this->assertFalse($this->form_validation->valid_url('code igniter')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | public function test_rule_valid_email() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 247 | { |
| 248 | $this->assertTrue($this->form_validation->valid_email('email@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 249 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 250 | $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | public function test_rule_valid_emails() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 254 | { |
| 255 | $this->assertTrue($this->form_validation->valid_emails('1@sample.com,2@sample.com')); |
| 256 | $this->assertTrue($this->form_validation->valid_emails('email@sample.com')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 257 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 258 | $this->assertFalse($this->form_validation->valid_emails('valid_email', '@sample.com')); |
| 259 | $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] | 260 | } |
| 261 | |
| 262 | public function test_rule_valid_ip() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 263 | { |
| 264 | $this->assertTrue($this->form_validation->valid_ip('127.0.0.1')); |
| 265 | $this->assertTrue($this->form_validation->valid_ip('127.0.0.1', 'ipv4')); |
| 266 | $this->assertTrue($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); |
| 267 | $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] | 268 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 269 | $this->assertFalse($this->form_validation->valid_ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'ipv4')); |
| 270 | $this->assertFalse($this->form_validation->valid_ip('127.0.0.1', 'ipv6')); |
| 271 | $this->assertFalse($this->form_validation->valid_ip('H001:0db8:85a3:0000:0000:8a2e:0370:7334')); |
| 272 | $this->assertFalse($this->form_validation->valid_ip('127.0.0.259')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | public function test_rule_valid_base64() |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 276 | { |
| 277 | $this->assertTrue($this->form_validation->valid_base64(base64_encode('string'))); |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 278 | |
David Woods | 317cad9 | 2015-03-20 22:32:24 -0700 | [diff] [blame^] | 279 | $this->assertFalse($this->form_validation->valid_base64('FA08GG')); |
| 280 | } |
| 281 | |
| 282 | public function test_set_data() |
| 283 | { |
| 284 | // Reset test environment |
| 285 | $_POST = array(); |
| 286 | $this->form_validation->reset_validation(); |
| 287 | |
| 288 | $data = array('field' => 'some_data'); |
| 289 | $this->form_validation->set_data($data); |
| 290 | $this->form_validation->set_rules('field', 'label', 'required'); |
| 291 | $this->assertTrue($this->form_validation->run()); |
| 292 | |
| 293 | // Test with empty array |
| 294 | $_POST = array(); |
| 295 | $data = array(); |
| 296 | $this->form_validation->reset_validation(); |
| 297 | $this->form_validation->set_data($data); |
| 298 | $this->form_validation->set_rules('field', 'label', 'required'); |
| 299 | $this->assertFalse($this->form_validation->run()); |
| 300 | } |
| 301 | |
| 302 | public function test_set_message() |
| 303 | { |
| 304 | // Reset test environment |
| 305 | $_POST = array(); |
| 306 | $this->form_validation->reset_validation(); |
| 307 | $err_message = 'What a terrible error!'; |
| 308 | $rules = array( |
| 309 | array( |
| 310 | 'field' => 'req_field', |
| 311 | 'label' => 'label', |
| 312 | 'rules' => 'required' |
| 313 | ) |
| 314 | ); |
| 315 | $errorless_data = array('req_field' => 'some text'); |
| 316 | $erroneous_data = array('req_field' => ''); |
| 317 | |
| 318 | $this->form_validation->set_message('required', $err_message); |
| 319 | $this->form_validation->set_data($erroneous_data); |
| 320 | $this->form_validation->set_rules($rules); |
| 321 | $this->form_validation->run(); |
| 322 | $this->assertEquals('<p>'.$err_message.'</p>', $this->form_validation->error('req_field')); |
| 323 | |
| 324 | $this->form_validation->reset_validation(); |
| 325 | $this->form_validation->set_message('required', $err_message); |
| 326 | $this->form_validation->set_data($errorless_data); |
| 327 | $this->form_validation->set_rules($rules); |
| 328 | $this->form_validation->run(); |
| 329 | $this->assertEquals('', $this->form_validation->error('req_field')); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 330 | } |
| 331 | |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 332 | public function run_rule($rule, $test_value, $reset_post = TRUE) |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 333 | { |
David Woods | c7029e2 | 2015-03-17 10:52:01 -0700 | [diff] [blame] | 334 | // $this->markTestSkipped('Not designed to be a unit test'); |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 335 | $this->form_validation->reset_validation(); |
David Woods | 70e220a | 2015-03-18 10:29:20 -0700 | [diff] [blame] | 336 | if ($reset_post === TRUE) |
| 337 | { |
| 338 | $_POST = array(); |
| 339 | } |
David Woods | 5b88473 | 2015-03-18 10:37:35 -0700 | [diff] [blame] | 340 | |
David Woods | 64af3bb | 2015-03-18 10:09:26 -0700 | [diff] [blame] | 341 | $this->form_validation->set_rules('field', 'name', $rule); |
| 342 | $_POST['field'] = $test_value; |
| 343 | return $this->form_validation->run(); |
| 344 | } |
| 345 | |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 346 | } |