1591acd87SAndreas Gohr<?php 2591acd87SAndreas Gohr 3ccc4c71cSAndreas Gohruse dokuwiki\Input\Input; 4ccc4c71cSAndreas Gohr 5591acd87SAndreas Gohr/** 6ccc4c71cSAndreas Gohr * Tests for the dokuwiki\Input\Input class 7591acd87SAndreas Gohr */ 8591acd87SAndreas Gohrclass input_test extends DokuWikiTest { 9591acd87SAndreas Gohr 10591acd87SAndreas Gohr private $data = array( 11591acd87SAndreas Gohr 'array' => array('foo', 'bar'), 12591acd87SAndreas Gohr 'string' => 'foo', 13591acd87SAndreas Gohr 'int' => '17', 14591acd87SAndreas Gohr 'zero' => '0', 15591acd87SAndreas Gohr 'one' => '1', 16591acd87SAndreas Gohr 'empty' => '', 17d9e9c1bbSAndreas Gohr 'emptya' => array(), 18d9e9c1bbSAndreas Gohr 'do' => array('save' => 'Speichern'), 1937abef5fSAndreas Gohr 20591acd87SAndreas Gohr ); 21591acd87SAndreas Gohr 2237abef5fSAndreas Gohr /** 2337abef5fSAndreas Gohr * custom filter function 2437abef5fSAndreas Gohr * 2537abef5fSAndreas Gohr * @param $string 2637abef5fSAndreas Gohr * @return mixed 2737abef5fSAndreas Gohr */ 2837abef5fSAndreas Gohr public function myfilter($string) { 2937abef5fSAndreas Gohr $string = str_replace('foo', 'bar', $string); 3037abef5fSAndreas Gohr $string = str_replace('baz', '', $string); 3137abef5fSAndreas Gohr return $string; 3237abef5fSAndreas Gohr } 3337abef5fSAndreas Gohr 3437abef5fSAndreas Gohr public function test_filter() { 3537abef5fSAndreas Gohr $_GET = array( 3637abef5fSAndreas Gohr 'foo' => 'foo', 3737abef5fSAndreas Gohr 'zstring'=> "foo\0bar", 3837abef5fSAndreas Gohr 'znull' => "\0", 3937abef5fSAndreas Gohr 'zint' => '42'."\0".'42', 4037abef5fSAndreas Gohr 'zintbaz'=> "baz42", 4137abef5fSAndreas Gohr ); 4237abef5fSAndreas Gohr $_POST = $_GET; 4337abef5fSAndreas Gohr $_REQUEST = $_GET; 4437abef5fSAndreas Gohr $INPUT = new Input(); 4537abef5fSAndreas Gohr 4637abef5fSAndreas Gohr $filter = array($this,'myfilter'); 4737abef5fSAndreas Gohr 4837abef5fSAndreas Gohr $this->assertNotSame('foobar', $INPUT->str('zstring')); 4937abef5fSAndreas Gohr $this->assertSame('foobar', $INPUT->filter()->str('zstring')); 5037abef5fSAndreas Gohr $this->assertSame('bar', $INPUT->filter($filter)->str('foo')); 5137abef5fSAndreas Gohr $this->assertSame('bar', $INPUT->filter()->str('znull', 'bar', true)); 5237abef5fSAndreas Gohr $this->assertNotSame('foobar', $INPUT->str('zstring')); // make sure original input is unmodified 5337abef5fSAndreas Gohr 5437abef5fSAndreas Gohr $this->assertNotSame('foobar', $INPUT->get->str('zstring')); 5537abef5fSAndreas Gohr $this->assertSame('foobar', $INPUT->get->filter()->str('zstring')); 5637abef5fSAndreas Gohr $this->assertSame('bar', $INPUT->get->filter($filter)->str('foo')); 5737abef5fSAndreas Gohr $this->assertSame('bar', $INPUT->get->filter()->str('znull', 'bar', true)); 5837abef5fSAndreas Gohr $this->assertNotSame('foobar', $INPUT->get->str('zstring')); // make sure original input is unmodified 5937abef5fSAndreas Gohr 6037abef5fSAndreas Gohr $this->assertNotSame(4242, $INPUT->int('zint')); 6137abef5fSAndreas Gohr $this->assertSame(4242, $INPUT->filter()->int('zint')); 6237abef5fSAndreas Gohr $this->assertSame(42, $INPUT->filter($filter)->int('zintbaz')); 6337abef5fSAndreas Gohr $this->assertSame(42, $INPUT->filter()->str('znull', 42, true)); 6437abef5fSAndreas Gohr 6537abef5fSAndreas Gohr $this->assertSame(true, $INPUT->bool('znull')); 6637abef5fSAndreas Gohr $this->assertSame(false, $INPUT->filter()->bool('znull')); 6737abef5fSAndreas Gohr 6837abef5fSAndreas Gohr $this->assertSame('foobar', $INPUT->filter()->valid('zstring', array('foobar', 'bang'))); 6937abef5fSAndreas Gohr } 7037abef5fSAndreas Gohr 71591acd87SAndreas Gohr public function test_str() { 72591acd87SAndreas Gohr $_REQUEST = $this->data; 73591acd87SAndreas Gohr $_POST = $this->data; 74591acd87SAndreas Gohr $_GET = $this->data; 75591acd87SAndreas Gohr $_GET['get'] = 1; 76591acd87SAndreas Gohr $_POST['post'] = 1; 77591acd87SAndreas Gohr $INPUT = new Input(); 78591acd87SAndreas Gohr 79591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('string')); 80591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('none')); 81591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('empty')); 82591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('none', 'foo')); 83591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('empty', 'foo')); 84591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('empty', 'foo', true)); 85591acd87SAndreas Gohr 86591acd87SAndreas Gohr $this->assertSame(false, $INPUT->str('get', false)); 87591acd87SAndreas Gohr $this->assertSame(false, $INPUT->str('post', false)); 88591acd87SAndreas Gohr 89591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('string')); 90591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('none')); 91591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('empty')); 92591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('none', 'foo')); 93591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('empty', 'foo')); 94591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('empty', 'foo', true)); 95591acd87SAndreas Gohr 96591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->str('get', false)); 97591acd87SAndreas Gohr $this->assertSame('1', $INPUT->post->str('post', false)); 98591acd87SAndreas Gohr 99591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('string')); 100591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('none')); 101591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('empty')); 102591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('none', 'foo')); 103591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('empty', 'foo')); 104591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('empty', 'foo', true)); 105591acd87SAndreas Gohr 106591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->str('post', false)); 107591acd87SAndreas Gohr $this->assertSame('1', $INPUT->get->str('get', false)); 108591acd87SAndreas Gohr 109591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('array')); 110591acd87SAndreas Gohr } 111591acd87SAndreas Gohr 112591acd87SAndreas Gohr public function test_int() { 113591acd87SAndreas Gohr $_REQUEST = $this->data; 114591acd87SAndreas Gohr $_POST = $this->data; 115591acd87SAndreas Gohr $_GET = $this->data; 116591acd87SAndreas Gohr $_GET['get'] = 1; 117591acd87SAndreas Gohr $_POST['post'] = 1; 118591acd87SAndreas Gohr $INPUT = new Input(); 119591acd87SAndreas Gohr 120591acd87SAndreas Gohr $this->assertSame(17, $INPUT->int('int')); 121591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('none')); 122591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('empty')); 123591acd87SAndreas Gohr $this->assertSame(42, $INPUT->int('none', 42)); 124591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('zero', 42)); 125591acd87SAndreas Gohr $this->assertSame(42, $INPUT->int('zero', 42, true)); 126591acd87SAndreas Gohr 127591acd87SAndreas Gohr $this->assertSame(false, $INPUT->int('get', false)); 128591acd87SAndreas Gohr $this->assertSame(false, $INPUT->int('post', false)); 129591acd87SAndreas Gohr 130591acd87SAndreas Gohr $this->assertSame(17, $INPUT->post->int('int')); 131591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('none')); 132591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('empty')); 133591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('none', 42)); 134591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('zero', 42)); 135591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('zero', 42, true)); 136591acd87SAndreas Gohr 137591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->int('get', false)); 138591acd87SAndreas Gohr $this->assertSame(1, $INPUT->post->int('post', false)); 139591acd87SAndreas Gohr 140591acd87SAndreas Gohr $this->assertSame(17, $INPUT->post->int('int')); 141591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('none')); 142591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('empty')); 143591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('none', 42)); 144591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('zero', 42)); 145591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('zero', 42, true)); 146591acd87SAndreas Gohr 147591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->int('post', false)); 148591acd87SAndreas Gohr $this->assertSame(1, $INPUT->get->int('get', false)); 149591acd87SAndreas Gohr 150591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('array')); 1515d0aaf95SAndreas Gohr 1525d0aaf95SAndreas Gohr $this->assertSame(0, $INPUT->int('zero', -1)); 1535d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->int('empty', -1)); 1545d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->int('zero', -1, true)); 1555d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->int('empty', -1, true)); 156591acd87SAndreas Gohr } 157591acd87SAndreas Gohr 158591acd87SAndreas Gohr public function test_arr() { 159591acd87SAndreas Gohr $_REQUEST = $this->data; 160591acd87SAndreas Gohr $_POST = $this->data; 161591acd87SAndreas Gohr $_GET = $this->data; 162591acd87SAndreas Gohr $_GET['get'] = array(1, 2); 163591acd87SAndreas Gohr $_POST['post'] = array(1, 2); 164591acd87SAndreas Gohr $INPUT = new Input(); 165591acd87SAndreas Gohr 166591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->arr('array')); 167591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('none')); 168591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('empty')); 169591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->arr('none', array(1, 2))); 170591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('emptya', array(1, 2))); 171591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->arr('emptya', array(1, 2), true)); 172591acd87SAndreas Gohr 173591acd87SAndreas Gohr $this->assertSame(false, $INPUT->arr('get', false)); 174591acd87SAndreas Gohr $this->assertSame(false, $INPUT->arr('post', false)); 175591acd87SAndreas Gohr 176591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->post->arr('array')); 177591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('none')); 178591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('empty')); 179591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('none', array(1, 2))); 180591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('emptya', array(1, 2))); 181591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('emptya', array(1, 2), true)); 182591acd87SAndreas Gohr 183591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->arr('get', false)); 184591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('post', false)); 185591acd87SAndreas Gohr 186591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->get->arr('array')); 187591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('none')); 188591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('empty')); 189591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('none', array(1, 2))); 190591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('emptya', array(1, 2))); 191591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('emptya', array(1, 2), true)); 192591acd87SAndreas Gohr 193591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('get', false)); 194591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->arr('post', false)); 195591acd87SAndreas Gohr } 196591acd87SAndreas Gohr 197591acd87SAndreas Gohr public function test_bool() { 198591acd87SAndreas Gohr $_REQUEST = $this->data; 199591acd87SAndreas Gohr $_POST = $this->data; 200591acd87SAndreas Gohr $_GET = $this->data; 201591acd87SAndreas Gohr $_GET['get'] = '1'; 202591acd87SAndreas Gohr $_POST['post'] = '1'; 203591acd87SAndreas Gohr $INPUT = new Input(); 204591acd87SAndreas Gohr 205591acd87SAndreas Gohr $this->assertSame(true, $INPUT->bool('one')); 206591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('zero')); 207591acd87SAndreas Gohr 208591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('get')); 209591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('post')); 210591acd87SAndreas Gohr 211591acd87SAndreas Gohr $this->assertSame(true, $INPUT->post->bool('one')); 212591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->bool('zero')); 213591acd87SAndreas Gohr 214591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->bool('get')); 215591acd87SAndreas Gohr $this->assertSame(true, $INPUT->post->bool('post')); 2165d0aaf95SAndreas Gohr 2175d0aaf95SAndreas Gohr $this->assertSame(false, $INPUT->bool('zero', -1)); 2185d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->bool('empty', -1)); 2195d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->bool('zero', -1, true)); 2205d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->bool('empty', -1, true)); 221591acd87SAndreas Gohr } 222591acd87SAndreas Gohr 223591acd87SAndreas Gohr public function test_remove() { 224591acd87SAndreas Gohr $_REQUEST = $this->data; 225591acd87SAndreas Gohr $_POST = $this->data; 226591acd87SAndreas Gohr $_GET = $this->data; 227591acd87SAndreas Gohr $INPUT = new Input(); 228591acd87SAndreas Gohr 229591acd87SAndreas Gohr $INPUT->remove('string'); 230*056bf31fSDamien Regad $this->assertArrayNotHasKey('string', $_REQUEST); 231*056bf31fSDamien Regad $this->assertArrayNotHasKey('string', $_POST); 232*056bf31fSDamien Regad $this->assertArrayNotHasKey('string', $_GET); 233591acd87SAndreas Gohr 234591acd87SAndreas Gohr $INPUT->post->remove('int'); 235*056bf31fSDamien Regad $this->assertArrayNotHasKey('int', $_POST); 236591acd87SAndreas Gohr $this->assertEquals(17, $_GET['int']); 237591acd87SAndreas Gohr $this->assertEquals(17, $_REQUEST['int']); 238591acd87SAndreas Gohr } 239591acd87SAndreas Gohr 240591acd87SAndreas Gohr public function test_set(){ 241591acd87SAndreas Gohr $_REQUEST = $this->data; 242591acd87SAndreas Gohr $_POST = $this->data; 243591acd87SAndreas Gohr $_GET = $this->data; 244591acd87SAndreas Gohr $INPUT = new Input(); 245591acd87SAndreas Gohr 246591acd87SAndreas Gohr $INPUT->set('test','foo'); 247591acd87SAndreas Gohr $this->assertEquals('foo',$_REQUEST['test']); 248*056bf31fSDamien Regad $this->assertArrayNotHasKey('test', $_POST); 249*056bf31fSDamien Regad $this->assertArrayNotHasKey('test', $_GET); 250591acd87SAndreas Gohr 251591acd87SAndreas Gohr $INPUT->get->set('test2','foo'); 252591acd87SAndreas Gohr $this->assertEquals('foo',$_GET['test2']); 253591acd87SAndreas Gohr $this->assertEquals('foo',$_REQUEST['test2']); 254*056bf31fSDamien Regad $this->assertArrayNotHasKey('test', $_POST); 255591acd87SAndreas Gohr } 256591acd87SAndreas Gohr 257591acd87SAndreas Gohr public function test_ref(){ 258591acd87SAndreas Gohr $_REQUEST = $this->data; 259591acd87SAndreas Gohr $_POST = $this->data; 260591acd87SAndreas Gohr $_GET = $this->data; 261591acd87SAndreas Gohr $INPUT = new Input(); 262591acd87SAndreas Gohr 263591acd87SAndreas Gohr $test = &$INPUT->ref('string'); 264591acd87SAndreas Gohr $this->assertEquals('foo',$test); 265591acd87SAndreas Gohr $_REQUEST['string'] = 'bla'; 266591acd87SAndreas Gohr $this->assertEquals('bla',$test); 267591acd87SAndreas Gohr } 268591acd87SAndreas Gohr 2696920d2fdSAndreas Gohr public function test_valid(){ 2706920d2fdSAndreas Gohr $_REQUEST = $this->data; 2716920d2fdSAndreas Gohr $_POST = $this->data; 2726920d2fdSAndreas Gohr $_GET = $this->data; 2736920d2fdSAndreas Gohr $INPUT = new Input(); 2746920d2fdSAndreas Gohr 2756920d2fdSAndreas Gohr $valids = array(17, 'foo'); 2766920d2fdSAndreas Gohr $this->assertSame(null, $INPUT->valid('nope', $valids)); 2776920d2fdSAndreas Gohr $this->assertSame('bang', $INPUT->valid('nope', $valids, 'bang')); 2786920d2fdSAndreas Gohr $this->assertSame(17, $INPUT->valid('int', $valids)); 2796920d2fdSAndreas Gohr $this->assertSame('foo', $INPUT->valid('string', $valids)); 28061ee3dfcSAndreas Gohr $this->assertSame(null, $INPUT->valid('array', $valids)); 2816920d2fdSAndreas Gohr 2826920d2fdSAndreas Gohr $valids = array(true); 2836920d2fdSAndreas Gohr $this->assertSame(true, $INPUT->valid('string', $valids)); 2846920d2fdSAndreas Gohr $this->assertSame(true, $INPUT->valid('one', $valids)); 2856920d2fdSAndreas Gohr $this->assertSame(null, $INPUT->valid('zero', $valids)); 2866920d2fdSAndreas Gohr } 2876920d2fdSAndreas Gohr 288d9e9c1bbSAndreas Gohr public function test_extract(){ 289d9e9c1bbSAndreas Gohr $_REQUEST = $this->data; 290d9e9c1bbSAndreas Gohr $_POST = $this->data; 291d9e9c1bbSAndreas Gohr $_GET = $this->data; 292d9e9c1bbSAndreas Gohr $INPUT = new Input(); 293d9e9c1bbSAndreas Gohr 294d9e9c1bbSAndreas Gohr $this->assertEquals('save', $INPUT->extract('do')->str('do')); 295d9e9c1bbSAndreas Gohr $this->assertEquals('', $INPUT->extract('emptya')->str('emptya')); 296d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->extract('string')->str('string')); 297d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->extract('array')->str('array')); 298d9e9c1bbSAndreas Gohr 299d9e9c1bbSAndreas Gohr $this->assertEquals('save', $INPUT->post->extract('do')->str('do')); 300d9e9c1bbSAndreas Gohr $this->assertEquals('', $INPUT->post->extract('emptya')->str('emptya')); 301d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->post->extract('string')->str('string')); 302d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->post->extract('array')->str('array')); 303d9e9c1bbSAndreas Gohr } 304591acd87SAndreas Gohr} 305