Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * RouterTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Routing
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Router', 'Routing');
20
App::uses('CakeResponse', 'Network');
21
 
22
if (!defined('FULL_BASE_URL')) {
23
	define('FULL_BASE_URL', 'http://cakephp.org');
24
}
25
 
26
/**
27
 * RouterTest class
28
 *
29
 * @package       Cake.Test.Case.Routing
30
 */
31
class RouterTest extends CakeTestCase {
32
 
33
/**
34
 * setUp method
35
 *
36
 * @return void
37
 */
38
	public function setUp() {
39
		parent::setUp();
40
		Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
41
	}
42
 
43
/**
44
 * tearDown method
45
 *
46
 * @return void
47
 */
48
	public function tearDown() {
49
		parent::tearDown();
50
		CakePlugin::unload();
51
		Router::fullBaseUrl('');
52
		Configure::write('App.fullBaseUrl', 'http://localhost');
53
	}
54
 
55
/**
56
 * testFullBaseUrl method
57
 *
58
 * @return void
59
 */
60
	public function testFullBaseUrl() {
61
		$this->assertRegExp('/^http(s)?:\/\//', Router::url('/', true));
62
		$this->assertRegExp('/^http(s)?:\/\//', Router::url(null, true));
63
		$this->assertRegExp('/^http(s)?:\/\//', Router::url(array('full_base' => true)));
64
		$this->assertSame(FULL_BASE_URL . '/', Router::url(array('full_base' => true)));
65
	}
66
 
67
/**
68
 * Tests that the base URL can be changed at runtime.
69
 *
70
 * @return void
71
 */
72
	public function testBaseUrl() {
73
		$this->assertEquals(FULL_BASE_URL, Router::fullBaseUrl());
74
		Router::fullBaseUrl('http://example.com');
75
		$this->assertEquals('http://example.com/', Router::url('/', true));
76
		$this->assertEquals('http://example.com', Configure::read('App.fullBaseUrl'));
77
		Router::fullBaseUrl('https://example.com');
78
		$this->assertEquals('https://example.com/', Router::url('/', true));
79
		$this->assertEquals('https://example.com', Configure::read('App.fullBaseUrl'));
80
	}
81
 
82
/**
83
 * Test that Router uses App.base to build URL's when there are no stored
84
 * request objects.
85
 *
86
 * @return void
87
 */
88
	public function testBaseUrlWithBasePath() {
89
		Configure::write('App.base', '/cakephp');
90
		Router::fullBaseUrl('http://example.com');
91
		$this->assertEquals('http://example.com/cakephp/tasks', Router::url('/tasks', true));
92
	}
93
 
94
/**
95
 * testRouteDefaultParams method
96
 *
97
 * @return void
98
 */
99
	public function testRouteDefaultParams() {
100
		Router::connect('/:controller', array('controller' => 'posts'));
101
		$this->assertEquals(Router::url(array('action' => 'index')), '/');
102
	}
103
 
104
/**
105
 * testMapResources method
106
 *
107
 * @return void
108
 */
109
	public function testMapResources() {
110
		$resources = Router::mapResources('Posts');
111
 
112
		$_SERVER['REQUEST_METHOD'] = 'GET';
113
		$result = Router::parse('/posts');
114
		$this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => 'GET'), $result);
115
		$this->assertEquals(array('posts'), $resources);
116
 
117
		$_SERVER['REQUEST_METHOD'] = 'GET';
118
		$result = Router::parse('/posts/13');
119
		$this->assertEquals(array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => '13', '[method]' => 'GET'), $result);
120
 
121
		$_SERVER['REQUEST_METHOD'] = 'POST';
122
		$result = Router::parse('/posts');
123
		$this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST'), $result);
124
 
125
		$_SERVER['REQUEST_METHOD'] = 'PUT';
126
		$result = Router::parse('/posts/13');
127
		$this->assertEquals(array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '13', '[method]' => 'PUT'), $result);
128
 
129
		$result = Router::parse('/posts/475acc39-a328-44d3-95fb-015000000000');
130
		$this->assertEquals(array('pass' => array('475acc39-a328-44d3-95fb-015000000000'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '475acc39-a328-44d3-95fb-015000000000', '[method]' => 'PUT'), $result);
131
 
132
		$_SERVER['REQUEST_METHOD'] = 'DELETE';
133
		$result = Router::parse('/posts/13');
134
		$this->assertEquals(array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'delete', 'id' => '13', '[method]' => 'DELETE'), $result);
135
 
136
		$_SERVER['REQUEST_METHOD'] = 'GET';
137
		$result = Router::parse('/posts/add');
138
		$this->assertSame(array(), $result);
139
 
140
		Router::reload();
141
		$resources = Router::mapResources('Posts', array('id' => '[a-z0-9_]+'));
142
		$this->assertEquals(array('posts'), $resources);
143
 
144
		$_SERVER['REQUEST_METHOD'] = 'GET';
145
		$result = Router::parse('/posts/add');
146
		$this->assertEquals(array('pass' => array('add'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => 'add', '[method]' => 'GET'), $result);
147
 
148
		$_SERVER['REQUEST_METHOD'] = 'PUT';
149
		$result = Router::parse('/posts/name');
150
		$this->assertEquals(array('pass' => array('name'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => 'name', '[method]' => 'PUT'), $result);
151
	}
152
 
153
/**
154
 * testMapResources with plugin controllers.
155
 *
156
 * @return void
157
 */
158
	public function testPluginMapResources() {
159
		App::build(array(
160
			'Plugin' => array(
161
				CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
162
			)
163
		));
164
		$resources = Router::mapResources('TestPlugin.TestPlugin');
165
 
166
		$_SERVER['REQUEST_METHOD'] = 'GET';
167
		$result = Router::parse('/test_plugin/test_plugin');
168
		$expected = array(
169
			'pass' => array(),
170
			'named' => array(),
171
			'plugin' => 'test_plugin',
172
			'controller' => 'test_plugin',
173
			'action' => 'index',
174
			'[method]' => 'GET'
175
		);
176
		$this->assertEquals($expected, $result);
177
		$this->assertEquals(array('test_plugin'), $resources);
178
 
179
		$_SERVER['REQUEST_METHOD'] = 'GET';
180
		$result = Router::parse('/test_plugin/test_plugin/13');
181
		$expected = array(
182
			'pass' => array('13'),
183
			'named' => array(),
184
			'plugin' => 'test_plugin',
185
			'controller' => 'test_plugin',
186
			'action' => 'view',
187
			'id' => '13',
188
			'[method]' => 'GET'
189
		);
190
		$this->assertEquals($expected, $result);
191
	}
192
 
193
/**
194
 * Test mapResources with a plugin and prefix.
195
 *
196
 * @return void
197
 */
198
	public function testPluginMapResourcesWithPrefix() {
199
		App::build(array(
200
			'Plugin' => array(
201
				CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
202
			)
203
		));
204
		$resources = Router::mapResources('TestPlugin.TestPlugin', array('prefix' => '/api/'));
205
 
206
		$_SERVER['REQUEST_METHOD'] = 'GET';
207
		$result = Router::parse('/api/test_plugin');
208
		$expected = array(
209
			'pass' => array(),
210
			'named' => array(),
211
			'plugin' => 'test_plugin',
212
			'controller' => 'test_plugin',
213
			'action' => 'index',
214
			'[method]' => 'GET'
215
		);
216
		$this->assertEquals($expected, $result);
217
		$this->assertEquals(array('test_plugin'), $resources);
218
 
219
		$resources = Router::mapResources('Posts', array('prefix' => 'api'));
220
 
221
		$_SERVER['REQUEST_METHOD'] = 'GET';
222
		$result = Router::parse('/api/posts');
223
		$expected = array(
224
			'pass' => array(),
225
			'named' => array(),
226
			'plugin' => null,
227
			'controller' => 'posts',
228
			'action' => 'index',
229
			'[method]' => 'GET'
230
		);
231
		$this->assertEquals($expected, $result);
232
	}
233
 
234
/**
235
 * testMultipleResourceRoute method
236
 *
237
 * @return void
238
 */
239
	public function testMultipleResourceRoute() {
240
		Router::connect('/:controller', array('action' => 'index', '[method]' => array('GET', 'POST')));
241
 
242
		$_SERVER['REQUEST_METHOD'] = 'GET';
243
		$result = Router::parse('/posts');
244
		$this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => array('GET', 'POST')), $result);
245
 
246
		$_SERVER['REQUEST_METHOD'] = 'POST';
247
		$result = Router::parse('/posts');
248
		$this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => array('GET', 'POST')), $result);
249
	}
250
 
251
/**
252
 * testGenerateUrlResourceRoute method
253
 *
254
 * @return void
255
 */
