RouteTest.php 61 KB

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