Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * CakeRequest Test case file.
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
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://cakephp.org CakePHP(tm) Project
14
 * @package       Cake.Test.Case.Routing.Route
15
 * @since         CakePHP(tm) v 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('CakeRoute', 'Routing/Route');
20
App::uses('Router', 'Routing');
21
 
22
/**
23
 * Test case for CakeRoute
24
 *
25
 * @package       Cake.Test.Case.Routing.Route
26
 */
27
class CakeRouteTest extends CakeTestCase {
28
 
29
/**
30
 * setUp method
31
 *
32
 * @return void
33
 */
34
	public function setUp() {
35
		parent::setUp();
36
		Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
37
	}
38
 
39
/**
40
 * Test the construction of a CakeRoute
41
 *
42
 * @return void
43
 */
44
	public function testConstruction() {
45
		$route = new CakeRoute('/:controller/:action/:id', array(), array('id' => '[0-9]+'));
46
 
47
		$this->assertEquals('/:controller/:action/:id', $route->template);
48
		$this->assertEquals(array(), $route->defaults);
49
		$this->assertEquals(array('id' => '[0-9]+'), $route->options);
50
		$this->assertFalse($route->compiled());
51
	}
52
 
53
/**
54
 * test Route compiling.
55
 *
56
 * @return void
57
 */
58
	public function testBasicRouteCompiling() {
59
		$route = new CakeRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
60
		$result = $route->compile();
61
		$expected = '#^/*$#';
62
		$this->assertEquals($expected, $result);
63
		$this->assertEquals(array(), $route->keys);
64
 
65
		$route = new CakeRoute('/:controller/:action', array('controller' => 'posts'));
66
		$result = $route->compile();
67
 
68
		$this->assertRegExp($result, '/posts/edit');
69
		$this->assertRegExp($result, '/posts/super_delete');
70
		$this->assertNotRegExp($result, '/posts');
71
		$this->assertNotRegExp($result, '/posts/super_delete/1');
72
 
73
		$route = new CakeRoute('/posts/foo:id', array('controller' => 'posts', 'action' => 'view'));
74
		$result = $route->compile();
75
 
76
		$this->assertRegExp($result, '/posts/foo:1');
77
		$this->assertRegExp($result, '/posts/foo:param');
78
		$this->assertNotRegExp($result, '/posts');
79
		$this->assertNotRegExp($result, '/posts/');
80
 
81
		$this->assertEquals(array('id'), $route->keys);
82
 
83
		$route = new CakeRoute('/:plugin/:controller/:action/*', array('plugin' => 'test_plugin', 'action' => 'index'));
84
		$result = $route->compile();
85
		$this->assertRegExp($result, '/test_plugin/posts/index');
86
		$this->assertRegExp($result, '/test_plugin/posts/edit/5');
87
		$this->assertRegExp($result, '/test_plugin/posts/edit/5/name:value/nick:name');
88
	}
89
 
90
/**
91
 * test that route parameters that overlap don't cause errors.
92
 *
93
 * @return void
94
 */
95
	public function testRouteParameterOverlap() {
96
		$route = new CakeRoute('/invoices/add/:idd/:id', array('controller' => 'invoices', 'action' => 'add'));
97
		$result = $route->compile();
98
		$this->assertRegExp($result, '/invoices/add/1/3');
99
 
100
		$route = new CakeRoute('/invoices/add/:id/:idd', array('controller' => 'invoices', 'action' => 'add'));
101
		$result = $route->compile();
102
		$this->assertRegExp($result, '/invoices/add/1/3');
103
	}
104
 
105
/**
106
 * test compiling routes with keys that have patterns
107
 *
108
 * @return void
109
 */
110
	public function testRouteCompilingWithParamPatterns() {
111
		$route = new CakeRoute(
112
			'/:controller/:action/:id',
113
			array(),
114
			array('id' => Router::ID)
115
		);
116
		$result = $route->compile();
117
		$this->assertRegExp($result, '/posts/edit/1');
118
		$this->assertRegExp($result, '/posts/view/518098');
119
		$this->assertNotRegExp($result, '/posts/edit/name-of-post');
120
		$this->assertNotRegExp($result, '/posts/edit/4/other:param');
121
		$this->assertEquals(array('controller', 'action', 'id'), $route->keys);
122
 
123
		$route = new CakeRoute(
124
			'/:lang/:controller/:action/:id',
125
			array('controller' => 'testing4'),
126
			array('id' => Router::ID, 'lang' => '[a-z]{3}')
127
		);
128
		$result = $route->compile();
129
		$this->assertRegExp($result, '/eng/posts/edit/1');
130
		$this->assertRegExp($result, '/cze/articles/view/1');
131
		$this->assertNotRegExp($result, '/language/articles/view/2');
132
		$this->assertNotRegExp($result, '/eng/articles/view/name-of-article');
133
		$this->assertEquals(array('lang', 'controller', 'action', 'id'), $route->keys);
134
 
135
		foreach (array(':', '@', ';', '$', '-') as $delim) {
136
			$route = new CakeRoute('/posts/:id' . $delim . ':title');
137
			$result = $route->compile();
138
 
139
			$this->assertRegExp($result, '/posts/1' . $delim . 'name-of-article');
140
			$this->assertRegExp($result, '/posts/13244' . $delim . 'name-of_Article[]');
141
			$this->assertNotRegExp($result, '/posts/11!nameofarticle');
142
			$this->assertNotRegExp($result, '/posts/11');
143
 
144
			$this->assertEquals(array('id', 'title'), $route->keys);
145
		}
146
 
147
		$route = new CakeRoute(
148
			'/posts/:id::title/:year',
149
			array('controller' => 'posts', 'action' => 'view'),
150
			array('id' => Router::ID, 'year' => Router::YEAR, 'title' => '[a-z-_]+')
151
		);
152
		$result = $route->compile();
153
		$this->assertRegExp($result, '/posts/1:name-of-article/2009/');
154
		$this->assertRegExp($result, '/posts/13244:name-of-article/1999');
155
		$this->assertNotRegExp($result, '/posts/hey_now:nameofarticle');
156
		$this->assertNotRegExp($result, '/posts/:nameofarticle/2009');
157
		$this->assertNotRegExp($result, '/posts/:nameofarticle/01');
158
		$this->assertEquals(array('id', 'title', 'year'), $route->keys);
159
 
160
		$route = new CakeRoute(
161
			'/posts/:url_title-(uuid::id)',
162
			array('controller' => 'posts', 'action' => 'view'),
163
			array('pass' => array('id', 'url_title'), 'id' => Router::ID)
164
		);
165
		$result = $route->compile();
166
		$this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)/');
167
		$this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)');
