xref: /dokuwiki/_test/tests/inc/input.test.php (revision 5d0aaf958325f500ce69cfb79e69eb0d8f83fdeb)
1591acd87SAndreas Gohr<?php
2591acd87SAndreas Gohr
3591acd87SAndreas Gohr/**
4591acd87SAndreas Gohr * Tests for the Input class
5591acd87SAndreas Gohr */
6591acd87SAndreas Gohrclass input_test extends DokuWikiTest {
7591acd87SAndreas Gohr
8591acd87SAndreas Gohr    private $data = array(
9591acd87SAndreas Gohr        'array'  => array('foo', 'bar'),
10591acd87SAndreas Gohr        'string' => 'foo',
11591acd87SAndreas Gohr        'int'    => '17',
12591acd87SAndreas Gohr        'zero'   => '0',
13591acd87SAndreas Gohr        'one'    => '1',
14591acd87SAndreas Gohr        'empty'  => '',
15591acd87SAndreas Gohr        'emptya' => array()
16591acd87SAndreas Gohr    );
17591acd87SAndreas Gohr
18591acd87SAndreas Gohr    public function test_str() {
19591acd87SAndreas Gohr        $_REQUEST      = $this->data;
20591acd87SAndreas Gohr        $_POST         = $this->data;
21591acd87SAndreas Gohr        $_GET          = $this->data;
22591acd87SAndreas Gohr        $_GET['get']   = 1;
23591acd87SAndreas Gohr        $_POST['post'] = 1;
24591acd87SAndreas Gohr        $INPUT         = new Input();
25591acd87SAndreas Gohr
26591acd87SAndreas Gohr        $this->assertSame('foo', $INPUT->str('string'));
27591acd87SAndreas Gohr        $this->assertSame('', $INPUT->str('none'));
28591acd87SAndreas Gohr        $this->assertSame('', $INPUT->str('empty'));
29591acd87SAndreas Gohr        $this->assertSame('foo', $INPUT->str('none', 'foo'));
30591acd87SAndreas Gohr        $this->assertSame('', $INPUT->str('empty', 'foo'));
31591acd87SAndreas Gohr        $this->assertSame('foo', $INPUT->str('empty', 'foo', true));
32591acd87SAndreas Gohr
33591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->str('get', false));
34591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->str('post', false));
35591acd87SAndreas Gohr
36591acd87SAndreas Gohr        $this->assertSame('foo', $INPUT->post->str('string'));
37591acd87SAndreas Gohr        $this->assertSame('', $INPUT->post->str('none'));
38591acd87SAndreas Gohr        $this->assertSame('', $INPUT->post->str('empty'));
39591acd87SAndreas Gohr        $this->assertSame('foo', $INPUT->post->str('none', 'foo'));
40591acd87SAndreas Gohr        $this->assertSame('', $INPUT->post->str('empty', 'foo'));
41591acd87SAndreas Gohr        $this->assertSame('foo', $INPUT->post->str('empty', 'foo', true));
42591acd87SAndreas Gohr
43591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->post->str('get', false));
44591acd87SAndreas Gohr        $this->assertSame('1', $INPUT->post->str('post', false));
45591acd87SAndreas Gohr
46591acd87SAndreas Gohr        $this->assertSame('foo', $INPUT->get->str('string'));
47591acd87SAndreas Gohr        $this->assertSame('', $INPUT->get->str('none'));
48591acd87SAndreas Gohr        $this->assertSame('', $INPUT->get->str('empty'));
49591acd87SAndreas Gohr        $this->assertSame('foo', $INPUT->get->str('none', 'foo'));
50591acd87SAndreas Gohr        $this->assertSame('', $INPUT->get->str('empty', 'foo'));
51591acd87SAndreas Gohr        $this->assertSame('foo', $INPUT->get->str('empty', 'foo', true));
52591acd87SAndreas Gohr
53591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->get->str('post', false));
54591acd87SAndreas Gohr        $this->assertSame('1', $INPUT->get->str('get', false));
55591acd87SAndreas Gohr
56591acd87SAndreas Gohr        $this->assertSame('', $INPUT->str('array'));
57591acd87SAndreas Gohr    }
58591acd87SAndreas Gohr
59591acd87SAndreas Gohr    public function test_int() {
60591acd87SAndreas Gohr        $_REQUEST      = $this->data;
61591acd87SAndreas Gohr        $_POST         = $this->data;
62591acd87SAndreas Gohr        $_GET          = $this->data;
63591acd87SAndreas Gohr        $_GET['get']   = 1;
64591acd87SAndreas Gohr        $_POST['post'] = 1;
65591acd87SAndreas Gohr        $INPUT         = new Input();
66591acd87SAndreas Gohr
67591acd87SAndreas Gohr        $this->assertSame(17, $INPUT->int('int'));
68591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->int('none'));
69591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->int('empty'));
70591acd87SAndreas Gohr        $this->assertSame(42, $INPUT->int('none', 42));
71591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->int('zero', 42));
72591acd87SAndreas Gohr        $this->assertSame(42, $INPUT->int('zero', 42, true));
73591acd87SAndreas Gohr
74591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->int('get', false));
75591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->int('post', false));
76591acd87SAndreas Gohr
77591acd87SAndreas Gohr        $this->assertSame(17, $INPUT->post->int('int'));
78591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->post->int('none'));
79591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->post->int('empty'));
80591acd87SAndreas Gohr        $this->assertSame(42, $INPUT->post->int('none', 42));
81591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->post->int('zero', 42));
82591acd87SAndreas Gohr        $this->assertSame(42, $INPUT->post->int('zero', 42, true));
83591acd87SAndreas Gohr
84591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->post->int('get', false));
85591acd87SAndreas Gohr        $this->assertSame(1, $INPUT->post->int('post', false));
86591acd87SAndreas Gohr
87591acd87SAndreas Gohr        $this->assertSame(17, $INPUT->post->int('int'));
88591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->post->int('none'));
89591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->post->int('empty'));
90591acd87SAndreas Gohr        $this->assertSame(42, $INPUT->post->int('none', 42));
91591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->post->int('zero', 42));
92591acd87SAndreas Gohr        $this->assertSame(42, $INPUT->post->int('zero', 42, true));
93591acd87SAndreas Gohr
94591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->get->int('post', false));
95591acd87SAndreas Gohr        $this->assertSame(1, $INPUT->get->int('get', false));
96591acd87SAndreas Gohr
97591acd87SAndreas Gohr        $this->assertSame(0, $INPUT->int('array'));
98*5d0aaf95SAndreas Gohr
99*5d0aaf95SAndreas Gohr        $this->assertSame(0, $INPUT->int('zero', -1));
100*5d0aaf95SAndreas Gohr        $this->assertSame(-1, $INPUT->int('empty', -1));
101*5d0aaf95SAndreas Gohr        $this->assertSame(-1, $INPUT->int('zero', -1, true));
102*5d0aaf95SAndreas Gohr        $this->assertSame(-1, $INPUT->int('empty', -1, true));
103591acd87SAndreas Gohr    }
104591acd87SAndreas Gohr
105591acd87SAndreas Gohr    public function test_arr() {
106591acd87SAndreas Gohr        $_REQUEST      = $this->data;
107591acd87SAndreas Gohr        $_POST         = $this->data;
108591acd87SAndreas Gohr        $_GET          = $this->data;
109591acd87SAndreas Gohr        $_GET['get']   = array(1, 2);
110591acd87SAndreas Gohr        $_POST['post'] = array(1, 2);
111591acd87SAndreas Gohr        $INPUT         = new Input();
112591acd87SAndreas Gohr
113591acd87SAndreas Gohr        $this->assertSame(array('foo', 'bar'), $INPUT->arr('array'));
114591acd87SAndreas Gohr        $this->assertSame(array(), $INPUT->arr('none'));
115591acd87SAndreas Gohr        $this->assertSame(array(), $INPUT->arr('empty'));
116591acd87SAndreas Gohr        $this->assertSame(array(1, 2), $INPUT->arr('none', array(1, 2)));
117591acd87SAndreas Gohr        $this->assertSame(array(), $INPUT->arr('emptya', array(1, 2)));
118591acd87SAndreas Gohr        $this->assertSame(array(1, 2), $INPUT->arr('emptya', array(1, 2), true));
119591acd87SAndreas Gohr
120591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->arr('get', false));
121591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->arr('post', false));
122591acd87SAndreas Gohr
123591acd87SAndreas Gohr        $this->assertSame(array('foo', 'bar'), $INPUT->post->arr('array'));
124591acd87SAndreas Gohr        $this->assertSame(array(), $INPUT->post->arr('none'));
125591acd87SAndreas Gohr        $this->assertSame(array(), $INPUT->post->arr('empty'));
126591acd87SAndreas Gohr        $this->assertSame(array(1, 2), $INPUT->post->arr('none', array(1, 2)));
127591acd87SAndreas Gohr        $this->assertSame(array(), $INPUT->post->arr('emptya', array(1, 2)));
128591acd87SAndreas Gohr        $this->assertSame(array(1, 2), $INPUT->post->arr('emptya', array(1, 2), true));
129591acd87SAndreas Gohr
130591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->post->arr('get', false));
131591acd87SAndreas Gohr        $this->assertSame(array(1, 2), $INPUT->post->arr('post', false));
132591acd87SAndreas Gohr
133591acd87SAndreas Gohr        $this->assertSame(array('foo', 'bar'), $INPUT->get->arr('array'));
134591acd87SAndreas Gohr        $this->assertSame(array(), $INPUT->get->arr('none'));
135591acd87SAndreas Gohr        $this->assertSame(array(), $INPUT->get->arr('empty'));
136591acd87SAndreas Gohr        $this->assertSame(array(1, 2), $INPUT->get->arr('none', array(1, 2)));
137591acd87SAndreas Gohr        $this->assertSame(array(), $INPUT->get->arr('emptya', array(1, 2)));
138591acd87SAndreas Gohr        $this->assertSame(array(1, 2), $INPUT->get->arr('emptya', array(1, 2), true));
139591acd87SAndreas Gohr
140591acd87SAndreas Gohr        $this->assertSame(array(1, 2), $INPUT->get->arr('get', false));
141591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->get->arr('post', false));
142591acd87SAndreas Gohr    }
143591acd87SAndreas Gohr
144591acd87SAndreas Gohr    public function test_bool() {
145591acd87SAndreas Gohr        $_REQUEST      = $this->data;
146591acd87SAndreas Gohr        $_POST         = $this->data;
147591acd87SAndreas Gohr        $_GET          = $this->data;
148591acd87SAndreas Gohr        $_GET['get']   = '1';
149591acd87SAndreas Gohr        $_POST['post'] = '1';
150591acd87SAndreas Gohr        $INPUT         = new Input();
151591acd87SAndreas Gohr
152591acd87SAndreas Gohr        $this->assertSame(true, $INPUT->bool('one'));
153591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->bool('zero'));
154591acd87SAndreas Gohr
155591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->bool('get'));
156591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->bool('post'));
157591acd87SAndreas Gohr
158591acd87SAndreas Gohr        $this->assertSame(true, $INPUT->post->bool('one'));
159591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->post->bool('zero'));
160591acd87SAndreas Gohr
161591acd87SAndreas Gohr        $this->assertSame(false, $INPUT->post->bool('get'));
162591acd87SAndreas Gohr        $this->assertSame(true, $INPUT->post->bool('post'));
163*5d0aaf95SAndreas Gohr
164*5d0aaf95SAndreas Gohr        $this->assertSame(false, $INPUT->bool('zero', -1));
165*5d0aaf95SAndreas Gohr        $this->assertSame(-1, $INPUT->bool('empty', -1));
166*5d0aaf95SAndreas Gohr        $this->assertSame(-1, $INPUT->bool('zero', -1, true));
167*5d0aaf95SAndreas Gohr        $this->assertSame(-1, $INPUT->bool('empty', -1, true));
168591acd87SAndreas Gohr    }
169591acd87SAndreas Gohr
170591acd87SAndreas Gohr    public function test_remove() {
171591acd87SAndreas Gohr        $_REQUEST = $this->data;
172591acd87SAndreas Gohr        $_POST    = $this->data;
173591acd87SAndreas Gohr        $_GET     = $this->data;
174591acd87SAndreas Gohr        $INPUT    = new Input();
175591acd87SAndreas Gohr
176591acd87SAndreas Gohr        $INPUT->remove('string');
177591acd87SAndreas Gohr        $this->assertNull($_REQUEST['string']);
178591acd87SAndreas Gohr        $this->assertNull($_POST['string']);
179591acd87SAndreas Gohr        $this->assertNull($_GET['string']);
180591acd87SAndreas Gohr
181591acd87SAndreas Gohr        $INPUT->post->remove('int');
182591acd87SAndreas Gohr        $this->assertNull($_POST['int']);
183591acd87SAndreas Gohr        $this->assertEquals(17, $_GET['int']);
184591acd87SAndreas Gohr        $this->assertEquals(17, $_REQUEST['int']);
185591acd87SAndreas Gohr    }
186591acd87SAndreas Gohr
187591acd87SAndreas Gohr    public function test_set(){
188591acd87SAndreas Gohr        $_REQUEST = $this->data;
189591acd87SAndreas Gohr        $_POST    = $this->data;
190591acd87SAndreas Gohr        $_GET     = $this->data;
191591acd87SAndreas Gohr        $INPUT    = new Input();
192591acd87SAndreas Gohr
193591acd87SAndreas Gohr        $INPUT->set('test','foo');
194591acd87SAndreas Gohr        $this->assertEquals('foo',$_REQUEST['test']);
195591acd87SAndreas Gohr        $this->assertNull($_POST['test']);
196591acd87SAndreas Gohr        $this->assertNull($_GET['test']);
197591acd87SAndreas Gohr
198591acd87SAndreas Gohr        $INPUT->get->set('test2','foo');
199591acd87SAndreas Gohr        $this->assertEquals('foo',$_GET['test2']);
200591acd87SAndreas Gohr        $this->assertEquals('foo',$_REQUEST['test2']);
201591acd87SAndreas Gohr        $this->assertNull($_POST['test']);
202591acd87SAndreas Gohr    }
203591acd87SAndreas Gohr
204591acd87SAndreas Gohr    public function test_ref(){
205591acd87SAndreas Gohr        $_REQUEST = $this->data;
206591acd87SAndreas Gohr        $_POST    = $this->data;
207591acd87SAndreas Gohr        $_GET     = $this->data;
208591acd87SAndreas Gohr        $INPUT    = new Input();
209591acd87SAndreas Gohr
210591acd87SAndreas Gohr        $test = &$INPUT->ref('string');
211591acd87SAndreas Gohr        $this->assertEquals('foo',$test);
212591acd87SAndreas Gohr        $_REQUEST['string'] = 'bla';
213591acd87SAndreas Gohr        $this->assertEquals('bla',$test);
214591acd87SAndreas Gohr    }
215591acd87SAndreas Gohr
216591acd87SAndreas Gohr}
217