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