168
		$this->assertNotRegExp($result, '/posts/');
169
		$this->assertNotRegExp($result, '/posts/nameofarticle');
170
		$this->assertNotRegExp($result, '/posts/nameofarticle-12347');
171
		$this->assertEquals(array('url_title', 'id'), $route->keys);
172
	}
173
 
174
/**
175
 * test more complex route compiling & parsing with mid route greedy stars
176
 * and optional routing parameters
177
 *
178
 * @return void
179
 */
180
	public function testComplexRouteCompilingAndParsing() {
181
		$route = new CakeRoute(
182
			'/posts/:month/:day/:year/*',
183
			array('controller' => 'posts', 'action' => 'view'),
184
			array('year' => Router::YEAR, 'month' => Router::MONTH, 'day' => Router::DAY)
185
		);
186
		$result = $route->compile();
187
		$this->assertRegExp($result, '/posts/08/01/2007/title-of-post');
188
		$result = $route->parse('/posts/08/01/2007/title-of-post');
189
 
190
		$this->assertEquals(7, count($result));
191
		$this->assertEquals('posts', $result['controller']);
192
		$this->assertEquals('view', $result['action']);
193
		$this->assertEquals('2007', $result['year']);
194
		$this->assertEquals('08', $result['month']);
195
		$this->assertEquals('01', $result['day']);
196
		$this->assertEquals('title-of-post', $result['pass'][0]);
197
 
198
		$route = new CakeRoute(
199
			"/:extra/page/:slug/*",
200
			array('controller' => 'pages', 'action' => 'view', 'extra' => null),
201
			array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
202
		);
203
		$result = $route->compile();
204
 
205
		$this->assertRegExp($result, '/some_extra/page/this_is_the_slug');
206
		$this->assertRegExp($result, '/page/this_is_the_slug');
207
		$this->assertEquals(array('extra', 'slug'), $route->keys);
208
		$this->assertEquals(array('extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'), $route->options);
209
		$expected = array(
210
			'controller' => 'pages',
211
			'action' => 'view'
212
		);
213
		$this->assertEquals($expected, $route->defaults);
214
 
215
		$route = new CakeRoute(
216
			'/:controller/:action/*',
217
			array('project' => false),
218
			array(
219
				'controller' => 'source|wiki|commits|tickets|comments|view',
220
				'action' => 'branches|history|branch|logs|view|start|add|edit|modify'
221
			)
222
		);
223
		$this->assertFalse($route->parse('/chaw_test/wiki'));
224
 
225
		$result = $route->compile();
226
		$this->assertNotRegExp($result, '/some_project/source');
227
		$this->assertRegExp($result, '/source/view');
228
		$this->assertRegExp($result, '/source/view/other/params');
229
		$this->assertNotRegExp($result, '/chaw_test/wiki');
230
		$this->assertNotRegExp($result, '/source/wierd_action');
231
	}
