1<?php
2
3use dokuwiki\Input\Input;
4
5/**
6 * Tests for the dokuwiki\Input\Input class
7 */
8class input_test extends DokuWikiTest {
9
10    private $data = array(
11        'array'  => array('foo', 'bar'),
12        'string' => 'foo',
13        'int'    => '17',
14        'zero'   => '0',
15        'one'    => '1',
16        'empty'  => '',
17        'emptya' => array(),
18        'do'     => array('save' => 'Speichern'),
19
20    );
21
22    /**
23     * custom filter function
24     *
25     * @param $string
26     * @return mixed
27     */
28    public function myfilter($string) {
29        $string = str_replace('foo', 'bar', $string);
30        $string = str_replace('baz', '', $string);
31        return $string;
32    }
33
34    public function test_filter() {
35        $_GET     = array(
36            'foo'    => 'foo',
37            'zstring'=> "foo\0bar",
38            'znull'  => "\0",
39            'zint'   => '42'."\0".'42',
40            'zintbaz'=> "baz42",
41        );
42        $_POST    = $_GET;
43        $_REQUEST = $_GET;
44        $INPUT    = new Input();
45
46        $filter = array($this,'myfilter');
47
48        $this->assertNotSame('foobar', $INPUT->str('zstring'));
49        $this->assertSame('foobar', $INPUT->filter()->str('zstring'));
50        $this->assertSame('bar', $INPUT->filter($filter)->str('foo'));
51        $this->assertSame('bar', $INPUT->filter()->str('znull', 'bar', true));
52        $this->assertNotSame('foobar', $INPUT->str('zstring')); // make sure original input is unmodified
53
54        $this->assertNotSame('foobar', $INPUT->get->str('zstring'));
55        $this->assertSame('foobar', $INPUT->get->filter()->str('zstring'));
56        $this->assertSame('bar', $INPUT->get->filter($filter)->str('foo'));
57        $this->assertSame('bar', $INPUT->get->filter()->str('znull', 'bar', true));
58        $this->assertNotSame('foobar', $INPUT->get->str('zstring')); // make sure original input is unmodified
59
60        $this->assertNotSame(4242, $INPUT->int('zint'));
61        $this->assertSame(4242, $INPUT->filter()->int('zint'));
62        $this->assertSame(42, $INPUT->filter($filter)->int('zintbaz'));
63        $this->assertSame(42, $INPUT->filter()->str('znull', 42, true));
64
65        $this->assertSame(true, $INPUT->bool('znull'));
66        $this->assertSame(false, $INPUT->filter()->bool('znull'));
67
68        $this->assertSame('foobar', $INPUT->filter()->valid('zstring', array('foobar', 'bang')));
69    }
70
71    public function test_str() {
72        $_REQUEST      = $this->data;
73        $_POST         = $this->data;
74        $_GET          = $this->data;
75        $_GET['get']   = 1;
76        $_POST['post'] = 1;
77        $INPUT         = new Input();
78
79        $this->assertSame('foo', $INPUT->str('string'));
80        $this->assertSame('', $INPUT->str('none'));
81        $this->assertSame('', $INPUT->str('empty'));
82        $this->assertSame('foo', $INPUT->str('none', 'foo'));
83        $this->assertSame('', $INPUT->str('empty', 'foo'));
84        $this->assertSame('foo', $INPUT->str('empty', 'foo', true));
85
86        $this->assertSame(false, $INPUT->str('get', false));
87        $this->assertSame(false, $INPUT->str('post', false));
88
89        $this->assertSame('foo', $INPUT->post->str('string'));
90        $this->assertSame('', $INPUT->post->str('none'));
91        $this->assertSame('', $INPUT->post->str('empty'));
92        $this->assertSame('foo', $INPUT->post->str('none', 'foo'));
93        $this->assertSame('', $INPUT->post->str('empty', 'foo'));
94        $this->assertSame('foo', $INPUT->post->str('empty', 'foo', true));
95
96        $this->assertSame(false, $INPUT->post->str('get', false));
97        $this->assertSame('1', $INPUT->post->str('post', false));
98
99        $this->assertSame('foo', $INPUT->get->str('string'));
100        $this->assertSame('', $INPUT->get->str('none'));
101        $this->assertSame('', $INPUT->get->str('empty'));
102        $this->assertSame('foo', $INPUT->get->str('none', 'foo'));
103        $this->assertSame('', $INPUT->get->str('empty', 'foo'));
104        $this->assertSame('foo', $INPUT->get->str('empty', 'foo', true));
105
106        $this->assertSame(false, $INPUT->get->str('post', false));
107        $this->assertSame('1', $INPUT->get->str('get', false));
108
109        $this->assertSame('', $INPUT->str('array'));
110    }
111
112    public function test_int() {
113        $_REQUEST      = $this->data;
114        $_POST         = $this->data;
115        $_GET          = $this->data;
116        $_GET['get']   = 1;
117        $_POST['post'] = 1;
118        $INPUT         = new Input();
119
120        $this->assertSame(17, $INPUT->int('int'));
121        $this->assertSame(0, $INPUT->int('none'));
122        $this->assertSame(0, $INPUT->int('empty'));
123        $this->assertSame(42, $INPUT->int('none', 42));
124        $this->assertSame(0, $INPUT->int('zero', 42));
125        $this->assertSame(42, $INPUT->int('zero', 42, true));
126
127        $this->assertSame(false, $INPUT->int('get', false));
128        $this->assertSame(false, $INPUT->int('post', false));
129
130        $this->assertSame(17, $INPUT->post->int('int'));
131        $this->assertSame(0, $INPUT->post->int('none'));
132        $this->assertSame(0, $INPUT->post->int('empty'));
133        $this->assertSame(42, $INPUT->post->int('none', 42));
134        $this->assertSame(0, $INPUT->post->int('zero', 42));
135        $this->assertSame(42, $INPUT->post->int('zero', 42, true));
136
137        $this->assertSame(false, $INPUT->post->int('get', false));
138        $this->assertSame(1, $INPUT->post->int('post', false));
139
140        $this->assertSame(17, $INPUT->post->int('int'));
141        $this->assertSame(0, $INPUT->post->int('none'));
142        $this->assertSame(0, $INPUT->post->int('empty'));
143        $this->assertSame(42, $INPUT->post->int('none', 42));
144        $this->assertSame(0, $INPUT->post->int('zero', 42));
145        $this->assertSame(42, $INPUT->post->int('zero', 42, true));
146
147        $this->assertSame(false, $INPUT->get->int('post', false));
148        $this->assertSame(1, $INPUT->get->int('get', false));
149
150        $this->assertSame(0, $INPUT->int('array'));
151
152        $this->assertSame(0, $INPUT->int('zero', -1));
153        $this->assertSame(-1, $INPUT->int('empty', -1));
154        $this->assertSame(-1, $INPUT->int('zero', -1, true));
155        $this->assertSame(-1, $INPUT->int('empty', -1, true));
156    }
157
158    public function test_arr() {
159        $_REQUEST      = $this->data;
160        $_POST         = $this->data;
161        $_GET          = $this->data;
162        $_GET['get']   = array(1, 2);
163        $_POST['post'] = array(1, 2);
164        $INPUT         = new Input();
165
166        $this->assertSame(array('foo', 'bar'), $INPUT->arr('array'));
167        $this->assertSame(array(), $INPUT->arr('none'));
168        $this->assertSame(array(), $INPUT->arr('empty'));
169        $this->assertSame(array(1, 2), $INPUT->arr('none', array(1, 2)));
170        $this->assertSame(array(), $INPUT->arr('emptya', array(1, 2)));
171        $this->assertSame(array(1, 2), $INPUT->arr('emptya', array(1, 2), true));
172
173        $this->assertSame(false, $INPUT->arr('get', false));
174        $this->assertSame(false, $INPUT->arr('post', false));
175
176        $this->assertSame(array('foo', 'bar'), $INPUT->post->arr('array'));
177        $this->assertSame(array(), $INPUT->post->arr('none'));
178        $this->assertSame(array(), $INPUT->post->arr('empty'));
179        $this->assertSame(array(1, 2), $INPUT->post->arr('none', array(1, 2)));
180        $this->assertSame(array(), $INPUT->post->arr('emptya', array(1, 2)));
181        $this->assertSame(array(1, 2), $INPUT->post->arr('emptya', array(1, 2), true));
182
183        $this->assertSame(false, $INPUT->post->arr('get', false));
184        $this->assertSame(array(1, 2), $INPUT->post->arr('post', false));
185
186        $this->assertSame(array('foo', 'bar'), $INPUT->get->arr('array'));
187        $this->assertSame(array(), $INPUT->get->arr('none'));
188        $this->assertSame(array(), $INPUT->get->arr('empty'));
189        $this->assertSame(array(1, 2), $INPUT->get->arr('none', array(1, 2)));
190        $this->assertSame(array(), $INPUT->get->arr('emptya', array(1, 2)));
191        $this->assertSame(array(1, 2), $INPUT->get->arr('emptya', array(1, 2), true));
192
193        $this->assertSame(array(1, 2), $INPUT->get->arr('get', false));
194        $this->assertSame(false, $INPUT->get->arr('post', false));
195    }
196
197    public function test_bool() {
198        $_REQUEST      = $this->data;
199        $_POST         = $this->data;
200        $_GET          = $this->data;
201        $_GET['get']   = '1';
202        $_POST['post'] = '1';
203        $INPUT         = new Input();
204
205        $this->assertSame(true, $INPUT->bool('one'));
206        $this->assertSame(false, $INPUT->bool('zero'));
207
208        $this->assertSame(false, $INPUT->bool('get'));
209        $this->assertSame(false, $INPUT->bool('post'));
210
211        $this->assertSame(true, $INPUT->post->bool('one'));
212        $this->assertSame(false, $INPUT->post->bool('zero'));
213
214        $this->assertSame(false, $INPUT->post->bool('get'));
215        $this->assertSame(true, $INPUT->post->bool('post'));
216
217        $this->assertSame(false, $INPUT->bool('zero', -1));
218        $this->assertSame(-1, $INPUT->bool('empty', -1));
219        $this->assertSame(-1, $INPUT->bool('zero', -1, true));
220        $this->assertSame(-1, $INPUT->bool('empty', -1, true));
221    }
222
223    public function test_remove() {
224        $_REQUEST = $this->data;
225        $_POST    = $this->data;
226        $_GET     = $this->data;
227        $INPUT    = new Input();
228
229        $INPUT->remove('string');
230        $this->assertArrayNotHasKey('string', $_REQUEST);
231        $this->assertArrayNotHasKey('string', $_POST);
232        $this->assertArrayNotHasKey('string', $_GET);
233
234        $INPUT->post->remove('int');
235        $this->assertArrayNotHasKey('int', $_POST);
236        $this->assertEquals(17, $_GET['int']);
237        $this->assertEquals(17, $_REQUEST['int']);
238    }
239
240    public function test_set(){
241        $_REQUEST = $this->data;
242        $_POST    = $this->data;
243        $_GET     = $this->data;
244        $INPUT    = new Input();
245
246        $INPUT->set('test','foo');
247        $this->assertEquals('foo',$_REQUEST['test']);
248        $this->assertArrayNotHasKey('test', $_POST);
249        $this->assertArrayNotHasKey('test', $_GET);
250
251        $INPUT->get->set('test2','foo');
252        $this->assertEquals('foo',$_GET['test2']);
253        $this->assertEquals('foo',$_REQUEST['test2']);
254        $this->assertArrayNotHasKey('test2', $_POST);
255    }
256
257    public function test_ref(){
258        $_REQUEST = $this->data;
259        $_POST    = $this->data;
260        $_GET     = $this->data;
261        $INPUT    = new Input();
262
263        $test = &$INPUT->ref('string');
264        $this->assertEquals('foo',$test);
265        $_REQUEST['string'] = 'bla';
266        $this->assertEquals('bla',$test);
267    }
268
269    public function test_valid(){
270        $_REQUEST = $this->data;
271        $_POST    = $this->data;
272        $_GET     = $this->data;
273        $INPUT    = new Input();
274
275        $valids = array(17, 'foo');
276        $this->assertSame(null, $INPUT->valid('nope', $valids));
277        $this->assertSame('bang', $INPUT->valid('nope', $valids, 'bang'));
278        $this->assertSame(17, $INPUT->valid('int', $valids));
279        $this->assertSame('foo', $INPUT->valid('string', $valids));
280        $this->assertSame(null, $INPUT->valid('array', $valids));
281
282        $valids = array(true);
283        $this->assertSame(true, $INPUT->valid('string', $valids));
284        $this->assertSame(true, $INPUT->valid('one', $valids));
285        $this->assertSame(null, $INPUT->valid('zero', $valids));
286    }
287
288    public function test_extract(){
289        $_REQUEST = $this->data;
290        $_POST    = $this->data;
291        $_GET     = $this->data;
292        $INPUT    = new Input();
293
294        $this->assertEquals('save', $INPUT->extract('do')->str('do'));
295        $this->assertEquals('', $INPUT->extract('emptya')->str('emptya'));
296        $this->assertEquals('foo', $INPUT->extract('string')->str('string'));
297        $this->assertEquals('foo', $INPUT->extract('array')->str('array'));
298
299        $this->assertEquals('save', $INPUT->post->extract('do')->str('do'));
300        $this->assertEquals('', $INPUT->post->extract('emptya')->str('emptya'));
301        $this->assertEquals('foo', $INPUT->post->extract('string')->str('string'));
302        $this->assertEquals('foo', $INPUT->post->extract('array')->str('array'));
303    }
304}
305