CakeRouteTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. <?php
  2. /**
  3. * CakeRequest Test case file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Routing.Route
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeRoute', 'Routing/Route');
  20. App::uses('Router', 'Routing');
  21. /**
  22. * Test case for CakeRoute
  23. *
  24. * @package Cake.Test.Case.Routing.Route
  25. */
  26. class CakeRouteTest extends CakeTestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
  35. }
  36. /**
  37. * Test the construction of a CakeRoute
  38. *
  39. * @return void
  40. */
  41. public function testConstruction() {
  42. $route = new CakeRoute('/:controller/:action/:id', array(), array('id' => '[0-9]+'));
  43. $this->assertEquals('/:controller/:action/:id', $route->template);
  44. $this->assertEquals(array(), $route->defaults);
  45. $this->assertEquals(array('id' => '[0-9]+'), $route->options);
  46. $this->assertFalse($route->compiled());
  47. }
  48. /**
  49. * test Route compiling.
  50. *
  51. * @return void
  52. */
  53. public function testBasicRouteCompiling() {
  54. $route = new CakeRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  55. $result = $route->compile();
  56. $expected = '#^/*$#';
  57. $this->assertEquals($expected, $result);
  58. $this->assertEquals(array(), $route->keys);
  59. $route = new CakeRoute('/:controller/:action', array('controller' => 'posts'));
  60. $result = $route->compile();
  61. $this->assertRegExp($result, '/posts/edit');
  62. $this->assertRegExp($result, '/posts/super_delete');
  63. $this->assertNotRegExp($result, '/posts');
  64. $this->assertNotRegExp($result, '/posts/super_delete/1');
  65. $route = new CakeRoute('/posts/foo:id', array('controller' => 'posts', 'action' => 'view'));
  66. $result = $route->compile();
  67. $this->assertRegExp($result, '/posts/foo:1');
  68. $this->assertRegExp($result, '/posts/foo:param');
  69. $this->assertNotRegExp($result, '/posts');
  70. $this->assertNotRegExp($result, '/posts/');
  71. $this->assertEquals(array('id'), $route->keys);
  72. $route = new CakeRoute('/:plugin/:controller/:action/*', array('plugin' => 'test_plugin', 'action' => 'index'));
  73. $result = $route->compile();
  74. $this->assertRegExp($result, '/test_plugin/posts/index');
  75. $this->assertRegExp($result, '/test_plugin/posts/edit/5');
  76. $this->assertRegExp($result, '/test_plugin/posts/edit/5/name:value/nick:name');
  77. }
  78. /**
  79. * test that route parameters that overlap don't cause errors.
  80. *
  81. * @return void
  82. */
  83. public function testRouteParameterOverlap() {
  84. $route = new CakeRoute('/invoices/add/:idd/:id', array('controller' => 'invoices', 'action' => 'add'));
  85. $result = $route->compile();
  86. $this->assertRegExp($result, '/invoices/add/1/3');
  87. $route = new CakeRoute('/invoices/add/:id/:idd', array('controller' => 'invoices', 'action' => 'add'));
  88. $result = $route->compile();
  89. $this->assertRegExp($result, '/invoices/add/1/3');
  90. }
  91. /**
  92. * test compiling routes with keys that have patterns
  93. *
  94. * @return void
  95. */
  96. public function testRouteCompilingWithParamPatterns() {
  97. $route = new CakeRoute(
  98. '/:controller/:action/:id',
  99. array(),
  100. array('id' => Router::ID)
  101. );
  102. $result = $route->compile();
  103. $this->assertRegExp($result, '/posts/edit/1');
  104. $this->assertRegExp($result, '/posts/view/518098');
  105. $this->assertNotRegExp($result, '/posts/edit/name-of-post');
  106. $this->assertNotRegExp($result, '/posts/edit/4/other:param');
  107. $this->assertEquals(array('controller', 'action', 'id'), $route->keys);
  108. $route = new CakeRoute(
  109. '/:lang/:controller/:action/:id',
  110. array('controller' => 'testing4'),
  111. array('id' => Router::ID, 'lang' => '[a-z]{3}')
  112. );
  113. $result = $route->compile();
  114. $this->assertRegExp($result, '/eng/posts/edit/1');
  115. $this->assertRegExp($result, '/cze/articles/view/1');
  116. $this->assertNotRegExp($result, '/language/articles/view/2');
  117. $this->assertNotRegExp($result, '/eng/articles/view/name-of-article');
  118. $this->assertEquals(array('lang', 'controller', 'action', 'id'), $route->keys);
  119. foreach (array(':', '@', ';', '$', '-') as $delim) {
  120. $route = new CakeRoute('/posts/:id' . $delim . ':title');
  121. $result = $route->compile();
  122. $this->assertRegExp($result, '/posts/1' . $delim . 'name-of-article');
  123. $this->assertRegExp($result, '/posts/13244' . $delim . 'name-of_Article[]');
  124. $this->assertNotRegExp($result, '/posts/11!nameofarticle');
  125. $this->assertNotRegExp($result, '/posts/11');
  126. $this->assertEquals(array('id', 'title'), $route->keys);
  127. }
  128. $route = new CakeRoute(
  129. '/posts/:id::title/:year',
  130. array('controller' => 'posts', 'action' => 'view'),
  131. array('id' => Router::ID, 'year' => Router::YEAR, 'title' => '[a-z-_]+')
  132. );
  133. $result = $route->compile();
  134. $this->assertRegExp($result, '/posts/1:name-of-article/2009/');
  135. $this->assertRegExp($result, '/posts/13244:name-of-article/1999');
  136. $this->assertNotRegExp($result, '/posts/hey_now:nameofarticle');
  137. $this->assertNotRegExp($result, '/posts/:nameofarticle/2009');
  138. $this->assertNotRegExp($result, '/posts/:nameofarticle/01');
  139. $this->assertEquals(array('id', 'title', 'year'), $route->keys);
  140. $route = new CakeRoute(
  141. '/posts/:url_title-(uuid::id)',
  142. array('controller' => 'posts', 'action' => 'view'),
  143. array('pass' => array('id', 'url_title'), 'id' => Router::ID)
  144. );
  145. $result = $route->compile();
  146. $this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)/');
  147. $this->assertRegExp($result, '/posts/some_title_for_article-(uuid:12534)');
  148. $this->assertNotRegExp($result, '/posts/');
  149. $this->assertNotRegExp($result, '/posts/nameofarticle');
  150. $this->assertNotRegExp($result, '/posts/nameofarticle-12347');
  151. $this->assertEquals(array('url_title', 'id'), $route->keys);
  152. }
  153. /**
  154. * test more complex route compiling & parsing with mid route greedy stars
  155. * and optional routing parameters
  156. *
  157. * @return void
  158. */
  159. public function testComplexRouteCompilingAndParsing() {
  160. $route = new CakeRoute(
  161. '/posts/:month/:day/:year/*',
  162. array('controller' => 'posts', 'action' => 'view'),
  163. array('year' => Router::YEAR, 'month' => Router::MONTH, 'day' => Router::DAY)
  164. );
  165. $result = $route->compile();
  166. $this->assertRegExp($result, '/posts/08/01/2007/title-of-post');
  167. $result = $route->parse('/posts/08/01/2007/title-of-post');
  168. $this->assertEquals(7, count($result));
  169. $this->assertEquals('posts', $result['controller']);
  170. $this->assertEquals('view', $result['action']);
  171. $this->assertEquals('2007', $result['year']);
  172. $this->assertEquals('08', $result['month']);
  173. $this->assertEquals('01', $result['day']);
  174. $this->assertEquals('title-of-post', $result['pass'][0]);
  175. $route = new CakeRoute(
  176. "/:extra/page/:slug/*",
  177. array('controller' => 'pages', 'action' => 'view', 'extra' => null),
  178. array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
  179. );
  180. $result = $route->compile();
  181. $this->assertRegExp($result, '/some_extra/page/this_is_the_slug');
  182. $this->assertRegExp($result, '/page/this_is_the_slug');
  183. $this->assertEquals(array('extra', 'slug'), $route->keys);
  184. $this->assertEquals(array('extra' => '[a-z1-9_]*', 'slug' => '[a-z1-9_]+', 'action' => 'view'), $route->options);
  185. $expected = array(
  186. 'controller' => 'pages',
  187. 'action' => 'view'
  188. );
  189. $this->assertEquals($expected, $route->defaults);
  190. $route = new CakeRoute(
  191. '/:controller/:action/*',
  192. array('project' => false),
  193. array(
  194. 'controller' => 'source|wiki|commits|tickets|comments|view',
  195. 'action' => 'branches|history|branch|logs|view|start|add|edit|modify'
  196. )
  197. );
  198. $this->assertFalse($route->parse('/chaw_test/wiki'));
  199. $result = $route->compile();
  200. $this->assertNotRegExp($result, '/some_project/source');
  201. $this->assertRegExp($result, '/source/view');
  202. $this->assertRegExp($result, '/source/view/other/params');
  203. $this->assertNotRegExp($result, '/chaw_test/wiki');
  204. $this->assertNotRegExp($result, '/source/wierd_action');
  205. }
  206. /**
  207. * test that routes match their pattern.
  208. *
  209. * @return void
  210. */
  211. public function testMatchBasic() {
  212. $route = new CakeRoute('/:controller/:action/:id', array('plugin' => null));
  213. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
  214. $this->assertFalse($result);
  215. $result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 0));
  216. $this->assertFalse($result);
  217. $result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1));
  218. $this->assertEquals('/posts/view/1', $result);
  219. $route = new CakeRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  220. $result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
  221. $this->assertEquals('/', $result);
  222. $result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
  223. $this->assertFalse($result);
  224. $route = new CakeRoute('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  225. $result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
  226. $this->assertEquals('/pages/home', $result);
  227. $result = $route->match(array('controller' => 'pages', 'action' => 'display', 'about'));
  228. $this->assertEquals('/pages/about', $result);
  229. $route = new CakeRoute('/blog/:action', array('controller' => 'posts'));
  230. $result = $route->match(array('controller' => 'posts', 'action' => 'view'));
  231. $this->assertEquals('/blog/view', $result);
  232. $result = $route->match(array('controller' => 'nodes', 'action' => 'view'));
  233. $this->assertFalse($result);
  234. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 1));
  235. $this->assertFalse($result);
  236. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'id' => 2));
  237. $this->assertFalse($result);
  238. $route = new CakeRoute('/foo/:controller/:action', array('action' => 'index'));
  239. $result = $route->match(array('controller' => 'posts', 'action' => 'view'));
  240. $this->assertEquals('/foo/posts/view', $result);
  241. $route = new CakeRoute('/:plugin/:id/*', array('controller' => 'posts', 'action' => 'view'));
  242. $result = $route->match(array('plugin' => 'test', 'controller' => 'posts', 'action' => 'view', 'id' => '1'));
  243. $this->assertEquals('/test/1/', $result);
  244. $result = $route->match(array('plugin' => 'fo', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0'));
  245. $this->assertEquals('/fo/1/0', $result);
  246. $result = $route->match(array('plugin' => 'fo', 'controller' => 'nodes', 'action' => 'view', 'id' => 1));
  247. $this->assertFalse($result);
  248. $result = $route->match(array('plugin' => 'fo', 'controller' => 'posts', 'action' => 'edit', 'id' => 1));
  249. $this->assertFalse($result);
  250. $route = new CakeRoute('/admin/subscriptions/:action/*', array(
  251. 'controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin'
  252. ));
  253. $url = array('controller' => 'subscribe', 'admin' => true, 'action' => 'edit', 1);
  254. $result = $route->match($url);
  255. $expected = '/admin/subscriptions/edit/1';
  256. $this->assertEquals($expected, $result);
  257. $url = array(
  258. 'controller' => 'subscribe',
  259. 'admin' => true,
  260. 'action' => 'edit_admin_e',
  261. 1
  262. );
  263. $result = $route->match($url);
  264. $expected = '/admin/subscriptions/edit_admin_e/1';
  265. $this->assertEquals($expected, $result);
  266. }
  267. /**
  268. * test that non-greedy routes fail with extra passed args
  269. *
  270. * @return void
  271. */
  272. public function testGreedyRouteFailurePassedArg() {
  273. $route = new CakeRoute('/:controller/:action', array('plugin' => null));
  274. $result = $route->match(array('controller' => 'posts', 'action' => 'view', '0'));
  275. $this->assertFalse($result);
  276. $route = new CakeRoute('/:controller/:action', array('plugin' => null));
  277. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'test'));
  278. $this->assertFalse($result);
  279. }
  280. /**
  281. * test that non-greedy routes fail with extra passed args
  282. *
  283. * @return void
  284. */
  285. public function testGreedyRouteFailureNamedParam() {
  286. $route = new CakeRoute('/:controller/:action', array('plugin' => null));
  287. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'page' => 1));
  288. $this->assertFalse($result);
  289. }
  290. /**
  291. * test that falsey values do not interrupt a match.
  292. *
  293. * @return void
  294. */
  295. public function testMatchWithFalseyValues() {
  296. $route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
  297. $result = $route->match(array(
  298. 'controller' => 'posts', 'action' => 'index', 'plugin' => null, 'admin' => false
  299. ));
  300. $this->assertEquals('/posts/index/', $result);
  301. }
  302. /**
  303. * test match() with greedy routes, named parameters and passed args.
  304. *
  305. * @return void
  306. */
  307. public function testMatchWithNamedParametersAndPassedArgs() {
  308. Router::connectNamed(true);
  309. $route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
  310. $result = $route->match(array('controller' => 'posts', 'action' => 'index', 'plugin' => null, 'page' => 1));
  311. $this->assertEquals('/posts/index/page:1', $result);
  312. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5));
  313. $this->assertEquals('/posts/view/5', $result);
  314. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 0));
  315. $this->assertEquals('/posts/view/0', $result);
  316. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, '0'));
  317. $this->assertEquals('/posts/view/0', $result);
  318. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5, 'page' => 1, 'limit' => 20, 'order' => 'title'));
  319. $this->assertEquals('/posts/view/5/page:1/limit:20/order:title', $result);
  320. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 'word space', 'order' => 'Θ'));
  321. $this->assertEquals('/posts/view/word%20space/order:%CE%98', $result);
  322. $route = new CakeRoute('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
  323. $result = $route->match(array('controller' => 'pages', 'action' => 'display', 1));
  324. $this->assertFalse($result);
  325. $result = $route->match(array('controller' => 'pages', 'action' => 'display', 2, 'something'));
  326. $this->assertEquals('/test2/something', $result);
  327. $result = $route->match(array('controller' => 'pages', 'action' => 'display', 5, 'something'));
  328. $this->assertFalse($result);
  329. }
  330. /**
  331. * Ensure that named parameters are urldecoded
  332. *
  333. * @return void
  334. */
  335. public function testParseNamedParametersUrlDecode() {
  336. Router::connectNamed(true);
  337. $route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
  338. $result = $route->parse('/posts/index/page:%CE%98');
  339. $this->assertEquals('Θ', $result['named']['page']);
  340. $result = $route->parse('/posts/index/page[]:%CE%98');
  341. $this->assertEquals('Θ', $result['named']['page'][0]);
  342. $result = $route->parse('/posts/index/something%20else/page[]:%CE%98');
  343. $this->assertEquals('Θ', $result['named']['page'][0]);
  344. $this->assertEquals('something else', $result['pass'][0]);
  345. }
  346. /**
  347. * Ensure that keys at named parameters are urldecoded
  348. *
  349. * @return void
  350. */
  351. public function testParseNamedKeyUrlDecode() {
  352. Router::connectNamed(true);
  353. $route = new CakeRoute('/:controller/:action/*', array('plugin' => null));
  354. // checking /post/index/user[0]:a/user[1]:b
  355. $result = $route->parse('/posts/index/user%5B0%5D:a/user%5B1%5D:b');
  356. $this->assertArrayHasKey('user', $result['named']);
  357. $this->assertEquals(array('a', 'b'), $result['named']['user']);
  358. // checking /post/index/user[]:a/user[]:b
  359. $result = $route->parse('/posts/index/user%5B%5D:a/user%5B%5D:b');
  360. $this->assertArrayHasKey('user', $result['named']);
  361. $this->assertEquals(array('a', 'b'), $result['named']['user']);
  362. }
  363. /**
  364. * test that named params with null/false are excluded
  365. *
  366. * @return void
  367. */
  368. public function testNamedParamsWithNullFalse() {
  369. $route = new CakeRoute('/:controller/:action/*');
  370. $result = $route->match(array('controller' => 'posts', 'action' => 'index', 'page' => null, 'sort' => false));
  371. $this->assertEquals('/posts/index/', $result);
  372. }
  373. /**
  374. * test that match with patterns works.
  375. *
  376. * @return void
  377. */
  378. public function testMatchWithPatterns() {
  379. $route = new CakeRoute('/:controller/:action/:id', array('plugin' => null), array('id' => '[0-9]+'));
  380. $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'id' => 'foo'));
  381. $this->assertFalse($result);
  382. $result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '9'));
  383. $this->assertEquals('/posts/view/9', $result);
  384. $result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => '922'));
  385. $this->assertEquals('/posts/view/922', $result);
  386. $result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 'a99'));
  387. $this->assertFalse($result);
  388. }
  389. /**
  390. * test persistParams ability to persist parameters from $params and remove params.
  391. *
  392. * @return void
  393. */
  394. public function testPersistParams() {
  395. $route = new CakeRoute(
  396. '/:lang/:color/blog/:action',
  397. array('controller' => 'posts'),
  398. array('persist' => array('lang', 'color'))
  399. );
  400. $url = array('controller' => 'posts', 'action' => 'index');
  401. $params = array('lang' => 'en', 'color' => 'blue');
  402. $result = $route->persistParams($url, $params);
  403. $this->assertEquals('en', $result['lang']);
  404. $this->assertEquals('blue', $result['color']);
  405. $url = array('controller' => 'posts', 'action' => 'index', 'color' => 'red');
  406. $params = array('lang' => 'en', 'color' => 'blue');
  407. $result = $route->persistParams($url, $params);
  408. $this->assertEquals('en', $result['lang']);
  409. $this->assertEquals('red', $result['color']);
  410. }
  411. /**
  412. * test the parse method of CakeRoute.
  413. *
  414. * @return void
  415. */
  416. public function testParse() {
  417. $route = new CakeRoute(
  418. '/:controller/:action/:id',
  419. array('controller' => 'testing4', 'id' => null),
  420. array('id' => Router::ID)
  421. );
  422. $route->compile();
  423. $result = $route->parse('/posts/view/1');
  424. $this->assertEquals('posts', $result['controller']);
  425. $this->assertEquals('view', $result['action']);
  426. $this->assertEquals('1', $result['id']);
  427. $route = new Cakeroute(
  428. '/admin/:controller',
  429. array('prefix' => 'admin', 'admin' => 1, 'action' => 'index')
  430. );
  431. $route->compile();
  432. $result = $route->parse('/admin/');
  433. $this->assertFalse($result);
  434. $result = $route->parse('/admin/posts');
  435. $this->assertEquals('posts', $result['controller']);
  436. $this->assertEquals('index', $result['action']);
  437. }
  438. /**
  439. * Test that :key elements are urldecoded
  440. *
  441. * @return void
  442. */
  443. public function testParseUrlDecodeElements() {
  444. $route = new Cakeroute(
  445. '/:controller/:slug',
  446. array('action' => 'view')
  447. );
  448. $route->compile();
  449. $result = $route->parse('/posts/%E2%88%82%E2%88%82');
  450. $this->assertEquals('posts', $result['controller']);
  451. $this->assertEquals('view', $result['action']);
  452. $this->assertEquals('∂∂', $result['slug']);
  453. $result = $route->parse('/posts/∂∂');
  454. $this->assertEquals('posts', $result['controller']);
  455. $this->assertEquals('view', $result['action']);
  456. $this->assertEquals('∂∂', $result['slug']);
  457. }
  458. /**
  459. * test numerically indexed defaults, get appended to pass
  460. *
  461. * @return void
  462. */
  463. public function testParseWithPassDefaults() {
  464. $route = new Cakeroute('/:controller', array('action' => 'display', 'home'));
  465. $result = $route->parse('/posts');
  466. $expected = array(
  467. 'controller' => 'posts',
  468. 'action' => 'display',
  469. 'pass' => array('home'),
  470. 'named' => array()
  471. );
  472. $this->assertEquals($expected, $result);
  473. }
  474. /**
  475. * test that http header conditions can cause route failures.
  476. *
  477. * @return void
  478. */
  479. public function testParseWithHttpHeaderConditions() {
  480. $_SERVER['REQUEST_METHOD'] = 'GET';
  481. $route = new CakeRoute('/sample', array('controller' => 'posts', 'action' => 'index', '[method]' => 'POST'));
  482. $this->assertFalse($route->parse('/sample'));
  483. }
  484. /**
  485. * test that patterns work for :action
  486. *
  487. * @return void
  488. */
  489. public function testPatternOnAction() {
  490. $route = new CakeRoute(
  491. '/blog/:action/*',
  492. array('controller' => 'blog_posts'),
  493. array('action' => 'other|actions')
  494. );
  495. $result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
  496. $this->assertFalse($result);
  497. $result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
  498. $this->assertNotEmpty($result);
  499. $result = $route->parse('/blog/other');
  500. $expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => array(), 'named' => array());
  501. $this->assertEquals($expected, $result);
  502. $result = $route->parse('/blog/foobar');
  503. $this->assertFalse($result);
  504. }
  505. /**
  506. * test the parseArgs method
  507. *
  508. * @return void
  509. */
  510. public function testParsePassedArgument() {
  511. $route = new CakeRoute('/:controller/:action/*');
  512. $result = $route->parse('/posts/edit/1/2/0');
  513. $expected = array(
  514. 'controller' => 'posts',
  515. 'action' => 'edit',
  516. 'pass' => array('1', '2', '0'),
  517. 'named' => array()
  518. );
  519. $this->assertEquals($expected, $result);
  520. $result = $route->parse('/posts/edit/a-string/page:1/sort:value');
  521. $expected = array(
  522. 'controller' => 'posts',
  523. 'action' => 'edit',
  524. 'pass' => array('a-string'),
  525. 'named' => array(
  526. 'page' => 1,
  527. 'sort' => 'value'
  528. )
  529. );
  530. $this->assertEquals($expected, $result);
  531. }
  532. /**
  533. * test that only named parameter rules are followed.
  534. *
  535. * @return void
  536. */
  537. public function testParseNamedParametersWithRules() {
  538. $route = new CakeRoute('/:controller/:action/*', array(), array(
  539. 'named' => array(
  540. 'wibble',
  541. 'fish' => array('action' => 'index'),
  542. 'fizz' => array('controller' => array('comments', 'other')),
  543. 'pattern' => 'val-[\d]+'
  544. )
  545. ));
  546. $result = $route->parse('/posts/display/wibble:spin/fish:trout/fizz:buzz/unknown:value');
  547. $expected = array(
  548. 'controller' => 'posts',
  549. 'action' => 'display',
  550. 'pass' => array('fish:trout', 'fizz:buzz', 'unknown:value'),
  551. 'named' => array(
  552. 'wibble' => 'spin'
  553. )
  554. );
  555. $this->assertEquals($expected, $result, 'Fish should not be parsed, as action != index');
  556. $result = $route->parse('/posts/index/wibble:spin/fish:trout/fizz:buzz');
  557. $expected = array(
  558. 'controller' => 'posts',
  559. 'action' => 'index',
  560. 'pass' => array('fizz:buzz'),
  561. 'named' => array(
  562. 'wibble' => 'spin',
  563. 'fish' => 'trout'
  564. )
  565. );
  566. $this->assertEquals($expected, $result, 'Fizz should be parsed, as controller == comments|other');
  567. $result = $route->parse('/comments/index/wibble:spin/fish:trout/fizz:buzz');
  568. $expected = array(
  569. 'controller' => 'comments',
  570. 'action' => 'index',
  571. 'pass' => array(),
  572. 'named' => array(
  573. 'wibble' => 'spin',
  574. 'fish' => 'trout',
  575. 'fizz' => 'buzz'
  576. )
  577. );
  578. $this->assertEquals($expected, $result, 'All params should be parsed as conditions were met.');
  579. $result = $route->parse('/comments/index/pattern:val--');
  580. $expected = array(
  581. 'controller' => 'comments',
  582. 'action' => 'index',
  583. 'pass' => array('pattern:val--'),
  584. 'named' => array()
  585. );
  586. $this->assertEquals($expected, $result, 'Named parameter pattern unmet.');
  587. $result = $route->parse('/comments/index/pattern:val-2');
  588. $expected = array(
  589. 'controller' => 'comments',
  590. 'action' => 'index',
  591. 'pass' => array(),
  592. 'named' => array('pattern' => 'val-2')
  593. );
  594. $this->assertEquals($expected, $result, 'Named parameter pattern met.');
  595. }
  596. /**
  597. * test that greedyNamed ignores rules.
  598. *
  599. * @return void
  600. */
  601. public function testParseGreedyNamed() {
  602. $route = new CakeRoute('/:controller/:action/*', array(), array(
  603. 'named' => array(
  604. 'fizz' => array('controller' => 'comments'),
  605. 'pattern' => 'val-[\d]+',
  606. ),
  607. 'greedyNamed' => true
  608. ));
  609. $result = $route->parse('/posts/display/wibble:spin/fizz:buzz/pattern:ignored');
  610. $expected = array(
  611. 'controller' => 'posts',
  612. 'action' => 'display',
  613. 'pass' => array('fizz:buzz', 'pattern:ignored'),
  614. 'named' => array(
  615. 'wibble' => 'spin',
  616. )
  617. );
  618. $this->assertEquals($expected, $result, 'Greedy named grabs everything, rules are followed');
  619. }
  620. /**
  621. * Having greedNamed enabled should not capture routing.prefixes.
  622. *
  623. * @return void
  624. */
  625. public function testMatchGreedyNamedExcludesPrefixes() {
  626. Configure::write('Routing.prefixes', array('admin'));
  627. Router::reload();
  628. $route = new CakeRoute('/sales/*', array('controller' => 'sales', 'action' => 'index'));
  629. $this->assertFalse($route->match(array('controller' => 'sales', 'action' => 'index', 'admin' => 1)), 'Greedy named consume routing prefixes.');
  630. }
  631. /**
  632. * test that parsing array format named parameters works
  633. *
  634. * @return void
  635. */
  636. public function testParseArrayNamedParameters() {
  637. $route = new CakeRoute('/:controller/:action/*');
  638. $result = $route->parse('/tests/action/var[]:val1/var[]:val2');
  639. $expected = array(
  640. 'controller' => 'tests',
  641. 'action' => 'action',
  642. 'named' => array(
  643. 'var' => array(
  644. 'val1',
  645. 'val2'
  646. )
  647. ),
  648. 'pass' => array(),
  649. );
  650. $this->assertEquals($expected, $result);
  651. $result = $route->parse('/tests/action/theanswer[is]:42/var[]:val2/var[]:val3');
  652. $expected = array(
  653. 'controller' => 'tests',
  654. 'action' => 'action',
  655. 'named' => array(
  656. 'theanswer' => array(
  657. 'is' => 42
  658. ),
  659. 'var' => array(
  660. 'val2',
  661. 'val3'
  662. )
  663. ),
  664. 'pass' => array(),
  665. );
  666. $this->assertEquals($expected, $result);
  667. $result = $route->parse('/tests/action/theanswer[is][not]:42/theanswer[]:5/theanswer[is]:6');
  668. $expected = array(
  669. 'controller' => 'tests',
  670. 'action' => 'action',
  671. 'named' => array(
  672. 'theanswer' => array(
  673. 5,
  674. 'is' => array(
  675. 6,
  676. 'not' => 42
  677. )
  678. ),
  679. ),
  680. 'pass' => array(),
  681. );
  682. $this->assertEquals($expected, $result);
  683. }
  684. /**
  685. * Test that match can handle array named parameters
  686. *
  687. * @return void
  688. */
  689. public function testMatchNamedParametersArray() {
  690. $route = new CakeRoute('/:controller/:action/*');
  691. $url = array(
  692. 'controller' => 'posts',
  693. 'action' => 'index',
  694. 'filter' => array(
  695. 'one',
  696. 'model' => 'value'
  697. )
  698. );
  699. $result = $route->match($url);
  700. $expected = '/posts/index/filter%5B0%5D:one/filter%5Bmodel%5D:value';
  701. $this->assertEquals($expected, $result);
  702. $url = array(
  703. 'controller' => 'posts',
  704. 'action' => 'index',
  705. 'filter' => array(
  706. 'one',
  707. 'model' => array(
  708. 'two',
  709. 'order' => 'field'
  710. )
  711. )
  712. );
  713. $result = $route->match($url);
  714. $expected = '/posts/index/filter%5B0%5D:one/filter%5Bmodel%5D%5B0%5D:two/filter%5Bmodel%5D%5Border%5D:field';
  715. $this->assertEquals($expected, $result);
  716. }
  717. /**
  718. * test restructuring args with pass key
  719. *
  720. * @return void
  721. */
  722. public function testPassArgRestructure() {
  723. $route = new CakeRoute('/:controller/:action/:slug', array(), array(
  724. 'pass' => array('slug')
  725. ));
  726. $result = $route->parse('/posts/view/my-title');
  727. $expected = array(
  728. 'controller' => 'posts',
  729. 'action' => 'view',
  730. 'slug' => 'my-title',
  731. 'pass' => array('my-title'),
  732. 'named' => array()
  733. );
  734. $this->assertEquals($expected, $result, 'Slug should have moved');
  735. }
  736. /**
  737. * Test the /** special type on parsing.
  738. *
  739. * @return void
  740. */
  741. public function testParseTrailing() {
  742. $route = new CakeRoute('/:controller/:action/**');
  743. $result = $route->parse('/posts/index/1/2/3/foo:bar');
  744. $expected = array(
  745. 'controller' => 'posts',
  746. 'action' => 'index',
  747. 'pass' => array('1/2/3/foo:bar'),
  748. 'named' => array()
  749. );
  750. $this->assertEquals($expected, $result);
  751. $result = $route->parse('/posts/index/http://example.com');
  752. $expected = array(
  753. 'controller' => 'posts',
  754. 'action' => 'index',
  755. 'pass' => array('http://example.com'),
  756. 'named' => array()
  757. );
  758. $this->assertEquals($expected, $result);
  759. }
  760. /**
  761. * Test the /** special type on parsing - UTF8.
  762. *
  763. * @return void
  764. */
  765. public function testParseTrailingUTF8() {
  766. $route = new CakeRoute('/category/**', array('controller' => 'categories','action' => 'index'));
  767. $result = $route->parse('/category/%D9%85%D9%88%D8%A8%D8%A7%DB%8C%D9%84');
  768. $expected = array(
  769. 'controller' => 'categories',
  770. 'action' => 'index',
  771. 'pass' => array('موبایل'),
  772. 'named' => array()
  773. );
  774. $this->assertEquals($expected, $result);
  775. }
  776. /**
  777. * test that utf-8 patterns work for :section
  778. *
  779. * @return void
  780. */
  781. public function testUTF8PatternOnSection() {
  782. $route = new CakeRoute(
  783. '/:section',
  784. array('plugin' => 'blogs', 'controller' => 'posts' , 'action' => 'index' ),
  785. array(
  786. 'persist' => array('section'),
  787. 'section' => 'آموزش|weblog'
  788. )
  789. );
  790. $result = $route->parse('/%D8%A2%D9%85%D9%88%D8%B2%D8%B4');
  791. $expected = array('section' => 'آموزش', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => array(), 'named' => array());
  792. $this->assertEquals($expected, $result);
  793. $result = $route->parse('/weblog');
  794. $expected = array('section' => 'weblog', 'plugin' => 'blogs', 'controller' => 'posts', 'action' => 'index', 'pass' => array(), 'named' => array());
  795. $this->assertEquals($expected, $result);
  796. }
  797. }