232
 
233
/**
234
 * test that routes match their pattern.
235
 *
236
 * @return void
237
 */
238
	public function testMatchBasic() {
239
		$route = new CakeRoute('/:controller/:action/:id', array('plugin' => null));
240
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
241
		$this->assertFalse($result);
242
 
243
		$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 0));
244
		$this->assertFalse($result);
245
 
246
		$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1));
247
		$this->assertEquals('/posts/view/1', $result);
248
 
249
		$route = new CakeRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
250
		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
251
		$this->assertEquals('/', $result);
252
 
253
		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
254
		$this->assertFalse($result);
255
 
256
		$route = new CakeRoute('/pages/*', array('controller' => 'pages', 'action' => 'display'));
257
		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
258
		$this->assertEquals('/pages/home', $result);
259
 
260
		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
261
		$this->assertEquals('/pages/about', $result);
262
 
263
		$route = new CakeRoute('/blog/:action', array('controller' => 'posts'));
264
		$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
265
		$this->assertEquals('/blog/view', $result);
266
 
267
		$result = $route->match(array('controller' => 'nodes', 'action' => 'view'));
268
		$this->assertFalse($result);
269
 
270
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 1));
271
		$this->assertFalse($result);
272
 
273
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'id' => 2));
274
		$this->assertFalse($result);
275
 
276
		$route = new CakeRoute('/foo/:controller/:action', array('action' => 'index'));
277
		$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
278
		$this->assertEquals('/foo/posts/view', $result);
279
 
280
		$route = new CakeRoute('/:plugin/:id/*', array('controller' => 'posts', 'action' => 'view'));
281
		$result = $route->match(array('plugin' => 'test', 'controller' => 'posts', 'action' => 'view', 'id' => '1'));
282
		$this->assertEquals('/test/1/', $result);
283
 
284
		$result = $route->match(array('plugin' => 'fo', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0'));
285
		$this->assertEquals('/fo/1/0', $result);
286
 
287
		$result = $route->match(array('plugin' => 'fo', 'controller' => 'nodes', 'action' => 'view', 'id' => 1));
288
		$this->assertFalse($result);
289
 
290
		$result = $route->match(array('plugin' => 'fo', 'controller' => 'posts', 'action' => 'edit', 'id' => 1));
291
		$this->assertFalse($result);
292
 
293
		$route = new CakeRoute('/admin/subscriptions/:action/*', array(
294
			'controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin'
295
		));
296
 
297
		$url = array('controller' => 'subscribe', 'admin' => true, 'action' => 'edit', 1);
298
		$result = $route->match($url);
299
		$expected = '/admin/subscriptions/edit/1';
300
		$this->assertEquals($expected, $result);
301
 
302
		$url = array(
303
			'controller' => 'subscribe',
304
			'admin' => true,
305
			'action' => 'edit_admin_e',
306
			1
307
		);
308
		$result = $route->match($url);
309
		$expected = '/admin/subscriptions/edit_admin_e/1';
310
		$this->assertEquals($expected, $result);
311
	}
312
 
313
/**
314
 * test that non-greedy routes fail with extra passed args
315
 *
316
 * @return void
317
 */
318
	public function testGreedyRouteFailurePassedArg() {
319
		$route = new CakeRoute('/:controller/:action', array('plugin' => null));
320
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', '0'));
321
		$this->assertFalse($result);
322
 
323
		$route = new CakeRoute('/:controller/:action', array('plugin' => null));
324
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'test'));
325
		$this->assertFalse($result);
