PageRenderTime 55ms CodeModel.GetById 18ms 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

Large files files are truncated, but you can click here to view the full 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' =

Large files files are truncated, but you can click here to view the full file