RouteTest.php 38 KB

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