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