summaryrefslogtreecommitdiff
path: root/_test/tests/inc/input.test.php
blob: 59b5ea4b99df67594ab327d4c4a51db560cb5953 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php

/**
 * Tests for the Input class
 */
class input_test extends DokuWikiTest {

    private $data = array(
        'array'  => array('foo', 'bar'),
        'string' => 'foo',
        'int'    => '17',
        'zero'   => '0',
        'one'    => '1',
        'empty'  => '',
        'emptya' => array(),
        'do'     => array('save' => 'Speichern'),
    );

    public function test_str() {
        $_REQUEST      = $this->data;
        $_POST         = $this->data;
        $_GET          = $this->data;
        $_GET['get']   = 1;
        $_POST['post'] = 1;
        $INPUT         = new Input();

        $this->assertSame('foo', $INPUT->str('string'));
        $this->assertSame('', $INPUT->str('none'));
        $this->assertSame('', $INPUT->str('empty'));
        $this->assertSame('foo', $INPUT->str('none', 'foo'));
        $this->assertSame('', $INPUT->str('empty', 'foo'));
        $this->assertSame('foo', $INPUT->str('empty', 'foo', true));

        $this->assertSame(false, $INPUT->str('get', false));
        $this->assertSame(false, $INPUT->str('post', false));

        $this->assertSame('foo', $INPUT->post->str('string'));
        $this->assertSame('', $INPUT->post->str('none'));
        $this->assertSame('', $INPUT->post->str('empty'));
        $this->assertSame('foo', $INPUT->post->str('none', 'foo'));
        $this->assertSame('', $INPUT->post->str('empty', 'foo'));
        $this->assertSame('foo', $INPUT->post->str('empty', 'foo', true));

        $this->assertSame(false, $INPUT->post->str('get', false));
        $this->assertSame('1', $INPUT->post->str('post', false));

        $this->assertSame('foo', $INPUT->get->str('string'));
        $this->assertSame('', $INPUT->get->str('none'));
        $this->assertSame('', $INPUT->get->str('empty'));
        $this->assertSame('foo', $INPUT->get->str('none', 'foo'));
        $this->assertSame('', $INPUT->get->str('empty', 'foo'));
        $this->assertSame('foo', $INPUT->get->str('empty', 'foo', true));

        $this->assertSame(false, $INPUT->get->str('post', false));
        $this->assertSame('1', $INPUT->get->str('get', false));

        $this->assertSame('', $INPUT->str('array'));
    }

    public function test_int() {
        $_REQUEST      = $this->data;
        $_POST         = $this->data;
        $_GET          = $this->data;
        $_GET['get']   = 1;
        $_POST['post'] = 1;
        $INPUT         = new Input();

        $this->assertSame(17, $INPUT->int('int'));
        $this->assertSame(0, $INPUT->int('none'));
        $this->assertSame(0, $INPUT->int('empty'));
        $this->assertSame(42, $INPUT->int('none', 42));
        $this->assertSame(0, $INPUT->int('zero', 42));
        $this->assertSame(42, $INPUT->int('zero', 42, true));

        $this->assertSame(false, $INPUT->int('get', false));
        $this->assertSame(false, $INPUT->int('post', false));

        $this->assertSame(17, $INPUT->post->int('int'));
        $this->assertSame(0, $INPUT->post->int('none'));
        $this->assertSame(0, $INPUT->post->int('empty'));
        $this->assertSame(42, $INPUT->post->int('none', 42));
        $this->assertSame(0, $INPUT->post->int('zero', 42));
        $this->assertSame(42, $INPUT->post->int('zero', 42, true));

        $this->assertSame(false, $INPUT->post->int('get', false));
        $this->assertSame(1, $INPUT->post->int('post', false));

        $this->assertSame(17, $INPUT->post->int('int'));
        $this->assertSame(0, $INPUT->post->int('none'));
        $this->assertSame(0, $INPUT->post->int('empty'));
        $this->assertSame(42, $INPUT->post->int('none', 42));
        $this->assertSame(0, $INPUT->post->int('zero', 42));
        $this->assertSame(42, $INPUT->post->int('zero', 42, true));

        $this->assertSame(false, $INPUT->get->int('post', false));
        $this->assertSame(1, $INPUT->get->int('get', false));

        $this->assertSame(0, $INPUT->int('array'));

        $this->assertSame(0, $INPUT->int('zero', -1));
        $this->assertSame(-1, $INPUT->int('empty', -1));
        $this->assertSame(-1, $INPUT->int('zero', -1, true));
        $this->assertSame(-1, $INPUT->int('empty', -1, true));
    }

