xref: /dokuwiki/_test/tests/inc/input.test.php (revision 591acd873d64abb271864b581c48ca419aa5d329)
1<?php
2
3/**
4 * Tests for the Input class
5 */
6class input_test extends DokuWikiTest {
7
8    private $data = array(
9        'array'  => array('foo', 'bar'),
10        'string' => 'foo',
11        'int'    => '17',
12        'zero'   => '0',
13        'one'    => '1',
14        'empty'  => '',
15        'emptya' => array()
16    );
17
18    public function test_str() {
19        $_REQUEST      = $this->data;
20        $_POST         = $this->data;
21        $_GET          = $this->data;
22        $_GET['get']   = 1;
23        $_POST['post'] = 1;
24        $INPUT         = new Input();
25
26        $this->assertSame('foo', $INPUT->str('string'));
27        $this->assertSame('', $INPUT->str('none'));
28        $this->assertSame('', $INPUT->str('empty'));
29        $this->assertSame('foo', $INPUT->str('none', 'foo'));
30        $this->assertSame('', $INPUT->str('empty', 'foo'));
31        $this->assertSame('foo', $INPUT->str('empty', 'foo', true));
32
33        $this->assertSame(false, $INPUT->str('get', false));
34        $this->assertSame(false, $INPUT->str('post', false));
35
36        $this->assertSame('foo', $INPUT->post->str('string'));
37        $this->assertSame('', $INPUT->post->str('none'));
38        $this->assertSame('', $INPUT->post->str('empty'));
39        $this->assertSame('foo', $INPUT->post->str('none', 'foo'));
40        $this->assertSame('', $INPUT->post->str('empty', 'foo'));
41        $this->assertSame('foo', $INPUT->post->str('empty', 'foo', true));
42
43        $this->assertSame(false, $INPUT->post->str('get', false));
44        $this->assertSame('1', $INPUT->post->str('post', false));
45
46        $this->assertSame('foo', $INPUT->get->str('string'));
47        $this->assertSame('', $INPUT->get->str('none'));
48        $this->assertSame('', $INPUT->get->str('empty'));
49        $this->assertSame('foo', $INPUT->get->str('none', 'foo'));
50        $this->assertSame('', $INPUT->get->str('empty', 'foo'));
51        $this->assertSame('foo', $INPUT->get->str('empty', 'foo', true));
52
53        $this->assertSame(false, $INPUT->get->str('post', false));
54        $this->assertSame('1', $INPUT->get->str('get', false));
55
56        $this->assertSame('', $INPUT->str('array'));
57    }
58
59    public function test_int() {
60        $_REQUEST      = $this->data;
61        $_POST         = $this->data;
62        $_GET          = $this->data;
63        $_GET['get']   = 1;
64        $_POST['post'] = 1;
65        $INPUT         = new Input();
66
67        $this->assertSame(17, $INPUT->int('int'));
68        $this->assertSame(0, $INPUT->int('none'));
69        $this->assertSame(0, $INPUT->int('empty'));
70        $this->assertSame(42, $INPUT->int('none', 42));
71        $this->assertSame(0, $INPUT->int('zero', 42));
72        $this->assertSame(42, $INPUT->int('zero', 42, true));
73
74        $this->assertSame(false, $INPUT->int('get', false));
75        $this->assertSame(false, $INPUT->int('post', false));
76
77        $this->assertSame(17, $INPUT->post->int('int'));
78        $this->assertSame(0, $INPUT->post->int('none'));
79        $this->assertSame(0, $INPUT->post->int('empty'));
80        $this->assertSame(42, $INPUT->post->int('none', 42));
81        $this->assertSame(0, $INPUT->post->int('zero', 42));
82        $this->assertSame(42, $INPUT->post->int('zero', 42, true));
83
84        $this->assertSame(false, $INPUT->post->int('get', false));
85        $this->assertSame(1, $INPUT->post->int('post', false));
86
87        $this->assertSame(17, $INPUT->post->int('int'));
88        $this->assertSame(0, $INPUT->post->int('none'));
89        $this->assertSame(0, $INPUT->post->int('empty'));
90        $this->assertSame(42, $INPUT->post->int('none', 42));
91        $this->assertSame(0, $INPUT->post->int('zero', 42));
92        $this->assertSame(42, $INPUT->post->int('zero', 42, true));
93
94        $this->assertSame(false, $INPUT->get->int('post', false));
95        $this->assertSame(1, $INPUT->get->int('get', false));
96
97        $this->assertSame(0, $INPUT->int('array'));
98    }
99
100    public function test_arr() {
101        $_REQUEST      = $this->data;
102        $_POST         = $this->data;
103        $_GET          = $this->data;
104        $_GET['get']   = array(1, 2);
105        $_POST['post'] = array(1, 2);
106        $INPUT         = new Input();
107
108        $this->assertSame(array('foo', 'bar'), $INPUT->arr('array'));
109        $this->assertSame(array(), $INPUT->arr('none'));
110        $this->assertSame(array(), $INPUT->arr('empty'));
111        $this->assertSame(array(1, 2), $INPUT->arr('none', array(1, 2)));
112        $this->assertSame(array(), $INPUT->arr('emptya', array(1, 2)));
113        $this->assertSame(array(1, 2), $INPUT->arr('emptya', array(1, 2), true));
114
115        $this->assertSame(false, $INPUT->arr('get', false));
116        $this->assertSame(false, $INPUT->arr('post', false));
117
118        $this->assertSame(array('foo', 'bar'), $INPUT->post->arr('array'));
119        $this->assertSame(array(), $INPUT->post->arr('none'));
120        $this->assertSame(array(), $INPUT->post->arr('empty'));
121        $this->assertSame(array(1, 2), $INPUT->post->arr('none', array(1, 2)));
122        $this->assertSame(array(), $INPUT->post->arr('emptya', array(1, 2)));
123        $this->assertSame(array(1, 2), $INPUT->post->arr('emptya', array(1, 2), true));
124
125        $this->assertSame(false, $INPUT->post->arr('get', false));
126        $this->assertSame(array(1, 2), $INPUT->post->arr('post', false));
127
128        $this->assertSame(array('foo', 'bar'), $INPUT->get->arr('array'));
129        $this->assertSame(array(), $INPUT->get->arr('none'));
130        $this->assertSame(array(), $INPUT->get->arr('empty'));
131        $this->assertSame(array(1, 2), $INPUT->get->arr('none', array(1, 2)));
132        $this->assertSame(array(), $INPUT->get->arr('emptya', array(1, 2)));
133        $this->assertSame(array(1, 2), $INPUT->get->arr('emptya', array(1, 2), true));
134
135        $this->assertSame(array(1, 2), $INPUT->get->arr('get', false));
136        $this->assertSame(false, $INPUT->get->arr('post', false));
137    }
138
139    public function test_bool() {
140        $_REQUEST      = $this->data;
141        $_POST         = $this->data;
142        $_GET          = $this->data;
143        $_GET['get']   = '1';
144        $_POST['post'] = '1';
145        $INPUT         = new Input();
146
147        $this->assertSame(true, $INPUT->bool('one'));
148        $this->assertSame(false, $INPUT->bool('zero'));
149
150        $this->assertSame(false, $INPUT->bool('get'));
151        $this->assertSame(false, $INPUT->bool('post'));
152
153        $this->assertSame(true, $INPUT->post->bool('one'));
154        $this->assertSame(false, $INPUT->post->bool('zero'));
155
156        $this->assertSame(false, $INPUT->post->bool('get'));
157        $this->assertSame(true, $INPUT->post->bool('post'));
158    }
159
160    public function test_remove() {
161        $_REQUEST = $this->data;
162        $_POST    = $this->data;
163        $_GET     = $this->data;
164        $INPUT    = new Input();
165
166        $INPUT->remove('string');
167        $this->assertNull($_REQUEST['string']);
168        $this->assertNull($_POST['string']);
169        $this->assertNull($_GET['string']);
170
171        $INPUT->post->remove('int');
172        $this->assertNull($_POST['int']);
173        $this->assertEquals(17, $_GET['int']);
174        $this->assertEquals(17, $_REQUEST['int']);
175    }
176
177    public function test_set(){
178        $_REQUEST = $this->data;
179        $_POST    = $this->data;
180        $_GET     = $this->data;
181        $INPUT    = new Input();
182
183        $INPUT->set('test','foo');
184        $this->assertEquals('foo',$_REQUEST['test']);
185        $this->assertNull($_POST['test']);
186        $this->assertNull($_GET['test']);
187
188        $INPUT->get->set('test2','foo');
189        $this->assertEquals('foo',$_GET['test2']);
190        $this->assertEquals('foo',$_REQUEST['test2']);
191        $this->assertNull($_POST['test']);
192    }
193
194    public function test_ref(){
195        $_REQUEST = $this->data;
196        $_POST    = $this->data;
197        $_GET     = $this->data;
198        $INPUT    = new Input();
199
200        $test = &$INPUT->ref('string');
201        $this->assertEquals('foo',$test);
202        $_REQUEST['string'] = 'bla';
203        $this->assertEquals('bla',$test);
204    }
205
206}