RouterTest.php 77 KB

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