256
	public function testGenerateUrlResourceRoute() {
257
		Router::mapResources('Posts');
258
 
259
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', '[method]' => 'GET'));
260
		$expected = '/posts';
261
		$this->assertEquals($expected, $result);
262
 
263
		$result = Router::url(array('controller' => 'posts', 'action' => 'view', '[method]' => 'GET', 'id' => 10));
264
		$expected = '/posts/10';
265
		$this->assertEquals($expected, $result);
266
 
267
		$result = Router::url(array('controller' => 'posts', 'action' => 'add', '[method]' => 'POST'));
268
		$expected = '/posts';
269
		$this->assertEquals($expected, $result);
270
 
271
		$result = Router::url(array('controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'id' => 10));
272
		$expected = '/posts/10';
273
		$this->assertEquals($expected, $result);
274
 
275
		$result = Router::url(array('controller' => 'posts', 'action' => 'delete', '[method]' => 'DELETE', 'id' => 10));
276
		$expected = '/posts/10';
277
		$this->assertEquals($expected, $result);
278
 
279
		$result = Router::url(array('controller' => 'posts', 'action' => 'edit', '[method]' => 'POST', 'id' => 10));
280
		$expected = '/posts/10';
281
		$this->assertEquals($expected, $result);
282
	}
283
 
284
/**
285
 * testUrlNormalization method
286
 *
287
 * @return void
288
 */
289
	public function testUrlNormalization() {
290
		$expected = '/users/logout';
291
 
292
		$result = Router::normalize('/users/logout/');
293
		$this->assertEquals($expected, $result);
294
 
295
		$result = Router::normalize('//users//logout//');
296
		$this->assertEquals($expected, $result);
297
 
298
		$result = Router::normalize('users/logout');
299
		$this->assertEquals($expected, $result);
300
 
301
		$result = Router::normalize(array('controller' => 'users', 'action' => 'logout'));
302
		$this->assertEquals($expected, $result);
303
 
304
		$result = Router::normalize('/');
305
		$this->assertEquals('/', $result);
306
 
307
		$result = Router::normalize('http://google.com/');
308
		$this->assertEquals('http://google.com/', $result);
309
 
310
		$result = Router::normalize('http://google.com//');
311
		$this->assertEquals('http://google.com//', $result);
312
 
313
		$result = Router::normalize('/users/login/scope://foo');
314
		$this->assertEquals('/users/login/scope:/foo', $result);
315
 
316
		$result = Router::normalize('/recipe/recipes/add');
317
		$this->assertEquals('/recipe/recipes/add', $result);
318
 
319
		$request = new CakeRequest();
320
		$request->base = '/us';
321
		Router::setRequestInfo($request);
322
		$result = Router::normalize('/us/users/logout/');
323
		$this->assertEquals('/users/logout', $result);
324
 
325
		Router::reload();
326
 
327
		$request = new CakeRequest();
328
		$request->base = '/cake_12';
329
		Router::setRequestInfo($request);
330
		$result = Router::normalize('/cake_12/users/logout/');
331
		$this->assertEquals('/users/logout', $result);
332
 
333
		Router::reload();
334
		$_back = Configure::read('App.baseUrl');
335
		Configure::write('App.baseUrl', '/');
336
 
337
		$request = new CakeRequest();
338
		$request->base = '/';
339
		Router::setRequestInfo($request);
340
		$result = Router::normalize('users/login');
341
		$this->assertEquals('/users/login', $result);
342
		Configure::write('App.baseUrl', $_back);
343
 
344
		Router::reload();
345
		$request = new CakeRequest();
346
		$request->base = 'beer';
347
		Router::setRequestInfo($request);
348
		$result = Router::normalize('beer/admin/beers_tags/add');
349
		$this->assertEquals('/admin/beers_tags/add', $result);
350
 
351
		$result = Router::normalize('/admin/beers_tags/add');
352
		$this->assertEquals('/admin/beers_tags/add', $result);
353
	}
354
 
355
/**
356
 * test generation of basic URLs.
357
 *
358
 * @return void
359
 */
360
	public function testUrlGenerationBasic() {
361
		extract(Router::getNamedExpressions());
362
 
363
		$request = new CakeRequest();
364
		$request->addParams(array(
365
			'action' => 'index', 'plugin' => null, 'controller' => 'subscribe', 'admin' => true
366
		));
367
		$request->base = '/magazine';
368
		$request->here = '/magazine';
369
		$request->webroot = '/magazine/';
370
		Router::setRequestInfo($request);
371
 
372
		$result = Router::url();
373
		$this->assertEquals('/magazine', $result);
374
 
375
		Router::reload();
376
 
377
		Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
378
		$out = Router::url(array('controller' => 'pages', 'action' => 'display', 'home'));
379
		$this->assertEquals('/', $out);
380
 
381
		Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
382
		$result = Router::url(array('controller' => 'pages', 'action' => 'display', 'about'));
383
		$expected = '/pages/about';
384
		$this->assertEquals($expected, $result);
385
 
386
		Router::reload();
387
		Router::connect('/:plugin/:id/*', array('controller' => 'posts', 'action' => 'view'), array('id' => $ID));
388
		Router::parse('/');
389
 
390
		$result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1'));
391
		$expected = '/cake_plugin/1';
392
		$this->assertEquals($expected, $result);
393
 
394
		$result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0'));
395
		$expected = '/cake_plugin/1/0';
396
		$this->assertEquals($expected, $result);
397
 
398
		Router::reload();
399
		Router::connect('/:controller/:action/:id', array(), array('id' => $ID));
400
		Router::parse('/');
401
 
402
		$result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1'));
403
		$expected = '/posts/view/1';
404
		$this->assertEquals($expected, $result);
405
 
406
		Router::reload();
407
		Router::connect('/:controller/:id', array('action' => 'view'));
408
		Router::parse('/');
409
 
410
		$result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1'));
411
		$expected = '/posts/1';
412
		$this->assertEquals($expected, $result);
413
 
414
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0'));
415
		$expected = '/posts/index/0';
416
		$this->assertEquals($expected, $result);
417
 
418
		Router::connect('/view/*', array('controller' => 'posts', 'action' => 'view'));
419
		Router::promote();
420
		$result = Router::url(array('controller' => 'posts', 'action' => 'view', '1'));
421
		$expected = '/view/1';
422
		$this->assertEquals($expected, $result);
423
 
424
		Router::reload();
425
		$request = new CakeRequest();
426
		$request->addParams(array(
427
			'action' => 'index', 'plugin' => null, 'controller' => 'real_controller_name'
428
		));
429
		$request->base = '/';
430
		$request->here = '/';
431
		$request->webroot = '/';
432
		Router::setRequestInfo($request);
433
 
434
		Router::connect('short_controller_name/:action/*', array('controller' => 'real_controller_name'));
435
		Router::parse('/');
436
 
437
		$result = Router::url(array('controller' => 'real_controller_name', 'page' => '1'));
438
		$expected = '/short_controller_name/index/page:1';
439
		$this->assertEquals($expected, $result);
440
 
441
		$result = Router::url(array('action' => 'add'));
442
		$expected = '/short_controller_name/add';
443
		$this->assertEquals($expected, $result);
444
 
445
		Router::reload();
446
		Router::parse('/');
447
		$request = new CakeRequest();
448
		$request->addParams(array(
449
			'action' => 'index', 'plugin' => null, 'controller' => 'users', 'url' => array('url' => 'users')
450
		));
451
		$request->base = '/';
452
		$request->here = '/';
453
		$request->webroot = '/';
454
		Router::setRequestInfo($request);
455
 
456
		$result = Router::url(array('action' => 'login'));
457
		$expected = '/users/login';
458
		$this->assertEquals($expected, $result);
459
 
460
		Router::reload();
461
		Router::connect('/page/*', array('plugin' => null, 'controller' => 'pages', 'action' => 'view'));
462
		Router::parse('/');
463
 
464
		$result = Router::url(array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'view', 'my-page'));
465
		$expected = '/my_plugin/pages/view/my-page';
466
		$this->assertEquals($expected, $result);
467
 
468
		Router::reload();
469
		Router::connect('/contact/:action', array('plugin' => 'contact', 'controller' => 'contact'));
470
		Router::parse('/');
471
 
472
		$result = Router::url(array('plugin' => 'contact', 'controller' => 'contact', 'action' => 'me'));
473
 
474
		$expected = '/contact/me';
475
		$this->assertEquals($expected, $result);
476
 
477
		Router::reload();
478
		$request = new CakeRequest();
479
		$request->addParams(array(
480
			'action' => 'index', 'plugin' => 'myplugin', 'controller' => 'mycontroller', 'admin' => false
481
		));
482
		$request->base = '/';
483
		$request->here = '/';
484
		$request->webroot = '/';
485
		Router::setRequestInfo($request);
486
 
487
		$result = Router::url(array('plugin' => null, 'controller' => 'myothercontroller'));
488
		$expected = '/myothercontroller';
489
		$this->assertEquals($expected, $result);
490
	}
491
 
492
/**
493
 * Test that catch all routes work with a variety of falsey inputs.
494
 *
495
 * @return void
496
 */
497
	public function testUrlCatchAllRoute() {
498
		Router::connect('/*', array('controller' => 'categories', 'action' => 'index'));
499
		$result = Router::url(array('controller' => 'categories', 'action' => 'index', '0'));
500
		$this->assertEquals('/0', $result);
501
 
502
		$expected = array(
503
			'plugin' => null,
504
			'controller' => 'categories',
505
			'action' => 'index',
506
			'pass' => array('0'),
507
			'named' => array()
508
		);
509
		$result = Router::parse('/0');
510
		$this->assertEquals($expected, $result);
511
 
512
		$result = Router::parse('0');
513
		$this->assertEquals($expected, $result);
514
	}
515
 
516
/**
517
 * Tests using arrays in named parameters
518
 *
519
 * @return void
520
 */
521
	public function testArrayNamedParameters() {
522
		$result = Router::url(array('controller' => 'tests', 'pages' => array(
523
			1, 2, 3
524
		)));
525
		$expected = '/tests/index/pages%5B0%5D:1/pages%5B1%5D:2/pages%5B2%5D:3';
526
		$this->assertEquals($expected, $result);
527
 
528
		$result = Router::url(array('controller' => 'tests',
529
			'pages' => array(
530
				'param1' => array(
531
					'one',
532
					'two'
533
				),
534
				'three'
535
			)
536
		));
537
		$expected = '/tests/index/pages%5Bparam1%5D%5B0%5D:one/pages%5Bparam1%5D%5B1%5D:two/pages%5B0%5D:three';
538
		$this->assertEquals($expected, $result);
539
 
540
		$result = Router::url(array('controller' => 'tests',
541
			'pages' => array(
542
				'param1' => array(
543
					'one' => 1,
544
					'two' => 2
545
				),
546
				'three'
547
			)
548
		));
549
		$expected = '/tests/index/pages%5Bparam1%5D%5Bone%5D:1/pages%5Bparam1%5D%5Btwo%5D:2/pages%5B0%5D:three';
550
		$this->assertEquals($expected, $result);
551
 
552
		$result = Router::url(array('controller' => 'tests',
553
			'super' => array(
554
				'nested' => array(
555
					'array' => 'awesome',
556
					'something' => 'else'
557
				),
558
				'cool'
559
			)
560
		));
561
		$expected = '/tests/index/super%5Bnested%5D%5Barray%5D:awesome/super%5Bnested%5D%5Bsomething%5D:else/super%5B0%5D:cool';
562
		$this->assertEquals($expected, $result);
563
 
564
		$result = Router::url(array('controller' => 'tests', 'namedParam' => array(
565
			'keyed' => 'is an array',
566
			'test'
567
		)));
568
		$expected = '/tests/index/namedParam%5Bkeyed%5D:is%20an%20array/namedParam%5B0%5D:test';
569
		$this->assertEquals($expected, $result);
570
	}
571
 
572
/**
573
 * Test generation of routes with query string parameters.
574
 *
575
 * @return void
576
 */
577
	public function testUrlGenerationWithQueryStrings() {
578
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
579
		$expected = '/posts/index/0?var=test&var2=test2';
580
		$this->assertEquals($expected, $result);
581
 
582
		$result = Router::url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2'));
583
		$this->assertEquals($expected, $result);
584
 
585
		$result = Router::url(array('controller' => 'posts', '0', '?' => array('var' => 'test', 'var2' => 'test2')));
586
		$this->assertEquals($expected, $result);
587
 
588
		$result = Router::url(array('controller' => 'posts', '0', '?' => array('var' => null)));
589
		$this->assertEquals('/posts/index/0', $result);
590
 
591
		$result = Router::url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2', '#' => 'unencoded string %'));
592
		$expected = '/posts/index/0?var=test&var2=test2#unencoded string %';
593
		$this->assertEquals($expected, $result);
594
	}
595
 
596
/**
597
 * test that regex validation of keyed route params is working.
598
 *
599
 * @return void
600
 */
601
	public function testUrlGenerationWithRegexQualifiedParams() {
602
		Router::connect(
603
			':language/galleries',
604
			array('controller' => 'galleries', 'action' => 'index'),
605
			array('language' => '[a-z]{3}')
606
		);
607
 
608
		Router::connect(
609
			'/:language/:admin/:controller/:action/*',
610
			array('admin' => 'admin'),
611
			array('language' => '[a-z]{3}', 'admin' => 'admin')
612
		);
613
 
614
		Router::connect('/:language/:controller/:action/*',
615
			array(),
616
			array('language' => '[a-z]{3}')
617
		);
618
 
619
		$result = Router::url(array('admin' => false, 'language' => 'dan', 'action' => 'index', 'controller' => 'galleries'));
620
		$expected = '/dan/galleries';
621
		$this->assertEquals($expected, $result);
622
 
623
		$result = Router::url(array('admin' => false, 'language' => 'eng', 'action' => 'index', 'controller' => 'galleries'));
624
		$expected = '/eng/galleries';
625
		$this->assertEquals($expected, $result);
626
 
627
		Router::reload();
628
		Router::connect('/:language/pages',
629
			array('controller' => 'pages', 'action' => 'index'),
630
			array('language' => '[a-z]{3}')
631
		);
632
		Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}'));
633
 
634
		$result = Router::url(array('language' => 'eng', 'action' => 'index', 'controller' => 'pages'));
635
		$expected = '/eng/pages';
636
		$this->assertEquals($expected, $result);
637
 
638
		$result = Router::url(array('language' => 'eng', 'controller' => 'pages'));
639
		$this->assertEquals($expected, $result);
640
 
641
		$result = Router::url(array('language' => 'eng', 'controller' => 'pages', 'action' => 'add'));
642
		$expected = '/eng/pages/add';
643
		$this->assertEquals($expected, $result);
644
 
645
		Router::reload();
646
		Router::connect('/forestillinger/:month/:year/*',
647
			array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'),
648
			array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')
649
		);
650
		Router::parse('/');
651
 
652
		$result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'month' => 10, 'year' => 2007, 'min-forestilling'));
653
		$expected = '/forestillinger/10/2007/min-forestilling';
654
		$this->assertEquals($expected, $result);
655
 
656
		Router::reload();
657
		Router::connect('/kalender/:month/:year/*',
658
			array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'),
659
			array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')
660
		);
661
		Router::connect('/kalender/*', array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'));
662
		Router::parse('/');
663
 
664
		$result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'min-forestilling'));
665
		$expected = '/kalender/min-forestilling';
666
		$this->assertEquals($expected, $result);
667
 
668
		$result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'min-forestilling'));
669
		$expected = '/kalender/10/2007/min-forestilling';
670
		$this->assertEquals($expected, $result);
671
 
672
		Router::reload();
673
		Router::connect('/:controller/:action/*', array(), array(
674
			'controller' => 'source|wiki|commits|tickets|comments|view',
675
			'action' => 'branches|history|branch|logs|view|start|add|edit|modify'
676
		));
677
	}
678
 
679
/**
680
 * Test URL generation with an admin prefix
681
 *
682
 * @return void
683
 */
