RouterTest.php 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  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. Configure::write('Routing.prefixes', array('admin'));
  698. Router::reload();
  699. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  700. Router::connect('/reset/*', array('admin' => true, 'controller' => 'users', 'action' => 'reset'));
  701. Router::connect('/tests', array('controller' => 'tests', 'action' => 'index'));
  702. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  703. Router::extensions('rss', false);
  704. $request = new Request();
  705. $request->addParams(array(
  706. 'controller' => 'registrations',
  707. 'action' => 'admin_index',
  708. 'plugin' => null,
  709. 'prefix' => 'admin',
  710. '_ext' => 'html'
  711. ));
  712. $request->base = '';
  713. $request->here = '/admin/registrations/index';
  714. $request->webroot = '/';
  715. Router::setRequestInfo($request);
  716. $result = Router::url([]);
  717. $expected = '/admin/registrations/index';
  718. $this->assertEquals($expected, $result);
  719. Router::reload();
  720. Router::connect('/admin/subscriptions/:action/*', array('controller' => 'subscribe', 'prefix' => 'admin'));
  721. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  722. $request = new Request();
  723. $request->addParams(array(
  724. 'action' => 'index',
  725. 'plugin' => null,
  726. 'controller' => 'subscribe',
  727. 'prefix' => 'admin',
  728. ));
  729. $request->base = '/magazine';
  730. $request->here = '/magazine/admin/subscriptions/edit/1';
  731. $request->webroot = '/magazine/';
  732. Router::setRequestInfo($request);
  733. $result = Router::url(array('action' => 'edit', 1));
  734. $expected = '/magazine/admin/subscriptions/edit/1';
  735. $this->assertEquals($expected, $result);
  736. $result = Router::url(array('prefix' => 'admin', 'controller' => 'users', 'action' => 'login'));
  737. $expected = '/magazine/admin/users/login';
  738. $this->assertEquals($expected, $result);
  739. Router::reload();
  740. $request = new Request();
  741. $request->addParams(array(
  742. 'prefix' => 'admin',
  743. 'action' => 'index',
  744. 'plugin' => null,
  745. 'controller' => 'users',
  746. ));
  747. $request->base = '/';
  748. $request->here = '/';
  749. $request->webroot = '/';
  750. Router::setRequestInfo($request);
  751. Router::connect('/page/*', array('controller' => 'pages', 'action' => 'view', 'prefix' => 'admin'));
  752. $result = Router::url(array('prefix' => 'admin', 'controller' => 'pages', 'action' => 'view', 'my-page'));
  753. $expected = '/page/my-page';
  754. $this->assertEquals($expected, $result);
  755. Router::reload();
  756. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  757. $request = new Request();
  758. $request->addParams(array(
  759. 'plugin' => null,
  760. 'controller' => 'pages',
  761. 'action' => 'add',
  762. 'prefix' => 'admin'
  763. ));
  764. $request->base = '';
  765. $request->here = '/admin/pages/add';
  766. $request->webroot = '/';
  767. Router::setRequestInfo($request);
  768. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
  769. $expected = '/admin/pages/add';
  770. $this->assertEquals($expected, $result);
  771. Router::reload();
  772. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  773. $request = new Request();
  774. $request->addParams(array(
  775. 'plugin' => null,
  776. 'controller' => 'pages',
  777. 'action' => 'add',
  778. 'prefix' => 'admin'
  779. ));
  780. $request->base = '';
  781. $request->here = '/admin/pages/add';
  782. $request->webroot = '/';
  783. Router::setRequestInfo($request);
  784. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
  785. $expected = '/admin/pages/add';
  786. $this->assertEquals($expected, $result);
  787. Router::reload();
  788. Router::connect('/admin/:controller/:action/:id', array('prefix' => 'admin'), array('id' => '[0-9]+'));
  789. $request = new Request();
  790. Router::setRequestInfo(
  791. $request->addParams(array(
  792. 'plugin' => null,
  793. 'controller' => 'pages',
  794. 'action' => 'edit',
  795. 'pass' => array('284'),
  796. 'prefix' => 'admin'
  797. ))->addPaths(array(
  798. 'base' => '',
  799. 'here' => '/admin/pages/edit/284',
  800. 'webroot' => '/'
  801. ))
  802. );
  803. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 'id' => '284'));
  804. $expected = '/admin/pages/edit/284';
  805. $this->assertEquals($expected, $result);
  806. Router::reload();
  807. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  808. $request = new Request();
  809. Router::setRequestInfo(
  810. $request->addParams(array(
  811. 'plugin' => null, 'controller' => 'pages', 'action' => 'add', 'prefix' => 'admin',
  812. ))->addPaths(array(
  813. 'base' => '', 'here' => '/admin/pages/add', 'webroot' => '/'
  814. ))
  815. );
  816. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'add', 'id' => false));
  817. $expected = '/admin/pages/add';
  818. $this->assertEquals($expected, $result);
  819. Router::reload();
  820. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  821. $request = new Request();
  822. Router::setRequestInfo(
  823. $request->addParams(array(
  824. 'plugin' => null, 'controller' => 'pages', 'action' => 'edit', 'prefix' => 'admin',
  825. 'pass' => array('284')
  826. ))->addPaths(array(
  827. 'base' => '', 'here' => '/admin/pages/edit/284', 'webroot' => '/'
  828. ))
  829. );
  830. $result = Router::url(array('plugin' => null, 'controller' => 'pages', 'action' => 'edit', 284));
  831. $expected = '/admin/pages/edit/284';
  832. $this->assertEquals($expected, $result);
  833. Router::reload();
  834. Router::connect('/admin/posts/*', array('controller' => 'posts', 'action' => 'index', 'prefix' => 'admin'));
  835. Router::setRequestInfo(
  836. $request->addParams(array(
  837. 'plugin' => null, 'controller' => 'posts', 'action' => 'index', 'prefix' => 'admin',
  838. 'pass' => array('284')
  839. ))->addPaths(array(
  840. 'base' => '', 'here' => '/admin/posts', 'webroot' => '/'
  841. ))
  842. );
  843. $result = Router::url(array('all'));
  844. $expected = '/admin/posts/all';
  845. $this->assertEquals($expected, $result);
  846. }
  847. /**
  848. * Test URL generation inside a prefixed plugin.
  849. *
  850. * @return void
  851. */
  852. public function testUrlGenerationPrefixedPlugin() {
  853. Router::prefix('admin', function ($routes) {
  854. $routes->plugin('MyPlugin', function ($routes) {
  855. $routes->fallbacks();
  856. });
  857. });
  858. $result = Router::url(['prefix' => 'admin', 'plugin' => 'MyPlugin', 'controller' => 'Forms', 'action' => 'edit', 2]);
  859. $expected = '/admin/my_plugin/forms/edit/2';
  860. $this->assertEquals($expected, $result);
  861. }
  862. /**
  863. * testUrlGenerationWithExtensions method
  864. *
  865. * @return void
  866. */
  867. public function testUrlGenerationWithExtensions() {
  868. Router::connect('/:controller', array('action' => 'index'));
  869. Router::connect('/:controller/:action');
  870. $result = Router::url(array(
  871. 'plugin' => null,
  872. 'controller' => 'articles',
  873. 'action' => 'add',
  874. 'id' => null,
  875. '_ext' => 'json'
  876. ));
  877. $expected = '/articles/add.json';
  878. $this->assertEquals($expected, $result);
  879. $result = Router::url(array(
  880. 'plugin' => null,
  881. 'controller' => 'articles',
  882. 'action' => 'add',
  883. '_ext' => 'json'
  884. ));
  885. $expected = '/articles/add.json';
  886. $this->assertEquals($expected, $result);
  887. $result = Router::url(array(
  888. 'plugin' => null,
  889. 'controller' => 'articles',
  890. 'action' => 'index',
  891. 'id' => null,
  892. '_ext' => 'json'
  893. ));
  894. $expected = '/articles.json';
  895. $this->assertEquals($expected, $result);
  896. }
  897. /**
  898. * test url() when the current request has an extension.
  899. *
  900. * @return void
  901. */
  902. public function testUrlGenerationWithExtensionInCurrentRequest() {
  903. Router::extensions('rss');
  904. Router::scope('/', function ($r) {
  905. $r->fallbacks();
  906. });
  907. $request = new Request();
  908. $request->addParams(['controller' => 'Tasks', 'action' => 'index', '_ext' => 'rss']);
  909. Router::pushRequest($request);
  910. $result = Router::url(array(
  911. 'controller' => 'Tasks',
  912. 'action' => 'view',
  913. 1
  914. ));
  915. $expected = '/tasks/view/1';
  916. $this->assertEquals($expected, $result);
  917. $result = Router::url(array(
  918. 'controller' => 'Tasks',
  919. 'action' => 'view',
  920. 1,
  921. '_ext' => 'json'
  922. ));
  923. $expected = '/tasks/view/1.json';
  924. $this->assertEquals($expected, $result);
  925. }
  926. /**
  927. * Test url generation with named routes.
  928. */
  929. public function testUrlGenerationNamedRoute() {
  930. Router::connect(
  931. '/users',
  932. array('controller' => 'users', 'action' => 'index'),
  933. array('_name' => 'users-index')
  934. );
  935. Router::connect(
  936. '/users/:name',
  937. array('controller' => 'users', 'action' => 'view'),
  938. array('_name' => 'test')
  939. );
  940. Router::connect(
  941. '/view/*',
  942. ['action' => 'view'],
  943. ['_name' => 'Articles::view']
  944. );
  945. $url = Router::url(['_name' => 'test', 'name' => 'mark']);
  946. $this->assertEquals('/users/mark', $url);
  947. $url = Router::url([
  948. '_name' => 'test', 'name' => 'mark',
  949. 'page' => 1, 'sort' => 'title', 'dir' => 'desc'
  950. ]);
  951. $this->assertEquals('/users/mark?page=1&sort=title&dir=desc', $url);
  952. $url = Router::url(['_name' => 'Articles::view']);
  953. $this->assertEquals('/view/', $url);
  954. $url = Router::url(['_name' => 'Articles::view', '1']);
  955. $this->assertEquals('/view/1', $url);
  956. $url = Router::url(['_name' => 'Articles::view', '_full' => true, '1']);
  957. $this->assertEquals('http://localhost/view/1', $url);
  958. $url = Router::url(['_name' => 'Articles::view', '1', '#' => 'frag']);
  959. $this->assertEquals('/view/1#frag', $url);
  960. }
  961. /**
  962. * Test that using invalid names causes exceptions.
  963. *
  964. * @expectedException \Cake\Routing\Exception\MissingRouteException
  965. * @return void
  966. */
  967. public function testNamedRouteException() {
  968. Router::connect(
  969. '/users/:name',
  970. array('controller' => 'users', 'action' => 'view'),
  971. array('_name' => 'test')
  972. );
  973. $url = Router::url(['_name' => 'junk', 'name' => 'mark']);
  974. }
  975. /**
  976. * Test that url filters are applied to url params.
  977. *
  978. * @return void
  979. */
  980. public function testUrlGenerationWithUrlFilter() {
  981. Router::connect('/:lang/:controller/:action/*');
  982. $request = new Request();
  983. $request->addParams(array(
  984. 'lang' => 'en',
  985. 'controller' => 'posts',
  986. 'action' => 'index'
  987. ))->addPaths(array(
  988. 'base' => '',
  989. 'here' => '/'
  990. ));
  991. Router::pushRequest($request);
  992. $calledCount = 0;
  993. Router::addUrlFilter(function ($url, $request) use (&$calledCount) {
  994. $calledCount++;
  995. $url['lang'] = $request->lang;
  996. return $url;
  997. });
  998. Router::addUrlFilter(function ($url, $request) use (&$calledCount) {
  999. $calledCount++;
  1000. $url[] = '1234';
  1001. return $url;
  1002. });
  1003. $result = Router::url(array('controller' => 'tasks', 'action' => 'edit'));
  1004. $this->assertEquals('/en/tasks/edit/1234', $result);
  1005. $this->assertEquals(2, $calledCount);
  1006. }
  1007. /**
  1008. * Test that plain strings urls work
  1009. *
  1010. * @return void
  1011. */
  1012. public function testUrlGenerationPlainString() {
  1013. $mailto = 'mailto:mark@example.com';
  1014. $result = Router::url($mailto);
  1015. $this->assertEquals($mailto, $result);
  1016. $js = 'javascript:alert("hi")';
  1017. $result = Router::url($js);
  1018. $this->assertEquals($js, $result);
  1019. $hash = '#first';
  1020. $result = Router::url($hash);
  1021. $this->assertEquals($hash, $result);
  1022. }
  1023. /**
  1024. * test that you can leave active plugin routes with plugin = null
  1025. *
  1026. * @return void
  1027. */
  1028. public function testCanLeavePlugin() {
  1029. Router::connect('/admin/:controller', array('action' => 'index', 'prefix' => 'admin'));
  1030. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  1031. $request = new Request();
  1032. Router::setRequestInfo(
  1033. $request->addParams(array(
  1034. 'pass' => [],
  1035. 'prefix' => 'admin',
  1036. 'plugin' => 'this',
  1037. 'action' => 'index',
  1038. 'controller' => 'interesting',
  1039. ))->addPaths(array(
  1040. 'base' => '',
  1041. 'here' => '/admin/this/interesting/index',
  1042. 'webroot' => '/',
  1043. ))
  1044. );
  1045. $result = Router::url(array('plugin' => null, 'controller' => 'posts', 'action' => 'index'));
  1046. $this->assertEquals('/admin/posts', $result);
  1047. }
  1048. /**
  1049. * testUrlParsing method
  1050. *
  1051. * @return void
  1052. */
  1053. public function testUrlParsing() {
  1054. extract(Router::getNamedExpressions());
  1055. Router::connect(
  1056. '/posts/:value/:somevalue/:othervalue/*',
  1057. array('controller' => 'Posts', 'action' => 'view'),
  1058. array('value', 'somevalue', 'othervalue')
  1059. );
  1060. $result = Router::parse('/posts/2007/08/01/title-of-post-here');
  1061. $expected = array(
  1062. 'value' => '2007',
  1063. 'somevalue' => '08',
  1064. 'othervalue' => '01',
  1065. 'controller' => 'Posts',
  1066. 'action' => 'view',
  1067. 'plugin' => null,
  1068. 'pass' => array('0' => 'title-of-post-here')
  1069. );
  1070. $this->assertEquals($expected, $result);
  1071. Router::reload();
  1072. Router::connect(
  1073. '/posts/:year/:month/:day/*',
  1074. array('controller' => 'posts', 'action' => 'view'),
  1075. array('year' => $Year, 'month' => $Month, 'day' => $Day)
  1076. );
  1077. $result = Router::parse('/posts/2007/08/01/title-of-post-here');
  1078. $expected = array(
  1079. 'year' => '2007',
  1080. 'month' => '08',
  1081. 'day' => '01',
  1082. 'controller' => 'posts',
  1083. 'action' => 'view',
  1084. 'plugin' => null,
  1085. 'pass' => array('0' => 'title-of-post-here')
  1086. );
  1087. $this->assertEquals($expected, $result);
  1088. Router::reload();
  1089. Router::connect(
  1090. '/posts/:day/:year/:month/*',
  1091. array('controller' => 'posts', 'action' => 'view'),
  1092. array('year' => $Year, 'month' => $Month, 'day' => $Day)
  1093. );
  1094. $result = Router::parse('/posts/01/2007/08/title-of-post-here');
  1095. $expected = array(
  1096. 'day' => '01',
  1097. 'year' => '2007',
  1098. 'month' => '08',
  1099. 'controller' => 'posts',
  1100. 'action' => 'view',
  1101. 'plugin' => null,
  1102. 'pass' => array('0' => 'title-of-post-here')
  1103. );
  1104. $this->assertEquals($expected, $result);
  1105. Router::reload();
  1106. Router::connect(
  1107. '/posts/:month/:day/:year/*',
  1108. array('controller' => 'posts', 'action' => 'view'),
  1109. array('year' => $Year, 'month' => $Month, 'day' => $Day)
  1110. );
  1111. $result = Router::parse('/posts/08/01/2007/title-of-post-here');
  1112. $expected = array(
  1113. 'month' => '08',
  1114. 'day' => '01',
  1115. 'year' => '2007',
  1116. 'controller' => 'posts',
  1117. 'action' => 'view',
  1118. 'plugin' => null,
  1119. 'pass' => array('0' => 'title-of-post-here')
  1120. );
  1121. $this->assertEquals($expected, $result);
  1122. Router::reload();
  1123. Router::connect(
  1124. '/posts/:year/:month/:day/*',
  1125. array('controller' => 'posts', 'action' => 'view')
  1126. );
  1127. $result = Router::parse('/posts/2007/08/01/title-of-post-here');
  1128. $expected = array(
  1129. 'year' => '2007',
  1130. 'month' => '08',
  1131. 'day' => '01',
  1132. 'controller' => 'posts',
  1133. 'action' => 'view',
  1134. 'plugin' => null,
  1135. 'pass' => array('0' => 'title-of-post-here')
  1136. );
  1137. $this->assertEquals($expected, $result);
  1138. Router::reload();
  1139. $this->_connectDefaultRoutes();
  1140. $result = Router::parse('/pages/display/home');
  1141. $expected = array(
  1142. 'plugin' => null,
  1143. 'pass' => array('home'),
  1144. 'controller' => 'Pages',
  1145. 'action' => 'display'
  1146. );
  1147. $this->assertEquals($expected, $result);
  1148. $result = Router::parse('pages/display/home/');
  1149. $this->assertEquals($expected, $result);
  1150. $result = Router::parse('pages/display/home');
  1151. $this->assertEquals($expected, $result);
  1152. Router::reload();
  1153. Router::connect('/page/*', array('controller' => 'test'));
  1154. $result = Router::parse('/page/my-page');
  1155. $expected = array('pass' => array('my-page'), 'plugin' => null, 'controller' => 'test', 'action' => 'index');
  1156. $this->assertEquals($expected, $result);
  1157. Router::reload();
  1158. Router::connect(
  1159. '/:language/contact',
  1160. array('language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'),
  1161. array('language' => '[a-z]{3}')
  1162. );
  1163. $result = Router::parse('/eng/contact');
  1164. $expected = array(
  1165. 'pass' => [],
  1166. 'language' => 'eng',
  1167. 'plugin' => 'contact',
  1168. 'controller' => 'contact',
  1169. 'action' => 'index'
  1170. );
  1171. $this->assertEquals($expected, $result);
  1172. Router::reload();
  1173. Router::connect('/forestillinger/:month/:year/*',
  1174. array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'),
  1175. array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')
  1176. );
  1177. $result = Router::parse('/forestillinger/10/2007/min-forestilling');
  1178. $expected = array(
  1179. 'pass' => array('min-forestilling'),
  1180. 'plugin' => 'shows',
  1181. 'controller' => 'shows',
  1182. 'action' => 'calendar',
  1183. 'year' => 2007,
  1184. 'month' => 10
  1185. );
  1186. $this->assertEquals($expected, $result);
  1187. Router::reload();
  1188. Router::connect('/:controller/:action/*');
  1189. Router::connect('/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'display'));
  1190. $result = Router::parse('/');
  1191. $expected = array('pass' => [], 'controller' => 'pages', 'action' => 'display', 'plugin' => 'pages');
  1192. $this->assertEquals($expected, $result);
  1193. $result = Router::parse('/posts/edit/0');
  1194. $expected = array('pass' => array(0), 'controller' => 'posts', 'action' => 'edit', 'plugin' => null);
  1195. $this->assertEquals($expected, $result);
  1196. Router::reload();
  1197. Router::connect(
  1198. '/posts/:id::url_title',
  1199. array('controller' => 'posts', 'action' => 'view'),
  1200. array('pass' => array('id', 'url_title'), 'id' => '[\d]+')
  1201. );
  1202. $result = Router::parse('/posts/5:sample-post-title');
  1203. $expected = array(
  1204. 'pass' => array('5', 'sample-post-title'),
  1205. 'id' => 5,
  1206. 'url_title' => 'sample-post-title',
  1207. 'plugin' => null,
  1208. 'controller' => 'posts',
  1209. 'action' => 'view'
  1210. );
  1211. $this->assertEquals($expected, $result);
  1212. Router::reload();
  1213. Router::connect(
  1214. '/posts/:id::url_title/*',
  1215. array('controller' => 'posts', 'action' => 'view'),
  1216. array('pass' => array('id', 'url_title'), 'id' => '[\d]+')
  1217. );
  1218. $result = Router::parse('/posts/5:sample-post-title/other/params/4');
  1219. $expected = array(
  1220. 'pass' => array('5', 'sample-post-title', 'other', 'params', '4'),
  1221. 'id' => 5,
  1222. 'url_title' => 'sample-post-title',
  1223. 'plugin' => null,
  1224. 'controller' => 'posts',
  1225. 'action' => 'view'
  1226. );
  1227. $this->assertEquals($expected, $result);
  1228. Router::reload();
  1229. Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'));
  1230. $result = Router::parse('/posts/view/10?id=123&tab=abc');
  1231. $expected = array('pass' => array(10), 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '?' => array('id' => '123', 'tab' => 'abc'));
  1232. $this->assertEquals($expected, $result);
  1233. Router::reload();
  1234. Router::connect(
  1235. '/posts/:url_title-(uuid::id)',
  1236. array('controller' => 'posts', 'action' => 'view'),
  1237. array('pass' => array('id', 'url_title'), 'id' => $UUID)
  1238. );
  1239. $result = Router::parse('/posts/sample-post-title-(uuid:47fc97a9-019c-41d1-a058-1fa3cbdd56cb)');
  1240. $expected = array(
  1241. 'pass' => array('47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'sample-post-title'),
  1242. 'id' => '47fc97a9-019c-41d1-a058-1fa3cbdd56cb',
  1243. 'url_title' => 'sample-post-title',
  1244. 'plugin' => null,
  1245. 'controller' => 'posts',
  1246. 'action' => 'view'
  1247. );
  1248. $this->assertEquals($expected, $result);
  1249. Router::reload();
  1250. Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'));
  1251. $result = Router::parse('/posts/view/foo:bar/routing:fun');
  1252. $expected = array(
  1253. 'pass' => array('foo:bar', 'routing:fun'),
  1254. 'plugin' => null,
  1255. 'controller' => 'posts',
  1256. 'action' => 'view'
  1257. );
  1258. $this->assertEquals($expected, $result);
  1259. }
  1260. /**
  1261. * testUuidRoutes method
  1262. *
  1263. * @return void
  1264. */
  1265. public function testUuidRoutes() {
  1266. Router::connect(
  1267. '/subjects/add/:category_id',
  1268. array('controller' => 'subjects', 'action' => 'add'),
  1269. array('category_id' => '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}')
  1270. );
  1271. $result = Router::parse('/subjects/add/4795d601-19c8-49a6-930e-06a8b01d17b7');
  1272. $expected = array(
  1273. 'pass' => [],
  1274. 'category_id' => '4795d601-19c8-49a6-930e-06a8b01d17b7',
  1275. 'plugin' => null,
  1276. 'controller' => 'subjects',
  1277. 'action' => 'add'
  1278. );
  1279. $this->assertEquals($expected, $result);
  1280. }
  1281. /**
  1282. * testRouteSymmetry method
  1283. *
  1284. * @return void
  1285. */
  1286. public function testRouteSymmetry() {
  1287. Router::connect(
  1288. "/:extra/page/:slug/*",
  1289. array('controller' => 'pages', 'action' => 'view', 'extra' => null),
  1290. array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
  1291. );
  1292. $result = Router::parse('/some_extra/page/this_is_the_slug');
  1293. $expected = array(
  1294. 'pass' => [],
  1295. 'plugin' => null,
  1296. 'controller' => 'pages',
  1297. 'action' => 'view',
  1298. 'slug' => 'this_is_the_slug',
  1299. 'extra' => 'some_extra'
  1300. );
  1301. $this->assertEquals($expected, $result);
  1302. $result = Router::parse('/page/this_is_the_slug');
  1303. $expected = array(
  1304. 'pass' => [],
  1305. 'plugin' => null,
  1306. 'controller' => 'pages',
  1307. 'action' => 'view',
  1308. 'slug' => 'this_is_the_slug',
  1309. 'extra' => null
  1310. );
  1311. $this->assertEquals($expected, $result);
  1312. Router::reload();
  1313. Router::connect(
  1314. "/:extra/page/:slug/*",
  1315. array('controller' => 'pages', 'action' => 'view', 'extra' => null),
  1316. array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+')
  1317. );
  1318. $result = Router::url(array(
  1319. 'admin' => null,
  1320. 'plugin' => null,
  1321. 'controller' => 'pages',
  1322. 'action' => 'view',
  1323. 'slug' => 'this_is_the_slug',
  1324. 'extra' => null
  1325. ));
  1326. $expected = '/page/this_is_the_slug';
  1327. $this->assertEquals($expected, $result);
  1328. $result = Router::url(array(
  1329. 'admin' => null,
  1330. 'plugin' => null,
  1331. 'controller' => 'pages',
  1332. 'action' => 'view',
  1333. 'slug' => 'this_is_the_slug',
  1334. 'extra' => 'some_extra'
  1335. ));
  1336. $expected = '/some_extra/page/this_is_the_slug';
  1337. $this->assertEquals($expected, $result);
  1338. }
  1339. /**
  1340. * Test exceptions when parsing fails.
  1341. *
  1342. * @expectedException Cake\Routing\Exception\MissingRouteException
  1343. */
  1344. public function testParseError() {
  1345. Router::connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
  1346. Router::parse('/nope');
  1347. }
  1348. /**
  1349. * Test parse and reverse symmetry
  1350. *
  1351. * @return void
  1352. * @dataProvider parseReverseSymmetryData
  1353. */
  1354. public function testParseReverseSymmetry($url) {
  1355. $this->_connectDefaultRoutes();
  1356. $this->assertSame($url, Router::reverse(Router::parse($url) + array('url' => [])));
  1357. }
  1358. /**
  1359. * Data for parse and reverse test
  1360. *
  1361. * @return array
  1362. */
  1363. public function parseReverseSymmetryData() {
  1364. return array(
  1365. array('/controller/action'),
  1366. array('/controller/action/param'),
  1367. array('/controller/action?param1=value1&param2=value2'),
  1368. array('/controller/action/param?param1=value1'),
  1369. );
  1370. }
  1371. /**
  1372. * testSetExtensions method
  1373. *
  1374. * @return void
  1375. */
  1376. public function testSetExtensions() {
  1377. Router::extensions('rss', false);
  1378. $this->assertContains('rss', Router::extensions());
  1379. $this->_connectDefaultRoutes();
  1380. $result = Router::parse('/posts.rss');
  1381. $this->assertEquals('rss', $result['_ext']);
  1382. $result = Router::parse('/posts.xml');
  1383. $this->assertFalse(isset($result['_ext']));
  1384. Router::extensions(array('xml'));
  1385. }
  1386. /**
  1387. * Test that route builders propagate extensions to the top.
  1388. *
  1389. * @return void
  1390. */
  1391. public function testExtensionsWithScopedRoutes() {
  1392. Router::scope('/', function ($routes) {
  1393. $routes->extensions('rss');
  1394. $routes->connect('/', ['controller' => 'Pages', 'action' => 'index']);
  1395. $routes->scope('/api', function ($routes) {
  1396. $routes->extensions('xml');
  1397. $routes->connect('/docs', ['controller' => 'ApiDocs', 'action' => 'index']);
  1398. });
  1399. });
  1400. $this->assertEquals(['rss', 'xml', 'json'], Router::extensions());
  1401. }
  1402. /**
  1403. * testExtensionParsing method
  1404. *
  1405. * @return void
  1406. */
  1407. public function testExtensionParsing() {
  1408. Router::extensions('rss', false);
  1409. $this->_connectDefaultRoutes();
  1410. $result = Router::parse('/posts.rss');
  1411. $expected = array(
  1412. 'plugin' => null,
  1413. 'controller' => 'Posts',
  1414. 'action' => 'index',
  1415. '_ext' => 'rss',
  1416. 'pass' => []
  1417. );
  1418. $this->assertEquals($expected, $result);
  1419. $result = Router::parse('/posts/view/1.rss');
  1420. $expected = array(
  1421. 'plugin' => null,
  1422. 'controller' => 'Posts',
  1423. 'action' => 'view',
  1424. 'pass' => array('1'),
  1425. '_ext' => 'rss'
  1426. );
  1427. $this->assertEquals($expected, $result);
  1428. $result = Router::parse('/posts/view/1.rss?query=test');
  1429. $expected['?'] = array('query' => 'test');
  1430. $this->assertEquals($expected, $result);
  1431. Router::reload();
  1432. Router::extensions(['rss', 'xml'], false);
  1433. $this->_connectDefaultRoutes();
  1434. $result = Router::parse('/posts.xml');
  1435. $expected = array(
  1436. 'plugin' => null,
  1437. 'controller' => 'Posts',
  1438. 'action' => 'index',
  1439. '_ext' => 'xml',
  1440. 'pass' => []
  1441. );
  1442. $this->assertEquals($expected, $result);
  1443. $result = Router::parse('/posts.atom?hello=goodbye');
  1444. $expected = array(
  1445. 'plugin' => null,
  1446. 'controller' => 'Posts.atom',
  1447. 'action' => 'index',
  1448. 'pass' => [],
  1449. '?' => array('hello' => 'goodbye')
  1450. );
  1451. $this->assertEquals($expected, $result);
  1452. Router::reload();
  1453. Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
  1454. $result = Router::parse('/controller/action');
  1455. $expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, '_ext' => 'rss', 'pass' => []);
  1456. $this->assertEquals($expected, $result);
  1457. Router::reload();
  1458. Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
  1459. $result = Router::parse('/controller/action');
  1460. $expected = array(
  1461. 'controller' => 'controller',
  1462. 'action' => 'action',
  1463. 'plugin' => null,
  1464. '_ext' => 'rss',
  1465. 'pass' => []
  1466. );
  1467. $this->assertEquals($expected, $result);
  1468. Router::reload();
  1469. Router::extensions('rss', false);
  1470. Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
  1471. $result = Router::parse('/controller/action');
  1472. $expected = array(
  1473. 'controller' => 'controller',
  1474. 'action' => 'action',
  1475. 'plugin' => null,
  1476. '_ext' => 'rss',
  1477. 'pass' => []
  1478. );
  1479. $this->assertEquals($expected, $result);
  1480. }
  1481. /**
  1482. * Test newer style automatically generated prefix routes.
  1483. *
  1484. * @return void
  1485. * @see testUrlGenerationWithAutoPrefixes
  1486. */
  1487. public function testUrlGenerationWithAutoPrefixes() {
  1488. Router::reload();
  1489. Router::connect('/protected/:controller/:action/*', array('prefix' => 'protected'));
  1490. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  1491. Router::connect('/:controller/:action/*');
  1492. $request = new Request();
  1493. Router::setRequestInfo(
  1494. $request->addParams(array(
  1495. 'plugin' => null, 'controller' => 'images', 'action' => 'index',
  1496. 'prefix' => null, 'protected' => false, 'url' => array('url' => 'images/index')
  1497. ))->addPaths(array(
  1498. 'base' => '',
  1499. 'here' => '/images/index',
  1500. 'webroot' => '/',
  1501. ))
  1502. );
  1503. $result = Router::url(array('controller' => 'images', 'action' => 'add'));
  1504. $expected = '/images/add';
  1505. $this->assertEquals($expected, $result);
  1506. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => 'protected'));
  1507. $expected = '/protected/images/add';
  1508. $this->assertEquals($expected, $result);
  1509. $result = Router::url(array('controller' => 'images', 'action' => 'add_protected_test', 'prefix' => 'protected'));
  1510. $expected = '/protected/images/add_protected_test';
  1511. $this->assertEquals($expected, $result);
  1512. $result = Router::url(array('action' => 'edit', 1));
  1513. $expected = '/images/edit/1';
  1514. $this->assertEquals($expected, $result);
  1515. $result = Router::url(array('action' => 'edit', 1, 'prefix' => 'protected'));
  1516. $expected = '/protected/images/edit/1';
  1517. $this->assertEquals($expected, $result);
  1518. $result = Router::url(array('action' => 'protectededit', 1, 'prefix' => 'protected'));
  1519. $expected = '/protected/images/protectededit/1';
  1520. $this->assertEquals($expected, $result);
  1521. $result = Router::url(array('action' => 'edit', 1, 'prefix' => 'protected'));
  1522. $expected = '/protected/images/edit/1';
  1523. $this->assertEquals($expected, $result);
  1524. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1));
  1525. $expected = '/others/edit/1';
  1526. $this->assertEquals($expected, $result);
  1527. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'prefix' => 'protected'));
  1528. $expected = '/protected/others/edit/1';
  1529. $this->assertEquals($expected, $result);
  1530. }
  1531. /**
  1532. * Test that the ssl option works.
  1533. *
  1534. * @return void
  1535. */
  1536. public function testGenerationWithSslOption() {
  1537. Router::connect('/:controller/:action/*');
  1538. $request = new Request();
  1539. $request->env('HTTP_HOST', 'localhost');
  1540. Router::pushRequest(
  1541. $request->addParams(array(
  1542. 'plugin' => null, 'controller' => 'images', 'action' => 'index'
  1543. ))->addPaths(array(
  1544. 'base' => '',
  1545. 'here' => '/images/index',
  1546. 'webroot' => '/',
  1547. ))
  1548. );
  1549. $result = Router::url(array(
  1550. '_ssl' => true
  1551. ));
  1552. $this->assertEquals('https://localhost/images/index', $result);
  1553. $result = Router::url(array(
  1554. '_ssl' => false
  1555. ));
  1556. $this->assertEquals('http://localhost/images/index', $result);
  1557. }
  1558. /**
  1559. * Test ssl option when the current request is ssl.
  1560. *
  1561. * @return void
  1562. */
  1563. public function testGenerateWithSslInSsl() {
  1564. Router::connect('/:controller/:action/*');
  1565. $request = new Request();
  1566. $request->env('HTTP_HOST', 'localhost');
  1567. $request->env('HTTPS', 'on');
  1568. Router::pushRequest(
  1569. $request->addParams(array(
  1570. 'plugin' => null,
  1571. 'controller' => 'images',
  1572. 'action' => 'index'
  1573. ))->addPaths(array(
  1574. 'base' => '',
  1575. 'here' => '/images/index',
  1576. 'webroot' => '/',
  1577. ))
  1578. );
  1579. $result = Router::url(array(
  1580. '_ssl' => false
  1581. ));
  1582. $this->assertEquals('http://localhost/images/index', $result);
  1583. $result = Router::url(array(
  1584. '_ssl' => true
  1585. ));
  1586. $this->assertEquals('https://localhost/images/index', $result);
  1587. }
  1588. /**
  1589. * test that prefix routes persist when they are in the current request.
  1590. *
  1591. * @return void
  1592. */
  1593. public function testPrefixRoutePersistence() {
  1594. Router::reload();
  1595. Router::connect('/protected/:controller/:action', array('prefix' => 'protected'));
  1596. Router::connect('/:controller/:action');
  1597. $request = new Request();
  1598. Router::setRequestInfo(
  1599. $request->addParams(array(
  1600. 'plugin' => null,
  1601. 'controller' => 'images',
  1602. 'action' => 'index',
  1603. 'prefix' => 'protected',
  1604. ))->addPaths(array(
  1605. 'base' => '',
  1606. 'here' => '/protected/images/index',
  1607. 'webroot' => '/',
  1608. ))
  1609. );
  1610. $result = Router::url(array('prefix' => 'protected', 'controller' => 'images', 'action' => 'add'));
  1611. $expected = '/protected/images/add';
  1612. $this->assertEquals($expected, $result);
  1613. $result = Router::url(array('controller' => 'images', 'action' => 'add'));
  1614. $expected = '/protected/images/add';
  1615. $this->assertEquals($expected, $result);
  1616. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => false));
  1617. $expected = '/images/add';
  1618. $this->assertEquals($expected, $result);
  1619. }
  1620. /**
  1621. * test that setting a prefix override the current one
  1622. *
  1623. * @return void
  1624. */
  1625. public function testPrefixOverride() {
  1626. Router::connect('/admin/:controller/:action', array('prefix' => 'admin'));
  1627. Router::connect('/protected/:controller/:action', array('prefix' => 'protected'));
  1628. $request = new Request();
  1629. Router::setRequestInfo(
  1630. $request->addParams(array(
  1631. 'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'protected',
  1632. ))->addPaths(array(
  1633. 'base' => '',
  1634. 'here' => '/protected/images/index',
  1635. 'webroot' => '/',
  1636. ))
  1637. );
  1638. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => 'admin'));
  1639. $expected = '/admin/images/add';
  1640. $this->assertEquals($expected, $result);
  1641. $request = new Request();
  1642. Router::setRequestInfo(
  1643. $request->addParams(array(
  1644. 'plugin' => null,
  1645. 'controller' => 'images',
  1646. 'action' => 'index',
  1647. 'prefix' => 'admin',
  1648. ))->addPaths(array(
  1649. 'base' => '',
  1650. 'here' => '/admin/images/index',
  1651. 'webroot' => '/',
  1652. ))
  1653. );
  1654. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => 'protected'));
  1655. $expected = '/protected/images/add';
  1656. $this->assertEquals($expected, $result);
  1657. }
  1658. /**
  1659. * Test that well known route parameters are passed through.
  1660. *
  1661. * @return void
  1662. */
  1663. public function testRouteParamDefaults() {
  1664. Router::connect('/cache/*', array('prefix' => false, 'plugin' => true, 'controller' => 0, 'action' => 1));
  1665. $url = Router::url(array('prefix' => 0, 'plugin' => 1, 'controller' => 0, 'action' => 1, 'test'));
  1666. $expected = '/cache/test';
  1667. $this->assertEquals($expected, $url);
  1668. try {
  1669. Router::url(array('controller' => 0, 'action' => 1, 'test'));
  1670. $this->fail('No exception raised');
  1671. } catch (\Exception $e) {
  1672. $this->assertTrue(true, 'Exception was raised');
  1673. }
  1674. try {
  1675. Router::url(array('prefix' => 1, 'controller' => 0, 'action' => 1, 'test'));
  1676. $this->fail('No exception raised');
  1677. } catch (\Exception $e) {
  1678. $this->assertTrue(true, 'Exception was raised');
  1679. }
  1680. }
  1681. /**
  1682. * testRemoveBase method
  1683. *
  1684. * @return void
  1685. */
  1686. public function testRemoveBase() {
  1687. Router::connect('/:controller/:action');
  1688. $request = new Request();
  1689. Router::setRequestInfo(
  1690. $request->addParams(array(
  1691. 'plugin' => null, 'controller' => 'controller', 'action' => 'index',
  1692. ))->addPaths(array(
  1693. 'base' => '/base',
  1694. 'here' => '/',
  1695. 'webroot' => '/base/',
  1696. ))
  1697. );
  1698. $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action'));
  1699. $expected = '/base/my_controller/my_action';
  1700. $this->assertEquals($expected, $result);
  1701. $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', '_base' => false));
  1702. $expected = '/my_controller/my_action';
  1703. $this->assertEquals($expected, $result);
  1704. }
  1705. /**
  1706. * testPagesUrlParsing method
  1707. *
  1708. * @return void
  1709. */
  1710. public function testPagesUrlParsing() {
  1711. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  1712. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  1713. $result = Router::parse('/');
  1714. $expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1715. $this->assertEquals($expected, $result);
  1716. $result = Router::parse('/pages/home/');
  1717. $expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1718. $this->assertEquals($expected, $result);
  1719. Router::reload();
  1720. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  1721. $result = Router::parse('/');
  1722. $expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1723. $this->assertEquals($expected, $result);
  1724. Router::reload();
  1725. Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
  1726. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  1727. $result = Router::parse('/pages/contact/');
  1728. $expected = array('pass' => array('contact'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1729. $this->assertEquals($expected, $result);
  1730. }
  1731. /**
  1732. * test that requests with a trailing dot don't loose the do.
  1733. *
  1734. * @return void
  1735. */
  1736. public function testParsingWithTrailingPeriod() {
  1737. Router::reload();
  1738. Router::connect('/:controller/:action/*');
  1739. $result = Router::parse('/posts/view/something.');
  1740. $this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
  1741. $result = Router::parse('/posts/view/something. . .');
  1742. $this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s');
  1743. }
  1744. /**
  1745. * test that requests with a trailing dot don't loose the do.
  1746. *
  1747. * @return void
  1748. */
  1749. public function testParsingWithTrailingPeriodAndParseExtensions() {
  1750. Router::reload();
  1751. Router::connect('/:controller/:action/*');
  1752. Router::extensions('json', false);
  1753. $result = Router::parse('/posts/view/something.');
  1754. $this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
  1755. $result = Router::parse('/posts/view/something. . .');
  1756. $this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s');
  1757. }
  1758. /**
  1759. * test that patterns work for :action
  1760. *
  1761. * @expectedException Cake\Routing\Exception\MissingRouteException
  1762. * @return void
  1763. */
  1764. public function testParsingWithPatternOnAction() {
  1765. Router::connect(
  1766. '/blog/:action/*',
  1767. array('controller' => 'blog_posts'),
  1768. array('action' => 'other|actions')
  1769. );
  1770. $result = Router::parse('/blog/other');
  1771. $expected = array(
  1772. 'plugin' => null,
  1773. 'controller' => 'blog_posts',
  1774. 'action' => 'other',
  1775. 'pass' => [],
  1776. );
  1777. $this->assertEquals($expected, $result);
  1778. Router::parse('/blog/foobar');
  1779. }
  1780. /**
  1781. * Test url() works with patterns on :action
  1782. *
  1783. * @expectedException Cake\Routing\Exception\MissingRouteException
  1784. * @return void
  1785. */
  1786. public function testUrlPatternOnAction() {
  1787. Router::connect(
  1788. '/blog/:action/*',
  1789. array('controller' => 'blog_posts'),
  1790. array('action' => 'other|actions')
  1791. );
  1792. $result = Router::url(array('controller' => 'blog_posts', 'action' => 'actions'));
  1793. $this->assertEquals('/blog/actions', $result);
  1794. $result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo'));
  1795. $this->assertEquals('/', $result);
  1796. }
  1797. /**
  1798. * testParsingWithLiteralPrefixes method
  1799. *
  1800. * @return void
  1801. */
  1802. public function testParsingWithLiteralPrefixes() {
  1803. Router::reload();
  1804. $adminParams = array('prefix' => 'admin');
  1805. Router::connect('/admin/:controller', $adminParams);
  1806. Router::connect('/admin/:controller/:action/*', $adminParams);
  1807. $request = new Request();
  1808. Router::setRequestInfo(
  1809. $request->addParams(array(
  1810. 'plugin' => null, 'controller' => 'controller', 'action' => 'index'
  1811. ))->addPaths(array(
  1812. 'base' => '/base',
  1813. 'here' => '/',
  1814. 'webroot' => '/base/',
  1815. ))
  1816. );
  1817. $result = Router::parse('/admin/posts/');
  1818. $expected = array('pass' => [], 'prefix' => 'admin', 'plugin' => null, 'controller' => 'posts', 'action' => 'index');
  1819. $this->assertEquals($expected, $result);
  1820. $result = Router::parse('/admin/posts');
  1821. $this->assertEquals($expected, $result);
  1822. $result = Router::url(array('prefix' => 'admin', 'controller' => 'posts'));
  1823. $expected = '/base/admin/posts';
  1824. $this->assertEquals($expected, $result);
  1825. Router::reload();
  1826. $prefixParams = array('prefix' => 'members');
  1827. Router::connect('/members/:controller', $prefixParams);
  1828. Router::connect('/members/:controller/:action', $prefixParams);
  1829. Router::connect('/members/:controller/:action/*', $prefixParams);
  1830. $request = new Request();
  1831. Router::setRequestInfo(
  1832. $request->addParams(array(
  1833. 'plugin' => null, 'controller' => 'controller', 'action' => 'index',
  1834. ))->addPaths(array(
  1835. 'base' => '/base',
  1836. 'here' => '/',
  1837. 'webroot' => '/',
  1838. ))
  1839. );
  1840. $result = Router::parse('/members/posts/index');
  1841. $expected = array('pass' => [], 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'index');
  1842. $this->assertEquals($expected, $result);
  1843. $result = Router::url(array('prefix' => 'members', 'controller' => 'users', 'action' => 'add'));
  1844. $expected = '/base/members/users/add';
  1845. $this->assertEquals($expected, $result);
  1846. }
  1847. /**
  1848. * Tests URL generation with flags and prefixes in and out of context
  1849. *
  1850. * @return void
  1851. */
  1852. public function testUrlWritingWithPrefixes() {
  1853. Router::connect('/company/:controller/:action/*', array('prefix' => 'company'));
  1854. Router::connect('/:action', array('controller' => 'users'));
  1855. $result = Router::url(array('controller' => 'users', 'action' => 'login', 'prefix' => 'company'));
  1856. $expected = '/company/users/login';
  1857. $this->assertEquals($expected, $result);
  1858. $request = new Request();
  1859. Router::setRequestInfo(
  1860. $request->addParams(array(
  1861. 'plugin' => null,
  1862. 'controller' => 'users',
  1863. 'action' => 'login',
  1864. 'prefix' => 'company'
  1865. ))->addPaths(array(
  1866. 'base' => '/',
  1867. 'here' => '/',
  1868. 'webroot' => '/base/',
  1869. ))
  1870. );
  1871. $result = Router::url(array('controller' => 'users', 'action' => 'login', 'prefix' => false));
  1872. $expected = '/login';
  1873. $this->assertEquals($expected, $result);
  1874. }
  1875. /**
  1876. * test url generation with prefixes and custom routes
  1877. *
  1878. * @return void
  1879. */
  1880. public function testUrlWritingWithPrefixesAndCustomRoutes() {
  1881. Router::connect(
  1882. '/admin/login',
  1883. array('controller' => 'users', 'action' => 'login', 'prefix' => 'admin')
  1884. );
  1885. $request = new Request();
  1886. Router::setRequestInfo(
  1887. $request->addParams(array(
  1888. 'plugin' => null, 'controller' => 'posts', 'action' => 'index',
  1889. 'prefix' => 'admin'
  1890. ))->addPaths(array(
  1891. 'base' => '/',
  1892. 'here' => '/',
  1893. 'webroot' => '/',
  1894. ))
  1895. );
  1896. $result = Router::url(array('controller' => 'users', 'action' => 'login'));
  1897. $this->assertEquals('/admin/login', $result);
  1898. $result = Router::url(array('controller' => 'users', 'action' => 'login'));
  1899. $this->assertEquals('/admin/login', $result);
  1900. }
  1901. /**
  1902. * testPassedArgsOrder method
  1903. *
  1904. * @return void
  1905. */
  1906. public function testPassedArgsOrder() {
  1907. Router::connect('/test-passed/*', array('controller' => 'pages', 'action' => 'display', 'home'));
  1908. Router::connect('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
  1909. Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 1));
  1910. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 1, 'whatever'));
  1911. $expected = '/test/whatever';
  1912. $this->assertEquals($expected, $result);
  1913. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 2, 'whatever'));
  1914. $expected = '/test2/whatever';
  1915. $this->assertEquals($expected, $result);
  1916. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 'home', 'whatever'));
  1917. $expected = '/test-passed/whatever';
  1918. $this->assertEquals($expected, $result);
  1919. }
  1920. /**
  1921. * testRegexRouteMatching method
  1922. *
  1923. * @return void
  1924. */
  1925. public function testRegexRouteMatching() {
  1926. Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
  1927. $result = Router::parse('/eng/test/test_action');
  1928. $expected = array('pass' => [], 'locale' => 'eng', 'controller' => 'test', 'action' => 'test_action', 'plugin' => null);
  1929. $this->assertEquals($expected, $result);
  1930. }
  1931. /**
  1932. * testRegexRouteMatching error
  1933. *
  1934. * @expectedException Cake\Routing\Exception\MissingRouteException
  1935. * @return void
  1936. */
  1937. public function testRegexRouteMatchingError() {
  1938. Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
  1939. Router::parse('/badness/test/test_action');
  1940. }
  1941. /**
  1942. * testRegexRouteMatching method
  1943. *
  1944. * @expectedException Cake\Routing\Exception\MissingRouteException
  1945. * @return void
  1946. */
  1947. public function testRegexRouteMatchUrl() {
  1948. Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
  1949. $request = new Request();
  1950. Router::setRequestInfo(
  1951. $request->addParams(array(
  1952. 'plugin' => null,
  1953. 'controller' => 'test',
  1954. 'action' => 'index',
  1955. 'url' => array('url' => 'test/test_action')
  1956. ))->addPaths(array(
  1957. 'base' => '',
  1958. 'here' => '/test/test_action',
  1959. 'webroot' => '/',
  1960. ))
  1961. );
  1962. $result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng'));
  1963. $expected = '/eng/test/test_another_action';
  1964. $this->assertEquals($expected, $result);
  1965. $result = Router::url(array('action' => 'test_another_action'));
  1966. $expected = '/';
  1967. $this->assertEquals($expected, $result);
  1968. }
  1969. /**
  1970. * test using a custom route class for route connection
  1971. *
  1972. * @return void
  1973. */
  1974. public function testUsingCustomRouteClass() {
  1975. Plugin::load('TestPlugin');
  1976. Router::connect(
  1977. '/:slug',
  1978. array('plugin' => 'TestPlugin', 'action' => 'index'),
  1979. array('routeClass' => 'PluginShortRoute', 'slug' => '[a-z_-]+')
  1980. );
  1981. $result = Router::parse('/the-best');
  1982. $expected = [
  1983. 'plugin' => 'TestPlugin',
  1984. 'controller' => 'TestPlugin',
  1985. 'action' => 'index',
  1986. 'slug' => 'the-best',
  1987. 'pass' => [],
  1988. ];
  1989. $this->assertEquals($expected, $result);
  1990. }
  1991. /**
  1992. * test using custom route class in PluginDot notation
  1993. *
  1994. * @return void
  1995. */
  1996. public function testUsingCustomRouteClassPluginDotSyntax() {
  1997. Plugin::load('TestPlugin');
  1998. Router::connect(
  1999. '/:slug',
  2000. array('controller' => 'posts', 'action' => 'view'),
  2001. array('routeClass' => 'TestPlugin.TestRoute', 'slug' => '[a-z_-]+')
  2002. );
  2003. $this->assertTrue(true); // Just to make sure the connect do not throw exception
  2004. Plugin::unload('TestPlugin');
  2005. }
  2006. /**
  2007. * test that route classes must extend \Cake\Routing\Route\Route
  2008. *
  2009. * @expectedException InvalidArgumentException
  2010. * @return void
  2011. */
  2012. public function testCustomRouteException() {
  2013. Router::connect('/:controller', [], array('routeClass' => 'Object'));
  2014. }
  2015. /**
  2016. * test reversing parameter arrays back into strings.
  2017. *
  2018. * Mark the router as initialized so it doesn't auto-load routes
  2019. *
  2020. * @return void
  2021. */
  2022. public function testReverse() {
  2023. Router::connect('/:controller/:action/*');
  2024. $params = array(
  2025. 'controller' => 'posts',
  2026. 'action' => 'view',
  2027. 'pass' => array(1),
  2028. 'url' => [],
  2029. 'autoRender' => 1,
  2030. 'bare' => 1,
  2031. 'return' => 1,
  2032. 'requested' => 1,
  2033. '_Token' => array('key' => 'sekret')
  2034. );
  2035. $result = Router::reverse($params);
  2036. $this->assertEquals('/posts/view/1', $result);
  2037. Router::reload();
  2038. Router::connect('/:lang/:controller/:action/*', [], array('lang' => '[a-z]{3}'));
  2039. $params = array(
  2040. 'lang' => 'eng',
  2041. 'controller' => 'posts',
  2042. 'action' => 'view',
  2043. 'pass' => array(1),
  2044. 'url' => array('url' => 'eng/posts/view/1')
  2045. );
  2046. $result = Router::reverse($params);
  2047. $this->assertEquals('/eng/posts/view/1', $result);
  2048. $params = array(
  2049. 'lang' => 'eng',
  2050. 'controller' => 'posts',
  2051. 'action' => 'view',
  2052. 1,
  2053. '?' => ['foo' => 'bar']
  2054. );
  2055. $result = Router::reverse($params);
  2056. $this->assertEquals('/eng/posts/view/1?foo=bar', $result);
  2057. $params = array(
  2058. 'lang' => 'eng',
  2059. 'controller' => 'posts',
  2060. 'action' => 'view',
  2061. 'pass' => array(1),
  2062. 'url' => array('url' => 'eng/posts/view/1', 'foo' => 'bar', 'baz' => 'quu'),
  2063. 'paging' => [],
  2064. 'models' => []
  2065. );
  2066. $result = Router::reverse($params);
  2067. $this->assertEquals('/eng/posts/view/1?foo=bar&baz=quu', $result);
  2068. $request = new Request('/eng/posts/view/1');
  2069. $request->addParams(array(
  2070. 'lang' => 'eng',
  2071. 'controller' => 'posts',
  2072. 'action' => 'view',
  2073. 'pass' => array(1),
  2074. ));
  2075. $request->query = array('url' => 'eng/posts/view/1', 'test' => 'value');
  2076. $result = Router::reverse($request);
  2077. $expected = '/eng/posts/view/1?test=value';
  2078. $this->assertEquals($expected, $result);
  2079. $params = array(
  2080. 'lang' => 'eng',
  2081. 'controller' => 'posts',
  2082. 'action' => 'view',
  2083. 'pass' => array(1),
  2084. 'url' => array('url' => 'eng/posts/view/1')
  2085. );
  2086. $result = Router::reverse($params, true);
  2087. $this->assertRegExp('/^http(s)?:\/\//', $result);
  2088. }
  2089. /**
  2090. * Test that extensions work with Router::reverse()
  2091. *
  2092. * @return void
  2093. */
  2094. public function testReverseWithExtension() {
  2095. Router::connect('/:controller/:action/*');
  2096. Router::extensions('json', false);
  2097. $request = new Request('/posts/view/1.json');
  2098. $request->addParams(array(
  2099. 'controller' => 'posts',
  2100. 'action' => 'view',
  2101. 'pass' => array(1),
  2102. '_ext' => 'json',
  2103. ));
  2104. $request->query = [];
  2105. $result = Router::reverse($request);
  2106. $expected = '/posts/view/1.json';
  2107. $this->assertEquals($expected, $result);
  2108. }
  2109. /**
  2110. * test that setRequestInfo can accept arrays and turn that into a Request object.
  2111. *
  2112. * @return void
  2113. */
  2114. public function testSetRequestInfoLegacy() {
  2115. Router::setRequestInfo(array(
  2116. array(
  2117. 'plugin' => null, 'controller' => 'images', 'action' => 'index',
  2118. 'url' => array('url' => 'protected/images/index')
  2119. ),
  2120. array(
  2121. 'base' => '',
  2122. 'here' => '/protected/images/index',
  2123. 'webroot' => '/',
  2124. )
  2125. ));
  2126. $result = Router::getRequest();
  2127. $this->assertEquals('images', $result->controller);
  2128. $this->assertEquals('index', $result->action);
  2129. $this->assertEquals('', $result->base);
  2130. $this->assertEquals('/protected/images/index', $result->here);
  2131. $this->assertEquals('/', $result->webroot);
  2132. }
  2133. /**
  2134. * test get request.
  2135. *
  2136. * @return void
  2137. */
  2138. public function testGetRequest() {
  2139. $requestA = new Request('/');
  2140. $requestB = new Request('/posts');
  2141. Router::pushRequest($requestA);
  2142. Router::pushRequest($requestB);
  2143. $this->assertSame($requestA, Router::getRequest(false));
  2144. $this->assertSame($requestB, Router::getRequest(true));
  2145. }
  2146. /**
  2147. * Test that Router::url() uses the first request
  2148. */
  2149. public function testUrlWithRequestAction() {
  2150. Router::connect('/:controller', array('action' => 'index'));
  2151. Router::connect('/:controller/:action');
  2152. $firstRequest = new Request('/posts/index');
  2153. $firstRequest->addParams(array(
  2154. 'plugin' => null,
  2155. 'controller' => 'posts',
  2156. 'action' => 'index'
  2157. ))->addPaths(array('base' => ''));
  2158. $secondRequest = new Request('/posts/index');
  2159. $secondRequest->addParams(array(
  2160. 'requested' => 1,
  2161. 'plugin' => null,
  2162. 'controller' => 'comments',
  2163. 'action' => 'listing'
  2164. ))->addPaths(array('base' => ''));
  2165. Router::setRequestInfo($firstRequest);
  2166. Router::setRequestInfo($secondRequest);
  2167. $result = Router::url(array('_base' => false));
  2168. $this->assertEquals('/comments/listing', $result, 'with second requests, the last should win.');
  2169. Router::popRequest();
  2170. $result = Router::url(array('_base' => false));
  2171. $this->assertEquals('/posts', $result, 'with second requests, the last should win.');
  2172. // Make sure that popping an empty request doesn't fail.
  2173. Router::popRequest();
  2174. }
  2175. /**
  2176. * test that a route object returning a full URL is not modified.
  2177. *
  2178. * @return void
  2179. */
  2180. public function testUrlFullUrlReturnFromRoute() {
  2181. $url = 'http://example.com/posts/view/1';
  2182. $route = $this->getMock(
  2183. 'Cake\Routing\Route\Route',
  2184. ['match'],
  2185. ['/:controller/:action/*']
  2186. );
  2187. $route->expects($this->any())
  2188. ->method('match')
  2189. ->will($this->returnValue($url));
  2190. Router::connect($route);
  2191. $result = Router::url(array('controller' => 'posts', 'action' => 'view', 1));
  2192. $this->assertEquals($url, $result);
  2193. }
  2194. /**
  2195. * test protocol in url
  2196. *
  2197. * @return void
  2198. */
  2199. public function testUrlProtocol() {
  2200. $url = 'http://example.com';
  2201. $this->assertEquals($url, Router::url($url));
  2202. $url = 'ed2k://example.com';
  2203. $this->assertEquals($url, Router::url($url));
  2204. $url = 'svn+ssh://example.com';
  2205. $this->assertEquals($url, Router::url($url));
  2206. $url = '://example.com';
  2207. $this->assertEquals($url, Router::url($url));
  2208. $url = '//example.com';
  2209. $this->assertEquals($url, Router::url($url));
  2210. $url = 'javascript:void(0)';
  2211. $this->assertEquals($url, Router::url($url));
  2212. $url = 'tel:012345-678';
  2213. $this->assertEquals($url, Router::url($url));
  2214. $url = 'sms:012345-678';
  2215. $this->assertEquals($url, Router::url($url));
  2216. $url = '#here';
  2217. $this->assertEquals($url, Router::url($url));
  2218. $url = '?param=0';
  2219. $this->assertEquals($url, Router::url($url));
  2220. $url = '/posts/index#here';
  2221. $expected = Configure::read('App.fullBaseUrl') . '/posts/index#here';
  2222. $this->assertEquals($expected, Router::url($url, true));
  2223. }
  2224. /**
  2225. * Testing that patterns on the :action param work properly.
  2226. *
  2227. * @return void
  2228. */
  2229. public function testPatternOnAction() {
  2230. $route = new Route(
  2231. '/blog/:action/*',
  2232. array('controller' => 'blog_posts'),
  2233. array('action' => 'other|actions')
  2234. );
  2235. $result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
  2236. $this->assertFalse($result);
  2237. $result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
  2238. $this->assertEquals('/blog/actions/', $result);
  2239. $result = $route->parse('/blog/other');
  2240. $expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => []);
  2241. $this->assertEquals($expected, $result);
  2242. $result = $route->parse('/blog/foobar');
  2243. $this->assertFalse($result);
  2244. }
  2245. /**
  2246. * Test that redirect() works.
  2247. *
  2248. * @return void
  2249. */
  2250. public function testRedirect() {
  2251. Router::redirect('/mobile', '/', ['status' => 301]);
  2252. $routes = Router::routes();
  2253. $route = $routes[0];
  2254. $this->assertInstanceOf('Cake\Routing\Route\RedirectRoute', $route);
  2255. }
  2256. /**
  2257. * Test that the compatibility method for incoming urls works.
  2258. *
  2259. * @return void
  2260. */
  2261. public function testParseNamedParameters() {
  2262. $request = new Request();
  2263. $request->addParams(array(
  2264. 'controller' => 'posts',
  2265. 'action' => 'index',
  2266. ));
  2267. $result = Router::parseNamedParams($request);
  2268. $this->assertSame([], $result->params['named']);
  2269. $request = new Request();
  2270. $request->addParams(array(
  2271. 'controller' => 'posts',
  2272. 'action' => 'index',
  2273. 'pass' => array('home', 'one:two', 'three:four', 'five[nested][0]:six', 'five[nested][1]:seven')
  2274. ));
  2275. Router::parseNamedParams($request);
  2276. $expected = array(
  2277. 'plugin' => null,
  2278. 'controller' => 'posts',
  2279. 'action' => 'index',
  2280. '_ext' => null,
  2281. 'pass' => array('home'),
  2282. 'named' => array(
  2283. 'one' => 'two',
  2284. 'three' => 'four',
  2285. 'five' => array(
  2286. 'nested' => array('six', 'seven')
  2287. )
  2288. )
  2289. );
  2290. $this->assertEquals($expected, $request->params);
  2291. }
  2292. /**
  2293. * Test the scope() method
  2294. *
  2295. * @return void
  2296. */
  2297. public function testScope() {
  2298. Router::scope('/path', ['param' => 'value'], function ($routes) {
  2299. $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
  2300. $this->assertEquals('/path', $routes->path());
  2301. $this->assertEquals(['param' => 'value'], $routes->params());
  2302. $routes->connect('/articles', ['controller' => 'Articles']);
  2303. });
  2304. }
  2305. /**
  2306. * Test the scope() method
  2307. *
  2308. * @expectedException \InvalidArgumentException
  2309. * @return void
  2310. */
  2311. public function testScopeError() {
  2312. Router::scope('/path', 'derpy');
  2313. }
  2314. /**
  2315. * Test that prefix() creates a scope.
  2316. *
  2317. * @return void
  2318. */
  2319. public function testPrefix() {
  2320. Router::prefix('admin', function ($routes) {
  2321. $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
  2322. $this->assertEquals('/admin', $routes->path());
  2323. $this->assertEquals(['prefix' => 'admin'], $routes->params());
  2324. });
  2325. }
  2326. /**
  2327. * Test that plugin() creates a scope.
  2328. *
  2329. * @return void
  2330. */
  2331. public function testPlugin() {
  2332. Router::plugin('DebugKit', function ($routes) {
  2333. $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
  2334. $this->assertEquals('/debug_kit', $routes->path());
  2335. $this->assertEquals(['plugin' => 'DebugKit'], $routes->params());
  2336. });
  2337. }
  2338. /**
  2339. * Test that plugin() accepts options
  2340. *
  2341. * @return void
  2342. */
  2343. public function testPluginOptions() {
  2344. Router::plugin('DebugKit', ['path' => '/debugger'], function ($routes) {
  2345. $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
  2346. $this->assertEquals('/debugger', $routes->path());
  2347. $this->assertEquals(['plugin' => 'DebugKit'], $routes->params());
  2348. });
  2349. }
  2350. /**
  2351. * Test setting default route class.
  2352. *
  2353. * @return void
  2354. */
  2355. public function testDefaultRouteClass() {
  2356. Router::connect('/:controller', ['action' => 'index']);
  2357. $result = Router::url(['controller' => 'FooBar', 'action' => 'index']);
  2358. $this->assertEquals('/FooBar', $result);
  2359. // This is needed because tests/boostrap.php sets App.namespace to 'App'
  2360. Configure::write('App.namespace', 'TestApp');
  2361. Router::defaultRouteClass('DashedRoute');
  2362. Router::connect('/cake/:controller', ['action' => 'cake']);
  2363. $result = Router::url(['controller' => 'FooBar', 'action' => 'cake']);
  2364. $this->assertEquals('/cake/foo-bar', $result);
  2365. $result = Router::url(['controller' => 'FooBar', 'action' => 'index']);
  2366. $this->assertEquals('/FooBar', $result);
  2367. Router::reload();
  2368. Router::defaultRouteClass('DashedRoute');
  2369. Router::scope('/', function ($routes) {
  2370. $routes->fallbacks();
  2371. });
  2372. $result = Router::url(['controller' => 'FooBar', 'action' => 'index']);
  2373. $this->assertEquals('/foo-bar', $result);
  2374. }
  2375. /**
  2376. * Connect some fallback routes for testing router behavior.
  2377. *
  2378. * @return void
  2379. */
  2380. protected function _connectDefaultRoutes() {
  2381. Router::scope('/', function ($routes) {
  2382. $routes->fallbacks();
  2383. });
  2384. }
  2385. }