RouterTest.php 79 KB

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