RouterTest.php 79 KB

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