/
RouteTest.php
1904 lines (1696 loc) · 61.2 KB
/
RouteTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 2.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Routing\Route;
use Cake\Core\Configure;
use Cake\Http\ServerRequest;
use Cake\Routing\Router;
use Cake\Routing\Route\Route;
use Cake\TestSuite\TestCase;
/**
* Used to expose protected methods for testing.
*/
class RouteProtected extends Route
{
/**
* @param $url
* @return array
*/
public function parseExtension($url)
{
return $this->_parseExtension($url);
}
}
/**
* Test case for Route
*/
class RouteTest extends TestCase
{
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
Configure::write('Routing', ['admin' => null, 'prefixes' => []]);
}
/**
* Test the construction of a Route
*
* @return void
*/
public function testConstruction()
{
$route = new Route('/:controller/:action/:id', [], ['id' => '[0-9]+']);
$this->assertEquals('/:controller/:action/:id', $route->template);
$this->assertEquals([], $route->defaults);
$this->assertEquals(['id' => '[0-9]+', '_ext' => []], $route->options);
$this->assertFalse($route->compiled());
}
/**
* Test set middleware in the constructor
*
* @return void
*/
public function testConstructorSetMiddleware()
{
$route = new Route('/:controller/:action/*', [], ['_middleware' => ['auth', 'cookie']]);
$this->assertSame(['auth', 'cookie'], $route->getMiddleware());
}
/**
* Test Route compiling.
*
* @return void
*/
public function testBasicRouteCompiling()
{
$route = new Route('/', ['controller' => 'pages', 'action' => 'display', 'home']);
$result = $route->compile();
$expected = '#^/*$#';
$this->assertEquals($expected, $result);
$this->assertEquals([], $route->keys);
$route = new Route('/:controller/:action', ['controller' => 'posts']);
$result = $route->compile();
$this->assertRegExp($result, '/posts/edit');
$this->assertRegExp($result, '/posts/super_delete');
$this->assertNotRegExp($result, '/posts');
$this->assertNotRegExp($result, '/posts/super_delete/1');
$this->assertSame($result, $route->compile());
$route = new Route('/posts/foo:id', ['controller' => 'posts', 'action' => 'view']);
$result = $route->compile();
$this->assertRegExp($result, '/posts/foo:1');
$this->assertRegExp($result, '/posts/foo:param');
$this->assertNotRegExp($result, '/posts');
$this->assertNotRegExp($result, '/posts/');
$this->assertEquals(['id'], $route->keys);
$route = new Route('/:plugin/:controller/:action/*', ['plugin' => 'test_plugin', 'action' => 'index']);
$result = $route->compile();
$this->assertRegExp($result, '/test_plugin/posts/index');
$this->assertRegExp($result, '/test_plugin/posts/edit/5');
$this->assertRegExp($result, '/test_plugin/posts/edit/5/name:value/nick:name');
}
/**
* Test that single letter placeholders work.
*
* @return void
*/
public function testRouteCompileSmallPlaceholders()
{
$route = new Route(
'/fighters/:id/move/:x/:y',
['controller' => 'Fighters', 'action' => 'move'],
['id' => '\d+', 'x' => '\d+', 'y' => '\d+', 'pass' => ['id', 'x', 'y']]
);
$pattern = $route->compile();
$this->assertRegExp($pattern, '/fighters/123/move/8/42');
$result = $route->match([
'controller' => 'Fighters',
'action' => 'move',
'id' => 123,
'x' => 8,
'y' => 42
]);
$this->assertEquals('/fighters/123/move/8/42', $result);
}
/**
* Test route compile with brace format.
*
* @return void
*/
public function testRouteCompileBraces()
{
$route = new Route(
'/fighters/{id}/move/{x}/{y}',
['controller' => 'Fighters', 'action' => 'move'],
['id' => '\d+', 'x' => '\d+', 'y' => '\d+', 'pass' => ['id', 'x', 'y']]
);
$this->assertRegExp($route->compile(), '/fighters/123/move/8/42');
$result = $route->match([
'controller' => 'Fighters',
'action' => 'move',
'id' => 123,
'x' => 8,
'y' => 42
]);
$this->assertEquals('/fighters/123/move/8/42', $result);
$route = new Route(
'/images/{id}/{x}x{y}',
['controller' => 'Images', 'action' => 'view']
);
$this->assertRegExp($route->compile(), '/images/123/640x480');
$result = $route->match([
'controller' => 'Images',
'action' => 'view',
'id' => 123,
'x' => 8,
'y' => 42
]);
$this->assertEquals('/images/123/8x42', $result);
}
/**
* Test route compile with brace format.
*
* @return void
*/
public function testRouteCompileBracesVariableName()
{
$route = new Route(
'/fighters/{0id}',
['controller' => 'Fighters', 'action' => 'move']
);
$pattern = $route->compile();
$this->assertNotRegExp($route->compile(), '/fighters/123', 'Placeholders must start with letter');
$route = new Route('/fighters/{Id}', ['controller' => 'Fighters', 'action' => 'move']);
$this->assertRegExp($route->compile(), '/fighters/123');
$route = new Route('/fighters/{i_d}', ['controller' => 'Fighters', 'action' => 'move']);
$this->assertRegExp($route->compile(), '/fighters/123');
$route = new Route('/fighters/{id99}', ['controller' => 'Fighters', 'action' => 'move']);
$this->assertRegExp($route->compile(), '/fighters/123');
}
/**
* Test route compile with brace format.
*
* @return void
*/
public function testRouteCompileBracesInvalid()
{
$route = new Route(
'/fighters/{ id }',
['controller' => 'Fighters', 'action' => 'move']
);
$this->assertNotRegExp($route->compile(), '/fighters/123', 'no spaces in placeholder');
$route = new Route(
'/fighters/{i d}',
['controller' => 'Fighters', 'action' => 'move']
);
$this->assertNotRegExp($route->compile(), '/fighters/123', 'no spaces in placeholder');
}
/**
* Test route compile with mixed placeholder types brace format.
*
* @return void
*/
public function testRouteCompileMixedPlaceholders()
{
$route = new Route(
'/images/{open/:id',
['controller' => 'Images', 'action' => 'open']
);
$pattern = $route->compile();
$this->assertRegExp($pattern, '/images/{open/9', 'Need both {} to enable brace mode');
$result = $route->match([
'controller' => 'Images',
'action' => 'open',
'id' => 123,
]);
$this->assertEquals('/images/{open/123', $result);
$route = new Route(
'/fighters/{id}/move/{x}/:y',
['controller' => 'Fighters', 'action' => 'move'],
['id' => '\d+', 'x' => '\d+', 'pass' => ['id', 'x']]
);
$pattern = $route->compile();
$this->assertRegExp($pattern, '/fighters/123/move/8/:y');
$result = $route->match([
'controller' => 'Fighters',
'action' => 'move',
'id' => 123,
'x' => 8,
'y' => 9
]);
$this->assertEquals('/fighters/123/move/8/:y?y=9', $result);
}
/**
* Test parsing routes with extensions.
*
* @return void
*/
public function testRouteParsingWithExtensions()
{
$route = new Route(
'/:controller/:action/*',
[],
['_ext' => ['json', 'xml']]
);
$result = $route->parse('/posts/index', 'GET');
$this->assertArrayNotHasKey('_ext', $result);
$result = $route->parse('/posts/index.pdf', 'GET');
$this->assertArrayNotHasKey('_ext', $result);
$result = $route->setExtensions(['pdf', 'json', 'xml', 'xml.gz'])->parse('/posts/index.pdf', 'GET');
$this->assertEquals('pdf', $result['_ext']);
$result = $route->parse('/posts/index.json', 'GET');
$this->assertEquals('json', $result['_ext']);
$result = $route->parse('/posts/index.xml', 'GET');
$this->assertEquals('xml', $result['_ext']);
$result = $route->parse('/posts/index.xml.gz', 'GET');
$this->assertEquals('xml.gz', $result['_ext']);
}
/**
* @return array
*/
public function provideMatchParseExtension()
{
return [
['/foo/bar.xml', ['/foo/bar', 'xml'], ['xml', 'json', 'xml.gz']],
['/foo/bar.json', ['/foo/bar', 'json'], ['xml', 'json', 'xml.gz']],
['/foo/bar.xml.gz', ['/foo/bar', 'xml.gz'], ['xml', 'json', 'xml.gz']],
['/foo/with.dots.json.xml.zip', ['/foo/with.dots.json.xml', 'zip'], ['zip']],
['/foo/confusing.extensions.dots.json.xml.zip', ['/foo/confusing.extensions.dots.json.xml', 'zip'], ['json', 'xml', 'zip']],
['/foo/confusing.extensions.dots.json.xml', ['/foo/confusing.extensions.dots.json', 'xml'], ['json', 'xml', 'zip']],
['/foo/confusing.extensions.dots.json', ['/foo/confusing.extensions.dots', 'json'], ['json', 'xml', 'zip']],
];
}
/**
* Expects _parseExtension to match extensions in URLs
*
* @param string $url
* @param array $expected
* @param array $ext
* @return void
* @dataProvider provideMatchParseExtension
*/
public function testMatchParseExtension($url, array $expected, array $ext)
{
$route = new RouteProtected('/:controller/:action/*', [], ['_ext' => $ext]);
$result = $route->parseExtension($url);
$this->assertEquals($expected, $result);
}
/**
* @return array
*/
public function provideNoMatchParseExtension()
{
return [
['/foo/bar', ['xml']],
['/foo/bar.zip', ['xml']],
['/foo/bar.xml.zip', ['xml']],
['/foo/bar.', ['xml']],
['/foo/bar.xml', []],
['/foo/bar...xml...zip...', ['xml']]
];
}
/**
* Expects _parseExtension to not match extensions in URLs
*
* @param string $url
* @param array $ext
* @return void
* @dataProvider provideNoMatchParseExtension
*/
public function testNoMatchParseExtension($url, array $ext)
{
$route = new RouteProtected('/:controller/:action/*', [], ['_ext' => $ext]);
list($outUrl, $outExt) = $route->parseExtension($url);
$this->assertEquals($url, $outUrl);
$this->assertNull($outExt);
}
/**
* Expects extensions to be set
*
* @group deprecated
* @return void
*/
public function testExtensions()
{
$this->deprecated(function () {
$route = new RouteProtected('/:controller/:action/*', []);
$this->assertEquals([], $route->extensions());
$route->extensions(['xml']);
$this->assertEquals(['xml'], $route->extensions());
});
}
/**
* Expects extensions to be set
*
* @return void
*/
public function testSetExtensions()
{
$route = new RouteProtected('/:controller/:action/*', []);
$this->assertEquals([], $route->getExtensions());
$route->setExtensions(['xml']);
$this->assertEquals(['xml'], $route->getExtensions());
$route->setExtensions(['xml', 'json', 'zip']);
$this->assertEquals(['xml', 'json', 'zip'], $route->getExtensions());
$route->setExtensions([]);
$this->assertEquals([], $route->getExtensions());
$route = new RouteProtected('/:controller/:action/*', [], ['_ext' => ['one', 'two']]);
$this->assertEquals(['one', 'two'], $route->getExtensions());
}
/**
* Expects extensions to be return.
*
* @return void
*/
public function testGetExtensions()
{
$route = new RouteProtected('/:controller/:action/*', []);
$this->assertEquals([], $route->getExtensions());
$route = new RouteProtected('/:controller/:action/*', [], ['_ext' => ['one', 'two']]);
$this->assertEquals(['one', 'two'], $route->getExtensions());
$route = new RouteProtected('/:controller/:action/*', []);
$this->assertEquals([], $route->getExtensions());
$route->setExtensions(['xml', 'json', 'zip']);
$this->assertEquals(['xml', 'json', 'zip'], $route->getExtensions());
}
/**
* Test that route parameters that overlap don't cause errors.
*
* @return void
*/
public function testRouteParameterOverlap()
{
$route = new Route('/invoices/add/:idd/:id', ['controller' => 'invoices', 'action' => 'add']);
$result = $route->compile();
$this->assertRegExp($result, '/invoices/add/1/3');
$route = new Route('/invoices/add/:id/:idd', ['controller' => 'invoices', 'action' => 'add']);
$result = $route->compile();
$this->assertRegExp($result, '/invoices/add/1/3');
}
/**
* Test compiling routes with keys that have patterns
*
* @return void
*/
public function testRouteCompilingWithParamPatterns()
{
$route = new Route(
'/:controller/:action/:id',
[],
['id' => Router::ID]
);
$result = $route->compile();
$this->assertRegExp($result, '/posts/edit/1');
$this->assertRegExp($result, '/posts/view/518098');
$this->assertNotRegExp($result, '/posts/edit/name-of-post');
$this->assertNotRegExp($result, '/posts/edit/4/other:param');
$this->assertEquals(['id', 'controller', 'action'], $route->keys);
$route = new Route(
'/:lang/:controller/:action/:id',
['controller' => 'testing4'],
['id' => Router::ID, 'lang' => '[a-z]{3}']
);
$result = $route->compile();
$this->assertRegExp($result, '/eng/posts/edit/1');
$this->assertRegExp($result, '/cze/articles/view/1');
$this->assertNotRegExp($result, '/language/articles/view/2');
$this->assertNotRegExp($result, '/eng/articles/view/name-of-article');
$this->assertEquals(['lang', 'id', 'controller', 'action'], $route->keys);
foreach ([':', '@', ';', '$', '-'] as $delim) {
$route = new Route('/posts/:id' . $delim . ':title');
$result = $route->compile();
$this->assertRegExp($result, '/posts/1' . $delim . 'name-of-article');
$this->assertRegExp($result, '/posts/13244' . $delim . 'name-of_Article[]');
$this->assertNotRegExp($result, '/posts/11!nameofarticle');
$this->assertNotRegExp($result, '/posts/11');
$this->assertEquals(['title', 'id'], $route->keys);
}
$route = new Route(
'/posts/:id::title/:year',
['controller' => 'posts', 'action' => 'view'],
['id' => Router::ID, 'year' => Router::YEAR, 'title' => '[a-z-_]+']
);
$result = $route->compile();
$this->assertRegExp($result, '/posts/1:name-of-article/2009/');
$this->assertRegExp($result, '/posts/13244:name-of-article/1999');
$this->assertNotRegExp($result, '/posts/hey_now:nameofarticle');
$this->assertNotRegExp($result, '/posts/:nameofarticle/2009');
$this->assertNotRegExp($result, '/posts/:nameofarticle/01');
$this->assertEquals(['year', 'title', 'id'], $route->keys);
$route = new Route(
'/posts/:url_title-(uuid::id)',
['controller' => 'posts', 'action' => 'view'],
['pass' => ['id', 'url_title'], 'id' => Router::ID]
);
$result = $route->compile();
$this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)/');
$this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)');
$this->assertNotRegExp($result, '/posts/');
$this->assertNotRegExp($result, '/posts/nameofarticle');
$this->assertNotRegExp($result, '/posts/nameofarticle-12347');
$this->assertEquals(['url_title', 'id'], $route->keys);
}
/**
* Test route with unicode
*
* @return void
*/
public function testCompileWithUnicodePatterns()
{
$route = new Route(
'/test/:slug',
['controller' => 'Pages', 'action' => 'display'],
['pass' => ['slug'], 'multibytePattern' => false, 'slug' => '[A-zА-я\-\ ]+']
);
$result = $route->compile();
$this->assertNotRegExp($result, '/test/bla-blan-тест');
$route = new Route(
'/test/:slug',
['controller' => 'Pages', 'action' => 'display'],
['pass' => ['slug'], 'multibytePattern' => true, 'slug' => '[A-zА-я\-\ ]+']
);
$result = $route->compile();
$this->assertRegExp($result, '/test/bla-blan-тест');
}
/**
* Test more complex route compiling & parsing with mid route greedy stars
* and optional routing parameters
*
* @return void
*/
public function testComplexRouteCompilingAndParsing()
{
$route = new Route(
'/posts/:month/:day/:year/*',
['controller' => 'posts', 'action' => 'view'],
['year' => Router::YEAR, 'month' => Router::MONTH, 'day' => Router::DAY]
);
$result = $route->compile();
$this->assertRegExp($result, '/posts/08/01/2007/title-of-post');
$result = $route->parse('/posts/08/01/2007/title-of-post', 'GET');
$this->assertCount(7, $result);
$this->assertEquals($result['controller'], 'posts');
$this->assertEquals($result['action'], 'view');
$this->assertEquals($result['year'], '2007');
$this->assertEquals($result['month'], '08');
$this->assertEquals($result['day'], '01');
$this->assertEquals($result['pass'][0], 'title-of-post');
$this->assertEquals($result['_matchedRoute'], '/posts/:month/:day/:year/*');
$route = new Route(
'/:extra/page/:slug/*',
['controller' => 'pages', 'action' => 'view', 'extra' => null],
['extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view']
);
$result = $route->compile();
$this->assertRegExp($result, '/some_extra/page/this_is_the_slug');
$this->assertRegExp($result, '/page/this_is_the_slug');
$this->assertEquals(['slug', 'extra'], $route->keys);
$this->assertEquals(['extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view', '_ext' => []], $route->options);
$expected = [
'controller' => 'pages',
'action' => 'view'
];
$this->assertEquals($expected, $route->defaults);
$route = new Route(
'/:controller/:action/*',
['project' => false],
[
'controller' => 'source|wiki|commits|tickets|comments|view',
'action' => 'branches|history|branch|logs|view|start|add|edit|modify'
]
);
$this->assertFalse($route->parse('/chaw_test/wiki', 'GET'));
$result = $route->compile();
$this->assertNotRegExp($result, '/some_project/source');
$this->assertRegExp($result, '/source/view');
$this->assertRegExp($result, '/source/view/other/params');
$this->assertNotRegExp($result, '/chaw_test/wiki');
$this->assertNotRegExp($result, '/source/weird_action');
}
/**
* Test that routes match their pattern.
*
* @return void
*/
public function testMatchBasic()
{
$route = new Route('/:controller/:action/:id', ['plugin' => null]);
$result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null]);
$this->assertFalse($result);
$result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 0]);
$this->assertFalse($result);
$result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1]);
$this->assertEquals('/posts/view/1', $result);
$route = new Route('/', ['controller' => 'pages', 'action' => 'display', 'home']);
$result = $route->match(['controller' => 'pages', 'action' => 'display', 'home']);
$this->assertEquals('/', $result);
$result = $route->match(['controller' => 'pages', 'action' => 'display', 'about']);
$this->assertFalse($result);
$route = new Route('/pages/*', ['controller' => 'pages', 'action' => 'display']);
$result = $route->match(['controller' => 'pages', 'action' => 'display', 'home']);
$this->assertEquals('/pages/home', $result);
$result = $route->match(['controller' => 'pages', 'action' => 'display', 'about']);
$this->assertEquals('/pages/about', $result);
$route = new Route('/blog/:action', ['controller' => 'posts']);
$result = $route->match(['controller' => 'posts', 'action' => 'view']);
$this->assertEquals('/blog/view', $result);
$result = $route->match(['controller' => 'posts', 'action' => 'view', 'id' => 2]);
$this->assertEquals('/blog/view?id=2', $result);
$result = $route->match(['controller' => 'nodes', 'action' => 'view']);
$this->assertFalse($result);
$result = $route->match(['controller' => 'posts', 'action' => 'view', 1]);
$this->assertFalse($result);
$route = new Route('/foo/:controller/:action', ['action' => 'index']);
$result = $route->match(['controller' => 'posts', 'action' => 'view']);
$this->assertEquals('/foo/posts/view', $result);
$route = new Route('/:plugin/:id/*', ['controller' => 'posts', 'action' => 'view']);
$result = $route->match(['plugin' => 'test', 'controller' => 'posts', 'action' => 'view', 'id' => '1']);
$this->assertEquals('/test/1/', $result);
$result = $route->match(['plugin' => 'fo', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0']);
$this->assertEquals('/fo/1/0', $result);
$result = $route->match(['plugin' => 'fo', 'controller' => 'nodes', 'action' => 'view', 'id' => 1]);
$this->assertFalse($result);
$result = $route->match(['plugin' => 'fo', 'controller' => 'posts', 'action' => 'edit', 'id' => 1]);
$this->assertFalse($result);
$route = new Route('/admin/subscriptions/:action/*', [
'controller' => 'subscribe', 'prefix' => 'admin'
]);
$url = ['controller' => 'subscribe', 'prefix' => 'admin', 'action' => 'edit', 1];
$result = $route->match($url);
$expected = '/admin/subscriptions/edit/1';
$this->assertEquals($expected, $result);
$url = [
'controller' => 'subscribe',
'prefix' => 'admin',
'action' => 'edit_admin_e',
1
];
$result = $route->match($url);
$expected = '/admin/subscriptions/edit_admin_e/1';
$this->assertEquals($expected, $result);
}
/**
* Test match() with persist option
*
* @return void
*/
public function testMatchWithPersistOption()
{
$context = [
'params' => ['lang' => 'en']
];
$route = new Route('/:lang/:controller/:action', [], ['persist' => ['lang']]);
$result = $route->match(
['controller' => 'tasks', 'action' => 'add'],
$context
);
$this->assertEquals('/en/tasks/add', $result);
}
/**
* Test match() with _host and other keys.
*
* @return void
*/
public function testMatchWithHostKeys()
{
$context = [
'_host' => 'foo.com',
'_scheme' => 'http',
'_port' => 80,
'_base' => ''
];
$route = new Route('/:controller/:action');
$result = $route->match(
['controller' => 'posts', 'action' => 'index', '_host' => 'example.com'],
$context
);
// Http has port 80 as default, do not include it in the url
$this->assertEquals('http://example.com/posts/index', $result);
$result = $route->match(
['controller' => 'posts', 'action' => 'index', '_scheme' => 'webcal'],
$context
);
// Webcal is not on port 80 by default, include it in url
$this->assertEquals('webcal://foo.com:80/posts/index', $result);
$result = $route->match(
['controller' => 'posts', 'action' => 'index', '_port' => '8080'],
$context
);
$this->assertEquals('http://foo.com:8080/posts/index', $result);
$result = $route->match(
['controller' => 'posts', 'action' => 'index', '_base' => '/dir'],
$context
);
$this->assertEquals('/dir/posts/index', $result);
$result = $route->match(
[
'controller' => 'posts',
'action' => 'index',
'_port' => '8080',
'_host' => 'example.com',
'_scheme' => 'https',
'_base' => '/dir'
],
$context
);
$this->assertEquals('https://example.com:8080/dir/posts/index', $result);
$context = [
'_host' => 'foo.com',
'_scheme' => 'http',
'_port' => 8080,
'_base' => ''
];
$result = $route->match(
[
'controller' => 'posts',
'action' => 'index',
'_port' => '8080',
'_host' => 'example.com',
'_scheme' => 'https',
'_base' => '/dir'
],
$context
);
// Https scheme is not on port 8080 by default, include the port
$this->assertEquals('https://example.com:8080/dir/posts/index', $result);
}
/**
* Test that the _host option sets the default host.
*
* @return void
*/
public function testMatchWithHostOption()
{
$route = new Route(
'/fallback',
['controller' => 'Articles', 'action' => 'index'],
['_host' => 'www.example.com']
);
$result = $route->match([
'controller' => 'Articles',
'action' => 'index'
]);
$this->assertSame('http://www.example.com/fallback', $result);
}
/**
* Test wildcard host options
*
* @return void
*/
public function testMatchWithHostWildcardOption()
{
$route = new Route(
'/fallback',
['controller' => 'Articles', 'action' => 'index'],
['_host' => '*.example.com']
);
$result = $route->match([
'controller' => 'Articles',
'action' => 'index'
]);
$this->assertFalse($result, 'No request context means no match');
$result = $route->match([
'controller' => 'Articles',
'action' => 'index',
], ['_host' => 'wrong.com']);
$this->assertFalse($result, 'Request context has bad host');
$result = $route->match([
'controller' => 'Articles',
'action' => 'index',
'_host' => 'wrong.com'
]);
$this->assertFalse($result, 'Url param is wrong');
$result = $route->match([
'controller' => 'Articles',
'action' => 'index',
'_host' => 'foo.example.com'
]);
$this->assertSame('http://foo.example.com/fallback', $result);
$result = $route->match([
'controller' => 'Articles',
'action' => 'index',
], [
'_host' => 'foo.example.com'
]);
$this->assertSame('http://foo.example.com/fallback', $result);
$result = $route->match([
'controller' => 'Articles',
'action' => 'index',
], [
'_scheme' => 'https',
'_host' => 'foo.example.com',
'_port' => 8080
]);
// When the port and scheme in the context are not present in the original url, they should be added
$this->assertSame('https://foo.example.com:8080/fallback', $result);
}
/**
* Test that non-greedy routes fail with extra passed args
*
* @return void
*/
public function testMatchGreedyRouteFailurePassedArg()
{
$route = new Route('/:controller/:action', ['plugin' => null]);
$result = $route->match(['controller' => 'posts', 'action' => 'view', '0']);
$this->assertFalse($result);
$route = new Route('/:controller/:action', ['plugin' => null]);
$result = $route->match(['controller' => 'posts', 'action' => 'view', 'test']);
$this->assertFalse($result);
}
/**
* Test that falsey values do not interrupt a match.
*
* @return void
*/
public function testMatchWithFalseyValues()
{
$route = new Route('/:controller/:action/*', ['plugin' => null]);
$result = $route->match([
'controller' => 'posts', 'action' => 'index', 'plugin' => null, 'admin' => false
]);
$this->assertEquals('/posts/index/', $result);
}
/**
* Test match() with greedy routes, and passed args.
*
* @return void
*/
public function testMatchWithPassedArgs()
{
$route = new Route('/:controller/:action/*', ['plugin' => null]);
$result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 5]);
$this->assertEquals('/posts/view/5', $result);
$result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 0]);
$this->assertEquals('/posts/view/0', $result);
$result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, '0']);
$this->assertEquals('/posts/view/0', $result);
$result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 'word space']);
$this->assertEquals('/posts/view/word%20space', $result);
$route = new Route('/test2/*', ['controller' => 'pages', 'action' => 'display', 2]);
$result = $route->match(['controller' => 'pages', 'action' => 'display', 1]);
$this->assertFalse($result);
$result = $route->match(['controller' => 'pages', 'action' => 'display', 2, 'something']);
$this->assertEquals('/test2/something', $result);
$result = $route->match(['controller' => 'pages', 'action' => 'display', 5, 'something']);
$this->assertFalse($result);
}
/**
* Test that the pass option lets you use positional arguments for the
* route elements that were named.
*
* @return void
*/
public function testMatchWithPassOption()
{
$route = new Route(
'/blog/:id-:slug',
['controller' => 'Blog', 'action' => 'view'],
['pass' => ['id', 'slug']]
);
$result = $route->match([
'controller' => 'Blog',
'action' => 'view',
'id' => 1,
'slug' => 'second'
]);
$this->assertEquals('/blog/1-second', $result);
$result = $route->match([
'controller' => 'Blog',
'action' => 'view',
1,
'second'
]);
$this->assertEquals('/blog/1-second', $result);
$result = $route->match([
'controller' => 'Blog',
'action' => 'view',
1,
'second',
'query' => 'string'
]);
$this->assertEquals('/blog/1-second?query=string', $result);
$result = $route->match([
'controller' => 'Blog',
'action' => 'view',
1 => 2,
2 => 'second'
]);
$this->assertFalse($result, 'Positional args must match exactly.');
}
/**
* Test that match() with pass and greedy routes.
*
* @return void
*/
public function testMatchWithPassOptionGreedy()
{
$route = new Route(
'/blog/:id-:slug/*',
['controller' => 'Blog', 'action' => 'view'],
['pass' => ['id', 'slug']]
);
$result = $route->match([
'controller' => 'Blog',
'action' => 'view',
'id' => 1,
'slug' => 'second',
'third',
'fourth',
'query' => 'string'
]);
$this->assertEquals('/blog/1-second/third/fourth?query=string', $result);
$result = $route->match([
'controller' => 'Blog',
'action' => 'view',
1,
'second',
'third',
'fourth',
'query' => 'string'
]);
$this->assertEquals('/blog/1-second/third/fourth?query=string', $result);
}
/**
* Test that extensions work.
*
* @return void
*/
public function testMatchWithExtension()
{
$route = new Route('/:controller/:action');
$result = $route->match([
'controller' => 'posts',
'action' => 'index',
'_ext' => 'json'
]);
$this->assertEquals('/posts/index.json', $result);
$route = new Route('/:controller/:action/*');
$result = $route->match([
'controller' => 'posts',
'action' => 'index',