Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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