RouterTest.php 73 KB

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