summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/core/URI_test.php
blob: 40252aa146cee69183325679969cef9eebeed4c9 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php

require BASEPATH.'core/URI.php';

/**
 * Extend the URI class
 *  - override contructor
 *  - override CLI detection
 */
class URI_extended extends CI_URI {
	
	public function __construct()
	{
		$test = CI_TestCase::instance();
		$cls =& $test->ci_core_class('cfg');
		
		// set predictable config values
		$test->ci_set_config(array(
			'index_page'		=> 'index.php',
			'base_url'			=> 'http://example.com/',
			'subclass_prefix'	=> 'MY_'
		));

		$this->config = new $cls;	

	}
	
	protected function _is_cli_request()
	{
		return FALSE;
	}
}

class URI_test extends CI_TestCase {
	
	public function set_up()
	{
		$this->uri = new URI_extended();
	}

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

	public function test_set_uri_string()
	{
		// Slashes get killed
		$this->uri->_set_uri_string('/');
		
		$a = '';
		$b =& $this->uri->uri_string;
		
		$this->assertEquals($a, $b);
		
		$this->uri->_set_uri_string('nice/uri');
		
		$a = 'nice/uri';
		
		$this->assertEquals($a, $b);
	}
	
	// --------------------------------------------------------------------

	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();
		
		$a = 'controller/method';
		$b = $this->uri->uri_string;
		
		$this->assertEquals($a, $b);
		
		// death to request uri
		unset($_SERVER['REQUEST_URI']);
		
		// life to path info
		$_SERVER['PATH_INFO'] = '/controller/method/';
		
		$this->uri->_fetch_uri_string();
		
		$a = '/controller/method/';
		$b =& $this->uri->uri_string;

		$this->assertEquals($a, $b);
		
		// 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();

		$a = '/controller/method/';
		$b = $this->uri->uri_string;
		
		$this->assertEquals($a, $b);
		
		// At this point your server is a labotomy victim
		
		unset($_SERVER['QUERY_STRING']);
		
		$_GET['/controller/method/'] = '';
		
		$this->uri->_fetch_uri_string();
		$this->assertEquals($a, $b);

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

	public function test_explode_segments()
	{
		// Lets 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();
			
			$b = $this->uri->segments;
			
			$this->assertEquals($a, $b);
		}
		
	}

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

	public function test_filter_uri()
	{
		$this->uri->config->set_item('enable_query_strings', FALSE);
		$this->uri->config->set_item('permitted_uri_chars', 'a-z 0-9~%.:_\-');
		
		$str_in = 'abc01239~%.:_-';
		$str = $this->uri->_filter_uri($str_in);

		$this->assertEquals($str, $str_in);
	}

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

	public function test_filter_uri_escaping()
	{
		// ensure escaping even if dodgey characters are permitted
		
		$this->uri->config->set_item('enable_query_strings', FALSE);
		$this->uri->config->set_item('permitted_uri_chars', 'a-z 0-9~%.:_\-()$');

		$str = $this->uri->_filter_uri('$destroy_app(foo)');
		
		$this->assertEquals($str, '&#36;destroy_app&#40;foo&#41;');
	}

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

    public function test_filter_uri_throws_error()
    {
		$this->setExpectedException('RuntimeException');
		
		$this->uri->config->set_item('enable_query_strings', FALSE);
		$this->uri->config->set_item('permitted_uri_chars', 'a-z 0-9~%.:_\-');
		$this->uri->_filter_uri('$this()');
    }

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

	public function test_remove_url_suffix()
	{
		$this->uri->config->set_item('url_suffix', '.html');
		
		$this->uri->uri_string = 'controller/method/index.html';
		$this->uri->_remove_url_suffix();
		
		$this->assertEquals($this->uri->uri_string, 'controller/method/index');
		
		$this->uri->uri_string = 'controller/method/index.htmlify.html';
		$this->uri->_remove_url_suffix();
		
		$this->assertEquals($this->uri->uri_string, 'controller/method/index.htmlify');
	}

	// --------------------------------------------------------------------
	
	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');
		
		$a = array('a' => '1', 'b' => '2', 'c' => '3');
		$b = $this->uri->uri_to_assoc(1);
		$this->assertEquals($a, $b);
		
		$a = array('b' => '2', 'c' => '3');
		$b = $this->uri->uri_to_assoc(3);
		$this->assertEquals($a, $b);
		
		
		$this->uri->keyval = array(); // reset cache
				
		$this->uri->segments = array('a', '1', 'b', '2', 'c');
		
		$a = array('a' => '1', 'b' => '2', 'c' => FALSE);
		$b = $this->uri->uri_to_assoc(1);
		$this->assertEquals($a, $b);
		
		$this->uri->keyval = array(); // reset cache
		
		$this->uri->segments = array('a', '1');
		
		// test default
		$a = array('a' => '1', 'b' => FALSE);
		$b = $this->uri->uri_to_assoc(1, array('a', 'b'));
		$this->assertEquals($a, $b);
	}

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

	public function test_ruri_to_assoc()
	{
		$this->uri->rsegments = array('x', '1', 'y', '2', 'z', '3');
		
		$a = array('x' => '1', 'y' => '2', 'z' => '3');
		$b = $this->uri->ruri_to_assoc(1);
		$this->assertEquals($a, $b);
		
		$a = array('y' => '2', 'z' => '3');
		$b = $this->uri->ruri_to_assoc(3);
		$this->assertEquals($a, $b);
		
		
		$this->uri->keyval = array(); // reset cache
				
		$this->uri->rsegments = array('x', '1', 'y', '2', 'z');
		
		$a = array('x' => '1', 'y' => '2', 'z' => FALSE);
		$b = $this->uri->ruri_to_assoc(1);
		$this->assertEquals($a, $b);
		
		$this->uri->keyval = array(); // reset cache
		
		$this->uri->rsegments = array('x', '1');
		
		// test default
		$a = array('x' => '1', 'y' => FALSE);
		$b = $this->uri->ruri_to_assoc(1, array('x', 'y'));
		$this->assertEquals($a, $b);

	}

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

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

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

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

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


}
// END URI_test Class

/* End of file URI_test.php */
/* Location: ./tests/core/URI_test.php */