RouteTest.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing\Route;
  16. use Cake\Core\Configure;
  17. use Cake\Http\ServerRequest;
  18. use Cake\Routing\Router;
  19. use Cake\Routing\Route\Route;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Used to expose protected methods for testing.
  23. */
  24. class RouteProtected extends Route
  25. {
  26. /**
  27. * @param $url
  28. * @return array
  29. */
  30. public function parseExtension($url)
  31. {
  32. return $this->_parseExtension($url);
  33. }
  34. }
  35. /**
  36. * Test case for Route
  37. */
  38. class RouteTest extends TestCase
  39. {
  40. /**
  41. * setUp method
  42. *
  43. * @return void
  44. */
  45. public function setUp()
  46. {
  47. parent::setUp();
  48. Configure::write('Routing', ['admin' => null, 'prefixes' => []]);
  49. }
  50. /**
  51. * Test the construction of a Route
  52. *
  53. * @return void
  54. */
  55. public function testConstruction()
  56. {
  57. $route = new Route('/:controller/:action/:id', [], ['id' => '[0-9]+']);
  58. $this->assertEquals('/:controller/:action/:id', $route->template);
  59. $this->assertEquals([], $route->defaults);
  60. $this->assertEquals(['id' => '[0-9]+', '_ext' => []], $route->options);
  61. $this->assertFalse($route->compiled());
  62. }
  63. /**
  64. * Test set middleware in the constructor
  65. *
  66. * @return void
  67. */
  68. public function testConstructorSetMiddleware()
  69. {
  70. $route = new Route('/:controller/:action/*', [], ['_middleware' => ['auth', 'cookie']]);
  71. $this->assertSame(['auth', 'cookie'], $route->getMiddleware());
  72. }
  73. /**
  74. * Test Route compiling.
  75. *
  76. * @return void
  77. */
  78. public function testBasicRouteCompiling()
  79. {
  80. $route = new Route('/', ['controller' => 'pages', 'action' => 'display', 'home']);
  81. $result = $route->compile();
  82. $expected = '#^/*$#';
  83. $this->assertEquals($expected, $result);
  84. $this->assertEquals([], $route->keys);
  85. $route = new Route('/:controller/:action', ['controller' => 'posts']);
  86. $result = $route->compile();
  87. $this->assertRegExp($result, '/posts/edit');
  88. $this->assertRegExp($result, '/posts/super_delete');
  89. $this->assertNotRegExp($result, '/posts');
  90. $this->assertNotRegExp($result, '/posts/super_delete/1');
  91. $this->assertSame($result, $route->compile());
  92. $route = new Route('/posts/foo:id', ['controller' => 'posts', 'action' => 'view']);
  93. $result = $route->compile();
  94. $this->assertRegExp($result, '/posts/foo:1');
  95. $this->assertRegExp($result, '/posts/foo:param');
  96. $this->assertNotRegExp($result, '/posts');
  97. $this->assertNotRegExp($result, '/posts/');
  98. $this->assertEquals(['id'], $route->keys);
  99. $route = new Route('/:plugin/:controller/:action/*', ['plugin' => 'test_plugin', 'action' => 'index']);
  100. $result = $route->compile();
  101. $this->assertRegExp($result, '/test_plugin/posts/index');
  102. $this->assertRegExp($result, '/test_plugin/posts/edit/5');
  103. $this->assertRegExp($result, '/test_plugin/posts/edit/5/name:value/nick:name');
  104. }
  105. /**
  106. * Test that single letter placeholders work.
  107. *
  108. * @return void
  109. */
  110. public function testRouteBuildingSmallPlaceholders()
  111. {
  112. $route = new Route(
  113. '/fighters/:id/move/:x/:y',
  114. ['controller' => 'Fighters', 'action' => 'move'],
  115. ['id' => '\d+', 'x' => '\d+', 'y' => '\d+', 'pass' => ['id', 'x', 'y']]
  116. );
  117. $pattern = $route->compile();
  118. $this->assertRegExp($pattern, '/fighters/123/move/8/42');
  119. $result = $route->match([
  120. 'controller' => 'Fighters',
  121. 'action' => 'move',
  122. 'id' => 123,
  123. 'x' => 8,
  124. 'y' => 42
  125. ]);
  126. $this->assertEquals('/fighters/123/move/8/42', $result);
  127. }
  128. /**
  129. * Test parsing routes with extensions.
  130. *
  131. * @return void
  132. */
  133. public function testRouteParsingWithExtensions()
  134. {
  135. $route = new Route(
  136. '/:controller/:action/*',
  137. [],
  138. ['_ext' => ['json', 'xml']]
  139. );
  140. $result = $route->parse('/posts/index', 'GET');
  141. $this->assertFalse(isset($result['_ext']));
  142. $result = $route->parse('/posts/index.pdf', 'GET');
  143. $this->assertFalse(isset($result['_ext']));
  144. $result = $route->setExtensions(['pdf', 'json', 'xml', 'xml.gz'])->parse('/posts/index.pdf', 'GET');
  145. $this->assertEquals('pdf', $result['_ext']);
  146. $result = $route->parse('/posts/index.json', 'GET');
  147. $this->assertEquals('json', $result['_ext']);
  148. $result = $route->parse('/posts/index.xml', 'GET');
  149. $this->assertEquals('xml', $result['_ext']);
  150. $result = $route->parse('/posts/index.xml.gz', 'GET');
  151. $this->assertEquals('xml.gz', $result['_ext']);
  152. }
  153. /**
  154. * @return array
  155. */
  156. public function provideMatchParseExtension()
  157. {
  158. return [
  159. ['/foo/bar.xml', ['/foo/bar', 'xml'], ['xml', 'json', 'xml.gz']],
  160. ['/foo/bar.json', ['/foo/bar', 'json'], ['xml', 'json', 'xml.gz']],
  161. ['/foo/bar.xml.gz', ['/foo/bar', 'xml.gz'], ['xml', 'json', 'xml.gz']],
  162. ['/foo/with.dots.json.xml.zip', ['/foo/with.dots.json.xml', 'zip'], ['zip']],
  163. ['/foo/confusing.extensions.dots.json.xml.zip', ['/foo/confusing.extensions.dots.json.xml', 'zip'], ['json', 'xml', 'zip']],
  164. ['/foo/confusing.extensions.dots.json.xml', ['/foo/confusing.extensions.dots.json', 'xml'], ['json', 'xml', 'zip']],
  165. ['/foo/confusing.extensions.dots.json', ['/foo/confusing.extensions.dots', 'json'], ['json', 'xml', 'zip']],
  166. ];
  167. }
  168. /**
  169. * Expects _parseExtension to match extensions in URLs
  170. *
  171. * @param string $url
  172. * @param array $expected
  173. * @param array $ext
  174. * @return void
  175. * @dataProvider provideMatchParseExtension
  176. */
  177. public function testMatchParseExtension($url, array $expected, array $ext)
  178. {
  179. $route = new RouteProtected('/:controller/:action/*', [], ['_ext' => $ext]);
  180. $result = $route->parseExtension($url);
  181. $this->assertEquals($expected, $result);
  182. }
  183. /**
  184. * @return array
  185. */
  186. public function provideNoMatchParseExtension()
  187. {
  188. return [
  189. ['/foo/bar', ['xml']],
  190. ['/foo/bar.zip', ['xml']],
  191. ['/foo/bar.xml.zip', ['xml']],
  192. ['/foo/bar.', ['xml']],
  193. ['/foo/bar.xml', []],
  194. ['/foo/bar...xml...zip...', ['xml']]
  195. ];
  196. }
  197. /**
  198. * Expects _parseExtension to not match extensions in URLs
  199. *
  200. * @param string $url
  201. * @param array $ext
  202. * @return void
  203. * @dataProvider provideNoMatchParseExtension
  204. */
  205. public function testNoMatchParseExtension($url, array $ext)
  206. {
  207. $route = new RouteProtected('/:controller/:action/*', [], ['_ext' => $ext]);
  208. list($outUrl, $outExt) = $route->parseExtension($url);
  209. $this->assertEquals($url, $outUrl);
  210. $this->assertNull($outExt);
  211. }
  212. /**
  213. * Expects extensions to be set
  214. *
  215. * @return void
  216. */
  217. public function testSetExtensions()
  218. {
  219. $route = new RouteProtected('/:controller/:action/*', []);
  220. $this->assertEquals([], $route->getExtensions());
  221. $route->setExtensions(['xml']);
  222. $this->assertEquals(['xml'], $route->getExtensions());
  223. $route->setExtensions(['xml', 'json', 'zip']);
  224. $this->assertEquals(['xml', 'json', 'zip'], $route->getExtensions());
  225. $route->setExtensions([]);
  226. $this->assertEquals([], $route->getExtensions());
  227. $route = new RouteProtected('/:controller/:action/*', [], ['_ext' => ['one', 'two']]);
  228. $this->assertEquals(['one', 'two'], $route->getExtensions());
  229. }
  230. /**
  231. * Expects extensions to be return.
  232. *
  233. * @return void
  234. */
  235. public function testGetExtensions()
  236. {
  237. $route = new RouteProtected('/:controller/:action/*', []);
  238. $this->assertEquals([], $route->getExtensions());
  239. $route = new RouteProtected('/:controller/:action/*', [], ['_ext' => ['one', 'two']]);
  240. $this->assertEquals(['one', 'two'], $route->getExtensions());
  241. $route = new RouteProtected('/:controller/:action/*', []);
  242. $this->assertEquals([], $route->getExtensions());
  243. $route->setExtensions(['xml', 'json', 'zip']);
  244. $this->assertEquals(['xml', 'json', 'zip'], $route->getExtensions());
  245. }
  246. /**
  247. * Test that route parameters that overlap don't cause errors.
  248. *
  249. * @return void
  250. */
  251. public function testRouteParameterOverlap()
  252. {
  253. $route = new Route('/invoices/add/:idd/:id', ['controller' => 'invoices', 'action' => 'add']);
  254. $result = $route->compile();
  255. $this->assertRegExp($result, '/invoices/add/1/3');
  256. $route = new Route('/invoices/add/:id/:idd', ['controller' => 'invoices', 'action' => 'add']);
  257. $result = $route->compile();
  258. $this->assertRegExp($result, '/invoices/add/1/3');
  259. }
  260. /**
  261. * Test compiling routes with keys that have patterns
  262. *
  263. * @return void
  264. */
  265. public function testRouteCompilingWithParamPatterns()
  266. {
  267. $route = new Route(
  268. '/:controller/:action/:id',
  269. [],
  270. ['id' => Router::ID]
  271. );
  272. $result = $route->compile();
  273. $this->assertRegExp($result, '/posts/edit/1');
  274. $this->assertRegExp($result, '/posts/view/518098');
  275. $this->assertNotRegExp($result, '/posts/edit/name-of-post');
  276. $this->assertNotRegExp($result, '/posts/edit/4/other:param');
  277. $this->assertEquals(['id', 'controller', 'action'], $route->keys);
  278. $route = new Route(
  279. '/:lang/:controller/:action/:id',
  280. ['controller' => 'testing4'],
  281. ['id' => Router::ID, 'lang' => '[a-z]{3}']
  282. );
  283. $result = $route->compile();
  284. $this->assertRegExp($result, '/eng/posts/edit/1');
  285. $this->assertRegExp($result, '/cze/articles/view/1');
  286. $this->assertNotRegExp($result, '/language/articles/view/2');
  287. $this->assertNotRegExp($result, '/eng/articles/view/name-of-article');
  288. $this->assertEquals(['lang', 'id', 'controller', 'action'], $route->keys);
  289. foreach ([':', '@', ';', '$', '-'] as $delim) {
  290. $route = new Route('/posts/:id' . $delim . ':title');
  291. $result = $route->compile();
  292. $this->assertRegExp($result, '/posts/1' . $delim . 'name-of-article');
  293. $this->assertRegExp($result, '/posts/13244' . $delim . 'name-of_Article[]');
  294. $this->assertNotRegExp($result, '/posts/11!nameofarticle');
  295. $this->assertNotRegExp($result, '/posts/11');
  296. $this->assertEquals(['title', 'id'], $route->keys);
  297. }
  298. $route = new Route(
  299. '/posts/:id::title/:year',
  300. ['controller' => 'posts', 'action' => 'view'],
  301. ['id' => Router::ID, 'year' => Router::YEAR, 'title' => '[a-z-_]+']
  302. );
  303. $result = $route->compile();
  304. $this->assertRegExp($result, '/posts/1:name-of-article/2009/');
  305. $this->assertRegExp($result, '/posts/13244:name-of-article/1999');
  306. $this->assertNotRegExp($result, '/posts/hey_now:nameofarticle');
  307. $this->assertNotRegExp($result, '/posts/:nameofarticle/2009');
  308. $this->assertNotRegExp($result, '/posts/:nameofarticle/01');
  309. $this->assertEquals(['year', 'title', 'id'], $route->keys);
  310. $route = new Route(
  311. '/posts/:url_title-(uuid::id)',
  312. ['controller' => 'posts', 'action' => 'view'],
  313. ['pass' => ['id', 'url_title'], 'id' => Router::ID]
  314. );
  315. $result = $route->compile();
  316. $this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)/');
  317. $this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)');
  318. $this->assertNotRegExp($result, '/posts/');
  319. $this->assertNotRegExp($result, '/posts/nameofarticle');
  320. $this->assertNotRegExp($result, '/posts/nameofarticle-12347');
  321. $this->assertEquals(['url_title', 'id'], $route->keys);
  322. }
  323. /**
  324. * Test route with unicode
  325. *
  326. * @return void
  327. */
  328. public function testRouteCompilingWithUnicodePatterns()
  329. {
  330. $route = new Route(
  331. '/test/:slug',
  332. ['controller' => 'Pages', 'action' => 'display'],
  333. ['pass' => ['slug'], 'multibytePattern' => false, 'slug' => '[A-zА-я\-\ ]+']
  334. );
  335. $result = $route->compile();
  336. $this->assertNotRegExp($result, '/test/bla-blan-тест');
  337. $route = new Route(
  338. '/test/:slug',
  339. ['controller' => 'Pages', 'action' => 'display'],
  340. ['pass' => ['slug'], 'multibytePattern' => true, 'slug' => '[A-zА-я\-\ ]+']
  341. );
  342. $result = $route->compile();
  343. $this->assertRegExp($result, '/test/bla-blan-тест');
  344. }
  345. /**
  346. * Test more complex route compiling & parsing with mid route greedy stars
  347. * and optional routing parameters
  348. *
  349. * @return void
  350. */
  351. public function testComplexRouteCompilingAndParsing()
  352. {
  353. $route = new Route(
  354. '/posts/:month/:day/:year/*',
  355. ['controller' => 'posts', 'action' => 'view'],
  356. ['year' => Router::YEAR, 'month' => Router::MONTH, 'day' => Router::DAY]
  357. );
  358. $result = $route->compile();
  359. $this->assertRegExp($result, '/posts/08/01/2007/title-of-post');
  360. $result = $route->parse('/posts/08/01/2007/title-of-post', 'GET');
  361. $this->assertCount(7, $result);
  362. $this->assertEquals($result['controller'], 'posts');
  363. $this->assertEquals($result['action'], 'view');
  364. $this->assertEquals($result['year'], '2007');
  365. $this->assertEquals($result['month'], '08');
  366. $this->assertEquals($result['day'], '01');
  367. $this->assertEquals($result['pass'][0], 'title-of-post');
  368. $this->assertEquals($result['_matchedRoute'], '/posts/:month/:day/:year/*');
  369. $route = new Route(
  370. '/:extra/page/:slug/*',
  371. ['controller' => 'pages', 'action' => 'view', 'extra' => null],
  372. ['extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view']
  373. );
  374. $result = $route->compile();
  375. $this->assertRegExp($result, '/some_extra/page/this_is_the_slug');
  376. $this->assertRegExp($result, '/page/this_is_the_slug');
  377. $this->assertEquals(['slug', 'extra'], $route->keys);
  378. $this->assertEquals(['extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view', '_ext' => []], $route->options);
  379. $expected = [
  380. 'controller' => 'pages',
  381. 'action' => 'view'
  382. ];
  383. $this->assertEquals($expected, $route->defaults);
  384. $route = new Route(
  385. '/:controller/:action/*',
  386. ['project' => false],
  387. [
  388. 'controller' => 'source|wiki|commits|tickets|comments|view',
  389. 'action' => 'branches|history|branch|logs|view|start|add|edit|modify'
  390. ]
  391. );
  392. $this->assertFalse($route->parse('/chaw_test/wiki', 'GET'));
  393. $result = $route->compile();
  394. $this->assertNotRegExp($result, '/some_project/source');
  395. $this->assertRegExp($result, '/source/view');
  396. $this->assertRegExp($result, '/source/view/other/params');
  397. $this->assertNotRegExp($result, '/chaw_test/wiki');
  398. $this->assertNotRegExp($result, '/source/wierd_action');
  399. }
  400. /**
  401. * Test that routes match their pattern.
  402. *
  403. * @return void
  404. */
  405. public function testMatchBasic()
  406. {
  407. $route = new Route('/:controller/:action/:id', ['plugin' => null]);
  408. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null]);
  409. $this->assertFalse($result);
  410. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 0]);
  411. $this->assertFalse($result);
  412. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1]);
  413. $this->assertEquals('/posts/view/1', $result);
  414. $route = new Route('/', ['controller' => 'pages', 'action' => 'display', 'home']);
  415. $result = $route->match(['controller' => 'pages', 'action' => 'display', 'home']);
  416. $this->assertEquals('/', $result);
  417. $result = $route->match(['controller' => 'pages', 'action' => 'display', 'about']);
  418. $this->assertFalse($result);
  419. $route = new Route('/pages/*', ['controller' => 'pages', 'action' => 'display']);
  420. $result = $route->match(['controller' => 'pages', 'action' => 'display', 'home']);
  421. $this->assertEquals('/pages/home', $result);
  422. $result = $route->match(['controller' => 'pages', 'action' => 'display', 'about']);
  423. $this->assertEquals('/pages/about', $result);
  424. $route = new Route('/blog/:action', ['controller' => 'posts']);
  425. $result = $route->match(['controller' => 'posts', 'action' => 'view']);
  426. $this->assertEquals('/blog/view', $result);
  427. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'id' => 2]);
  428. $this->assertEquals('/blog/view?id=2', $result);
  429. $result = $route->match(['controller' => 'nodes', 'action' => 'view']);
  430. $this->assertFalse($result);
  431. $result = $route->match(['controller' => 'posts', 'action' => 'view', 1]);
  432. $this->assertFalse($result);
  433. $route = new Route('/foo/:controller/:action', ['action' => 'index']);
  434. $result = $route->match(['controller' => 'posts', 'action' => 'view']);
  435. $this->assertEquals('/foo/posts/view', $result);
  436. $route = new Route('/:plugin/:id/*', ['controller' => 'posts', 'action' => 'view']);
  437. $result = $route->match(['plugin' => 'test', 'controller' => 'posts', 'action' => 'view', 'id' => '1']);
  438. $this->assertEquals('/test/1/', $result);
  439. $result = $route->match(['plugin' => 'fo', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0']);
  440. $this->assertEquals('/fo/1/0', $result);
  441. $result = $route->match(['plugin' => 'fo', 'controller' => 'nodes', 'action' => 'view', 'id' => 1]);
  442. $this->assertFalse($result);
  443. $result = $route->match(['plugin' => 'fo', 'controller' => 'posts', 'action' => 'edit', 'id' => 1]);
  444. $this->assertFalse($result);
  445. $route = new Route('/admin/subscriptions/:action/*', [
  446. 'controller' => 'subscribe', 'prefix' => 'admin'
  447. ]);
  448. $url = ['controller' => 'subscribe', 'prefix' => 'admin', 'action' => 'edit', 1];
  449. $result = $route->match($url);
  450. $expected = '/admin/subscriptions/edit/1';
  451. $this->assertEquals($expected, $result);
  452. $url = [
  453. 'controller' => 'subscribe',
  454. 'prefix' => 'admin',
  455. 'action' => 'edit_admin_e',
  456. 1
  457. ];
  458. $result = $route->match($url);
  459. $expected = '/admin/subscriptions/edit_admin_e/1';
  460. $this->assertEquals($expected, $result);
  461. }
  462. /**
  463. * Test match() with persist option
  464. *
  465. * @return void
  466. */
  467. public function testMatchWithPersistOption()
  468. {
  469. $context = [
  470. 'params' => ['lang' => 'en']
  471. ];
  472. $route = new Route('/:lang/:controller/:action', [], ['persist' => ['lang']]);
  473. $result = $route->match(
  474. ['controller' => 'tasks', 'action' => 'add'],
  475. $context
  476. );
  477. $this->assertEquals('/en/tasks/add', $result);
  478. }
  479. /**
  480. * Test match() with _host and other keys.
  481. *
  482. * @return void
  483. */
  484. public function testMatchWithHostKeys()
  485. {
  486. $context = [
  487. '_host' => 'foo.com',
  488. '_scheme' => 'http',
  489. '_port' => 80,
  490. '_base' => ''
  491. ];
  492. $route = new Route('/:controller/:action');
  493. $result = $route->match(
  494. ['controller' => 'posts', 'action' => 'index', '_host' => 'example.com'],
  495. $context
  496. );
  497. // Http has port 80 as default, do not include it in the url
  498. $this->assertEquals('http://example.com/posts/index', $result);
  499. $result = $route->match(
  500. ['controller' => 'posts', 'action' => 'index', '_scheme' => 'webcal'],
  501. $context
  502. );
  503. // Webcal is not on port 80 by default, include it in url
  504. $this->assertEquals('webcal://foo.com:80/posts/index', $result);
  505. $result = $route->match(
  506. ['controller' => 'posts', 'action' => 'index', '_port' => '8080'],
  507. $context
  508. );
  509. $this->assertEquals('http://foo.com:8080/posts/index', $result);
  510. $result = $route->match(
  511. ['controller' => 'posts', 'action' => 'index', '_base' => '/dir'],
  512. $context
  513. );
  514. $this->assertEquals('/dir/posts/index', $result);
  515. $result = $route->match(
  516. [
  517. 'controller' => 'posts',
  518. 'action' => 'index',
  519. '_port' => '8080',
  520. '_host' => 'example.com',
  521. '_scheme' => 'https',
  522. '_base' => '/dir'
  523. ],
  524. $context
  525. );
  526. $this->assertEquals('https://example.com:8080/dir/posts/index', $result);
  527. $context = [
  528. '_host' => 'foo.com',
  529. '_scheme' => 'http',
  530. '_port' => 8080,
  531. '_base' => ''
  532. ];
  533. $result = $route->match(
  534. [
  535. 'controller' => 'posts',
  536. 'action' => 'index',
  537. '_port' => '8080',
  538. '_host' => 'example.com',
  539. '_scheme' => 'https',
  540. '_base' => '/dir'
  541. ],
  542. $context
  543. );
  544. // Https scheme is not on port 8080 by default, include the port
  545. $this->assertEquals('https://example.com:8080/dir/posts/index', $result);
  546. }
  547. /**
  548. * Test that the _host option sets the default host.
  549. *
  550. * @return void
  551. */
  552. public function testMatchWithHostOption()
  553. {
  554. $route = new Route(
  555. '/fallback',
  556. ['controller' => 'Articles', 'action' => 'index'],
  557. ['_host' => 'www.example.com']
  558. );
  559. $result = $route->match([
  560. 'controller' => 'Articles',
  561. 'action' => 'index'
  562. ]);
  563. $this->assertSame('http://www.example.com/fallback', $result);
  564. }
  565. /**
  566. * Test wildcard host options
  567. *
  568. * @return void
  569. */
  570. public function testMatchWithHostWildcardOption()
  571. {
  572. $route = new Route(
  573. '/fallback',
  574. ['controller' => 'Articles', 'action' => 'index'],
  575. ['_host' => '*.example.com']
  576. );
  577. $result = $route->match([
  578. 'controller' => 'Articles',
  579. 'action' => 'index'
  580. ]);
  581. $this->assertFalse($result, 'No request context means no match');
  582. $result = $route->match([
  583. 'controller' => 'Articles',
  584. 'action' => 'index',
  585. ], ['_host' => 'wrong.com']);
  586. $this->assertFalse($result, 'Request context has bad host');
  587. $result = $route->match([
  588. 'controller' => 'Articles',
  589. 'action' => 'index',
  590. '_host' => 'wrong.com'
  591. ]);
  592. $this->assertFalse($result, 'Url param is wrong');
  593. $result = $route->match([
  594. 'controller' => 'Articles',
  595. 'action' => 'index',
  596. '_host' => 'foo.example.com'
  597. ]);
  598. $this->assertSame('http://foo.example.com/fallback', $result);
  599. $result = $route->match([
  600. 'controller' => 'Articles',
  601. 'action' => 'index',
  602. ], [
  603. '_host' => 'foo.example.com'
  604. ]);
  605. $this->assertSame('http://foo.example.com/fallback', $result);
  606. $result = $route->match([
  607. 'controller' => 'Articles',
  608. 'action' => 'index',
  609. ], [
  610. '_scheme' => 'https',
  611. '_host' => 'foo.example.com',
  612. '_port' => 8080
  613. ]);
  614. // When the port and scheme in the context are not present in the original url, they should be added
  615. $this->assertSame('https://foo.example.com:8080/fallback', $result);
  616. }
  617. /**
  618. * Test that non-greedy routes fail with extra passed args
  619. *
  620. * @return void
  621. */
  622. public function testMatchGreedyRouteFailurePassedArg()
  623. {
  624. $route = new Route('/:controller/:action', ['plugin' => null]);
  625. $result = $route->match(['controller' => 'posts', 'action' => 'view', '0']);
  626. $this->assertFalse($result);
  627. $route = new Route('/:controller/:action', ['plugin' => null]);
  628. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'test']);
  629. $this->assertFalse($result);
  630. }
  631. /**
  632. * Test that falsey values do not interrupt a match.
  633. *
  634. * @return void
  635. */
  636. public function testMatchWithFalseyValues()
  637. {
  638. $route = new Route('/:controller/:action/*', ['plugin' => null]);
  639. $result = $route->match([
  640. 'controller' => 'posts', 'action' => 'index', 'plugin' => null, 'admin' => false
  641. ]);
  642. $this->assertEquals('/posts/index/', $result);
  643. }
  644. /**
  645. * Test match() with greedy routes, and passed args.
  646. *
  647. * @return void
  648. */
  649. public function testMatchWithPassedArgs()
  650. {
  651. $route = new Route('/:controller/:action/*', ['plugin' => null]);
  652. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 5]);
  653. $this->assertEquals('/posts/view/5', $result);
  654. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 0]);
  655. $this->assertEquals('/posts/view/0', $result);
  656. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, '0']);
  657. $this->assertEquals('/posts/view/0', $result);
  658. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 'word space']);
  659. $this->assertEquals('/posts/view/word%20space', $result);
  660. $route = new Route('/test2/*', ['controller' => 'pages', 'action' => 'display', 2]);
  661. $result = $route->match(['controller' => 'pages', 'action' => 'display', 1]);
  662. $this->assertFalse($result);
  663. $result = $route->match(['controller' => 'pages', 'action' => 'display', 2, 'something']);
  664. $this->assertEquals('/test2/something', $result);
  665. $result = $route->match(['controller' => 'pages', 'action' => 'display', 5, 'something']);
  666. $this->assertFalse($result);
  667. }
  668. /**
  669. * Test that the pass option lets you use positional arguments for the
  670. * route elements that were named.
  671. *
  672. * @return void
  673. */
  674. public function testMatchWithPassOption()
  675. {
  676. $route = new Route(
  677. '/blog/:id-:slug',
  678. ['controller' => 'Blog', 'action' => 'view'],
  679. ['pass' => ['id', 'slug']]
  680. );
  681. $result = $route->match([
  682. 'controller' => 'Blog',
  683. 'action' => 'view',
  684. 'id' => 1,
  685. 'slug' => 'second'
  686. ]);
  687. $this->assertEquals('/blog/1-second', $result);
  688. $result = $route->match([
  689. 'controller' => 'Blog',
  690. 'action' => 'view',
  691. 1,
  692. 'second'
  693. ]);
  694. $this->assertEquals('/blog/1-second', $result);
  695. $result = $route->match([
  696. 'controller' => 'Blog',
  697. 'action' => 'view',
  698. 1,
  699. 'second',
  700. 'query' => 'string'
  701. ]);
  702. $this->assertEquals('/blog/1-second?query=string', $result);
  703. $result = $route->match([
  704. 'controller' => 'Blog',
  705. 'action' => 'view',
  706. 1 => 2,
  707. 2 => 'second'
  708. ]);
  709. $this->assertFalse($result, 'Positional args must match exactly.');
  710. }
  711. /**
  712. * Test that match() with pass and greedy routes.
  713. *
  714. * @return void
  715. */
  716. public function testMatchWithPassOptionGreedy()
  717. {
  718. $route = new Route(
  719. '/blog/:id-:slug/*',
  720. ['controller' => 'Blog', 'action' => 'view'],
  721. ['pass' => ['id', 'slug']]
  722. );
  723. $result = $route->match([
  724. 'controller' => 'Blog',
  725. 'action' => 'view',
  726. 'id' => 1,
  727. 'slug' => 'second',
  728. 'third',
  729. 'fourth',
  730. 'query' => 'string'
  731. ]);
  732. $this->assertEquals('/blog/1-second/third/fourth?query=string', $result);
  733. $result = $route->match([
  734. 'controller' => 'Blog',
  735. 'action' => 'view',
  736. 1,
  737. 'second',
  738. 'third',
  739. 'fourth',
  740. 'query' => 'string'
  741. ]);
  742. $this->assertEquals('/blog/1-second/third/fourth?query=string', $result);
  743. }
  744. /**
  745. * Test that extensions work.
  746. *
  747. * @return void
  748. */
  749. public function testMatchWithExtension()
  750. {
  751. $route = new Route('/:controller/:action');
  752. $result = $route->match([
  753. 'controller' => 'posts',
  754. 'action' => 'index',
  755. '_ext' => 'json'
  756. ]);
  757. $this->assertEquals('/posts/index.json', $result);
  758. $route = new Route('/:controller/:action/*');
  759. $result = $route->match([
  760. 'controller' => 'posts',
  761. 'action' => 'index',
  762. '_ext' => 'json',
  763. ]);
  764. $this->assertEquals('/posts/index.json', $result);
  765. $result = $route->match([
  766. 'controller' => 'posts',
  767. 'action' => 'view',
  768. 1,
  769. '_ext' => 'json',
  770. ]);
  771. $this->assertEquals('/posts/view/1.json', $result);
  772. $result = $route->match([
  773. 'controller' => 'posts',
  774. 'action' => 'view',
  775. 1,
  776. '_ext' => 'json',
  777. 'id' => 'b',
  778. 'c' => 'd'
  779. ]);
  780. $this->assertEquals('/posts/view/1.json?id=b&c=d', $result);
  781. $result = $route->match([
  782. 'controller' => 'posts',
  783. 'action' => 'index',
  784. '_ext' => 'json.gz',
  785. ]);
  786. $this->assertEquals('/posts/index.json.gz', $result);
  787. }
  788. /**
  789. * Test that match with patterns works.
  790. *
  791. * @return void
  792. */
  793. public function testMatchWithPatterns()
  794. {
  795. $route = new Route('/:controller/:action/:id', ['plugin' => null], ['id' => '[0-9]+']);
  796. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'id' => 'foo']);
  797. $this->assertFalse($result);
  798. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '9']);
  799. $this->assertEquals('/posts/view/9', $result);
  800. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '922']);
  801. $this->assertEquals('/posts/view/922', $result);
  802. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 'a99']);
  803. $this->assertFalse($result);
  804. }
  805. /**
  806. * Test that match() pulls out extra arguments as query string params.
  807. *
  808. * @return void
  809. */
  810. public function testMatchExtractQueryStringArgs()
  811. {
  812. $route = new Route('/:controller/:action/*');
  813. $result = $route->match([
  814. 'controller' => 'posts',
  815. 'action' => 'index',
  816. 'page' => 1
  817. ]);
  818. $this->assertEquals('/posts/index?page=1', $result);
  819. $result = $route->match([
  820. 'controller' => 'posts',
  821. 'action' => 'index',
  822. 'page' => 0
  823. ]);
  824. $this->assertEquals('/posts/index?page=0', $result);
  825. $result = $route->match([
  826. 'controller' => 'posts',
  827. 'action' => 'index',
  828. 1,
  829. 'page' => 1,
  830. 'dir' => 'desc',
  831. 'order' => 'title'
  832. ]);
  833. $this->assertEquals('/posts/index/1?page=1&dir=desc&order=title', $result);
  834. }
  835. /**
  836. * Test separartor.
  837. *
  838. * @return void
  839. */
  840. public function testQueryStringGeneration()
  841. {
  842. $route = new Route('/:controller/:action/*');
  843. $restore = ini_get('arg_separator.output');
  844. ini_set('arg_separator.output', '&amp;');
  845. $result = $route->match([
  846. 'controller' => 'posts',
  847. 'action' => 'index',
  848. 0,
  849. 'test' => 'var',
  850. 'var2' => 'test2',
  851. 'more' => 'test data'
  852. ]);
  853. $expected = '/posts/index/0?test=var&amp;var2=test2&amp;more=test+data';
  854. $this->assertEquals($expected, $result);
  855. ini_set('arg_separator.output', $restore);
  856. }
  857. /**
  858. * Ensure that parseRequest() calls parse() as that is required
  859. * for backwards compat
  860. *
  861. * @return void
  862. */
  863. public function testParseRequestDelegates()
  864. {
  865. $route = $this->getMockBuilder('Cake\Routing\Route\Route')
  866. ->setMethods(['parse'])
  867. ->setConstructorArgs(['/forward', ['controller' => 'Articles', 'action' => 'index']])
  868. ->getMock();
  869. $route->expects($this->once())
  870. ->method('parse')
  871. ->with('/forward', 'GET')
  872. ->will($this->returnValue('works!'));
  873. $request = new ServerRequest([
  874. 'environment' => [
  875. 'REQUEST_METHOD' => 'GET',
  876. 'PATH_INFO' => '/forward'
  877. ]
  878. ]);
  879. $result = $route->parseRequest($request);
  880. }
  881. /**
  882. * Test that parseRequest() applies host conditions
  883. *
  884. * @return void
  885. */
  886. public function testParseRequestHostConditions()
  887. {
  888. $route = new Route(
  889. '/fallback',
  890. ['controller' => 'Articles', 'action' => 'index'],
  891. ['_host' => '*.example.com']
  892. );
  893. $request = new ServerRequest([
  894. 'environment' => [
  895. 'HTTP_HOST' => 'a.example.com',
  896. 'PATH_INFO' => '/fallback'
  897. ]
  898. ]);
  899. $result = $route->parseRequest($request);
  900. $expected = [
  901. 'controller' => 'Articles',
  902. 'action' => 'index',
  903. 'pass' => [],
  904. '_matchedRoute' => '/fallback'
  905. ];
  906. $this->assertEquals($expected, $result, 'Should match, domain is correct');
  907. $request = new ServerRequest([
  908. 'environment' => [
  909. 'HTTP_HOST' => 'foo.bar.example.com',
  910. 'PATH_INFO' => '/fallback'
  911. ]
  912. ]);
  913. $result = $route->parseRequest($request);
  914. $this->assertEquals($expected, $result, 'Should match, domain is a matching subdomain');
  915. $request = new ServerRequest([
  916. 'environment' => [
  917. 'HTTP_HOST' => 'example.test.com',
  918. 'PATH_INFO' => '/fallback'
  919. ]
  920. ]);
  921. $this->assertFalse($route->parseRequest($request));
  922. }
  923. /**
  924. * test the parse method of Route.
  925. *
  926. * @return void
  927. */
  928. public function testParse()
  929. {
  930. $route = new Route(
  931. '/:controller/:action/:id',
  932. ['controller' => 'testing4', 'id' => null],
  933. ['id' => Router::ID]
  934. );
  935. $route->compile();
  936. $result = $route->parse('/posts/view/1', 'GET');
  937. $this->assertEquals('posts', $result['controller']);
  938. $this->assertEquals('view', $result['action']);
  939. $this->assertEquals('1', $result['id']);
  940. $route = new Route(
  941. '/admin/:controller',
  942. ['prefix' => 'admin', 'admin' => 1, 'action' => 'index']
  943. );
  944. $route->compile();
  945. $result = $route->parse('/admin/', 'GET');
  946. $this->assertFalse($result);
  947. $result = $route->parse('/admin/posts', 'GET');
  948. $this->assertEquals('posts', $result['controller']);
  949. $this->assertEquals('index', $result['action']);
  950. $route = new Route(
  951. '/media/search/*',
  952. ['controller' => 'Media', 'action' => 'search']
  953. );
  954. $result = $route->parse('/media/search', 'GET');
  955. $this->assertEquals('Media', $result['controller']);
  956. $this->assertEquals('search', $result['action']);
  957. $this->assertEquals([], $result['pass']);
  958. $result = $route->parse('/media/search/tv/shows', 'GET');
  959. $this->assertEquals('Media', $result['controller']);
  960. $this->assertEquals('search', $result['action']);
  961. $this->assertEquals(['tv', 'shows'], $result['pass']);
  962. }
  963. /**
  964. * Test that :key elements are urldecoded
  965. *
  966. * @return void
  967. */
  968. public function testParseUrlDecodeElements()
  969. {
  970. $route = new Route(
  971. '/:controller/:slug',
  972. ['action' => 'view']
  973. );
  974. $route->compile();
  975. $result = $route->parse('/posts/%E2%88%82%E2%88%82', 'GET');
  976. $this->assertEquals('posts', $result['controller']);
  977. $this->assertEquals('view', $result['action']);
  978. $this->assertEquals('∂∂', $result['slug']);
  979. $result = $route->parse('/posts/∂∂', 'GET');
  980. $this->assertEquals('posts', $result['controller']);
  981. $this->assertEquals('view', $result['action']);
  982. $this->assertEquals('∂∂', $result['slug']);
  983. }
  984. /**
  985. * Test numerically indexed defaults, get appended to pass
  986. *
  987. * @return void
  988. */
  989. public function testParseWithPassDefaults()
  990. {
  991. $route = new Route('/:controller', ['action' => 'display', 'home']);
  992. $result = $route->parse('/posts', 'GET');
  993. $expected = [
  994. 'controller' => 'posts',
  995. 'action' => 'display',
  996. 'pass' => ['home'],
  997. '_matchedRoute' => '/:controller'
  998. ];
  999. $this->assertEquals($expected, $result);
  1000. }
  1001. /**
  1002. * Test that middleware is returned from parse()
  1003. *
  1004. * @return void
  1005. */
  1006. public function testParseWithMiddleware()
  1007. {
  1008. $route = new Route('/:controller', ['action' => 'display', 'home']);
  1009. $route->setMiddleware(['auth', 'cookie']);
  1010. $result = $route->parse('/posts', 'GET');
  1011. $expected = [
  1012. 'controller' => 'posts',
  1013. 'action' => 'display',
  1014. 'pass' => ['home'],
  1015. '_matchedRoute' => '/:controller',
  1016. '_middleware' => ['auth', 'cookie'],
  1017. ];
  1018. $this->assertEquals($expected, $result);
  1019. }
  1020. /**
  1021. * Test that http header conditions can cause route failures.
  1022. *
  1023. * @return void
  1024. */
  1025. public function testParseWithHttpHeaderConditions()
  1026. {
  1027. $route = new Route('/sample', ['controller' => 'posts', 'action' => 'index', '_method' => 'POST']);
  1028. $this->assertFalse($route->parse('/sample', 'GET'));
  1029. $expected = [
  1030. 'controller' => 'posts',
  1031. 'action' => 'index',
  1032. 'pass' => [],
  1033. '_method' => 'POST',
  1034. '_matchedRoute' => '/sample'
  1035. ];
  1036. $this->assertEquals($expected, $route->parse('/sample', 'POST'));
  1037. }
  1038. /**
  1039. * Test that http header conditions can cause route failures.
  1040. *
  1041. * @return void
  1042. */
  1043. public function testParseWithMultipleHttpMethodConditions()
  1044. {
  1045. $route = new Route('/sample', [
  1046. 'controller' => 'posts',
  1047. 'action' => 'index',
  1048. '_method' => ['PUT', 'POST']
  1049. ]);
  1050. $this->assertFalse($route->parse('/sample', 'GET'));
  1051. // Test for deprecated behavior
  1052. $_SERVER['REQUEST_METHOD'] = 'POST';
  1053. $expected = [
  1054. 'controller' => 'posts',
  1055. 'action' => 'index',
  1056. 'pass' => [],
  1057. '_method' => ['PUT', 'POST'],
  1058. '_matchedRoute' => '/sample'
  1059. ];
  1060. $this->assertEquals($expected, $route->parse('/sample'));
  1061. }
  1062. /**
  1063. * Test that http header conditions can work with URL generation
  1064. *
  1065. * @return void
  1066. */
  1067. public function testMatchWithMultipleHttpMethodConditions()
  1068. {
  1069. $route = new Route('/sample', [
  1070. 'controller' => 'posts',
  1071. 'action' => 'index',
  1072. '_method' => ['PUT', 'POST']
  1073. ]);
  1074. $url = [
  1075. 'controller' => 'posts',
  1076. 'action' => 'index',
  1077. ];
  1078. $this->assertFalse($route->match($url));
  1079. $url = [
  1080. 'controller' => 'posts',
  1081. 'action' => 'index',
  1082. '_method' => 'GET',
  1083. ];
  1084. $this->assertFalse($route->match($url));
  1085. $url = [
  1086. 'controller' => 'posts',
  1087. 'action' => 'index',
  1088. '_method' => 'PUT',
  1089. ];
  1090. $this->assertEquals('/sample', $route->match($url));
  1091. $url = [
  1092. 'controller' => 'posts',
  1093. 'action' => 'index',
  1094. '_method' => 'POST',
  1095. ];
  1096. $this->assertEquals('/sample', $route->match($url));
  1097. $url = [
  1098. 'controller' => 'posts',
  1099. 'action' => 'index',
  1100. '_method' => ['PUT', 'POST'],
  1101. ];
  1102. $this->assertEquals('/sample', $route->match($url));
  1103. }
  1104. /**
  1105. * Check [method] compatibility.
  1106. *
  1107. * @return void
  1108. */
  1109. public function testMethodCompatibility()
  1110. {
  1111. $_SERVER['REQUEST_METHOD'] = 'POST';
  1112. $route = new Route('/sample', [
  1113. 'controller' => 'Articles',
  1114. 'action' => 'index',
  1115. '[method]' => 'POST',
  1116. ]);
  1117. $url = [
  1118. 'controller' => 'Articles',
  1119. 'action' => 'index',
  1120. '_method' => 'POST',
  1121. ];
  1122. $this->assertEquals('/sample', $route->match($url));
  1123. $url = [
  1124. 'controller' => 'Articles',
  1125. 'action' => 'index',
  1126. '[method]' => 'POST',
  1127. ];
  1128. $this->assertEquals('/sample', $route->match($url));
  1129. }
  1130. /**
  1131. * Test that patterns work for :action
  1132. *
  1133. * @return void
  1134. */
  1135. public function testPatternOnAction()
  1136. {
  1137. $route = new Route(
  1138. '/blog/:action/*',
  1139. ['controller' => 'blog_posts'],
  1140. ['action' => 'other|actions']
  1141. );
  1142. $result = $route->match(['controller' => 'blog_posts', 'action' => 'foo']);
  1143. $this->assertFalse($result);
  1144. $result = $route->match(['controller' => 'blog_posts', 'action' => 'actions']);
  1145. $this->assertNotEmpty($result);
  1146. $result = $route->parse('/blog/other', 'GET');
  1147. $expected = [
  1148. 'controller' => 'blog_posts',
  1149. 'action' => 'other',
  1150. 'pass' => [],
  1151. '_matchedRoute' => '/blog/:action/*'
  1152. ];
  1153. $this->assertEquals($expected, $result);
  1154. $result = $route->parse('/blog/foobar', 'GET');
  1155. $this->assertFalse($result);
  1156. }
  1157. /**
  1158. * Test the parseArgs method
  1159. *
  1160. * @return void
  1161. */
  1162. public function testParsePassedArgument()
  1163. {
  1164. $route = new Route('/:controller/:action/*');
  1165. $result = $route->parse('/posts/edit/1/2/0', 'GET');
  1166. $expected = [
  1167. 'controller' => 'posts',
  1168. 'action' => 'edit',
  1169. 'pass' => ['1', '2', '0'],
  1170. '_matchedRoute' => '/:controller/:action/*'
  1171. ];
  1172. $this->assertEquals($expected, $result);
  1173. }
  1174. /**
  1175. * Test matching of parameters where one parameter name starts with another parameter name
  1176. *
  1177. * @return void
  1178. */
  1179. public function testMatchSimilarParameters()
  1180. {
  1181. $route = new Route('/:thisParam/:thisParamIsLonger');
  1182. $url = [
  1183. 'thisParamIsLonger' => 'bar',
  1184. 'thisParam' => 'foo',
  1185. ];
  1186. $result = $route->match($url);
  1187. $expected = '/foo/bar';
  1188. $this->assertEquals($expected, $result);
  1189. }
  1190. /**
  1191. * Test match() with trailing ** style routes.
  1192. *
  1193. * @return void
  1194. */
  1195. public function testMatchTrailing()
  1196. {
  1197. $route = new Route('/pages/**', ['controller' => 'pages', 'action' => 'display']);
  1198. $id = 'test/ spaces/漢字/la†în';
  1199. $result = $route->match([
  1200. 'controller' => 'pages',
  1201. 'action' => 'display',
  1202. $id
  1203. ]);
  1204. $expected = '/pages/test/%20spaces/%E6%BC%A2%E5%AD%97/la%E2%80%A0%C3%AEn';
  1205. $this->assertEquals($expected, $result);
  1206. }
  1207. /**
  1208. * Test restructuring args with pass key
  1209. *
  1210. * @return void
  1211. */
  1212. public function testPassArgRestructure()
  1213. {
  1214. $route = new Route('/:controller/:action/:slug', [], [
  1215. 'pass' => ['slug']
  1216. ]);
  1217. $result = $route->parse('/posts/view/my-title', 'GET');
  1218. $expected = [
  1219. 'controller' => 'posts',
  1220. 'action' => 'view',
  1221. 'slug' => 'my-title',
  1222. 'pass' => ['my-title'],
  1223. '_matchedRoute' => '/:controller/:action/:slug'
  1224. ];
  1225. $this->assertEquals($expected, $result, 'Slug should have moved');
  1226. }
  1227. /**
  1228. * Test the /** special type on parsing.
  1229. *
  1230. * @return void
  1231. */
  1232. public function testParseTrailing()
  1233. {
  1234. $route = new Route('/:controller/:action/**');
  1235. $result = $route->parse('/posts/index/1/2/3/foo:bar', 'GET');
  1236. $expected = [
  1237. 'controller' => 'posts',
  1238. 'action' => 'index',
  1239. 'pass' => ['1/2/3/foo:bar'],
  1240. '_matchedRoute' => '/:controller/:action/**',
  1241. ];
  1242. $this->assertEquals($expected, $result);
  1243. $result = $route->parse('/posts/index/http://example.com', 'GET');
  1244. $expected = [
  1245. 'controller' => 'posts',
  1246. 'action' => 'index',
  1247. 'pass' => ['http://example.com'],
  1248. '_matchedRoute' => '/:controller/:action/**',
  1249. ];
  1250. $this->assertEquals($expected, $result);
  1251. }
  1252. /**
  1253. * Test the /** special type on parsing - UTF8.
  1254. *
  1255. * @return void
  1256. */
  1257. public function testParseTrailingUTF8()
  1258. {
  1259. $route = new Route('/category/**', ['controller' => 'categories', 'action' => 'index']);
  1260. $result = $route->parse('/category/%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84', 'GET');
  1261. $expected = [
  1262. 'controller' => 'categories',
  1263. 'action' => 'index',
  1264. 'pass' => ['موبایل'],
  1265. '_matchedRoute' => '/category/**',
  1266. ];
  1267. $this->assertEquals($expected, $result);
  1268. }
  1269. /**
  1270. * Test getName();
  1271. *
  1272. * @return void
  1273. */
  1274. public function testGetName()
  1275. {
  1276. $route = new Route('/foo/bar', [], ['_name' => 'testing']);
  1277. $this->assertEquals('', $route->getName());
  1278. $route = new Route('/:controller/:action');
  1279. $this->assertEquals('_controller:_action', $route->getName());
  1280. $route = new Route('/articles/:action', ['controller' => 'posts']);
  1281. $this->assertEquals('posts:_action', $route->getName());
  1282. $route = new Route('/articles/list', ['controller' => 'posts', 'action' => 'index']);
  1283. $this->assertEquals('posts:index', $route->getName());
  1284. $route = new Route('/:controller/:action', ['action' => 'index']);
  1285. $this->assertEquals('_controller:_action', $route->getName());
  1286. }
  1287. /**
  1288. * Test getName() with plugins.
  1289. *
  1290. * @return void
  1291. */
  1292. public function testGetNamePlugins()
  1293. {
  1294. $route = new Route(
  1295. '/a/:controller/:action',
  1296. ['plugin' => 'asset']
  1297. );
  1298. $this->assertEquals('asset._controller:_action', $route->getName());
  1299. $route = new Route(
  1300. '/a/assets/:action',
  1301. ['plugin' => 'asset', 'controller' => 'assets']
  1302. );
  1303. $this->assertEquals('asset.assets:_action', $route->getName());
  1304. $route = new Route(
  1305. '/assets/get',
  1306. ['plugin' => 'asset', 'controller' => 'assets', 'action' => 'get']
  1307. );
  1308. $this->assertEquals('asset.assets:get', $route->getName());
  1309. }
  1310. /**
  1311. * Test getName() with prefixes.
  1312. *
  1313. * @return void
  1314. */
  1315. public function testGetNamePrefix()
  1316. {
  1317. $route = new Route(
  1318. '/admin/:controller/:action',
  1319. ['prefix' => 'admin']
  1320. );
  1321. $this->assertEquals('admin:_controller:_action', $route->getName());
  1322. $route = new Route(
  1323. '/:prefix/assets/:action',
  1324. ['controller' => 'assets']
  1325. );
  1326. $this->assertEquals('_prefix:assets:_action', $route->getName());
  1327. $route = new Route(
  1328. '/admin/assets/get',
  1329. ['prefix' => 'admin', 'plugin' => 'asset', 'controller' => 'assets', 'action' => 'get']
  1330. );
  1331. $this->assertEquals('admin:asset.assets:get', $route->getName());
  1332. $route = new Route(
  1333. '/:prefix/:plugin/:controller/:action/*',
  1334. []
  1335. );
  1336. $this->assertEquals('_prefix:_plugin._controller:_action', $route->getName());
  1337. }
  1338. /**
  1339. * Test that utf-8 patterns work for :section
  1340. *
  1341. * @return void
  1342. */
  1343. public function testUTF8PatternOnSection()
  1344. {
  1345. $route = new Route(
  1346. '/:section',
  1347. ['plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index'],
  1348. [
  1349. 'persist' => ['section'],
  1350. 'section' => 'آموزش|weblog'
  1351. ]
  1352. );
  1353. $result = $route->parse('/%D8%A2%D9%85%D9%88%D8%B2%D8%B4', 'GET');
  1354. $expected = [
  1355. 'section' => 'آموزش',
  1356. 'plugin' => 'blogs',
  1357. 'controller' => 'posts',
  1358. 'action' => 'index',
  1359. 'pass' => [],
  1360. '_matchedRoute' => '/:section',
  1361. ];
  1362. $this->assertEquals($expected, $result);
  1363. $result = $route->parse('/weblog', 'GET');
  1364. $expected = [
  1365. 'section' => 'weblog',
  1366. 'plugin' => 'blogs',
  1367. 'controller' => 'posts',
  1368. 'action' => 'index',
  1369. 'pass' => [],
  1370. '_matchedRoute' => '/:section',
  1371. ];
  1372. $this->assertEquals($expected, $result);
  1373. }
  1374. /**
  1375. * Test getting the static path for a route.
  1376. *
  1377. * @return void
  1378. */
  1379. public function testStaticPath()
  1380. {
  1381. $route = new Route('/*', ['controller' => 'Pages', 'action' => 'display']);
  1382. $this->assertEquals('/', $route->staticPath());
  1383. $route = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  1384. $this->assertEquals('/pages', $route->staticPath());
  1385. $route = new Route('/pages/:id/*', ['controller' => 'Pages', 'action' => 'display']);
  1386. $this->assertEquals('/pages/', $route->staticPath());
  1387. $route = new Route('/:controller/:action/*');
  1388. $this->assertEquals('/', $route->staticPath());
  1389. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1390. $this->assertEquals('/books/reviews', $route->staticPath());
  1391. }
  1392. /**
  1393. * Test for __set_state magic method on CakeRoute
  1394. *
  1395. * @return void
  1396. */
  1397. public function testSetState()
  1398. {
  1399. $route = Route::__set_state([
  1400. 'keys' => [],
  1401. 'options' => [],
  1402. 'defaults' => [
  1403. 'controller' => 'pages',
  1404. 'action' => 'display',
  1405. 'home',
  1406. ],
  1407. 'template' => '/',
  1408. '_greedy' => false,
  1409. '_compiledRoute' => null,
  1410. ]);
  1411. $this->assertInstanceOf('Cake\Routing\Route\Route', $route);
  1412. $this->assertSame('/', $route->match(['controller' => 'pages', 'action' => 'display', 'home']));
  1413. $this->assertFalse($route->match(['controller' => 'pages', 'action' => 'display', 'about']));
  1414. $expected = [
  1415. 'controller' => 'pages',
  1416. 'action' => 'display',
  1417. 'pass' => ['home'],
  1418. '_matchedRoute' => '/',
  1419. ];
  1420. $this->assertEquals($expected, $route->parse('/', 'GET'));
  1421. }
  1422. /**
  1423. * Test setting the method on a route.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testSetMethods()
  1428. {
  1429. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1430. $result = $route->setMethods(['put']);
  1431. $this->assertSame($result, $route, 'Should return this');
  1432. $this->assertSame(['PUT'], $route->defaults['_method'], 'method is wrong');
  1433. $route->setMethods(['post', 'get', 'patch']);
  1434. $this->assertSame(['POST', 'GET', 'PATCH'], $route->defaults['_method']);
  1435. }
  1436. /**
  1437. * Test setting the method on a route to an invalid method
  1438. *
  1439. * @return void
  1440. */
  1441. public function testSetMethodsInvalid()
  1442. {
  1443. $this->expectException(\InvalidArgumentException::class);
  1444. $this->expectExceptionMessage('Invalid HTTP method received. NOPE is invalid');
  1445. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1446. $route->setMethods(['nope']);
  1447. }
  1448. /**
  1449. * Test setting patterns through the method
  1450. *
  1451. * @return void
  1452. */
  1453. public function testSetPatterns()
  1454. {
  1455. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1456. $result = $route->setPatterns([
  1457. 'date' => '\d+\-\d+\-\d+',
  1458. 'id' => '[a-z]+'
  1459. ]);
  1460. $this->assertSame($result, $route, 'Should return this');
  1461. $this->assertArrayHasKey('id', $route->options);
  1462. $this->assertArrayHasKey('date', $route->options);
  1463. $this->assertSame('[a-z]+', $route->options['id']);
  1464. $this->assertArrayNotHasKey('multibytePattern', $route->options);
  1465. $this->assertFalse($route->parse('/reviews/a-b-c/xyz'));
  1466. $this->assertNotEmpty($route->parse('/reviews/2016-05-12/xyz'));
  1467. }
  1468. /**
  1469. * Test setting patterns enables multibyte mode
  1470. *
  1471. * @return void
  1472. */
  1473. public function testSetPatternsMultibyte()
  1474. {
  1475. $route = new Route('/reviews/:accountid/:slug', ['controller' => 'Reviews', 'action' => 'view']);
  1476. $result = $route->setPatterns([
  1477. 'date' => '[A-zА-я\-\ ]+',
  1478. 'accountid' => '[a-z]+'
  1479. ]);
  1480. $this->assertArrayHasKey('multibytePattern', $route->options);
  1481. $this->assertNotEmpty($route->parse('/reviews/abcs/bla-blan-тест'));
  1482. }
  1483. /**
  1484. * Test setting host requirements
  1485. *
  1486. * @return void
  1487. */
  1488. public function testSetHost()
  1489. {
  1490. $route = new Route('/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1491. $result = $route->setHost('blog.example.com');
  1492. $this->assertSame($result, $route, 'Should return this');
  1493. $request = new ServerRequest([
  1494. 'environment' => [
  1495. 'HTTP_HOST' => 'a.example.com',
  1496. 'PATH_INFO' => '/reviews'
  1497. ]
  1498. ]);
  1499. $this->assertFalse($route->parseRequest($request));
  1500. $uri = $request->getUri();
  1501. $request = $request->withUri($uri->withHost('blog.example.com'));
  1502. $this->assertNotEmpty($route->parseRequest($request));
  1503. }
  1504. /**
  1505. * Test setting pass parameters
  1506. *
  1507. * @return void
  1508. */
  1509. public function testSetPass()
  1510. {
  1511. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1512. $result = $route->setPass(['date', 'id']);
  1513. $this->assertSame($result, $route, 'Should return this');
  1514. $this->assertEquals(['date', 'id'], $route->options['pass']);
  1515. }
  1516. /**
  1517. * Test setting persisted parameters
  1518. *
  1519. * @return void
  1520. */
  1521. public function testSetPersist()
  1522. {
  1523. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1524. $result = $route->setPersist(['date']);
  1525. $this->assertSame($result, $route, 'Should return this');
  1526. $this->assertEquals(['date'], $route->options['persist']);
  1527. }
  1528. /**
  1529. * Test setting/getting middleware.
  1530. *
  1531. * @return void
  1532. */
  1533. public function testSetMiddleware()
  1534. {
  1535. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1536. $result = $route->setMiddleware(['auth', 'cookie']);
  1537. $this->assertSame($result, $route);
  1538. $this->assertSame(['auth', 'cookie'], $route->getMiddleware());
  1539. }
  1540. }