summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/libraries/Form_validation_test.php
blob: aefc9a2c756b427f0d3a64f134f68d7613154c5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php

class Form_validation_test extends CI_TestCase {
    
    public function set_up()
    {          
        // Create a mock loader since load->helper() looks in the wrong directories for unit tests,
        // We'll use CI_TestCase->helper() instead
        $ldr = $this->getMock('CI_Loader', array('helper'));
        // At current, CI_Form_Validation only calls load->helper("form")
        // Assert this so that if that changes this fails fast
        $ldr->expects($this->once())
                ->method('helper')
                ->with($this->equalTo('form'));
        
        // Same applies for lang
        $lang = $this->getMock('CI_Lang', array('load'));
        
        // Setup CI_Input
        // Set server variable to GET as default, since this will leave unset in STDIN env
        $_SERVER['REQUEST_METHOD'] = 'GET';

        // Set config for Input class
        $this->ci_set_config('allow_get_array',	TRUE);
        $this->ci_set_config('global_xss_filtering', FALSE);
        $this->ci_set_config('csrf_protection', FALSE);

        $security = new Mock_Core_Security();

        $this->ci_set_config('charset', 'UTF-8');
        $utf8 = new Mock_Core_Utf8();

        $inp = new Mock_Core_Input($security, $utf8);
        
        $this->ci_instance_var('lang', $lang);
        $this->ci_instance_var('load', $ldr);
        $this->ci_instance_var('input', $inp);
        
        $this->lang('form_validation');
        $this->helper('form');
    }
    
    public function test___construct()
    {
        $this->form_validation = new CI_Form_validation();

        $this->assertNotNull($this->form_validation);
    }
    
    public function test_rules_valid() 
    {
        $this->form_validation = new CI_Form_validation();
        
        $valid_posts = array(
            'required' => array('required',' !'),
            'matches[sample]' => 'sample',
            'differs[sample]' => 'differ',
            'min_length[4]' => array('is_more_than_4', '1234', '   1'),
            'max_length[8]' => array('less_8', '12345678'),
            'exact_length[5]' => '12345',
            'greater_than[-5]' => array('-4','0','123124451'),
            'greater_than_equal_to[8]' => array('8', '99'),
            'less_than[0]' => '-1',
            'less_than_equal_to[5]' => array('-5', '5'),
            'in_list[red,blue,green]' => array('red', 'blue','green'),
            'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ',
            'alpha_numeric' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789',
            'alpha_numeric_spaces' => ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789',
            'alpha_dash' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-_',
            'numeric' => '0123456789',
            'integer' => array('0', '-1231', '987234'),
            'decimal' => array('0.123', '1.0'),
            'is_natural' => '0',
            'is_natural_no_zero' => '1',
            'valid_url' => array('www.codeigniter.com','http://codeigniter.eu'),
            'valid_email' => 'email@sample.com',
            'valid_emails' => '1@sample.com,2@sample.com',
            'valid_ip[ipv4]' => '127.0.0.1',
            'valid_ip[ipv6]' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
            'valid_base64' => 'string'
            );
          
        // Loop through each rule and test
        foreach ($valid_posts as $rule => $value) {
            // Reset $_POST
            $_POST = array();
            
            if (is_array($value)) {
                foreach($value as $item) {
//                    printf("%s => %s\n", $rule, $item);
                    $this->form_validation->set_rules('field', 'name', $rule);
                    $_POST['field'] = $item;
                    $this->assertTrue($this->form_validation->run());
                    $this->form_validation->reset_validation();
                }
            }
            else {
//                printf("%s => %s\n", $rule, $value);
                $this->form_validation->set_rules('field', 'name', $rule);
                $_POST['field'] = $value;
                $this->assertTrue($this->form_validation->run());
                $this->form_validation->reset_validation();
            }
        }
    }
    
    public function test_rules_invalid()
    {
        $this->form_validation = new CI_Form_validation();
        
        $invalid_posts = array(
            'required' => array('',' '),
            'matches[sample]' => 'Sample',
            'differ[sample]' => 'sample',
            'min_length[4]' => array('123', ''),
            'max_length[8]' => array('more_than_8', '12345678 '),
            'exact_length[5]' => ' 12345',
            'greater_than[-5]' => array('-5, -12415'),
            'greater_than_equal_to[8]' => array('7', '0'),
            'less_than[0]' => '0',
            'less_than_equal_to[5]' => array('6', '98234'),
            'in_list[red,blue,green]' => array(' red', 'Blue','failz'),
            'alpha' => array('*', ' a', '1'),
            'alpha_numeric' => array('a1 ', '*', '1-'),
            'alpha_numeric_spaces' => array('a1*', '  ~'),
            'alpha_dash' => array('a b', '*'),
            'numeric' => array('a', ''),
            'integer' => array('0.123', '1a', ''),
            'decimal' => array('1', 'a',''),
            'is_natural' => array('1.2','aA',''),
            'is_natural_no_zero' => array('0','1.2',''),
            'valid_url' => array('codeigniter.com','nosite', ''),
            'valid_email' => '@sample.com',
            'valid_emails' => '@sample.com,2@sample.com,validemail@email.ca',
            'valid_ip[ipv4]' => '257.0.0.1',
            'valid_ip[ipv6]' => 'A001:0db8:85a3:0000:0000:8a2e:0370:7334',
            );
          
        // Loop through each rule and test
        foreach ($invalid_posts as $rule => $value) {
            // Reset $_POST
            $_POST = array();
            
            if (is_array($value)) {
                foreach($value as $item) {
                    printf("%s => %s\n", $rule, $item);
                    $this->form_validation->set_rules('field', 'name', $rule);
                    $_POST['field'] = $item;
                    $this->assertFalse($this->form_validation->run());
                    $this->form_validation->reset_validation();
                }
            }
            else {
                printf("%s => %s\n", $rule, $value);
                $this->form_validation->set_rules('field', 'name', $rule);
                $_POST['field'] = $value;
                $this->assertFalse($this->form_validation->run());
                $this->form_validation->reset_validation();
            }
        }
    }
   
}