RouteTest.php 38 KB

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