684
	public function testUrlGenerationWithAdminPrefix() {
685
		Configure::write('Routing.prefixes', array('admin'));
686
		Router::reload();
687
 
688
		Router::connectNamed(array('event', 'lang'));
689
		Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
690
		Router::connect('/pages/contact_us', array('controller' => 'pages', 'action' => 'contact_us'));
691
		Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
692
		Router::connect('/reset/*', array('admin' => true, 'controller' => 'users', 'action' => 'reset'));
693
		Router::connect('/tests', array('controller' => 'tests', 'action' => 'index'));
694
		Router::parseExtensions('rss');
695
 
696
		$request = new CakeRequest();
697
		$request->addParams(array(
698
			'controller' => 'registrations', 'action' => 'admin_index',
699
			'plugin' => null, 'prefix' => 'admin', 'admin' => true,
700
			'ext' => 'html'
701
		));
702
		$request->base = '';
703
		$request->here = '/admin/registrations/index';
704
		$request->webroot = '/';
705
		Router::setRequestInfo($request);
706
 
707
		$result = Router::url(array('page' => 2));
708
		$expected = '/admin/registrations/index/page:2';
709
		$this->assertEquals($expected, $result);
710
 
711
		Router::reload();
712
		$request = new CakeRequest();
713
		$request->addParams(array(
714
			'controller' => 'subscriptions', 'action' => 'admin_index',
715
			'plugin' => null, 'admin' => true,
716
			'url' => array('url' => 'admin/subscriptions/index/page:2')
717
		));
718
		$request->base = '/magazine';
719
		$request->here = '/magazine/admin/subscriptions/index/page:2';
720
		$request->webroot = '/magazine/';
721
		Router::setRequestInfo($request);
722
 
723
		Router::parse('/');
724
 
725
		$result = Router::url(array('page' => 3));
726
		$expected = '/magazine/admin/subscriptions/index/page:3';
727
		$this->assertEquals($expected, $result);
728
 
729
		Router::reload();
730
		Router::connect('/admin/subscriptions/:action/*', array('controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin'));
731
		Router::parse('/');
732
 
733
		$request = new CakeRequest();
734
		$request->addParams(array(
735
			'action' => 'admin_index', 'plugin' => null, 'controller' => 'subscribe',
736
			'admin' => true, 'url' => array('url' => 'admin/subscriptions/edit/1')
737
		));
738
		$request->base = '/magazine';
739
		$request->here = '/magazine/admin/subscriptions/edit/1';
740
		$request->webroot = '/magazine/';
741
		Router::setRequestInfo($request);
742
 
743
		$result = Router::url(array('action' => 'edit', 1));
744
		$expected = '/magazine/admin/subscriptions/edit/1';
745
		$this->assertEquals($expected, $result);
746
 
747
		$result = Router::url(array('admin' => true, 'controller' => 'users', 'action' => 'login'));
748
		$expected = '/magazine/admin/users/login';
749
		$this->assertEquals($expected, $result);
750
 
751
		Router::reload();
752
		$request = new CakeRequest();
753
		$request->addParams(array(
754
			'admin' => true, 'action' => 'index', 'plugin' => null, 'controller' => 'users',
755
			'url' => array('url' => 'users')
756
		));
757
		$request->base = '/';
758
		$request->here = '/';
759
		$request->webroot = '/';
760
		Router::setRequestInfo($request);
761
 
762
		Router::connect('/page/*', array('controller' => 'pages', 'action' => 'view', 'admin' => true, 'prefix' => 'admin'));
763
		Router::parse('/');
764
 
765
		$result = Router::url(array('admin' => true, 'controller' => 'pages', 'action' => 'view', 'my-page'));
766
		$expected = '/page/my-page';
767
		$this->assertEquals($expected, $result);
768
 
769
		Router::reload();
770
 
771
		$request = new CakeRequest();
772
		$request->addParams(array(
773
			'plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'prefix' => 'admin', 'admin' => true,
774
			'url' => array('url' => 'admin/pages/add')
775
		));
776
		$request->base = '';
777
		$request->here = '/admin/pages/add';
778
		$request->webroot = '/';
779
		Router::setRequestInfo($request);
780
		Router::parse('/');
781
 
782
		$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
783
		$expected = '/admin/pages/add';
784
		$this->assertEquals($expected, $result);
785
 
786
		Router::reload();
787
		Router::parse('/');
788
		$request = new CakeRequest();
789
		$request->addParams(array(
790
			'plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'prefix' => 'admin', 'admin' => true,
791
			'url' => array('url' => 'admin/pages/add')
792
		));
793
		$request->base = '';
794
		$request->here = '/admin/pages/add';
795
		$request->webroot = '/';
796
		Router::setRequestInfo($request);
797
 
798
		$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
799
		$expected = '/admin/pages/add';
800
		$this->assertEquals($expected, $result);
801
 
802
		Router::reload();
803
		Router::connect('/admin/:controller/:action/:id', array('admin' => true), array('id' => '[0-9]+'));
804
		Router::parse('/');
805
		$request = new CakeRequest();
806
		Router::setRequestInfo(
807
			$request->addParams(array(
808
				'plugin' => null, 'controller' => 'pages', 'action' => 'admin_edit', 'pass' => array('284'),
809
				'prefix' => 'admin', 'admin' => true,
810
				'url' => array('url' => 'admin/pages/edit/284')
811
			))->addPaths(array(
812
				'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/'
813
			))
814
		);
815
 
816
		$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 'id' => '284'));
817
		$expected = '/admin/pages/edit/284';
818
		$this->assertEquals($expected, $result);
819
 
820
		Router::reload();
821
		Router::parse('/');
822
 
823
		$request = new CakeRequest();
824
		Router::setRequestInfo(
825
			$request->addParams(array(
826
				'plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'prefix' => 'admin',
827
				'admin' => true, 'url' => array('url' => 'admin/pages/add')
828
			))->addPaths(array(
829
				'base' => '', 'here' => '/admin/pages/add', 'webroot' => '/'
830
			))
831
		);
832
 
833
		$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
834
		$expected = '/admin/pages/add';
835
		$this->assertEquals($expected, $result);
836
 
837
		Router::reload();
838
		Router::parse('/');
839
 
840
		$request = new CakeRequest();
841
		Router::setRequestInfo(
842
			$request->addParams(array(
843
				'plugin' => null, 'controller' => 'pages', 'action' => 'admin_edit', 'prefix' => 'admin',
844
				'admin' => true, 'pass' => array('284'), 'url' => array('url' => 'admin/pages/edit/284')
845
			))->addPaths(array(
846
				'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/'
847
			))
848
		);
849
 
850
		$result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 284));
851
		$expected = '/admin/pages/edit/284';
852
		$this->assertEquals($expected, $result);
853
 
854
		Router::reload();
855
		Router::connect('/admin/posts/*', array('controller' => 'posts', 'action' => 'index', 'admin' => true));
856
		Router::parse('/');
857
		Router::setRequestInfo(
858
			$request->addParams(array(
859
				'plugin' => null, 'controller' => 'posts', 'action' => 'admin_index', 'prefix' => 'admin',
860
				'admin' => true, 'pass' => array('284'), 'url' => array('url' => 'admin/posts')
861
			))->addPaths(array(
862
				'base' => '', 'here' => '/admin/posts', 'webroot' => '/'
863
			))
864
		);
865
 
866
		$result = Router::url(array('all'));
867
		$expected = '/admin/posts/all';
868
		$this->assertEquals($expected, $result);
869
	}
870
 
871
/**
872
 * testUrlGenerationWithExtensions method
873
 *
874
 * @return void
875
 */
876
	public function testUrlGenerationWithExtensions() {
877
		Router::parse('/');
878
		$result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'add', 'id' => null, 'ext' => 'json'));
879
		$expected = '/articles/add.json';
880
		$this->assertEquals($expected, $result);
881
 
882
		$result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'add', 'ext' => 'json'));
883
		$expected = '/articles/add.json';
884
		$this->assertEquals($expected, $result);
885
 
886
		$result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'id' => null, 'ext' => 'json'));
887
		$expected = '/articles.json';
888
		$this->assertEquals($expected, $result);
889
 
890
		$result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'ext' => 'json'));
891
		$expected = '/articles.json';
892
		$this->assertEquals($expected, $result);
893
	}
894
 
895
/**
896
 * testPluginUrlGeneration method
897
 *
898
 * @return void
899
 */
900
	public function testUrlGenerationPlugins() {
901
		$request = new CakeRequest();
902
		Router::setRequestInfo(
903
			$request->addParams(array(
904
				'plugin' => 'test', 'controller' => 'controller', 'action' => 'index'
905
			))->addPaths(array(
906
				'base' => '/base', 'here' => '/clients/sage/portal/donations', 'webroot' => '/base/'
907
			))
908
		);
909
 
910
		$this->assertEquals(Router::url('read/1'), '/base/test/controller/read/1');
911
 
912
		Router::reload();
913
		Router::connect('/:lang/:plugin/:controller/*', array('action' => 'index'));
914
 
915
		$request = new CakeRequest();
916
		Router::setRequestInfo(
917
			$request->addParams(array(
918
				'lang' => 'en',
919
				'plugin' => 'shows', 'controller' => 'shows', 'action' => 'index',
920
				'url' => array('url' => 'en/shows/'),
921
			))->addPaths(array(
922
				'base' => '', 'here' => '/en/shows', 'webroot' => '/'
923
			))
924
		);
925
 
926
		Router::parse('/en/shows/');
927
 
928
		$result = Router::url(array(
929
			'lang' => 'en',
930
			'controller' => 'shows', 'action' => 'index', 'page' => '1',
931
		));
932
		$expected = '/en/shows/shows/page:1';
933
		$this->assertEquals($expected, $result);
934
	}
935
 
936
/**
937
 * test that you can leave active plugin routes with plugin = null
938
 *
939
 * @return void
940
 */
941
	public function testCanLeavePlugin() {
942
		Router::reload();
943
		Router::connect(
944
			'/admin/other/:controller/:action/*',
945
			array(
946
				'admin' => 1,
947
				'plugin' => 'aliased',
948
				'prefix' => 'admin'
949
			)
950
		);
951
		$request = new CakeRequest();
952
		Router::setRequestInfo(
953
			$request->addParams(array(
954
				'pass' => array(),
955
				'admin' => true,
956
				'prefix' => 'admin',
957
				'plugin' => 'this',
958
				'action' => 'admin_index',
959
				'controller' => 'interesting',
960
				'url' => array('url' => 'admin/this/interesting/index'),
961
			))->addPaths(array(
962
				'base' => '',
963
				'here' => '/admin/this/interesting/index',
964
				'webroot' => '/',
965
			))
966
		);
967
		$result = Router::url(array('plugin' => null, 'controller' => 'posts', 'action' => 'index'));
968
		$this->assertEquals('/admin/posts', $result);
969
 
970
		$result = Router::url(array('controller' => 'posts', 'action' => 'index'));
971
		$this->assertEquals('/admin/this/posts', $result);
972
 
973
		$result = Router::url(array('plugin' => 'aliased', 'controller' => 'posts', 'action' => 'index'));
974
		$this->assertEquals('/admin/other/posts/index', $result);
975
	}
976
 
977
/**
978
 * testUrlParsing method
979
 *
980
 * @return void
981
 */
