RouteTest.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing\Route;
  16. use Cake\Core\Configure;
  17. use Cake\Routing\Router;
  18. use Cake\Routing\Route\Route;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Test case for Route
  22. */
  23. class RouteTest extends TestCase
  24. {
  25. /**
  26. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. Configure::write('Routing', ['admin' => null, 'prefixes' => []]);
  34. }
  35. /**
  36. * Test the construction of a Route
  37. *
  38. * @return void
  39. */
  40. public function testConstruction()
  41. {
  42. $route = new Route('/:controller/:action/:id', [], ['id' => '[0-9]+']);
  43. $this->assertEquals('/:controller/:action/:id', $route->template);
  44. $this->assertEquals([], $route->defaults);
  45. $this->assertEquals(['id' => '[0-9]+'], $route->options);
  46. $this->assertFalse($route->compiled());
  47. }
  48. /**
  49. * test Route compiling.
  50. *
  51. * @return void
  52. */
  53. public function testBasicRouteCompiling()
  54. {
  55. $route = new Route('/', ['controller' => 'pages', 'action' => 'display', 'home']);
  56. $result = $route->compile();
  57. $expected = '#^/*$#';
  58. $this->assertEquals($expected, $result);
  59. $this->assertEquals([], $route->keys);
  60. $route = new Route('/:controller/:action', ['controller' => 'posts']);
  61. $result = $route->compile();
  62. $this->assertRegExp($result, '/posts/edit');
  63. $this->assertRegExp($result, '/posts/super_delete');
  64. $this->assertNotRegExp($result, '/posts');
  65. $this->assertNotRegExp($result, '/posts/super_delete/1');
  66. $this->assertSame($result, $route->compile());
  67. $route = new Route('/posts/foo:id', ['controller' => 'posts', 'action' => 'view']);
  68. $result = $route->compile();
  69. $this->assertRegExp($result, '/posts/foo:1');
  70. $this->assertRegExp($result, '/posts/foo:param');
  71. $this->assertNotRegExp($result, '/posts');
  72. $this->assertNotRegExp($result, '/posts/');
  73. $this->assertEquals(['id'], $route->keys);
  74. $route = new Route('/:plugin/:controller/:action/*', ['plugin' => 'test_plugin', 'action' => 'index']);
  75. $result = $route->compile();
  76. $this->assertRegExp($result, '/test_plugin/posts/index');
  77. $this->assertRegExp($result, '/test_plugin/posts/edit/5');
  78. $this->assertRegExp($result, '/test_plugin/posts/edit/5/name:value/nick:name');
  79. }
  80. /**
  81. * Test parsing routes with extensions.
  82. *
  83. * @return void
  84. */
  85. public function testRouteParsingWithExtensions()
  86. {
  87. $route = new Route(
  88. '/:controller/:action/*',
  89. [],
  90. ['_ext' => ['json', 'xml']]
  91. );
  92. $result = $route->parse('/posts/index', 'GET');
  93. $this->assertFalse(isset($result['_ext']));
  94. $result = $route->parse('/posts/index.pdf', 'GET');
  95. $this->assertFalse(isset($result['_ext']));
  96. $route->extensions(['pdf', 'json', 'xml']);
  97. $result = $route->parse('/posts/index.pdf', 'GET');
  98. $this->assertEquals('pdf', $result['_ext']);
  99. $result = $route->parse('/posts/index.json', 'GET');
  100. $this->assertEquals('json', $result['_ext']);
  101. $result = $route->parse('/posts/index.xml', 'GET');
  102. $this->assertEquals('xml', $result['_ext']);
  103. }
  104. /**
  105. * test that route parameters that overlap don't cause errors.
  106. *
  107. * @return void
  108. */
  109. public function testRouteParameterOverlap()
  110. {
  111. $route = new Route('/invoices/add/:idd/:id', ['controller' => 'invoices', 'action' => 'add']);
  112. $result = $route->compile();
  113. $this->assertRegExp($result, '/invoices/add/1/3');
  114. $route = new Route('/invoices/add/:id/:idd', ['controller' => 'invoices', 'action' => 'add']);
  115. $result = $route->compile();
  116. $this->assertRegExp($result, '/invoices/add/1/3');
  117. }
  118. /**
  119. * test compiling routes with keys that have patterns
  120. *
  121. * @return void
  122. */
  123. public function testRouteCompilingWithParamPatterns()
  124. {
  125. $route = new Route(
  126. '/:controller/:action/:id',
  127. [],
  128. ['id' => Router::ID]
  129. );
  130. $result = $route->compile();
  131. $this->assertRegExp($result, '/posts/edit/1');
  132. $this->assertRegExp($result, '/posts/view/518098');
  133. $this->assertNotRegExp($result, '/posts/edit/name-of-post');
  134. $this->assertNotRegExp($result, '/posts/edit/4/other:param');
  135. $this->assertEquals(['id', 'controller', 'action'], $route->keys);
  136. $route = new Route(
  137. '/:lang/:controller/:action/:id',
  138. ['controller' => 'testing4'],
  139. ['id' => Router::ID, 'lang' => '[a-z]{3}']
  140. );
  141. $result = $route->compile();
  142. $this->assertRegExp($result, '/eng/posts/edit/1');
  143. $this->assertRegExp($result, '/cze/articles/view/1');
  144. $this->assertNotRegExp($result, '/language/articles/view/2');
  145. $this->assertNotRegExp($result, '/eng/articles/view/name-of-article');
  146. $this->assertEquals(['lang', 'id', 'controller', 'action'], $route->keys);
  147. foreach ([':', '@', ';', '$', '-'] as $delim) {
  148. $route = new Route('/posts/:id' . $delim . ':title');
  149. $result = $route->compile();
  150. $this->assertRegExp($result, '/posts/1' . $delim . 'name-of-article');
  151. $this->assertRegExp($result, '/posts/13244' . $delim . 'name-of_Article[]');
  152. $this->assertNotRegExp($result, '/posts/11!nameofarticle');
  153. $this->assertNotRegExp($result, '/posts/11');
  154. $this->assertEquals(['title', 'id'], $route->keys);
  155. }
  156. $route = new Route(
  157. '/posts/:id::title/:year',
  158. ['controller' => 'posts', 'action' => 'view'],
  159. ['id' => Router::ID, 'year' => Router::YEAR, 'title' => '[a-z-_]+']
  160. );
  161. $result = $route->compile();
  162. $this->assertRegExp($result, '/posts/1:name-of-article/2009/');
  163. $this->assertRegExp($result, '/posts/13244:name-of-article/1999');
  164. $this->assertNotRegExp($result, '/posts/hey_now:nameofarticle');
  165. $this->assertNotRegExp($result, '/posts/:nameofarticle/2009');
  166. $this->assertNotRegExp($result, '/posts/:nameofarticle/01');
  167. $this->assertEquals(['year', 'title', 'id'], $route->keys);
  168. $route = new Route(
  169. '/posts/:url_title-(uuid::id)',
  170. ['controller' => 'posts', 'action' => 'view'],
  171. ['pass' => ['id', 'url_title'], 'id' => Router::ID]
  172. );
  173. $result = $route->compile();
  174. $this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)/');
  175. $this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)');
  176. $this->assertNotRegExp($result, '/posts/');
  177. $this->assertNotRegExp($result, '/posts/nameofarticle');
  178. $this->assertNotRegExp($result, '/posts/nameofarticle-12347');
  179. $this->assertEquals(['url_title', 'id'], $route->keys);
  180. }
  181. public function testRouteCompilingWithUnicodePatterns()
  182. {
  183. $route = new Route(
  184. '/test/:slug',
  185. ['controller' => 'Pages', 'action' => 'display'],
  186. ['pass' => ['slug'], 'multibytePattern' => false, 'slug' => '[A-zА-я\-\ ]+']
  187. );
  188. $result = $route->compile();
  189. $this->assertNotRegExp($result, '/test/bla-blan-тест');
  190. $route = new Route(
  191. '/test/:slug',
  192. ['controller' => 'Pages', 'action' => 'display'],
  193. ['pass' => ['slug'], 'multibytePattern' => true, 'slug' => '[A-zА-я\-\ ]+']
  194. );
  195. $result = $route->compile();
  196. $this->assertRegExp($result, '/test/bla-blan-тест');
  197. }
  198. /**
  199. * test more complex route compiling & parsing with mid route greedy stars
  200. * and optional routing parameters
  201. *
  202. * @return void
  203. */
  204. public function testComplexRouteCompilingAndParsing()
  205. {
  206. $route = new Route(
  207. '/posts/:month/:day/:year/*',
  208. ['controller' => 'posts', 'action' => 'view'],
  209. ['year' => Router::YEAR, 'month' => Router::MONTH, 'day' => Router::DAY]
  210. );
  211. $result = $route->compile();
  212. $this->assertRegExp($result, '/posts/08/01/2007/title-of-post');
  213. $result = $route->parse('/posts/08/01/2007/title-of-post', 'GET');
  214. $this->assertEquals(count($result), 7);
  215. $this->assertEquals($result['controller'], 'posts');
  216. $this->assertEquals($result['action'], 'view');
  217. $this->assertEquals($result['year'], '2007');
  218. $this->assertEquals($result['month'], '08');
  219. $this->assertEquals($result['day'], '01');
  220. $this->assertEquals($result['pass'][0], 'title-of-post');
  221. $this->assertEquals($result['_matchedRoute'], '/posts/:month/:day/:year/*');
  222. $route = new Route(
  223. "/:extra/page/:slug/*",
  224. ['controller' => 'pages', 'action' => 'view', 'extra' => null],
  225. ["extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view']
  226. );
  227. $result = $route->compile();
  228. $this->assertRegExp($result, '/some_extra/page/this_is_the_slug');
  229. $this->assertRegExp($result, '/page/this_is_the_slug');
  230. $this->assertEquals(['slug', 'extra'], $route->keys);
  231. $this->assertEquals(['extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'], $route->options);
  232. $expected = [
  233. 'controller' => 'pages',
  234. 'action' => 'view'
  235. ];
  236. $this->assertEquals($expected, $route->defaults);
  237. $route = new Route(
  238. '/:controller/:action/*',
  239. ['project' => false],
  240. [
  241. 'controller' => 'source|wiki|commits|tickets|comments|view',
  242. 'action' => 'branches|history|branch|logs|view|start|add|edit|modify'
  243. ]
  244. );
  245. $this->assertFalse($route->parse('/chaw_test/wiki', 'GET'));
  246. $result = $route->compile();
  247. $this->assertNotRegExp($result, '/some_project/source');
  248. $this->assertRegExp($result, '/source/view');
  249. $this->assertRegExp($result, '/source/view/other/params');
  250. $this->assertNotRegExp($result, '/chaw_test/wiki');
  251. $this->assertNotRegExp($result, '/source/wierd_action');
  252. }
  253. /**
  254. * test that routes match their pattern.
  255. *
  256. * @return void
  257. */
  258. public function testMatchBasic()
  259. {
  260. $route = new Route('/:controller/:action/:id', ['plugin' => null]);
  261. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null]);
  262. $this->assertFalse($result);
  263. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 0]);
  264. $this->assertFalse($result);
  265. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1]);
  266. $this->assertEquals('/posts/view/1', $result);
  267. $route = new Route('/', ['controller' => 'pages', 'action' => 'display', 'home']);
  268. $result = $route->match(['controller' => 'pages', 'action' => 'display', 'home']);
  269. $this->assertEquals('/', $result);
  270. $result = $route->match(['controller' => 'pages', 'action' => 'display', 'about']);
  271. $this->assertFalse($result);
  272. $route = new Route('/pages/*', ['controller' => 'pages', 'action' => 'display']);
  273. $result = $route->match(['controller' => 'pages', 'action' => 'display', 'home']);
  274. $this->assertEquals('/pages/home', $result);
  275. $result = $route->match(['controller' => 'pages', 'action' => 'display', 'about']);
  276. $this->assertEquals('/pages/about', $result);
  277. $route = new Route('/blog/:action', ['controller' => 'posts']);
  278. $result = $route->match(['controller' => 'posts', 'action' => 'view']);
  279. $this->assertEquals('/blog/view', $result);
  280. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'id' => 2]);
  281. $this->assertEquals('/blog/view?id=2', $result);
  282. $result = $route->match(['controller' => 'nodes', 'action' => 'view']);
  283. $this->assertFalse($result);
  284. $result = $route->match(['controller' => 'posts', 'action' => 'view', 1]);
  285. $this->assertFalse($result);
  286. $route = new Route('/foo/:controller/:action', ['action' => 'index']);
  287. $result = $route->match(['controller' => 'posts', 'action' => 'view']);
  288. $this->assertEquals('/foo/posts/view', $result);
  289. $route = new Route('/:plugin/:id/*', ['controller' => 'posts', 'action' => 'view']);
  290. $result = $route->match(['plugin' => 'test', 'controller' => 'posts', 'action' => 'view', 'id' => '1']);
  291. $this->assertEquals('/test/1/', $result);
  292. $result = $route->match(['plugin' => 'fo', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0']);
  293. $this->assertEquals('/fo/1/0', $result);
  294. $result = $route->match(['plugin' => 'fo', 'controller' => 'nodes', 'action' => 'view', 'id' => 1]);
  295. $this->assertFalse($result);
  296. $result = $route->match(['plugin' => 'fo', 'controller' => 'posts', 'action' => 'edit', 'id' => 1]);
  297. $this->assertFalse($result);
  298. $route = new Route('/admin/subscriptions/:action/*', [
  299. 'controller' => 'subscribe', 'prefix' => 'admin'
  300. ]);
  301. $url = ['controller' => 'subscribe', 'prefix' => 'admin', 'action' => 'edit', 1];
  302. $result = $route->match($url);
  303. $expected = '/admin/subscriptions/edit/1';
  304. $this->assertEquals($expected, $result);
  305. $url = [
  306. 'controller' => 'subscribe',
  307. 'prefix' => 'admin',
  308. 'action' => 'edit_admin_e',
  309. 1
  310. ];
  311. $result = $route->match($url);
  312. $expected = '/admin/subscriptions/edit_admin_e/1';
  313. $this->assertEquals($expected, $result);
  314. }
  315. /**
  316. * Test match() with persist option
  317. *
  318. * @return void
  319. */
  320. public function testMatchWithPersistOption()
  321. {
  322. $context = [
  323. 'params' => ['lang' => 'en']
  324. ];
  325. $route = new Route('/:lang/:controller/:action', [], ['persist' => ['lang']]);
  326. $result = $route->match(
  327. ['controller' => 'tasks', 'action' => 'add'],
  328. $context
  329. );
  330. $this->assertEquals('/en/tasks/add', $result);
  331. }
  332. /**
  333. * Test match() with _host and other keys.
  334. */
  335. public function testMatchWithHostKeys()
  336. {
  337. $context = [
  338. '_host' => 'foo.com',
  339. '_scheme' => 'http',
  340. '_port' => 80,
  341. '_base' => ''
  342. ];
  343. $route = new Route('/:controller/:action');
  344. $result = $route->match(
  345. ['controller' => 'posts', 'action' => 'index', '_host' => 'example.com'],
  346. $context
  347. );
  348. $this->assertEquals('http://example.com/posts/index', $result);
  349. $result = $route->match(
  350. ['controller' => 'posts', 'action' => 'index', '_scheme' => 'webcal'],
  351. $context
  352. );
  353. $this->assertEquals('webcal://foo.com/posts/index', $result);
  354. $result = $route->match(
  355. ['controller' => 'posts', 'action' => 'index', '_port' => '8080'],
  356. $context
  357. );
  358. $this->assertEquals('http://foo.com:8080/posts/index', $result);
  359. $result = $route->match(
  360. ['controller' => 'posts', 'action' => 'index', '_base' => '/dir'],
  361. $context
  362. );
  363. $this->assertEquals('/dir/posts/index', $result);
  364. $result = $route->match(
  365. [
  366. 'controller' => 'posts',
  367. 'action' => 'index',
  368. '_port' => '8080',
  369. '_host' => 'example.com',
  370. '_scheme' => 'https',
  371. '_base' => '/dir'
  372. ],
  373. $context
  374. );
  375. $this->assertEquals('https://example.com:8080/dir/posts/index', $result);
  376. }
  377. /**
  378. * test that non-greedy routes fail with extra passed args
  379. *
  380. * @return void
  381. */
  382. public function testMatchGreedyRouteFailurePassedArg()
  383. {
  384. $route = new Route('/:controller/:action', ['plugin' => null]);
  385. $result = $route->match(['controller' => 'posts', 'action' => 'view', '0']);
  386. $this->assertFalse($result);
  387. $route = new Route('/:controller/:action', ['plugin' => null]);
  388. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'test']);
  389. $this->assertFalse($result);
  390. }
  391. /**
  392. * test that falsey values do not interrupt a match.
  393. *
  394. * @return void
  395. */
  396. public function testMatchWithFalseyValues()
  397. {
  398. $route = new Route('/:controller/:action/*', ['plugin' => null]);
  399. $result = $route->match([
  400. 'controller' => 'posts', 'action' => 'index', 'plugin' => null, 'admin' => false
  401. ]);
  402. $this->assertEquals('/posts/index/', $result);
  403. }
  404. /**
  405. * test match() with greedy routes, and passed args.
  406. *
  407. * @return void
  408. */
  409. public function testMatchWithPassedArgs()
  410. {
  411. $route = new Route('/:controller/:action/*', ['plugin' => null]);
  412. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 5]);
  413. $this->assertEquals('/posts/view/5', $result);
  414. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 0]);
  415. $this->assertEquals('/posts/view/0', $result);
  416. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, '0']);
  417. $this->assertEquals('/posts/view/0', $result);
  418. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'plugin' => null, 'word space']);
  419. $this->assertEquals('/posts/view/word%20space', $result);
  420. $route = new Route('/test2/*', ['controller' => 'pages', 'action' => 'display', 2]);
  421. $result = $route->match(['controller' => 'pages', 'action' => 'display', 1]);
  422. $this->assertFalse($result);
  423. $result = $route->match(['controller' => 'pages', 'action' => 'display', 2, 'something']);
  424. $this->assertEquals('/test2/something', $result);
  425. $result = $route->match(['controller' => 'pages', 'action' => 'display', 5, 'something']);
  426. $this->assertFalse($result);
  427. }
  428. /**
  429. * Test that the pass option lets you use positional arguments for the
  430. * route elements that were named.
  431. *
  432. * @return void
  433. */
  434. public function testMatchWithPassOption()
  435. {
  436. $route = new Route(
  437. '/blog/:id-:slug',
  438. ['controller' => 'Blog', 'action' => 'view'],
  439. ['pass' => ['id', 'slug']]
  440. );
  441. $result = $route->match([
  442. 'controller' => 'Blog',
  443. 'action' => 'view',
  444. 'id' => 1,
  445. 'slug' => 'second'
  446. ]);
  447. $this->assertEquals('/blog/1-second', $result);
  448. $result = $route->match([
  449. 'controller' => 'Blog',
  450. 'action' => 'view',
  451. 1,
  452. 'second'
  453. ]);
  454. $this->assertEquals('/blog/1-second', $result);
  455. $result = $route->match([
  456. 'controller' => 'Blog',
  457. 'action' => 'view',
  458. 1,
  459. 'second',
  460. 'query' => 'string'
  461. ]);
  462. $this->assertEquals('/blog/1-second?query=string', $result);
  463. $result = $route->match([
  464. 'controller' => 'Blog',
  465. 'action' => 'view',
  466. 1 => 2,
  467. 2 => 'second'
  468. ]);
  469. $this->assertFalse($result, 'Positional args must match exactly.');
  470. }
  471. /**
  472. * Test that match() with pass and greedy routes.
  473. *
  474. * @return void
  475. */
  476. public function testMatchWithPassOptionGreedy()
  477. {
  478. $route = new Route(
  479. '/blog/:id-:slug/*',
  480. ['controller' => 'Blog', 'action' => 'view'],
  481. ['pass' => ['id', 'slug']]
  482. );
  483. $result = $route->match([
  484. 'controller' => 'Blog',
  485. 'action' => 'view',
  486. 'id' => 1,
  487. 'slug' => 'second',
  488. 'third',
  489. 'fourth',
  490. 'query' => 'string'
  491. ]);
  492. $this->assertEquals('/blog/1-second/third/fourth?query=string', $result);
  493. $result = $route->match([
  494. 'controller' => 'Blog',
  495. 'action' => 'view',
  496. 1,
  497. 'second',
  498. 'third',
  499. 'fourth',
  500. 'query' => 'string'
  501. ]);
  502. $this->assertEquals('/blog/1-second/third/fourth?query=string', $result);
  503. }
  504. /**
  505. * Test that extensions work.
  506. *
  507. * @return void
  508. */
  509. public function testMatchWithExtension()
  510. {
  511. $route = new Route('/:controller/:action');
  512. $result = $route->match([
  513. 'controller' => 'posts',
  514. 'action' => 'index',
  515. '_ext' => 'json'
  516. ]);
  517. $this->assertEquals('/posts/index.json', $result);
  518. $route = new Route('/:controller/:action/*');
  519. $result = $route->match([
  520. 'controller' => 'posts',
  521. 'action' => 'index',
  522. '_ext' => 'json',
  523. ]);
  524. $this->assertEquals('/posts/index.json', $result);
  525. $result = $route->match([
  526. 'controller' => 'posts',
  527. 'action' => 'view',
  528. 1,
  529. '_ext' => 'json',
  530. ]);
  531. $this->assertEquals('/posts/view/1.json', $result);
  532. $result = $route->match([
  533. 'controller' => 'posts',
  534. 'action' => 'view',
  535. 1,
  536. '_ext' => 'json',
  537. 'id' => 'b',
  538. 'c' => 'd'
  539. ]);
  540. $this->assertEquals('/posts/view/1.json?id=b&c=d', $result);
  541. }
  542. /**
  543. * test that match with patterns works.
  544. *
  545. * @return void
  546. */
  547. public function testMatchWithPatterns()
  548. {
  549. $route = new Route('/:controller/:action/:id', ['plugin' => null], ['id' => '[0-9]+']);
  550. $result = $route->match(['controller' => 'posts', 'action' => 'view', 'id' => 'foo']);
  551. $this->assertFalse($result);
  552. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '9']);
  553. $this->assertEquals('/posts/view/9', $result);
  554. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '922']);
  555. $this->assertEquals('/posts/view/922', $result);
  556. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 'a99']);
  557. $this->assertFalse($result);
  558. }
  559. /**
  560. * Test that match() pulls out extra arguments as query string params.
  561. *
  562. * @return void
  563. */
  564. public function testMatchExtractQueryStringArgs()
  565. {
  566. $route = new Route('/:controller/:action/*');
  567. $result = $route->match([
  568. 'controller' => 'posts',
  569. 'action' => 'index',
  570. 'page' => 1
  571. ]);
  572. $this->assertEquals('/posts/index?page=1', $result);
  573. $result = $route->match([
  574. 'controller' => 'posts',
  575. 'action' => 'index',
  576. 'page' => 0
  577. ]);
  578. $this->assertEquals('/posts/index?page=0', $result);
  579. $result = $route->match([
  580. 'controller' => 'posts',
  581. 'action' => 'index',
  582. 1,
  583. 'page' => 1,
  584. 'dir' => 'desc',
  585. 'order' => 'title'
  586. ]);
  587. $this->assertEquals('/posts/index/1?page=1&dir=desc&order=title', $result);
  588. }
  589. /**
  590. * Test separartor.
  591. *
  592. * @return void
  593. */
  594. public function testQueryStringGeneration()
  595. {
  596. $route = new Route('/:controller/:action/*');
  597. $restore = ini_get('arg_separator.output');
  598. ini_set('arg_separator.output', '&amp;');
  599. $result = $route->match([
  600. 'controller' => 'posts',
  601. 'action' => 'index',
  602. 0,
  603. 'test' => 'var',
  604. 'var2' => 'test2',
  605. 'more' => 'test data'
  606. ]);
  607. $expected = '/posts/index/0?test=var&amp;var2=test2&amp;more=test+data';
  608. $this->assertEquals($expected, $result);
  609. ini_set('arg_separator.output', $restore);
  610. }
  611. /**
  612. * test the parse method of Route.
  613. *
  614. * @return void
  615. */
  616. public function testParse()
  617. {
  618. $route = new Route(
  619. '/:controller/:action/:id',
  620. ['controller' => 'testing4', 'id' => null],
  621. ['id' => Router::ID]
  622. );
  623. $route->compile();
  624. $result = $route->parse('/posts/view/1', 'GET');
  625. $this->assertEquals('posts', $result['controller']);
  626. $this->assertEquals('view', $result['action']);
  627. $this->assertEquals('1', $result['id']);
  628. $route = new Route(
  629. '/admin/:controller',
  630. ['prefix' => 'admin', 'admin' => 1, 'action' => 'index']
  631. );
  632. $route->compile();
  633. $result = $route->parse('/admin/', 'GET');
  634. $this->assertFalse($result);
  635. $result = $route->parse('/admin/posts', 'GET');
  636. $this->assertEquals('posts', $result['controller']);
  637. $this->assertEquals('index', $result['action']);
  638. $route = new Route(
  639. '/media/search/*',
  640. ['controller' => 'Media', 'action' => 'search']
  641. );
  642. $result = $route->parse('/media/search', 'GET');
  643. $this->assertEquals('Media', $result['controller']);
  644. $this->assertEquals('search', $result['action']);
  645. $this->assertEquals([], $result['pass']);
  646. $result = $route->parse('/media/search/tv/shows', 'GET');
  647. $this->assertEquals('Media', $result['controller']);
  648. $this->assertEquals('search', $result['action']);
  649. $this->assertEquals(['tv', 'shows'], $result['pass']);
  650. }
  651. /**
  652. * Test that :key elements are urldecoded
  653. *
  654. * @return void
  655. */
  656. public function testParseUrlDecodeElements()
  657. {
  658. $route = new Route(
  659. '/:controller/:slug',
  660. ['action' => 'view']
  661. );
  662. $route->compile();
  663. $result = $route->parse('/posts/%E2%88%82%E2%88%82', 'GET');
  664. $this->assertEquals('posts', $result['controller']);
  665. $this->assertEquals('view', $result['action']);
  666. $this->assertEquals('∂∂', $result['slug']);
  667. $result = $route->parse('/posts/∂∂', 'GET');
  668. $this->assertEquals('posts', $result['controller']);
  669. $this->assertEquals('view', $result['action']);
  670. $this->assertEquals('∂∂', $result['slug']);
  671. }
  672. /**
  673. * test numerically indexed defaults, get appended to pass
  674. *
  675. * @return void
  676. */
  677. public function testParseWithPassDefaults()
  678. {
  679. $route = new Route('/:controller', ['action' => 'display', 'home']);
  680. $result = $route->parse('/posts', 'GET');
  681. $expected = [
  682. 'controller' => 'posts',
  683. 'action' => 'display',
  684. 'pass' => ['home'],
  685. '_matchedRoute' => '/:controller'
  686. ];
  687. $this->assertEquals($expected, $result);
  688. }
  689. /**
  690. * test that http header conditions can cause route failures.
  691. *
  692. * @return void
  693. */
  694. public function testParseWithHttpHeaderConditions()
  695. {
  696. $route = new Route('/sample', ['controller' => 'posts', 'action' => 'index', '_method' => 'POST']);
  697. $this->assertFalse($route->parse('/sample', 'GET'));
  698. $expected = [
  699. 'controller' => 'posts',
  700. 'action' => 'index',
  701. 'pass' => [],
  702. '_method' => 'POST',
  703. '_matchedRoute' => '/sample'
  704. ];
  705. $this->assertEquals($expected, $route->parse('/sample', 'POST'));
  706. }
  707. /**
  708. * test that http header conditions can cause route failures.
  709. *
  710. * @return void
  711. */
  712. public function testParseWithMultipleHttpMethodConditions()
  713. {
  714. $route = new Route('/sample', [
  715. 'controller' => 'posts',
  716. 'action' => 'index',
  717. '_method' => ['PUT', 'POST']
  718. ]);
  719. $this->assertFalse($route->parse('/sample', 'GET'));
  720. // Test for deprecated behavior
  721. $_SERVER['REQUEST_METHOD'] = 'POST';
  722. $expected = [
  723. 'controller' => 'posts',
  724. 'action' => 'index',
  725. 'pass' => [],
  726. '_method' => ['PUT', 'POST'],
  727. '_matchedRoute' => '/sample'
  728. ];
  729. $this->assertEquals($expected, $route->parse('/sample'));
  730. }
  731. /**
  732. * test that http header conditions can work with URL generation
  733. *
  734. * @return void
  735. */
  736. public function testMatchWithMultipleHttpMethodConditions()
  737. {
  738. $route = new Route('/sample', [
  739. 'controller' => 'posts',
  740. 'action' => 'index',
  741. '_method' => ['PUT', 'POST']
  742. ]);
  743. $url = [
  744. 'controller' => 'posts',
  745. 'action' => 'index',
  746. ];
  747. $this->assertFalse($route->match($url));
  748. $url = [
  749. 'controller' => 'posts',
  750. 'action' => 'index',
  751. '_method' => 'GET',
  752. ];
  753. $this->assertFalse($route->match($url));
  754. $url = [
  755. 'controller' => 'posts',
  756. 'action' => 'index',
  757. '_method' => 'PUT',
  758. ];
  759. $this->assertEquals('/sample', $route->match($url));
  760. $url = [
  761. 'controller' => 'posts',
  762. 'action' => 'index',
  763. '_method' => 'POST',
  764. ];
  765. $this->assertEquals('/sample', $route->match($url));
  766. }
  767. /**
  768. * Check [method] compatibility.
  769. *
  770. * @return void
  771. */
  772. public function testMethodCompatibility()
  773. {
  774. $_SERVER['REQUEST_METHOD'] = 'POST';
  775. $route = new Route('/sample', [
  776. 'controller' => 'Articles',
  777. 'action' => 'index',
  778. '[method]' => 'POST',
  779. ]);
  780. $url = [
  781. 'controller' => 'Articles',
  782. 'action' => 'index',
  783. '_method' => 'POST',
  784. ];
  785. $this->assertEquals('/sample', $route->match($url));
  786. $url = [
  787. 'controller' => 'Articles',
  788. 'action' => 'index',
  789. '[method]' => 'POST',
  790. ];
  791. $this->assertEquals('/sample', $route->match($url));
  792. }
  793. /**
  794. * test that patterns work for :action
  795. *
  796. * @return void
  797. */
  798. public function testPatternOnAction()
  799. {
  800. $route = new Route(
  801. '/blog/:action/*',
  802. ['controller' => 'blog_posts'],
  803. ['action' => 'other|actions']
  804. );
  805. $result = $route->match(['controller' => 'blog_posts', 'action' => 'foo']);
  806. $this->assertFalse($result);
  807. $result = $route->match(['controller' => 'blog_posts', 'action' => 'actions']);
  808. $this->assertNotEmpty($result);
  809. $result = $route->parse('/blog/other', 'GET');
  810. $expected = [
  811. 'controller' => 'blog_posts',
  812. 'action' => 'other',
  813. 'pass' => [],
  814. '_matchedRoute' => '/blog/:action/*'
  815. ];
  816. $this->assertEquals($expected, $result);
  817. $result = $route->parse('/blog/foobar', 'GET');
  818. $this->assertFalse($result);
  819. }
  820. /**
  821. * test the parseArgs method
  822. *
  823. * @return void
  824. */
  825. public function testParsePassedArgument()
  826. {
  827. $route = new Route('/:controller/:action/*');
  828. $result = $route->parse('/posts/edit/1/2/0', 'GET');
  829. $expected = [
  830. 'controller' => 'posts',
  831. 'action' => 'edit',
  832. 'pass' => ['1', '2', '0'],
  833. '_matchedRoute' => '/:controller/:action/*'
  834. ];
  835. $this->assertEquals($expected, $result);
  836. }
  837. /**
  838. * Test matching of parameters where one parameter name starts with another parameter name
  839. *
  840. * @return void
  841. */
  842. public function testMatchSimilarParameters()
  843. {
  844. $route = new Route('/:thisParam/:thisParamIsLonger');
  845. $url = [
  846. 'thisParamIsLonger' => 'bar',
  847. 'thisParam' => 'foo',
  848. ];
  849. $result = $route->match($url);
  850. $expected = '/foo/bar';
  851. $this->assertEquals($expected, $result);
  852. }
  853. /**
  854. * Test match() with trailing ** style routes.
  855. *
  856. * @return void
  857. */
  858. public function testMatchTrailing()
  859. {
  860. $route = new Route('/pages/**', ['controller' => 'pages', 'action' => 'display']);
  861. $id = 'test/ spaces/漢字/la†în';
  862. $result = $route->match([
  863. 'controller' => 'pages',
  864. 'action' => 'display',
  865. $id
  866. ]);
  867. $expected = '/pages/test/%20spaces/%E6%BC%A2%E5%AD%97/la%E2%80%A0%C3%AEn';
  868. $this->assertEquals($expected, $result);
  869. }
  870. /**
  871. * test restructuring args with pass key
  872. *
  873. * @return void
  874. */
  875. public function testPassArgRestructure()
  876. {
  877. $route = new Route('/:controller/:action/:slug', [], [
  878. 'pass' => ['slug']
  879. ]);
  880. $result = $route->parse('/posts/view/my-title', 'GET');
  881. $expected = [
  882. 'controller' => 'posts',
  883. 'action' => 'view',
  884. 'slug' => 'my-title',
  885. 'pass' => ['my-title'],
  886. '_matchedRoute' => '/:controller/:action/:slug'
  887. ];
  888. $this->assertEquals($expected, $result, 'Slug should have moved');
  889. }
  890. /**
  891. * Test the /** special type on parsing.
  892. *
  893. * @return void
  894. */
  895. public function testParseTrailing()
  896. {
  897. $route = new Route('/:controller/:action/**');
  898. $result = $route->parse('/posts/index/1/2/3/foo:bar', 'GET');
  899. $expected = [
  900. 'controller' => 'posts',
  901. 'action' => 'index',
  902. 'pass' => ['1/2/3/foo:bar'],
  903. '_matchedRoute' => '/:controller/:action/**',
  904. ];
  905. $this->assertEquals($expected, $result);
  906. $result = $route->parse('/posts/index/http://example.com', 'GET');
  907. $expected = [
  908. 'controller' => 'posts',
  909. 'action' => 'index',
  910. 'pass' => ['http://example.com'],
  911. '_matchedRoute' => '/:controller/:action/**',
  912. ];
  913. $this->assertEquals($expected, $result);
  914. }
  915. /**
  916. * Test the /** special type on parsing - UTF8.
  917. *
  918. * @return void
  919. */
  920. public function testParseTrailingUTF8()
  921. {
  922. $route = new Route('/category/**', ['controller' => 'categories', 'action' => 'index']);
  923. $result = $route->parse('/category/%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84', 'GET');
  924. $expected = [
  925. 'controller' => 'categories',
  926. 'action' => 'index',
  927. 'pass' => ['موبایل'],
  928. '_matchedRoute' => '/category/**',
  929. ];
  930. $this->assertEquals($expected, $result);
  931. }
  932. /**
  933. * test getName();
  934. *
  935. * @return void
  936. */
  937. public function testGetName()
  938. {
  939. $route = new Route('/foo/bar', [], ['_name' => 'testing']);
  940. $this->assertEquals('', $route->getName());
  941. $route = new Route('/:controller/:action');
  942. $this->assertEquals('_controller:_action', $route->getName());
  943. $route = new Route('/articles/:action', ['controller' => 'posts']);
  944. $this->assertEquals('posts:_action', $route->getName());
  945. $route = new Route('/articles/list', ['controller' => 'posts', 'action' => 'index']);
  946. $this->assertEquals('posts:index', $route->getName());
  947. $route = new Route('/:controller/:action', ['action' => 'index']);
  948. $this->assertEquals('_controller:_action', $route->getName());
  949. }
  950. /**
  951. * Test getName() with plugins.
  952. *
  953. * @return void
  954. */
  955. public function testGetNamePlugins()
  956. {
  957. $route = new Route(
  958. '/a/:controller/:action',
  959. ['plugin' => 'asset']
  960. );
  961. $this->assertEquals('asset._controller:_action', $route->getName());
  962. $route = new Route(
  963. '/a/assets/:action',
  964. ['plugin' => 'asset', 'controller' => 'assets']
  965. );
  966. $this->assertEquals('asset.assets:_action', $route->getName());
  967. $route = new Route(
  968. '/assets/get',
  969. ['plugin' => 'asset', 'controller' => 'assets', 'action' => 'get']
  970. );
  971. $this->assertEquals('asset.assets:get', $route->getName());
  972. }
  973. /**
  974. * Test getName() with prefixes.
  975. *
  976. * @return void
  977. */
  978. public function testGetNamePrefix()
  979. {
  980. $route = new Route(
  981. '/admin/:controller/:action',
  982. ['prefix' => 'admin']
  983. );
  984. $this->assertEquals('admin:_controller:_action', $route->getName());
  985. $route = new Route(
  986. '/:prefix/assets/:action',
  987. ['controller' => 'assets']
  988. );
  989. $this->assertEquals('_prefix:assets:_action', $route->getName());
  990. $route = new Route(
  991. '/admin/assets/get',
  992. ['prefix' => 'admin', 'plugin' => 'asset', 'controller' => 'assets', 'action' => 'get']
  993. );
  994. $this->assertEquals('admin:asset.assets:get', $route->getName());
  995. $route = new Route(
  996. '/:prefix/:plugin/:controller/:action/*',
  997. []
  998. );
  999. $this->assertEquals('_prefix:_plugin._controller:_action', $route->getName());
  1000. }
  1001. /**
  1002. * test that utf-8 patterns work for :section
  1003. *
  1004. * @return void
  1005. */
  1006. public function testUTF8PatternOnSection()
  1007. {
  1008. $route = new Route(
  1009. '/:section',
  1010. ['plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index'],
  1011. [
  1012. 'persist' => ['section'],
  1013. 'section' => 'آموزش|weblog'
  1014. ]
  1015. );
  1016. $result = $route->parse('/%D8%A2%D9%85%D9%88%D8%B2%D8%B4', 'GET');
  1017. $expected = [
  1018. 'section' => 'آموزش',
  1019. 'plugin' => 'blogs',
  1020. 'controller' => 'posts',
  1021. 'action' => 'index',
  1022. 'pass' => [],
  1023. '_matchedRoute' => '/:section',
  1024. ];
  1025. $this->assertEquals($expected, $result);
  1026. $result = $route->parse('/weblog', 'GET');
  1027. $expected = [
  1028. 'section' => 'weblog',
  1029. 'plugin' => 'blogs',
  1030. 'controller' => 'posts',
  1031. 'action' => 'index',
  1032. 'pass' => [],
  1033. '_matchedRoute' => '/:section',
  1034. ];
  1035. $this->assertEquals($expected, $result);
  1036. }
  1037. /**
  1038. * Test getting the static path for a route.
  1039. *
  1040. * @return void
  1041. */
  1042. public function testStaticPath()
  1043. {
  1044. $route = new Route('/*', ['controller' => 'Pages', 'action' => 'display']);
  1045. $this->assertEquals('/', $route->staticPath());
  1046. $route = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  1047. $this->assertEquals('/pages', $route->staticPath());
  1048. $route = new Route('/pages/:id/*', ['controller' => 'Pages', 'action' => 'display']);
  1049. $this->assertEquals('/pages/', $route->staticPath());
  1050. $route = new Route('/:controller/:action/*');
  1051. $this->assertEquals('/', $route->staticPath());
  1052. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1053. $this->assertEquals('/books/reviews', $route->staticPath());
  1054. }
  1055. /**
  1056. * Test for __set_state magic method on CakeRoute
  1057. *
  1058. * @return void
  1059. */
  1060. public function testSetState()
  1061. {
  1062. $route = Route::__set_state([
  1063. 'keys' => [],
  1064. 'options' => [],
  1065. 'defaults' => [
  1066. 'controller' => 'pages',
  1067. 'action' => 'display',
  1068. 'home',
  1069. ],
  1070. 'template' => '/',
  1071. '_greedy' => false,
  1072. '_compiledRoute' => null,
  1073. ]);
  1074. $this->assertInstanceOf('Cake\Routing\Route\Route', $route);
  1075. $this->assertSame('/', $route->match(['controller' => 'pages', 'action' => 'display', 'home']));
  1076. $this->assertFalse($route->match(['controller' => 'pages', 'action' => 'display', 'about']));
  1077. $expected = [
  1078. 'controller' => 'pages',
  1079. 'action' => 'display',
  1080. 'pass' => ['home'],
  1081. '_matchedRoute' => '/',
  1082. ];
  1083. $this->assertEquals($expected, $route->parse('/', 'GET'));
  1084. }
  1085. }