RouteTest.php 49 KB

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