RouteTest.php 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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/weird_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->assertSame('/posts/view/9', $result);
  923. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '9']);
  924. $this->assertEquals('/posts/view/9', $result);
  925. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '922']);
  926. $this->assertEquals('/posts/view/922', $result);
  927. $result = $route->match(['plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 'a99']);
  928. $this->assertFalse($result);
  929. }
  930. /**
  931. * Test that match() with multibyte pattern
  932. *
  933. * @return void
  934. */
  935. public function testMatchWithMultibytePattern()
  936. {
  937. $route = new Route(
  938. '/articles/:action/:id',
  939. ['controller' => 'Articles'],
  940. ['multibytePattern' => true, 'id' => '\pL+']
  941. );
  942. $result = $route->match([
  943. 'controller' => 'Articles',
  944. 'action' => 'view',
  945. 'id' => "\xC4\x81",
  946. ]);
  947. $this->assertEquals("/articles/view/\xC4\x81", $result);
  948. }
  949. /**
  950. * Test that match() matches explicit GET routes
  951. *
  952. * @return void
  953. */
  954. public function testMatchWithExplicitGet()
  955. {
  956. $route = new Route(
  957. '/anything',
  958. ['controller' => 'Articles', 'action' => 'foo', '_method' => 'GET']
  959. );
  960. $result = $route->match([
  961. 'controller' => 'Articles',
  962. 'action' => 'foo',
  963. ]);
  964. $this->assertEquals("/anything", $result);
  965. }
  966. /**
  967. * Test separartor.
  968. *
  969. * @return void
  970. */
  971. public function testQueryStringGeneration()
  972. {
  973. $route = new Route('/:controller/:action/*');
  974. $restore = ini_get('arg_separator.output');
  975. ini_set('arg_separator.output', '&amp;');
  976. $result = $route->match([
  977. 'controller' => 'posts',
  978. 'action' => 'index',
  979. 0,
  980. 'test' => 'var',
  981. 'var2' => 'test2',
  982. 'more' => 'test data',
  983. ]);
  984. $expected = '/posts/index/0?test=var&amp;var2=test2&amp;more=test+data';
  985. $this->assertEquals($expected, $result);
  986. ini_set('arg_separator.output', $restore);
  987. }
  988. /**
  989. * Ensure that parseRequest() calls parse() as that is required
  990. * for backwards compat
  991. *
  992. * @return void
  993. */
  994. public function testParseRequestDelegates()
  995. {
  996. $route = $this->getMockBuilder('Cake\Routing\Route\Route')
  997. ->setMethods(['parse'])
  998. ->setConstructorArgs(['/forward', ['controller' => 'Articles', 'action' => 'index']])
  999. ->getMock();
  1000. $route->expects($this->once())
  1001. ->method('parse')
  1002. ->with('/forward', 'GET')
  1003. ->will($this->returnValue('works!'));
  1004. $request = new ServerRequest([
  1005. 'environment' => [
  1006. 'REQUEST_METHOD' => 'GET',
  1007. 'PATH_INFO' => '/forward',
  1008. ],
  1009. ]);
  1010. $result = $route->parseRequest($request);
  1011. }
  1012. /**
  1013. * Test that parseRequest() applies host conditions
  1014. *
  1015. * @return void
  1016. */
  1017. public function testParseRequestHostConditions()
  1018. {
  1019. $route = new Route(
  1020. '/fallback',
  1021. ['controller' => 'Articles', 'action' => 'index'],
  1022. ['_host' => '*.example.com']
  1023. );
  1024. $request = new ServerRequest([
  1025. 'environment' => [
  1026. 'HTTP_HOST' => 'a.example.com',
  1027. 'PATH_INFO' => '/fallback',
  1028. ],
  1029. ]);
  1030. $result = $route->parseRequest($request);
  1031. $expected = [
  1032. 'controller' => 'Articles',
  1033. 'action' => 'index',
  1034. 'pass' => [],
  1035. '_matchedRoute' => '/fallback',
  1036. ];
  1037. $this->assertEquals($expected, $result, 'Should match, domain is correct');
  1038. $request = new ServerRequest([
  1039. 'environment' => [
  1040. 'HTTP_HOST' => 'foo.bar.example.com',
  1041. 'PATH_INFO' => '/fallback',
  1042. ],
  1043. ]);
  1044. $result = $route->parseRequest($request);
  1045. $this->assertEquals($expected, $result, 'Should match, domain is a matching subdomain');
  1046. $request = new ServerRequest([
  1047. 'environment' => [
  1048. 'HTTP_HOST' => 'example.test.com',
  1049. 'PATH_INFO' => '/fallback',
  1050. ],
  1051. ]);
  1052. $this->assertFalse($route->parseRequest($request));
  1053. }
  1054. /**
  1055. * test the parse method of Route.
  1056. *
  1057. * @return void
  1058. */
  1059. public function testParse()
  1060. {
  1061. $route = new Route(
  1062. '/:controller/:action/:id',
  1063. ['controller' => 'testing4', 'id' => null],
  1064. ['id' => Router::ID]
  1065. );
  1066. $route->compile();
  1067. $result = $route->parse('/posts/view/1', 'GET');
  1068. $this->assertEquals('posts', $result['controller']);
  1069. $this->assertEquals('view', $result['action']);
  1070. $this->assertEquals('1', $result['id']);
  1071. $route = new Route(
  1072. '/admin/:controller',
  1073. ['prefix' => 'admin', 'admin' => 1, 'action' => 'index']
  1074. );
  1075. $route->compile();
  1076. $result = $route->parse('/admin/', 'GET');
  1077. $this->assertFalse($result);
  1078. $result = $route->parse('/admin/posts', 'GET');
  1079. $this->assertEquals('posts', $result['controller']);
  1080. $this->assertEquals('index', $result['action']);
  1081. $route = new Route(
  1082. '/media/search/*',
  1083. ['controller' => 'Media', 'action' => 'search']
  1084. );
  1085. $result = $route->parse('/media/search', 'GET');
  1086. $this->assertEquals('Media', $result['controller']);
  1087. $this->assertEquals('search', $result['action']);
  1088. $this->assertEquals([], $result['pass']);
  1089. $result = $route->parse('/media/search/tv/shows', 'GET');
  1090. $this->assertEquals('Media', $result['controller']);
  1091. $this->assertEquals('search', $result['action']);
  1092. $this->assertEquals(['tv', 'shows'], $result['pass']);
  1093. }
  1094. /**
  1095. * Test that :key elements are urldecoded
  1096. *
  1097. * @return void
  1098. */
  1099. public function testParseUrlDecodeElements()
  1100. {
  1101. $route = new Route(
  1102. '/:controller/:slug',
  1103. ['action' => 'view']
  1104. );
  1105. $route->compile();
  1106. $result = $route->parse('/posts/%E2%88%82%E2%88%82', 'GET');
  1107. $this->assertEquals('posts', $result['controller']);
  1108. $this->assertEquals('view', $result['action']);
  1109. $this->assertEquals('∂∂', $result['slug']);
  1110. $result = $route->parse('/posts/∂∂', 'GET');
  1111. $this->assertEquals('posts', $result['controller']);
  1112. $this->assertEquals('view', $result['action']);
  1113. $this->assertEquals('∂∂', $result['slug']);
  1114. }
  1115. /**
  1116. * Test numerically indexed defaults, get appended to pass
  1117. *
  1118. * @return void
  1119. */
  1120. public function testParseWithPassDefaults()
  1121. {
  1122. $route = new Route('/:controller', ['action' => 'display', 'home']);
  1123. $result = $route->parse('/posts', 'GET');
  1124. $expected = [
  1125. 'controller' => 'posts',
  1126. 'action' => 'display',
  1127. 'pass' => ['home'],
  1128. '_matchedRoute' => '/:controller',
  1129. ];
  1130. $this->assertEquals($expected, $result);
  1131. }
  1132. /**
  1133. * Test that middleware is returned from parse()
  1134. *
  1135. * @return void
  1136. */
  1137. public function testParseWithMiddleware()
  1138. {
  1139. $route = new Route('/:controller', ['action' => 'display', 'home']);
  1140. $route->setMiddleware(['auth', 'cookie']);
  1141. $result = $route->parse('/posts', 'GET');
  1142. $expected = [
  1143. 'controller' => 'posts',
  1144. 'action' => 'display',
  1145. 'pass' => ['home'],
  1146. '_matchedRoute' => '/:controller',
  1147. '_middleware' => ['auth', 'cookie'],
  1148. ];
  1149. $this->assertEquals($expected, $result);
  1150. }
  1151. /**
  1152. * Test that http header conditions can cause route failures.
  1153. *
  1154. * @return void
  1155. */
  1156. public function testParseWithHttpHeaderConditions()
  1157. {
  1158. $route = new Route('/sample', ['controller' => 'posts', 'action' => 'index', '_method' => 'POST']);
  1159. $this->assertFalse($route->parse('/sample', 'GET'));
  1160. $expected = [
  1161. 'controller' => 'posts',
  1162. 'action' => 'index',
  1163. 'pass' => [],
  1164. '_method' => 'POST',
  1165. '_matchedRoute' => '/sample',
  1166. ];
  1167. $this->assertEquals($expected, $route->parse('/sample', 'POST'));
  1168. }
  1169. /**
  1170. * Test that http header conditions can cause route failures.
  1171. *
  1172. * @return void
  1173. */
  1174. public function testParseWithMultipleHttpMethodConditions()
  1175. {
  1176. $route = new Route('/sample', [
  1177. 'controller' => 'posts',
  1178. 'action' => 'index',
  1179. '_method' => ['PUT', 'POST'],
  1180. ]);
  1181. $this->assertFalse($route->parse('/sample', 'GET'));
  1182. $expected = [
  1183. 'controller' => 'posts',
  1184. 'action' => 'index',
  1185. 'pass' => [],
  1186. '_method' => ['PUT', 'POST'],
  1187. '_matchedRoute' => '/sample',
  1188. ];
  1189. $this->assertEquals($expected, $route->parse('/sample', 'POST'));
  1190. }
  1191. /**
  1192. * Test deprecated globals reading for method matching
  1193. *
  1194. * @group deprecated
  1195. * @return void
  1196. */
  1197. public function testParseWithMultipleHttpMethodDeprecated()
  1198. {
  1199. $this->deprecated(function () {
  1200. $_SERVER['REQUEST_METHOD'] = 'GET';
  1201. $route = new Route('/sample', [
  1202. 'controller' => 'posts',
  1203. 'action' => 'index',
  1204. '_method' => ['PUT', 'POST'],
  1205. ]);
  1206. $this->assertFalse($route->parse('/sample'));
  1207. $_SERVER['REQUEST_METHOD'] = 'POST';
  1208. $expected = [
  1209. 'controller' => 'posts',
  1210. 'action' => 'index',
  1211. 'pass' => [],
  1212. '_method' => ['PUT', 'POST'],
  1213. '_matchedRoute' => '/sample',
  1214. ];
  1215. $this->assertEquals($expected, $route->parse('/sample'));
  1216. });
  1217. }
  1218. /**
  1219. * Test that http header conditions can work with URL generation
  1220. *
  1221. * @return void
  1222. */
  1223. public function testMatchWithMultipleHttpMethodConditions()
  1224. {
  1225. $route = new Route('/sample', [
  1226. 'controller' => 'posts',
  1227. 'action' => 'index',
  1228. '_method' => ['PUT', 'POST'],
  1229. ]);
  1230. $url = [
  1231. 'controller' => 'posts',
  1232. 'action' => 'index',
  1233. ];
  1234. $this->assertFalse($route->match($url));
  1235. $url = [
  1236. 'controller' => 'posts',
  1237. 'action' => 'index',
  1238. '_method' => 'GET',
  1239. ];
  1240. $this->assertFalse($route->match($url));
  1241. $url = [
  1242. 'controller' => 'posts',
  1243. 'action' => 'index',
  1244. '_method' => 'PUT',
  1245. ];
  1246. $this->assertEquals('/sample', $route->match($url));
  1247. $url = [
  1248. 'controller' => 'posts',
  1249. 'action' => 'index',
  1250. '_method' => 'POST',
  1251. ];
  1252. $this->assertEquals('/sample', $route->match($url));
  1253. $url = [
  1254. 'controller' => 'posts',
  1255. 'action' => 'index',
  1256. '_method' => ['PUT', 'POST'],
  1257. ];
  1258. $this->assertEquals('/sample', $route->match($url));
  1259. }
  1260. /**
  1261. * Check [method] compatibility.
  1262. *
  1263. * @group deprecated
  1264. * @return void
  1265. */
  1266. public function testMethodCompatibility()
  1267. {
  1268. $this->deprecated(function () {
  1269. $_SERVER['REQUEST_METHOD'] = 'POST';
  1270. $route = new Route('/sample', [
  1271. 'controller' => 'Articles',
  1272. 'action' => 'index',
  1273. '[method]' => 'POST',
  1274. ]);
  1275. $url = [
  1276. 'controller' => 'Articles',
  1277. 'action' => 'index',
  1278. '_method' => 'POST',
  1279. ];
  1280. $this->assertEquals('/sample', $route->match($url));
  1281. $url = [
  1282. 'controller' => 'Articles',
  1283. 'action' => 'index',
  1284. '[method]' => 'POST',
  1285. ];
  1286. $this->assertEquals('/sample', $route->match($url));
  1287. });
  1288. }
  1289. /**
  1290. * Test that patterns work for :action
  1291. *
  1292. * @return void
  1293. */
  1294. public function testPatternOnAction()
  1295. {
  1296. $route = new Route(
  1297. '/blog/:action/*',
  1298. ['controller' => 'blog_posts'],
  1299. ['action' => 'other|actions']
  1300. );
  1301. $result = $route->match(['controller' => 'blog_posts', 'action' => 'foo']);
  1302. $this->assertFalse($result);
  1303. $result = $route->match(['controller' => 'blog_posts', 'action' => 'actions']);
  1304. $this->assertNotEmpty($result);
  1305. $result = $route->parse('/blog/other', 'GET');
  1306. $expected = [
  1307. 'controller' => 'blog_posts',
  1308. 'action' => 'other',
  1309. 'pass' => [],
  1310. '_matchedRoute' => '/blog/:action/*',
  1311. ];
  1312. $this->assertEquals($expected, $result);
  1313. $result = $route->parse('/blog/foobar', 'GET');
  1314. $this->assertFalse($result);
  1315. }
  1316. /**
  1317. * Test the parseArgs method
  1318. *
  1319. * @return void
  1320. */
  1321. public function testParsePassedArgument()
  1322. {
  1323. $route = new Route('/:controller/:action/*');
  1324. $result = $route->parse('/posts/edit/1/2/0', 'GET');
  1325. $expected = [
  1326. 'controller' => 'posts',
  1327. 'action' => 'edit',
  1328. 'pass' => ['1', '2', '0'],
  1329. '_matchedRoute' => '/:controller/:action/*',
  1330. ];
  1331. $this->assertEquals($expected, $result);
  1332. }
  1333. /**
  1334. * Test matching of parameters where one parameter name starts with another parameter name
  1335. *
  1336. * @return void
  1337. */
  1338. public function testMatchSimilarParameters()
  1339. {
  1340. $route = new Route('/:thisParam/:thisParamIsLonger');
  1341. $url = [
  1342. 'thisParamIsLonger' => 'bar',
  1343. 'thisParam' => 'foo',
  1344. ];
  1345. $result = $route->match($url);
  1346. $expected = '/foo/bar';
  1347. $this->assertEquals($expected, $result);
  1348. }
  1349. /**
  1350. * Test match() with trailing ** style routes.
  1351. *
  1352. * @return void
  1353. */
  1354. public function testMatchTrailing()
  1355. {
  1356. $route = new Route('/pages/**', ['controller' => 'pages', 'action' => 'display']);
  1357. $id = 'test/ spaces/漢字/la†în';
  1358. $result = $route->match([
  1359. 'controller' => 'pages',
  1360. 'action' => 'display',
  1361. $id,
  1362. ]);
  1363. $expected = '/pages/test/%20spaces/%E6%BC%A2%E5%AD%97/la%E2%80%A0%C3%AEn';
  1364. $this->assertEquals($expected, $result);
  1365. }
  1366. /**
  1367. * Test match handles optional keys
  1368. *
  1369. * @return void
  1370. */
  1371. public function testMatchNullValueOptionalKey()
  1372. {
  1373. $route = new Route('/path/:optional/fixed');
  1374. $this->assertSame('/path/fixed', $route->match(['optional' => null]));
  1375. $route = new Route('/path/{optional}/fixed');
  1376. $this->assertSame('/path/fixed', $route->match(['optional' => null]));
  1377. }
  1378. /**
  1379. * Test matching fails on required keys (controller/action)
  1380. *
  1381. * @return void
  1382. */
  1383. public function testMatchControllerRequiredKeys()
  1384. {
  1385. $route = new Route('/:controller/:action', ['action' => 'test']);
  1386. $this->assertFalse($route->match(['controller' => null]));
  1387. $route = new Route('/test/:action', ['controller' => 'thing']);
  1388. $this->assertFalse($route->match(['action' => null]));
  1389. }
  1390. /**
  1391. * Test restructuring args with pass key
  1392. *
  1393. * @return void
  1394. */
  1395. public function testPassArgRestructure()
  1396. {
  1397. $route = new Route('/:controller/:action/:slug', [], [
  1398. 'pass' => ['slug'],
  1399. ]);
  1400. $result = $route->parse('/posts/view/my-title', 'GET');
  1401. $expected = [
  1402. 'controller' => 'posts',
  1403. 'action' => 'view',
  1404. 'slug' => 'my-title',
  1405. 'pass' => ['my-title'],
  1406. '_matchedRoute' => '/:controller/:action/:slug',
  1407. ];
  1408. $this->assertEquals($expected, $result, 'Slug should have moved');
  1409. }
  1410. /**
  1411. * Test the /** special type on parsing.
  1412. *
  1413. * @return void
  1414. */
  1415. public function testParseTrailing()
  1416. {
  1417. $route = new Route('/:controller/:action/**');
  1418. $result = $route->parse('/posts/index/1/2/3/foo:bar', 'GET');
  1419. $expected = [
  1420. 'controller' => 'posts',
  1421. 'action' => 'index',
  1422. 'pass' => ['1/2/3/foo:bar'],
  1423. '_matchedRoute' => '/:controller/:action/**',
  1424. ];
  1425. $this->assertEquals($expected, $result);
  1426. $result = $route->parse('/posts/index/http://example.com', 'GET');
  1427. $expected = [
  1428. 'controller' => 'posts',
  1429. 'action' => 'index',
  1430. 'pass' => ['http://example.com'],
  1431. '_matchedRoute' => '/:controller/:action/**',
  1432. ];
  1433. $this->assertEquals($expected, $result);
  1434. }
  1435. /**
  1436. * Test the /** special type on parsing - UTF8.
  1437. *
  1438. * @return void
  1439. */
  1440. public function testParseTrailingUTF8()
  1441. {
  1442. $route = new Route('/category/**', ['controller' => 'categories', 'action' => 'index']);
  1443. $result = $route->parse('/category/%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84', 'GET');
  1444. $expected = [
  1445. 'controller' => 'categories',
  1446. 'action' => 'index',
  1447. 'pass' => ['موبایل'],
  1448. '_matchedRoute' => '/category/**',
  1449. ];
  1450. $this->assertEquals($expected, $result);
  1451. }
  1452. /**
  1453. * Test getName();
  1454. *
  1455. * @return void
  1456. */
  1457. public function testGetName()
  1458. {
  1459. $route = new Route('/foo/bar', [], ['_name' => 'testing']);
  1460. $this->assertEquals('', $route->getName());
  1461. $route = new Route('/:controller/:action');
  1462. $this->assertEquals('_controller:_action', $route->getName());
  1463. $route = new Route('/{controller}/{action}');
  1464. $this->assertSame('_controller:_action', $route->getName());
  1465. $route = new Route('/articles/:action', ['controller' => 'posts']);
  1466. $this->assertEquals('posts:_action', $route->getName());
  1467. $route = new Route('/articles/list', ['controller' => 'posts', 'action' => 'index']);
  1468. $this->assertEquals('posts:index', $route->getName());
  1469. $route = new Route('/:controller/:action', ['action' => 'index']);
  1470. $this->assertEquals('_controller:_action', $route->getName());
  1471. }
  1472. /**
  1473. * Test getName() with plugins.
  1474. *
  1475. * @return void
  1476. */
  1477. public function testGetNamePlugins()
  1478. {
  1479. $route = new Route(
  1480. '/a/:controller/:action',
  1481. ['plugin' => 'asset']
  1482. );
  1483. $this->assertEquals('asset._controller:_action', $route->getName());
  1484. $route = new Route(
  1485. '/a/assets/:action',
  1486. ['plugin' => 'asset', 'controller' => 'assets']
  1487. );
  1488. $this->assertEquals('asset.assets:_action', $route->getName());
  1489. $route = new Route(
  1490. '/assets/get',
  1491. ['plugin' => 'asset', 'controller' => 'assets', 'action' => 'get']
  1492. );
  1493. $this->assertEquals('asset.assets:get', $route->getName());
  1494. }
  1495. /**
  1496. * Test getName() with prefixes.
  1497. *
  1498. * @return void
  1499. */
  1500. public function testGetNamePrefix()
  1501. {
  1502. $route = new Route(
  1503. '/admin/:controller/:action',
  1504. ['prefix' => 'admin']
  1505. );
  1506. $this->assertEquals('admin:_controller:_action', $route->getName());
  1507. $route = new Route(
  1508. '/:prefix/assets/:action',
  1509. ['controller' => 'assets']
  1510. );
  1511. $this->assertEquals('_prefix:assets:_action', $route->getName());
  1512. $route = new Route(
  1513. '/admin/assets/get',
  1514. ['prefix' => 'admin', 'plugin' => 'asset', 'controller' => 'assets', 'action' => 'get']
  1515. );
  1516. $this->assertEquals('admin:asset.assets:get', $route->getName());
  1517. $route = new Route(
  1518. '/:prefix/:plugin/:controller/:action/*',
  1519. []
  1520. );
  1521. $this->assertEquals('_prefix:_plugin._controller:_action', $route->getName());
  1522. }
  1523. /**
  1524. * Test that utf-8 patterns work for :section
  1525. *
  1526. * @return void
  1527. */
  1528. public function testUTF8PatternOnSection()
  1529. {
  1530. $route = new Route(
  1531. '/:section',
  1532. ['plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index'],
  1533. [
  1534. 'persist' => ['section'],
  1535. 'section' => 'آموزش|weblog',
  1536. ]
  1537. );
  1538. $result = $route->parse('/%D8%A2%D9%85%D9%88%D8%B2%D8%B4', 'GET');
  1539. $expected = [
  1540. 'section' => 'آموزش',
  1541. 'plugin' => 'blogs',
  1542. 'controller' => 'posts',
  1543. 'action' => 'index',
  1544. 'pass' => [],
  1545. '_matchedRoute' => '/:section',
  1546. ];
  1547. $this->assertEquals($expected, $result);
  1548. $result = $route->parse('/weblog', 'GET');
  1549. $expected = [
  1550. 'section' => 'weblog',
  1551. 'plugin' => 'blogs',
  1552. 'controller' => 'posts',
  1553. 'action' => 'index',
  1554. 'pass' => [],
  1555. '_matchedRoute' => '/:section',
  1556. ];
  1557. $this->assertEquals($expected, $result);
  1558. }
  1559. /**
  1560. * Test getting the static path for a route.
  1561. *
  1562. * @return void
  1563. */
  1564. public function testStaticPath()
  1565. {
  1566. $route = new Route('/*', ['controller' => 'Pages', 'action' => 'display']);
  1567. $this->assertEquals('/', $route->staticPath());
  1568. $route = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  1569. $this->assertEquals('/pages', $route->staticPath());
  1570. $route = new Route('/pages/:id/*', ['controller' => 'Pages', 'action' => 'display']);
  1571. $this->assertEquals('/pages/', $route->staticPath());
  1572. $route = new Route('/:controller/:action/*');
  1573. $this->assertEquals('/', $route->staticPath());
  1574. $route = new Route('/api/{/:action/*');
  1575. $this->assertEquals('/api/{/', $route->staticPath());
  1576. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1577. $this->assertEquals('/books/reviews', $route->staticPath());
  1578. }
  1579. /**
  1580. * Test getting the static path for a route.
  1581. *
  1582. * @return void
  1583. */
  1584. public function testStaticPathBrace()
  1585. {
  1586. $route = new Route('/pages/{id}/*', ['controller' => 'Pages', 'action' => 'display']);
  1587. $this->assertEquals('/pages/', $route->staticPath());
  1588. $route = new Route('/{controller}/{action}/*');
  1589. $this->assertEquals('/', $route->staticPath());
  1590. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1591. $this->assertEquals('/books/reviews', $route->staticPath());
  1592. }
  1593. /**
  1594. * Test for __set_state magic method on CakeRoute
  1595. *
  1596. * @return void
  1597. */
  1598. public function testSetState()
  1599. {
  1600. $route = Route::__set_state([
  1601. 'keys' => [],
  1602. 'options' => [],
  1603. 'defaults' => [
  1604. 'controller' => 'pages',
  1605. 'action' => 'display',
  1606. 'home',
  1607. ],
  1608. 'template' => '/',
  1609. '_greedy' => false,
  1610. '_compiledRoute' => null,
  1611. ]);
  1612. $this->assertInstanceOf('Cake\Routing\Route\Route', $route);
  1613. $this->assertSame('/', $route->match(['controller' => 'pages', 'action' => 'display', 'home']));
  1614. $this->assertFalse($route->match(['controller' => 'pages', 'action' => 'display', 'about']));
  1615. $expected = [
  1616. 'controller' => 'pages',
  1617. 'action' => 'display',
  1618. 'pass' => ['home'],
  1619. '_matchedRoute' => '/',
  1620. ];
  1621. $this->assertEquals($expected, $route->parse('/', 'GET'));
  1622. }
  1623. /**
  1624. * Test setting the method on a route.
  1625. *
  1626. * @return void
  1627. */
  1628. public function testSetMethods()
  1629. {
  1630. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1631. $result = $route->setMethods(['put']);
  1632. $this->assertSame($result, $route, 'Should return this');
  1633. $this->assertSame(['PUT'], $route->defaults['_method'], 'method is wrong');
  1634. $route->setMethods(['post', 'get', 'patch']);
  1635. $this->assertSame(['POST', 'GET', 'PATCH'], $route->defaults['_method']);
  1636. }
  1637. /**
  1638. * Test setting the method on a route to an invalid method
  1639. *
  1640. * @return void
  1641. */
  1642. public function testSetMethodsInvalid()
  1643. {
  1644. $this->expectException(\InvalidArgumentException::class);
  1645. $this->expectExceptionMessage('Invalid HTTP method received. NOPE is invalid');
  1646. $route = new Route('/books/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1647. $route->setMethods(['nope']);
  1648. }
  1649. /**
  1650. * Test setting patterns through the method
  1651. *
  1652. * @return void
  1653. */
  1654. public function testSetPatterns()
  1655. {
  1656. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1657. $result = $route->setPatterns([
  1658. 'date' => '\d+\-\d+\-\d+',
  1659. 'id' => '[a-z]+',
  1660. ]);
  1661. $this->assertSame($result, $route, 'Should return this');
  1662. $this->assertArrayHasKey('id', $route->options);
  1663. $this->assertArrayHasKey('date', $route->options);
  1664. $this->assertSame('[a-z]+', $route->options['id']);
  1665. $this->assertArrayNotHasKey('multibytePattern', $route->options);
  1666. $this->assertFalse($route->parse('/reviews/a-b-c/xyz'));
  1667. $this->assertNotEmpty($route->parse('/reviews/2016-05-12/xyz'));
  1668. }
  1669. /**
  1670. * Test setting patterns enables multibyte mode
  1671. *
  1672. * @return void
  1673. */
  1674. public function testSetPatternsMultibyte()
  1675. {
  1676. $route = new Route('/reviews/:accountid/:slug', ['controller' => 'Reviews', 'action' => 'view']);
  1677. $result = $route->setPatterns([
  1678. 'date' => '[A-zА-я\-\ ]+',
  1679. 'accountid' => '[a-z]+',
  1680. ]);
  1681. $this->assertArrayHasKey('multibytePattern', $route->options);
  1682. $this->assertNotEmpty($route->parse('/reviews/abcs/bla-blan-тест'));
  1683. }
  1684. /**
  1685. * Test setting host requirements
  1686. *
  1687. * @return void
  1688. */
  1689. public function testSetHost()
  1690. {
  1691. $route = new Route('/reviews', ['controller' => 'Reviews', 'action' => 'index']);
  1692. $result = $route->setHost('blog.example.com');
  1693. $this->assertSame($result, $route, 'Should return this');
  1694. $request = new ServerRequest([
  1695. 'environment' => [
  1696. 'HTTP_HOST' => 'a.example.com',
  1697. 'PATH_INFO' => '/reviews',
  1698. ],
  1699. ]);
  1700. $this->assertFalse($route->parseRequest($request));
  1701. $uri = $request->getUri();
  1702. $request = $request->withUri($uri->withHost('blog.example.com'));
  1703. $this->assertNotEmpty($route->parseRequest($request));
  1704. }
  1705. /**
  1706. * Test setting pass parameters
  1707. *
  1708. * @return void
  1709. */
  1710. public function testSetPass()
  1711. {
  1712. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1713. $result = $route->setPass(['date', 'id']);
  1714. $this->assertSame($result, $route, 'Should return this');
  1715. $this->assertEquals(['date', 'id'], $route->options['pass']);
  1716. }
  1717. /**
  1718. * Test setting persisted parameters
  1719. *
  1720. * @return void
  1721. */
  1722. public function testSetPersist()
  1723. {
  1724. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1725. $result = $route->setPersist(['date']);
  1726. $this->assertSame($result, $route, 'Should return this');
  1727. $this->assertEquals(['date'], $route->options['persist']);
  1728. }
  1729. /**
  1730. * Test setting/getting middleware.
  1731. *
  1732. * @return void
  1733. */
  1734. public function testSetMiddleware()
  1735. {
  1736. $route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
  1737. $result = $route->setMiddleware(['auth', 'cookie']);
  1738. $this->assertSame($result, $route);
  1739. $this->assertSame(['auth', 'cookie'], $route->getMiddleware());
  1740. }
  1741. }