    public function test_arr() {
        $_REQUEST      = $this->data;
        $_POST         = $this->data;
        $_GET          = $this->data;
        $_GET['get']   = array(1, 2);
        $_POST['post'] = array(1, 2);
        $INPUT         = new Input();

        $this->assertSame(array('foo', 'bar'), $INPUT->arr('array'));
        $this->assertSame(array(), $INPUT->arr('none'));
        $this->assertSame(array(), $INPUT->arr('empty'));
        $this->assertSame(array(1, 2), $INPUT->arr('none', array(1, 2)));
        $this->assertSame(array(), $INPUT->arr('emptya', array(1, 2)));
        $this->assertSame(array(1, 2), $INPUT->arr('emptya', array(1, 2), true));

        $this->assertSame(false, $INPUT->arr('get', false));
        $this->assertSame(false, $INPUT->arr('post', false));

        $this->assertSame(array('foo', 'bar'), $INPUT->post->arr('array'));
        $this->assertSame(array(), $INPUT->post->arr('none'));
        $this->assertSame(array(), $INPUT->post->arr('empty'));
        $this->assertSame(array(1, 2), $INPUT->post->arr('none', array(1, 2)));
        $this->assertSame(array(), $INPUT->post->arr('emptya', array(1, 2)));
        $this->assertSame(array(1, 2), $INPUT->post->arr('emptya', array(1, 2), true));

        $this->assertSame(false, $INPUT->post->arr('get', false));
        $this->assertSame(array(1, 2), $INPUT->post->arr('post', false));

        $this->assertSame(array('foo', 'bar'), $INPUT->get->arr('array'));
        $this->assertSame(array(), $INPUT->get->arr('none'));
        $this->assertSame(array(), $INPUT->get->arr('empty'));
        $this->assertSame(array(1, 2), $INPUT->get->arr('none', array(1, 2)));
        $this->assertSame(array(), $INPUT->get->arr('emptya', array(1, 2)));
        $this->assertSame(array(1, 2), $INPUT->get->arr('emptya', array(1, 2), true));

        $this->assertSame(array(1, 2), $INPUT->get->arr('get', false));
        $this->assertSame(false, $INPUT->get->arr('post', false));
    }

    public function test_bool() {
        $_REQUEST      = $this->data;
        $_POST         = $this->data;
        $_GET          = $this->data;
        $_GET['get']   = '1';
        $_POST['post'] = '1';
        $INPUT         = new Input();

        $this->assertSame(true, $INPUT->bool('one'));
        $this->assertSame(false, $INPUT->bool('zero'));

        $this->assertSame(false, $INPUT->bool('get'));
        $this->assertSame(false, $INPUT->bool('post'));

        $this->assertSame(true, $INPUT->post->bool('one'));
        $this->assertSame(false, $INPUT->post->bool('zero'));

        $this->assertSame(false, $INPUT->post->bool('get'));
        $this->assertSame(true, $INPUT->post->bool('post'));

        $this->assertSame(false, $INPUT->bool('zero', -1));
        $this->assertSame(-1, $INPUT->bool('empty', -1));
        $this->assertSame(-1, $INPUT->bool('zero', -1, true));
        $this->assertSame(-1, $INPUT->bool('empty', -1, true));
    }

    public function test_remove() {
        $_REQUEST = $this->data;
        $_POST    = $this->data;
        $_GET     = $this->data;
        $INPUT    = new Input();

        $INPUT->remove('string');
        $this->assertNull($_REQUEST['string']);
        $this->assertNull($_POST['string']);
        $this->assertNull($_GET['string']);

        $INPUT->post->remove('int');
        $this->assertNull($_POST['int']);
        $this->assertEquals(17, $_GET['int']);
        $this->assertEquals(17, $_REQUEST['int']);
    }

    public function test_set(){
        $_REQUEST = $this->data;
        $_POST    = $this->data;
        $_GET     = $this->data;
        $INPUT    = new Input();

        $INPUT->set('test','foo');
        $this->assertEquals('foo',$_REQUEST['test']);
        $this->assertNull($_POST['test']);
        $this->assertNull($_GET['test']);

        $INPUT->get->set('test2','foo');
        $this->assertEquals('foo',$_GET['test2']);
        $this->assertEquals('foo',$_REQUEST['test2']);
        $this->assertNull($_POST['test']);
    }

    public function test_ref(){
        $_REQUEST = $this->data;
        $_POST    = $this->data;
        $_GET     = $this->data;
        $INPUT    = new Input();

        $test = &$INPUT->ref('string');
        $this->assertEquals('foo',$test);
        $_REQUEST['string'] = 'bla';
        $this->assertEquals('bla',$test);
    }

    public function test_extract(){
        $_REQUEST = $this->data;
        $_POST    = $this->data;
        $_GET     = $this->data;
        $INPUT    = new Input();

        $this->assertEquals('save', $INPUT->extract('do')->str('do'));
        $this->assertEquals('', $INPUT->extract('emptya')->str('emptya'));
        $this->assertEquals('foo', $INPUT->extract('string')->str('string'));
        $this->assertEquals('foo', $INPUT->extract('array')->str('array'));

        $this->assertEquals('save', $INPUT->post->extract('do')->str('do'));
        $this->assertEquals('', $INPUT->post->extract('emptya')->str('emptya'));
        $this->assertEquals('foo', $INPUT->post->extract('string')->str('string'));
        $this->assertEquals('foo', $INPUT->post->extract('array')->str('array'));
    }
}