RouteCollectionTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing;
  16. use Cake\Routing\RouteBuilder;
  17. use Cake\Routing\RouteCollection;
  18. use Cake\Routing\Route\Route;
  19. use Cake\TestSuite\TestCase;
  20. class RouteCollectionTest extends TestCase
  21. {
  22. /**
  23. * Setup method
  24. *
  25. * @return void
  26. */
  27. public function setUp()
  28. {
  29. parent::setUp();
  30. $this->collection = new RouteCollection();
  31. }
  32. /**
  33. * Test parse() throws an error on unknown routes.
  34. *
  35. * @expectedException \Cake\Routing\Exception\MissingRouteException
  36. * @expectedExceptionMessage A route matching "/" could not be found
  37. */
  38. public function testParseMissingRoute()
  39. {
  40. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  41. $routes->connect('/', ['controller' => 'Articles']);
  42. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  43. $result = $this->collection->parse('/');
  44. $this->assertEquals([], $result, 'Should not match, missing /b');
  45. }
  46. /**
  47. * Test parsing routes.
  48. *
  49. * @return void
  50. */
  51. public function testParse()
  52. {
  53. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  54. $routes->connect('/', ['controller' => 'Articles']);
  55. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  56. $routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search']);
  57. $result = $this->collection->parse('/b/');
  58. $expected = [
  59. 'controller' => 'Articles',
  60. 'action' => 'index',
  61. 'pass' => [],
  62. 'plugin' => null,
  63. 'key' => 'value',
  64. '_matchedRoute' => '/b',
  65. ];
  66. $this->assertEquals($expected, $result);
  67. $result = $this->collection->parse('/b/the-thing?one=two');
  68. $expected = [
  69. 'controller' => 'Articles',
  70. 'action' => 'view',
  71. 'id' => 'the-thing',
  72. 'pass' => [],
  73. 'plugin' => null,
  74. 'key' => 'value',
  75. '?' => ['one' => 'two'],
  76. '_matchedRoute' => '/b/:id',
  77. ];
  78. $this->assertEquals($expected, $result);
  79. $result = $this->collection->parse('/b/media/search');
  80. $expected = [
  81. 'key' => 'value',
  82. 'pass' => [],
  83. 'plugin' => null,
  84. 'controller' => 'Media',
  85. 'action' => 'search',
  86. '_matchedRoute' => '/b/media/search/*',
  87. ];
  88. $this->assertEquals($expected, $result);
  89. $result = $this->collection->parse('/b/media/search/thing');
  90. $expected = [
  91. 'key' => 'value',
  92. 'pass' => ['thing'],
  93. 'plugin' => null,
  94. 'controller' => 'Media',
  95. 'action' => 'search',
  96. '_matchedRoute' => '/b/media/search/*',
  97. ];
  98. $this->assertEquals($expected, $result);
  99. }
  100. /**
  101. * Test that parse decodes URL data before matching.
  102. * This is important for multibyte URLs that pass through URL rewriting.
  103. *
  104. * @return void
  105. */
  106. public function testParseEncodedBytesInFixedSegment()
  107. {
  108. $routes = new RouteBuilder($this->collection, '/');
  109. $routes->connect('/ден/:day-:month', ['controller' => 'Events', 'action' => 'index']);
  110. $url = '/%D0%B4%D0%B5%D0%BD/15-%D0%BE%D0%BA%D1%82%D0%BE%D0%BC%D0%B2%D1%80%D0%B8?test=foo';
  111. $result = $this->collection->parse($url);
  112. $expected = [
  113. 'pass' => [],
  114. 'plugin' => null,
  115. 'controller' => 'Events',
  116. 'action' => 'index',
  117. 'day' => '15',
  118. 'month' => 'октомври',
  119. '?' => ['test' => 'foo'],
  120. '_matchedRoute' => '/ден/:day-:month',
  121. ];
  122. $this->assertEquals($expected, $result);
  123. }
  124. /**
  125. * Test that parsing checks all the related path scopes.
  126. *
  127. * @return void
  128. */
  129. public function testParseFallback()
  130. {
  131. $routes = new RouteBuilder($this->collection, '/', []);
  132. $routes->resources('Articles');
  133. $routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'InflectedRoute']);
  134. $routes->connect('/:controller/:action', [], ['routeClass' => 'InflectedRoute']);
  135. $result = $this->collection->parse('/articles/add');
  136. $expected = [
  137. 'controller' => 'Articles',
  138. 'action' => 'add',
  139. 'plugin' => null,
  140. 'pass' => [],
  141. '_matchedRoute' => '/:controller/:action',
  142. ];
  143. $this->assertEquals($expected, $result);
  144. }
  145. /**
  146. * Test match() throws an error on unknown routes.
  147. *
  148. * @expectedException \Cake\Routing\Exception\MissingRouteException
  149. * @expectedExceptionMessage A route matching "array (
  150. */
  151. public function testMatchError()
  152. {
  153. $context = [
  154. '_base' => '/',
  155. '_scheme' => 'http',
  156. '_host' => 'example.org',
  157. ];
  158. $routes = new RouteBuilder($this->collection, '/b');
  159. $routes->connect('/', ['controller' => 'Articles']);
  160. $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
  161. }
  162. /**
  163. * Test matching routes.
  164. *
  165. * @return void
  166. */
  167. public function testMatch()
  168. {
  169. $context = [
  170. '_base' => '/',
  171. '_scheme' => 'http',
  172. '_host' => 'example.org',
  173. ];
  174. $routes = new RouteBuilder($this->collection, '/b');
  175. $routes->connect('/', ['controller' => 'Articles']);
  176. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  177. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
  178. $this->assertEquals('b', $result);
  179. $result = $this->collection->match(
  180. ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
  181. $context
  182. );
  183. $this->assertEquals('b/thing', $result);
  184. }
  185. /**
  186. * Test matching routes with names
  187. *
  188. * @return void
  189. */
  190. public function testMatchNamed()
  191. {
  192. $context = [
  193. '_base' => '/',
  194. '_scheme' => 'http',
  195. '_host' => 'example.org',
  196. ];
  197. $routes = new RouteBuilder($this->collection, '/b');
  198. $routes->connect('/', ['controller' => 'Articles']);
  199. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  200. $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
  201. $this->assertEquals('/b/2', $result);
  202. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'view', 'id' => '2'], $context);
  203. $this->assertEquals('b/2', $result);
  204. }
  205. /**
  206. * Test match() throws an error on named routes that fail to match
  207. *
  208. * @expectedException \Cake\Routing\Exception\MissingRouteException
  209. * @expectedExceptionMessage A named route was found for "fail", but matching failed
  210. */
  211. public function testMatchNamedError()
  212. {
  213. $context = [
  214. '_base' => '/',
  215. '_scheme' => 'http',
  216. '_host' => 'example.org',
  217. ];
  218. $routes = new RouteBuilder($this->collection, '/b');
  219. $routes->connect('/:lang/articles', ['controller' => 'Articles'], ['_name' => 'fail']);
  220. $this->collection->match(['_name' => 'fail'], $context);
  221. }
  222. /**
  223. * Test matching routes with names and failing
  224. *
  225. * @expectedException \Cake\Routing\Exception\MissingRouteException
  226. * @return void
  227. */
  228. public function testMatchNamedMissingError()
  229. {
  230. $context = [
  231. '_base' => '/',
  232. '_scheme' => 'http',
  233. '_host' => 'example.org',
  234. ];
  235. $routes = new RouteBuilder($this->collection, '/b');
  236. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  237. $this->collection->match(['_name' => 'derp'], $context);
  238. }
  239. /**
  240. * Test matching plugin routes.
  241. *
  242. * @return void
  243. */
  244. public function testMatchPlugin()
  245. {
  246. $context = [
  247. '_base' => '/',
  248. '_scheme' => 'http',
  249. '_host' => 'example.org',
  250. ];
  251. $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
  252. $routes->connect('/', ['controller' => 'Contacts']);
  253. $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
  254. $this->assertEquals('contacts', $result);
  255. }
  256. /**
  257. * Test that prefixes increase the specificity of a route match.
  258. *
  259. * Connect the admin route after the non prefixed version, this means
  260. * the non-prefix route would have a more specific name (users:index)
  261. *
  262. * @return void
  263. */
  264. public function testMatchPrefixSpecificity()
  265. {
  266. $context = [
  267. '_base' => '/',
  268. '_scheme' => 'http',
  269. '_host' => 'example.org',
  270. ];
  271. $routes = new RouteBuilder($this->collection, '/');
  272. $routes->connect('/:action/*', ['controller' => 'Users']);
  273. $routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
  274. $url = [
  275. 'plugin' => null,
  276. 'prefix' => 'admin',
  277. 'controller' => 'Users',
  278. 'action' => 'index'
  279. ];
  280. $result = $this->collection->match($url, $context);
  281. $this->assertEquals('admin/Users', $result);
  282. $url = [
  283. 'plugin' => null,
  284. 'controller' => 'Users',
  285. 'action' => 'index'
  286. ];
  287. $result = $this->collection->match($url, $context);
  288. $this->assertEquals('index', $result);
  289. }
  290. /**
  291. * Test getting named routes.
  292. *
  293. * @return void
  294. */
  295. public function testNamed()
  296. {
  297. $routes = new RouteBuilder($this->collection, '/l');
  298. $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
  299. $routes->connect('/:controller/:action/*');
  300. $all = $this->collection->named();
  301. $this->assertCount(1, $all);
  302. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  303. $this->assertEquals('/l/:controller', $all['cntrl']->template);
  304. }
  305. /**
  306. * Test the add() and routes() method.
  307. *
  308. * @return void
  309. */
  310. public function testAddingRoutes()
  311. {
  312. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  313. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  314. $this->collection->add($one);
  315. $this->collection->add($two);
  316. $routes = $this->collection->routes();
  317. $this->assertCount(2, $routes);
  318. $this->assertSame($one, $routes[0]);
  319. $this->assertSame($two, $routes[1]);
  320. }
  321. /**
  322. * Test the add() with some _name.
  323. *
  324. * @expectedException \Cake\Routing\Exception\DuplicateNamedRouteException
  325. *
  326. * @return void
  327. */
  328. public function testAddingDuplicateNamedRoutes()
  329. {
  330. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  331. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  332. $this->collection->add($one, ['_name' => 'test']);
  333. $this->collection->add($two, ['_name' => 'test']);
  334. }
  335. /**
  336. * Test basic get/set of extensions.
  337. *
  338. * @return void
  339. */
  340. public function testExtensions()
  341. {
  342. $this->assertEquals([], $this->collection->extensions());
  343. $this->collection->extensions('json');
  344. $this->assertEquals(['json'], $this->collection->extensions());
  345. $this->collection->extensions(['rss', 'xml']);
  346. $this->assertEquals(['json', 'rss', 'xml'], $this->collection->extensions());
  347. $this->collection->extensions(['csv'], false);
  348. $this->assertEquals(['csv'], $this->collection->extensions());
  349. }
  350. }