David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Form_validation_test extends CI_TestCase { |
| 4 | |
| 5 | public function set_up() |
| 6 | { |
| 7 | // Create a mock loader since load->helper() looks in the wrong directories for unit tests, |
| 8 | // We'll use CI_TestCase->helper() instead |
| 9 | $ldr = $this->getMock('CI_Loader', array('helper')); |
| 10 | // At current, CI_Form_Validation only calls load->helper("form") |
| 11 | // Assert this so that if that changes this fails fast |
| 12 | $ldr->expects($this->once()) |
| 13 | ->method('helper') |
| 14 | ->with($this->equalTo('form')); |
| 15 | |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame^] | 16 | // Same applies for lang |
| 17 | $lang = $this->getMock('CI_Lang', array('load')); |
| 18 | |
| 19 | // Setup CI_Input |
| 20 | // Set server variable to GET as default, since this will leave unset in STDIN env |
| 21 | $_SERVER['REQUEST_METHOD'] = 'GET'; |
| 22 | |
| 23 | // Set config for Input class |
| 24 | $this->ci_set_config('allow_get_array', TRUE); |
| 25 | $this->ci_set_config('global_xss_filtering', FALSE); |
| 26 | $this->ci_set_config('csrf_protection', FALSE); |
| 27 | |
| 28 | $security = new Mock_Core_Security(); |
| 29 | |
| 30 | $this->ci_set_config('charset', 'UTF-8'); |
| 31 | $utf8 = new Mock_Core_Utf8(); |
| 32 | |
| 33 | $inp = new Mock_Core_Input($security, $utf8); |
| 34 | |
| 35 | $this->ci_instance_var('lang', $lang); |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 36 | $this->ci_instance_var('load', $ldr); |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame^] | 37 | $this->ci_instance_var('input', $inp); |
| 38 | |
| 39 | $this->lang('form_validation'); |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 40 | $this->helper('form'); |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | public function test___construct() |
| 44 | { |
| 45 | $this->form_validation = new CI_Form_validation(); |
| 46 | |
| 47 | $this->assertNotNull($this->form_validation); |
| 48 | } |
| 49 | |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame^] | 50 | public function test_rules_valid() |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 51 | { |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame^] | 52 | $this->form_validation = new CI_Form_validation(); |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 53 | |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame^] | 54 | $valid_posts = array( |
| 55 | 'required' => array('required',' !'), |
| 56 | 'matches[sample]' => 'sample', |
| 57 | 'differs[sample]' => 'differ', |
| 58 | 'min_length[4]' => array('is_more_than_4', '1234', ' 1'), |
| 59 | 'max_length[8]' => array('less_8', '12345678'), |
| 60 | 'exact_length[5]' => '12345', |
| 61 | 'greater_than[-5]' => array('-4','0','123124451'), |
| 62 | 'greater_than_equal_to[8]' => array('8', '99'), |
| 63 | 'less_than[0]' => '-1', |
| 64 | 'less_than_equal_to[5]' => array('-5', '5'), |
| 65 | 'in_list[red,blue,green]' => array('red', 'blue','green'), |
| 66 | 'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ', |
| 67 | 'alpha_numeric' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789', |
| 68 | 'alpha_numeric_spaces' => ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789', |
| 69 | 'alpha_dash' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_', |
| 70 | 'numeric' => '0123456789', |
| 71 | 'integer' => array('0', '-1231', '987234'), |
| 72 | 'decimal' => array('0.123', '1.0'), |
| 73 | 'is_natural' => '0', |
| 74 | 'is_natural_no_zero' => '1', |
| 75 | 'valid_url' => array('www.codeigniter.com','http://codeigniter.eu'), |
| 76 | 'valid_email' => 'email@sample.com', |
| 77 | 'valid_emails' => '1@sample.com,2@sample.com', |
| 78 | 'valid_ip[ipv4]' => '127.0.0.1', |
| 79 | 'valid_ip[ipv6]' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334', |
| 80 | 'valid_base64' => 'string' |
| 81 | ); |
| 82 | |
| 83 | // Loop through each rule and test |
| 84 | foreach ($valid_posts as $rule => $value) { |
| 85 | // Reset $_POST |
| 86 | $_POST = array(); |
| 87 | |
| 88 | if (is_array($value)) { |
| 89 | foreach($value as $item) { |
| 90 | // printf("%s => %s\n", $rule, $item); |
| 91 | $this->form_validation->set_rules('field', 'name', $rule); |
| 92 | $_POST['field'] = $item; |
| 93 | $this->assertTrue($this->form_validation->run()); |
| 94 | $this->form_validation->reset_validation(); |
| 95 | } |
| 96 | } |
| 97 | else { |
| 98 | // printf("%s => %s\n", $rule, $value); |
| 99 | $this->form_validation->set_rules('field', 'name', $rule); |
| 100 | $_POST['field'] = $value; |
| 101 | $this->assertTrue($this->form_validation->run()); |
| 102 | $this->form_validation->reset_validation(); |
| 103 | } |
| 104 | } |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 105 | } |
| 106 | |
David Woods | dc1ae6b | 2015-03-16 23:36:54 -0700 | [diff] [blame^] | 107 | public function test_rules_invalid() |
| 108 | { |
| 109 | $this->form_validation = new CI_Form_validation(); |
| 110 | |
| 111 | $invalid_posts = array( |
| 112 | 'required' => array('',' '), |
| 113 | 'matches[sample]' => 'Sample', |
| 114 | 'differ[sample]' => 'sample', |
| 115 | 'min_length[4]' => array('123', ''), |
| 116 | 'max_length[8]' => array('more_than_8', '12345678 '), |
| 117 | 'exact_length[5]' => ' 12345', |
| 118 | 'greater_than[-5]' => array('-5, -12415'), |
| 119 | 'greater_than_equal_to[8]' => array('7', '0'), |
| 120 | 'less_than[0]' => '0', |
| 121 | 'less_than_equal_to[5]' => array('6', '98234'), |
| 122 | 'in_list[red,blue,green]' => array(' red', 'Blue','failz'), |
| 123 | 'alpha' => array('*', ' a', '1'), |
| 124 | 'alpha_numeric' => array('a1 ', '*', '1-'), |
| 125 | 'alpha_numeric_spaces' => array('a1*', ' ~'), |
| 126 | 'alpha_dash' => array('a b', '*'), |
| 127 | 'numeric' => array('a', ''), |
| 128 | 'integer' => array('0.123', '1a', ''), |
| 129 | 'decimal' => array('1', 'a',''), |
| 130 | 'is_natural' => array('1.2','aA',''), |
| 131 | 'is_natural_no_zero' => array('0','1.2',''), |
| 132 | 'valid_url' => array('codeigniter.com','nosite', ''), |
| 133 | 'valid_email' => '@sample.com', |
| 134 | 'valid_emails' => '@sample.com,2@sample.com,validemail@email.ca', |
| 135 | 'valid_ip[ipv4]' => '257.0.0.1', |
| 136 | 'valid_ip[ipv6]' => 'A001:0db8:85a3:0000:0000:8a2e:0370:7334', |
| 137 | ); |
| 138 | |
| 139 | // Loop through each rule and test |
| 140 | foreach ($invalid_posts as $rule => $value) { |
| 141 | // Reset $_POST |
| 142 | $_POST = array(); |
| 143 | |
| 144 | if (is_array($value)) { |
| 145 | foreach($value as $item) { |
| 146 | printf("%s => %s\n", $rule, $item); |
| 147 | $this->form_validation->set_rules('field', 'name', $rule); |
| 148 | $_POST['field'] = $item; |
| 149 | $this->assertFalse($this->form_validation->run()); |
| 150 | $this->form_validation->reset_validation(); |
| 151 | } |
| 152 | } |
| 153 | else { |
| 154 | printf("%s => %s\n", $rule, $value); |
| 155 | $this->form_validation->set_rules('field', 'name', $rule); |
| 156 | $_POST['field'] = $value; |
| 157 | $this->assertFalse($this->form_validation->run()); |
| 158 | $this->form_validation->reset_validation(); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
David Woods | f3ac71e | 2015-03-16 20:00:21 -0700 | [diff] [blame] | 163 | } |