RouteCollectionTest.php 11 KB

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