RouterTest.php 75 KB

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