326
	}
327
 
328
/**
329
 * test that non-greedy routes fail with extra passed args
330
 *
331
 * @return void
332
 */
333
	public function testGreedyRouteFailureNamedParam() {
334
		$route = new CakeRoute('/:controller/:action', array('plugin' => null));
335
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'page' => 1));
336
		$this->assertFalse($result);
337
	}
338
 
339
/**
340
 * test that falsey values do not interrupt a match.
341
 *
342
 * @return void
343
 */
344
	public function testMatchWithFalseyValues() {
345
		$route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
346
		$result = $route->match(array(
347
			'controller' => 'posts', 'action' => 'index', 'plugin' => null, 'admin' => false
348
		));
349
		$this->assertEquals('/posts/index/', $result);
350
	}
351
 
352
/**
353
 * test match() with greedy routes, named parameters and passed args.
354
 *
355
 * @return void
356
 */
357
	public function testMatchWithNamedParametersAndPassedArgs() {
358
		Router::connectNamed(true);
359
 
360
		$route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
361
		$result = $route->match(array('controller' => 'posts', 'action' => 'index', 'plugin' => null, 'page' => 1));
362
		$this->assertEquals('/posts/index/page:1', $result);
363
 
364
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5));
365
		$this->assertEquals('/posts/view/5', $result);
366
 
367
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 0));
368
		$this->assertEquals('/posts/view/0', $result);
369
 
370
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, '0'));
371
		$this->assertEquals('/posts/view/0', $result);
372
 
373
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5, 'page' => 1, 'limit' => 20, 'order' => 'title'));
374
		$this->assertEquals('/posts/view/5/page:1/limit:20/order:title', $result);
375
 
376
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 'word space', 'order' => 'Θ'));
377
		$this->assertEquals('/posts/view/word%20space/order:%CE%98', $result);
378
 
379
		$route = new CakeRoute('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
380
		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 1));
381
		$this->assertFalse($result);
382
 
383
		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 2, 'something'));
384
		$this->assertEquals('/test2/something', $result);
385
 
386
		$result = $route->match(array('controller' => 'pages', 'action' => 'display', 5, 'something'));
387
		$this->assertFalse($result);
388
	}
389
 
390
/**
391
 * Ensure that named parameters are urldecoded
392
 *
393
 * @return void
394
 */
395
	public function testParseNamedParametersUrlDecode() {
396
		Router::connectNamed(true);
397
		$route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
398
 
399
		$result = $route->parse('/posts/index/page:%CE%98');
400
		$this->assertEquals('Θ', $result['named']['page']);
401
 
402
		$result = $route->parse('/posts/index/page[]:%CE%98');
403
		$this->assertEquals('Θ', $result['named']['page'][0]);
404
 
405
		$result = $route->parse('/posts/index/something%20else/page[]:%CE%98');
406
		$this->assertEquals('Θ', $result['named']['page'][0]);
407
		$this->assertEquals('something else', $result['pass'][0]);
408
	}
409
 
410
/**
411
 * Ensure that keys at named parameters are urldecoded
412
 *
413
 * @return void
414
 */
415
	public function testParseNamedKeyUrlDecode() {
416
		Router::connectNamed(true);
417
		$route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
418
 
419
		// checking /post/index/user[0]:a/user[1]:b
420
		$result = $route->parse('/posts/index/user%5B0%5D:a/user%5B1%5D:b');
421
		$this->assertArrayHasKey('user', $result['named']);
422
		$this->assertEquals(array('a', 'b'), $result['named']['user']);
423
 
424
		// checking /post/index/user[]:a/user[]:b
425
		$result = $route->parse('/posts/index/user%5B%5D:a/user%5B%5D:b');
426
		$this->assertArrayHasKey('user', $result['named']);
427
		$this->assertEquals(array('a', 'b'), $result['named']['user']);
428
	}
429
 
430
/**
431
 * test that named params with null/false are excluded
432
 *
433
 * @return void
434
 */
