RouteTest.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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. $this->assertEquals('http://example.com/posts/index', $result);
  498. $result = $route->match(
  499. ['controller' => 'posts', 'action' => 'index', '_scheme' => 'webcal'],
  500. $context
  501. );
  502. $this->assertEquals('webcal://foo.com/posts/index', $result);
  503. $result = $route->match(
  504. ['controller' => 'posts', 'action' => 'index', '_port' => '8080'],
  505. $context
  506. );
  507. $this->assertEquals('http://foo.com:8080/posts/index', $result);
  508. $result = $route->match(
  509. ['controller' => 'posts', 'action' => 'index', '_base' => '/dir'],
  510. $context
  511. );
  512. $this->assertEquals('/dir/posts/index', $result);
  513. $result = $route->match(
  514. [
  515. 'controller' => 'posts',
  516. 'action' => 'index',
  517. '_port' => '8080',
  518. '_host' => 'example.com',
  519. '_scheme' => 'https',
  520. '_base' => '/dir'
  521. ],
  522. $context
  523. );
  524. $this->assertEquals('https://example.com:8080/dir/posts/index', $result);
  525. }
  526. /**
  527. * Test that the _host option sets the default host.
  528. *
  529. * @return void
  530. */
  531. public function testMatchWithHostOption()
  532. {
  533. $route = new Route(
  534. '/fallback',
  535. ['controller' => 'Articles', 'action' => 'index'],
  536. ['_host' => 'www.example.com']
  537. );
  538. $result = $route->match([
  539. 'controller' => 'Articles',
  540. 'action' => 'index'
  541. ]);
  542. $this->assertSame('http://www.example.com/fallback', $result);
  543. }
  544. /**
  545. * Test wildcard host options
  546. *
  547. * @return void
  548. */
  549. public function testMatchWithHostWildcardOption()
  550. {
  551. $route = new Route(
  552. '/fallback',
  553. ['controller' => 'Articles', 'action' => 'index'],
  554. ['_host' => '*.example.com']
  555. );
  556. $result = $route->match([
  557. 'controller' => 'Articles',
  558. 'action' => 'index'
  559. ]);
  560. $this->assertFalse($result, 'No request context means no match');
  561. $result = $route->match([
  562. 'controller' => 'Articles',
  563. 'action' => 'index',
  564. ], ['_host' => 'wrong.com']);
  565. $this->assertFalse($result, 'Request context has bad host');
  566. $result = $route->match([
  567. 'controller' => 'Articles',
  568. 'action' => 'index',
  569. '_host' => 'wrong.com'
  570. ]);
  571. $this->assertFalse($result, 'Url param is wrong');
  572. $result = $route->match([
  573. 'controller' => 'Articles',
  574. 'action' => 'index',
  575. '_host' => 'foo.example.com'
  576. ]);
  577. $this->assertSame('http://foo.example.com/fallback', $result);
  578. $result = $route->match([
  579. 'controller' => 'Articles',
  580. 'action' => 'index',
  581. ], [
  582. '_host' => 'foo.example.com'
  583. ]);
  584. $this->assertSame('http://foo.example.com/fallback', $result);
  585. }
  586. /**
  587. * Test that non-greedy routes fail with extra passed args
  588. *
  589. * @return void
  590. */
  591. public function testMatchGreedyRouteFailurePassedArg()
  592. {
  593. $route = new Route('/:controller/:action', ['plugin' => null]);
  594. $result = $route->match(['controller' => 'posts', 'action' => 'view', '0']);
  595. $this->assertFalse($result);
  596. $route = new Route('/:controller/:action', ['plugin' => null]);
  597. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'test']);
  598. $this->assertFalse($result);
  599. }
  600. /**
  601. * Test that falsey values do not interrupt a match.
  602. *
  603. * @return void
  604. */
  605. public function testMatchWithFalseyValues()
  606. {
  607. $route = new Route('/:controller/:action/*', ['plugin' => null]);
  608. $result = $route->match([
  609. 'controller' => 'posts', 'action' => 'index', 'plugin' => null, 'admin' => false
  610. ]);
  611. $this->assertEquals('/posts/index/', $result);
  612. }
  613. /**
  614. * Test match() with greedy routes, and passed args.
  615. *
  616. * @return void
  617. */
  618. public function testMatchWithPassedArgs()
  619. {
  620. $route = new Route('/:controller/:action/*', ['plugin' => null]);
  621. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 5]);
  622. $this->assertEquals('/posts/view/5', $result);
  623. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 0]);
  624. $this->assertEquals('/posts/view/0', $result);
  625. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, '0']);
  626. $this->assertEquals('/posts/view/0', $result);
  627. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 'word space']);
  628. $this->assertEquals('/posts/view/word%20space', $result);
  629. $route = new Route('/test2/*', ['controller' => 'pages', 'action' => 'display', 2]);
  630. $result = $route->match(['controller' => 'pages', 'action' => 'display', 1]);
  631. $this->assertFalse($result);
  632. $result = $route->match(['controller' => 'pages', 'action' => 'display', 2, 'something']);
  633. $this->assertEquals('/test2/something', $result);
  634. $result = $route->match(['controller' => 'pages', 'action' => 'display', 5, 'something']);
  635. $this->assertFalse($result);
  636. }
  637. /**
  638. * Test that the pass option lets you use positional arguments for the
  639. * route elements that were named.
  640. *
  641. * @return void
  642. */
  643. public function testMatchWithPassOption()
  644. {
  645. $route = new Route(
  646. '/blog/:id-:slug',
  647. ['controller' => 'Blog', 'action' => 'view'],
  648. ['pass' => ['id', 'slug']]
  649. );
  650. $result = $route->match([
  651. 'controller' => 'Blog',
  652. 'action' => 'view',
  653. 'id' => 1,
  654. 'slug' => 'second'
  655. ]);
  656. $this->assertEquals('/blog/1-second', $result);
  657. $result = $route->match([
  658. 'controller' => 'Blog',
  659. 'action' => 'view',
  660. 1,
  661. 'second'
  662. ]);
  663. $this->assertEquals('/blog/1-second', $result);
  664. $result = $route->match([
  665. 'controller' => 'Blog',
  666. 'action' => 'view',
  667. 1,
  668. 'second',
  669. 'query' => 'string'
  670. ]);
  671. $this->assertEquals('/blog/1-second?query=string', $result);
  672. $result = $route->match([
  673. 'controller' => 'Blog',
  674. 'action' => 'view',
  675. 1 => 2,
  676. 2 => 'second'
  677. ]);
  678. $this->assertFalse($result, 'Positional args must match exactly.');
  679. }
  680. /**
  681. * Test that match() with pass and greedy routes.
  682. *
  683. * @return void
  684. */
  685. public function testMatchWithPassOptionGreedy()
  686. {
  687. $route = new Route(
  688. '/blog/:id-:slug/*',
  689. ['controller' => 'Blog', 'action' => 'view'],
  690. ['pass' => ['id', 'slug']]
  691. );
  692. $result = $route->match([
  693. 'controller' => 'Blog',
  694. 'action' => 'view',
  695. 'id' => 1,
  696. 'slug' => 'second',
  697. 'third',
  698. 'fourth',
  699. 'query' => 'string'
  700. ]);
  701. $this->assertEquals('/blog/1-second/third/fourth?query=string', $result);
  702. $result = $route->match([
  703. 'controller' => 'Blog',
  704. 'action' => 'view',
  705. 1,
  706. 'second',
  707. 'third',
  708. 'fourth',
  709. 'query' => 'string'
  710. ]);
  711. $this->assertEquals('/blog/1-second/third/fourth?query=string', $result);
  712. }
  713. /**
  714. * Test that extensions work.
  715. *
  716. * @return void
  717. */
  718. public function testMatchWithExtension()
  719. {
  720. $route = new Route('/:controller/:action');
  721. $result = $route->match([
  722. 'controller' => 'posts',
  723. 'action' => 'index',
  724. '_ext' => 'json'
  725. ]);
  726. $this->assertEquals('/posts/index.json', $result);
  727. $route = new Route('/:controller/:action/*');
  728. $result = $route->match([
  729. 'controller' => 'posts',
  730. 'action' => 'index',
  731. '_ext' => 'json',
  732. ]);
  733. $this->assertEquals('/posts/index.json', $result);
  734. $result = $route->match([
  735. 'controller' => 'posts',
  736. 'action' => 'view',
  737. 1,
  738. '_ext' => 'json',
  739. ]);
  740. $this->assertEquals('/posts/view/1.json', $result);
  741. $result = $route->match([
  742. 'controller' => 'posts',
  743. 'action' => 'view',
  744. 1,
  745. '_ext' => 'json',
  746. 'id' => 'b',
  747. 'c' => 'd'
  748. ]);
  749. $this->assertEquals('/posts/view/1.json?id=b&c=d', $result);
  750. $result = $route->match([
  751. 'controller' => 'posts',
  752. 'action' => 'index',
  753. '_ext' => 'json.gz',
  754. ]);
  755. $this->assertEquals('/posts/index.json.gz', $result);
  756. }
  757. /**
  758. * Test that match with patterns works.
  759. *
  760. * @return void
  761. */
  762. public function testMatchWithPatterns()
  763. {
  764. $route = new Route('/:controller/:action/:id', ['plugin' => null], ['id' => '[0-9]+']);
  765. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'id' => 'foo']);
  766. $this->assertFalse($result);
  767. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '9']);
  768. $this->assertEquals('/posts/view/9', $result);
  769. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '922']);
  770. $this->assertEquals('/posts/view/922', $result);
  771. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 'a99']);
  772. $this->assertFalse($result);
  773. }
  774. /**
  775. * Test that match() pulls out extra arguments as query string params.
  776. *
  777. * @return void
  778. */
  779. public function testMatchExtractQueryStringArgs()
  780. {
  781. $route = new Route('/:controller/:action/*');
  782. $result = $route->match([
  783. 'controller' => 'posts',
  784. 'action' => 'index',
  785. 'page' => 1
  786. ]);
  787. $this->assertEquals('/posts/index?page=1', $result);
  788. $result = $route->match([
  789. 'controller' => 'posts',
  790. 'action' => 'index',
  791. 'page' => 0
  792. ]);
  793. $this->assertEquals('/posts/index?page=0', $result);
  794. $result = $route->match([
  795. 'controller' => 'posts',
  796. 'action' => 'index',
  797. 1,
  798. 'page' => 1,
  799. 'dir' => 'desc',
  800. 'order' => 'title'
  801. ]);
  802. $this->assertEquals('/posts/index/1?page=1&dir=desc&order=title', $result);
  803. }
  804. /**
  805. * Test separartor.
  806. *
  807. * @return void
  808. */
  809. public function testQueryStringGeneration()
  810. {
  811. $route = new Route('/:controller/:action/*');
  812. $restore = ini_get('arg_separator.output');
  813. ini_set('arg_separator.output', '&amp;');
  814. $result = $route->match([
  815. 'controller' => 'posts',
  816. 'action' => 'index',
  817. 0,
  818. 'test' => 'var',
  819. 'var2' => 'test2',
  820. 'more' => 'test data'
  821. ]);
  822. $expected = '/posts/index/0?test=var&amp;var2=test2&amp;more=test+data';
  823. $this->assertEquals($expected, $result);
  824. ini_set('arg_separator.output', $restore);
  825. }
  826. /**
  827. * Ensure that parseRequest() calls parse() as that is required
  828. * for backwards compat
  829. *
  830. * @return void
  831. */
  832. public function testParseRequestDelegates()
  833. {
  834. $route = $this->getMockBuilder('Cake\Routing\Route\Route')
  835. ->setMethods(['parse'])
  836. ->setConstructorArgs(['/forward', ['controller' => 'Articles', 'action' => 'index']])
  837. ->getMock();
  838. $route->expects($this->once())
  839. ->method('parse')
  840. ->with('/forward', 'GET')
  841. ->will($this->returnValue('works!'));
  842. $request = new ServerRequest([
  843. 'environment' => [
  844. 'REQUEST_METHOD' => 'GET',
  845. 'PATH_INFO' => '/forward'
  846. ]
  847. ]);
  848. $result = $route->parseRequest($request);
  849. }
  850. /**
  851. * Test that parseRequest() applies host conditions
  852. *
  853. * @return void
  854. */
  855. public function testParseRequestHostConditions()
  856. {
  857. $route = new Route(
  858. '/fallback',
  859. ['controller' => 'Articles', 'action' => 'index'],
  860. ['_host' => '*.example.com']
  861. );
  862. $request = new ServerRequest([
  863. 'environment' => [
  864. 'HTTP_HOST' => 'a.example.com',
  865. 'PATH_INFO' => '/fallback'
  866. ]
  867. ]);
  868. $result = $route->parseRequest($request);
  869. $expected = [
  870. 'controller' => 'Articles',
  871. 'action' => 'index',
  872. 'pass' => [],
  873. '_matchedRoute' => '/fallback'
  874. ];
  875. $this->assertEquals($expected, $result, 'Should match, domain is correct');
  876. $request = new ServerRequest([
  877. 'environment' => [
  878. 'HTTP_HOST' => 'foo.bar.example.com',
  879. 'PATH_INFO' => '/fallback'
  880. ]
  881. ]);
  882. $result = $route->parseRequest($request);
  883. $this->assertEquals($expected, $result, 'Should match, domain is a matching subdomain');
  884. $request = new ServerRequest([
  885. 'environment' => [
  886. 'HTTP_HOST' => 'example.test.com',
  887. 'PATH_INFO' => '/fallback'
  888. ]
  889. ]);
  890. $this->assertFalse($route->parseRequest($request));
  891. }
  892. /**
  893. * test the parse method of Route.
  894. *
  895. * @return void
  896. */
  897. public function testParse()
  898. {
  899. $route = new Route(
  900. '/:controller/:action/:id',
  901. ['controller' => 'testing4', 'id' => null],
  902. ['id' => Router::ID]
  903. );
  904. $route->compile();
  905. $result = $route->parse('/posts/view/1', 'GET');
  906. $this->assertEquals('posts', $result['controller']);
  907. $this->assertEquals('view', $result['action']);
  908. $this->assertEquals('1', $result['id']);
  909. $route = new Route(
  910. '/admin/:controller',
  911. ['prefix' => 'admin', 'admin' => 1, 'action' => 'index']
  912. );
  913. $route->compile();
  914. $result = $route->parse('/admin/', 'GET');
  915. $this->assertFalse($result);
  916. $result = $route->parse('/admin/posts', 'GET');
  917. $this->assertEquals('posts', $result['controller']);
  918. $this->assertEquals('index', $result['action']);
  919. $route = new Route(
  920. '/media/search/*',
  921. ['controller' => 'Media', 'action' => 'search']
  922. );
  923. $result = $route->parse('/media/search', 'GET');
  924. $this->assertEquals('Media', $result['controller']);
  925. $this->assertEquals('search', $result['action']);
  926. $this->assertEquals([], $result['pass']);
  927. $result = $route->parse('/media/search/tv/shows', 'GET');
  928. $this->assertEquals('Media', $result['controller']);
  929. $this->assertEquals('search', $result['action']);
  930. $this->assertEquals(['tv', 'shows'], $result['pass']);
  931. }
  932. /**
  933. * Test that :key elements are urldecoded
  934. *
  935. * @return void
  936. */
  937. public function testParseUrlDecodeElements()
  938. {
  939. $route = new Route(
  940. '/:controller/:slug',
  941. ['action' => 'view']
  942. );
  943. $route->compile();
  944. $result = $route->parse('/posts/%E2%88%82%E2%88%82', 'GET');
  945. $this->assertEquals('posts', $result['controller']);
  946. $this->assertEquals('view', $result['action']);
  947. $this->assertEquals('∂∂', $result['slug']);
  948. $result = $route->parse('/posts/∂∂', 'GET');
  949. $this->assertEquals('posts', $result['controller']);
  950. $this->assertEquals('view', $result['action']);
  951. $this->assertEquals('∂∂', $result['slug']);
  952. }
  953. /**
  954. * Test numerically indexed defaults, get appended to pass
  955. *
  956. * @return void
  957. */
  958. public function testParseWithPassDefaults()
  959. {
  960. $route = new Route('/:controller', ['action' => 'display', 'home']);
  961. $result = $route->parse('/posts', 'GET');
  962. $expected = [
  963. 'controller' => 'posts',
  964. 'action' => 'display',
  965. 'pass' => ['home'],
  966. '_matchedRoute' => '/:controller'
  967. ];
  968. $this->assertEquals($expected, $result);
  969. }
  970. /**
  971. * Test that middleware is returned from parse()
  972. *
  973. * @return void
  974. */
  975. public function testParseWithMiddleware()
  976. {
  977. $route = new Route('/:controller', ['action' => 'display', 'home']);
  978. $route->setMiddleware(['auth', 'cookie']);
  979. $result = $route->parse('/posts', 'GET');
  980. $expected = [
  981. 'controller' => 'posts',
  982. 'action' => 'display',
  983. 'pass' => ['home'],
  984. '_matchedRoute' => '/:controller',
  985. '_middleware' => ['auth', 'cookie'],
  986. ];
  987. $this->assertEquals($expected, $result);
  988. }
  989. /**
  990. * Test that http header conditions can cause route failures.
  991. *
  992. * @return void
  993. */
  994. public function testParseWithHttpHeaderConditions()
  995. {
  996. $route = new Route('/sample', ['controller' => 'posts', 'action' => 'index', '_method' => 'POST']);
  997. $this->assertFalse($route->parse('/sample', 'GET'));
  998. $expected = [
  999. 'controller' => 'posts',
  1000. 'action' => 'index',
  1001. 'pass' => [],
  1002. '_method' => 'POST',
  1003. '_matchedRoute' => '/sample'
  1004. ];
  1005. $this->assertEquals($expected, $route->parse('/sample', 'POST'));
  1006. }
  1007. /**
  1008. * Test that http header conditions can cause route failures.
  1009. *
  1010. * @return void
  1011. */
  1012. public function testParseWithMultipleHttpMethodConditions()
  1013. {
  1014. $route = new Route('/sample', [
  1015. 'controller' => 'posts',
  1016. 'action' => 'index',
  1017. '_method' => ['PUT', 'POST']
  1018. ]);
  1019. $this->assertFalse($route->parse('/sample', 'GET'));
  1020. // Test for deprecated behavior
  1021. $_SERVER['REQUEST_METHOD'] = 'POST';
  1022. $expected = [
  1023. 'controller' => 'posts',
  1024. 'action' => 'index',
  1025. 'pass' => [],
  1026. '_method' => ['PUT', 'POST'],
  1027. '_matchedRoute' => '/sample'
  1028. ];
  1029. $this->assertEquals($expected, $route->parse('/sample'));
  1030. }
  1031. /**
  1032. * Test that http header conditions can work with URL generation
  1033. *
  1034. * @return void
  1035. */
  1036. public function testMatchWithMultipleHttpMethodConditions()
  1037. {
  1038. $route = new Route('/sample', [
  1039. 'controller' => 'posts',
  1040. 'action' => 'index',
  1041. '_method' => ['PUT', 'POST']
  1042. ]);
  1043. $url = [
  1044. 'controller' => 'posts',
  1045. 'action' => 'index',
  1046. ];
  1047. $this->assertFalse($route->match($url));
  1048. $url = [
  1049. 'controller' => 'posts',
  1050. 'action' => 'index',
  1051. '_method' => 'GET',
  1052. ];
  1053. $this->assertFalse($route->match($url));
  1054. $url = [
  1055. 'controller' => 'posts',
  1056. 'action' => 'index',
  1057. '_method' => 'PUT',
  1058. ];
  1059. $this->assertEquals('/sample', $route->match($url));
  1060. $url = [
  1061. 'controller' => 'posts',
  1062. 'action' => 'index',
  1063. '_method' => 'POST',
  1064. ];
  1065. $this->assertEquals('/sample', $route->match($url));
  1066. $url = [
  1067. 'controller' => 'posts',
  1068. 'action' => 'index',
  1069. '_method' => ['PUT', 'POST'],
  1070. ];
  1071. $this->assertEquals('/sample', $route->match($url));
  1072. }
  1073. /**
  1074. * Check [method] compatibility.
  1075. *
  1076. * @return void
  1077. */
  1078. public function testMethodCompatibility()
  1079. {
  1080. $_SERVER['REQUEST_METHOD'] = 'POST';
  1081. $route = new Route('/sample', [
  1082. 'controller' => 'Articles',
  1083. 'action' => 'index',
  1084. '[method]' => 'POST',
  1085. ]);
  1086. $url = [
  1087. 'controller' => 'Articles',
  1088. 'action' => 'index',
  1089. '_method' => 'POST',
  1090. ];
  1091. $this->assertEquals('/sample', $route->match($url));
  1092. $url = [
  1093. 'controller' => 'Articles',
  1094. 'action' => 'index',
  1095. '[method]' => 'POST',
  1096. ];
  1097. $this->assertEquals('/sample', $route->match($url));
  1098. }
  1099. /**
  1100. * Test that patterns work for :action
  1101. *
  1102. * @return void
  1103. */
  1104. public function testPatternOnAction()
  1105. {
  1106. $route = new Route(
  1107. '/blog/:action/*',
  1108. ['controller' => 'blog_posts'],
  1109. ['action' => 'other|actions']
  1110. );
  1111. $result = $route->match(['controller' => 'blog_posts', 'action' => 'foo']);
  1112. $this->assertFalse($result);
  1113. $result = $route->match(['controller' => 'blog_posts', 'action' => 'actions']);
  1114. $this->assertNotEmpty($result);
  1115. $result = $route->parse('/blog/other', 'GET');
  1116. $expected = [
  1117. 'controller' => 'blog_posts',
  1118. 'action' => 'other',
  1119. 'pass' => [],
  1120. '_matchedRoute' => '/blog/:action/*'
  1121. ];
  1122. $this->assertEquals($expected, $result);
  1123. $result = $route->parse('/blog/foobar', 'GET');
  1124. $this->assertFalse($result);
  1125. }
  1126. /**
  1127. * Test the parseArgs method
  1128. *
  1129. * @return void
  1130. */
  1131. public function testParsePassedArgument()
  1132. {
  1133. $route = new Route('/:controller/:action/*');
  1134. $result = $route->parse('/posts/edit/1/2/0', 'GET');
  1135. $expected = [
  1136. 'controller' => 'posts',
  1137. 'action' => 'edit',
  1138. 'pass' => ['1', '2', '0'],
  1139. '_matchedRoute' => '/:controller/:action/*'
  1140. ];
  1141. $this->assertEquals($expected, $result);
  1142. }
  1143. /**
  1144. * Test matching of parameters where one parameter name starts with another parameter name
  1145. *
  1146. * @return void
  1147. */
  1148. public function testMatchSimilarParameters()
  1149. {
  1150. $route = new Route('/:thisParam/:thisParamIsLonger');
  1151. $url = [
  1152. 'thisParamIsLonger' => 'bar',
  1153. 'thisParam' => 'foo',
  1154. ];
  1155. $result = $route->match($url);
  1156. $expected = '/foo/bar';
  1157. $this->assertEquals($expected, $result);
  1158. }
  1159. /**
  1160. * Test match() with trailing ** style routes.
  1161. *
  1162. * @return void
  1163. */
  1164. public function testMatchTrailing()
  1165. {
  1166. $route = new Route('/pages/**', ['controller' => 'pages', 'action' => 'display']);
  1167. $id = 'test/ spaces/漢字/la†în';
  1168. $result = $route->match([
  1169. 'controller' => 'pages',
  1170. 'action' => 'display',
  1171. $id
  1172. ]);
  1173. $expected = '/pages/test/%20spaces/%E6%BC%A2%E5%AD%97/la%E2%80%A0%C3%AEn';
  1174. $this->assertEquals($expected, $result);
  1175. }
  1176. /**
  1177. * Test restructuring args with pass key
  1178. *
  1179. * @return void
  1180. */
  1181. public function testPassArgRestructure()
  1182. {
  1183. $route = new Route('/:controller/:action/:slug', [], [
  1184. 'pass' => ['slug']
  1185. ]);
  1186. $result = $route->parse('/posts/view/my-title', 'GET');
  1187. $expected = [
  1188. 'controller' => 'posts',
  1189. 'action' => 'view',
  1190. 'slug' => 'my-title',
  1191. 'pass' => ['my-title'],
  1192. '_matchedRoute' => '/:controller/:action/:slug'
  1193. ];
  1194. $this->assertEquals($expected, $result, 'Slug should have moved');
  1195. }
  1196. /**
  1197. * Test the /** special type on parsing.
  1198. *
  1199. * @return void
  1200. */
  1201. public function testParseTrailing()
  1202. {
  1203. $route = new Route('/:controller/:action/**');
  1204. $result = $route->parse('/posts/index/1/2/3/foo:bar', 'GET');
  1205. $expected = [
  1206. 'controller' => 'posts',
  1207. 'action' => 'index',
  1208. 'pass' => ['1/2/3/foo:bar'],
  1209. '_matchedRoute' => '/:controller/:action/**',
  1210. ];
  1211. $this->assertEquals($expected, $result);
  1212. $result = $route->parse('/posts/index/http://example.com', 'GET');
  1213. $expected = [
  1214. 'controller' => 'posts',
  1215. 'action' => 'index',
  1216. 'pass' => ['http://example.com'],
  1217. '_matchedRoute' => '/:controller/:action/**',
  1218. ];
  1219. $this->assertEquals($expected, $result);
  1220. }
  1221. /**
  1222. * Test the /** special type on parsing - UTF8.
  1223. *
  1224. * @return void
  1225. */
  1226. public function testParseTrailingUTF8()
  1227. {
  1228. $route = new Route('/category/**', ['controller' => 'categories', 'action' => 'index']);
  1229. $result = $route->parse('/category/%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84', 'GET');
  1230. $expected = [
  1231. 'controller' => 'categories',
  1232. 'action' => 'index',
  1233. 'pass' => ['موبایل'],
  1234. '_matchedRoute' => '/category/**',
  1235. ];
  1236. $this->assertEquals($expected, $result);
  1237. }
  1238. /**
  1239. * Test getName();
  1240. *
  1241. * @return void
  1242. */
  1243. public function testGetName()
  1244. {
  1245. $route = new Route('/foo/bar', [], ['_name' => 'testing']);
  1246. $this->assertEquals('', $route->getName());
  1247. $route = new Route('/:controller/:action');
  1248. $this->assertEquals('_controller:_action', $route->getName());
  1249. $route = new Route('/articles/:action', ['controller' => 'posts']);
  1250. $this->assertEquals('posts:_action', $route->getName());
  1251. $route = new Route('/articles/list', ['controller' => 'posts', 'action' => 'index']);
  1252. $this->assertEquals('posts:index', $route->getName());
  1253. $route = new Route('/:controller/:action', ['action' => 'index']);
  1254. $this->assertEquals('_controller:_action', $route->getName());
  1255. }
  1256. /**
  1257. * Test getName() with plugins.
  1258. *
  1259. * @return void
  1260. */
  1261. public function testGetNamePlugins()
  1262. {
  1263. $route = new Route(
  1264. '/a/:controller/:action',
  1265. ['plugin' => 'asset']
  1266. );
  1267. $this->assertEquals('asset._controller:_action', $route->getName());
  1268. $route = new Route(
  1269. '/a/assets/:action',
  1270. ['plugin' => 'asset', 'controller' => 'assets']
  1271. );
  1272. $this->assertEquals('asset.assets:_action', $route->getName());
  1273. $route = new Route(
  1274. '/assets/get',
  1275. ['plugin' => 'asset', 'controller' => 'assets', 'action' => 'get']
  1276. );
  1277. $this->assertEquals('asset.assets:get', $route->getName());
  1278. }
  1279. /**
  1280. * Test getName() with prefixes.
  1281. *
  1282. * @return void
  1283. */
  1284. public function testGetNamePrefix()
  1285. {
  1286. $route = new Route(
  1287. '/admin/:controller/:action',
  1288. ['prefix' => 'admin']
  1289. );
  1290. $this->assertEquals('admin:_controller:_action', $route->getName());
  1291. $route = new Route(
  1292. '/:prefix/assets/:action',
  1293. ['controller' => 'assets']
  1294. );
  1295. $this->assertEquals('_prefix:assets:_action', $route->getName());
  1296. $route = new Route(
  1297. '/admin/assets/get',
  1298. ['prefix' => 'admin', 'plugin' => 'asset', 'controller' => 'assets', 'action' => 'get']
  1299. );
  1300. $this->assertEquals('admin:asset.assets:get', $route->getName());
  1301. $route = new Route(
  1302. '/:prefix/:plugin/:controller/:action/*',
  1303. []
  1304. );
  1305. $this->assertEquals('_prefix:_plugin._controller:_action', $route->getName());
  1306. }
  1307. /**
  1308. * Test that utf-8 patterns work for :section
  1309. *
  1310. * @return void
  1311. */
  1312. public function testUTF8PatternOnSection()
  1313. {
  1314. $route = new Route(
  1315. '/:section',
  1316. ['plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index'],
  1317. [
  1318. 'persist' => ['section'],
  1319. 'section' => 'آموزش|weblog'
  1320. ]
  1321. );
  1322. $result = $route->parse('/%D8%A2%D9%85%D9%88%D8%B2%D8%B4', 'GET');
  1323. $expected = [
  1324. 'section' => 'آموزش',
  1325. 'plugin' => 'blogs',
  1326. 'controller' => 'posts',
  1327. 'action' => 'index',
  1328. 'pass' => [],
  1329. '_matchedRoute' => '/:section',
  1330. ];
  1331. $this->assertEquals($expected, $result);
  1332. $result = $route->parse('/weblog', 'GET');
  1333. $expected = [
  1334. 'section' => 'weblog',
  1335. 'plugin' => 'blogs',
  1336. 'controller' => 'posts',
  1337. 'action' => 'index',
  1338. 'pass' => [],
  1339. '_matchedRoute' => '/:section',
  1340. ];
  1341. $this->assertEquals($expected, $result);
  1342. }
  1343. /**
  1344. * Test getting the static path for a route.
  1345. *
  1346. * @return void
  1347. */
  1348. public function testStaticPath()
  1349. {
  1350. $route = new Route('/*', ['controller' => 'Pages', 'action' => 'display']);
  1351. $this->assertEquals('/', $route->staticPath());
  1352. $route = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  1353. $this->assertEquals('/pages', $route->staticPath());
  1354. $route = new Route('/pages/:id/*', ['controller' => 'Pages', 'action' => 'display']);
  1355. $this->assertEquals('/pages/', $route->staticPath());
  1356. $route = new Route('/:controller/:action/*');
  1357. $this->assertEquals('/', $route->staticPath());
  1358. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1359. $this->assertEquals('/books/reviews', $route->staticPath());
  1360. }
  1361. /**
  1362. * Test for __set_state magic method on CakeRoute
  1363. *
  1364. * @return void
  1365. */
  1366. public function testSetState()
  1367. {
  1368. $route = Route::__set_state([
  1369. 'keys' => [],
  1370. 'options' => [],
  1371. 'defaults' => [
  1372. 'controller' => 'pages',
  1373. 'action' => 'display',
  1374. 'home',
  1375. ],
  1376. 'template' => '/',
  1377. '_greedy' => false,
  1378. '_compiledRoute' => null,
  1379. ]);
  1380. $this->assertInstanceOf('Cake\Routing\Route\Route', $route);
  1381. $this->assertSame('/', $route->match(['controller' => 'pages', 'action' => 'display', 'home']));
  1382. $this->assertFalse($route->match(['controller' => 'pages', 'action' => 'display', 'about']));
  1383. $expected = [
  1384. 'controller' => 'pages',
  1385. 'action' => 'display',
  1386. 'pass' => ['home'],
  1387. '_matchedRoute' => '/',
  1388. ];
  1389. $this->assertEquals($expected, $route->parse('/', 'GET'));
  1390. }
  1391. /**
  1392. * Test setting the method on a route.
  1393. *
  1394. * @return void
  1395. */
  1396. public function testSetMethods()
  1397. {
  1398. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1399. $result = $route->setMethods(['put']);
  1400. $this->assertSame($result, $route, 'Should return this');
  1401. $this->assertSame(['PUT'], $route->defaults['_method'], 'method is wrong');
  1402. $route->setMethods(['post', 'get', 'patch']);
  1403. $this->assertSame(['POST', 'GET', 'PATCH'], $route->defaults['_method']);
  1404. }
  1405. /**
  1406. * Test setting the method on a route to an invalid method
  1407. *
  1408. * @return void
  1409. */
  1410. public function testSetMethodsInvalid()
  1411. {
  1412. $this->expectException(\InvalidArgumentException::class);
  1413. $this->expectExceptionMessage('Invalid HTTP method received. NOPE is invalid');
  1414. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1415. $route->setMethods(['nope']);
  1416. }
  1417. /**
  1418. * Test setting patterns through the method
  1419. *
  1420. * @return void
  1421. */
  1422. public function testSetPatterns()
  1423. {
  1424. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1425. $result = $route->setPatterns([
  1426. 'date' => '\d+\-\d+\-\d+',
  1427. 'id' => '[a-z]+'
  1428. ]);
  1429. $this->assertSame($result, $route, 'Should return this');
  1430. $this->assertArrayHasKey('id', $route->options);
  1431. $this->assertArrayHasKey('date', $route->options);
  1432. $this->assertSame('[a-z]+', $route->options['id']);
  1433. $this->assertArrayNotHasKey('multibytePattern', $route->options);
  1434. $this->assertFalse($route->parse('/reviews/a-b-c/xyz'));
  1435. $this->assertNotEmpty($route->parse('/reviews/2016-05-12/xyz'));
  1436. }
  1437. /**
  1438. * Test setting patterns enables multibyte mode
  1439. *
  1440. * @return void
  1441. */
  1442. public function testSetPatternsMultibyte()
  1443. {
  1444. $route = new Route('/reviews/:accountid/:slug', ['controller' => 'Reviews', 'action' => 'view']);
  1445. $result = $route->setPatterns([
  1446. 'date' => '[A-zА-я\-\ ]+',
  1447. 'accountid' => '[a-z]+'
  1448. ]);
  1449. $this->assertArrayHasKey('multibytePattern', $route->options);
  1450. $this->assertNotEmpty($route->parse('/reviews/abcs/bla-blan-тест'));
  1451. }
  1452. /**
  1453. * Test setting host requirements
  1454. *
  1455. * @return void
  1456. */
  1457. public function testSetHost()
  1458. {
  1459. $route = new Route('/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1460. $result = $route->setHost('blog.example.com');
  1461. $this->assertSame($result, $route, 'Should return this');
  1462. $request = new ServerRequest([
  1463. 'environment' => [
  1464. 'HTTP_HOST' => 'a.example.com',
  1465. 'PATH_INFO' => '/reviews'
  1466. ]
  1467. ]);
  1468. $this->assertFalse($route->parseRequest($request));
  1469. $uri = $request->getUri();
  1470. $request = $request->withUri($uri->withHost('blog.example.com'));
  1471. $this->assertNotEmpty($route->parseRequest($request));
  1472. }
  1473. /**
  1474. * Test setting pass parameters
  1475. *
  1476. * @return void
  1477. */
  1478. public function testSetPass()
  1479. {
  1480. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1481. $result = $route->setPass(['date', 'id']);
  1482. $this->assertSame($result, $route, 'Should return this');
  1483. $this->assertEquals(['date', 'id'], $route->options['pass']);
  1484. }
  1485. /**
  1486. * Test setting persisted parameters
  1487. *
  1488. * @return void
  1489. */
  1490. public function testSetPersist()
  1491. {
  1492. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1493. $result = $route->setPersist(['date']);
  1494. $this->assertSame($result, $route, 'Should return this');
  1495. $this->assertEquals(['date'], $route->options['persist']);
  1496. }
  1497. /**
  1498. * Test setting/getting middleware.
  1499. *
  1500. * @return void
  1501. */
  1502. public function testSetMiddleware()
  1503. {
  1504. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1505. $result = $route->setMiddleware(['auth', 'cookie']);
  1506. $this->assertSame($result, $route);
  1507. $this->assertSame(['auth', 'cookie'], $route->getMiddleware());
  1508. }
  1509. }