RouterTest.php 74 KB

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