435
	public function testNamedParamsWithNullFalse() {
436
		$route = new CakeRoute('/:controller/:action/*');
437
		$result = $route->match(array('controller' => 'posts', 'action' => 'index', 'page' => null, 'sort' => false));
438
		$this->assertEquals('/posts/index/', $result);
439
	}
440
 
441
/**
442
 * test that match with patterns works.
443
 *
444
 * @return void
445
 */
446
	public function testMatchWithPatterns() {
447
		$route = new CakeRoute('/:controller/:action/:id', array('plugin' => null), array('id' => '[0-9]+'));
448
		$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'id' => 'foo'));
449
		$this->assertFalse($result);
450
 
451
		$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '9'));
452
		$this->assertEquals('/posts/view/9', $result);
453
 
454
		$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '922'));
455
		$this->assertEquals('/posts/view/922', $result);
456
 
457
		$result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 'a99'));
458
		$this->assertFalse($result);
459
	}
460
 
461
/**
462
 * test persistParams ability to persist parameters from $params and remove params.
463
 *
464
 * @return void
465
 */
466
	public function testPersistParams() {
467
		$route = new CakeRoute(
468
			'/:lang/:color/blog/:action',
469
			array('controller' => 'posts'),
470
			array('persist' => array('lang', 'color'))
471
		);
472
		$url = array('controller' => 'posts', 'action' => 'index');
473
		$params = array('lang' => 'en', 'color' => 'blue');
474
		$result = $route->persistParams($url, $params);
475
		$this->assertEquals('en', $result['lang']);
476
		$this->assertEquals('blue', $result['color']);
477
 
478
		$url = array('controller' => 'posts', 'action' => 'index', 'color' => 'red');
479
		$params = array('lang' => 'en', 'color' => 'blue');
480
		$result = $route->persistParams($url, $params);
481
		$this->assertEquals('en', $result['lang']);
482
		$this->assertEquals('red', $result['color']);
483
	}
484
 
485
/**
486
 * test persist with a non array value
487
 *
488
 * @return void
489
 */
490
	public function testPersistParamsNonArray() {
491
		$url = array('controller' => 'posts', 'action' => 'index');
492
		$params = array('lang' => 'en', 'color' => 'blue');
493
 
494
		$route = new CakeRoute(
495
			'/:lang/:color/blog/:action',
496
			array('controller' => 'posts')
497
			// No persist options
498
		);
499
		$result = $route->persistParams($url, $params);
500
		$this->assertEquals($url, $result);
501
 
502
		$route = new CakeRoute(
503
			'/:lang/:color/blog/:action',
504
			array('controller' => 'posts'),
505
			array('persist' => false)
506
		);
507
		$result = $route->persistParams($url, $params);
508
		$this->assertEquals($url, $result);
509
 
510
		$route = new CakeRoute(
511
			'/:lang/:color/blog/:action',
512
			array('controller' => 'posts'),
513
			array('persist' => 'derp')
514
		);
515
		$result = $route->persistParams($url, $params);
516
		$this->assertEquals($url, $result);
517
	}
518
 
519
/**
520
 * test the parse method of CakeRoute.
521
 *
522
 * @return void
523
 */
524
	public function testParse() {
525
		$route = new CakeRoute(
526
			'/:controller/:action/:id',
527
			array('controller' => 'testing4', 'id' => null),
528
			array('id' => Router::ID)
529
		);
530
		$route->compile();
531
		$result = $route->parse('/posts/view/1');
532
		$this->assertEquals('posts', $result['controller']);
533
		$this->assertEquals('view', $result['action']);
534
		$this->assertEquals('1', $result['id']);
535
 
536
		$route = new Cakeroute(
537
			'/admin/:controller',
538
			array('prefix' => 'admin', 'admin' => 1, 'action' => 'index')
539
		);
540
		$route->compile();
541
		$result = $route->parse('/admin/');
542
		$this->assertFalse($result);
543
 
544
		$result = $route->parse('/admin/posts');
545
		$this->assertEquals('posts', $result['controller']);
546
		$this->assertEquals('index', $result['action']);
547
	}
548
 
549
/**
550
 * Test that :key elements are urldecoded
551
 *
552
 * @return void
553
 */
