RouterTest.php 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731
  1. <?php
  2. /**
  3. * RouterTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite 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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Routing
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Router', 'Routing');
  20. App::uses('CakeResponse', 'Network');
  21. if (!defined('FULL_BASE_URL')) {
  22. define('FULL_BASE_URL', 'http://cakephp.org');
  23. }
  24. /**
  25. * RouterTest class
  26. *
  27. * @package Cake.Test.Case.Routing
  28. */
  29. class RouterTest extends CakeTestCase {
  30. /**
  31. * setUp method
  32. *
  33. * @return void
  34. */
  35. public function setUp() {
  36. parent::setUp();
  37. Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
  38. }
  39. /**
  40. * tearDown method
  41. *
  42. * @return void
  43. */
  44. public function tearDown() {
  45. parent::tearDown();
  46. CakePlugin::unload();
  47. }
  48. /**
  49. * testFullBaseURL method
  50. *
  51. * @return void
  52. */
  53. public function testFullBaseURL() {
  54. $skip = PHP_SAPI == 'cli';
  55. if ($skip) {
  56. $this->markTestSkipped('Cannot validate base urls in CLI');
  57. }
  58. $this->assertRegExp('/^http(s)?:\/\//', Router::url('/', true));
  59. $this->assertRegExp('/^http(s)?:\/\//', Router::url(null, true));
  60. $this->assertRegExp('/^http(s)?:\/\//', Router::url(array('full_base' => true)));
  61. $this->assertSame(FULL_BASE_URL . '/', Router::url(array('full_base' => true)));
  62. }
  63. /**
  64. * testRouteDefaultParams method
  65. *
  66. * @return void
  67. */
  68. public function testRouteDefaultParams() {
  69. Router::connect('/:controller', array('controller' => 'posts'));
  70. $this->assertEquals(Router::url(array('action' => 'index')), '/');
  71. }
  72. /**
  73. * testMapResources method
  74. *
  75. * @return void
  76. */
  77. public function testMapResources() {
  78. $resources = Router::mapResources('Posts');
  79. $_SERVER['REQUEST_METHOD'] = 'GET';
  80. $result = Router::parse('/posts');
  81. $this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => 'GET'), $result);
  82. $this->assertEquals(array('posts'), $resources);
  83. $_SERVER['REQUEST_METHOD'] = 'GET';
  84. $result = Router::parse('/posts/13');
  85. $this->assertEquals(array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => '13', '[method]' => 'GET'), $result);
  86. $_SERVER['REQUEST_METHOD'] = 'POST';
  87. $result = Router::parse('/posts');
  88. $this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST'), $result);
  89. $_SERVER['REQUEST_METHOD'] = 'PUT';
  90. $result = Router::parse('/posts/13');
  91. $this->assertEquals(array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '13', '[method]' => 'PUT'), $result);
  92. $result = Router::parse('/posts/475acc39-a328-44d3-95fb-015000000000');
  93. $this->assertEquals(array('pass' => array('475acc39-a328-44d3-95fb-015000000000'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '475acc39-a328-44d3-95fb-015000000000', '[method]' => 'PUT'), $result);
  94. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  95. $result = Router::parse('/posts/13');
  96. $this->assertEquals(array('pass' => array('13'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'delete', 'id' => '13', '[method]' => 'DELETE'), $result);
  97. $_SERVER['REQUEST_METHOD'] = 'GET';
  98. $result = Router::parse('/posts/add');
  99. $this->assertSame(array(), $result);
  100. Router::reload();
  101. $resources = Router::mapResources('Posts', array('id' => '[a-z0-9_]+'));
  102. $this->assertEquals(array('posts'), $resources);
  103. $_SERVER['REQUEST_METHOD'] = 'GET';
  104. $result = Router::parse('/posts/add');
  105. $this->assertEquals(array('pass' => array('add'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'view', 'id' => 'add', '[method]' => 'GET'), $result);
  106. $_SERVER['REQUEST_METHOD'] = 'PUT';
  107. $result = Router::parse('/posts/name');
  108. $this->assertEquals(array('pass' => array('name'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => 'name', '[method]' => 'PUT'), $result);
  109. }
  110. /**
  111. * testMapResources with plugin controllers.
  112. *
  113. * @return void
  114. */
  115. public function testPluginMapResources() {
  116. App::build(array(
  117. 'Plugin' => array(
  118. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  119. )
  120. ));
  121. $resources = Router::mapResources('TestPlugin.TestPlugin');
  122. $_SERVER['REQUEST_METHOD'] = 'GET';
  123. $result = Router::parse('/test_plugin/test_plugin');
  124. $expected = array(
  125. 'pass' => array(),
  126. 'named' => array(),
  127. 'plugin' => 'test_plugin',
  128. 'controller' => 'test_plugin',
  129. 'action' => 'index',
  130. '[method]' => 'GET'
  131. );
  132. $this->assertEquals($expected, $result);
  133. $this->assertEquals(array('test_plugin'), $resources);
  134. $_SERVER['REQUEST_METHOD'] = 'GET';
  135. $result = Router::parse('/test_plugin/test_plugin/13');
  136. $expected = array(
  137. 'pass' => array('13'),
  138. 'named' => array(),
  139. 'plugin' => 'test_plugin',
  140. 'controller' => 'test_plugin',
  141. 'action' => 'view',
  142. 'id' => '13',
  143. '[method]' => 'GET'
  144. );
  145. $this->assertEquals($expected, $result);
  146. }
  147. /**
  148. * Test mapResources with a plugin and prefix.
  149. *
  150. * @return void
  151. */
  152. public function testPluginMapResourcesWithPrefix() {
  153. App::build(array(
  154. 'Plugin' => array(
  155. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  156. )
  157. ));
  158. $resources = Router::mapResources('TestPlugin.TestPlugin', array('prefix' => '/api/'));
  159. $_SERVER['REQUEST_METHOD'] = 'GET';
  160. $result = Router::parse('/api/test_plugin');
  161. $expected = array(
  162. 'pass' => array(),
  163. 'named' => array(),
  164. 'plugin' => 'test_plugin',
  165. 'controller' => 'test_plugin',
  166. 'action' => 'index',
  167. '[method]' => 'GET'
  168. );
  169. $this->assertEquals($expected, $result);
  170. $this->assertEquals(array('test_plugin'), $resources);
  171. }
  172. /**
  173. * testMultipleResourceRoute method
  174. *
  175. * @return void
  176. */
  177. public function testMultipleResourceRoute() {
  178. Router::connect('/:controller', array('action' => 'index', '[method]' => array('GET', 'POST')));
  179. $_SERVER['REQUEST_METHOD'] = 'GET';
  180. $result = Router::parse('/posts');
  181. $this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => array('GET', 'POST')), $result);
  182. $_SERVER['REQUEST_METHOD'] = 'POST';
  183. $result = Router::parse('/posts');
  184. $this->assertEquals(array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'index', '[method]' => array('GET', 'POST')), $result);
  185. }
  186. /**
  187. * testGenerateUrlResourceRoute method
  188. *
  189. * @return void
  190. */
  191. public function testGenerateUrlResourceRoute() {
  192. Router::mapResources('Posts');
  193. $result = Router::url(array('controller' => 'posts', 'action' => 'index', '[method]' => 'GET'));
  194. $expected = '/posts';
  195. $this->assertEquals($expected, $result);
  196. $result = Router::url(array('controller' => 'posts', 'action' => 'view', '[method]' => 'GET', 'id' => 10));
  197. $expected = '/posts/10';
  198. $this->assertEquals($expected, $result);
  199. $result = Router::url(array('controller' => 'posts', 'action' => 'add', '[method]' => 'POST'));
  200. $expected = '/posts';
  201. $this->assertEquals($expected, $result);
  202. $result = Router::url(array('controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'id' => 10));
  203. $expected = '/posts/10';
  204. $this->assertEquals($expected, $result);
  205. $result = Router::url(array('controller' => 'posts', 'action' => 'delete', '[method]' => 'DELETE', 'id' => 10));
  206. $expected = '/posts/10';
  207. $this->assertEquals($expected, $result);
  208. $result = Router::url(array('controller' => 'posts', 'action' => 'edit', '[method]' => 'POST', 'id' => 10));
  209. $expected = '/posts/10';
  210. $this->assertEquals($expected, $result);
  211. }
  212. /**
  213. * testUrlNormalization method
  214. *
  215. * @return void
  216. */
  217. public function testUrlNormalization() {
  218. $expected = '/users/logout';
  219. $result = Router::normalize('/users/logout/');
  220. $this->assertEquals($expected, $result);
  221. $result = Router::normalize('//users//logout//');
  222. $this->assertEquals($expected, $result);
  223. $result = Router::normalize('users/logout');
  224. $this->assertEquals($expected, $result);
  225. $result = Router::normalize(array('controller' => 'users', 'action' => 'logout'));
  226. $this->assertEquals($expected, $result);
  227. $result = Router::normalize('/');
  228. $this->assertEquals('/', $result);
  229. $result = Router::normalize('http://google.com/');
  230. $this->assertEquals('http://google.com/', $result);
  231. $result = Router::normalize('http://google.com//');
  232. $this->assertEquals('http://google.com//', $result);
  233. $result = Router::normalize('/users/login/scope://foo');
  234. $this->assertEquals('/users/login/scope:/foo', $result);
  235. $result = Router::normalize('/recipe/recipes/add');
  236. $this->assertEquals('/recipe/recipes/add', $result);
  237. $request = new CakeRequest();
  238. $request->base = '/us';
  239. Router::setRequestInfo($request);
  240. $result = Router::normalize('/us/users/logout/');
  241. $this->assertEquals('/users/logout', $result);
  242. Router::reload();
  243. $request = new CakeRequest();
  244. $request->base = '/cake_12';
  245. Router::setRequestInfo($request);
  246. $result = Router::normalize('/cake_12/users/logout/');
  247. $this->assertEquals('/users/logout', $result);
  248. Router::reload();
  249. $_back = Configure::read('App.baseUrl');
  250. Configure::write('App.baseUrl', '/');
  251. $request = new CakeRequest();
  252. $request->base = '/';
  253. Router::setRequestInfo($request);
  254. $result = Router::normalize('users/login');
  255. $this->assertEquals('/users/login', $result);
  256. Configure::write('App.baseUrl', $_back);
  257. Router::reload();
  258. $request = new CakeRequest();
  259. $request->base = 'beer';
  260. Router::setRequestInfo($request);
  261. $result = Router::normalize('beer/admin/beers_tags/add');
  262. $this->assertEquals('/admin/beers_tags/add', $result);
  263. $result = Router::normalize('/admin/beers_tags/add');
  264. $this->assertEquals('/admin/beers_tags/add', $result);
  265. }
  266. /**
  267. * test generation of basic urls.
  268. *
  269. * @return void
  270. */
  271. public function testUrlGenerationBasic() {
  272. extract(Router::getNamedExpressions());
  273. $request = new CakeRequest();
  274. $request->addParams(array(
  275. 'action' => 'index', 'plugin' => null, 'controller' => 'subscribe', 'admin' => true
  276. ));
  277. $request->base = '/magazine';
  278. $request->here = '/magazine';
  279. $request->webroot = '/magazine/';
  280. Router::setRequestInfo($request);
  281. $result = Router::url();
  282. $this->assertEquals('/magazine', $result);
  283. Router::reload();
  284. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  285. $out = Router::url(array('controller' => 'pages', 'action' => 'display', 'home'));
  286. $this->assertEquals('/', $out);
  287. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  288. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 'about'));
  289. $expected = '/pages/about';
  290. $this->assertEquals($expected, $result);
  291. Router::reload();
  292. Router::connect('/:plugin/:id/*', array('controller' => 'posts', 'action' => 'view'), array('id' => $ID));
  293. Router::parse('/');
  294. $result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1'));
  295. $expected = '/cake_plugin/1';
  296. $this->assertEquals($expected, $result);
  297. $result = Router::url(array('plugin' => 'cake_plugin', 'controller' => 'posts', 'action' => 'view', 'id' => '1', '0'));
  298. $expected = '/cake_plugin/1/0';
  299. $this->assertEquals($expected, $result);
  300. Router::reload();
  301. Router::connect('/:controller/:action/:id', array(), array('id' => $ID));
  302. Router::parse('/');
  303. $result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1'));
  304. $expected = '/posts/view/1';
  305. $this->assertEquals($expected, $result);
  306. Router::reload();
  307. Router::connect('/:controller/:id', array('action' => 'view'));
  308. Router::parse('/');
  309. $result = Router::url(array('controller' => 'posts', 'action' => 'view', 'id' => '1'));
  310. $expected = '/posts/1';
  311. $this->assertEquals($expected, $result);
  312. $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0'));
  313. $expected = '/posts/index/0';
  314. $this->assertEquals($expected, $result);
  315. Router::connect('/view/*', array('controller' => 'posts', 'action' => 'view'));
  316. Router::promote();
  317. $result = Router::url(array('controller' => 'posts', 'action' => 'view', '1'));
  318. $expected = '/view/1';
  319. $this->assertEquals($expected, $result);
  320. Router::reload();
  321. $request = new CakeRequest();
  322. $request->addParams(array(
  323. 'action' => 'index', 'plugin' => null, 'controller' => 'real_controller_name'
  324. ));
  325. $request->base = '/';
  326. $request->here = '/';
  327. $request->webroot = '/';
  328. Router::setRequestInfo($request);
  329. Router::connect('short_controller_name/:action/*', array('controller' => 'real_controller_name'));
  330. Router::parse('/');
  331. $result = Router::url(array('controller' => 'real_controller_name', 'page' => '1'));
  332. $expected = '/short_controller_name/index/page:1';
  333. $this->assertEquals($expected, $result);
  334. $result = Router::url(array('action' => 'add'));
  335. $expected = '/short_controller_name/add';
  336. $this->assertEquals($expected, $result);
  337. Router::reload();
  338. Router::parse('/');
  339. $request = new CakeRequest();
  340. $request->addParams(array(
  341. 'action' => 'index', 'plugin' => null, 'controller' => 'users', 'url' => array('url' => 'users')
  342. ));
  343. $request->base = '/';
  344. $request->here = '/';
  345. $request->webroot = '/';
  346. Router::setRequestInfo($request);
  347. $result = Router::url(array('action' => 'login'));
  348. $expected = '/users/login';
  349. $this->assertEquals($expected, $result);
  350. Router::reload();
  351. Router::connect('/page/*', array('plugin' => null, 'controller' => 'pages', 'action' => 'view'));
  352. Router::parse('/');
  353. $result = Router::url(array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'view', 'my-page'));
  354. $expected = '/my_plugin/pages/view/my-page';
  355. $this->assertEquals($expected, $result);
  356. Router::reload();
  357. Router::connect('/contact/:action', array('plugin' => 'contact', 'controller' => 'contact'));
  358. Router::parse('/');
  359. $result = Router::url(array('plugin' => 'contact', 'controller' => 'contact', 'action' => 'me'));
  360. $expected = '/contact/me';
  361. $this->assertEquals($expected, $result);
  362. Router::reload();
  363. $request = new CakeRequest();
  364. $request->addParams(array(
  365. 'action' => 'index', 'plugin' => 'myplugin', 'controller' => 'mycontroller', 'admin' => false
  366. ));
  367. $request->base = '/';
  368. $request->here = '/';
  369. $request->webroot = '/';
  370. Router::setRequestInfo($request);
  371. $result = Router::url(array('plugin' => null, 'controller' => 'myothercontroller'));
  372. $expected = '/myothercontroller';
  373. $this->assertEquals($expected, $result);
  374. }
  375. /**
  376. * Test that catch all routes work with a variety of falsey inputs.
  377. *
  378. * @return void
  379. */
  380. public function testUrlCatchAllRoute() {
  381. Router::connect('/*', array('controller' => 'categories', 'action' => 'index'));
  382. $result = Router::url(array('controller' => 'categories', 'action' => 'index', '0'));
  383. $this->assertEquals('/0', $result);
  384. $expected = array(
  385. 'plugin' => null,
  386. 'controller' => 'categories',
  387. 'action' => 'index',
  388. 'pass' => array('0'),
  389. 'named' => array()
  390. );
  391. $result = Router::parse('/0');
  392. $this->assertEquals($expected, $result);
  393. $result = Router::parse('0');
  394. $this->assertEquals($expected, $result);
  395. }
  396. /**
  397. * Tests using arrays in named parameters
  398. *
  399. * @return void
  400. */
  401. public function testArrayNamedParameters() {
  402. $result = Router::url(array('controller' => 'tests', 'pages' => array(
  403. 1, 2, 3
  404. )));
  405. $expected = '/tests/index/pages%5B0%5D:1/pages%5B1%5D:2/pages%5B2%5D:3';
  406. $this->assertEquals($expected, $result);
  407. $result = Router::url(array('controller' => 'tests',
  408. 'pages' => array(
  409. 'param1' => array(
  410. 'one',
  411. 'two'
  412. ),
  413. 'three'
  414. )
  415. ));
  416. $expected = '/tests/index/pages%5Bparam1%5D%5B0%5D:one/pages%5Bparam1%5D%5B1%5D:two/pages%5B0%5D:three';
  417. $this->assertEquals($expected, $result);
  418. $result = Router::url(array('controller' => 'tests',
  419. 'pages' => array(
  420. 'param1' => array(
  421. 'one' => 1,
  422. 'two' => 2
  423. ),
  424. 'three'
  425. )
  426. ));
  427. $expected = '/tests/index/pages%5Bparam1%5D%5Bone%5D:1/pages%5Bparam1%5D%5Btwo%5D:2/pages%5B0%5D:three';
  428. $this->assertEquals($expected, $result);
  429. $result = Router::url(array('controller' => 'tests',
  430. 'super' => array(
  431. 'nested' => array(
  432. 'array' => 'awesome',
  433. 'something' => 'else'
  434. ),
  435. 'cool'
  436. )
  437. ));
  438. $expected = '/tests/index/super%5Bnested%5D%5Barray%5D:awesome/super%5Bnested%5D%5Bsomething%5D:else/super%5B0%5D:cool';
  439. $this->assertEquals($expected, $result);
  440. $result = Router::url(array('controller' => 'tests', 'namedParam' => array(
  441. 'keyed' => 'is an array',
  442. 'test'
  443. )));
  444. $expected = '/tests/index/namedParam%5Bkeyed%5D:is%20an%20array/namedParam%5B0%5D:test';
  445. $this->assertEquals($expected, $result);
  446. }
  447. /**
  448. * Test generation of routes with query string parameters.
  449. *
  450. * @return void
  451. */
  452. public function testUrlGenerationWithQueryStrings() {
  453. $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
  454. $expected = '/posts/index/0?var=test&var2=test2';
  455. $this->assertEquals($expected, $result);
  456. $result = Router::url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2'));
  457. $this->assertEquals($expected, $result);
  458. $result = Router::url(array('controller' => 'posts', '0', '?' => array('var' => 'test', 'var2' => 'test2')));
  459. $this->assertEquals($expected, $result);
  460. $result = Router::url(array('controller' => 'posts', '0', '?' => array('var' => null)));
  461. $this->assertEquals('/posts/index/0', $result);
  462. $result = Router::url(array('controller' => 'posts', '0', '?' => 'var=test&var2=test2', '#' => 'unencoded string %'));
  463. $expected = '/posts/index/0?var=test&var2=test2#unencoded string %';
  464. $this->assertEquals($expected, $result);
  465. }
  466. /**
  467. * test that regex validation of keyed route params is working.
  468. *
  469. * @return void
  470. */
  471. public function testUrlGenerationWithRegexQualifiedParams() {
  472. Router::connect(
  473. ':language/galleries',
  474. array('controller' => 'galleries', 'action' => 'index'),
  475. array('language' => '[a-z]{3}')
  476. );
  477. Router::connect(
  478. '/:language/:admin/:controller/:action/*',
  479. array('admin' => 'admin'),
  480. array('language' => '[a-z]{3}', 'admin' => 'admin')
  481. );
  482. Router::connect('/:language/:controller/:action/*',
  483. array(),
  484. array('language' => '[a-z]{3}')
  485. );
  486. $result = Router::url(array('admin' => false, 'language' => 'dan', 'action' => 'index', 'controller' => 'galleries'));
  487. $expected = '/dan/galleries';
  488. $this->assertEquals($expected, $result);
  489. $result = Router::url(array('admin' => false, 'language' => 'eng', 'action' => 'index', 'controller' => 'galleries'));
  490. $expected = '/eng/galleries';
  491. $this->assertEquals($expected, $result);
  492. Router::reload();
  493. Router::connect('/:language/pages',
  494. array('controller' => 'pages', 'action' => 'index'),
  495. array('language' => '[a-z]{3}')
  496. );
  497. Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}'));
  498. $result = Router::url(array('language' => 'eng', 'action' => 'index', 'controller' => 'pages'));
  499. $expected = '/eng/pages';
  500. $this->assertEquals($expected, $result);
  501. $result = Router::url(array('language' => 'eng', 'controller' => 'pages'));
  502. $this->assertEquals($expected, $result);
  503. $result = Router::url(array('language' => 'eng', 'controller' => 'pages', 'action' => 'add'));
  504. $expected = '/eng/pages/add';
  505. $this->assertEquals($expected, $result);
  506. Router::reload();
  507. Router::connect('/forestillinger/:month/:year/*',
  508. array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'),
  509. array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')
  510. );
  511. Router::parse('/');
  512. $result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'month' => 10, 'year' => 2007, 'min-forestilling'));
  513. $expected = '/forestillinger/10/2007/min-forestilling';
  514. $this->assertEquals($expected, $result);
  515. Router::reload();
  516. Router::connect('/kalender/:month/:year/*',
  517. array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'),
  518. array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')
  519. );
  520. Router::connect('/kalender/*', array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'));
  521. Router::parse('/');
  522. $result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'min-forestilling'));
  523. $expected = '/kalender/min-forestilling';
  524. $this->assertEquals($expected, $result);
  525. $result = Router::url(array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'min-forestilling'));
  526. $expected = '/kalender/10/2007/min-forestilling';
  527. $this->assertEquals($expected, $result);
  528. Router::reload();
  529. Router::connect('/:controller/:action/*', array(), array(
  530. 'controller' => 'source|wiki|commits|tickets|comments|view',
  531. 'action' => 'branches|history|branch|logs|view|start|add|edit|modify'
  532. ));
  533. }
  534. /**
  535. * Test url generation with an admin prefix
  536. *
  537. * @return void
  538. */
  539. public function testUrlGenerationWithAdminPrefix() {
  540. Configure::write('Routing.prefixes', array('admin'));
  541. Router::reload();
  542. Router::connectNamed(array('event', 'lang'));
  543. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  544. Router::connect('/pages/contact_us', array('controller' => 'pages', 'action' => 'contact_us'));
  545. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  546. Router::connect('/reset/*', array('admin' => true, 'controller' => 'users', 'action' => 'reset'));
  547. Router::connect('/tests', array('controller' => 'tests', 'action' => 'index'));
  548. Router::parseExtensions('rss');
  549. $request = new CakeRequest();
  550. $request->addParams(array(
  551. 'controller' => 'registrations', 'action' => 'admin_index',
  552. 'plugin' => null, 'prefix' => 'admin', 'admin' => true,
  553. 'ext' => 'html'
  554. ));
  555. $request->base = '';
  556. $request->here = '/admin/registrations/index';
  557. $request->webroot = '/';
  558. Router::setRequestInfo($request);
  559. $result = Router::url(array('page' => 2));
  560. $expected = '/admin/registrations/index/page:2';
  561. $this->assertEquals($expected, $result);
  562. Router::reload();
  563. $request = new CakeRequest();
  564. $request->addParams(array(
  565. 'controller' => 'subscriptions', 'action' => 'admin_index',
  566. 'plugin' => null, 'admin' => true,
  567. 'url' => array('url' => 'admin/subscriptions/index/page:2')
  568. ));
  569. $request->base = '/magazine';
  570. $request->here = '/magazine/admin/subscriptions/index/page:2';
  571. $request->webroot = '/magazine/';
  572. Router::setRequestInfo($request);
  573. Router::parse('/');
  574. $result = Router::url(array('page' => 3));
  575. $expected = '/magazine/admin/subscriptions/index/page:3';
  576. $this->assertEquals($expected, $result);
  577. Router::reload();
  578. Router::connect('/admin/subscriptions/:action/*', array('controller' => 'subscribe', 'admin' => true, 'prefix' => 'admin'));
  579. Router::parse('/');
  580. $request = new CakeRequest();
  581. $request->addParams(array(
  582. 'action' => 'admin_index', 'plugin' => null, 'controller' => 'subscribe',
  583. 'admin' => true, 'url' => array('url' => 'admin/subscriptions/edit/1')
  584. ));
  585. $request->base = '/magazine';
  586. $request->here = '/magazine/admin/subscriptions/edit/1';
  587. $request->webroot = '/magazine/';
  588. Router::setRequestInfo($request);
  589. $result = Router::url(array('action' => 'edit', 1));
  590. $expected = '/magazine/admin/subscriptions/edit/1';
  591. $this->assertEquals($expected, $result);
  592. $result = Router::url(array('admin' => true, 'controller' => 'users', 'action' => 'login'));
  593. $expected = '/magazine/admin/users/login';
  594. $this->assertEquals($expected, $result);
  595. Router::reload();
  596. $request = new CakeRequest();
  597. $request->addParams(array(
  598. 'admin' => true, 'action' => 'index', 'plugin' => null, 'controller' => 'users',
  599. 'url' => array('url' => 'users')
  600. ));
  601. $request->base = '/';
  602. $request->here = '/';
  603. $request->webroot = '/';
  604. Router::setRequestInfo($request);
  605. Router::connect('/page/*', array('controller' => 'pages', 'action' => 'view', 'admin' => true, 'prefix' => 'admin'));
  606. Router::parse('/');
  607. $result = Router::url(array('admin' => true, 'controller' => 'pages', 'action' => 'view', 'my-page'));
  608. $expected = '/page/my-page';
  609. $this->assertEquals($expected, $result);
  610. Router::reload();
  611. $request = new CakeRequest();
  612. $request->addParams(array(
  613. 'plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'prefix' => 'admin', 'admin' => true,
  614. 'url' => array('url' => 'admin/pages/add')
  615. ));
  616. $request->base = '';
  617. $request->here = '/admin/pages/add';
  618. $request->webroot = '/';
  619. Router::setRequestInfo($request);
  620. Router::parse('/');
  621. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
  622. $expected = '/admin/pages/add';
  623. $this->assertEquals($expected, $result);
  624. Router::reload();
  625. Router::parse('/');
  626. $request = new CakeRequest();
  627. $request->addParams(array(
  628. 'plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'prefix' => 'admin', 'admin' => true,
  629. 'url' => array('url' => 'admin/pages/add')
  630. ));
  631. $request->base = '';
  632. $request->here = '/admin/pages/add';
  633. $request->webroot = '/';
  634. Router::setRequestInfo($request);
  635. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
  636. $expected = '/admin/pages/add';
  637. $this->assertEquals($expected, $result);
  638. Router::reload();
  639. Router::connect('/admin/:controller/:action/:id', array('admin' => true), array('id' => '[0-9]+'));
  640. Router::parse('/');
  641. $request = new CakeRequest();
  642. Router::setRequestInfo(
  643. $request->addParams(array(
  644. 'plugin' => null, 'controller' => 'pages', 'action' => 'admin_edit', 'pass' => array('284'),
  645. 'prefix' => 'admin', 'admin' => true,
  646. 'url' => array('url' => 'admin/pages/edit/284')
  647. ))->addPaths(array(
  648. 'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/'
  649. ))
  650. );
  651. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 'id' => '284'));
  652. $expected = '/admin/pages/edit/284';
  653. $this->assertEquals($expected, $result);
  654. Router::reload();
  655. Router::parse('/');
  656. $request = new CakeRequest();
  657. Router::setRequestInfo(
  658. $request->addParams(array(
  659. 'plugin' => null, 'controller' => 'pages', 'action' => 'admin_add', 'prefix' => 'admin',
  660. 'admin' => true, 'url' => array('url' => 'admin/pages/add')
  661. ))->addPaths(array(
  662. 'base' => '', 'here' => '/admin/pages/add', 'webroot' => '/'
  663. ))
  664. );
  665. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
  666. $expected = '/admin/pages/add';
  667. $this->assertEquals($expected, $result);
  668. Router::reload();
  669. Router::parse('/');
  670. $request = new CakeRequest();
  671. Router::setRequestInfo(
  672. $request->addParams(array(
  673. 'plugin' => null, 'controller' => 'pages', 'action' => 'admin_edit', 'prefix' => 'admin',
  674. 'admin' => true, 'pass' => array('284'), 'url' => array('url' => 'admin/pages/edit/284')
  675. ))->addPaths(array(
  676. 'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/'
  677. ))
  678. );
  679. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 284));
  680. $expected = '/admin/pages/edit/284';
  681. $this->assertEquals($expected, $result);
  682. Router::reload();
  683. Router::connect('/admin/posts/*', array('controller' => 'posts', 'action' => 'index', 'admin' => true));
  684. Router::parse('/');
  685. Router::setRequestInfo(
  686. $request->addParams(array(
  687. 'plugin' => null, 'controller' => 'posts', 'action' => 'admin_index', 'prefix' => 'admin',
  688. 'admin' => true, 'pass' => array('284'), 'url' => array('url' => 'admin/posts')
  689. ))->addPaths(array(
  690. 'base' => '', 'here' => '/admin/posts', 'webroot' => '/'
  691. ))
  692. );
  693. $result = Router::url(array('all'));
  694. $expected = '/admin/posts/all';
  695. $this->assertEquals($expected, $result);
  696. }
  697. /**
  698. * testUrlGenerationWithExtensions method
  699. *
  700. * @return void
  701. */
  702. public function testUrlGenerationWithExtensions() {
  703. Router::parse('/');
  704. $result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'add', 'id' => null, 'ext' => 'json'));
  705. $expected = '/articles/add.json';
  706. $this->assertEquals($expected, $result);
  707. $result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'add', 'ext' => 'json'));
  708. $expected = '/articles/add.json';
  709. $this->assertEquals($expected, $result);
  710. $result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'id' => null, 'ext' => 'json'));
  711. $expected = '/articles.json';
  712. $this->assertEquals($expected, $result);
  713. $result = Router::url(array('plugin' => null, 'controller' => 'articles', 'action' => 'index', 'ext' => 'json'));
  714. $expected = '/articles.json';
  715. $this->assertEquals($expected, $result);
  716. }
  717. /**
  718. * testPluginUrlGeneration method
  719. *
  720. * @return void
  721. */
  722. public function testUrlGenerationPlugins() {
  723. $request = new CakeRequest();
  724. Router::setRequestInfo(
  725. $request->addParams(array(
  726. 'plugin' => 'test', 'controller' => 'controller', 'action' => 'index'
  727. ))->addPaths(array(
  728. 'base' => '/base', 'here' => '/clients/sage/portal/donations', 'webroot' => '/base/'
  729. ))
  730. );
  731. $this->assertEquals(Router::url('read/1'), '/base/test/controller/read/1');
  732. Router::reload();
  733. Router::connect('/:lang/:plugin/:controller/*', array('action' => 'index'));
  734. $request = new CakeRequest();
  735. Router::setRequestInfo(
  736. $request->addParams(array(
  737. 'lang' => 'en',
  738. 'plugin' => 'shows', 'controller' => 'shows', 'action' => 'index',
  739. 'url' => array('url' => 'en/shows/'),
  740. ))->addPaths(array(
  741. 'base' => '', 'here' => '/en/shows', 'webroot' => '/'
  742. ))
  743. );
  744. Router::parse('/en/shows/');
  745. $result = Router::url(array(
  746. 'lang' => 'en',
  747. 'controller' => 'shows', 'action' => 'index', 'page' => '1',
  748. ));
  749. $expected = '/en/shows/shows/page:1';
  750. $this->assertEquals($expected, $result);
  751. }
  752. /**
  753. * test that you can leave active plugin routes with plugin = null
  754. *
  755. * @return void
  756. */
  757. public function testCanLeavePlugin() {
  758. Router::reload();
  759. Router::connect(
  760. '/admin/other/:controller/:action/*',
  761. array(
  762. 'admin' => 1,
  763. 'plugin' => 'aliased',
  764. 'prefix' => 'admin'
  765. )
  766. );
  767. $request = new CakeRequest();
  768. Router::setRequestInfo(
  769. $request->addParams(array(
  770. 'pass' => array(),
  771. 'admin' => true,
  772. 'prefix' => 'admin',
  773. 'plugin' => 'this',
  774. 'action' => 'admin_index',
  775. 'controller' => 'interesting',
  776. 'url' => array('url' => 'admin/this/interesting/index'),
  777. ))->addPaths(array(
  778. 'base' => '',
  779. 'here' => '/admin/this/interesting/index',
  780. 'webroot' => '/',
  781. ))
  782. );
  783. $result = Router::url(array('plugin' => null, 'controller' => 'posts', 'action' => 'index'));
  784. $this->assertEquals('/admin/posts', $result);
  785. $result = Router::url(array('controller' => 'posts', 'action' => 'index'));
  786. $this->assertEquals('/admin/this/posts', $result);
  787. $result = Router::url(array('plugin' => 'aliased', 'controller' => 'posts', 'action' => 'index'));
  788. $this->assertEquals('/admin/other/posts/index', $result);
  789. }
  790. /**
  791. * testUrlParsing method
  792. *
  793. * @return void
  794. */
  795. public function testUrlParsing() {
  796. extract(Router::getNamedExpressions());
  797. Router::connect('/posts/:value/:somevalue/:othervalue/*', array('controller' => 'posts', 'action' => 'view'), array('value','somevalue', 'othervalue'));
  798. $result = Router::parse('/posts/2007/08/01/title-of-post-here');
  799. $expected = array('value' => '2007', 'somevalue' => '08', 'othervalue' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
  800. $this->assertEquals($expected, $result);
  801. Router::reload();
  802. Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
  803. $result = Router::parse('/posts/2007/08/01/title-of-post-here');
  804. $expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
  805. $this->assertEquals($expected, $result);
  806. Router::reload();
  807. Router::connect('/posts/:day/:year/:month/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
  808. $result = Router::parse('/posts/01/2007/08/title-of-post-here');
  809. $expected = array('day' => '01', 'year' => '2007', 'month' => '08', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
  810. $this->assertEquals($expected, $result);
  811. Router::reload();
  812. Router::connect('/posts/:month/:day/:year/*', array('controller' => 'posts', 'action' => 'view'), array('year' => $Year, 'month' => $Month, 'day' => $Day));
  813. $result = Router::parse('/posts/08/01/2007/title-of-post-here');
  814. $expected = array('month' => '08', 'day' => '01', 'year' => '2007', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
  815. $this->assertEquals($expected, $result);
  816. Router::reload();
  817. Router::connect('/posts/:year/:month/:day/*', array('controller' => 'posts', 'action' => 'view'));
  818. $result = Router::parse('/posts/2007/08/01/title-of-post-here');
  819. $expected = array('year' => '2007', 'month' => '08', 'day' => '01', 'controller' => 'posts', 'action' => 'view', 'plugin' => '', 'pass' => array('0' => 'title-of-post-here'), 'named' => array());
  820. $this->assertEquals($expected, $result);
  821. Router::reload();
  822. require CAKE . 'Config' . DS . 'routes.php';
  823. $result = Router::parse('/pages/display/home');
  824. $expected = array('plugin' => null, 'pass' => array('home'), 'controller' => 'pages', 'action' => 'display', 'named' => array());
  825. $this->assertEquals($expected, $result);
  826. $result = Router::parse('pages/display/home/');
  827. $this->assertEquals($expected, $result);
  828. $result = Router::parse('pages/display/home');
  829. $this->assertEquals($expected, $result);
  830. Router::reload();
  831. Router::connect('/page/*', array('controller' => 'test'));
  832. $result = Router::parse('/page/my-page');
  833. $expected = array('pass' => array('my-page'), 'plugin' => null, 'controller' => 'test', 'action' => 'index', 'named' => array());
  834. $this->assertEquals($expected, $result);
  835. Router::reload();
  836. Router::connect('/:language/contact', array('language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'), array('language' => '[a-z]{3}'));
  837. $result = Router::parse('/eng/contact');
  838. $expected = array('pass' => array(), 'named' => array(), 'language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index');
  839. $this->assertEquals($expected, $result);
  840. Router::reload();
  841. Router::connect('/forestillinger/:month/:year/*',
  842. array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'),
  843. array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')
  844. );
  845. $result = Router::parse('/forestillinger/10/2007/min-forestilling');
  846. $expected = array('pass' => array('min-forestilling'), 'plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar', 'year' => 2007, 'month' => 10, 'named' => array());
  847. $this->assertEquals($expected, $result);
  848. Router::reload();
  849. Router::connect('/:controller/:action/*');
  850. Router::connect('/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'display'));
  851. $result = Router::parse('/');
  852. $expected = array('pass' => array(), 'named' => array(), 'controller' => 'pages', 'action' => 'display', 'plugin' => 'pages');
  853. $this->assertEquals($expected, $result);
  854. $result = Router::parse('/posts/edit/0');
  855. $expected = array('pass' => array(0), 'named' => array(), 'controller' => 'posts', 'action' => 'edit', 'plugin' => null);
  856. $this->assertEquals($expected, $result);
  857. Router::reload();
  858. Router::connect('/posts/:id::url_title', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\d]+'));
  859. $result = Router::parse('/posts/5:sample-post-title');
  860. $expected = array('pass' => array('5', 'sample-post-title'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
  861. $this->assertEquals($expected, $result);
  862. Router::reload();
  863. Router::connect('/posts/:id::url_title/*', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => '[\d]+'));
  864. $result = Router::parse('/posts/5:sample-post-title/other/params/4');
  865. $expected = array('pass' => array('5', 'sample-post-title', 'other', 'params', '4'), 'named' => array(), 'id' => 5, 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
  866. $this->assertEquals($expected, $result);
  867. Router::reload();
  868. Router::connect('/posts/:url_title-(uuid::id)', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('id', 'url_title'), 'id' => $UUID));
  869. $result = Router::parse('/posts/sample-post-title-(uuid:47fc97a9-019c-41d1-a058-1fa3cbdd56cb)');
  870. $expected = array('pass' => array('47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'sample-post-title'), 'named' => array(), 'id' => '47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'url_title' => 'sample-post-title', 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
  871. $this->assertEquals($expected, $result);
  872. Router::reload();
  873. Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => false));
  874. $result = Router::parse('/posts/view/foo:bar/routing:fun');
  875. $expected = array('pass' => array('foo:bar', 'routing:fun'), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
  876. $this->assertEquals($expected, $result);
  877. Router::reload();
  878. Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer')));
  879. $result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42');
  880. $expected = array('pass' => array('routing:fun'), 'named' => array('foo' => 'bar', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
  881. $this->assertEquals($expected, $result);
  882. Router::reload();
  883. Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('named' => array('foo', 'answer'), 'greedyNamed' => true));
  884. $result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42');
  885. $expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
  886. $this->assertEquals($expected, $result);
  887. }
  888. /**
  889. * test that the persist key works.
  890. *
  891. * @return void
  892. */
  893. public function testPersistentParameters() {
  894. Router::reload();
  895. Router::connect(
  896. '/:lang/:color/posts/view/*',
  897. array('controller' => 'posts', 'action' => 'view'),
  898. array('persist' => array('lang', 'color')
  899. ));
  900. Router::connect(
  901. '/:lang/:color/posts/index',
  902. array('controller' => 'posts', 'action' => 'index'),
  903. array('persist' => array('lang')
  904. ));
  905. Router::connect('/:lang/:color/posts/edit/*', array('controller' => 'posts', 'action' => 'edit'));
  906. Router::connect('/about', array('controller' => 'pages', 'action' => 'view', 'about'));
  907. Router::parse('/en/red/posts/view/5');
  908. $request = new CakeRequest();
  909. Router::setRequestInfo(
  910. $request->addParams(array(
  911. 'lang' => 'en',
  912. 'color' => 'red',
  913. 'prefix' => 'admin',
  914. 'plugin' => null,
  915. 'action' => 'view',
  916. 'controller' => 'posts',
  917. ))->addPaths(array(
  918. 'base' => '/',
  919. 'here' => '/en/red/posts/view/5',
  920. 'webroot' => '/',
  921. ))
  922. );
  923. $expected = '/en/red/posts/view/6';
  924. $result = Router::url(array('controller' => 'posts', 'action' => 'view', 6));
  925. $this->assertEquals($expected, $result);
  926. $expected = '/en/blue/posts/index';
  927. $result = Router::url(array('controller' => 'posts', 'action' => 'index', 'color' => 'blue'));
  928. $this->assertEquals($expected, $result);
  929. $expected = '/posts/edit/6';
  930. $result = Router::url(array('controller' => 'posts', 'action' => 'edit', 6, 'color' => null, 'lang' => null));
  931. $this->assertEquals($expected, $result);
  932. $expected = '/posts';
  933. $result = Router::url(array('controller' => 'posts', 'action' => 'index'));
  934. $this->assertEquals($expected, $result);
  935. $expected = '/posts/edit/7';
  936. $result = Router::url(array('controller' => 'posts', 'action' => 'edit', 7));
  937. $this->assertEquals($expected, $result);
  938. $expected = '/about';
  939. $result = Router::url(array('controller' => 'pages', 'action' => 'view', 'about'));
  940. $this->assertEquals($expected, $result);
  941. }
  942. /**
  943. * testUuidRoutes method
  944. *
  945. * @return void
  946. */
  947. public function testUuidRoutes() {
  948. Router::connect(
  949. '/subjects/add/:category_id',
  950. array('controller' => 'subjects', 'action' => 'add'),
  951. array('category_id' => '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}')
  952. );
  953. $result = Router::parse('/subjects/add/4795d601-19c8-49a6-930e-06a8b01d17b7');
  954. $expected = array('pass' => array(), 'named' => array(), 'category_id' => '4795d601-19c8-49a6-930e-06a8b01d17b7', 'plugin' => null, 'controller' => 'subjects', 'action' => 'add');
  955. $this->assertEquals($expected, $result);
  956. }
  957. /**
  958. * testRouteSymmetry method
  959. *
  960. * @return void
  961. */
  962. public function testRouteSymmetry() {
  963. Router::connect(
  964. "/:extra/page/:slug/*",
  965. array('controller' => 'pages', 'action' => 'view', 'extra' => null),
  966. array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
  967. );
  968. $result = Router::parse('/some_extra/page/this_is_the_slug');
  969. $expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => 'some_extra');
  970. $this->assertEquals($expected, $result);
  971. $result = Router::parse('/page/this_is_the_slug');
  972. $expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null);
  973. $this->assertEquals($expected, $result);
  974. Router::reload();
  975. Router::connect(
  976. "/:extra/page/:slug/*",
  977. array('controller' => 'pages', 'action' => 'view', 'extra' => null),
  978. array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+')
  979. );
  980. Router::parse('/');
  981. $result = Router::url(array('admin' => null, 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => null));
  982. $expected = '/page/this_is_the_slug';
  983. $this->assertEquals($expected, $result);
  984. $result = Router::url(array('admin' => null, 'plugin' => null, 'controller' => 'pages', 'action' => 'view', 'slug' => 'this_is_the_slug', 'extra' => 'some_extra'));
  985. $expected = '/some_extra/page/this_is_the_slug';
  986. $this->assertEquals($expected, $result);
  987. }
  988. /**
  989. * Test that Routing.prefixes are used when a Router instance is created
  990. * or reset
  991. *
  992. * @return void
  993. */
  994. public function testRoutingPrefixesSetting() {
  995. $restore = Configure::read('Routing');
  996. Configure::write('Routing.prefixes', array('admin', 'member', 'super_user'));
  997. Router::reload();
  998. $result = Router::prefixes();
  999. $expected = array('admin', 'member', 'super_user');
  1000. $this->assertEquals($expected, $result);
  1001. Configure::write('Routing.prefixes', array('admin', 'member'));
  1002. Router::reload();
  1003. $result = Router::prefixes();
  1004. $expected = array('admin', 'member');
  1005. $this->assertEquals($expected, $result);
  1006. Configure::write('Routing', $restore);
  1007. }
  1008. /**
  1009. * Test prefix routing and plugin combinations
  1010. *
  1011. * @return void
  1012. */
  1013. public function testPrefixRoutingAndPlugins() {
  1014. Configure::write('Routing.prefixes', array('admin'));
  1015. $paths = App::path('plugins');
  1016. App::build(array(
  1017. 'plugins' => array(
  1018. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  1019. )
  1020. ), App::RESET);
  1021. CakePlugin::load(array('TestPlugin'));
  1022. Router::reload();
  1023. require CAKE . 'Config' . DS . 'routes.php';
  1024. $request = new CakeRequest();
  1025. Router::setRequestInfo(
  1026. $request->addParams(array(
  1027. 'admin' => true, 'controller' => 'controller', 'action' => 'action',
  1028. 'plugin' => null, 'prefix' => 'admin'
  1029. ))->addPaths(array(
  1030. 'base' => '/',
  1031. 'here' => '/',
  1032. 'webroot' => '/base/',
  1033. ))
  1034. );
  1035. Router::parse('/');
  1036. $result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index'));
  1037. $expected = '/admin/test_plugin';
  1038. $this->assertEquals($expected, $result);
  1039. Router::reload();
  1040. require CAKE . 'Config' . DS . 'routes.php';
  1041. $request = new CakeRequest();
  1042. Router::setRequestInfo(
  1043. $request->addParams(array(
  1044. 'plugin' => 'test_plugin', 'controller' => 'show_tickets', 'action' => 'admin_edit',
  1045. 'pass' => array('6'), 'prefix' => 'admin', 'admin' => true, 'form' => array(),
  1046. 'url' => array('url' => 'admin/shows/show_tickets/edit/6')
  1047. ))->addPaths(array(
  1048. 'base' => '/',
  1049. 'here' => '/admin/shows/show_tickets/edit/6',
  1050. 'webroot' => '/',
  1051. ))
  1052. );
  1053. $result = Router::url(array(
  1054. 'plugin' => 'test_plugin', 'controller' => 'show_tickets', 'action' => 'edit', 6,
  1055. 'admin' => true, 'prefix' => 'admin'
  1056. ));
  1057. $expected = '/admin/test_plugin/show_tickets/edit/6';
  1058. $this->assertEquals($expected, $result);
  1059. $result = Router::url(array(
  1060. 'plugin' => 'test_plugin', 'controller' => 'show_tickets', 'action' => 'index', 'admin' => true
  1061. ));
  1062. $expected = '/admin/test_plugin/show_tickets';
  1063. $this->assertEquals($expected, $result);
  1064. App::build(array('plugins' => $paths));
  1065. }
  1066. /**
  1067. * testParseExtensions method
  1068. *
  1069. * @return void
  1070. */
  1071. public function testParseExtensions() {
  1072. $this->assertEquals(array(), Router::extensions());
  1073. Router::parseExtensions('rss');
  1074. $this->assertEquals(array('rss'), Router::extensions());
  1075. }
  1076. /**
  1077. * testSetExtensions method
  1078. *
  1079. * @return void
  1080. */
  1081. public function testSetExtensions() {
  1082. Router::setExtensions(array('rss'));
  1083. $this->assertEquals(array('rss'), Router::extensions());
  1084. require CAKE . 'Config' . DS . 'routes.php';
  1085. $result = Router::parse('/posts.rss');
  1086. $this->assertFalse(isset($result['ext']));
  1087. Router::parseExtensions();
  1088. $result = Router::parse('/posts.rss');
  1089. $this->assertEquals('rss', $result['ext']);
  1090. $result = Router::parse('/posts.xml');
  1091. $this->assertFalse(isset($result['ext']));
  1092. Router::setExtensions(array('xml'));
  1093. $result = Router::extensions();
  1094. $this->assertEquals(array('rss', 'xml'), $result);
  1095. $result = Router::parse('/posts.xml');
  1096. $this->assertEquals('xml', $result['ext']);
  1097. $result = Router::setExtensions(array('pdf'), false);
  1098. $this->assertEquals(array('pdf'), $result);
  1099. }
  1100. /**
  1101. * testExtensionParsing method
  1102. *
  1103. * @return void
  1104. */
  1105. public function testExtensionParsing() {
  1106. Router::parseExtensions();
  1107. require CAKE . 'Config' . DS . 'routes.php';
  1108. $result = Router::parse('/posts.rss');
  1109. $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'ext' => 'rss', 'pass' => array(), 'named' => array());
  1110. $this->assertEquals($expected, $result);
  1111. $result = Router::parse('/posts/view/1.rss');
  1112. $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'pass' => array('1'), 'named' => array(), 'ext' => 'rss');
  1113. $this->assertEquals($expected, $result);
  1114. $result = Router::parse('/posts/view/1.rss?query=test');
  1115. $this->assertEquals($expected, $result);
  1116. $result = Router::parse('/posts/view/1.atom');
  1117. $expected['ext'] = 'atom';
  1118. $this->assertEquals($expected, $result);
  1119. Router::reload();
  1120. require CAKE . 'Config' . DS . 'routes.php';
  1121. Router::parseExtensions('rss', 'xml');
  1122. $result = Router::parse('/posts.xml');
  1123. $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'ext' => 'xml', 'pass' => array(), 'named' => array());
  1124. $this->assertEquals($expected, $result);
  1125. $result = Router::parse('/posts.atom?hello=goodbye');
  1126. $expected = array('plugin' => null, 'controller' => 'posts.atom', 'action' => 'index', 'pass' => array(), 'named' => array());
  1127. $this->assertEquals($expected, $result);
  1128. Router::reload();
  1129. Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'ext' => 'rss'));
  1130. $result = Router::parse('/controller/action');
  1131. $expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'ext' => 'rss', 'named' => array(), 'pass' => array());
  1132. $this->assertEquals($expected, $result);
  1133. Router::reload();
  1134. Router::parseExtensions('rss');
  1135. Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', 'ext' => 'rss'));
  1136. $result = Router::parse('/controller/action');
  1137. $expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, 'ext' => 'rss', 'named' => array(), 'pass' => array());
  1138. $this->assertEquals($expected, $result);
  1139. }
  1140. /**
  1141. * testQuerystringGeneration method
  1142. *
  1143. * @return void
  1144. */
  1145. public function testQuerystringGeneration() {
  1146. $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => 'var=test&var2=test2'));
  1147. $expected = '/posts/index/0?var=test&var2=test2';
  1148. $this->assertEquals($expected, $result);
  1149. $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')));
  1150. $this->assertEquals($expected, $result);
  1151. $expected .= '&more=test+data';
  1152. $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data')));
  1153. $this->assertEquals($expected, $result);
  1154. // Test bug #4614
  1155. $restore = ini_get('arg_separator.output');
  1156. ini_set('arg_separator.output', '&amp;');
  1157. $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data')));
  1158. $this->assertEquals($expected, $result);
  1159. ini_set('arg_separator.output', $restore);
  1160. $result = Router::url(array('controller' => 'posts', 'action' => 'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')), array('escape' => true));
  1161. $expected = '/posts/index/0?var=test&amp;var2=test2';
  1162. $this->assertEquals($expected, $result);
  1163. }
  1164. /**
  1165. * testConnectNamed method
  1166. *
  1167. * @return void
  1168. */
  1169. public function testConnectNamed() {
  1170. $named = Router::connectNamed(false, array('default' => true));
  1171. $this->assertFalse($named['greedyNamed']);
  1172. $this->assertEquals(array_keys($named['rules']), $named['default']);
  1173. Router::reload();
  1174. Router::connect('/foo/*', array('controller' => 'bar', 'action' => 'fubar'));
  1175. Router::connectNamed(array(), array('separator' => '='));
  1176. $result = Router::parse('/foo/param1=value1/param2=value2');
  1177. $expected = array('pass' => array(), 'named' => array('param1' => 'value1', 'param2' => 'value2'), 'controller' => 'bar', 'action' => 'fubar', 'plugin' => null);
  1178. $this->assertEquals($expected, $result);
  1179. Router::reload();
  1180. Router::connect('/controller/action/*', array('controller' => 'controller', 'action' => 'action'), array('named' => array('param1' => 'value[\d]')));
  1181. Router::connectNamed(array(), array('greedy' => false, 'separator' => '='));
  1182. $result = Router::parse('/controller/action/param1=value1/param2=value2');
  1183. $expected = array('pass' => array('param2=value2'), 'named' => array('param1' => 'value1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
  1184. $this->assertEquals($expected, $result);
  1185. Router::reload();
  1186. Router::connect('/:controller/:action/*');
  1187. Router::connectNamed(array('page'), array('default' => false, 'greedy' => false));
  1188. $result = Router::parse('/categories/index/limit=5');
  1189. $this->assertTrue(empty($result['named']));
  1190. }
  1191. /**
  1192. * testNamedArgsUrlGeneration method
  1193. *
  1194. * @return void
  1195. */
  1196. public function testNamedArgsUrlGeneration() {
  1197. $result = Router::url(array('controller' => 'posts', 'action' => 'index', 'published' => 1, 'deleted' => 1));
  1198. $expected = '/posts/index/published:1/deleted:1';
  1199. $this->assertEquals($expected, $result);
  1200. $result = Router::url(array('controller' => 'posts', 'action' => 'index', 'published' => 0, 'deleted' => 0));
  1201. $expected = '/posts/index/published:0/deleted:0';
  1202. $this->assertEquals($expected, $result);
  1203. Router::reload();
  1204. extract(Router::getNamedExpressions());
  1205. Router::connectNamed(array('file' => '[\w\.\-]+\.(html|png)'));
  1206. Router::connect('/', array('controller' => 'graphs', 'action' => 'index'));
  1207. Router::connect('/:id/*', array('controller' => 'graphs', 'action' => 'view'), array('id' => $ID));
  1208. $result = Router::url(array('controller' => 'graphs', 'action' => 'view', 'id' => 12, 'file' => 'asdf.png'));
  1209. $expected = '/12/file:asdf.png';
  1210. $this->assertEquals($expected, $result);
  1211. $result = Router::url(array('controller' => 'graphs', 'action' => 'view', 12, 'file' => 'asdf.foo'));
  1212. $expected = '/graphs/view/12/file:asdf.foo';
  1213. $this->assertEquals($expected, $result);
  1214. Configure::write('Routing.prefixes', array('admin'));
  1215. Router::reload();
  1216. $request = new CakeRequest();
  1217. Router::setRequestInfo(
  1218. $request->addParams(array(
  1219. 'admin' => true, 'controller' => 'controller', 'action' => 'index', 'plugin' => null
  1220. ))->addPaths(array(
  1221. 'base' => '/',
  1222. 'here' => '/',
  1223. 'webroot' => '/base/',
  1224. ))
  1225. );
  1226. Router::parse('/');
  1227. $result = Router::url(array('page' => 1, 0 => null, 'sort' => 'controller', 'direction' => 'asc', 'order' => null));
  1228. $expected = "/admin/controller/index/page:1/sort:controller/direction:asc";
  1229. $this->assertEquals($expected, $result);
  1230. Router::reload();
  1231. $request = new CakeRequest('admin/controller/index');
  1232. $request->addParams(array(
  1233. 'admin' => true, 'controller' => 'controller', 'action' => 'index', 'plugin' => null
  1234. ));
  1235. $request->base = '/';
  1236. Router::setRequestInfo($request);
  1237. $result = Router::parse('/admin/controller/index/type:whatever');
  1238. $result = Router::url(array('type' => 'new'));
  1239. $expected = "/admin/controller/index/type:new";
  1240. $this->assertEquals($expected, $result);
  1241. }
  1242. /**
  1243. * testNamedArgsUrlParsing method
  1244. *
  1245. * @return void
  1246. */
  1247. public function testNamedArgsUrlParsing() {
  1248. Router::reload();
  1249. require CAKE . 'Config' . DS . 'routes.php';
  1250. $result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
  1251. $expected = array('pass' => array(), 'named' => array('param1' => 'value1:1', 'param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
  1252. $this->assertEquals($expected, $result);
  1253. Router::reload();
  1254. require CAKE . 'Config' . DS . 'routes.php';
  1255. $result = Router::connectNamed(false);
  1256. $this->assertEquals(array(), array_keys($result['rules']));
  1257. $this->assertFalse($result['greedyNamed']);
  1258. $result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
  1259. $expected = array('pass' => array('param1:value1:1', 'param2:value2:3', 'param:value'), 'named' => array(), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
  1260. $this->assertEquals($expected, $result);
  1261. Router::reload();
  1262. require CAKE . 'Config' . DS . 'routes.php';
  1263. $result = Router::connectNamed(true);
  1264. $named = Router::namedConfig();
  1265. $this->assertEquals($named['default'], array_keys($result['rules']));
  1266. $this->assertTrue($result['greedyNamed']);
  1267. Router::reload();
  1268. require CAKE . 'Config' . DS . 'routes.php';
  1269. Router::connectNamed(array('param1' => 'not-matching'));
  1270. $result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value');
  1271. $expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
  1272. $this->assertEquals($expected, $result);
  1273. $result = Router::parse('/foo/view/param1:value1:1/param2:value2:3/param:value');
  1274. $expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'foo', 'action' => 'view', 'plugin' => null);
  1275. $this->assertEquals($expected, $result);
  1276. Router::reload();
  1277. require CAKE . 'Config' . DS . 'routes.php';
  1278. Router::connectNamed(array('param1' => '[\d]', 'param2' => '[a-z]', 'param3' => '[\d]'));
  1279. $result = Router::parse('/controller/action/param1:1/param2:2/param3:3');
  1280. $expected = array('pass' => array('param2:2'), 'named' => array('param1' => '1', 'param3' => '3'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
  1281. $this->assertEquals($expected, $result);
  1282. Router::reload();
  1283. require CAKE . 'Config' . DS . 'routes.php';
  1284. Router::connectNamed(array('param1' => '[\d]', 'param2' => true, 'param3' => '[\d]'));
  1285. $result = Router::parse('/controller/action/param1:1/param2:2/param3:3');
  1286. $expected = array('pass' => array(), 'named' => array('param1' => '1', 'param2' => '2', 'param3' => '3'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
  1287. $this->assertEquals($expected, $result);
  1288. Router::reload();
  1289. require CAKE . 'Config' . DS . 'routes.php';
  1290. Router::connectNamed(array('param1' => 'value[\d]+:[\d]+'), array('greedy' => false));
  1291. $result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param3:value');
  1292. $expected = array('pass' => array('param2:value2:3', 'param3:value'), 'named' => array('param1' => 'value1:1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null);
  1293. $this->assertEquals($expected, $result);
  1294. }
  1295. /**
  1296. * test url generation with legacy (1.2) style prefix routes.
  1297. *
  1298. * @return void
  1299. * @see testUrlGenerationWithAutoPrefixes
  1300. */
  1301. public function testUrlGenerationWithLegacyPrefixes() {
  1302. Router::reload();
  1303. Router::connect('/protected/:controller/:action/*', array(
  1304. 'prefix' => 'protected',
  1305. 'protected' => true
  1306. ));
  1307. Router::parse('/');
  1308. $request = new CakeRequest();
  1309. Router::setRequestInfo(
  1310. $request->addParams(array(
  1311. 'plugin' => null, 'controller' => 'images', 'action' => 'index',
  1312. 'prefix' => null, 'admin' => false,'url' => array('url' => 'images/index')
  1313. ))->addPaths(array(
  1314. 'base' => '',
  1315. 'here' => '/images/index',
  1316. 'webroot' => '/',
  1317. ))
  1318. );
  1319. $result = Router::url(array('protected' => true));
  1320. $expected = '/protected/images/index';
  1321. $this->assertEquals($expected, $result);
  1322. $result = Router::url(array('controller' => 'images', 'action' => 'add'));
  1323. $expected = '/images/add';
  1324. $this->assertEquals($expected, $result);
  1325. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true));
  1326. $expected = '/protected/images/add';
  1327. $this->assertEquals($expected, $result);
  1328. $result = Router::url(array('action' => 'edit', 1));
  1329. $expected = '/images/edit/1';
  1330. $this->assertEquals($expected, $result);
  1331. $result = Router::url(array('action' => 'edit', 1, 'protected' => true));
  1332. $expected = '/protected/images/edit/1';
  1333. $this->assertEquals($expected, $result);
  1334. $result = Router::url(array('action' => 'protected_edit', 1, 'protected' => true));
  1335. $expected = '/protected/images/edit/1';
  1336. $this->assertEquals($expected, $result);
  1337. $result = Router::url(array('action' => 'edit', 1, 'protected' => true));
  1338. $expected = '/protected/images/edit/1';
  1339. $this->assertEquals($expected, $result);
  1340. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1));
  1341. $expected = '/others/edit/1';
  1342. $this->assertEquals($expected, $result);
  1343. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true));
  1344. $expected = '/protected/others/edit/1';
  1345. $this->assertEquals($expected, $result);
  1346. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'page' => 1));
  1347. $expected = '/protected/others/edit/1/page:1';
  1348. $this->assertEquals($expected, $result);
  1349. Router::connectNamed(array('random'));
  1350. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'random' => 'my-value'));
  1351. $expected = '/protected/others/edit/1/random:my-value';
  1352. $this->assertEquals($expected, $result);
  1353. }
  1354. /**
  1355. * test newer style automatically generated prefix routes.
  1356. *
  1357. * @return void
  1358. */
  1359. public function testUrlGenerationWithAutoPrefixes() {
  1360. Configure::write('Routing.prefixes', array('protected'));
  1361. Router::reload();
  1362. Router::parse('/');
  1363. $request = new CakeRequest();
  1364. Router::setRequestInfo(
  1365. $request->addParams(array(
  1366. 'plugin' => null, 'controller' => 'images', 'action' => 'index',
  1367. 'prefix' => null, 'protected' => false, 'url' => array('url' => 'images/index')
  1368. ))->addPaths(array(
  1369. 'base' => '',
  1370. 'here' => '/images/index',
  1371. 'webroot' => '/',
  1372. ))
  1373. );
  1374. $result = Router::url(array('controller' => 'images', 'action' => 'add'));
  1375. $expected = '/images/add';
  1376. $this->assertEquals($expected, $result);
  1377. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true));
  1378. $expected = '/protected/images/add';
  1379. $this->assertEquals($expected, $result);
  1380. $result = Router::url(array('controller' => 'images', 'action' => 'add_protected_test', 'protected' => true));
  1381. $expected = '/protected/images/add_protected_test';
  1382. $this->assertEquals($expected, $result);
  1383. $result = Router::url(array('action' => 'edit', 1));
  1384. $expected = '/images/edit/1';
  1385. $this->assertEquals($expected, $result);
  1386. $result = Router::url(array('action' => 'edit', 1, 'protected' => true));
  1387. $expected = '/protected/images/edit/1';
  1388. $this->assertEquals($expected, $result);
  1389. $result = Router::url(array('action' => 'protected_edit', 1, 'protected' => true));
  1390. $expected = '/protected/images/edit/1';
  1391. $this->assertEquals($expected, $result);
  1392. $result = Router::url(array('action' => 'protectededit', 1, 'protected' => true));
  1393. $expected = '/protected/images/protectededit/1';
  1394. $this->assertEquals($expected, $result);
  1395. $result = Router::url(array('action' => 'edit', 1, 'protected' => true));
  1396. $expected = '/protected/images/edit/1';
  1397. $this->assertEquals($expected, $result);
  1398. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1));
  1399. $expected = '/others/edit/1';
  1400. $this->assertEquals($expected, $result);
  1401. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true));
  1402. $expected = '/protected/others/edit/1';
  1403. $this->assertEquals($expected, $result);
  1404. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'page' => 1));
  1405. $expected = '/protected/others/edit/1/page:1';
  1406. $this->assertEquals($expected, $result);
  1407. Router::connectNamed(array('random'));
  1408. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'protected' => true, 'random' => 'my-value'));
  1409. $expected = '/protected/others/edit/1/random:my-value';
  1410. $this->assertEquals($expected, $result);
  1411. }
  1412. /**
  1413. * test that auto-generated prefix routes persist
  1414. *
  1415. * @return void
  1416. */
  1417. public function testAutoPrefixRoutePersistence() {
  1418. Configure::write('Routing.prefixes', array('protected'));
  1419. Router::reload();
  1420. Router::parse('/');
  1421. $request = new CakeRequest();
  1422. Router::setRequestInfo(
  1423. $request->addParams(array(
  1424. 'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'protected',
  1425. 'protected' => true, 'url' => array('url' => 'protected/images/index')
  1426. ))->addPaths(array(
  1427. 'base' => '',
  1428. 'here' => '/protected/images/index',
  1429. 'webroot' => '/',
  1430. ))
  1431. );
  1432. $result = Router::url(array('controller' => 'images', 'action' => 'add'));
  1433. $expected = '/protected/images/add';
  1434. $this->assertEquals($expected, $result);
  1435. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => false));
  1436. $expected = '/images/add';
  1437. $this->assertEquals($expected, $result);
  1438. }
  1439. /**
  1440. * test that setting a prefix override the current one
  1441. *
  1442. * @return void
  1443. */
  1444. public function testPrefixOverride() {
  1445. Configure::write('Routing.prefixes', array('protected', 'admin'));
  1446. Router::reload();
  1447. Router::parse('/');
  1448. $request = new CakeRequest();
  1449. Router::setRequestInfo(
  1450. $request->addParams(array(
  1451. 'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'protected',
  1452. 'protected' => true, 'url' => array('url' => 'protected/images/index')
  1453. ))->addPaths(array(
  1454. 'base' => '',
  1455. 'here' => '/protected/images/index',
  1456. 'webroot' => '/',
  1457. ))
  1458. );
  1459. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'admin' => true));
  1460. $expected = '/admin/images/add';
  1461. $this->assertEquals($expected, $result);
  1462. $request = new CakeRequest();
  1463. Router::setRequestInfo(
  1464. $request->addParams(array(
  1465. 'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'admin',
  1466. 'admin' => true, 'url' => array('url' => 'admin/images/index')
  1467. ))->addPaths(array(
  1468. 'base' => '',
  1469. 'here' => '/admin/images/index',
  1470. 'webroot' => '/',
  1471. ))
  1472. );
  1473. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true));
  1474. $expected = '/protected/images/add';
  1475. $this->assertEquals($expected, $result);
  1476. }
  1477. /**
  1478. * Test that setting a prefix to false is ignored, as its generally user error.
  1479. *
  1480. * @return void
  1481. */
  1482. public function testPrefixFalseIgnored() {
  1483. Configure::write('Routing.prefixes', array('admin'));
  1484. Router::reload();
  1485. Router::connect('/cache_css/*', array('admin' => false, 'controller' => 'asset_compress', 'action' => 'get'));
  1486. $url = Router::url(array('controller' => 'asset_compress', 'action' => 'get', 'test'));
  1487. $expected = '/cache_css/test';
  1488. $this->assertEquals($expected, $url);
  1489. $url = Router::url(array('admin' => false, 'controller' => 'asset_compress', 'action' => 'get', 'test'));
  1490. $expected = '/cache_css/test';
  1491. $this->assertEquals($expected, $url);
  1492. $url = Router::url(array('admin' => true, 'controller' => 'asset_compress', 'action' => 'get', 'test'));
  1493. $this->assertEquals('/admin/asset_compress/get/test', $url);
  1494. }
  1495. /**
  1496. * testRemoveBase method
  1497. *
  1498. * @return void
  1499. */
  1500. public function testRemoveBase() {
  1501. $request = new CakeRequest();
  1502. Router::setRequestInfo(
  1503. $request->addParams(array(
  1504. 'plugin' => null, 'controller' => 'controller', 'action' => 'index',
  1505. 'bare' => 0, 'url' => array('url' => 'protected/images/index')
  1506. ))->addPaths(array(
  1507. 'base' => '/base',
  1508. 'here' => '/',
  1509. 'webroot' => '/base/',
  1510. ))
  1511. );
  1512. $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action'));
  1513. $expected = '/base/my_controller/my_action';
  1514. $this->assertEquals($expected, $result);
  1515. $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => false));
  1516. $expected = '/my_controller/my_action';
  1517. $this->assertEquals($expected, $result);
  1518. $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', 'base' => true));
  1519. $expected = '/base/my_controller/my_action/base:1';
  1520. $this->assertEquals($expected, $result);
  1521. }
  1522. /**
  1523. * testPagesUrlParsing method
  1524. *
  1525. * @return void
  1526. */
  1527. public function testPagesUrlParsing() {
  1528. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  1529. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  1530. $result = Router::parse('/');
  1531. $expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1532. $this->assertEquals($expected, $result);
  1533. $result = Router::parse('/pages/home/');
  1534. $expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1535. $this->assertEquals($expected, $result);
  1536. Router::reload();
  1537. require CAKE . 'Config' . DS . 'routes.php';
  1538. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  1539. $result = Router::parse('/');
  1540. $expected = array('pass' => array('home'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1541. $this->assertEquals($expected, $result);
  1542. $result = Router::parse('/pages/display/home/event:value');
  1543. $expected = array('pass' => array('home'), 'named' => array('event' => 'value'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1544. $this->assertEquals($expected, $result);
  1545. $result = Router::parse('/pages/display/home/event:Val_u2');
  1546. $expected = array('pass' => array('home'), 'named' => array('event' => 'Val_u2'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1547. $this->assertEquals($expected, $result);
  1548. $result = Router::parse('/pages/display/home/event:val-ue');
  1549. $expected = array('pass' => array('home'), 'named' => array('event' => 'val-ue'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1550. $this->assertEquals($expected, $result);
  1551. Router::reload();
  1552. Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
  1553. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  1554. $result = Router::parse('/pages/contact/');
  1555. $expected = array('pass' => array('contact'), 'named' => array(), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1556. $this->assertEquals($expected, $result);
  1557. }
  1558. /**
  1559. * test that requests with a trailing dot don't loose the do.
  1560. *
  1561. * @return void
  1562. */
  1563. public function testParsingWithTrailingPeriod() {
  1564. Router::reload();
  1565. Router::connect('/:controller/:action/*');
  1566. $result = Router::parse('/posts/view/something.');
  1567. $this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
  1568. $result = Router::parse('/posts/view/something. . .');
  1569. $this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s');
  1570. }
  1571. /**
  1572. * test that requests with a trailing dot don't loose the do.
  1573. *
  1574. * @return void
  1575. */
  1576. public function testParsingWithTrailingPeriodAndParseExtensions() {
  1577. Router::reload();
  1578. Router::connect('/:controller/:action/*');
  1579. Router::parseExtensions('json');
  1580. $result = Router::parse('/posts/view/something.');
  1581. $this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
  1582. $result = Router::parse('/posts/view/something. . .');
  1583. $this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s');
  1584. }
  1585. /**
  1586. * test that patterns work for :action
  1587. *
  1588. * @return void
  1589. */
  1590. public function testParsingWithPatternOnAction() {
  1591. Router::reload();
  1592. Router::connect(
  1593. '/blog/:action/*',
  1594. array('controller' => 'blog_posts'),
  1595. array('action' => 'other|actions')
  1596. );
  1597. $result = Router::parse('/blog/other');
  1598. $expected = array(
  1599. 'plugin' => null,
  1600. 'controller' => 'blog_posts',
  1601. 'action' => 'other',
  1602. 'pass' => array(),
  1603. 'named' => array()
  1604. );
  1605. $this->assertEquals($expected, $result);
  1606. $result = Router::parse('/blog/foobar');
  1607. $this->assertSame(array(), $result);
  1608. $result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo'));
  1609. $this->assertEquals('/blog_posts/foo', $result);
  1610. $result = Router::url(array('controller' => 'blog_posts', 'action' => 'actions'));
  1611. $this->assertEquals('/blog/actions', $result);
  1612. }
  1613. /**
  1614. * testParsingWithPrefixes method
  1615. *
  1616. * @return void
  1617. */
  1618. public function testParsingWithPrefixes() {
  1619. $adminParams = array('prefix' => 'admin', 'admin' => true);
  1620. Router::connect('/admin/:controller', $adminParams);
  1621. Router::connect('/admin/:controller/:action', $adminParams);
  1622. Router::connect('/admin/:controller/:action/*', $adminParams);
  1623. $request = new CakeRequest();
  1624. Router::setRequestInfo(
  1625. $request->addParams(array(
  1626. 'plugin' => null, 'controller' => 'controller', 'action' => 'index'
  1627. ))->addPaths(array(
  1628. 'base' => '/base',
  1629. 'here' => '/',
  1630. 'webroot' => '/base/',
  1631. ))
  1632. );
  1633. $result = Router::parse('/admin/posts/');
  1634. $expected = array('pass' => array(), 'named' => array(), 'prefix' => 'admin', 'plugin' => null, 'controller' => 'posts', 'action' => 'admin_index', 'admin' => true);
  1635. $this->assertEquals($expected, $result);
  1636. $result = Router::parse('/admin/posts');
  1637. $this->assertEquals($expected, $result);
  1638. $result = Router::url(array('admin' => true, 'controller' => 'posts'));
  1639. $expected = '/base/admin/posts';
  1640. $this->assertEquals($expected, $result);
  1641. $result = Router::prefixes();
  1642. $expected = array('admin');
  1643. $this->assertEquals($expected, $result);
  1644. Router::reload();
  1645. $prefixParams = array('prefix' => 'members', 'members' => true);
  1646. Router::connect('/members/:controller', $prefixParams);
  1647. Router::connect('/members/:controller/:action', $prefixParams);
  1648. Router::connect('/members/:controller/:action/*', $prefixParams);
  1649. $request = new CakeRequest();
  1650. Router::setRequestInfo(
  1651. $request->addParams(array(
  1652. 'plugin' => null, 'controller' => 'controller', 'action' => 'index',
  1653. 'bare' => 0
  1654. ))->addPaths(array(
  1655. 'base' => '/base',
  1656. 'here' => '/',
  1657. 'webroot' => '/',
  1658. ))
  1659. );
  1660. $result = Router::parse('/members/posts/index');
  1661. $expected = array('pass' => array(), 'named' => array(), 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'members_index', 'members' => true);
  1662. $this->assertEquals($expected, $result);
  1663. $result = Router::url(array('members' => true, 'controller' => 'posts', 'action' => 'index', 'page' => 2));
  1664. $expected = '/base/members/posts/index/page:2';
  1665. $this->assertEquals($expected, $result);
  1666. $result = Router::url(array('members' => true, 'controller' => 'users', 'action' => 'add'));
  1667. $expected = '/base/members/users/add';
  1668. $this->assertEquals($expected, $result);
  1669. }
  1670. /**
  1671. * Tests URL generation with flags and prefixes in and out of context
  1672. *
  1673. * @return void
  1674. */
  1675. public function testUrlWritingWithPrefixes() {
  1676. Router::connect('/company/:controller/:action/*', array('prefix' => 'company', 'company' => true));
  1677. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  1678. $result = Router::url(array('controller' => 'users', 'action' => 'login', 'company' => true));
  1679. $expected = '/company/users/login';
  1680. $this->assertEquals($expected, $result);
  1681. $result = Router::url(array('controller' => 'users', 'action' => 'company_login', 'company' => true));
  1682. $expected = '/company/users/login';
  1683. $this->assertEquals($expected, $result);
  1684. $request = new CakeRequest();
  1685. Router::setRequestInfo(
  1686. $request->addParams(array(
  1687. 'plugin' => null, 'controller' => 'users', 'action' => 'login',
  1688. 'company' => true
  1689. ))->addPaths(array(
  1690. 'base' => '/',
  1691. 'here' => '/',
  1692. 'webroot' => '/base/',
  1693. ))
  1694. );
  1695. $result = Router::url(array('controller' => 'users', 'action' => 'login', 'company' => false));
  1696. $expected = '/login';
  1697. $this->assertEquals($expected, $result);
  1698. }
  1699. /**
  1700. * test url generation with prefixes and custom routes
  1701. *
  1702. * @return void
  1703. */
  1704. public function testUrlWritingWithPrefixesAndCustomRoutes() {
  1705. Router::connect(
  1706. '/admin/login',
  1707. array('controller' => 'users', 'action' => 'login', 'prefix' => 'admin', 'admin' => true)
  1708. );
  1709. $request = new CakeRequest();
  1710. Router::setRequestInfo(
  1711. $request->addParams(array(
  1712. 'plugin' => null, 'controller' => 'posts', 'action' => 'index',
  1713. 'admin' => true, 'prefix' => 'admin'
  1714. ))->addPaths(array(
  1715. 'base' => '/',
  1716. 'here' => '/',
  1717. 'webroot' => '/',
  1718. ))
  1719. );
  1720. $result = Router::url(array('controller' => 'users', 'action' => 'login', 'admin' => true));
  1721. $this->assertEquals('/admin/login', $result);
  1722. $result = Router::url(array('controller' => 'users', 'action' => 'login'));
  1723. $this->assertEquals('/admin/login', $result);
  1724. $result = Router::url(array('controller' => 'users', 'action' => 'admin_login'));
  1725. $this->assertEquals('/admin/login', $result);
  1726. }
  1727. /**
  1728. * testPassedArgsOrder method
  1729. *
  1730. * @return void
  1731. */
  1732. public function testPassedArgsOrder() {
  1733. Router::connect('/test-passed/*', array('controller' => 'pages', 'action' => 'display', 'home'));
  1734. Router::connect('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
  1735. Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 1));
  1736. Router::parse('/');
  1737. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 1, 'whatever'));
  1738. $expected = '/test/whatever';
  1739. $this->assertEquals($expected, $result);
  1740. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 2, 'whatever'));
  1741. $expected = '/test2/whatever';
  1742. $this->assertEquals($expected, $result);
  1743. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 'home', 'whatever'));
  1744. $expected = '/test-passed/whatever';
  1745. $this->assertEquals($expected, $result);
  1746. Configure::write('Routing.prefixes', array('admin'));
  1747. Router::reload();
  1748. $request = new CakeRequest();
  1749. Router::setRequestInfo(
  1750. $request->addParams(array(
  1751. 'plugin' => null, 'controller' => 'images', 'action' => 'index',
  1752. 'url' => array('url' => 'protected/images/index')
  1753. ))->addPaths(array(
  1754. 'base' => '',
  1755. 'here' => '/protected/images/index',
  1756. 'webroot' => '/',
  1757. ))
  1758. );
  1759. Router::connect('/protected/:controller/:action/*', array(
  1760. 'controller' => 'users',
  1761. 'action' => 'index',
  1762. 'prefix' => 'protected'
  1763. ));
  1764. Router::parse('/');
  1765. $result = Router::url(array('controller' => 'images', 'action' => 'add'));
  1766. $expected = '/protected/images/add';
  1767. $this->assertEquals($expected, $result);
  1768. $result = Router::prefixes();
  1769. $expected = array('admin', 'protected');
  1770. $this->assertEquals($expected, $result);
  1771. }
  1772. /**
  1773. * testRegexRouteMatching method
  1774. *
  1775. * @return void
  1776. */
  1777. public function testRegexRouteMatching() {
  1778. Router::connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng'));
  1779. $result = Router::parse('/eng/test/test_action');
  1780. $expected = array('pass' => array(), 'named' => array(), 'locale' => 'eng', 'controller' => 'test', 'action' => 'test_action', 'plugin' => null);
  1781. $this->assertEquals($expected, $result);
  1782. $result = Router::parse('/badness/test/test_action');
  1783. $this->assertSame(array(), $result);
  1784. Router::reload();
  1785. Router::connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng'));
  1786. $request = new CakeRequest();
  1787. Router::setRequestInfo(
  1788. $request->addParams(array(
  1789. 'plugin' => null, 'controller' => 'test', 'action' => 'index',
  1790. 'url' => array('url' => 'test/test_action')
  1791. ))->addPaths(array(
  1792. 'base' => '',
  1793. 'here' => '/test/test_action',
  1794. 'webroot' => '/',
  1795. ))
  1796. );
  1797. $result = Router::url(array('action' => 'test_another_action'));
  1798. $expected = '/test/test_another_action';
  1799. $this->assertEquals($expected, $result);
  1800. $result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng'));
  1801. $expected = '/eng/test/test_another_action';
  1802. $this->assertEquals($expected, $result);
  1803. $result = Router::url(array('action' => 'test_another_action', 'locale' => 'badness'));
  1804. $expected = '/test/test_another_action/locale:badness';
  1805. $this->assertEquals($expected, $result);
  1806. }
  1807. /**
  1808. * testStripPlugin
  1809. *
  1810. * @return void
  1811. */
  1812. public function testStripPlugin() {
  1813. $pluginName = 'forums';
  1814. $url = 'example.com/' . $pluginName . '/';
  1815. $expected = 'example.com';
  1816. $this->assertEquals($expected, Router::stripPlugin($url, $pluginName));
  1817. $this->assertEquals(Router::stripPlugin($url), $url);
  1818. $this->assertEquals(Router::stripPlugin($url, null), $url);
  1819. }
  1820. /**
  1821. * testCurrentRoute
  1822. *
  1823. * This test needs some improvement and actual requestAction() usage
  1824. *
  1825. * @return void
  1826. */
  1827. public function testCurrentRoute() {
  1828. $url = array('controller' => 'pages', 'action' => 'display', 'government');
  1829. Router::connect('/government', $url);
  1830. Router::parse('/government');
  1831. $route = Router::currentRoute();
  1832. $this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
  1833. }
  1834. /**
  1835. * testRequestRoute
  1836. *
  1837. * @return void
  1838. */
  1839. public function testRequestRoute() {
  1840. $url = array('controller' => 'products', 'action' => 'display', 5);
  1841. Router::connect('/government', $url);
  1842. Router::parse('/government');
  1843. $route = Router::requestRoute();
  1844. $this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
  1845. // test that the first route is matched
  1846. $newUrl = array('controller' => 'products', 'action' => 'display', 6);
  1847. Router::connect('/government', $url);
  1848. Router::parse('/government');
  1849. $route = Router::requestRoute();
  1850. $this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
  1851. // test that an unmatched route does not change the current route
  1852. $newUrl = array('controller' => 'products', 'action' => 'display', 6);
  1853. Router::connect('/actor', $url);
  1854. Router::parse('/government');
  1855. $route = Router::requestRoute();
  1856. $this->assertEquals(array_merge($url, array('plugin' => null)), $route->defaults);
  1857. }
  1858. /**
  1859. * testGetParams
  1860. *
  1861. * @return void
  1862. */
  1863. public function testGetParams() {
  1864. $paths = array('base' => '/', 'here' => '/products/display/5', 'webroot' => '/webroot');
  1865. $params = array('param1' => '1', 'param2' => '2');
  1866. Router::setRequestInfo(array($params, $paths));
  1867. $expected = array(
  1868. 'plugin' => null, 'controller' => false, 'action' => false,
  1869. 'named' => array(), 'pass' => array(),
  1870. 'param1' => '1', 'param2' => '2',
  1871. );
  1872. $this->assertEquals(Router::getParams(), $expected);
  1873. $this->assertEquals(Router::getParam('controller'), false);
  1874. $this->assertEquals(Router::getParam('param1'), '1');
  1875. $this->assertEquals(Router::getParam('param2'), '2');
  1876. Router::reload();
  1877. $params = array('controller' => 'pages', 'action' => 'display');
  1878. Router::setRequestInfo(array($params, $paths));
  1879. $expected = array(
  1880. 'plugin' => null, 'controller' => 'pages', 'action' => 'display',
  1881. 'named' => array(), 'pass' => array(),
  1882. );
  1883. $this->assertEquals(Router::getParams(), $expected);
  1884. $this->assertEquals(Router::getParams(true), $expected);
  1885. }
  1886. /**
  1887. * test that connectDefaults() can disable default route connection
  1888. *
  1889. * @return void
  1890. */
  1891. public function testDefaultsMethod() {
  1892. Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 2));
  1893. $result = Router::parse('/posts/edit/5');
  1894. $this->assertFalse(isset($result['controller']));
  1895. $this->assertFalse(isset($result['action']));
  1896. }
  1897. /**
  1898. * test that the required default routes are connected.
  1899. *
  1900. * @return void
  1901. */
  1902. public function testConnectDefaultRoutes() {
  1903. App::build(array(
  1904. 'plugins' => array(
  1905. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  1906. )
  1907. ), App::RESET);
  1908. CakePlugin::load(array('TestPlugin', 'PluginJs'));
  1909. Router::reload();
  1910. require CAKE . 'Config' . DS . 'routes.php';
  1911. $result = Router::url(array('plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index'));
  1912. $this->assertEquals('/plugin_js/js_file', $result);
  1913. $result = Router::parse('/plugin_js/js_file');
  1914. $expected = array(
  1915. 'plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index',
  1916. 'named' => array(), 'pass' => array()
  1917. );
  1918. $this->assertEquals($expected, $result);
  1919. $result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index'));
  1920. $this->assertEquals('/test_plugin', $result);
  1921. $result = Router::parse('/test_plugin');
  1922. $expected = array(
  1923. 'plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index',
  1924. 'named' => array(), 'pass' => array()
  1925. );
  1926. $this->assertEquals($expected, $result, 'Plugin shortcut route broken. %s');
  1927. }
  1928. /**
  1929. * test using a custom route class for route connection
  1930. *
  1931. * @return void
  1932. */
  1933. public function testUsingCustomRouteClass() {
  1934. $this->getMock('CakeRoute', array(), array(), 'MockConnectedRoute', false);
  1935. $routes = Router::connect(
  1936. '/:slug',
  1937. array('controller' => 'posts', 'action' => 'view'),
  1938. array('routeClass' => 'MockConnectedRoute', 'slug' => '[a-z_-]+')
  1939. );
  1940. $this->assertTrue(is_a($routes[0], 'MockConnectedRoute'), 'Incorrect class used. %s');
  1941. $expected = array('controller' => 'posts', 'action' => 'view', 'slug' => 'test');
  1942. $routes[0]->expects($this->any())
  1943. ->method('parse')
  1944. ->will($this->returnValue($expected));
  1945. $result = Router::parse('/test');
  1946. $this->assertEquals($expected, $result);
  1947. }
  1948. /**
  1949. * test using custom route class in PluginDot notation
  1950. */
  1951. public function testUsingCustomRouteClassPluginDotSyntax() {
  1952. App::build(array(
  1953. 'Plugin' => array(
  1954. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  1955. )
  1956. ));
  1957. CakePlugin::load('TestPlugin');
  1958. App::uses('TestRoute', 'TestPlugin.Routing/Route');
  1959. $routes = Router::connect(
  1960. '/:slug',
  1961. array('controller' => 'posts', 'action' => 'view'),
  1962. array('routeClass' => 'TestPlugin.TestRoute', 'slug' => '[a-z_-]+')
  1963. );
  1964. $this->assertInstanceOf('TestRoute', $routes[0]);
  1965. CakePlugin::unload('TestPlugin');
  1966. }
  1967. /**
  1968. * test that route classes must extend CakeRoute
  1969. *
  1970. * @expectedException RouterException
  1971. * @return void
  1972. */
  1973. public function testCustomRouteException() {
  1974. Router::connect('/:controller', array(), array('routeClass' => 'Object'));
  1975. }
  1976. /**
  1977. * test reversing parameter arrays back into strings.
  1978. *
  1979. * Mark the router as initialized so it doesn't auto-load routes
  1980. *
  1981. * @return void
  1982. */
  1983. public function testRouterReverse() {
  1984. Router::$initialized = true;
  1985. $params = array(
  1986. 'controller' => 'posts',
  1987. 'action' => 'view',
  1988. 'pass' => array(1),
  1989. 'named' => array(),
  1990. 'url' => array(),
  1991. 'autoRender' => 1,
  1992. 'bare' => 1,
  1993. 'return' => 1,
  1994. 'requested' => 1,
  1995. '_Token' => array('key' => 'sekret')
  1996. );
  1997. $result = Router::reverse($params);
  1998. $this->assertEquals('/posts/view/1', $result);
  1999. $params = array(
  2000. 'controller' => 'posts',
  2001. 'action' => 'index',
  2002. 'pass' => array(1),
  2003. 'named' => array('page' => 1, 'sort' => 'Article.title', 'direction' => 'desc'),
  2004. 'url' => array()
  2005. );
  2006. $result = Router::reverse($params);
  2007. $this->assertEquals('/posts/index/1/page:1/sort:Article.title/direction:desc', $result);
  2008. Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
  2009. $params = array(
  2010. 'lang' => 'eng',
  2011. 'controller' => 'posts',
  2012. 'action' => 'view',
  2013. 'pass' => array(1),
  2014. 'named' => array(),
  2015. 'url' => array('url' => 'eng/posts/view/1')
  2016. );
  2017. $result = Router::reverse($params);
  2018. $this->assertEquals('/eng/posts/view/1', $result);
  2019. $params = array(
  2020. 'lang' => 'eng',
  2021. 'controller' => 'posts',
  2022. 'action' => 'view',
  2023. 'pass' => array(1),
  2024. 'named' => array(),
  2025. 'url' => array('url' => 'eng/posts/view/1', 'foo' => 'bar', 'baz' => 'quu'),
  2026. 'paging' => array(),
  2027. 'models' => array()
  2028. );
  2029. $result = Router::reverse($params);
  2030. $this->assertEquals('/eng/posts/view/1?foo=bar&baz=quu', $result);
  2031. $request = new CakeRequest('/eng/posts/view/1');
  2032. $request->addParams(array(
  2033. 'lang' => 'eng',
  2034. 'controller' => 'posts',
  2035. 'action' => 'view',
  2036. 'pass' => array(1),
  2037. 'named' => array(),
  2038. ));
  2039. $request->query = array('url' => 'eng/posts/view/1', 'test' => 'value');
  2040. $result = Router::reverse($request);
  2041. $expected = '/eng/posts/view/1?test=value';
  2042. $this->assertEquals($expected, $result);
  2043. $params = array(
  2044. 'lang' => 'eng',
  2045. 'controller' => 'posts',
  2046. 'action' => 'view',
  2047. 'pass' => array(1),
  2048. 'named' => array(),
  2049. 'url' => array('url' => 'eng/posts/view/1')
  2050. );
  2051. $result = Router::reverse($params, true);
  2052. $this->assertRegExp('/^http(s)?:\/\//', $result);
  2053. }
  2054. /**
  2055. * Test that extensions work with Router::reverse()
  2056. *
  2057. * @return void
  2058. */
  2059. public function testReverseWithExtension() {
  2060. Router::parseExtensions('json');
  2061. $request = new CakeRequest('/posts/view/1.json');
  2062. $request->addParams(array(
  2063. 'controller' => 'posts',
  2064. 'action' => 'view',
  2065. 'pass' => array(1),
  2066. 'named' => array(),
  2067. 'ext' => 'json',
  2068. ));
  2069. $request->query = array();
  2070. $result = Router::reverse($request);
  2071. $expected = '/posts/view/1.json';
  2072. $this->assertEquals($expected, $result);
  2073. }
  2074. /**
  2075. * test that setRequestInfo can accept arrays and turn that into a CakeRequest object.
  2076. *
  2077. * @return void
  2078. */
  2079. public function testSetRequestInfoLegacy() {
  2080. Router::setRequestInfo(array(
  2081. array(
  2082. 'plugin' => null, 'controller' => 'images', 'action' => 'index',
  2083. 'url' => array('url' => 'protected/images/index')
  2084. ),
  2085. array(
  2086. 'base' => '',
  2087. 'here' => '/protected/images/index',
  2088. 'webroot' => '/',
  2089. )
  2090. ));
  2091. $result = Router::getRequest();
  2092. $this->assertEquals('images', $result->controller);
  2093. $this->assertEquals('index', $result->action);
  2094. $this->assertEquals('', $result->base);
  2095. $this->assertEquals('/protected/images/index', $result->here);
  2096. $this->assertEquals('/', $result->webroot);
  2097. }
  2098. /**
  2099. * Test that Router::url() uses the first request
  2100. */
  2101. public function testUrlWithRequestAction() {
  2102. $firstRequest = new CakeRequest('/posts/index');
  2103. $firstRequest->addParams(array(
  2104. 'plugin' => null,
  2105. 'controller' => 'posts',
  2106. 'action' => 'index'
  2107. ))->addPaths(array('base' => ''));
  2108. $secondRequest = new CakeRequest('/posts/index');
  2109. $secondRequest->addParams(array(
  2110. 'requested' => 1,
  2111. 'plugin' => null,
  2112. 'controller' => 'comments',
  2113. 'action' => 'listing'
  2114. ))->addPaths(array('base' => ''));
  2115. Router::setRequestInfo($firstRequest);
  2116. Router::setRequestInfo($secondRequest);
  2117. $result = Router::url(array('base' => false));
  2118. $this->assertEquals('/comments/listing', $result, 'with second requests, the last should win.');
  2119. Router::popRequest();
  2120. $result = Router::url(array('base' => false));
  2121. $this->assertEquals('/posts', $result, 'with second requests, the last should win.');
  2122. }
  2123. /**
  2124. * test that a route object returning a full url is not modified.
  2125. *
  2126. * @return void
  2127. */
  2128. public function testUrlFullUrlReturnFromRoute() {
  2129. $url = 'http://example.com/posts/view/1';
  2130. $this->getMock('CakeRoute', array(), array('/'), 'MockReturnRoute');
  2131. $routes = Router::connect('/:controller/:action', array(), array('routeClass' => 'MockReturnRoute'));
  2132. $routes[0]->expects($this->any())->method('match')
  2133. ->will($this->returnValue($url));
  2134. $result = Router::url(array('controller' => 'posts', 'action' => 'view', 1));
  2135. $this->assertEquals($url, $result);
  2136. }
  2137. /**
  2138. * test protocol in url
  2139. *
  2140. * @return void
  2141. */
  2142. public function testUrlProtocol() {
  2143. $url = 'http://example.com';
  2144. $this->assertEquals($url, Router::url($url));
  2145. $url = 'ed2k://example.com';
  2146. $this->assertEquals($url, Router::url($url));
  2147. $url = 'svn+ssh://example.com';
  2148. $this->assertEquals($url, Router::url($url));
  2149. $url = '://example.com';
  2150. $this->assertEquals($url, Router::url($url));
  2151. $url = 'javascript:void(0)';
  2152. $this->assertEquals($url, Router::url($url));
  2153. $url = 'tel:012345-678';
  2154. $this->assertEquals($url, Router::url($url));
  2155. $url = 'sms:012345-678';
  2156. $this->assertEquals($url, Router::url($url));
  2157. $url = '#here';
  2158. $this->assertEquals($url, Router::url($url));
  2159. $url = 'posts/index#here';
  2160. $expected = FULL_BASE_URL . '/posts/index#here';
  2161. $this->assertEquals($expected, Router::url($url, true));
  2162. }
  2163. /**
  2164. * Testing that patterns on the :action param work properly.
  2165. *
  2166. * @return void
  2167. */
  2168. public function testPatternOnAction() {
  2169. $route = new CakeRoute(
  2170. '/blog/:action/*',
  2171. array('controller' => 'blog_posts'),
  2172. array('action' => 'other|actions')
  2173. );
  2174. $result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
  2175. $this->assertFalse($result);
  2176. $result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
  2177. $this->assertEquals('/blog/actions/', $result);
  2178. $result = $route->parse('/blog/other');
  2179. $expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => array(), 'named' => array());
  2180. $this->assertEquals($expected, $result);
  2181. $result = $route->parse('/blog/foobar');
  2182. $this->assertFalse($result);
  2183. }
  2184. /**
  2185. * Tests resourceMap as getter and setter.
  2186. *
  2187. * @return void
  2188. */
  2189. public function testResourceMap() {
  2190. $default = Router::resourceMap();
  2191. $expected = array(
  2192. array('action' => 'index', 'method' => 'GET', 'id' => false),
  2193. array('action' => 'view', 'method' => 'GET', 'id' => true),
  2194. array('action' => 'add', 'method' => 'POST', 'id' => false),
  2195. array('action' => 'edit', 'method' => 'PUT', 'id' => true),
  2196. array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
  2197. array('action' => 'edit', 'method' => 'POST', 'id' => true)
  2198. );
  2199. $this->assertEquals($default, $expected);
  2200. $custom = array(
  2201. array('action' => 'index', 'method' => 'GET', 'id' => false),
  2202. array('action' => 'view', 'method' => 'GET', 'id' => true),
  2203. array('action' => 'add', 'method' => 'POST', 'id' => false),
  2204. array('action' => 'edit', 'method' => 'PUT', 'id' => true),
  2205. array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
  2206. array('action' => 'update', 'method' => 'POST', 'id' => true)
  2207. );
  2208. Router::resourceMap($custom);
  2209. $this->assertEquals(Router::resourceMap(), $custom);
  2210. Router::resourceMap($default);
  2211. }
  2212. /**
  2213. * test setting redirect routes
  2214. *
  2215. * @return void
  2216. */
  2217. public function testRouteRedirection() {
  2218. Router::redirect('/blog', array('controller' => 'posts'), array('status' => 302));
  2219. $this->assertEquals(1, count(Router::$routes));
  2220. Router::$routes[0]->response = $this->getMock('CakeResponse', array('_sendHeader'));
  2221. Router::$routes[0]->stop = false;
  2222. $this->assertEquals(302, Router::$routes[0]->options['status']);
  2223. Router::parse('/blog');
  2224. $header = Router::$routes[0]->response->header();
  2225. $this->assertEquals(Router::url('/posts', true), $header['Location']);
  2226. $this->assertEquals(302, Router::$routes[0]->response->statusCode());
  2227. Router::$routes[0]->response = $this->getMock('CakeResponse', array('_sendHeader'));
  2228. Router::parse('/not-a-match');
  2229. $this->assertEquals(array(), Router::$routes[0]->response->header());
  2230. }
  2231. /**
  2232. * Test setting the default route class
  2233. *
  2234. * @return void
  2235. */
  2236. public function testDefaultRouteClass() {
  2237. $this->getMock('CakeRoute', array(), array('/test'), 'TestDefaultRouteClass');
  2238. Router::defaultRouteClass('TestDefaultRouteClass');
  2239. $result = Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  2240. $this->assertInstanceOf('TestDefaultRouteClass', $result[0]);
  2241. }
  2242. /**
  2243. * Test getting the default route class
  2244. *
  2245. * @return void
  2246. */
  2247. public function testDefaultRouteClassGetter() {
  2248. $routeClass = 'TestDefaultRouteClass';
  2249. Router::defaultRouteClass($routeClass);
  2250. $this->assertEquals($routeClass, Router::defaultRouteClass());
  2251. $this->assertEquals($routeClass, Router::defaultRouteClass(null));
  2252. }
  2253. /**
  2254. * Test that route classes must extend CakeRoute
  2255. *
  2256. * @expectedException RouterException
  2257. * @return void
  2258. */
  2259. public function testDefaultRouteException() {
  2260. Router::defaultRouteClass('');
  2261. Router::connect('/:controller', array());
  2262. }
  2263. /**
  2264. * Test that route classes must extend CakeRoute
  2265. *
  2266. * @expectedException RouterException
  2267. * @return void
  2268. */
  2269. public function testSettingInvalidDefaultRouteException() {
  2270. Router::defaultRouteClass('Object');
  2271. }
  2272. /**
  2273. * Test that class must exist
  2274. *
  2275. * @expectedException RouterException
  2276. * @return void
  2277. */
  2278. public function testSettingNonExistentDefaultRouteException() {
  2279. Router::defaultRouteClass('NonExistentClass');
  2280. }
  2281. /**
  2282. * Tests generating well-formed querystrings
  2283. *
  2284. * @return void
  2285. */
  2286. public function testQueryString() {
  2287. $result = Router::queryString(array('var' => 'foo bar'));
  2288. $expected = '?var=foo+bar';
  2289. $this->assertEquals($expected, $result);
  2290. $result = Router::queryString(false, array('some' => 'param', 'foo' => 'bar'));
  2291. $expected = '?some=param&foo=bar';
  2292. $this->assertEquals($expected, $result);
  2293. $existing = array('apple' => 'red', 'pear' => 'green');
  2294. $result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar'));
  2295. $expected = '?apple=red&pear=green&some=param&foo=bar';
  2296. $this->assertEquals($expected, $result);
  2297. $existing = 'apple=red&pear=green';
  2298. $result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar'));
  2299. $expected = '?apple=red&pear=green&some=param&foo=bar';
  2300. $this->assertEquals($expected, $result);
  2301. $existing = '?apple=red&pear=green';
  2302. $result = Router::queryString($existing, array('some' => 'param', 'foo' => 'bar'));
  2303. $expected = '?apple=red&pear=green&some=param&foo=bar';
  2304. $this->assertEquals($expected, $result);
  2305. $result = Router::queryString('apple=red&pear=green');
  2306. $expected = '?apple=red&pear=green';
  2307. $this->assertEquals($expected, $result);
  2308. $result = Router::queryString('foo=bar', array('php' => 'nut', 'jose' => 'zap'), true);
  2309. $expected = '?foo=bar&amp;php=nut&amp;jose=zap';
  2310. $this->assertEquals($expected, $result);
  2311. $result = Router::queryString('foo=bar&amp;', array('php' => 'nut', 'jose' => 'zap'), true);
  2312. $expected = '?foo=bar&amp;php=nut&amp;jose=zap';
  2313. $this->assertEquals($expected, $result);
  2314. $result = Router::queryString('foo=bar&', array('php' => 'nut', 'jose' => 'zap'));
  2315. $expected = '?foo=bar&php=nut&jose=zap';
  2316. $this->assertEquals($expected, $result);
  2317. }
  2318. }