RouterTest.php 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658
  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. Router::connect(
  913. '/view/*',
  914. ['action' => 'view'],
  915. ['_name' => 'Articles::view']
  916. );
  917. $url = Router::url(['_name' => 'test', 'name' => 'mark']);
  918. $this->assertEquals('/users/mark', $url);
  919. $url = Router::url([
  920. '_name' => 'test', 'name' => 'mark',
  921. 'page' => 1, 'sort' => 'title', 'dir' => 'desc'
  922. ]);
  923. $this->assertEquals('/users/mark?page=1&sort=title&dir=desc', $url);
  924. $url = Router::url(['_name' => 'Articles::view']);
  925. $this->assertEquals('/view/', $url);
  926. $url = Router::url(['_name' => 'Articles::view', '1']);
  927. $this->assertEquals('/view/1', $url);
  928. $url = Router::url(['_name' => 'Articles::view', '_full' => true, '1']);
  929. $this->assertEquals('http://localhost/view/1', $url);
  930. $url = Router::url(['_name' => 'Articles::view', '1', '#' => 'frag']);
  931. $this->assertEquals('/view/1#frag', $url);
  932. }
  933. /**
  934. * Test that using invalid names causes exceptions.
  935. *
  936. * @expectedException \Cake\Routing\Error\MissingRouteException
  937. * @return void
  938. */
  939. public function testNamedRouteException() {
  940. Router::connect(
  941. '/users/:name',
  942. array('controller' => 'users', 'action' => 'view'),
  943. array('_name' => 'test')
  944. );
  945. $url = Router::url(['_name' => 'junk', 'name' => 'mark']);
  946. }
  947. /**
  948. * Test that url filters are applied to url params.
  949. *
  950. * @return void
  951. */
  952. public function testUrlGenerationWithUrlFilter() {
  953. Router::connect('/:lang/:controller/:action/*');
  954. $request = new Request();
  955. $request->addParams(array(
  956. 'lang' => 'en',
  957. 'controller' => 'posts',
  958. 'action' => 'index'
  959. ))->addPaths(array(
  960. 'base' => '',
  961. 'here' => '/'
  962. ));
  963. Router::pushRequest($request);
  964. $calledCount = 0;
  965. Router::addUrlFilter(function ($url, $request) use (&$calledCount) {
  966. $calledCount++;
  967. $url['lang'] = $request->lang;
  968. return $url;
  969. });
  970. Router::addUrlFilter(function ($url, $request) use (&$calledCount) {
  971. $calledCount++;
  972. $url[] = '1234';
  973. return $url;
  974. });
  975. $result = Router::url(array('controller' => 'tasks', 'action' => 'edit'));
  976. $this->assertEquals('/en/tasks/edit/1234', $result);
  977. $this->assertEquals(2, $calledCount);
  978. }
  979. /**
  980. * Test that plain strings urls work
  981. *
  982. * @return void
  983. */
  984. public function testUrlGenerationPlainString() {
  985. $mailto = 'mailto:mark@example.com';
  986. $result = Router::url($mailto);
  987. $this->assertEquals($mailto, $result);
  988. $js = 'javascript:alert("hi")';
  989. $result = Router::url($js);
  990. $this->assertEquals($js, $result);
  991. $hash = '#first';
  992. $result = Router::url($hash);
  993. $this->assertEquals($hash, $result);
  994. }
  995. /**
  996. * test that you can leave active plugin routes with plugin = null
  997. *
  998. * @return void
  999. */
  1000. public function testCanLeavePlugin() {
  1001. Router::connect('/admin/:controller', array('action' => 'index', 'prefix' => 'admin'));
  1002. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  1003. $request = new Request();
  1004. Router::setRequestInfo(
  1005. $request->addParams(array(
  1006. 'pass' => [],
  1007. 'prefix' => 'admin',
  1008. 'plugin' => 'this',
  1009. 'action' => 'index',
  1010. 'controller' => 'interesting',
  1011. ))->addPaths(array(
  1012. 'base' => '',
  1013. 'here' => '/admin/this/interesting/index',
  1014. 'webroot' => '/',
  1015. ))
  1016. );
  1017. $result = Router::url(array('plugin' => null, 'controller' => 'posts', 'action' => 'index'));
  1018. $this->assertEquals('/admin/posts', $result);
  1019. }
  1020. /**
  1021. * testUrlParsing method
  1022. *
  1023. * @return void
  1024. */
  1025. public function testUrlParsing() {
  1026. extract(Router::getNamedExpressions());
  1027. Router::connect(
  1028. '/posts/:value/:somevalue/:othervalue/*',
  1029. array('controller' => 'Posts', 'action' => 'view'),
  1030. array('value', 'somevalue', 'othervalue')
  1031. );
  1032. $result = Router::parse('/posts/2007/08/01/title-of-post-here');
  1033. $expected = array(
  1034. 'value' => '2007',
  1035. 'somevalue' => '08',
  1036. 'othervalue' => '01',
  1037. 'controller' => 'Posts',
  1038. 'action' => 'view',
  1039. 'plugin' => null,
  1040. 'pass' => array('0' => 'title-of-post-here')
  1041. );
  1042. $this->assertEquals($expected, $result);
  1043. Router::reload();
  1044. Router::connect(
  1045. '/posts/:year/:month/:day/*',
  1046. array('controller' => 'posts', 'action' => 'view'),
  1047. array('year' => $Year, 'month' => $Month, 'day' => $Day)
  1048. );
  1049. $result = Router::parse('/posts/2007/08/01/title-of-post-here');
  1050. $expected = array(
  1051. 'year' => '2007',
  1052. 'month' => '08',
  1053. 'day' => '01',
  1054. 'controller' => 'posts',
  1055. 'action' => 'view',
  1056. 'plugin' => null,
  1057. 'pass' => array('0' => 'title-of-post-here')
  1058. );
  1059. $this->assertEquals($expected, $result);
  1060. Router::reload();
  1061. Router::connect(
  1062. '/posts/:day/:year/:month/*',
  1063. array('controller' => 'posts', 'action' => 'view'),
  1064. array('year' => $Year, 'month' => $Month, 'day' => $Day)
  1065. );
  1066. $result = Router::parse('/posts/01/2007/08/title-of-post-here');
  1067. $expected = array(
  1068. 'day' => '01',
  1069. 'year' => '2007',
  1070. 'month' => '08',
  1071. 'controller' => 'posts',
  1072. 'action' => 'view',
  1073. 'plugin' => null,
  1074. 'pass' => array('0' => 'title-of-post-here')
  1075. );
  1076. $this->assertEquals($expected, $result);
  1077. Router::reload();
  1078. Router::connect(
  1079. '/posts/:month/:day/:year/*',
  1080. array('controller' => 'posts', 'action' => 'view'),
  1081. array('year' => $Year, 'month' => $Month, 'day' => $Day)
  1082. );
  1083. $result = Router::parse('/posts/08/01/2007/title-of-post-here');
  1084. $expected = array(
  1085. 'month' => '08',
  1086. 'day' => '01',
  1087. 'year' => '2007',
  1088. 'controller' => 'posts',
  1089. 'action' => 'view',
  1090. 'plugin' => null,
  1091. 'pass' => array('0' => 'title-of-post-here')
  1092. );
  1093. $this->assertEquals($expected, $result);
  1094. Router::reload();
  1095. Router::connect(
  1096. '/posts/:year/:month/:day/*',
  1097. array('controller' => 'posts', 'action' => 'view')
  1098. );
  1099. $result = Router::parse('/posts/2007/08/01/title-of-post-here');
  1100. $expected = array(
  1101. 'year' => '2007',
  1102. 'month' => '08',
  1103. 'day' => '01',
  1104. 'controller' => 'posts',
  1105. 'action' => 'view',
  1106. 'plugin' => null,
  1107. 'pass' => array('0' => 'title-of-post-here')
  1108. );
  1109. $this->assertEquals($expected, $result);
  1110. Router::reload();
  1111. $this->_connectDefaultRoutes();
  1112. $result = Router::parse('/pages/display/home');
  1113. $expected = array(
  1114. 'plugin' => null,
  1115. 'pass' => array('home'),
  1116. 'controller' => 'Pages',
  1117. 'action' => 'display'
  1118. );
  1119. $this->assertEquals($expected, $result);
  1120. $result = Router::parse('pages/display/home/');
  1121. $this->assertEquals($expected, $result);
  1122. $result = Router::parse('pages/display/home');
  1123. $this->assertEquals($expected, $result);
  1124. Router::reload();
  1125. Router::connect('/page/*', array('controller' => 'test'));
  1126. $result = Router::parse('/page/my-page');
  1127. $expected = array('pass' => array('my-page'), 'plugin' => null, 'controller' => 'test', 'action' => 'index');
  1128. $this->assertEquals($expected, $result);
  1129. Router::reload();
  1130. Router::connect(
  1131. '/:language/contact',
  1132. array('language' => 'eng', 'plugin' => 'contact', 'controller' => 'contact', 'action' => 'index'),
  1133. array('language' => '[a-z]{3}')
  1134. );
  1135. $result = Router::parse('/eng/contact');
  1136. $expected = array(
  1137. 'pass' => [],
  1138. 'language' => 'eng',
  1139. 'plugin' => 'contact',
  1140. 'controller' => 'contact',
  1141. 'action' => 'index'
  1142. );
  1143. $this->assertEquals($expected, $result);
  1144. Router::reload();
  1145. Router::connect('/forestillinger/:month/:year/*',
  1146. array('plugin' => 'shows', 'controller' => 'shows', 'action' => 'calendar'),
  1147. array('month' => '0[1-9]|1[012]', 'year' => '[12][0-9]{3}')
  1148. );
  1149. $result = Router::parse('/forestillinger/10/2007/min-forestilling');
  1150. $expected = array(
  1151. 'pass' => array('min-forestilling'),
  1152. 'plugin' => 'shows',
  1153. 'controller' => 'shows',
  1154. 'action' => 'calendar',
  1155. 'year' => 2007,
  1156. 'month' => 10
  1157. );
  1158. $this->assertEquals($expected, $result);
  1159. Router::reload();
  1160. Router::connect('/:controller/:action/*');
  1161. Router::connect('/', array('plugin' => 'pages', 'controller' => 'pages', 'action' => 'display'));
  1162. $result = Router::parse('/');
  1163. $expected = array('pass' => [], 'controller' => 'pages', 'action' => 'display', 'plugin' => 'pages');
  1164. $this->assertEquals($expected, $result);
  1165. $result = Router::parse('/posts/edit/0');
  1166. $expected = array('pass' => array(0), 'controller' => 'posts', 'action' => 'edit', 'plugin' => null);
  1167. $this->assertEquals($expected, $result);
  1168. Router::reload();
  1169. Router::connect(
  1170. '/posts/:id::url_title',
  1171. array('controller' => 'posts', 'action' => 'view'),
  1172. array('pass' => array('id', 'url_title'), 'id' => '[\d]+')
  1173. );
  1174. $result = Router::parse('/posts/5:sample-post-title');
  1175. $expected = array(
  1176. 'pass' => array('5', 'sample-post-title'),
  1177. 'id' => 5,
  1178. 'url_title' => 'sample-post-title',
  1179. 'plugin' => null,
  1180. 'controller' => 'posts',
  1181. 'action' => 'view'
  1182. );
  1183. $this->assertEquals($expected, $result);
  1184. Router::reload();
  1185. Router::connect(
  1186. '/posts/:id::url_title/*',
  1187. array('controller' => 'posts', 'action' => 'view'),
  1188. array('pass' => array('id', 'url_title'), 'id' => '[\d]+')
  1189. );
  1190. $result = Router::parse('/posts/5:sample-post-title/other/params/4');
  1191. $expected = array(
  1192. 'pass' => array('5', 'sample-post-title', 'other', 'params', '4'),
  1193. 'id' => 5,
  1194. 'url_title' => 'sample-post-title',
  1195. 'plugin' => null,
  1196. 'controller' => 'posts',
  1197. 'action' => 'view'
  1198. );
  1199. $this->assertEquals($expected, $result);
  1200. Router::reload();
  1201. Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'));
  1202. $result = Router::parse('/posts/view/10?id=123&tab=abc');
  1203. $expected = array('pass' => array(10), 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '?' => array('id' => '123', 'tab' => 'abc'));
  1204. $this->assertEquals($expected, $result);
  1205. Router::reload();
  1206. Router::connect(
  1207. '/posts/:url_title-(uuid::id)',
  1208. array('controller' => 'posts', 'action' => 'view'),
  1209. array('pass' => array('id', 'url_title'), 'id' => $UUID)
  1210. );
  1211. $result = Router::parse('/posts/sample-post-title-(uuid:47fc97a9-019c-41d1-a058-1fa3cbdd56cb)');
  1212. $expected = array(
  1213. 'pass' => array('47fc97a9-019c-41d1-a058-1fa3cbdd56cb', 'sample-post-title'),
  1214. 'id' => '47fc97a9-019c-41d1-a058-1fa3cbdd56cb',
  1215. 'url_title' => 'sample-post-title',
  1216. 'plugin' => null,
  1217. 'controller' => 'posts',
  1218. 'action' => 'view'
  1219. );
  1220. $this->assertEquals($expected, $result);
  1221. Router::reload();
  1222. Router::connect('/posts/view/*', array('controller' => 'posts', 'action' => 'view'));
  1223. $result = Router::parse('/posts/view/foo:bar/routing:fun');
  1224. $expected = array(
  1225. 'pass' => array('foo:bar', 'routing:fun'),
  1226. 'plugin' => null,
  1227. 'controller' => 'posts',
  1228. 'action' => 'view'
  1229. );
  1230. $this->assertEquals($expected, $result);
  1231. }
  1232. /**
  1233. * testUuidRoutes method
  1234. *
  1235. * @return void
  1236. */
  1237. public function testUuidRoutes() {
  1238. Router::connect(
  1239. '/subjects/add/:category_id',
  1240. array('controller' => 'subjects', 'action' => 'add'),
  1241. array('category_id' => '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}')
  1242. );
  1243. $result = Router::parse('/subjects/add/4795d601-19c8-49a6-930e-06a8b01d17b7');
  1244. $expected = array(
  1245. 'pass' => [],
  1246. 'category_id' => '4795d601-19c8-49a6-930e-06a8b01d17b7',
  1247. 'plugin' => null,
  1248. 'controller' => 'subjects',
  1249. 'action' => 'add'
  1250. );
  1251. $this->assertEquals($expected, $result);
  1252. }
  1253. /**
  1254. * testRouteSymmetry method
  1255. *
  1256. * @return void
  1257. */
  1258. public function testRouteSymmetry() {
  1259. Router::connect(
  1260. "/:extra/page/:slug/*",
  1261. array('controller' => 'pages', 'action' => 'view', 'extra' => null),
  1262. array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+', "action" => 'view')
  1263. );
  1264. $result = Router::parse('/some_extra/page/this_is_the_slug');
  1265. $expected = array(
  1266. 'pass' => [],
  1267. 'plugin' => null,
  1268. 'controller' => 'pages',
  1269. 'action' => 'view',
  1270. 'slug' => 'this_is_the_slug',
  1271. 'extra' => 'some_extra'
  1272. );
  1273. $this->assertEquals($expected, $result);
  1274. $result = Router::parse('/page/this_is_the_slug');
  1275. $expected = array(
  1276. 'pass' => [],
  1277. 'plugin' => null,
  1278. 'controller' => 'pages',
  1279. 'action' => 'view',
  1280. 'slug' => 'this_is_the_slug',
  1281. 'extra' => null
  1282. );
  1283. $this->assertEquals($expected, $result);
  1284. Router::reload();
  1285. Router::connect(
  1286. "/:extra/page/:slug/*",
  1287. array('controller' => 'pages', 'action' => 'view', 'extra' => null),
  1288. array("extra" => '[a-z1-9_]*', "slug" => '[a-z1-9_]+')
  1289. );
  1290. $result = Router::url(array(
  1291. 'admin' => null,
  1292. 'plugin' => null,
  1293. 'controller' => 'pages',
  1294. 'action' => 'view',
  1295. 'slug' => 'this_is_the_slug',
  1296. 'extra' => null
  1297. ));
  1298. $expected = '/page/this_is_the_slug';
  1299. $this->assertEquals($expected, $result);
  1300. $result = Router::url(array(
  1301. 'admin' => null,
  1302. 'plugin' => null,
  1303. 'controller' => 'pages',
  1304. 'action' => 'view',
  1305. 'slug' => 'this_is_the_slug',
  1306. 'extra' => 'some_extra'
  1307. ));
  1308. $expected = '/some_extra/page/this_is_the_slug';
  1309. $this->assertEquals($expected, $result);
  1310. }
  1311. /**
  1312. * Test exceptions when parsing fails.
  1313. *
  1314. * @expectedException Cake\Routing\Error\MissingRouteException
  1315. */
  1316. public function testParseError() {
  1317. Router::connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
  1318. Router::parse('/nope');
  1319. }
  1320. /**
  1321. * Test parse and reverse symmetry
  1322. *
  1323. * @return void
  1324. * @dataProvider parseReverseSymmetryData
  1325. */
  1326. public function testParseReverseSymmetry($url) {
  1327. $this->_connectDefaultRoutes();
  1328. $this->assertSame($url, Router::reverse(Router::parse($url) + array('url' => [])));
  1329. }
  1330. /**
  1331. * Data for parse and reverse test
  1332. *
  1333. * @return array
  1334. */
  1335. public function parseReverseSymmetryData() {
  1336. return array(
  1337. array('/controller/action'),
  1338. array('/controller/action/param'),
  1339. array('/controller/action?param1=value1&param2=value2'),
  1340. array('/controller/action/param?param1=value1'),
  1341. );
  1342. }
  1343. /**
  1344. * testParseExtensions method
  1345. *
  1346. * @return void
  1347. */
  1348. public function testParseExtensions() {
  1349. Router::extensions();
  1350. Router::parseExtensions('rss', false);
  1351. $this->assertContains('rss', Router::extensions());
  1352. }
  1353. /**
  1354. * testSetExtensions method
  1355. *
  1356. * @return void
  1357. */
  1358. public function testSetExtensions() {
  1359. Router::parseExtensions('rss', false);
  1360. $this->assertContains('rss', Router::extensions());
  1361. $this->_connectDefaultRoutes();
  1362. $result = Router::parse('/posts.rss');
  1363. $this->assertEquals('rss', $result['_ext']);
  1364. $result = Router::parse('/posts.xml');
  1365. $this->assertFalse(isset($result['_ext']));
  1366. Router::parseExtensions(array('xml'));
  1367. }
  1368. /**
  1369. * testExtensionParsing method
  1370. *
  1371. * @return void
  1372. */
  1373. public function testExtensionParsing() {
  1374. Router::parseExtensions('rss', false);
  1375. $this->_connectDefaultRoutes();
  1376. $result = Router::parse('/posts.rss');
  1377. $expected = array(
  1378. 'plugin' => null,
  1379. 'controller' => 'Posts',
  1380. 'action' => 'index',
  1381. '_ext' => 'rss',
  1382. 'pass' => []
  1383. );
  1384. $this->assertEquals($expected, $result);
  1385. $result = Router::parse('/posts/view/1.rss');
  1386. $expected = array(
  1387. 'plugin' => null,
  1388. 'controller' => 'Posts',
  1389. 'action' => 'view',
  1390. 'pass' => array('1'),
  1391. '_ext' => 'rss'
  1392. );
  1393. $this->assertEquals($expected, $result);
  1394. $result = Router::parse('/posts/view/1.rss?query=test');
  1395. $expected['?'] = array('query' => 'test');
  1396. $this->assertEquals($expected, $result);
  1397. Router::reload();
  1398. Router::parseExtensions(['rss', 'xml'], false);
  1399. $this->_connectDefaultRoutes();
  1400. $result = Router::parse('/posts.xml');
  1401. $expected = array(
  1402. 'plugin' => null,
  1403. 'controller' => 'Posts',
  1404. 'action' => 'index',
  1405. '_ext' => 'xml',
  1406. 'pass' => []
  1407. );
  1408. $this->assertEquals($expected, $result);
  1409. $result = Router::parse('/posts.atom?hello=goodbye');
  1410. $expected = array(
  1411. 'plugin' => null,
  1412. 'controller' => 'Posts.atom',
  1413. 'action' => 'index',
  1414. 'pass' => [],
  1415. '?' => array('hello' => 'goodbye')
  1416. );
  1417. $this->assertEquals($expected, $result);
  1418. Router::reload();
  1419. Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
  1420. $result = Router::parse('/controller/action');
  1421. $expected = array('controller' => 'controller', 'action' => 'action', 'plugin' => null, '_ext' => 'rss', 'pass' => []);
  1422. $this->assertEquals($expected, $result);
  1423. Router::reload();
  1424. Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
  1425. $result = Router::parse('/controller/action');
  1426. $expected = array(
  1427. 'controller' => 'controller',
  1428. 'action' => 'action',
  1429. 'plugin' => null,
  1430. '_ext' => 'rss',
  1431. 'pass' => []
  1432. );
  1433. $this->assertEquals($expected, $result);
  1434. Router::reload();
  1435. Router::parseExtensions('rss', false);
  1436. Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
  1437. $result = Router::parse('/controller/action');
  1438. $expected = array(
  1439. 'controller' => 'controller',
  1440. 'action' => 'action',
  1441. 'plugin' => null,
  1442. '_ext' => 'rss',
  1443. 'pass' => []
  1444. );
  1445. $this->assertEquals($expected, $result);
  1446. }
  1447. /**
  1448. * Test newer style automatically generated prefix routes.
  1449. *
  1450. * @return void
  1451. * @see testUrlGenerationWithAutoPrefixes
  1452. */
  1453. public function testUrlGenerationWithAutoPrefixes() {
  1454. Router::reload();
  1455. Router::connect('/protected/:controller/:action/*', array('prefix' => 'protected'));
  1456. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  1457. Router::connect('/:controller/:action/*');
  1458. $request = new Request();
  1459. Router::setRequestInfo(
  1460. $request->addParams(array(
  1461. 'plugin' => null, 'controller' => 'images', 'action' => 'index',
  1462. 'prefix' => null, 'protected' => false, 'url' => array('url' => 'images/index')
  1463. ))->addPaths(array(
  1464. 'base' => '',
  1465. 'here' => '/images/index',
  1466. 'webroot' => '/',
  1467. ))
  1468. );
  1469. $result = Router::url(array('controller' => 'images', 'action' => 'add'));
  1470. $expected = '/images/add';
  1471. $this->assertEquals($expected, $result);
  1472. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => 'protected'));
  1473. $expected = '/protected/images/add';
  1474. $this->assertEquals($expected, $result);
  1475. $result = Router::url(array('controller' => 'images', 'action' => 'add_protected_test', 'prefix' => 'protected'));
  1476. $expected = '/protected/images/add_protected_test';
  1477. $this->assertEquals($expected, $result);
  1478. $result = Router::url(array('action' => 'edit', 1));
  1479. $expected = '/images/edit/1';
  1480. $this->assertEquals($expected, $result);
  1481. $result = Router::url(array('action' => 'edit', 1, 'prefix' => 'protected'));
  1482. $expected = '/protected/images/edit/1';
  1483. $this->assertEquals($expected, $result);
  1484. $result = Router::url(array('action' => 'protectededit', 1, 'prefix' => 'protected'));
  1485. $expected = '/protected/images/protectededit/1';
  1486. $this->assertEquals($expected, $result);
  1487. $result = Router::url(array('action' => 'edit', 1, 'prefix' => 'protected'));
  1488. $expected = '/protected/images/edit/1';
  1489. $this->assertEquals($expected, $result);
  1490. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1));
  1491. $expected = '/others/edit/1';
  1492. $this->assertEquals($expected, $result);
  1493. $result = Router::url(array('controller' => 'others', 'action' => 'edit', 1, 'prefix' => 'protected'));
  1494. $expected = '/protected/others/edit/1';
  1495. $this->assertEquals($expected, $result);
  1496. }
  1497. /**
  1498. * Test that the ssl option works.
  1499. *
  1500. * @return void
  1501. */
  1502. public function testGenerationWithSslOption() {
  1503. Router::connect('/:controller/:action/*');
  1504. $request = new Request();
  1505. $request->env('HTTP_HOST', 'localhost');
  1506. Router::pushRequest(
  1507. $request->addParams(array(
  1508. 'plugin' => null, 'controller' => 'images', 'action' => 'index'
  1509. ))->addPaths(array(
  1510. 'base' => '',
  1511. 'here' => '/images/index',
  1512. 'webroot' => '/',
  1513. ))
  1514. );
  1515. $result = Router::url(array(
  1516. '_ssl' => true
  1517. ));
  1518. $this->assertEquals('https://localhost/images/index', $result);
  1519. $result = Router::url(array(
  1520. '_ssl' => false
  1521. ));
  1522. $this->assertEquals('http://localhost/images/index', $result);
  1523. }
  1524. /**
  1525. * Test ssl option when the current request is ssl.
  1526. *
  1527. * @return void
  1528. */
  1529. public function testGenerateWithSslInSsl() {
  1530. Router::connect('/:controller/:action/*');
  1531. $request = new Request();
  1532. $request->env('HTTP_HOST', 'localhost');
  1533. $request->env('HTTPS', 'on');
  1534. Router::pushRequest(
  1535. $request->addParams(array(
  1536. 'plugin' => null,
  1537. 'controller' => 'images',
  1538. 'action' => 'index'
  1539. ))->addPaths(array(
  1540. 'base' => '',
  1541. 'here' => '/images/index',
  1542. 'webroot' => '/',
  1543. ))
  1544. );
  1545. $result = Router::url(array(
  1546. '_ssl' => false
  1547. ));
  1548. $this->assertEquals('http://localhost/images/index', $result);
  1549. $result = Router::url(array(
  1550. '_ssl' => true
  1551. ));
  1552. $this->assertEquals('https://localhost/images/index', $result);
  1553. }
  1554. /**
  1555. * test that prefix routes persist when they are in the current request.
  1556. *
  1557. * @return void
  1558. */
  1559. public function testPrefixRoutePersistence() {
  1560. Router::reload();
  1561. Router::connect('/protected/:controller/:action', array('prefix' => 'protected'));
  1562. Router::connect('/:controller/:action');
  1563. $request = new Request();
  1564. Router::setRequestInfo(
  1565. $request->addParams(array(
  1566. 'plugin' => null,
  1567. 'controller' => 'images',
  1568. 'action' => 'index',
  1569. 'prefix' => 'protected',
  1570. ))->addPaths(array(
  1571. 'base' => '',
  1572. 'here' => '/protected/images/index',
  1573. 'webroot' => '/',
  1574. ))
  1575. );
  1576. $result = Router::url(array('prefix' => 'protected', 'controller' => 'images', 'action' => 'add'));
  1577. $expected = '/protected/images/add';
  1578. $this->assertEquals($expected, $result);
  1579. $result = Router::url(array('controller' => 'images', 'action' => 'add'));
  1580. $expected = '/protected/images/add';
  1581. $this->assertEquals($expected, $result);
  1582. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => false));
  1583. $expected = '/images/add';
  1584. $this->assertEquals($expected, $result);
  1585. }
  1586. /**
  1587. * test that setting a prefix override the current one
  1588. *
  1589. * @return void
  1590. */
  1591. public function testPrefixOverride() {
  1592. Router::connect('/admin/:controller/:action', array('prefix' => 'admin'));
  1593. Router::connect('/protected/:controller/:action', array('prefix' => 'protected'));
  1594. $request = new Request();
  1595. Router::setRequestInfo(
  1596. $request->addParams(array(
  1597. 'plugin' => null, 'controller' => 'images', 'action' => 'index', 'prefix' => 'protected',
  1598. ))->addPaths(array(
  1599. 'base' => '',
  1600. 'here' => '/protected/images/index',
  1601. 'webroot' => '/',
  1602. ))
  1603. );
  1604. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => 'admin'));
  1605. $expected = '/admin/images/add';
  1606. $this->assertEquals($expected, $result);
  1607. $request = new Request();
  1608. Router::setRequestInfo(
  1609. $request->addParams(array(
  1610. 'plugin' => null,
  1611. 'controller' => 'images',
  1612. 'action' => 'index',
  1613. 'prefix' => 'admin',
  1614. ))->addPaths(array(
  1615. 'base' => '',
  1616. 'here' => '/admin/images/index',
  1617. 'webroot' => '/',
  1618. ))
  1619. );
  1620. $result = Router::url(array('controller' => 'images', 'action' => 'add', 'prefix' => 'protected'));
  1621. $expected = '/protected/images/add';
  1622. $this->assertEquals($expected, $result);
  1623. }
  1624. /**
  1625. * Test that well known route parameters are passed through.
  1626. *
  1627. * @return void
  1628. */
  1629. public function testRouteParamDefaults() {
  1630. Router::connect('/cache/*', array('prefix' => false, 'plugin' => true, 'controller' => 0, 'action' => 1));
  1631. $url = Router::url(array('prefix' => 0, 'plugin' => 1, 'controller' => 0, 'action' => 1, 'test'));
  1632. $expected = '/cache/test';
  1633. $this->assertEquals($expected, $url);
  1634. try {
  1635. Router::url(array('controller' => 0, 'action' => 1, 'test'));
  1636. $this->fail('No exception raised');
  1637. } catch (\Exception $e) {
  1638. $this->assertTrue(true, 'Exception was raised');
  1639. }
  1640. try {
  1641. Router::url(array('prefix' => 1, 'controller' => 0, 'action' => 1, 'test'));
  1642. $this->fail('No exception raised');
  1643. } catch (\Exception $e) {
  1644. $this->assertTrue(true, 'Exception was raised');
  1645. }
  1646. }
  1647. /**
  1648. * testRemoveBase method
  1649. *
  1650. * @return void
  1651. */
  1652. public function testRemoveBase() {
  1653. Router::connect('/:controller/:action');
  1654. $request = new Request();
  1655. Router::setRequestInfo(
  1656. $request->addParams(array(
  1657. 'plugin' => null, 'controller' => 'controller', 'action' => 'index',
  1658. ))->addPaths(array(
  1659. 'base' => '/base',
  1660. 'here' => '/',
  1661. 'webroot' => '/base/',
  1662. ))
  1663. );
  1664. $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action'));
  1665. $expected = '/base/my_controller/my_action';
  1666. $this->assertEquals($expected, $result);
  1667. $result = Router::url(array('controller' => 'my_controller', 'action' => 'my_action', '_base' => false));
  1668. $expected = '/my_controller/my_action';
  1669. $this->assertEquals($expected, $result);
  1670. }
  1671. /**
  1672. * testPagesUrlParsing method
  1673. *
  1674. * @return void
  1675. */
  1676. public function testPagesUrlParsing() {
  1677. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  1678. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  1679. $result = Router::parse('/');
  1680. $expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1681. $this->assertEquals($expected, $result);
  1682. $result = Router::parse('/pages/home/');
  1683. $expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1684. $this->assertEquals($expected, $result);
  1685. Router::reload();
  1686. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  1687. $result = Router::parse('/');
  1688. $expected = array('pass' => array('home'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1689. $this->assertEquals($expected, $result);
  1690. Router::reload();
  1691. Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
  1692. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  1693. $result = Router::parse('/pages/contact/');
  1694. $expected = array('pass' => array('contact'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display');
  1695. $this->assertEquals($expected, $result);
  1696. }
  1697. /**
  1698. * test that requests with a trailing dot don't loose the do.
  1699. *
  1700. * @return void
  1701. */
  1702. public function testParsingWithTrailingPeriod() {
  1703. Router::reload();
  1704. Router::connect('/:controller/:action/*');
  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 requests with a trailing dot don't loose the do.
  1712. *
  1713. * @return void
  1714. */
  1715. public function testParsingWithTrailingPeriodAndParseExtensions() {
  1716. Router::reload();
  1717. Router::connect('/:controller/:action/*');
  1718. Router::parseExtensions('json', false);
  1719. $result = Router::parse('/posts/view/something.');
  1720. $this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
  1721. $result = Router::parse('/posts/view/something. . .');
  1722. $this->assertEquals('something. . .', $result['pass'][0], 'Period was chopped off %s');
  1723. }
  1724. /**
  1725. * test that patterns work for :action
  1726. *
  1727. * @expectedException Cake\Routing\Error\MissingRouteException
  1728. * @return void
  1729. */
  1730. public function testParsingWithPatternOnAction() {
  1731. Router::connect(
  1732. '/blog/:action/*',
  1733. array('controller' => 'blog_posts'),
  1734. array('action' => 'other|actions')
  1735. );
  1736. $result = Router::parse('/blog/other');
  1737. $expected = array(
  1738. 'plugin' => null,
  1739. 'controller' => 'blog_posts',
  1740. 'action' => 'other',
  1741. 'pass' => [],
  1742. );
  1743. $this->assertEquals($expected, $result);
  1744. Router::parse('/blog/foobar');
  1745. }
  1746. /**
  1747. * Test url() works with patterns on :action
  1748. *
  1749. * @expectedException Cake\Routing\Error\MissingRouteException
  1750. * @return void
  1751. */
  1752. public function testUrlPatternOnAction() {
  1753. Router::connect(
  1754. '/blog/:action/*',
  1755. array('controller' => 'blog_posts'),
  1756. array('action' => 'other|actions')
  1757. );
  1758. $result = Router::url(array('controller' => 'blog_posts', 'action' => 'actions'));
  1759. $this->assertEquals('/blog/actions', $result);
  1760. $result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo'));
  1761. $this->assertEquals('/', $result);
  1762. }
  1763. /**
  1764. * testParsingWithLiteralPrefixes method
  1765. *
  1766. * @return void
  1767. */
  1768. public function testParsingWithLiteralPrefixes() {
  1769. Router::reload();
  1770. $adminParams = array('prefix' => 'admin');
  1771. Router::connect('/admin/:controller', $adminParams);
  1772. Router::connect('/admin/:controller/:action/*', $adminParams);
  1773. $request = new Request();
  1774. Router::setRequestInfo(
  1775. $request->addParams(array(
  1776. 'plugin' => null, 'controller' => 'controller', 'action' => 'index'
  1777. ))->addPaths(array(
  1778. 'base' => '/base',
  1779. 'here' => '/',
  1780. 'webroot' => '/base/',
  1781. ))
  1782. );
  1783. $result = Router::parse('/admin/posts/');
  1784. $expected = array('pass' => [], 'prefix' => 'admin', 'plugin' => null, 'controller' => 'posts', 'action' => 'index');
  1785. $this->assertEquals($expected, $result);
  1786. $result = Router::parse('/admin/posts');
  1787. $this->assertEquals($expected, $result);
  1788. $result = Router::url(array('prefix' => 'admin', 'controller' => 'posts'));
  1789. $expected = '/base/admin/posts';
  1790. $this->assertEquals($expected, $result);
  1791. Router::reload();
  1792. $prefixParams = array('prefix' => 'members');
  1793. Router::connect('/members/:controller', $prefixParams);
  1794. Router::connect('/members/:controller/:action', $prefixParams);
  1795. Router::connect('/members/:controller/:action/*', $prefixParams);
  1796. $request = new Request();
  1797. Router::setRequestInfo(
  1798. $request->addParams(array(
  1799. 'plugin' => null, 'controller' => 'controller', 'action' => 'index',
  1800. ))->addPaths(array(
  1801. 'base' => '/base',
  1802. 'here' => '/',
  1803. 'webroot' => '/',
  1804. ))
  1805. );
  1806. $result = Router::parse('/members/posts/index');
  1807. $expected = array('pass' => [], 'prefix' => 'members', 'plugin' => null, 'controller' => 'posts', 'action' => 'index');
  1808. $this->assertEquals($expected, $result);
  1809. $result = Router::url(array('prefix' => 'members', 'controller' => 'users', 'action' => 'add'));
  1810. $expected = '/base/members/users/add';
  1811. $this->assertEquals($expected, $result);
  1812. }
  1813. /**
  1814. * Tests URL generation with flags and prefixes in and out of context
  1815. *
  1816. * @return void
  1817. */
  1818. public function testUrlWritingWithPrefixes() {
  1819. Router::connect('/company/:controller/:action/*', array('prefix' => 'company'));
  1820. Router::connect('/:action', array('controller' => 'users'));
  1821. $result = Router::url(array('controller' => 'users', 'action' => 'login', 'prefix' => 'company'));
  1822. $expected = '/company/users/login';
  1823. $this->assertEquals($expected, $result);
  1824. $request = new Request();
  1825. Router::setRequestInfo(
  1826. $request->addParams(array(
  1827. 'plugin' => null,
  1828. 'controller' => 'users',
  1829. 'action' => 'login',
  1830. 'prefix' => 'company'
  1831. ))->addPaths(array(
  1832. 'base' => '/',
  1833. 'here' => '/',
  1834. 'webroot' => '/base/',
  1835. ))
  1836. );
  1837. $result = Router::url(array('controller' => 'users', 'action' => 'login', 'prefix' => false));
  1838. $expected = '/login';
  1839. $this->assertEquals($expected, $result);
  1840. }
  1841. /**
  1842. * test url generation with prefixes and custom routes
  1843. *
  1844. * @return void
  1845. */
  1846. public function testUrlWritingWithPrefixesAndCustomRoutes() {
  1847. Router::connect(
  1848. '/admin/login',
  1849. array('controller' => 'users', 'action' => 'login', 'prefix' => 'admin')
  1850. );
  1851. $request = new Request();
  1852. Router::setRequestInfo(
  1853. $request->addParams(array(
  1854. 'plugin' => null, 'controller' => 'posts', 'action' => 'index',
  1855. 'prefix' => 'admin'
  1856. ))->addPaths(array(
  1857. 'base' => '/',
  1858. 'here' => '/',
  1859. 'webroot' => '/',
  1860. ))
  1861. );
  1862. $result = Router::url(array('controller' => 'users', 'action' => 'login'));
  1863. $this->assertEquals('/admin/login', $result);
  1864. $result = Router::url(array('controller' => 'users', 'action' => 'login'));
  1865. $this->assertEquals('/admin/login', $result);
  1866. }
  1867. /**
  1868. * testPassedArgsOrder method
  1869. *
  1870. * @return void
  1871. */
  1872. public function testPassedArgsOrder() {
  1873. Router::connect('/test-passed/*', array('controller' => 'pages', 'action' => 'display', 'home'));
  1874. Router::connect('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
  1875. Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 1));
  1876. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 1, 'whatever'));
  1877. $expected = '/test/whatever';
  1878. $this->assertEquals($expected, $result);
  1879. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 2, 'whatever'));
  1880. $expected = '/test2/whatever';
  1881. $this->assertEquals($expected, $result);
  1882. $result = Router::url(array('controller' => 'pages', 'action' => 'display', 'home', 'whatever'));
  1883. $expected = '/test-passed/whatever';
  1884. $this->assertEquals($expected, $result);
  1885. }
  1886. /**
  1887. * testRegexRouteMatching method
  1888. *
  1889. * @return void
  1890. */
  1891. public function testRegexRouteMatching() {
  1892. Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
  1893. $result = Router::parse('/eng/test/test_action');
  1894. $expected = array('pass' => [], 'locale' => 'eng', 'controller' => 'test', 'action' => 'test_action', 'plugin' => null);
  1895. $this->assertEquals($expected, $result);
  1896. }
  1897. /**
  1898. * testRegexRouteMatching error
  1899. *
  1900. * @expectedException Cake\Routing\Error\MissingRouteException
  1901. * @return void
  1902. */
  1903. public function testRegexRouteMatchingError() {
  1904. Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
  1905. Router::parse('/badness/test/test_action');
  1906. }
  1907. /**
  1908. * testRegexRouteMatching method
  1909. *
  1910. * @expectedException Cake\Routing\Error\MissingRouteException
  1911. * @return void
  1912. */
  1913. public function testRegexRouteMatchUrl() {
  1914. Router::connect('/:locale/:controller/:action/*', [], array('locale' => 'dan|eng'));
  1915. $request = new Request();
  1916. Router::setRequestInfo(
  1917. $request->addParams(array(
  1918. 'plugin' => null,
  1919. 'controller' => 'test',
  1920. 'action' => 'index',
  1921. 'url' => array('url' => 'test/test_action')
  1922. ))->addPaths(array(
  1923. 'base' => '',
  1924. 'here' => '/test/test_action',
  1925. 'webroot' => '/',
  1926. ))
  1927. );
  1928. $result = Router::url(array('action' => 'test_another_action', 'locale' => 'eng'));
  1929. $expected = '/eng/test/test_another_action';
  1930. $this->assertEquals($expected, $result);
  1931. $result = Router::url(array('action' => 'test_another_action'));
  1932. $expected = '/';
  1933. $this->assertEquals($expected, $result);
  1934. }
  1935. /**
  1936. * test using a custom route class for route connection
  1937. *
  1938. * @return void
  1939. */
  1940. public function testUsingCustomRouteClass() {
  1941. Plugin::load('TestPlugin');
  1942. Router::connect(
  1943. '/:slug',
  1944. array('plugin' => 'TestPlugin', 'action' => 'index'),
  1945. array('routeClass' => 'PluginShortRoute', 'slug' => '[a-z_-]+')
  1946. );
  1947. $result = Router::parse('/the-best');
  1948. $expected = [
  1949. 'plugin' => 'TestPlugin',
  1950. 'controller' => 'TestPlugin',
  1951. 'action' => 'index',
  1952. 'slug' => 'the-best',
  1953. 'pass' => [],
  1954. ];
  1955. $this->assertEquals($expected, $result);
  1956. }
  1957. /**
  1958. * test using custom route class in PluginDot notation
  1959. *
  1960. * @return void
  1961. */
  1962. public function testUsingCustomRouteClassPluginDotSyntax() {
  1963. Plugin::load('TestPlugin');
  1964. Router::connect(
  1965. '/:slug',
  1966. array('controller' => 'posts', 'action' => 'view'),
  1967. array('routeClass' => 'TestPlugin.TestRoute', 'slug' => '[a-z_-]+')
  1968. );
  1969. $this->assertTrue(true); // Just to make sure the connect do not throw exception
  1970. Plugin::unload('TestPlugin');
  1971. }
  1972. /**
  1973. * test that route classes must extend \Cake\Routing\Route\Route
  1974. *
  1975. * @expectedException \Cake\Error\Exception
  1976. * @return void
  1977. */
  1978. public function testCustomRouteException() {
  1979. Router::connect('/:controller', [], array('routeClass' => 'Object'));
  1980. }
  1981. /**
  1982. * test reversing parameter arrays back into strings.
  1983. *
  1984. * Mark the router as initialized so it doesn't auto-load routes
  1985. *
  1986. * @return void
  1987. */
  1988. public function testReverse() {
  1989. Router::connect('/:controller/:action/*');
  1990. $params = array(
  1991. 'controller' => 'posts',
  1992. 'action' => 'view',
  1993. 'pass' => array(1),
  1994. 'url' => [],
  1995. 'autoRender' => 1,
  1996. 'bare' => 1,
  1997. 'return' => 1,
  1998. 'requested' => 1,
  1999. '_Token' => array('key' => 'sekret')
  2000. );
  2001. $result = Router::reverse($params);
  2002. $this->assertEquals('/posts/view/1', $result);
  2003. Router::reload();
  2004. Router::connect('/:lang/:controller/:action/*', [], array('lang' => '[a-z]{3}'));
  2005. $params = array(
  2006. 'lang' => 'eng',
  2007. 'controller' => 'posts',
  2008. 'action' => 'view',
  2009. 'pass' => array(1),
  2010. 'url' => array('url' => 'eng/posts/view/1')
  2011. );
  2012. $result = Router::reverse($params);
  2013. $this->assertEquals('/eng/posts/view/1', $result);
  2014. $params = array(
  2015. 'lang' => 'eng',
  2016. 'controller' => 'posts',
  2017. 'action' => 'view',
  2018. 1,
  2019. '?' => ['foo' => 'bar']
  2020. );
  2021. $result = Router::reverse($params);
  2022. $this->assertEquals('/eng/posts/view/1?foo=bar', $result);
  2023. $params = array(
  2024. 'lang' => 'eng',
  2025. 'controller' => 'posts',
  2026. 'action' => 'view',
  2027. 'pass' => array(1),
  2028. 'url' => array('url' => 'eng/posts/view/1', 'foo' => 'bar', 'baz' => 'quu'),
  2029. 'paging' => [],
  2030. 'models' => []
  2031. );
  2032. $result = Router::reverse($params);
  2033. $this->assertEquals('/eng/posts/view/1?foo=bar&baz=quu', $result);
  2034. $request = new Request('/eng/posts/view/1');
  2035. $request->addParams(array(
  2036. 'lang' => 'eng',
  2037. 'controller' => 'posts',
  2038. 'action' => 'view',
  2039. 'pass' => array(1),
  2040. ));
  2041. $request->query = array('url' => 'eng/posts/view/1', 'test' => 'value');
  2042. $result = Router::reverse($request);
  2043. $expected = '/eng/posts/view/1?test=value';
  2044. $this->assertEquals($expected, $result);
  2045. $params = array(
  2046. 'lang' => 'eng',
  2047. 'controller' => 'posts',
  2048. 'action' => 'view',
  2049. 'pass' => array(1),
  2050. 'url' => array('url' => 'eng/posts/view/1')
  2051. );
  2052. $result = Router::reverse($params, true);
  2053. $this->assertRegExp('/^http(s)?:\/\//', $result);
  2054. }
  2055. /**
  2056. * Test that extensions work with Router::reverse()
  2057. *
  2058. * @return void
  2059. */
  2060. public function testReverseWithExtension() {
  2061. Router::connect('/:controller/:action/*');
  2062. Router::parseExtensions('json', false);
  2063. $request = new Request('/posts/view/1.json');
  2064. $request->addParams(array(
  2065. 'controller' => 'posts',
  2066. 'action' => 'view',
  2067. 'pass' => array(1),
  2068. '_ext' => 'json',
  2069. ));
  2070. $request->query = [];
  2071. $result = Router::reverse($request);
  2072. $expected = '/posts/view/1.json';
  2073. $this->assertEquals($expected, $result);
  2074. }
  2075. /**
  2076. * test that setRequestInfo can accept arrays and turn that into a Request object.
  2077. *
  2078. * @return void
  2079. */
  2080. public function testSetRequestInfoLegacy() {
  2081. Router::setRequestInfo(array(
  2082. array(
  2083. 'plugin' => null, 'controller' => 'images', 'action' => 'index',
  2084. 'url' => array('url' => 'protected/images/index')
  2085. ),
  2086. array(
  2087. 'base' => '',
  2088. 'here' => '/protected/images/index',
  2089. 'webroot' => '/',
  2090. )
  2091. ));
  2092. $result = Router::getRequest();
  2093. $this->assertEquals('images', $result->controller);
  2094. $this->assertEquals('index', $result->action);
  2095. $this->assertEquals('', $result->base);
  2096. $this->assertEquals('/protected/images/index', $result->here);
  2097. $this->assertEquals('/', $result->webroot);
  2098. }
  2099. /**
  2100. * test get request.
  2101. *
  2102. * @return void
  2103. */
  2104. public function testGetRequest() {
  2105. $requestA = new Request('/');
  2106. $requestB = new Request('/posts');
  2107. Router::pushRequest($requestA);
  2108. Router::pushRequest($requestB);
  2109. $this->assertSame($requestA, Router::getRequest(false));
  2110. $this->assertSame($requestB, Router::getRequest(true));
  2111. }
  2112. /**
  2113. * Test that Router::url() uses the first request
  2114. */
  2115. public function testUrlWithRequestAction() {
  2116. Router::connect('/:controller', array('action' => 'index'));
  2117. Router::connect('/:controller/:action');
  2118. $firstRequest = new Request('/posts/index');
  2119. $firstRequest->addParams(array(
  2120. 'plugin' => null,
  2121. 'controller' => 'posts',
  2122. 'action' => 'index'
  2123. ))->addPaths(array('base' => ''));
  2124. $secondRequest = new Request('/posts/index');
  2125. $secondRequest->addParams(array(
  2126. 'requested' => 1,
  2127. 'plugin' => null,
  2128. 'controller' => 'comments',
  2129. 'action' => 'listing'
  2130. ))->addPaths(array('base' => ''));
  2131. Router::setRequestInfo($firstRequest);
  2132. Router::setRequestInfo($secondRequest);
  2133. $result = Router::url(array('_base' => false));
  2134. $this->assertEquals('/comments/listing', $result, 'with second requests, the last should win.');
  2135. Router::popRequest();
  2136. $result = Router::url(array('_base' => false));
  2137. $this->assertEquals('/posts', $result, 'with second requests, the last should win.');
  2138. // Make sure that popping an empty request doesn't fail.
  2139. Router::popRequest();
  2140. }
  2141. /**
  2142. * test that a route object returning a full URL is not modified.
  2143. *
  2144. * @return void
  2145. */
  2146. public function testUrlFullUrlReturnFromRoute() {
  2147. $url = 'http://example.com/posts/view/1';
  2148. $route = $this->getMock(
  2149. 'Cake\Routing\Route\Route',
  2150. ['match'],
  2151. ['/:controller/:action/*']
  2152. );
  2153. $route->expects($this->any())
  2154. ->method('match')
  2155. ->will($this->returnValue($url));
  2156. Router::connect($route);
  2157. $result = Router::url(array('controller' => 'posts', 'action' => 'view', 1));
  2158. $this->assertEquals($url, $result);
  2159. }
  2160. /**
  2161. * test protocol in url
  2162. *
  2163. * @return void
  2164. */
  2165. public function testUrlProtocol() {
  2166. $url = 'http://example.com';
  2167. $this->assertEquals($url, Router::url($url));
  2168. $url = 'ed2k://example.com';
  2169. $this->assertEquals($url, Router::url($url));
  2170. $url = 'svn+ssh://example.com';
  2171. $this->assertEquals($url, Router::url($url));
  2172. $url = '://example.com';
  2173. $this->assertEquals($url, Router::url($url));
  2174. $url = '//example.com';
  2175. $this->assertEquals($url, Router::url($url));
  2176. $url = 'javascript:void(0)';
  2177. $this->assertEquals($url, Router::url($url));
  2178. $url = 'tel:012345-678';
  2179. $this->assertEquals($url, Router::url($url));
  2180. $url = 'sms:012345-678';
  2181. $this->assertEquals($url, Router::url($url));
  2182. $url = '#here';
  2183. $this->assertEquals($url, Router::url($url));
  2184. $url = '?param=0';
  2185. $this->assertEquals($url, Router::url($url));
  2186. $url = '/posts/index#here';
  2187. $expected = Configure::read('App.fullBaseUrl') . '/posts/index#here';
  2188. $this->assertEquals($expected, Router::url($url, true));
  2189. }
  2190. /**
  2191. * Testing that patterns on the :action param work properly.
  2192. *
  2193. * @return void
  2194. */
  2195. public function testPatternOnAction() {
  2196. $route = new Route(
  2197. '/blog/:action/*',
  2198. array('controller' => 'blog_posts'),
  2199. array('action' => 'other|actions')
  2200. );
  2201. $result = $route->match(array('controller' => 'blog_posts', 'action' => 'foo'));
  2202. $this->assertFalse($result);
  2203. $result = $route->match(array('controller' => 'blog_posts', 'action' => 'actions'));
  2204. $this->assertEquals('/blog/actions/', $result);
  2205. $result = $route->parse('/blog/other');
  2206. $expected = array('controller' => 'blog_posts', 'action' => 'other', 'pass' => []);
  2207. $this->assertEquals($expected, $result);
  2208. $result = $route->parse('/blog/foobar');
  2209. $this->assertFalse($result);
  2210. }
  2211. /**
  2212. * Test that redirect() works.
  2213. *
  2214. * @return void
  2215. */
  2216. public function testRedirect() {
  2217. Router::redirect('/mobile', '/', ['status' => 301]);
  2218. $routes = Router::routes();
  2219. $route = $routes[0];
  2220. $this->assertInstanceOf('Cake\Routing\Route\RedirectRoute', $route);
  2221. }
  2222. /**
  2223. * Test that the compatibility method for incoming urls works.
  2224. *
  2225. * @return void
  2226. */
  2227. public function testParseNamedParameters() {
  2228. $request = new Request();
  2229. $request->addParams(array(
  2230. 'controller' => 'posts',
  2231. 'action' => 'index',
  2232. ));
  2233. $result = Router::parseNamedParams($request);
  2234. $this->assertSame([], $result->params['named']);
  2235. $request = new Request();
  2236. $request->addParams(array(
  2237. 'controller' => 'posts',
  2238. 'action' => 'index',
  2239. 'pass' => array('home', 'one:two', 'three:four', 'five[nested][0]:six', 'five[nested][1]:seven')
  2240. ));
  2241. Router::parseNamedParams($request);
  2242. $expected = array(
  2243. 'plugin' => null,
  2244. 'controller' => 'posts',
  2245. 'action' => 'index',
  2246. '_ext' => null,
  2247. 'pass' => array('home'),
  2248. 'named' => array(
  2249. 'one' => 'two',
  2250. 'three' => 'four',
  2251. 'five' => array(
  2252. 'nested' => array('six', 'seven')
  2253. )
  2254. )
  2255. );
  2256. $this->assertEquals($expected, $request->params);
  2257. }
  2258. /**
  2259. * Test the scope() method
  2260. *
  2261. * @return void
  2262. */
  2263. public function testScope() {
  2264. Router::scope('/path', ['param' => 'value'], function($routes) {
  2265. $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
  2266. $this->assertEquals('/path', $routes->path());
  2267. $this->assertEquals(['param' => 'value'], $routes->params());
  2268. $routes->connect('/articles', ['controller' => 'Articles']);
  2269. });
  2270. }
  2271. /**
  2272. * Test the scope() method
  2273. *
  2274. * @expectedException \InvalidArgumentException
  2275. * @return void
  2276. */
  2277. public function testScopeError() {
  2278. Router::scope('/path', 'derpy');
  2279. }
  2280. /**
  2281. * Test that prefix() creates a scope.
  2282. *
  2283. * @return void
  2284. */
  2285. public function testPrefix() {
  2286. Router::prefix('admin', function($routes) {
  2287. $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
  2288. $this->assertEquals('/admin', $routes->path());
  2289. $this->assertEquals(['prefix' => 'admin'], $routes->params());
  2290. });
  2291. }
  2292. /**
  2293. * Test that plugin() creates a scope.
  2294. *
  2295. * @return void
  2296. */
  2297. public function testPlugin() {
  2298. Router::plugin('DebugKit', function($routes) {
  2299. $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
  2300. $this->assertEquals('/debug_kit', $routes->path());
  2301. $this->assertEquals(['plugin' => 'DebugKit'], $routes->params());
  2302. });
  2303. }
  2304. /**
  2305. * Test that plugin() accepts options
  2306. *
  2307. * @return void
  2308. */
  2309. public function testPluginOptions() {
  2310. Router::plugin('DebugKit', ['path' => '/debugger'], function($routes) {
  2311. $this->assertInstanceOf('Cake\Routing\RouteBuilder', $routes);
  2312. $this->assertEquals('/debugger', $routes->path());
  2313. $this->assertEquals(['plugin' => 'DebugKit'], $routes->params());
  2314. });
  2315. }
  2316. /**
  2317. * Connect some fallback routes for testing router behavior.
  2318. *
  2319. * @return void
  2320. */
  2321. protected function _connectDefaultRoutes() {
  2322. Router::scope('/', function($routes) {
  2323. $routes->fallbacks();
  2324. });
  2325. }
  2326. }