554
	public function testParseUrlDecodeElements() {
555
		$route = new Cakeroute(
556
			'/:controller/:slug',
557
			array('action' => 'view')
558
		);
559
		$route->compile();
560
		$result = $route->parse('/posts/%E2%88%82%E2%88%82');
561
		$this->assertEquals('posts', $result['controller']);
562
		$this->assertEquals('view', $result['action']);
563
		$this->assertEquals('∂∂', $result['slug']);
564
 
565
		$result = $route->parse('/posts/∂∂');
566
		$this->assertEquals('posts', $result['controller']);
567
		$this->assertEquals('view', $result['action']);
568
		$this->assertEquals('∂∂', $result['slug']);
569
	}
570
 
571
/**
572
 * test numerically indexed defaults, get appended to pass
573
 *
574
 * @return void
575
 */
576
	public function testParseWithPassDefaults() {
577
		$route = new Cakeroute('/:controller', array('action' => 'display', 'home'));
578
		$result = $route->parse('/posts');
579
		$expected = array(
580
			'controller' => 'posts',
581
			'action' => 'display',
582
			'pass' => array('home'),
583
			'named' => array()
584
		);
585
		$this->assertEquals($expected, $result);
586
	}
587
 
588
/**
589
 * test that http header conditions can cause route failures.
590
 *
591
 * @return void
592
 */
593
	public function testParseWithHttpHeaderConditions() {
594
		$_SERVER['REQUEST_METHOD'] = 'GET';
595
		$route = new CakeRoute('/sample', array('controller' => 'posts', 'action' => 'index', '[method]' => 'POST'));
596
 
597
		$this->assertFalse($route->parse('/sample'));
598
	}
599
 
600
/**
601
 * test that patterns work for :action
602
 *
603
 * @return void
604
 */
605
	public function testPatternOnAction() {
606
		$route = new CakeRoute(
607
			'/blog/:action/*',
608
			array('controller' => 'blog_posts'),
609
			array('action' => 'other|actions')
610
		);
611
		$result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
612
		$this->assertFalse($result);
613
 
614
		$result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
615
		$this->assertNotEmpty($result);
616
 
617
		$result = $route->parse('/blog/other');
618
		$expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => array(), 'named' => array());
619
		$this->assertEquals($expected, $result);
620
 
621
		$result = $route->parse('/blog/foobar');
622
		$this->assertFalse($result);
623
	}
624
 
625
/**
626
 * test the parseArgs method
627
 *
628
 * @return void
629
 */
630
	public function testParsePassedArgument() {
631
		$route = new CakeRoute('/:controller/:action/*');
632
		$result = $route->parse('/posts/edit/1/2/0');
633
		$expected = array(
634
			'controller' => 'posts',
635
			'action' => 'edit',
636
			'pass' => array('1', '2', '0'),
637
			'named' => array()
638
		);
639
		$this->assertEquals($expected, $result);
640
 
641
		$result = $route->parse('/posts/edit/a-string/page:1/sort:value');
642
		$expected = array(
643
			'controller' => 'posts',
644
			'action' => 'edit',
645
			'pass' => array('a-string'),
646
			'named' => array(
647
				'page' => 1,
648
				'sort' => 'value'
649
			)
650
		);
651
		$this->assertEquals($expected, $result);
652
	}
653
 
654
/**
655
 * test that only named parameter rules are followed.
656
 *
657
 * @return void
658
 */
