RouteTest.php 45 KB

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