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' => '', 15*d9e9c1bbSAndreas Gohr 'emptya' => array(), 16*d9e9c1bbSAndreas Gohr 'do' => array('save' => 'Speichern'), 17591acd87SAndreas Gohr ); 18591acd87SAndreas Gohr 19591acd87SAndreas Gohr public function test_str() { 20591acd87SAndreas Gohr $_REQUEST = $this->data; 21591acd87SAndreas Gohr $_POST = $this->data; 22591acd87SAndreas Gohr $_GET = $this->data; 23591acd87SAndreas Gohr $_GET['get'] = 1; 24591acd87SAndreas Gohr $_POST['post'] = 1; 25591acd87SAndreas Gohr $INPUT = new Input(); 26591acd87SAndreas Gohr 27591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('string')); 28591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('none')); 29591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('empty')); 30591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('none', 'foo')); 31591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('empty', 'foo')); 32591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('empty', 'foo', true)); 33591acd87SAndreas Gohr 34591acd87SAndreas Gohr $this->assertSame(false, $INPUT->str('get', false)); 35591acd87SAndreas Gohr $this->assertSame(false, $INPUT->str('post', false)); 36591acd87SAndreas Gohr 37591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('string')); 38591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('none')); 39591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('empty')); 40591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('none', 'foo')); 41591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('empty', 'foo')); 42591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('empty', 'foo', true)); 43591acd87SAndreas Gohr 44591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->str('get', false)); 45591acd87SAndreas Gohr $this->assertSame('1', $INPUT->post->str('post', false)); 46591acd87SAndreas Gohr 47591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('string')); 48591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('none')); 49591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('empty')); 50591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('none', 'foo')); 51591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('empty', 'foo')); 52591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('empty', 'foo', true)); 53591acd87SAndreas Gohr 54591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->str('post', false)); 55591acd87SAndreas Gohr $this->assertSame('1', $INPUT->get->str('get', false)); 56591acd87SAndreas Gohr 57591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('array')); 58591acd87SAndreas Gohr } 59591acd87SAndreas Gohr 60591acd87SAndreas Gohr public function test_int() { 61591acd87SAndreas Gohr $_REQUEST = $this->data; 62591acd87SAndreas Gohr $_POST = $this->data; 63591acd87SAndreas Gohr $_GET = $this->data; 64591acd87SAndreas Gohr $_GET['get'] = 1; 65591acd87SAndreas Gohr $_POST['post'] = 1; 66591acd87SAndreas Gohr $INPUT = new Input(); 67591acd87SAndreas Gohr 68591acd87SAndreas Gohr $this->assertSame(17, $INPUT->int('int')); 69591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('none')); 70591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('empty')); 71591acd87SAndreas Gohr $this->assertSame(42, $INPUT->int('none', 42)); 72591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('zero', 42)); 73591acd87SAndreas Gohr $this->assertSame(42, $INPUT->int('zero', 42, true)); 74591acd87SAndreas Gohr 75591acd87SAndreas Gohr $this->assertSame(false, $INPUT->int('get', false)); 76591acd87SAndreas Gohr $this->assertSame(false, $INPUT->int('post', false)); 77591acd87SAndreas Gohr 78591acd87SAndreas Gohr $this->assertSame(17, $INPUT->post->int('int')); 79591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('none')); 80591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('empty')); 81591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('none', 42)); 82591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('zero', 42)); 83591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('zero', 42, true)); 84591acd87SAndreas Gohr 85591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->int('get', false)); 86591acd87SAndreas Gohr $this->assertSame(1, $INPUT->post->int('post', false)); 87591acd87SAndreas Gohr 88591acd87SAndreas Gohr $this->assertSame(17, $INPUT->post->int('int')); 89591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('none')); 90591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('empty')); 91591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('none', 42)); 92591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('zero', 42)); 93591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('zero', 42, true)); 94591acd87SAndreas Gohr 95591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->int('post', false)); 96591acd87SAndreas Gohr $this->assertSame(1, $INPUT->get->int('get', false)); 97591acd87SAndreas Gohr 98591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('array')); 995d0aaf95SAndreas Gohr 1005d0aaf95SAndreas Gohr $this->assertSame(0, $INPUT->int('zero', -1)); 1015d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->int('empty', -1)); 1025d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->int('zero', -1, true)); 1035d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->int('empty', -1, true)); 104591acd87SAndreas Gohr } 105591acd87SAndreas Gohr 106591acd87SAndreas Gohr public function test_arr() { 107591acd87SAndreas Gohr $_REQUEST = $this->data; 108591acd87SAndreas Gohr $_POST = $this->data; 109591acd87SAndreas Gohr $_GET = $this->data; 110591acd87SAndreas Gohr $_GET['get'] = array(1, 2); 111591acd87SAndreas Gohr $_POST['post'] = array(1, 2); 112591acd87SAndreas Gohr $INPUT = new Input(); 113591acd87SAndreas Gohr 114591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->arr('array')); 115591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('none')); 116591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('empty')); 117591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->arr('none', array(1, 2))); 118591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('emptya', array(1, 2))); 119591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->arr('emptya', array(1, 2), true)); 120591acd87SAndreas Gohr 121591acd87SAndreas Gohr $this->assertSame(false, $INPUT->arr('get', false)); 122591acd87SAndreas Gohr $this->assertSame(false, $INPUT->arr('post', false)); 123591acd87SAndreas Gohr 124591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->post->arr('array')); 125591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('none')); 126591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('empty')); 127591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('none', array(1, 2))); 128591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('emptya', array(1, 2))); 129591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('emptya', array(1, 2), true)); 130591acd87SAndreas Gohr 131591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->arr('get', false)); 132591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('post', false)); 133591acd87SAndreas Gohr 134591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->get->arr('array')); 135591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('none')); 136591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('empty')); 137591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('none', array(1, 2))); 138591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('emptya', array(1, 2))); 139591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('emptya', array(1, 2), true)); 140591acd87SAndreas Gohr 141591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('get', false)); 142591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->arr('post', false)); 143591acd87SAndreas Gohr } 144591acd87SAndreas Gohr 145591acd87SAndreas Gohr public function test_bool() { 146591acd87SAndreas Gohr $_REQUEST = $this->data; 147591acd87SAndreas Gohr $_POST = $this->data; 148591acd87SAndreas Gohr $_GET = $this->data; 149591acd87SAndreas Gohr $_GET['get'] = '1'; 150591acd87SAndreas Gohr $_POST['post'] = '1'; 151591acd87SAndreas Gohr $INPUT = new Input(); 152591acd87SAndreas Gohr 153591acd87SAndreas Gohr $this->assertSame(true, $INPUT->bool('one')); 154591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('zero')); 155591acd87SAndreas Gohr 156591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('get')); 157591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('post')); 158591acd87SAndreas Gohr 159591acd87SAndreas Gohr $this->assertSame(true, $INPUT->post->bool('one')); 160591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->bool('zero')); 161591acd87SAndreas Gohr 162591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->bool('get')); 163591acd87SAndreas Gohr $this->assertSame(true, $INPUT->post->bool('post')); 1645d0aaf95SAndreas Gohr 1655d0aaf95SAndreas Gohr $this->assertSame(false, $INPUT->bool('zero', -1)); 1665d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->bool('empty', -1)); 1675d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->bool('zero', -1, true)); 1685d0aaf95SAndreas Gohr $this->assertSame(-1, $INPUT->bool('empty', -1, true)); 169591acd87SAndreas Gohr } 170591acd87SAndreas Gohr 171591acd87SAndreas Gohr public function test_remove() { 172591acd87SAndreas Gohr $_REQUEST = $this->data; 173591acd87SAndreas Gohr $_POST = $this->data; 174591acd87SAndreas Gohr $_GET = $this->data; 175591acd87SAndreas Gohr $INPUT = new Input(); 176591acd87SAndreas Gohr 177591acd87SAndreas Gohr $INPUT->remove('string'); 178591acd87SAndreas Gohr $this->assertNull($_REQUEST['string']); 179591acd87SAndreas Gohr $this->assertNull($_POST['string']); 180591acd87SAndreas Gohr $this->assertNull($_GET['string']); 181591acd87SAndreas Gohr 182591acd87SAndreas Gohr $INPUT->post->remove('int'); 183591acd87SAndreas Gohr $this->assertNull($_POST['int']); 184591acd87SAndreas Gohr $this->assertEquals(17, $_GET['int']); 185591acd87SAndreas Gohr $this->assertEquals(17, $_REQUEST['int']); 186591acd87SAndreas Gohr } 187591acd87SAndreas Gohr 188591acd87SAndreas Gohr public function test_set(){ 189591acd87SAndreas Gohr $_REQUEST = $this->data; 190591acd87SAndreas Gohr $_POST = $this->data; 191591acd87SAndreas Gohr $_GET = $this->data; 192591acd87SAndreas Gohr $INPUT = new Input(); 193591acd87SAndreas Gohr 194591acd87SAndreas Gohr $INPUT->set('test','foo'); 195591acd87SAndreas Gohr $this->assertEquals('foo',$_REQUEST['test']); 196591acd87SAndreas Gohr $this->assertNull($_POST['test']); 197591acd87SAndreas Gohr $this->assertNull($_GET['test']); 198591acd87SAndreas Gohr 199591acd87SAndreas Gohr $INPUT->get->set('test2','foo'); 200591acd87SAndreas Gohr $this->assertEquals('foo',$_GET['test2']); 201591acd87SAndreas Gohr $this->assertEquals('foo',$_REQUEST['test2']); 202591acd87SAndreas Gohr $this->assertNull($_POST['test']); 203591acd87SAndreas Gohr } 204591acd87SAndreas Gohr 205591acd87SAndreas Gohr public function test_ref(){ 206591acd87SAndreas Gohr $_REQUEST = $this->data; 207591acd87SAndreas Gohr $_POST = $this->data; 208591acd87SAndreas Gohr $_GET = $this->data; 209591acd87SAndreas Gohr $INPUT = new Input(); 210591acd87SAndreas Gohr 211591acd87SAndreas Gohr $test = &$INPUT->ref('string'); 212591acd87SAndreas Gohr $this->assertEquals('foo',$test); 213591acd87SAndreas Gohr $_REQUEST['string'] = 'bla'; 214591acd87SAndreas Gohr $this->assertEquals('bla',$test); 215591acd87SAndreas Gohr } 216591acd87SAndreas Gohr 217*d9e9c1bbSAndreas Gohr public function test_extract(){ 218*d9e9c1bbSAndreas Gohr $_REQUEST = $this->data; 219*d9e9c1bbSAndreas Gohr $_POST = $this->data; 220*d9e9c1bbSAndreas Gohr $_GET = $this->data; 221*d9e9c1bbSAndreas Gohr $INPUT = new Input(); 222*d9e9c1bbSAndreas Gohr 223*d9e9c1bbSAndreas Gohr $this->assertEquals('save', $INPUT->extract('do')->str('do')); 224*d9e9c1bbSAndreas Gohr $this->assertEquals('', $INPUT->extract('emptya')->str('emptya')); 225*d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->extract('string')->str('string')); 226*d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->extract('array')->str('array')); 227*d9e9c1bbSAndreas Gohr 228*d9e9c1bbSAndreas Gohr $this->assertEquals('save', $INPUT->post->extract('do')->str('do')); 229*d9e9c1bbSAndreas Gohr $this->assertEquals('', $INPUT->post->extract('emptya')->str('emptya')); 230*d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->post->extract('string')->str('string')); 231*d9e9c1bbSAndreas Gohr $this->assertEquals('foo', $INPUT->post->extract('array')->str('array')); 232*d9e9c1bbSAndreas Gohr } 233591acd87SAndreas Gohr} 234