RouterTest.php 75 KB

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