RouterTest.php 78 KB

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