982
	public function testUrlParsing() {
983
		extract(Router::getNamedExpressions());
984
 
985
		Router::connect('/posts/:value/:somevalue/:othervalue/*', array('controller' => 'posts', 'action' => 'view'), array('value', 'somevalue', 'othervalue'));
986
		$result = Router::parse('/posts/2007/08/01/title-of-post-here');
987
		$expected = array('value' => '2007', 'somevalue' => '08', 'othervalue' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
988
		$this->assertEquals($expected, $result);
989
 
990
		Router::reload();
991
		Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
992
		$result = Router::parse('/posts/2007/08/01/title-of-post-here');
993
		$expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
994
		$this->assertEquals($expected, $result);
995
 
996
		Router::reload();
997
		Router::connect('/posts/:day/:year/:month/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
998
		$result = Router::parse('/posts/01/2007/08/title-of-post-here');
999
		$expected = array('day' => '01', 'year' => '2007', 'month' => '08', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
1000
		$this->assertEquals($expected, $result);
1001
 
1002
		Router::reload();
1003
		Router::connect('/posts/:month/:day/:year/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
1004
		$result = Router::parse('/posts/08/01/2007/title-of-post-here');
1005
		$expected = array('month' => '08', 'day' => '01', 'year' => '2007', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
1006
		$this->assertEquals($expected, $result);
1007
 
1008
		Router::reload();
1009
		Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view'));
1010
		$result = Router::parse('/posts/2007/08/01/title-of-post-here');
1011
		$expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
1012
		$this->assertEquals($expected, $result);
1013
 
1014
		Router::reload();
1015
		require CAKE . 'Config' . DS . 'routes.php';
1016
		$result = Router::parse('/pages/display/home');
1017
		$expected = array('plugin' => null, 'pass' => array('home'), 'controller' => 'pages', 'action' => 'display', 'named' => array());
1018
		$this->assertEquals($expected, $result);
1019
 
1020
		$result = Router::parse('pages/display/home/');
1021
		$this->assertEquals($expected, $result);
1022
 
1023
		$result = Router::parse('pages/display/home');
1024
		$this->assertEquals($expected, $result);
1025
 
1026
		Router::reload();
1027
		Router::connect('/page/*', array('controller' => 'test'));
1028
		$result = Router::parse('/page/my-page');
1029
		$expected = array('pass' => array('my-page'), 'plugin' => null, 'controller' => 'test', 'action' => 'index', 'named' => array());
1030
		$this->assertEquals($expected, $result);
1031
 
1032
		Router::reload();
1033
		Router::connect('/:language/contact', array('language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'), array('language' => '[a-z]{3}'));
1034
		$result = Router::parse('/eng/contact');
1035
		$expected = array('pass' => array(), 'named' => array(), 'language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index');
1036
		$this->assertEquals($expected, $result);
1037
 
1038
		Router::reload();
1039
		Router::connect('/forestillinger/:month/:year/*',
1040
			array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'),
1041
			array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')
1042
		);
1043
 
1044
		$result = Router::parse('/forestillinger/10/2007/min-forestilling');
1045
		$expected = array('pass' => array('min-forestilling'), 'plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'named' => array());
1046
		$this->assertEquals($expected, $result);
1047
 
1048
		Router::reload();
1049
		Router::connect('/:controller/:action/*');
1050
		Router::connect('/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'display'));
1051
		$result = Router::parse('/');
1052
		$expected = array('pass' => array(), 'named' => array(), 'controller' => 'pages', 'action' => 'display', 'plugin' => 'pages');
1053
		$this->assertEquals($expected, $result);
1054
 
1055
		$result = Router::parse('/posts/edit/0');
1056
		$expected = array('pass' => array(0), 'named' => array(), 'controller' => 'posts', 'action' => 'edit', 'plugin' => null);
1057
		$this->assertEquals($expected, $result);
1058
 
1059
		Router::reload();
1060
		Router::connect('/posts/:id::url_title', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\d]+'));
1061
		$result = Router::parse('/posts/5:sample-post-title');
1062
		$expected = array('pass' => array('5', 'sample-post-title'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
1063
		$this->assertEquals($expected, $result);
1064
 
1065
		Router::reload();
1066
		Router::connect('/posts/:id::url_title/*', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\d]+'));
1067
		$result = Router::parse('/posts/5:sample-post-title/other/params/4');
1068
		$expected = array('pass' => array('5', 'sample-post-title', 'other', 'params', '4'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
1069
		$this->assertEquals($expected, $result);
1070
 
1071
		Router::reload();
1072
		Router::connect('/posts/:url_title-(uuid::id)', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => $UUID));
1073
		$result = Router::parse('/posts/sample-post-title-(uuid:47fc97a9-019c-41d1-a058-1fa3cbdd56cb)');
1074
		$expected = array('pass' => array('47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'sample-post-title'), 'named' => array(), 'id' => '47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
1075
		$this->assertEquals($expected, $result);
1076
 
1077
		Router::reload();
1078
		Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => false));
1079
		$result = Router::parse('/posts/view/foo:bar/routing:fun');
1080
		$expected = array('pass' => array('foo:bar', 'routing:fun'), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
1081
		$this->assertEquals($expected, $result);
1082
 
1083
		Router::reload();
1084
		Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer')));
1085
		$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42');
1086
		$expected = array('pass' => array('routing:fun'), 'named' => array('foo' => 'bar', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
1087
		$this->assertEquals($expected, $result);
1088
 
1089
		Router::reload();
1090
		Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedyNamed' => true));
1091
		$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42');
1092
		$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
1093
		$this->assertEquals($expected, $result);
1094
 
1095
		Router::reload();
1096
		Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedyNamed' => true));
1097
		$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42?id=123&tab=abc');
1098
		$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '?' => array('id' => '123', 'tab' => 'abc'));
1099
		$this->assertEquals($expected, $result);
1100
	}
1101
 
1102
/**
1103
 * test that the persist key works.
1104
 *
1105
 * @return void
1106
 */
1107
	public function testPersistentParameters() {
1108
		Router::reload();
1109
		Router::connect(
1110
			'/:lang/:color/posts/view/*',
1111
			array('controller' => 'posts', 'action' => 'view'),
1112
			array('persist' => array('lang', 'color'))
1113
		);
1114
		Router::connect(
1115
			'/:lang/:color/posts/index',
1116
			array('controller' => 'posts', 'action' => 'index'),
1117
			array('persist' => array('lang'))
1118
		);
1119
		Router::connect('/:lang/:color/posts/edit/*', array('controller' => 'posts', 'action' => 'edit'));
1120
		Router::connect('/about', array('controller' => 'pages', 'action' => 'view', 'about'));
1121
		Router::parse('/en/red/posts/view/5');
1122
 
1123
		$request = new CakeRequest();
1124
		Router::setRequestInfo(
1125
			$request->addParams(array(
1126
				'lang' => 'en',
1127
				'color' => 'red',
1128
				'prefix' => 'admin',
1129
				'plugin' => null,
1130
				'action' => 'view',
1131
				'controller' => 'posts',
1132
			))->addPaths(array(
1133
				'base' => '/',
1134
				'here' => '/en/red/posts/view/5',
1135
				'webroot' => '/',
1136
			))
1137
		);
1138
		$expected = '/en/red/posts/view/6';
1139
		$result = Router::url(array('controller' => 'posts', 'action' => 'view', 6));
1140
		$this->assertEquals($expected, $result);
1141
 
1142
		$expected = '/en/blue/posts/index';
1143
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'color' => 'blue'));
1144
		$this->assertEquals($expected, $result);
1145
 
1146
		$expected = '/posts/edit/6';
1147
		$result = Router::url(array('controller' => 'posts', 'action' => 'edit', 6, 'color' => null, 'lang' => null));
1148
		$this->assertEquals($expected, $result);
1149
 
1150
		$expected = '/posts';
1151
		$result = Router::url(array('controller' => 'posts', 'action' => 'index'));
1152
		$this->assertEquals($expected, $result);
1153
 
1154
		$expected = '/posts/edit/7';
1155
		$result = Router::url(array('controller' => 'posts', 'action' => 'edit', 7));
1156
		$this->assertEquals($expected, $result);
1157
 
1158
		$expected = '/about';
1159
		$result = Router::url(array('controller' => 'pages', 'action' => 'view', 'about'));
1160
		$this->assertEquals($expected, $result);
1161
	}
1162
 
1163
/**
1164
 * testUuidRoutes method
1165
 *
1166
 * @return void
1167
 */
1168
	public function testUuidRoutes() {
1169
		Router::connect(
1170
			'/subjects/add/:category_id',
1171
			array('controller' => 'subjects', 'action' => 'add'),
1172
			array('category_id' => '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}')
1173
		);
1174
		$result = Router::parse('/subjects/add/4795d601-19c8-49a6-930e-06a8b01d17b7');
1175
		$expected = array('pass' => array(), 'named' => array(), 'category_id' => '4795d601-19c8-49a6-930e-06a8b01d17b7', 'plugin' => null, 'controller' => 'subjects', 'action' => 'add');
1176
		$this->assertEquals($expected, $result);
1177
	}
1178
 
1179
/**
1180
 * testRouteSymmetry method
1181
 *
1182
 * @return void
1183
 */
1184
	public function testRouteSymmetry() {
1185
		Router::connect(
1186
			"/:extra/page/:slug/*",
1187
			array('controller' => 'pages', 'action' => 'view', 'extra' => null),
1188
			array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
1189
		);
1190
 
1191
		$result = Router::parse('/some_extra/page/this_is_the_slug');
1192
		$expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => 'some_extra');
1193
		$this->assertEquals($expected, $result);
1194
 
1195
		$result = Router::parse('/page/this_is_the_slug');
1196
		$expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null);
1197
		$this->assertEquals($expected, $result);
1198
 
1199
		Router::reload();
1200
		Router::connect(
1201
			"/:extra/page/:slug/*",
1202
			array('controller' => 'pages', 'action' => 'view', 'extra' => null),
1203
			array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+')
1204
		);
1205
		Router::parse('/');
1206
 
1207
		$result = Router::url(array('admin' => null, 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null));
1208
		$expected = '/page/this_is_the_slug';
1209
		$this->assertEquals($expected, $result);
1210
 
1211
		$result = Router::url(array('admin' => null, 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => 'some_extra'));
1212
		$expected = '/some_extra/page/this_is_the_slug';
1213
		$this->assertEquals($expected, $result);
1214
	}
1215
 
1216
/**
1217
 * Test parse and reverse symmetry
1218
 *
1219
 * @return void
1220
 * @dataProvider parseReverseSymmetryData
1221
 */
1222
	public function testParseReverseSymmetry($url) {
1223
		$this->assertSame($url, Router::reverse(Router::parse($url) + array('url' => array())));
1224
	}
1225
 
1226
/**
1227
 * Data for parse and reverse test
1228
 *
1229
 * @return array
1230
 */
1231
	public function parseReverseSymmetryData() {
1232
		return array(
1233
			array('/'),
1234
			array('/controller/action'),
1235
			array('/controller/action/param'),
1236
			array('/controller/action?param1=value1&param2=value2'),
1237
			array('/controller/action/param?param1=value1'),
1238
			array('/controller/action/named1:nv1'),
1239
			array('/controller/action/named1:nv1?param1=value1')
1240
		);
1241
	}
1242
 
1243
/**
1244
 * Test that Routing.prefixes are used when a Router instance is created
1245
 * or reset
1246
 *
1247
 * @return void
1248
 */
1249
	public function testRoutingPrefixesSetting() {
1250
		$restore = Configure::read('Routing');
1251
 
1252
		Configure::write('Routing.prefixes', array('admin', 'member', 'super_user'));
1253
		Router::reload();
1254
		$result = Router::prefixes();
1255
		$expected = array('admin', 'member', 'super_user');
1256
		$this->assertEquals($expected, $result);
1257
 
1258
		Configure::write('Routing.prefixes', array('admin', 'member'));
1259
		Router::reload();
1260
		$result = Router::prefixes();
1261
		$expected = array('admin', 'member');
1262
		$this->assertEquals($expected, $result);
1263
 
1264
		Configure::write('Routing', $restore);
1265
	}
1266
 
1267
/**
1268
 * Test prefix routing and plugin combinations
1269
 *
1270
 * @return void
1271
 */
1272
	public function testPrefixRoutingAndPlugins() {
1273
		Configure::write('Routing.prefixes', array('admin'));
1274
		$paths = App::path('plugins');
1275
		App::build(array(
1276
			'plugins' => array(
1277
				CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
1278
			)
1279
		), App::RESET);
1280
		CakePlugin::load(array('TestPlugin'));
1281
 
1282
		Router::reload();
1283
		require CAKE . 'Config' . DS . 'routes.php';
1284
		$request = new CakeRequest();
1285
		Router::setRequestInfo(
1286
			$request->addParams(array(
1287
				'admin' => true, 'controller' => 'controller', 'action' => 'action',
1288
				'plugin' => null, 'prefix' => 'admin'
1289
			))->addPaths(array(
1290
				'base' => '/',
1291
				'here' => '/',
1292
				'webroot' => '/base/',
1293
			))
1294
		);
1295
		Router::parse('/');
1296
 
1297
		$result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index'));
1298
		$expected = '/admin/test_plugin';
1299
		$this->assertEquals($expected, $result);
1300
 
1301
		Router::reload();
1302
		require CAKE . 'Config' . DS . 'routes.php';
1303
		$request = new CakeRequest();
1304
		Router::setRequestInfo(
1305
			$request->addParams(array(
1306
				'plugin' => 'test_plugin', 'controller' => 'show_tickets', 'action' => 'admin_edit',
1307
				'pass' => array('6'), 'prefix' => 'admin', 'admin' => true, 'form' => array(),
1308
				'url' => array('url' => 'admin/shows/show_tickets/edit/6')
1309
			))->addPaths(array(
1310
				'base' => '/',
1311
				'here' => '/admin/shows/show_tickets/edit/6',
1312
				'webroot' => '/',
1313
			))
1314
		);
1315
 
1316
		$result = Router::url(array(
1317
			'plugin' => 'test_plugin', 'controller' => 'show_tickets', 'action' => 'edit', 6,
1318
			'admin' => true, 'prefix' => 'admin'
1319
		));
1320
		$expected = '/admin/test_plugin/show_tickets/edit/6';
1321
		$this->assertEquals($expected, $result);
1322
 
1323
		$result = Router::url(array(
1324
			'plugin' => 'test_plugin', 'controller' => 'show_tickets', 'action' => 'index', 'admin' => true
1325
		));
1326
		$expected = '/admin/test_plugin/show_tickets';
1327
		$this->assertEquals($expected, $result);
1328
 
1329
		App::build(array('plugins' => $paths));
1330
	}
1331
 
1332
/**
1333
 * testParseExtensions method
1334
 *
1335
 * @return void
1336
 */
1337
	public function testParseExtensions() {
1338
		$this->assertEquals(array(), Router::extensions());
1339
 
1340
		Router::parseExtensions('rss');
1341
		$this->assertEquals(array('rss'), Router::extensions());
1342
	}
1343
 
1344
/**
1345
 * testSetExtensions method
1346
 *
1347
 * @return void
1348
 */
1349
	public function testSetExtensions() {
1350
		Router::setExtensions(array('rss'));
1351
		$this->assertEquals(array('rss'), Router::extensions());
1352
 
1353
		require CAKE . 'Config' . DS . 'routes.php';
1354
		$result = Router::parse('/posts.rss');
1355
		$this->assertFalse(isset($result['ext']));
1356
 
1357
		Router::parseExtensions();
1358
		$result = Router::parse('/posts.rss');
1359
		$this->assertEquals('rss', $result['ext']);
1360
 
1361
		$result = Router::parse('/posts.xml');
1362
		$this->assertFalse(isset($result['ext']));
1363
 
1364
		Router::setExtensions(array('xml'));
1365
		$result = Router::extensions();
1366
		$this->assertEquals(array('rss', 'xml'), $result);
1367
 
1368
		$result = Router::parse('/posts.xml');
1369
		$this->assertEquals('xml', $result['ext']);
1370
 
1371
		$result = Router::setExtensions(array('pdf'), false);
1372
		$this->assertEquals(array('pdf'), $result);
1373
	}
1374
 
1375
/**
1376
 * testExtensionParsing method
1377
 *
1378
 * @return void
1379
 */
1380
	public function testExtensionParsing() {
1381
		Router::parseExtensions();
1382
		require CAKE . 'Config' . DS . 'routes.php';
1383
 
1384
		$result = Router::parse('/posts.rss');
1385
		$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'ext' => 'rss', 'pass' => array(), 'named' => array());
1386
		$this->assertEquals($expected, $result);
1387
 
1388
		$result = Router::parse('/posts/view/1.rss');
1389
		$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'pass' => array('1'), 'named' => array(), 'ext' => 'rss');
1390
		$this->assertEquals($expected, $result);
1391
 
1392
		$result = Router::parse('/posts/view/1.rss?query=test');
1393
		$expected['?'] = array('query' => 'test');
1394
		$this->assertEquals($expected, $result);
1395
 
1396
		$result = Router::parse('/posts/view/1.atom');
1397
		unset($expected['?']);
1398
		$expected['ext'] = 'atom';
1399
		$this->assertEquals($expected, $result);
1400
 
1401
		Router::reload();
1402
		require CAKE . 'Config' . DS . 'routes.php';
1403
 
1404
		Router::parseExtensions('rss', 'xml');
1405
 
1406
		$result = Router::parse('/posts.xml');
1407
		$expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'ext' => 'xml', 'pass' => array(), 'named' => array());
1408
		$this->assertEquals($expected, $result);
1409
 
1410
		$result = Router::parse('/posts.atom?hello=goodbye');
1411
		$expected = array('plugin' => null, 'controller' => 'posts.atom', 'action' => 'index', 'pass' => array(), 'named' => array(), '?' => array('hello' => 'goodbye'));
1412
		$this->assertEquals($expected, $result);
1413
 
1414
		Router::reload();
1415
		Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'ext' => 'rss'));
1416
		$result = Router::parse('/controller/action');
1417
		$expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'ext' => 'rss', 'named' => array(), 'pass' => array());
1418
		$this->assertEquals($expected, $result);
1419
 
1420
		Router::reload();
1421
		Router::parseExtensions('rss');
1422
		Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'ext' => 'rss'));
1423
		$result = Router::parse('/controller/action');
1424
		$expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'ext' => 'rss', 'named' => array(), 'pass' => array());
