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' => '', 15d9e9c1bbSAndreas Gohr 'emptya' => array(), 16d9e9c1bbSAndreas Gohr 'do' => array('save' => 'Speichern'), 17*37abef5fSAndreas Gohr 18591acd87SAndreas Gohr ); 19591acd87SAndreas Gohr 20*37abef5fSAndreas Gohr /** 21*37abef5fSAndreas Gohr * custom filter function 22*37abef5fSAndreas Gohr * 23*37abef5fSAndreas Gohr * @param $string 24*37abef5fSAndreas Gohr * @return mixed 25*37abef5fSAndreas Gohr */ 26*37abef5fSAndreas Gohr public function myfilter($string) { 27*37abef5fSAndreas Gohr $string = str_replace('foo', 'bar', $string); 28*37abef5fSAndreas Gohr $string = str_replace('baz', '', $string); 29*37abef5fSAndreas Gohr return $string; 30*37abef5fSAndreas Gohr } 31*37abef5fSAndreas Gohr 32*37abef5fSAndreas Gohr public function test_filter() { 33*37abef5fSAndreas Gohr $_GET = array( 34*37abef5fSAndreas Gohr 'foo' => 'foo', 35*37abef5fSAndreas Gohr 'zstring'=> "foo\0bar", 36*37abef5fSAndreas Gohr 'znull' => "\0", 37*37abef5fSAndreas Gohr 'zint' => '42'."\0".'42', 38*37abef5fSAndreas Gohr 'zintbaz'=> "baz42", 39*37abef5fSAndreas Gohr ); 40*37abef5fSAndreas Gohr $_POST = $_GET; 41*37abef5fSAndreas Gohr $_REQUEST = $_GET; 42*37abef5fSAndreas Gohr $INPUT = new Input(); 43*37abef5fSAndreas Gohr 44*37abef5fSAndreas Gohr $filter = array($this,'myfilter'); 45*37abef5fSAndreas Gohr 46*37abef5fSAndreas Gohr $this->assertNotSame('foobar', $INPUT->str('zstring')); 47*37abef5fSAndreas Gohr $this->assertSame('foobar', $INPUT->filter()->str('zstring')); 48*37abef5fSAndreas Gohr $this->assertSame('bar', $INPUT->filter($filter)->str('foo')); 49*37abef5fSAndreas Gohr $this->assertSame('bar', $INPUT->filter()->str('znull', 'bar', true)); 50*37abef5fSAndreas Gohr $this->assertNotSame('foobar', $INPUT->str('zstring')); // make sure original input is unmodified 51*37abef5fSAndreas Gohr 52*37abef5fSAndreas Gohr $this->assertNotSame('foobar', $INPUT->get->str('zstring')); 53*37abef5fSAndreas Gohr $this->assertSame('foobar', $INPUT->get->filter()->str('zstring')); 54*37abef5fSAndreas Gohr $this->assertSame('bar', $INPUT->get->filter($filter)->str('foo')); 55*37abef5fSAndreas Gohr $this->assertSame('bar', $INPUT->get->filter()->str('znull', 'bar', true)); 56*37abef5fSAndreas Gohr $this->assertNotSame('foobar', $INPUT->get->str('zstring')); // make sure original input is unmodified 57*37abef5fSAndreas Gohr 58*37abef5fSAndreas Gohr $this->assertNotSame(4242, $INPUT->int('zint')); 59*37abef5fSAndreas Gohr $this->assertSame(4242, $INPUT->filter()->int('zint')); 60*37abef5fSAndreas Gohr $this->assertSame(42, $INPUT->filter($filter)->int('zintbaz')); 61*37abef5fSAndreas Gohr $this->assertSame(42, $INPUT->filter()->str('znull', 42, true)); 62*37abef5fSAndreas Gohr 63*37abef5fSAndreas Gohr $this->assertSame(true, $INPUT->bool('znull')); 64*37abef5fSAndreas Gohr $this->assertSame(false, $INPUT->filter()->bool('znull')); 65*37abef5fSAndreas Gohr 66*37abef5fSAndreas Gohr $this->assertSame('foobar', $INPUT->filter()->valid('zstring', array('foobar', 'bang'))); 67*37abef5fSAndreas Gohr } 68*37abef5fSAndreas Gohr 69591acd87SAndreas Gohr public function test_str() { 70591acd87SAndreas Gohr $_REQUEST = $this->data; 71591acd87SAndreas Gohr $_POST = $this->data; 72591acd87SAndreas Gohr $_GET = $this->data; 73591acd87SAndreas Gohr $_GET['get'] = 1; 74591acd87SAndreas Gohr $_POST['post'] = 1; 75591acd87SAndreas Gohr $INPUT = new Input(); 76591acd87SAndreas Gohr 77591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('string')); 78591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('none')); 79591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('empty')); 80591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('none', 'foo')); 81591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('empty', 'foo')); 82591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('empty', 'foo', true)); 83591acd87SAndreas Gohr 84591acd87SAndreas Gohr $this->assertSame(false, $INPUT->str('get', false)); 85591acd87SAndreas Gohr $this->assertSame(false, $INPUT->str('post', false)); 86591acd87SAndreas Gohr 87591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('string')); 88591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('none')); 89591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('empty')); 90591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('none', 'foo')); 91591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('empty', 'foo')); 92591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('empty', 'foo', true)); 93591acd87SAndreas Gohr 94591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->str('get', false)); 95591acd87SAndreas Gohr $this->assertSame('1', $INPUT->post->str('post', false)); 96591acd87SAndreas Gohr 97591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('string')); 98591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('none')); 99591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('empty')); 100591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('none', 'foo')); 101591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('empty', 'foo')); 102591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('empty', 'foo', true)); 103591acd87SAndreas Gohr 104591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->str('post', false)); 105591acd87SAndreas Gohr $this->assertSame('1', $INPUT->get->str('get', false)); 106591acd87SAndreas Gohr 107591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('array')); 108591acd87SAndreas Gohr } 109591acd87SAndreas Gohr 110591acd87SAndreas Gohr public function test_int() { 111591acd87SAndreas Gohr $_REQUEST = $this->data; 112591acd87SAndreas Gohr $_POST = $this->data; 113591acd87SAndreas Gohr $_GET = $this->data; 114591acd87SAndreas Gohr $_GET['get'] = 1; 115591acd87SAndreas Gohr $_POST['post'] = 1; 116591acd87SAndreas Gohr $INPUT = new Input(); 117591acd87SAndreas Gohr 118591acd87SAndreas Gohr $this->assertSame(17, $INPUT->int('int')); 119591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('none')); 120591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('empty')); 121591acd87SAndreas Gohr $this->assertSame(42, $INPUT->int('none', 42)); 122591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('zero', 42)); 123591acd87SAndreas Gohr $this->assertSame(42, $INPUT->int('zero', 42, true)); 124591acd87SAndreas Gohr 125591acd87SAndreas Gohr $this->assertSame(false, $INPUT->int('get', false)); 126591acd87SAndreas Gohr $this->assertSame(false, $INPUT->int('post', false)); 127591acd87SAndreas Gohr 128591acd87SAndreas Gohr $this->assertSame(17, $INPUT->post->int('int')); 129591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('none')); 130591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('empty')); 131591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('none', 42)); 132591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('zero', 42)); 133591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('zero', 42, true)); 134591acd87SAndreas Gohr 135591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->int('get', false)); 136591acd87SAndreas Gohr $this->assertSame(1, $INPUT->post->int('post', false)); 137591acd87SAndreas Gohr 138591acd87SAndreas Gohr $this->assertSame(17, $INPUT->post->int('int')); 139591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('none')); 140591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('empty')); 141591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('none', 42)); 142591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('zero', 42)); 143591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('zero', 42, true)); 144591acd87SAndreas Gohr 145591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->int('post', false)); 146591acd87SAndreas Gohr $this->assertSame(1, $INPUT->get->int('get', false)); 147591acd87SAndreas Gohr 148591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('array')); 1495d0aaf95SAndreas Gohr 1505d0aaf95SAndreas Gohr $this->assertSame(0, $INPUT->int('zero', -1)); 1515d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->int('empty', -1)); 1525d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->int('zero', -1, true)); 1535d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->int('empty', -1, true)); 154591acd87SAndreas Gohr } 155591acd87SAndreas Gohr 156591acd87SAndreas Gohr public function test_arr() { 157591acd87SAndreas Gohr $_REQUEST = $this->data; 158591acd87SAndreas Gohr $_POST = $this->data; 159591acd87SAndreas Gohr $_GET = $this->data; 160591acd87SAndreas Gohr $_GET['get'] = array(1, 2); 161591acd87SAndreas Gohr $_POST['post'] = array(1, 2); 162591acd87SAndreas Gohr $INPUT = new Input(); 163591acd87SAndreas Gohr 164591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->arr('array')); 165591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('none')); 166591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('empty')); 167591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->arr('none', array(1, 2))); 168591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('emptya', array(1, 2))); 169591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->arr('emptya', array(1, 2), true)); 170591acd87SAndreas Gohr 171591acd87SAndreas Gohr $this->assertSame(false, $INPUT->arr('get', false)); 172591acd87SAndreas Gohr $this->assertSame(false, $INPUT->arr('post', false)); 173591acd87SAndreas Gohr 174591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->post->arr('array')); 175591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('none')); 176591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('empty')); 177591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('none', array(1, 2))); 178591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('emptya', array(1, 2))); 179591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('emptya', array(1, 2), true)); 180591acd87SAndreas Gohr 181591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->arr('get', false)); 182591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('post', false)); 183591acd87SAndreas Gohr 184591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->get->arr('array')); 185591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('none')); 186591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('empty')); 187591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('none', array(1, 2))); 188591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('emptya', array(1, 2))); 189591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('emptya', array(1, 2), true)); 190591acd87SAndreas Gohr 191591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('get', false)); 192591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->arr('post', false)); 193591acd87SAndreas Gohr } 194591acd87SAndreas Gohr 195591acd87SAndreas Gohr public function test_bool() { 196591acd87SAndreas Gohr $_REQUEST = $this->data; 197591acd87SAndreas Gohr $_POST = $this->data; 198591acd87SAndreas Gohr $_GET = $this->data; 199591acd87SAndreas Gohr $_GET['get'] = '1'; 200591acd87SAndreas Gohr $_POST['post'] = '1'; 201591acd87SAndreas Gohr $INPUT = new Input(); 202591acd87SAndreas Gohr 203591acd87SAndreas Gohr $this->assertSame(true, $INPUT->bool('one')); 204591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('zero')); 205591acd87SAndreas Gohr 206591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('get')); 207591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('post')); 208591acd87SAndreas Gohr 209591acd87SAndreas Gohr $this->assertSame(true, $INPUT->post->bool('one')); 210591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->bool('zero')); 211591acd87SAndreas Gohr 212591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->bool('get')); 213591acd87SAndreas Gohr $this->assertSame(true, $INPUT->post->bool('post')); 2145d0aaf95SAndreas Gohr 2155d0aaf95SAndreas Gohr $this->assertSame(false, $INPUT->bool('zero', -1)); 2165d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->bool('empty', -1)); 2175d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->bool('zero', -1, true)); 2185d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->bool('empty', -1, true)); 219591acd87SAndreas Gohr } 220591acd87SAndreas Gohr 221591acd87SAndreas Gohr public function test_remove() { 222591acd87SAndreas Gohr $_REQUEST = $this->data; 223591acd87SAndreas Gohr $_POST = $this->data; 224591acd87SAndreas Gohr $_GET = $this->data; 225591acd87SAndreas Gohr $INPUT = new Input(); 226591acd87SAndreas Gohr 227591acd87SAndreas Gohr $INPUT->remove('string'); 228591acd87SAndreas Gohr $this->assertNull($_REQUEST['string']); 229591acd87SAndreas Gohr $this->assertNull($_POST['string']); 230591acd87SAndreas Gohr $this->assertNull($_GET['string']); 231591acd87SAndreas Gohr 232591acd87SAndreas Gohr $INPUT->post->remove('int'); 233591acd87SAndreas Gohr $this->assertNull($_POST['int']); 234591acd87SAndreas Gohr $this->assertEquals(17, $_GET['int']); 235591acd87SAndreas Gohr $this->assertEquals(17, $_REQUEST['int']); 236591acd87SAndreas Gohr } 237591acd87SAndreas Gohr 238591acd87SAndreas Gohr public function test_set(){ 239591acd87SAndreas Gohr $_REQUEST = $this->data; 240591acd87SAndreas Gohr $_POST = $this->data; 241591acd87SAndreas Gohr $_GET = $this->data; 242591acd87SAndreas Gohr $INPUT = new Input(); 243591acd87SAndreas Gohr 244591acd87SAndreas Gohr $INPUT->set('test','foo'); 245591acd87SAndreas Gohr $this->assertEquals('foo',$_REQUEST['test']); 246591acd87SAndreas Gohr $this->assertNull($_POST['test']); 247591acd87SAndreas Gohr $this->assertNull($_GET['test']); 248591acd87SAndreas Gohr 249591acd87SAndreas Gohr $INPUT->get->set('test2','foo'); 250591acd87SAndreas Gohr $this->assertEquals('foo',$_GET['test2']); 251591acd87SAndreas Gohr $this->assertEquals('foo',$_REQUEST['test2']); 252591acd87SAndreas Gohr $this->assertNull($_POST['test']); 253591acd87SAndreas Gohr } 254591acd87SAndreas Gohr 255591acd87SAndreas Gohr public function test_ref(){ 256591acd87SAndreas Gohr $_REQUEST = $this->data; 257591acd87SAndreas Gohr $_POST = $this->data; 258591acd87SAndreas Gohr $_GET = $this->data; 259591acd87SAndreas Gohr $INPUT = new Input(); 260591acd87SAndreas Gohr 261591acd87SAndreas Gohr $test = &$INPUT->ref('string'); 262591acd87SAndreas Gohr $this->assertEquals('foo',$test); 263591acd87SAndreas Gohr $_REQUEST['string'] = 'bla'; 264591acd87SAndreas Gohr $this->assertEquals('bla',$test); 265591acd87SAndreas Gohr } 266591acd87SAndreas Gohr 2676920d2fdSAndreas Gohr public function test_valid(){ 2686920d2fdSAndreas Gohr $_REQUEST = $this->data; 2696920d2fdSAndreas Gohr $_POST = $this->data; 2706920d2fdSAndreas Gohr $_GET = $this->data; 2716920d2fdSAndreas Gohr $INPUT = new Input(); 2726920d2fdSAndreas Gohr 2736920d2fdSAndreas Gohr $valids = array(17, 'foo'); 2746920d2fdSAndreas Gohr $this->assertSame(null, $INPUT->valid('nope', $valids)); 2756920d2fdSAndreas Gohr $this->assertSame('bang', $INPUT->valid('nope', $valids, 'bang')); 2766920d2fdSAndreas Gohr $this->assertSame(17, $INPUT->valid('int', $valids)); 2776920d2fdSAndreas Gohr $this->assertSame('foo', $INPUT->valid('string', $valids)); 27861ee3dfcSAndreas Gohr $this->assertSame(null, $INPUT->valid('array', $valids)); 2796920d2fdSAndreas Gohr 2806920d2fdSAndreas Gohr $valids = array(true); 2816920d2fdSAndreas Gohr $this->assertSame(true, $INPUT->valid('string', $valids)); 2826920d2fdSAndreas Gohr $this->assertSame(true, $INPUT->valid('one', $valids)); 2836920d2fdSAndreas Gohr $this->assertSame(null, $INPUT->valid('zero', $valids)); 2846920d2fdSAndreas Gohr } 2856920d2fdSAndreas Gohr 286d9e9c1bbSAndreas Gohr public function test_extract(){ 287d9e9c1bbSAndreas Gohr $_REQUEST = $this->data; 288d9e9c1bbSAndreas Gohr $_POST = $this->data; 289d9e9c1bbSAndreas Gohr $_GET = $this->data; 290d9e9c1bbSAndreas Gohr $INPUT = new Input(); 291d9e9c1bbSAndreas Gohr 292d9e9c1bbSAndreas Gohr $this->assertEquals('save', $INPUT->extract('do')->str('do')); 293d9e9c1bbSAndreas Gohr $this->assertEquals('', $INPUT->extract('emptya')->str('emptya')); 294d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->extract('string')->str('string')); 295d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->extract('array')->str('array')); 296d9e9c1bbSAndreas Gohr 297d9e9c1bbSAndreas Gohr $this->assertEquals('save', $INPUT->post->extract('do')->str('do')); 298d9e9c1bbSAndreas Gohr $this->assertEquals('', $INPUT->post->extract('emptya')->str('emptya')); 299d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->post->extract('string')->str('string')); 300d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->post->extract('array')->str('array')); 301d9e9c1bbSAndreas Gohr } 302591acd87SAndreas Gohr} 303