RouteCollectionTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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\Router;
  19. use Cake\Routing\Route\Route;
  20. use Cake\TestSuite\TestCase;
  21. class RouteCollectionTest extends TestCase
  22. {
  23. /**
  24. * Setup method
  25. *
  26. * @return void
  27. */
  28. public function setUp()
  29. {
  30. parent::setUp();
  31. $this->collection = new RouteCollection();
  32. }
  33. /**
  34. * Test parse() throws an error on unknown routes.
  35. *
  36. * @expectedException \Cake\Routing\Exception\MissingRouteException
  37. * @expectedExceptionMessage A route matching "/" could not be found
  38. */
  39. public function testParseMissingRoute()
  40. {
  41. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  42. $routes->connect('/', ['controller' => 'Articles']);
  43. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  44. $result = $this->collection->parse('/');
  45. $this->assertEquals([], $result, 'Should not match, missing /b');
  46. }
  47. /**
  48. * Test parsing routes.
  49. *
  50. * @return void
  51. */
  52. public function testParse()
  53. {
  54. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  55. $routes->connect('/', ['controller' => 'Articles']);
  56. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  57. $routes->connect('/media/search/*', ['controller' => 'Media', 'action' => 'search']);
  58. $result = $this->collection->parse('/b/');
  59. $expected = [
  60. 'controller' => 'Articles',
  61. 'action' => 'index',
  62. 'pass' => [],
  63. 'plugin' => null,
  64. 'key' => 'value',
  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. ];
  77. $this->assertEquals($expected, $result);
  78. $result = $this->collection->parse('/b/media/search');
  79. $expected = [
  80. 'key' => 'value',
  81. 'pass' => [],
  82. 'plugin' => null,
  83. 'controller' => 'Media',
  84. 'action' => 'search'
  85. ];
  86. $this->assertEquals($expected, $result);
  87. $result = $this->collection->parse('/b/media/search/thing');
  88. $expected = [
  89. 'key' => 'value',
  90. 'pass' => ['thing'],
  91. 'plugin' => null,
  92. 'controller' => 'Media',
  93. 'action' => 'search'
  94. ];
  95. $this->assertEquals($expected, $result);
  96. }
  97. /**
  98. * Test that parse decodes URL data before matching.
  99. * This is important for multibyte URLs that pass through URL rewriting.
  100. *
  101. * @return void
  102. */
  103. public function testParseEncodedBytesInFixedSegment()
  104. {
  105. $routes = new RouteBuilder($this->collection, '/');
  106. $routes->connect('/ден/:day-:month', ['controller' => 'Events', 'action' => 'index']);
  107. $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';
  108. $result = $this->collection->parse($url);
  109. $expected = [
  110. 'pass' => [],
  111. 'plugin' => null,
  112. 'controller' => 'Events',
  113. 'action' => 'index',
  114. 'day' => 15,
  115. 'month' => 'октомври',
  116. '?' => ['test' => 'foo'],
  117. ];
  118. $this->assertEquals($expected, $result);
  119. }
  120. /**
  121. * Test that parsing checks all the related path scopes.
  122. *
  123. * @return void
  124. */
  125. public function testParseFallback()
  126. {
  127. $routes = new RouteBuilder($this->collection, '/', []);
  128. $routes->resources('Articles');
  129. $routes->connect('/:controller', ['action' => 'index'], [], ['routeClass' => 'InflectedRoute']);
  130. $routes->connect('/:controller/:action', [], ['routeClass' => 'InflectedRoute']);
  131. $result = $this->collection->parse('/articles/add');
  132. $expected = [
  133. 'controller' => 'Articles',
  134. 'action' => 'add',
  135. 'plugin' => null,
  136. 'pass' => []
  137. ];
  138. $this->assertEquals($expected, $result);
  139. }
  140. /**
  141. * Test match() throws an error on unknown routes.
  142. *
  143. * @expectedException \Cake\Routing\Exception\MissingRouteException
  144. * @expectedExceptionMessage A route matching "array (
  145. */
  146. public function testMatchError()
  147. {
  148. $context = [
  149. '_base' => '/',
  150. '_scheme' => 'http',
  151. '_host' => 'example.org',
  152. ];
  153. $routes = new RouteBuilder($this->collection, '/b');
  154. $routes->connect('/', ['controller' => 'Articles']);
  155. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
  156. $this->assertFalse($result, 'No matches');
  157. }
  158. /**
  159. * Test matching routes.
  160. *
  161. * @return void
  162. */
  163. public function testMatch()
  164. {
  165. $context = [
  166. '_base' => '/',
  167. '_scheme' => 'http',
  168. '_host' => 'example.org',
  169. ];
  170. $routes = new RouteBuilder($this->collection, '/b');
  171. $routes->connect('/', ['controller' => 'Articles']);
  172. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  173. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
  174. $this->assertEquals('b', $result);
  175. $result = $this->collection->match(
  176. ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
  177. $context
  178. );
  179. $this->assertEquals('b/thing', $result);
  180. }
  181. /**
  182. * Test matching routes with names
  183. *
  184. * @return void
  185. */
  186. public function testMatchNamed()
  187. {
  188. $context = [
  189. '_base' => '/',
  190. '_scheme' => 'http',
  191. '_host' => 'example.org',
  192. ];
  193. $routes = new RouteBuilder($this->collection, '/b');
  194. $routes->connect('/', ['controller' => 'Articles']);
  195. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  196. $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
  197. $this->assertEquals('/b/2', $result);
  198. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'view', 'id' => '2'], $context);
  199. $this->assertEquals('b/2', $result);
  200. }
  201. /**
  202. * Test matching routes with names and failing
  203. *
  204. * @expectedException \Cake\Routing\Exception\MissingRouteException
  205. * @return void
  206. */
  207. public function testMatchNamedError()
  208. {
  209. $context = [
  210. '_base' => '/',
  211. '_scheme' => 'http',
  212. '_host' => 'example.org',
  213. ];
  214. $routes = new RouteBuilder($this->collection, '/b');
  215. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  216. $this->collection->match(['_name' => 'derp'], $context);
  217. }
  218. /**
  219. * Test matching plugin routes.
  220. *
  221. * @return void
  222. */
  223. public function testMatchPlugin()
  224. {
  225. $context = [
  226. '_base' => '/',
  227. '_scheme' => 'http',
  228. '_host' => 'example.org',
  229. ];
  230. $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
  231. $routes->connect('/', ['controller' => 'Contacts']);
  232. $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
  233. $this->assertEquals('contacts', $result);
  234. }
  235. /**
  236. * Test that prefixes increase the specificity of a route match.
  237. *
  238. * Connect the admin route after the non prefixed version, this means
  239. * the non-prefix route would have a more specific name (users:index)
  240. *
  241. * @return void
  242. */
  243. public function testMatchPrefixSpecificity()
  244. {
  245. $context = [
  246. '_base' => '/',
  247. '_scheme' => 'http',
  248. '_host' => 'example.org',
  249. ];
  250. $routes = new RouteBuilder($this->collection, '/');
  251. $routes->connect('/:action/*', ['controller' => 'Users']);
  252. $routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
  253. $url = [
  254. 'plugin' => null,
  255. 'prefix' => 'admin',
  256. 'controller' => 'Users',
  257. 'action' => 'index'
  258. ];
  259. $result = $this->collection->match($url, $context);
  260. $this->assertEquals('admin/Users', $result);
  261. $url = [
  262. 'plugin' => null,
  263. 'controller' => 'Users',
  264. 'action' => 'index'
  265. ];
  266. $result = $this->collection->match($url, $context);
  267. $this->assertEquals('index', $result);
  268. }
  269. /**
  270. * Test getting named routes.
  271. *
  272. * @return void
  273. */
  274. public function testNamed()
  275. {
  276. $routes = new RouteBuilder($this->collection, '/l');
  277. $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
  278. $routes->connect('/:controller/:action/*');
  279. $all = $this->collection->named();
  280. $this->assertCount(1, $all);
  281. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  282. $this->assertEquals('/l/:controller', $all['cntrl']->template);
  283. }
  284. /**
  285. * Test the add() and routes() method.
  286. *
  287. * @return void
  288. */
  289. public function testAddingRoutes()
  290. {
  291. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  292. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  293. $this->collection->add($one);
  294. $this->collection->add($two);
  295. $routes = $this->collection->routes();
  296. $this->assertCount(2, $routes);
  297. $this->assertSame($one, $routes[0]);
  298. $this->assertSame($two, $routes[1]);
  299. }
  300. /**
  301. * Test basic get/set of extensions.
  302. *
  303. * @return void
  304. */
  305. public function testExtensions()
  306. {
  307. $this->assertEquals([], $this->collection->extensions());
  308. $this->collection->extensions('json');
  309. $this->assertEquals(['json'], $this->collection->extensions());
  310. $this->collection->extensions(['rss', 'xml']);
  311. $this->assertEquals(['json', 'rss', 'xml'], $this->collection->extensions());
  312. $this->collection->extensions(['csv'], false);
  313. $this->assertEquals(['csv'], $this->collection->extensions());
  314. }
  315. }