1425
		$this->assertEquals($expected, $result);
1426
	}
1427
 
1428
/**
1429
 * testQuerystringGeneration method
1430
 *
1431
 * @return void
1432
 */
1433
	public function testQuerystringGeneration() {
1434
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
1435
		$expected = '/posts/index/0?var=test&var2=test2';
1436
		$this->assertEquals($expected, $result);
1437
 
1438
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')));
1439
		$this->assertEquals($expected, $result);
1440
 
1441
		$expected .= '&more=test+data';
1442
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data')));
1443
		$this->assertEquals($expected, $result);
1444
 
1445
		// Test bug #4614
1446
		$restore = ini_get('arg_separator.output');
1447
		ini_set('arg_separator.output', '&amp;');
1448
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data')));
1449
		$this->assertEquals($expected, $result);
1450
		ini_set('arg_separator.output', $restore);
1451
 
1452
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')), array('escape' => true));
1453
		$expected = '/posts/index/0?var=test&amp;var2=test2';
1454
		$this->assertEquals($expected, $result);
1455
	}
1456
 
1457
/**
1458
 * testConnectNamed method
1459
 *
1460
 * @return void
1461
 */
1462
	public function testConnectNamed() {
1463
		$named = Router::connectNamed(false, array('default' => true));
1464
		$this->assertFalse($named['greedyNamed']);
1465
		$this->assertEquals(array_keys($named['rules']), $named['default']);
1466
 
1467
		Router::reload();
1468
		Router::connect('/foo/*', array('controller' => 'bar', 'action' => 'fubar'));
1469
		Router::connectNamed(array(), array('separator' => '='));
1470
		$result = Router::parse('/foo/param1=value1/param2=value2');
1471
		$expected = array('pass' => array(), 'named' => array('param1' => 'value1', 'param2' => 'value2'), 'controller' => 'bar', 'action' => 'fubar', 'plugin' => null);
1472
		$this->assertEquals($expected, $result);
1473
 
1474
		Router::reload();
1475
		Router::connect('/controller/action/*', array('controller' => 'controller', 'action' => 'action'), array('named' => array('param1' => 'value[\d]')));
1476
		Router::connectNamed(array(), array('greedy' => false, 'separator' => '='));
1477
		$result = Router::parse('/controller/action/param1=value1/param2=value2');
1478
		$expected = array('pass' => array('param2=value2'), 'named' => array('param1' => 'value1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
1479
		$this->assertEquals($expected, $result);
1480
 
1481
		Router::reload();
1482
		Router::connect('/:controller/:action/*');
1483
		Router::connectNamed(array('page'), array('default' => false, 'greedy' => false));
1484
		$result = Router::parse('/categories/index/limit=5');
1485
		$this->assertTrue(empty($result['named']));
1486
	}
1487
 
1488
/**
1489
 * testNamedArgsUrlGeneration method
1490
 *
1491
 * @return void
1492
 */
1493
	public function testNamedArgsUrlGeneration() {
1494
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'published' => 1, 'deleted' => 1));
1495
		$expected = '/posts/index/published:1/deleted:1';
1496
		$this->assertEquals($expected, $result);
1497
 
1498
		$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'published' => 0, 'deleted' => 0));
1499
		$expected = '/posts/index/published:0/deleted:0';
1500
		$this->assertEquals($expected, $result);
1501
 
1502
		Router::reload();
1503
		extract(Router::getNamedExpressions());
1504
		Router::connectNamed(array('file' => '[\w\.\-]+\.(html|png)'));
1505
		Router::connect('/', array('controller' => 'graphs', 'action' => 'index'));
1506
		Router::connect('/:id/*', array('controller' => 'graphs', 'action' => 'view'), array('id' => $ID));
1507
 
1508
		$result = Router::url(array('controller' => 'graphs', 'action' => 'view', 'id' => 12, 'file' => 'asdf.png'));
1509
		$expected = '/12/file:asdf.png';
1510
		$this->assertEquals($expected, $result);
1511
 
1512
		$result = Router::url(array('controller' => 'graphs', 'action' => 'view', 12, 'file' => 'asdf.foo'));
1513
		$expected = '/graphs/view/12/file:asdf.foo';
1514
		$this->assertEquals($expected, $result);
1515
 
1516
		Configure::write('Routing.prefixes', array('admin'));
1517
 
1518
		Router::reload();
1519
		$request = new CakeRequest();
1520
		Router::setRequestInfo(
1521
			$request->addParams(array(
1522
				'admin' => true, 'controller' => 'controller', 'action' => 'index', 'plugin' => null
1523
			))->addPaths(array(
1524
				'base' => '/',
1525
				'here' => '/',
1526
				'webroot' => '/base/',
1527
			))
1528
		);
1529
		Router::parse('/');
1530
 
1531
		$result = Router::url(array('page' => 1, 0 => null, 'sort' => 'controller', 'direction' => 'asc', 'order' => null));
1532
		$expected = "/admin/controller/index/page:1/sort:controller/direction:asc";
1533
		$this->assertEquals($expected, $result);
1534
 
1535
		Router::reload();
1536
		$request = new CakeRequest('admin/controller/index');
1537
		$request->addParams(array(
1538
			'admin' => true, 'controller' => 'controller', 'action' => 'index', 'plugin' => null
1539
		));
1540
		$request->base = '/';
1541
		Router::setRequestInfo($request);
1542
 
1543
		$result = Router::parse('/admin/controller/index/type:whatever');
1544
		$result = Router::url(array('type' => 'new'));
1545
		$expected = "/admin/controller/index/type:new";
1546
		$this->assertEquals($expected, $result);
1547
	}
1548
 
1549
/**
1550
 * testNamedArgsUrlParsing method
1551
 *
1552
 * @return void
1553
 */
1554
	public function testNamedArgsUrlParsing() {
1555
		Router::reload();
1556
		require CAKE . 'Config' . DS . 'routes.php';
1557
		$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
1558
		$expected = array('pass' => array(), 'named' => array('param1' => 'value1:1', 'param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
1559
		$this->assertEquals($expected, $result);
1560
 
1561
		Router::reload();
1562
		require CAKE . 'Config' . DS . 'routes.php';
1563
		$result = Router::connectNamed(false);
1564
		$this->assertEquals(array(), array_keys($result['rules']));
1565
		$this->assertFalse($result['greedyNamed']);
1566
		$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
1567
		$expected = array('pass' => array('param1:value1:1', 'param2:value2:3', 'param:value'), 'named' => array(), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
1568
		$this->assertEquals($expected, $result);
1569
 
1570
		Router::reload();
1571
		require CAKE . 'Config' . DS . 'routes.php';
1572
		$result = Router::connectNamed(true);
1573
		$named = Router::namedConfig();
1574
		$this->assertEquals($named['default'], array_keys($result['rules']));
1575
		$this->assertTrue($result['greedyNamed']);
1576
 
1577
		Router::reload();
1578
		require CAKE . 'Config' . DS . 'routes.php';
1579
		Router::connectNamed(array('param1' => 'not-matching'));
1580
		$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
1581
		$expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
1582
		$this->assertEquals($expected, $result);
1583
 
1584
		$result = Router::parse('/foo/view/param1:value1:1/param2:value2:3/param:value');
1585
		$expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'foo', 'action' => 'view', 'plugin' => null);
1586
		$this->assertEquals($expected, $result);
1587
 
1588
		Router::reload();
1589
		require CAKE . 'Config' . DS . 'routes.php';
1590
		Router::connectNamed(array('param1' => '[\d]', 'param2' => '[a-z]', 'param3' => '[\d]'));
1591
		$result = Router::parse('/controller/action/param1:1/param2:2/param3:3');
1592
		$expected = array('pass' => array('param2:2'), 'named' => array('param1' => '1', 'param3' => '3'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
1593
		$this->assertEquals($expected, $result);
1594
 
1595
		Router::reload();
1596
		require CAKE . 'Config' . DS . 'routes.php';
1597
		Router::connectNamed(array('param1' => '[\d]', 'param2' => true, 'param3' => '[\d]'));
1598
		$result = Router::parse('/controller/action/param1:1/param2:2/param3:3');
1599
		$expected = array('pass' => array(), 'named' => array('param1' => '1', 'param2' => '2', 'param3' => '3'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
1600
		$this->assertEquals($expected, $result);
1601
 
1602
		Router::reload();
1603
		require CAKE . 'Config' . DS . 'routes.php';
1604
		Router::connectNamed(array('param1' => 'value[\d]+:[\d]+'), array('greedy' => false));
1605
		$result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param3:value');
1606
		$expected = array('pass' => array('param2:value2:3', 'param3:value'), 'named' => array('param1' => 'value1:1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
1607
		$this->assertEquals($expected, $result);
1608
	}
1609
 
1610
/**
1611
 * Test URL generation with legacy (1.2) style prefix routes.
1612
 *
1613
 * @return void
1614
 * @see testUrlGenerationWithAutoPrefixes
1615
 */
1616
	public function testUrlGenerationWithLegacyPrefixes() {
1617
		Router::reload();
1618
		Router::connect('/protected/:controller/:action/*', array(
1619
			'prefix' => 'protected',
1620
			'protected' => true
1621
		));
1622
		Router::parse('/');
1623
 
1624
		$request = new CakeRequest();
1625
		Router::setRequestInfo(
1626
			$request->addParams(array(
1627
				'plugin' => null, 'controller' => 'images', 'action' => 'index',
1628
				'prefix' => null, 'admin' => false, 'url' => array('url' => 'images/index')
1629
			))->addPaths(array(
1630
				'base' => '',
1631
				'here' => '/images/index',
1632
				'webroot' => '/',
1633
			))
1634
		);
1635
 
1636
		$result = Router::url(array('protected' => true));
1637
		$expected = '/protected/images/index';
1638
		$this->assertEquals($expected, $result);
1639
 
1640
		$result = Router::url(array('controller' => 'images', 'action' => 'add'));
1641
		$expected = '/images/add';
1642
		$this->assertEquals($expected, $result);
1643
 
1644
		$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true));
1645
		$expected = '/protected/images/add';
1646
		$this->assertEquals($expected, $result);
1647
 
1648
		$result = Router::url(array('action' => 'edit', 1));
1649
		$expected = '/images/edit/1';
1650
		$this->assertEquals($expected, $result);
1651
 
1652
		$result = Router::url(array('action' => 'edit', 1, 'protected' => true));
1653
		$expected = '/protected/images/edit/1';
1654
		$this->assertEquals($expected, $result);
1655
 
1656
		$result = Router::url(array('action' => 'protected_edit', 1, 'protected' => true));
1657
		$expected = '/protected/images/edit/1';
1658
		$this->assertEquals($expected, $result);
1659
 
1660
		$result = Router::url(array('action' => 'edit', 1, 'protected' => true));
1661
		$expected = '/protected/images/edit/1';
1662
		$this->assertEquals($expected, $result);
1663
 
1664
		$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1));
1665
		$expected = '/others/edit/1';
1666
		$this->assertEquals($expected, $result);
1667
 
1668
		$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true));
1669
		$expected = '/protected/others/edit/1';
1670
		$this->assertEquals($expected, $result);
1671
 
1672
		$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'page' => 1));
1673
		$expected = '/protected/others/edit/1/page:1';
1674
		$this->assertEquals($expected, $result);
1675
 
1676
		Router::connectNamed(array('random'));
1677
		$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'random' => 'my-value'));
1678
		$expected = '/protected/others/edit/1/random:my-value';
1679
		$this->assertEquals($expected, $result);
1680
	}
1681
 
1682
/**
1683
 * test newer style automatically generated prefix routes.
1684
 *
1685
 * @return void
1686
 */
1687
	public function testUrlGenerationWithAutoPrefixes() {
1688
		Configure::write('Routing.prefixes', array('protected'));
1689
		Router::reload();
1690
		Router::parse('/');
1691
 
1692
		$request = new CakeRequest();
1693
		Router::setRequestInfo(
1694
			$request->addParams(array(
1695
				'plugin' => null, 'controller' => 'images', 'action' => 'index',
1696
				'prefix' => null, 'protected' => false, 'url' => array('url' => 'images/index')
1697
			))->addPaths(array(
1698
				'base' => '',
1699
				'here' => '/images/index',
1700
				'webroot' => '/',
1701
			))
1702
		);
1703
 
1704
		$result = Router::url(array('controller' => 'images', 'action' => 'add'));
1705
		$expected = '/images/add';
1706
		$this->assertEquals($expected, $result);
1707
 
1708
		$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true));
1709
		$expected = '/protected/images/add';
1710
		$this->assertEquals($expected, $result);
1711
 
1712
		$result = Router::url(array('controller' => 'images', 'action' => 'add_protected_test', 'protected' => true));
1713
		$expected = '/protected/images/add_protected_test';
1714
		$this->assertEquals($expected, $result);
1715
 
1716
		$result = Router::url(array('action' => 'edit', 1));
1717
		$expected = '/images/edit/1';
1718
		$this->assertEquals($expected, $result);
1719
 
1720
		$result = Router::url(array('action' => 'edit', 1, 'protected' => true));
1721
		$expected = '/protected/images/edit/1';
1722
		$this->assertEquals($expected, $result);
1723
 
1724
		$result = Router::url(array('action' => 'protected_edit', 1, 'protected' => true));
1725
		$expected = '/protected/images/edit/1';
1726
		$this->assertEquals($expected, $result);
1727
 
1728
		$result = Router::url(array('action' => 'protectededit', 1, 'protected' => true));
1729
		$expected = '/protected/images/protectededit/1';
1730
		$this->assertEquals($expected, $result);
1731
 
1732
		$result = Router::url(array('action' => 'edit', 1, 'protected' => true));
