RouterTest.php 98 KB

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