RouterTest.php 74 KB

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