RouteTest.php 61 KB

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