summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/core/URI_test.php
blob: 42dff3639ebddfed3c952c23363198e5d526058e (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php

class URI_test extends CI_TestCase {

	public function set_up()
	{
		$this->uri = new Mock_Core_URI();
	}

	// --------------------------------------------------------------------

	/* As of the following commit, _set_uri_string() is a protected method:

		https://github.com/bcit-ci/CodeIgniter/commit/d461934184d95b0cfb2feec93f27b621ef72a5c2

	public function test_set_uri_string()
	{
		// Slashes get killed
		$this->uri->_set_uri_string('/');
		$this->assertEquals('', $this->uri->uri_string);

		$this->uri->_set_uri_string('nice/uri');
		$this->assertEquals('nice/uri', $this->uri->uri_string);
	}
	*/

	// --------------------------------------------------------------------

	/*

		This has been moved to the constructor

	public function test_fetch_uri_string()
	{
		define('SELF', 'index.php');

		// uri_protocol: AUTO
		$this->uri->config->set_item('uri_protocol', 'AUTO');

		// Test a variety of request uris
		$requests = array(
			'/index.php/controller/method' => 'controller/method',
			'/index.php?/controller/method' => 'controller/method',
			'/index.php?/controller/method/?var=foo' => 'controller/method'
		);

		foreach ($requests as $request => $expected)
		{
			$_SERVER['SCRIPT_NAME'] = '/index.php';
			$_SERVER['REQUEST_URI'] = $request;

			$this->uri->_fetch_uri_string();
			$this->assertEquals($expected, $this->uri->uri_string);
		}

		// Test a subfolder
		$_SERVER['SCRIPT_NAME'] = '/subfolder/index.php';
		$_SERVER['REQUEST_URI'] = '/subfolder/index.php/controller/method';

		$this->uri->_fetch_uri_string();
		$this->assertEquals('controller/method', $this->uri->uri_string);

		// death to request uri
		unset($_SERVER['REQUEST_URI']);

		// life to path info
		$_SERVER['PATH_INFO'] = '/controller/method/';

		$this->uri->_fetch_uri_string();
		$this->assertEquals('controller/method', $this->uri->uri_string);

		// death to path info
		// At this point your server must be seriously drunk
		unset($_SERVER['PATH_INFO']);

		$_SERVER['QUERY_STRING'] = '/controller/method/';

		$this->uri->_fetch_uri_string();
		$this->assertEquals('controller/method', $this->uri->uri_string);

		// At this point your server is a labotomy victim
		unset($_SERVER['QUERY_STRING']);

		$_GET['/controller/method/'] = '';

		$this->uri->_fetch_uri_string();
		$this->assertEquals('controller/method', $this->uri->uri_string);

		// Test coverage implies that these will work
		// uri_protocol: REQUEST_URI
		// uri_protocol: CLI
	}
	*/

	// --------------------------------------------------------------------

	/*

		This has been moved into _set_uri_string()

	public function test_explode_segments()
	{
		// Let's test the function's ability to clean up this mess
		$uris = array(
			'test/uri' => array('test', 'uri'),
			'/test2/uri2' => array('test2', 'uri2'),
			'//test3/test3///' => array('test3', 'test3')
		);

		foreach ($uris as $uri => $a)
		{
			$this->uri->segments = array();
			$this->uri->uri_string = $uri;
			$this->uri->_explode_segments();

			$this->assertEquals($a, $this->uri->segments);
		}
	}
	*/
	// --------------------------------------------------------------------

	public function test_filter_uri_passing()
	{
		$this->uri->_set_permitted_uri_chars('a-z 0-9~%.:_\-');

		$str = 'abc01239~%.:_-';
		$this->uri->filter_uri($str);
	}

	// --------------------------------------------------------------------

	public function test_filter_uri_throws_error()
	{
		$this->setExpectedException('RuntimeException');

		$this->uri->config->set_item('enable_query_strings', FALSE);
		$this->uri->_set_permitted_uri_chars('a-z 0-9~%.:_\-');
		$segment = '$this()'; // filter_uri() accepts by reference
		$this->uri->filter_uri($segment);
	}

	// --------------------------------------------------------------------

	public function test_segment()
	{
		$this->uri->segments = array(1 => 'controller');
		$this->assertEquals($this->uri->segment(1), 'controller');
		$this->assertEquals($this->uri->segment(2, 'default'), 'default');
	}

	// --------------------------------------------------------------------

	public function test_rsegment()
	{
		$this->uri->rsegments = array(1 => 'method');
		$this->assertEquals($this->uri->rsegment(1), 'method');
		$this->assertEquals($this->uri->rsegment(2, 'default'), 'default');
	}

	// --------------------------------------------------------------------

	public function test_uri_to_assoc()
	{
		$this->uri->segments = array('a', '1', 'b', '2', 'c', '3');

		$this->assertEquals(
			array('a' => '1', 'b' => '2', 'c' => '3'),
			$this->uri->uri_to_assoc(1)
		);

		$this->assertEquals(
			array('b' => '2', 'c' => '3'),
			$this->uri->uri_to_assoc(3)
		);

		$this->uri->keyval = array(); // reset cache
		$this->uri->segments = array('a', '1', 'b', '2', 'c');

		$this->assertEquals(
			array('a' => '1', 'b' => '2', 'c' => FALSE),
			$this->uri->uri_to_assoc(1)
		);

		$this->uri->keyval = array(); // reset cache
		$this->uri->segments = array('a', '1');

		// test default
		$this->assertEquals(
			array('a' => '1', 'b' => FALSE),
			$this->uri->uri_to_assoc(1, array('a', 'b'))
		);
	}

	// --------------------------------------------------------------------

	public function test_ruri_to_assoc()
	{
		$this->uri->rsegments = array('x', '1', 'y', '2', 'z', '3');

		$this->assertEquals(
			array('x' => '1', 'y' => '2', 'z' => '3'),
			$this->uri->ruri_to_assoc(1)
		);

		$this->assertEquals(
			array('y' => '2', 'z' => '3'),
			$this->uri->ruri_to_assoc(3)
		);

		$this->uri->keyval = array(); // reset cache
		$this->uri->rsegments = array('x', '1', 'y', '2', 'z');

		$this->assertEquals(
			array('x' => '1', 'y' => '2', 'z' => FALSE),
			$this->uri->ruri_to_assoc(1)
		);

		$this->uri->keyval = array(); // reset cache
		$this->uri->rsegments = array('x', '1');

		// test default
		$this->assertEquals(
			array('x' => '1', 'y' => FALSE),
			$this->uri->ruri_to_assoc(1, array('x', 'y'))
		);
	}

	// --------------------------------------------------------------------

	public function test_assoc_to_uri()
	{
		$this->uri->config->set_item('uri_string_slashes', 'none');
		$this->assertEquals('a/1/b/2', $this->uri->assoc_to_uri(array('a' => '1', 'b' => '2')));
	}

	// --------------------------------------------------------------------

	public function test_slash_segment()
	{
		$this->uri->segments[1] = 'segment';
		$this->uri->rsegments[1] = 'segment';

		$this->assertEquals('/segment/', $this->uri->slash_segment(1, 'both'));
		$this->assertEquals('/segment/', $this->uri->slash_rsegment(1, 'both'));

		$a = '/segment';
		$this->assertEquals('/segment', $this->uri->slash_segment(1, 'leading'));
		$this->assertEquals('/segment', $this->uri->slash_rsegment(1, 'leading'));

		$this->assertEquals('segment/', $this->uri->slash_segment(1, 'trailing'));
		$this->assertEquals('segment/', $this->uri->slash_rsegment(1, 'trailing'));
	}

}