1733
		$expected = '/protected/images/edit/1';
1734
		$this->assertEquals($expected, $result);
1735
 
1736
		$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1));
1737
		$expected = '/others/edit/1';
1738
		$this->assertEquals($expected, $result);
1739
 
1740
		$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true));
1741
		$expected = '/protected/others/edit/1';
1742
		$this->assertEquals($expected, $result);
1743
 
1744
		$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'page' => 1));
1745
		$expected = '/protected/others/edit/1/page:1';
1746
		$this->assertEquals($expected, $result);
1747
 
1748
		Router::connectNamed(array('random'));
1749
		$result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'random' => 'my-value'));
1750
		$expected = '/protected/others/edit/1/random:my-value';
1751
		$this->assertEquals($expected, $result);
1752
	}
1753
 
1754
/**
1755
 * test that auto-generated prefix routes persist
1756
 *
1757
 * @return void
1758
 */
1759
	public function testAutoPrefixRoutePersistence() {
1760
		Configure::write('Routing.prefixes', array('protected'));
1761
		Router::reload();
1762
		Router::parse('/');
1763
 
1764
		$request = new CakeRequest();
1765
		Router::setRequestInfo(
1766
			$request->addParams(array(
1767
				'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'protected',
1768
				'protected' => true, 'url' => array('url' => 'protected/images/index')
1769
			))->addPaths(array(
1770
				'base' => '',
1771
				'here' => '/protected/images/index',
1772
				'webroot' => '/',
1773
			))
1774
		);
1775
 
1776
		$result = Router::url(array('controller' => 'images', 'action' => 'add'));
1777
		$expected = '/protected/images/add';
1778
		$this->assertEquals($expected, $result);
1779
 
1780
		$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => false));
1781
		$expected = '/images/add';
1782
		$this->assertEquals($expected, $result);
1783
	}
1784
 
1785
/**
1786
 * test that setting a prefix override the current one
1787
 *
1788
 * @return void
1789
 */
1790
	public function testPrefixOverride() {
1791
		Configure::write('Routing.prefixes', array('protected', 'admin'));
1792
		Router::reload();
1793
		Router::parse('/');
1794
 
1795
		$request = new CakeRequest();
1796
		Router::setRequestInfo(
1797
			$request->addParams(array(
1798
				'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'protected',
1799
				'protected' => true, 'url' => array('url' => 'protected/images/index')
1800
			))->addPaths(array(
1801
				'base' => '',
1802
				'here' => '/protected/images/index',
1803
				'webroot' => '/',
1804
			))
1805
		);
1806
 
1807
		$result = Router::url(array('controller' => 'images', 'action' => 'add', 'admin' => true));
1808
		$expected = '/admin/images/add';
1809
		$this->assertEquals($expected, $result);
1810
 
1811
		$request = new CakeRequest();
1812
		Router::setRequestInfo(
1813
			$request->addParams(array(
1814
				'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'admin',
1815
				'admin' => true, 'url' => array('url' => 'admin/images/index')
1816
			))->addPaths(array(
1817
				'base' => '',
1818
				'here' => '/admin/images/index',
1819
				'webroot' => '/',
1820
			))
1821
		);
1822
		$result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true));
1823
		$expected = '/protected/images/add';
1824
		$this->assertEquals($expected, $result);
1825
	}
1826
 
1827
/**
1828
 * Test that setting a prefix to false is ignored, as its generally user error.
1829
 *
1830
 * @return void
1831
 */
1832
	public function testPrefixFalseIgnored() {
1833
		Configure::write('Routing.prefixes', array('admin'));
1834
		Router::reload();
1835
 
1836
		Router::connect('/cache_css/*', array('admin' => false, 'controller' => 'asset_compress', 'action' => 'get'));
1837
 
1838
		$url = Router::url(array('controller' => 'asset_compress', 'action' => 'get', 'test'));
1839
		$expected = '/cache_css/test';
1840
		$this->assertEquals($expected, $url);
1841
 
1842
		$url = Router::url(array('admin' => false, 'controller' => 'asset_compress', 'action' => 'get', 'test'));
1843
		$expected = '/cache_css/test';
1844
		$this->assertEquals($expected, $url);
1845
 
1846
		$url = Router::url(array('admin' => true, 'controller' => 'asset_compress', 'action' => 'get', 'test'));
1847
		$this->assertEquals('/admin/asset_compress/get/test', $url);
1848
	}
1849
 
1850
/**
1851
 * testRemoveBase method
1852
 *
1853
 * @return void
1854
 */
1855
	public function testRemoveBase() {
1856
		$request = new CakeRequest();
1857
		Router::setRequestInfo(
1858
			$request->addParams(array(
1859
				'plugin' => null, 'controller' => 'controller', 'action' => 'index',
1860
				'bare' => 0, 'url' => array('url' => 'protected/images/index')
1861
			))->addPaths(array(
1862
				'base' => '/base',
1863
				'here' => '/',
1864
				'webroot' => '/base/',
1865
			))
1866
		);
1867
 
1868
		$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action'));
1869
		$expected = '/base/my_controller/my_action';
1870
		$this->assertEquals($expected, $result);
1871
 
1872
		$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => false));
1873
		$expected = '/my_controller/my_action';
1874
		$this->assertEquals($expected, $result);
1875
 
1876
		$result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => true));
1877
		$expected = '/base/my_controller/my_action/base:1';
1878
		$this->assertEquals($expected, $result);
1879
	}
1880
 
1881
/**
1882
 * testPagesUrlParsing method
1883
 *
1884
 * @return void
1885
 */
