1*591acd87SAndreas Gohr<?php 2*591acd87SAndreas Gohr 3*591acd87SAndreas Gohr/** 4*591acd87SAndreas Gohr * Tests for the Input class 5*591acd87SAndreas Gohr */ 6*591acd87SAndreas Gohrclass input_test extends DokuWikiTest { 7*591acd87SAndreas Gohr 8*591acd87SAndreas Gohr private $data = array( 9*591acd87SAndreas Gohr 'array' => array('foo', 'bar'), 10*591acd87SAndreas Gohr 'string' => 'foo', 11*591acd87SAndreas Gohr 'int' => '17', 12*591acd87SAndreas Gohr 'zero' => '0', 13*591acd87SAndreas Gohr 'one' => '1', 14*591acd87SAndreas Gohr 'empty' => '', 15*591acd87SAndreas Gohr 'emptya' => array() 16*591acd87SAndreas Gohr ); 17*591acd87SAndreas Gohr 18*591acd87SAndreas Gohr public function test_str() { 19*591acd87SAndreas Gohr $_REQUEST = $this->data; 20*591acd87SAndreas Gohr $_POST = $this->data; 21*591acd87SAndreas Gohr $_GET = $this->data; 22*591acd87SAndreas Gohr $_GET['get'] = 1; 23*591acd87SAndreas Gohr $_POST['post'] = 1; 24*591acd87SAndreas Gohr $INPUT = new Input(); 25*591acd87SAndreas Gohr 26*591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('string')); 27*591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('none')); 28*591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('empty')); 29*591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('none', 'foo')); 30*591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('empty', 'foo')); 31*591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->str('empty', 'foo', true)); 32*591acd87SAndreas Gohr 33*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->str('get', false)); 34*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->str('post', false)); 35*591acd87SAndreas Gohr 36*591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('string')); 37*591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('none')); 38*591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('empty')); 39*591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('none', 'foo')); 40*591acd87SAndreas Gohr $this->assertSame('', $INPUT->post->str('empty', 'foo')); 41*591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->post->str('empty', 'foo', true)); 42*591acd87SAndreas Gohr 43*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->str('get', false)); 44*591acd87SAndreas Gohr $this->assertSame('1', $INPUT->post->str('post', false)); 45*591acd87SAndreas Gohr 46*591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('string')); 47*591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('none')); 48*591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('empty')); 49*591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('none', 'foo')); 50*591acd87SAndreas Gohr $this->assertSame('', $INPUT->get->str('empty', 'foo')); 51*591acd87SAndreas Gohr $this->assertSame('foo', $INPUT->get->str('empty', 'foo', true)); 52*591acd87SAndreas Gohr 53*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->str('post', false)); 54*591acd87SAndreas Gohr $this->assertSame('1', $INPUT->get->str('get', false)); 55*591acd87SAndreas Gohr 56*591acd87SAndreas Gohr $this->assertSame('', $INPUT->str('array')); 57*591acd87SAndreas Gohr } 58*591acd87SAndreas Gohr 59*591acd87SAndreas Gohr public function test_int() { 60*591acd87SAndreas Gohr $_REQUEST = $this->data; 61*591acd87SAndreas Gohr $_POST = $this->data; 62*591acd87SAndreas Gohr $_GET = $this->data; 63*591acd87SAndreas Gohr $_GET['get'] = 1; 64*591acd87SAndreas Gohr $_POST['post'] = 1; 65*591acd87SAndreas Gohr $INPUT = new Input(); 66*591acd87SAndreas Gohr 67*591acd87SAndreas Gohr $this->assertSame(17, $INPUT->int('int')); 68*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('none')); 69*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('empty')); 70*591acd87SAndreas Gohr $this->assertSame(42, $INPUT->int('none', 42)); 71*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('zero', 42)); 72*591acd87SAndreas Gohr $this->assertSame(42, $INPUT->int('zero', 42, true)); 73*591acd87SAndreas Gohr 74*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->int('get', false)); 75*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->int('post', false)); 76*591acd87SAndreas Gohr 77*591acd87SAndreas Gohr $this->assertSame(17, $INPUT->post->int('int')); 78*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('none')); 79*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('empty')); 80*591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('none', 42)); 81*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('zero', 42)); 82*591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('zero', 42, true)); 83*591acd87SAndreas Gohr 84*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->int('get', false)); 85*591acd87SAndreas Gohr $this->assertSame(1, $INPUT->post->int('post', false)); 86*591acd87SAndreas Gohr 87*591acd87SAndreas Gohr $this->assertSame(17, $INPUT->post->int('int')); 88*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('none')); 89*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('empty')); 90*591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('none', 42)); 91*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->post->int('zero', 42)); 92*591acd87SAndreas Gohr $this->assertSame(42, $INPUT->post->int('zero', 42, true)); 93*591acd87SAndreas Gohr 94*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->int('post', false)); 95*591acd87SAndreas Gohr $this->assertSame(1, $INPUT->get->int('get', false)); 96*591acd87SAndreas Gohr 97*591acd87SAndreas Gohr $this->assertSame(0, $INPUT->int('array')); 98*591acd87SAndreas Gohr } 99*591acd87SAndreas Gohr 100*591acd87SAndreas Gohr public function test_arr() { 101*591acd87SAndreas Gohr $_REQUEST = $this->data; 102*591acd87SAndreas Gohr $_POST = $this->data; 103*591acd87SAndreas Gohr $_GET = $this->data; 104*591acd87SAndreas Gohr $_GET['get'] = array(1, 2); 105*591acd87SAndreas Gohr $_POST['post'] = array(1, 2); 106*591acd87SAndreas Gohr $INPUT = new Input(); 107*591acd87SAndreas Gohr 108*591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->arr('array')); 109*591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('none')); 110*591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('empty')); 111*591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->arr('none', array(1, 2))); 112*591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->arr('emptya', array(1, 2))); 113*591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->arr('emptya', array(1, 2), true)); 114*591acd87SAndreas Gohr 115*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->arr('get', false)); 116*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->arr('post', false)); 117*591acd87SAndreas Gohr 118*591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->post->arr('array')); 119*591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('none')); 120*591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('empty')); 121*591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('none', array(1, 2))); 122*591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->post->arr('emptya', array(1, 2))); 123*591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('emptya', array(1, 2), true)); 124*591acd87SAndreas Gohr 125*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->arr('get', false)); 126*591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->post->arr('post', false)); 127*591acd87SAndreas Gohr 128*591acd87SAndreas Gohr $this->assertSame(array('foo', 'bar'), $INPUT->get->arr('array')); 129*591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('none')); 130*591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('empty')); 131*591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('none', array(1, 2))); 132*591acd87SAndreas Gohr $this->assertSame(array(), $INPUT->get->arr('emptya', array(1, 2))); 133*591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('emptya', array(1, 2), true)); 134*591acd87SAndreas Gohr 135*591acd87SAndreas Gohr $this->assertSame(array(1, 2), $INPUT->get->arr('get', false)); 136*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->get->arr('post', false)); 137*591acd87SAndreas Gohr } 138*591acd87SAndreas Gohr 139*591acd87SAndreas Gohr public function test_bool() { 140*591acd87SAndreas Gohr $_REQUEST = $this->data; 141*591acd87SAndreas Gohr $_POST = $this->data; 142*591acd87SAndreas Gohr $_GET = $this->data; 143*591acd87SAndreas Gohr $_GET['get'] = '1'; 144*591acd87SAndreas Gohr $_POST['post'] = '1'; 145*591acd87SAndreas Gohr $INPUT = new Input(); 146*591acd87SAndreas Gohr 147*591acd87SAndreas Gohr $this->assertSame(true, $INPUT->bool('one')); 148*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('zero')); 149*591acd87SAndreas Gohr 150*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('get')); 151*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->bool('post')); 152*591acd87SAndreas Gohr 153*591acd87SAndreas Gohr $this->assertSame(true, $INPUT->post->bool('one')); 154*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->bool('zero')); 155*591acd87SAndreas Gohr 156*591acd87SAndreas Gohr $this->assertSame(false, $INPUT->post->bool('get')); 157*591acd87SAndreas Gohr $this->assertSame(true, $INPUT->post->bool('post')); 158*591acd87SAndreas Gohr } 159*591acd87SAndreas Gohr 160*591acd87SAndreas Gohr public function test_remove() { 161*591acd87SAndreas Gohr $_REQUEST = $this->data; 162*591acd87SAndreas Gohr $_POST = $this->data; 163*591acd87SAndreas Gohr $_GET = $this->data; 164*591acd87SAndreas Gohr $INPUT = new Input(); 165*591acd87SAndreas Gohr 166*591acd87SAndreas Gohr $INPUT->remove('string'); 167*591acd87SAndreas Gohr $this->assertNull($_REQUEST['string']); 168*591acd87SAndreas Gohr $this->assertNull($_POST['string']); 169*591acd87SAndreas Gohr $this->assertNull($_GET['string']); 170*591acd87SAndreas Gohr 171*591acd87SAndreas Gohr $INPUT->post->remove('int'); 172*591acd87SAndreas Gohr $this->assertNull($_POST['int']); 173*591acd87SAndreas Gohr $this->assertEquals(17, $_GET['int']); 174*591acd87SAndreas Gohr $this->assertEquals(17, $_REQUEST['int']); 175*591acd87SAndreas Gohr } 176*591acd87SAndreas Gohr 177*591acd87SAndreas Gohr public function test_set(){ 178*591acd87SAndreas Gohr $_REQUEST = $this->data; 179*591acd87SAndreas Gohr $_POST = $this->data; 180*591acd87SAndreas Gohr $_GET = $this->data; 181*591acd87SAndreas Gohr $INPUT = new Input(); 182*591acd87SAndreas Gohr 183*591acd87SAndreas Gohr $INPUT->set('test','foo'); 184*591acd87SAndreas Gohr $this->assertEquals('foo',$_REQUEST['test']); 185*591acd87SAndreas Gohr $this->assertNull($_POST['test']); 186*591acd87SAndreas Gohr $this->assertNull($_GET['test']); 187*591acd87SAndreas Gohr 188*591acd87SAndreas Gohr $INPUT->get->set('test2','foo'); 189*591acd87SAndreas Gohr $this->assertEquals('foo',$_GET['test2']); 190*591acd87SAndreas Gohr $this->assertEquals('foo',$_REQUEST['test2']); 191*591acd87SAndreas Gohr $this->assertNull($_POST['test']); 192*591acd87SAndreas Gohr } 193*591acd87SAndreas Gohr 194*591acd87SAndreas Gohr public function test_ref(){ 195*591acd87SAndreas Gohr $_REQUEST = $this->data; 196*591acd87SAndreas Gohr $_POST = $this->data; 197*591acd87SAndreas Gohr $_GET = $this->data; 198*591acd87SAndreas Gohr $INPUT = new Input(); 199*591acd87SAndreas Gohr 200*591acd87SAndreas Gohr $test = &$INPUT->ref('string'); 201*591acd87SAndreas Gohr $this->assertEquals('foo',$test); 202*591acd87SAndreas Gohr $_REQUEST['string'] = 'bla'; 203*591acd87SAndreas Gohr $this->assertEquals('bla',$test); 204*591acd87SAndreas Gohr } 205*591acd87SAndreas Gohr 206*591acd87SAndreas Gohr}