RouteCollectionTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'add'], $context);
  155. }
  156. /**
  157. * Test matching routes.
  158. *
  159. * @return void
  160. */
  161. public function testMatch()
  162. {
  163. $context = [
  164. '_base' => '/',
  165. '_scheme' => 'http',
  166. '_host' => 'example.org',
  167. ];
  168. $routes = new RouteBuilder($this->collection, '/b');
  169. $routes->connect('/', ['controller' => 'Articles']);
  170. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view']);
  171. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'index'], $context);
  172. $this->assertEquals('b', $result);
  173. $result = $this->collection->match(
  174. ['id' => 'thing', 'plugin' => null, 'controller' => 'Articles', 'action' => 'view'],
  175. $context
  176. );
  177. $this->assertEquals('b/thing', $result);
  178. }
  179. /**
  180. * Test matching routes with names
  181. *
  182. * @return void
  183. */
  184. public function testMatchNamed()
  185. {
  186. $context = [
  187. '_base' => '/',
  188. '_scheme' => 'http',
  189. '_host' => 'example.org',
  190. ];
  191. $routes = new RouteBuilder($this->collection, '/b');
  192. $routes->connect('/', ['controller' => 'Articles']);
  193. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  194. $result = $this->collection->match(['_name' => 'article:view', 'id' => '2'], $context);
  195. $this->assertEquals('/b/2', $result);
  196. $result = $this->collection->match(['plugin' => null, 'controller' => 'Articles', 'action' => 'view', 'id' => '2'], $context);
  197. $this->assertEquals('b/2', $result);
  198. }
  199. /**
  200. * Test match() throws an error on named routes that fail to match
  201. *
  202. * @expectedException \Cake\Routing\Exception\MissingRouteException
  203. * @expectedExceptionMessage A named route was found for "fail", but matching failed
  204. */
  205. public function testMatchNamedError()
  206. {
  207. $context = [
  208. '_base' => '/',
  209. '_scheme' => 'http',
  210. '_host' => 'example.org',
  211. ];
  212. $routes = new RouteBuilder($this->collection, '/b');
  213. $routes->connect('/:lang/articles', ['controller' => 'Articles'], ['_name' => 'fail']);
  214. $this->collection->match(['_name' => 'fail'], $context);
  215. }
  216. /**
  217. * Test matching routes with names and failing
  218. *
  219. * @expectedException \Cake\Routing\Exception\MissingRouteException
  220. * @return void
  221. */
  222. public function testMatchNamedMissingError()
  223. {
  224. $context = [
  225. '_base' => '/',
  226. '_scheme' => 'http',
  227. '_host' => 'example.org',
  228. ];
  229. $routes = new RouteBuilder($this->collection, '/b');
  230. $routes->connect('/:id', ['controller' => 'Articles', 'action' => 'view'], ['_name' => 'article:view']);
  231. $this->collection->match(['_name' => 'derp'], $context);
  232. }
  233. /**
  234. * Test matching plugin routes.
  235. *
  236. * @return void
  237. */
  238. public function testMatchPlugin()
  239. {
  240. $context = [
  241. '_base' => '/',
  242. '_scheme' => 'http',
  243. '_host' => 'example.org',
  244. ];
  245. $routes = new RouteBuilder($this->collection, '/contacts', ['plugin' => 'Contacts']);
  246. $routes->connect('/', ['controller' => 'Contacts']);
  247. $result = $this->collection->match(['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], $context);
  248. $this->assertEquals('contacts', $result);
  249. }
  250. /**
  251. * Test that prefixes increase the specificity of a route match.
  252. *
  253. * Connect the admin route after the non prefixed version, this means
  254. * the non-prefix route would have a more specific name (users:index)
  255. *
  256. * @return void
  257. */
  258. public function testMatchPrefixSpecificity()
  259. {
  260. $context = [
  261. '_base' => '/',
  262. '_scheme' => 'http',
  263. '_host' => 'example.org',
  264. ];
  265. $routes = new RouteBuilder($this->collection, '/');
  266. $routes->connect('/:action/*', ['controller' => 'Users']);
  267. $routes->connect('/admin/:controller', ['prefix' => 'admin', 'action' => 'index']);
  268. $url = [
  269. 'plugin' => null,
  270. 'prefix' => 'admin',
  271. 'controller' => 'Users',
  272. 'action' => 'index'
  273. ];
  274. $result = $this->collection->match($url, $context);
  275. $this->assertEquals('admin/Users', $result);
  276. $url = [
  277. 'plugin' => null,
  278. 'controller' => 'Users',
  279. 'action' => 'index'
  280. ];
  281. $result = $this->collection->match($url, $context);
  282. $this->assertEquals('index', $result);
  283. }
  284. /**
  285. * Test getting named routes.
  286. *
  287. * @return void
  288. */
  289. public function testNamed()
  290. {
  291. $routes = new RouteBuilder($this->collection, '/l');
  292. $routes->connect('/:controller', ['action' => 'index'], ['_name' => 'cntrl']);
  293. $routes->connect('/:controller/:action/*');
  294. $all = $this->collection->named();
  295. $this->assertCount(1, $all);
  296. $this->assertInstanceOf('Cake\Routing\Route\Route', $all['cntrl']);
  297. $this->assertEquals('/l/:controller', $all['cntrl']->template);
  298. }
  299. /**
  300. * Test the add() and routes() method.
  301. *
  302. * @return void
  303. */
  304. public function testAddingRoutes()
  305. {
  306. $one = new Route('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
  307. $two = new Route('/', ['controller' => 'Dashboards', 'action' => 'display']);
  308. $this->collection->add($one);
  309. $this->collection->add($two);
  310. $routes = $this->collection->routes();
  311. $this->assertCount(2, $routes);
  312. $this->assertSame($one, $routes[0]);
  313. $this->assertSame($two, $routes[1]);
  314. }
  315. /**
  316. * Test basic get/set of extensions.
  317. *
  318. * @return void
  319. */
  320. public function testExtensions()
  321. {
  322. $this->assertEquals([], $this->collection->extensions());
  323. $this->collection->extensions('json');
  324. $this->assertEquals(['json'], $this->collection->extensions());
  325. $this->collection->extensions(['rss', 'xml']);
  326. $this->assertEquals(['json', 'rss', 'xml'], $this->collection->extensions());
  327. $this->collection->extensions(['csv'], false);
  328. $this->assertEquals(['csv'], $this->collection->extensions());
  329. }
  330. }