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