Add http\Input class to provide an input sanitization system.
[hoplite.git] / testing / tests / http / input_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2013 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use hoplite\http\Input;
19
20 require_once HOPLITE_ROOT . '/http/input.php';
21
22 /**
23 * @backupGlobals enabled
24 */
25 class InputTest extends \PHPUnit_Framework_TestCase
26 {
27 public function setUp()
28 {
29 $_GET = array(
30 'gint' => '12',
31 'gfloat' => '3.14',
32 'gstr' => '"Hello World"',
33 'ghtml' => '<blink>"Hello World"</blink>',
34 'gbool' => 'True',
35 );
36 $_POST = array(
37 'pstr' => '<script type="text/javascript"> alert("Hello world"); </script>',
38 'pbool' => 'YES',
39 'parray' => array('13', 15, '19', '22.23'),
40 );
41 $_REQUEST = array_merge($_GET, $_POST);
42 }
43
44 public function testDefaultMode()
45 {
46 $input = new Input();
47 $this->assertEquals('"Hello World"', $input->in['gstr']);
48
49 $input = new Input(Input::TYPE_RAW);
50 $this->assertArrayNotHasKey('gstr', $input->in);
51 }
52
53 public function testClean()
54 {
55 $input = new Input();
56 $this->assertSame(12, $input->Clean('gint', Input::TYPE_INT));
57 $this->assertSame(12, $input->in['gint']);
58 }
59
60 public function testCleanArray()
61 {
62 $input = new Input();
63 $input->CleanArray(array(
64 'gfloat' => Input::TYPE_FLOAT,
65 'pbool' => Input::TYPE_BOOL,
66 ));
67 $this->assertSame(3.14, $input->in['gfloat']);
68 $this->assertSame(TRUE, $input->in['pbool']);
69 }
70
71 public function testInputCleanDeep()
72 {
73 $input = new Input();
74 $test = $input->InputCleanDeep('p', 'parray', Input::TYPE_UINT);
75 $this->assertSame(array(13, 15, 19, 22), $test);
76 $this->assertSame($test, $input->in['parray']);
77 }
78 }