659
	public function testParseNamedParametersWithRules() {
660
		$route = new CakeRoute('/:controller/:action/*', array(), array(
661
			'named' => array(
662
				'wibble',
663
				'fish' => array('action' => 'index'),
664
				'fizz' => array('controller' => array('comments', 'other')),
665
				'pattern' => 'val-[\d]+'
666
			)
667
		));
668
		$result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz/unknown:value');
669
		$expected = array(
670
			'controller' => 'posts',
671
			'action' => 'display',
672
			'pass' => array('fish:trout', 'fizz:buzz', 'unknown:value'),
673
			'named' => array(
674
				'wibble' => 'spin'
675
			)
676
		);
677
		$this->assertEquals($expected, $result, 'Fish should not be parsed, as action != index');
678
 
679
		$result = $route->parse('/posts/index/wibble:spin/fish:trout/fizz:buzz');
680
		$expected = array(
681
			'controller' => 'posts',
682
			'action' => 'index',
683
			'pass' => array('fizz:buzz'),
684
			'named' => array(
685
				'wibble' => 'spin',
686
				'fish' => 'trout'
687
			)
688
		);
689
		$this->assertEquals($expected, $result, 'Fizz should be parsed, as controller == comments|other');
690
 
691
		$result = $route->parse('/comments/index/wibble:spin/fish:trout/fizz:buzz');
692
		$expected = array(
693
			'controller' => 'comments',
694
			'action' => 'index',
695
			'pass' => array(),
696
			'named' => array(
697
				'wibble' => 'spin',
698
				'fish' => 'trout',
699
				'fizz' => 'buzz'
700
			)
701
		);
702
		$this->assertEquals($expected, $result, 'All params should be parsed as conditions were met.');
703
 
704
		$result = $route->parse('/comments/index/pattern:val--');
705
		$expected = array(
706
			'controller' => 'comments',
707
			'action' => 'index',
708
			'pass' => array('pattern:val--'),
709
			'named' => array()
710
		);
711
		$this->assertEquals($expected, $result, 'Named parameter pattern unmet.');
712
 
713
		$result = $route->parse('/comments/index/pattern:val-2');
714
		$expected = array(
715
			'controller' => 'comments',
716
			'action' => 'index',
717
			'pass' => array(),
718
			'named' => array('pattern' => 'val-2')
719
		);
720
		$this->assertEquals($expected, $result, 'Named parameter pattern met.');
721
	}
722
 
723
/**
724
 * test that greedyNamed ignores rules.
725
 *
726
 * @return void
727
 */
728
	public function testParseGreedyNamed() {
729
		$route = new CakeRoute('/:controller/:action/*', array(), array(
730
			'named' => array(
731
				'fizz' => array('controller' => 'comments'),
732
				'pattern' => 'val-[\d]+',
733
			),
734
			'greedyNamed' => true
735
		));
736
		$result = $route->parse('/posts/display/wibble:spin/fizz:buzz/pattern:ignored');
737
		$expected = array(
738
			'controller' => 'posts',
739
			'action' => 'display',
740
			'pass' => array('fizz:buzz', 'pattern:ignored'),
741
			'named' => array(
742
				'wibble' => 'spin',
743
			)
744
		);
745
		$this->assertEquals($expected, $result, 'Greedy named grabs everything, rules are followed');
746
	}
747
 
748
/**
749
 * Having greedNamed enabled should not capture routing.prefixes.
750
 *
751
 * @return void
752
 */
753
	public function testMatchGreedyNamedExcludesPrefixes() {
754
		Configure::write('Routing.prefixes', array('admin'));
755
		Router::reload();
756
 
757
		$route = new CakeRoute('/sales/*', array('controller' => 'sales', 'action' => 'index'));
758
		$this->assertFalse($route->match(array('controller' => 'sales', 'action' => 'index', 'admin' => 1)), 'Greedy named consume routing prefixes.');
759
	}
760
 
761
/**
762
 * test that parsing array format named parameters works
763
 *
764
 * @return void
765
 */
766
	public function testParseArrayNamedParameters() {
767
		$route = new CakeRoute('/:controller/:action/*');
768
		$result = $route->parse('/tests/action/var[]:val1/var[]:val2');
769
		$expected = array(
770
			'controller' => 'tests',
771
			'action' => 'action',
772
			'named' => array(
773
				'var' => array(
774
					'val1',
775
					'val2'
776
				)
777
			),
778
			'pass' => array(),
779
		);
780
		$this->assertEquals($expected, $result);
781
 
782
		$result = $route->parse('/tests/action/theanswer[is]:42/var[]:val2/var[]:val3');
783
		$expected = array(
784
			'controller' => 'tests',
785
			'action' => 'action',
786
			'named' => array(
787
				'theanswer' => array(
788
					'is' => 42
789
				),
790
				'var' => array(
791
					'val2',
792
					'val3'
793
				)
794
			),
795
			'pass' => array(),
796
		);
797
		$this->assertEquals($expected, $result);
798
 
799
		$result = $route->parse('/tests/action/theanswer[is][not]:42/theanswer[]:5/theanswer[is]:6');
800
		$expected = array(
801
			'controller' => 'tests',
802
			'action' => 'action',
803
			'named' => array(
804
				'theanswer' => array(
805
					5,
806
					'is' => array(
807
						6,
808
						'not' => 42
809
					)
810
				),
811
			),
812
			'pass' => array(),
813
		);
814
		$this->assertEquals($expected, $result);
815
	}
