RouterTest.php 77 KB

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