PageRenderTime 70ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Routing/RouterTest.php

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