1886
	public function testPagesUrlParsing() {
1887
		Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
1888
		Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
1889
 
1890
		$result = Router::parse('/');
1891
		$expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
1892
		$this->assertEquals($expected, $result);
1893
 
1894
		$result = Router::parse('/pages/home/');
1895
		$expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
1896
		$this->assertEquals($expected, $result);
1897
 
1898
		Router::reload();
1899
		require CAKE . 'Config' . DS . 'routes.php';
1900
		Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
1901
 
1902
		$result = Router::parse('/');
1903
		$expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
1904
		$this->assertEquals($expected, $result);
1905
 
1906
		$result = Router::parse('/pages/display/home/event:value');
1907
		$expected = array('pass' => array('home'), 'named' => array('event' => 'value'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
1908
		$this->assertEquals($expected, $result);
1909
 
1910
		$result = Router::parse('/pages/display/home/event:Val_u2');
1911
		$expected = array('pass' => array('home'), 'named' => array('event' => 'Val_u2'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
1912
		$this->assertEquals($expected, $result);
1913
 
1914
		$result = Router::parse('/pages/display/home/event:val-ue');
1915
		$expected = array('pass' => array('home'), 'named' => array('event' => 'val-ue'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
1916
		$this->assertEquals($expected, $result);
1917
 
1918
		Router::reload();
1919
		Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
1920
		Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
1921
		$result = Router::parse('/pages/contact/');
1922
 
1923
		$expected = array('pass' => array('contact'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
1924
		$this->assertEquals($expected, $result);
1925
	}
1926
 
1927
/**
1928
 * test that requests with a trailing dot don't loose the do.
1929
 *
1930
 * @return void
1931
 */
1932
	public function testParsingWithTrailingPeriod() {
1933
		Router::reload();
1934
		Router::connect('/:controller/:action/*');
1935
		$result = Router::parse('/posts/view/something.');
1936
		$this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
1937
 
1938
		$result = Router::parse('/posts/view/something. . .');
1939
		$this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s');
1940
	}
1941
 
1942
/**
1943
 * test that requests with a trailing dot don't loose the do.
1944
 *
1945
 * @return void
1946
 */
1947
	public function testParsingWithTrailingPeriodAndParseExtensions() {
1948
		Router::reload();
1949
		Router::connect('/:controller/:action/*');
1950
		Router::parseExtensions('json');
1951
 
1952
		$result = Router::parse('/posts/view/something.');
1953
		$this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
1954
 
1955
		$result = Router::parse('/posts/view/something. . .');
1956
		$this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s');
1957
	}
1958
 
1959
/**
1960
 * test that patterns work for :action
1961
 *
1962
 * @return void
1963
 */
1964
	public function testParsingWithPatternOnAction() {
1965
		Router::reload();
1966
		Router::connect(
1967
			'/blog/:action/*',
1968
			array('controller' => 'blog_posts'),
1969
			array('action' => 'other|actions')
1970
		);
1971
		$result = Router::parse('/blog/other');
1972
		$expected = array(
1973
			'plugin' => null,
1974
			'controller' => 'blog_posts',
1975
			'action' => 'other',
1976
			'pass' => array(),
1977
			'named' => array()
1978
		);
1979
		$this->assertEquals($expected, $result);
1980
 
1981
		$result = Router::parse('/blog/foobar');
1982
		$this->assertSame(array(), $result);
1983
 
1984
		$result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo'));
1985
		$this->assertEquals('/blog_posts/foo', $result);
1986
 
1987
		$result = Router::url(array('controller' => 'blog_posts', 'action' => 'actions'));
1988
		$this->assertEquals('/blog/actions', $result);
1989
	}
1990
 
1991
/**
1992
 * testParsingWithPrefixes method
1993
 *
1994
 * @return void
1995
 */
1996
	public function testParsingWithPrefixes() {
1997
		$adminParams = array('prefix' => 'admin', 'admin' => true);
1998
		Router::connect('/admin/:controller', $adminParams);
1999
		Router::connect('/admin/:controller/:action', $adminParams);
2000
		Router::connect('/admin/:controller/:action/*', $adminParams);
2001
 
2002
		$request = new CakeRequest();
2003
		Router::setRequestInfo(
2004
			$request->addParams(array(
2005
				'plugin' => null, 'controller' => 'controller', 'action' => 'index'
2006
			))->addPaths(array(
2007
				'base' => '/base',
2008
				'here' => '/',
2009
				'webroot' => '/base/',
2010
			))
2011
		);
2012
 
2013
		$result = Router::parse('/admin/posts/');
2014
		$expected = array('pass' => array(), 'named' => array(), 'prefix' => 'admin', 'plugin' => null, 'controller' => 'posts', 'action' => 'admin_index', 'admin' => true);
2015
		$this->assertEquals($expected, $result);
2016
 
2017
		$result = Router::parse('/admin/posts');
2018
		$this->assertEquals($expected, $result);
2019
 
2020
		$result = Router::url(array('admin' => true, 'controller' => 'posts'));
2021
		$expected = '/base/admin/posts';
2022
		$this->assertEquals($expected, $result);
2023
 
2024
		$result = Router::prefixes();
2025
		$expected = array('admin');
2026
		$this->assertEquals($expected, $result);
2027
 
2028
		Router::reload();
2029
 
2030
		$prefixParams = array('prefix' => 'members', 'members' => true);
2031
		Router::connect('/members/:controller', $prefixParams);
2032
		Router::connect('/members/:controller/:action', $prefixParams);
2033
		Router::connect('/members/:controller/:action/*', $prefixParams);
2034
 
2035
		$request = new CakeRequest();
2036
		Router::setRequestInfo(
2037
			$request->addParams(array(
2038
				'plugin' => null, 'controller' => 'controller', 'action' => 'index',
2039
				'bare' => 0
2040
			))->addPaths(array(
2041
				'base' => '/base',
2042
				'here' => '/',
2043
				'webroot' => '/',
2044
			))
2045
		);
2046
 
2047
		$result = Router::parse('/members/posts/index');
2048
		$expected = array('pass' => array(), 'named' => array(), 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'members_index', 'members' => true);
2049
		$this->assertEquals($expected, $result);
2050
 
2051
		$result = Router::url(array('members' => true, 'controller' => 'posts', 'action' => 'index', 'page' => 2));
2052
		$expected = '/base/members/posts/index/page:2';
2053
		$this->assertEquals($expected, $result);
2054
 
2055
		$result = Router::url(array('members' => true, 'controller' => 'users', 'action' => 'add'));
2056
		$expected = '/base/members/users/add';
2057
		$this->assertEquals($expected, $result);
2058
	}
2059
 
2060
/**
2061
 * Tests URL generation with flags and prefixes in and out of context
2062
 *
2063
 * @return void
2064
 */
2065
	public function testUrlWritingWithPrefixes() {
2066
		Router::connect('/company/:controller/:action/*', array('prefix' => 'company', 'company' => true));
2067
		Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
2068
 
2069
		$result = Router::url(array('controller' => 'users', 'action' => 'login', 'company' => true));
2070
		$expected = '/company/users/login';
2071
		$this->assertEquals($expected, $result);
2072
 
2073
		$result = Router::url(array('controller' => 'users', 'action' => 'company_login', 'company' => true));
2074
		$expected = '/company/users/login';
2075
		$this->assertEquals($expected, $result);
2076
 
2077
		$request = new CakeRequest();
2078
		Router::setRequestInfo(
2079
			$request->addParams(array(
2080
				'plugin' => null, 'controller' => 'users', 'action' => 'login',
2081
				'company' => true
2082
			))->addPaths(array(
2083
				'base' => '/',
2084
				'here' => '/',
2085
				'webroot' => '/base/',
2086
			))
2087
		);
2088
 
2089
		$result = Router::url(array('controller' => 'users', 'action' => 'login', 'company' => false));
2090
		$expected = '/login';
2091
		$this->assertEquals($expected, $result);
2092
	}
2093
 
2094
/**
2095
 * test url generation with prefixes and custom routes
2096
 *
2097
 * @return void
2098
 */
2099
	public function testUrlWritingWithPrefixesAndCustomRoutes() {
2100
		Router::connect(
2101
			'/admin/login',
2102
			array('controller' => 'users', 'action' => 'login', 'prefix' => 'admin', 'admin' => true)
2103
		);
2104
		$request = new CakeRequest();
2105
		Router::setRequestInfo(
2106
			$request->addParams(array(
2107
				'plugin' => null, 'controller' => 'posts', 'action' => 'index',
2108
				'admin' => true, 'prefix' => 'admin'
2109
			))->addPaths(array(
2110
				'base' => '/',
2111
				'here' => '/',
2112
				'webroot' => '/',
2113
			))
2114
		);
2115
		$result = Router::url(array('controller' => 'users', 'action' => 'login', 'admin' => true));
2116
		$this->assertEquals('/admin/login', $result);
2117
 
2118
		$result = Router::url(array('controller' => 'users', 'action' => 'login'));
2119
		$this->assertEquals('/admin/login', $result);
2120
 
2121
		$result = Router::url(array('controller' => 'users', 'action' => 'admin_login'));
2122
		$this->assertEquals('/admin/login', $result);
2123
	}
2124
 
2125
/**
2126
 * testPassedArgsOrder method
2127
 *
2128
 * @return void
2129
 */
2130
	public function testPassedArgsOrder() {
2131
		Router::connect('/test-passed/*', array('controller' => 'pages', 'action' => 'display', 'home'));
2132
		Router::connect('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
2133
		Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 1));
2134
		Router::parse('/');
2135
 
2136
		$result = Router::url(array('controller' => 'pages', 'action' => 'display', 1, 'whatever'));
2137
		$expected = '/test/whatever';
2138
		$this->assertEquals($expected, $result);
2139
 
2140
		$result = Router::url(array('controller' => 'pages', 'action' => 'display', 2, 'whatever'));
2141
		$expected = '/test2/whatever';
2142
		$this->assertEquals($expected, $result);
2143
 
2144
		$result = Router::url(array('controller' => 'pages', 'action' => 'display', 'home', 'whatever'));
2145
		$expected = '/test-passed/whatever';
2146
		$this->assertEquals($expected, $result);
2147
 
2148
		Configure::write('Routing.prefixes', array('admin'));
2149
		Router::reload();
2150
 
2151
		$request = new CakeRequest();
2152
		Router::setRequestInfo(
2153
			$request->addParams(array(
2154
				'plugin' => null, 'controller' => 'images', 'action' => 'index',
2155
				'url' => array('url' => 'protected/images/index')
2156
			))->addPaths(array(
2157
				'base' => '',
2158
				'here' => '/protected/images/index',
2159
				'webroot' => '/',
2160
			))
2161
		);
2162
 
2163
		Router::connect('/protected/:controller/:action/*', array(
2164
			'controller' => 'users',
2165
			'action' => 'index',
2166
			'prefix' => 'protected'
2167
		));
2168
 
2169
		Router::parse('/');
2170
		$result = Router::url(array('controller' => 'images', 'action' => 'add'));
2171
		$expected = '/protected/images/add';
2172
		$this->assertEquals($expected, $result);
2173
 
2174
		$result = Router::prefixes();
2175
		$expected = array('admin', 'protected');
2176
		$this->assertEquals($expected, $result);
2177
	}
2178
 
2179
/**
2180
 * testRegexRouteMatching method
2181
 *
2182
 * @return void
2183
 */
2184
	public function testRegexRouteMatching() {
2185
		Router::connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng'));
2186
 
2187
		$result = Router::parse('/eng/test/test_action');
2188
		$expected = array('pass' => array(), 'named' => array(), 'locale' => 'eng', 'controller' => 'test', 'action' => 'test_action', 'plugin' => null);
2189
		$this->assertEquals($expected, $result);
2190
 
2191
		$result = Router::parse('/badness/test/test_action');
2192
		$this->assertSame(array(), $result);
2193
 
2194
		Router::reload();
2195
		Router::connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng'));
2196
 
2197
		$request = new CakeRequest();
2198
		Router::setRequestInfo(
2199
			$request->addParams(array(
2200
				'plugin' => null, 'controller' => 'test', 'action' => 'index',
2201
				'url' => array('url' => 'test/test_action')
2202
			))->addPaths(array(
2203
				'base' => '',
2204
				'here' => '/test/test_action',
2205
				'webroot' => '/',
2206
			))
2207
		);
2208
 
2209
		$result = Router::url(array('action' => 'test_another_action'));
2210
		$expected = '/test/test_another_action';
2211
		$this->assertEquals($expected, $result);
2212
 
2213
		$result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng'));
2214
		$expected = '/eng/test/test_another_action';
2215
		$this->assertEquals($expected, $result);
2216
 
2217
		$result = Router::url(array('action' => 'test_another_action', 'locale' => 'badness'));
2218
		$expected = '/test/test_another_action/locale:badness';
2219
		$this->assertEquals($expected, $result);
2220
	}
2221
 
2222
/**
2223
 * testStripPlugin
2224
 *
2225
 * @return void
2226
 */
2227
	public function testStripPlugin() {
2228
		$pluginName = 'forums';
2229
		$url = 'example.com/' . $pluginName . '/';
2230
		$expected = 'example.com';
2231
 
2232
		$this->assertEquals($expected, Router::stripPlugin($url, $pluginName));
2233
		$this->assertEquals(Router::stripPlugin($url), $url);
2234
		$this->assertEquals(Router::stripPlugin($url, null), $url);
2235
	}
2236
 
2237
/**
2238
 * testCurrentRouteWhenNonExistentRoute
2239
 *
2240
 * @return void
2241
 */
2242
	public function testCurrentRouteWhenNonExistentRoute() {
2243
		$route = Router::currentRoute();
2244
		$this->assertFalse($route);
2245
	}
2246
 
2247
/**
2248
 * testCurrentRoute
2249
 *
2250
 * This test needs some improvement and actual requestAction() usage
2251
 *
2252
 * @return void
2253
 */
2254
	public function testCurrentRoute() {
2255
		$url = array('controller' => 'pages', 'action' => 'display', 'government');
2256
		Router::connect('/government', $url);
2257
		Router::parse('/government');
2258
		$route = Router::currentRoute();
2259
		$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
2260
	}
2261
 
2262
/**
2263
 * testRequestRoute
2264
 *
2265
 * @return void
2266
 */
2267
	public function testRequestRoute() {
2268
		$url = array('controller' => 'products', 'action' => 'display', 5);
2269
		Router::connect('/government', $url);
2270
		Router::parse('/government');
2271
		$route = Router::requestRoute();
2272
		$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
2273
 
2274
		// test that the first route is matched
2275
		$newUrl = array('controller' => 'products', 'action' => 'display', 6);
2276
		Router::connect('/government', $url);
2277
		Router::parse('/government');
2278
		$route = Router::requestRoute();
2279
		$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
2280
 
2281
		// test that an unmatched route does not change the current route
2282
		$newUrl = array('controller' => 'products', 'action' => 'display', 6);
2283
		Router::connect('/actor', $url);
2284
		Router::parse('/government');
2285
		$route = Router::requestRoute();
2286
		$this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
2287
	}
2288
 
2289
/**
2290
 * testGetParams
2291
 *
2292
 * @return void
2293
 */
2294
	public function testGetParams() {
2295
		$paths = array('base' => '/', 'here' => '/products/display/5', 'webroot' => '/webroot');
2296
		$params = array('param1' => '1', 'param2' => '2');
2297
		Router::setRequestInfo(array($params, $paths));
2298
 
2299
		$expected = array(
2300
			'plugin' => null, 'controller' => false, 'action' => false,
2301
			'named' => array(), 'pass' => array(),
2302
			'param1' => '1', 'param2' => '2',
2303
		);
2304
		$this->assertEquals(Router::getParams(), $expected);
2305
		$this->assertEquals(Router::getParam('controller'), false);
2306
		$this->assertEquals(Router::getParam('param1'), '1');
2307
		$this->assertEquals(Router::getParam('param2'), '2');
2308
 
2309
		Router::reload();
2310
 
2311
		$params = array('controller' => 'pages', 'action' => 'display');
2312
		Router::setRequestInfo(array($params, $paths));
2313
		$expected = array(
2314
			'plugin' => null, 'controller' => 'pages', 'action' => 'display',
2315
			'named' => array(), 'pass' => array(),
2316
		);
2317
		$this->assertEquals(Router::getParams(), $expected);
2318
		$this->assertEquals(Router::getParams(true), $expected);
2319
	}
2320
 
2321
/**
2322
 * test that connectDefaults() can disable default route connection
2323
 *
2324
 * @return void
2325
 */
2326
	public function testDefaultsMethod() {
2327
		Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 2));
2328
		$result = Router::parse('/posts/edit/5');
2329
		$this->assertFalse(isset($result['controller']));
2330
		$this->assertFalse(isset($result['action']));
2331
	}
2332
 
2333
/**
2334
 * test that the required default routes are connected.
2335
 *
2336
 * @return void
2337
 */
2338
	public function testConnectDefaultRoutes() {
2339
		App::build(array(
2340
			'plugins' => array(
2341
				CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
2342
			)
2343
		), App::RESET);
2344
		CakePlugin::load(array('TestPlugin', 'PluginJs'));
2345
		Router::reload();
2346
		require CAKE . 'Config' . DS . 'routes.php';
2347
 
2348
		$result = Router::url(array('plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index'));
2349
		$this->assertEquals('/plugin_js/js_file', $result);
2350
 
2351
		$result = Router::parse('/plugin_js/js_file');
2352
		$expected = array(
2353
			'plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index',
2354
			'named' => array(), 'pass' => array()
2355
		);
2356
		$this->assertEquals($expected, $result);
2357
 
2358
		$result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index'));
2359
		$this->assertEquals('/test_plugin', $result);
2360
 
2361
		$result = Router::parse('/test_plugin');
2362
		$expected = array(
2363
			'plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index',
2364
			'named' => array(), 'pass' => array()
2365
		);
2366
 
2367
		$this->assertEquals($expected, $result, 'Plugin shortcut route broken. %s');
2368
	}
2369
 
2370
/**
2371
 * test using a custom route class for route connection
2372
 *
2373
 * @return void
2374
 */
2375
	public function testUsingCustomRouteClass() {
2376
		$this->getMock('CakeRoute', array(), array(), 'MockConnectedRoute', false);
2377
		$routes = Router::connect(
2378
			'/:slug',
2379
			array('controller' => 'posts', 'action' => 'view'),
2380
			array('routeClass' => 'MockConnectedRoute', 'slug' => '[a-z_-]+')
2381
		);
2382
		$this->assertInstanceOf('MockConnectedRoute', $routes[0], 'Incorrect class used. %s');
2383
		$expected = array('controller' => 'posts', 'action' => 'view', 'slug' => 'test');
2384
		$routes[0]->expects($this->any())
2385
			->method('parse')
2386
			->will($this->returnValue($expected));
2387
		$result = Router::parse('/test');
2388
		$this->assertEquals($expected, $result);
2389
	}
2390
 
2391
/**
2392
 * test using custom route class in PluginDot notation
2393
 */
2394
	public function testUsingCustomRouteClassPluginDotSyntax() {
2395
		App::build(array(
2396
			'Plugin' => array(
2397
				CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
2398
			)
2399
		));
2400
		CakePlugin::load('TestPlugin');
2401
		App::uses('TestRoute', 'TestPlugin.Routing/Route');
2402
		$routes = Router::connect(
2403
			'/:slug',
2404
			array('controller' => 'posts', 'action' => 'view'),
2405
			array('routeClass' => 'TestPlugin.TestRoute', 'slug' => '[a-z_-]+')
2406
		);
2407
		$this->assertInstanceOf('TestRoute', $routes[0]);
2408
		CakePlugin::unload('TestPlugin');
2409
	}
2410
 
2411
/**
2412
 * test that route classes must extend CakeRoute
2413
 *
2414
 * @expectedException RouterException
2415
 * @return void
2416
 */
2417
	public function testCustomRouteException() {
2418
		Router::connect('/:controller', array(), array('routeClass' => 'Object'));
2419
	}
2420
 
2421
/**
2422
 * test reversing parameter arrays back into strings.
2423
 *
2424
 * Mark the router as initialized so it doesn't auto-load routes
2425
 *
2426
 * @return void
2427
 */
2428
	public function testRouterReverse() {
2429
		Router::$initialized = true;
2430
 
2431
		$params = array(
2432
			'controller' => 'posts',
2433
			'action' => 'view',
2434
			'pass' => array(1),
2435
			'named' => array(),
2436
			'url' => array(),
2437
			'autoRender' => 1,
2438
			'bare' => 1,
2439
			'return' => 1,
2440
			'requested' => 1,
2441
			'_Token' => array('key' => 'sekret')
2442
		);
2443
		$result = Router::reverse($params);
2444
		$this->assertEquals('/posts/view/1', $result);
2445
 
2446
		$params = array(
2447
			'controller' => 'posts',
2448
			'action' => 'index',
2449
			'pass' => array(1),
2450
			'named' => array('page' => 1, 'sort' => 'Article.title', 'direction' => 'desc'),
2451
			'url' => array()
2452
		);
2453
		$result = Router::reverse($params);
2454
		$this->assertEquals('/posts/index/1/page:1/sort:Article.title/direction:desc', $result);
2455
 
2456
		Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
2457
		$params = array(
2458
			'lang' => 'eng',
2459
			'controller' => 'posts',
2460
			'action' => 'view',
2461
			'pass' => array(1),
2462
			'named' => array(),
2463
			'url' => array('url' => 'eng/posts/view/1')
2464
		);
2465
		$result = Router::reverse($params);
2466
		$this->assertEquals('/eng/posts/view/1', $result);
2467
 
2468
		$params = array(
2469
			'lang' => 'eng',
2470
			'controller' => 'posts',
2471
			'action' => 'view',
2472
			'pass' => array(1),
2473
			'named' => array(),
2474
			'url' => array('url' => 'eng/posts/view/1', 'foo' => 'bar', 'baz' => 'quu'),
2475
			'paging' => array(),
2476
			'models' => array()
2477
		);
2478
		$result = Router::reverse($params);
2479
		$this->assertEquals('/eng/posts/view/1?foo=bar&baz=quu', $result);
2480
 
2481
		$request = new CakeRequest('/eng/posts/view/1');
2482
		$request->addParams(array(
2483
			'lang' => 'eng',
2484
			'controller' => 'posts',
2485
			'action' => 'view',
2486
			'pass' => array(1),
2487
			'named' => array(),
2488
		));
2489
		$request->query = array('url' => 'eng/posts/view/1', 'test' => 'value');
2490
		$result = Router::reverse($request);
2491
		$expected = '/eng/posts/view/1?test=value';
2492
		$this->assertEquals($expected, $result);
2493
 
2494
		$params = array(
2495
			'lang' => 'eng',
2496
			'controller' => 'posts',
2497
			'action' => 'view',
2498
			'pass' => array(1),
2499
			'named' => array(),
2500
			'url' => array('url' => 'eng/posts/view/1')
2501
		);
2502
		$result = Router::reverse($params, true);
2503
		$this->assertRegExp('/^http(s)?:\/\//', $result);
2504
	}
2505
 
2506
/**
2507
 * Test that extensions work with Router::reverse()
2508
 *
2509
 * @return void
2510
 */
2511
	public function testReverseWithExtension() {
2512
		Router::parseExtensions('json');
2513
 
2514
		$request = new CakeRequest('/posts/view/1.json');
2515
		$request->addParams(array(
2516
			'controller' => 'posts',
2517
			'action' => 'view',
2518
			'pass' => array(1),
2519
			'named' => array(),
2520
			'ext' => 'json',
2521
		));
2522
		$request->query = array();
2523
		$result = Router::reverse($request);
2524
		$expected = '/posts/view/1.json';
2525
		$this->assertEquals($expected, $result);
2526
	}
2527
 
2528
/**
2529
 * test that setRequestInfo can accept arrays and turn that into a CakeRequest object.
2530
 *
2531
 * @return void
2532
 */
2533
	public function testSetRequestInfoLegacy() {
2534
		Router::setRequestInfo(array(
2535
			array(
2536
				'plugin' => null, 'controller' => 'images', 'action' => 'index',
2537
				'url' => array('url' => 'protected/images/index')
2538
			),
2539
			array(
2540
				'base' => '',
2541
				'here' => '/protected/images/index',
2542
				'webroot' => '/',
2543
			)
2544
		));
2545
		$result = Router::getRequest();
2546
		$this->assertEquals('images', $result->controller);
2547
		$this->assertEquals('index', $result->action);
2548
		$this->assertEquals('', $result->base);
2549
		$this->assertEquals('/protected/images/index', $result->here);
2550
		$this->assertEquals('/', $result->webroot);
2551
	}
2552
 
2553
/**
2554
 * Test that Router::url() uses the first request
2555
 */
2556
	public function testUrlWithRequestAction() {
2557
		$firstRequest = new CakeRequest('/posts/index');
2558
		$firstRequest->addParams(array(
2559
			'plugin' => null,
2560
			'controller' => 'posts',
2561
			'action' => 'index'
2562
		))->addPaths(array('base' => ''));
2563
 
2564
		$secondRequest = new CakeRequest('/posts/index');
2565
		$secondRequest->addParams(array(
2566
			'requested' => 1,
2567
			'plugin' => null,
2568
			'controller' => 'comments',
2569
			'action' => 'listing'
2570
		))->addPaths(array('base' => ''));
2571
 
2572
		Router::setRequestInfo($firstRequest);
2573
		Router::setRequestInfo($secondRequest);
2574
 
2575
		$result = Router::url(array('base' => false));
2576
		$this->assertEquals('/comments/listing', $result, 'with second requests, the last should win.');
2577
 
2578
		Router::popRequest();
2579
		$result = Router::url(array('base' => false));
2580
		$this->assertEquals('/posts', $result, 'with second requests, the last should win.');
2581
	}
2582
 
2583
/**
2584
 * test that a route object returning a full URL is not modified.
2585
 *
2586
 * @return void
2587
 */
2588
	public function testUrlFullUrlReturnFromRoute() {
2589
		$url = 'http://example.com/posts/view/1';
2590
 
2591
		$this->getMock('CakeRoute', array(), array('/'), 'MockReturnRoute');
2592
		$routes = Router::connect('/:controller/:action', array(), array('routeClass' => 'MockReturnRoute'));
2593
		$routes[0]->expects($this->any())->method('match')
2594
			->will($this->returnValue($url));
2595
 
2596
		$result = Router::url(array('controller' => 'posts', 'action' => 'view', 1));
2597
		$this->assertEquals($url, $result);
2598
	}
2599
 
2600
/**
2601
 * test protocol in url
2602
 *
2603
 * @return void
2604
 */
2605
	public function testUrlProtocol() {
2606
		$url = 'http://example.com';
2607
		$this->assertEquals($url, Router::url($url));
2608
 
2609
		$url = 'ed2k://example.com';
2610
		$this->assertEquals($url, Router::url($url));
2611
 
2612
		$url = 'svn+ssh://example.com';
2613
		$this->assertEquals($url, Router::url($url));
2614
 
2615
		$url = '://example.com';
2616
		$this->assertEquals($url, Router::url($url));
2617
 
2618
		$url = '//example.com';
2619
		$this->assertEquals($url, Router::url($url));
2620
 
2621
		$url = 'javascript:void(0)';
2622
		$this->assertEquals($url, Router::url($url));
2623
 
2624
		$url = 'tel:012345-678';
2625
		$this->assertEquals($url, Router::url($url));
2626
 
2627
		$url = 'sms:012345-678';
2628
		$this->assertEquals($url, Router::url($url));
2629
 
2630
		$url = '#here';
2631
		$this->assertEquals($url, Router::url($url));
2632
 
2633
		$url = '?param=0';
2634
		$this->assertEquals($url, Router::url($url));
2635
 
2636
		$url = 'posts/index#here';
2637
		$expected = FULL_BASE_URL . '/posts/index#here';
2638
		$this->assertEquals($expected, Router::url($url, true));
2639
	}
2640
 
2641
/**
2642
 * Testing that patterns on the :action param work properly.
2643
 *
2644
 * @return void
2645
 */
2646
	public function testPatternOnAction() {
2647
		$route = new CakeRoute(
2648
			'/blog/:action/*',
2649
			array('controller' => 'blog_posts'),
2650
			array('action' => 'other|actions')
2651
		);
2652
		$result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
2653
		$this->assertFalse($result);
2654
 
2655
		$result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
2656
		$this->assertEquals('/blog/actions/', $result);
2657
 
2658
		$result = $route->parse('/blog/other');
2659
		$expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => array(), 'named' => array());
2660
		$this->assertEquals($expected, $result);
2661
 
2662
		$result = $route->parse('/blog/foobar');
2663
		$this->assertFalse($result);
2664
	}
2665
 
2666
/**
2667
 * Tests resourceMap as getter and setter.
2668
 *
2669
 * @return void
2670
 */
2671
	public function testResourceMap() {
2672
		$default = Router::resourceMap();
2673
		$expected = array(
2674
			array('action' => 'index', 'method' => 'GET', 'id' => false),
2675
			array('action' => 'view', 'method' => 'GET', 'id' => true),
2676
			array('action' => 'add', 'method' => 'POST', 'id' => false),
2677
			array('action' => 'edit', 'method' => 'PUT', 'id' => true),
2678
			array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
2679
			array('action' => 'edit', 'method' => 'POST', 'id' => true)
2680
		);
2681
		$this->assertEquals($expected, $default);
2682
 
2683
		$custom = array(
2684
			array('action' => 'index', 'method' => 'GET', 'id' => false),
2685
			array('action' => 'view', 'method' => 'GET', 'id' => true),
2686
			array('action' => 'add', 'method' => 'POST', 'id' => false),
2687
			array('action' => 'edit', 'method' => 'PUT', 'id' => true),
2688
			array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
2689
			array('action' => 'update', 'method' => 'POST', 'id' => true)
2690
		);
2691
		Router::resourceMap($custom);
2692
		$this->assertEquals(Router::resourceMap(), $custom);
2693
 
2694
		Router::resourceMap($default);
2695
	}
2696
 
2697
/**
2698
 * test setting redirect routes
2699
 *
2700
 * @return void
2701
 */
2702
	public function testRouteRedirection() {
2703
		Router::redirect('/blog', array('controller' => 'posts'), array('status' => 302));
2704
		$this->assertEquals(1, count(Router::$routes));
2705
		Router::$routes[0]->response = $this->getMock('CakeResponse', array('_sendHeader'));
2706
		Router::$routes[0]->stop = false;
2707
		$this->assertEquals(302, Router::$routes[0]->options['status']);
2708
 
2709
		Router::parse('/blog');
2710
		$header = Router::$routes[0]->response->header();
2711
		$this->assertEquals(Router::url('/posts', true), $header['Location']);
2712
		$this->assertEquals(302, Router::$routes[0]->response->statusCode());
2713
 
2714
		Router::$routes[0]->response = $this->getMock('CakeResponse', array('_sendHeader'));
2715
		Router::parse('/not-a-match');
2716
		$this->assertEquals(array(), Router::$routes[0]->response->header());
2717
	}
2718
 
2719
/**
2720
 * Test setting the default route class
2721
 *
2722
 * @return void
2723
 */
2724
	public function testDefaultRouteClass() {
2725
		$this->getMock('CakeRoute', array(), array('/test'), 'TestDefaultRouteClass');
2726
		Router::defaultRouteClass('TestDefaultRouteClass');
2727
 
2728
		$result = Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
2729
		$this->assertInstanceOf('TestDefaultRouteClass', $result[0]);
2730
	}
2731
 
2732
/**
2733
 * Test getting the default route class
2734
 *
2735
 * @return void
2736
 */
2737
	public function testDefaultRouteClassGetter() {
2738
		$routeClass = 'TestDefaultRouteClass';
2739
		Router::defaultRouteClass($routeClass);
2740
 
2741
		$this->assertEquals($routeClass, Router::defaultRouteClass());
2742
		$this->assertEquals($routeClass, Router::defaultRouteClass(null));
2743
	}
2744
 
2745
/**
2746
 * Test that route classes must extend CakeRoute
2747
 *
2748
 * @expectedException RouterException
2749
 * @return void
2750
 */
2751
	public function testDefaultRouteException() {
2752
		Router::defaultRouteClass('');
2753
		Router::connect('/:controller', array());
2754
	}
2755
 
2756
/**
2757
 * Test that route classes must extend CakeRoute
2758
 *
2759
 * @expectedException RouterException
2760
 * @return void
2761
 */
2762
	public function testSettingInvalidDefaultRouteException() {
2763
		Router::defaultRouteClass('Object');
2764
	}
2765
 
2766
/**
2767
 * Test that class must exist
2768
 *
2769
 * @expectedException RouterException
2770
 * @return void
2771
 */
2772
	public function testSettingNonExistentDefaultRouteException() {
2773
		Router::defaultRouteClass('NonExistentClass');
2774
	}
2775
 
2776
/**
2777
 * Tests generating well-formed querystrings
2778
 *
2779
 * @return void
2780
 */
2781
	public function testQueryString() {
2782
		$result = Router::queryString(array('var' => 'foo bar'));
2783
		$expected = '?var=foo+bar';
2784
		$this->assertEquals($expected, $result);
2785
 
2786
		$result = Router::queryString(false, array('some' => 'param', 'foo' => 'bar'));
2787
		$expected = '?some=param&foo=bar';
2788
		$this->assertEquals($expected, $result);
2789
 
2790
		$existing = array('apple' => 'red', 'pear' => 'green');
2791
		$result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar'));
2792
		$expected = '?apple=red&pear=green&some=param&foo=bar';
2793
		$this->assertEquals($expected, $result);
2794
 
2795
		$existing = 'apple=red&pear=green';
2796
		$result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar'));
2797
		$expected = '?apple=red&pear=green&some=param&foo=bar';
2798
		$this->assertEquals($expected, $result);
2799
 
2800
		$existing = '?apple=red&pear=green';
2801
		$result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar'));
2802
		$expected = '?apple=red&pear=green&some=param&foo=bar';
2803
		$this->assertEquals($expected, $result);
2804
 
2805
		$result = Router::queryString('apple=red&pear=green');
2806
		$expected = '?apple=red&pear=green';
2807
		$this->assertEquals($expected, $result);
2808
 
2809
		$result = Router::queryString('foo=bar', array('php' => 'nut', 'jose' => 'zap'), true);
2810
		$expected = '?foo=bar&amp;php=nut&amp;jose=zap';
2811
		$this->assertEquals($expected, $result);
2812
 
2813
		$result = Router::queryString('foo=bar&amp;', array('php' => 'nut', 'jose' => 'zap'), true);
2814
		$expected = '?foo=bar&amp;php=nut&amp;jose=zap';
2815
		$this->assertEquals($expected, $result);
2816
 
2817
		$result = Router::queryString('foo=bar&', array('php' => 'nut', 'jose' => 'zap'));
2818
		$expected = '?foo=bar&php=nut&jose=zap';
2819
		$this->assertEquals($expected, $result);
2820
	}
2821
}