RouteTest.php 60 KB

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