summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/core/Input_test.php
blob: a066d9960ac3f451ff5f3eb376a6fa95463ac4cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

class Input_test extends CI_TestCase {
	
	public function set_up()
	{
		// Set server variable to GET as default, since this will leave unset in STDIN env
		$_SERVER['REQUEST_METHOD'] = 'GET';

		// Set config for Input class
		$this->ci_set_config('allow_get_array',	TRUE);
		$this->ci_set_config('global_xss_filtering', FALSE);
		$this->ci_set_config('csrf_protection', FALSE);

		$security = new Mock_Core_Security();
		$utf8 = new Mock_Core_Utf8();

		$this->input = new Mock_Core_Input($security, $utf8);
	}
	
	// --------------------------------------------------------------------
	
	public function test_get_not_exists()
	{
		$this->assertEmpty($this->input->get());
		$this->assertEmpty($this->input->get('foo'));

		$this->assertTrue( ! $this->input->get());
		$this->assertTrue( ! $this->input->get('foo'));

		// Test we're getting empty results
		$this->assertTrue($this->input->get() == NULL);
		$this->assertTrue($this->input->get('foo') == NULL);

		// Test new 3.0 behaviour for non existant results (used to be FALSE)
		$this->assertTrue($this->input->get() === NULL);
		$this->assertTrue($this->input->get('foo') === NULL);
	}

	// --------------------------------------------------------------------
	
	public function test_get_exist()
	{
		$_SERVER['REQUEST_METHOD'] = 'GET';
		$_GET['foo'] = 'bar';

		$this->assertArrayHasKey('foo', $this->input->get());
		$this->assertEquals('bar', $this->input->get('foo'));
	}

	// --------------------------------------------------------------------
	
	public function test_get_exist_with_xss_clean()
	{
		$_SERVER['REQUEST_METHOD'] = 'GET';
		$_GET['harm'] = "Hello, i try to <script>alert('Hack');</script> your site";

		$this->assertArrayHasKey('harm', $this->input->get());
		$this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $this->input->get('harm'));
		$this->assertEquals("Hello, i try to [removed]alert&#40;'Hack'&#41;;[removed] your site", $this->input->get('harm', TRUE));
	}

	// --------------------------------------------------------------------
	
	public function test_post_not_exists()
	{
		$this->assertEmpty($this->input->post());
		$this->assertEmpty($this->input->post('foo'));

		$this->assertTrue( ! $this->input->post());
		$this->assertTrue( ! $this->input->post('foo'));

		$this->assertTrue($this->input->post() == NULL);
		$this->assertTrue($this->input->post('foo') == NULL);

		$this->assertTrue($this->input->post() === NULL);
		$this->assertTrue($this->input->post('foo') === NULL);
	}

	// --------------------------------------------------------------------
	
	public function test_post_exist()
	{
		$_SERVER['REQUEST_METHOD'] = 'POST';
		$_POST['foo'] = 'bar';

		$this->assertArrayHasKey('foo', $this->input->post());
		$this->assertEquals('bar', $this->input->post('foo'));
	}

	// --------------------------------------------------------------------
	
	public function test_post_exist_with_xss_clean()
	{
		$_SERVER['REQUEST_METHOD'] = 'POST';
		$_POST['harm'] = "Hello, i try to <script>alert('Hack');</script> your site";

		$this->assertArrayHasKey('harm', $this->input->post());
		$this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $this->input->post('harm'));
		$this->assertEquals("Hello, i try to [removed]alert&#40;'Hack'&#41;;[removed] your site", $this->input->post('harm', TRUE));
	}

	// --------------------------------------------------------------------
	
	public function test_get_post()
	{
		$_SERVER['REQUEST_METHOD'] = 'POST';
		$_POST['foo'] = 'bar';

		$this->assertEquals('bar', $this->input->get_post('foo'));
	}

	// --------------------------------------------------------------------
	
	public function test_cookie()
	{
		$_COOKIE['foo'] = 'bar';

		$this->assertEquals('bar', $this->input->cookie('foo'));
	}

	// --------------------------------------------------------------------
	
	public function test_server()
	{
		$this->assertEquals('GET', $this->input->server('REQUEST_METHOD'));
	}

	// --------------------------------------------------------------------
	
	public function test_fetch_from_array()
	{
		$data = array(
			'foo' => 'bar',
			'harm' => 'Hello, i try to <script>alert(\'Hack\');</script> your site',
		);

		$foo = $this->input->fetch_from_array($data, 'foo');
		$harm = $this->input->fetch_from_array($data, 'harm');
		$harmless = $this->input->fetch_from_array($data, 'harm', TRUE);

		$this->assertEquals('bar', $foo);
		$this->assertEquals("Hello, i try to <script>alert('Hack');</script> your site", $harm);
		$this->assertEquals("Hello, i try to [removed]alert&#40;'Hack'&#41;;[removed] your site", $harmless);
	}
}