816
 
817
/**
818
 * Test that match can handle array named parameters
819
 *
820
 * @return void
821
 */
822
	public function testMatchNamedParametersArray() {
823
		$route = new CakeRoute('/:controller/:action/*');
824
 
825
		$url = array(
826
			'controller' => 'posts',
827
			'action' => 'index',
828
			'filter' => array(
829
				'one',
830
				'model' => 'value'
831
			)
832
		);
833
		$result = $route->match($url);
834
		$expected = '/posts/index/filter%5B0%5D:one/filter%5Bmodel%5D:value';
835
		$this->assertEquals($expected, $result);
836
 
837
		$url = array(
838
			'controller' => 'posts',
839
			'action' => 'index',
840
			'filter' => array(
841
				'one',
842
				'model' => array(
843
					'two',
844
					'order' => 'field'
845
				)
846
			)
847
		);
848
		$result = $route->match($url);
849
		$expected = '/posts/index/filter%5B0%5D:one/filter%5Bmodel%5D%5B0%5D:two/filter%5Bmodel%5D%5Border%5D:field';
850
		$this->assertEquals($expected, $result);
851
	}
852
 
853
/**
854
 * test restructuring args with pass key
855
 *
856
 * @return void
857
 */
858
	public function testPassArgRestructure() {
859
		$route = new CakeRoute('/:controller/:action/:slug', array(), array(
860
			'pass' => array('slug')
861
		));
862
		$result = $route->parse('/posts/view/my-title');
863
		$expected = array(
864
			'controller' => 'posts',
865
			'action' => 'view',
866
			'slug' => 'my-title',
867
			'pass' => array('my-title'),
868
			'named' => array()
869
		);
870
		$this->assertEquals($expected, $result, 'Slug should have moved');
871
	}
872
 
873
/**
874
 * Test the /** special type on parsing.
875
 *
876
 * @return void
877
 */
878
	public function testParseTrailing() {
879
		$route = new CakeRoute('/:controller/:action/**');
880
		$result = $route->parse('/posts/index/1/2/3/foo:bar');
881
		$expected = array(
882
			'controller' => 'posts',
883
			'action' => 'index',
884
			'pass' => array('1/2/3/foo:bar'),
885
			'named' => array()
886
		);
887
		$this->assertEquals($expected, $result);
888
 
889
		$result = $route->parse('/posts/index/http://example.com');
890
		$expected = array(
891
			'controller' => 'posts',
892
			'action' => 'index',
893
			'pass' => array('http://example.com'),
894
			'named' => array()
895
		);
896
		$this->assertEquals($expected, $result);
897
	}
898
 
899
/**
900
 * Test the /** special type on parsing - UTF8.
901
 *
902
 * @return void
903
 */
904
	public function testParseTrailingUTF8() {
905
		$route = new CakeRoute('/category/**', array('controller' => 'categories', 'action' => 'index'));
906
		$result = $route->parse('/category/%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84');
907
		$expected = array(
908
			'controller' => 'categories',
909
			'action' => 'index',
910
			'pass' => array('موبایل'),
911
			'named' => array()
912
		);
913
		$this->assertEquals($expected, $result);
914
	}
915
 
916
/**
917
 * test that utf-8 patterns work for :section
918
 *
919
 * @return void
920
 */
921
	public function testUTF8PatternOnSection() {
922
		$route = new CakeRoute(
923
			'/:section',
924
			array('plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index'),
925
			array(
926
				'persist' => array('section'),
927
				'section' => 'آموزش|weblog'
928
			)
929
		);
930
 
931
		$result = $route->parse('/%D8%A2%D9%85%D9%88%D8%B2%D8%B4');
932
		$expected = array('section' => 'آموزش', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => array(), 'named' => array());
933
		$this->assertEquals($expected, $result);
934
 
935
		$result = $route->parse('/weblog');
936
		$expected = array('section' => 'weblog', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => array(), 'named' => array());
937
		$this->assertEquals($expected, $result);
938
	}
939
}