RouteBuilderTest.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing;
  16. use Cake\Core\Plugin;
  17. use Cake\Routing\RouteBuilder;
  18. use Cake\Routing\RouteCollection;
  19. use Cake\Routing\Router;
  20. use Cake\Routing\Route\InflectedRoute;
  21. use Cake\Routing\Route\RedirectRoute;
  22. use Cake\Routing\Route\Route;
  23. use Cake\TestSuite\TestCase;
  24. use RuntimeException;
  25. /**
  26. * RouteBuilder test case
  27. */
  28. class RouteBuilderTest extends TestCase
  29. {
  30. /**
  31. * Setup method
  32. *
  33. * @return void
  34. */
  35. public function setUp()
  36. {
  37. parent::setUp();
  38. $this->collection = new RouteCollection();
  39. }
  40. /**
  41. * Teardown method
  42. *
  43. * @return void
  44. */
  45. public function tearDown()
  46. {
  47. parent::tearDown();
  48. Plugin::unload();
  49. }
  50. /**
  51. * Test path()
  52. *
  53. * @return void
  54. */
  55. public function testPath()
  56. {
  57. $routes = new RouteBuilder($this->collection, '/some/path');
  58. $this->assertEquals('/some/path', $routes->path());
  59. $routes = new RouteBuilder($this->collection, '/:book_id');
  60. $this->assertEquals('/', $routes->path());
  61. $routes = new RouteBuilder($this->collection, '/path/:book_id');
  62. $this->assertEquals('/path/', $routes->path());
  63. $routes = new RouteBuilder($this->collection, '/path/book:book_id');
  64. $this->assertEquals('/path/book', $routes->path());
  65. }
  66. /**
  67. * Test params()
  68. *
  69. * @return void
  70. */
  71. public function testParams()
  72. {
  73. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  74. $this->assertEquals(['prefix' => 'api'], $routes->params());
  75. }
  76. /**
  77. * Test getting connected routes.
  78. *
  79. * @return void
  80. */
  81. public function testRoutes()
  82. {
  83. $routes = new RouteBuilder($this->collection, '/l');
  84. $routes->connect('/:controller', ['action' => 'index']);
  85. $routes->connect('/:controller/:action/*');
  86. $all = $this->collection->routes();
  87. $this->assertCount(2, $all);
  88. $this->assertInstanceOf(Route::class, $all[0]);
  89. $this->assertInstanceOf(Route::class, $all[1]);
  90. }
  91. /**
  92. * Test setting default route class
  93. *
  94. * @return void
  95. */
  96. public function testRouteClass()
  97. {
  98. $routes = new RouteBuilder(
  99. $this->collection,
  100. '/l',
  101. [],
  102. ['routeClass' => 'InflectedRoute']
  103. );
  104. $routes->connect('/:controller', ['action' => 'index']);
  105. $routes->connect('/:controller/:action/*');
  106. $all = $this->collection->routes();
  107. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  108. $this->assertInstanceOf(InflectedRoute::class, $all[1]);
  109. $this->collection = new RouteCollection();
  110. $routes = new RouteBuilder($this->collection, '/l');
  111. $this->assertSame($routes, $routes->setRouteClass('TestApp\Routing\Route\DashedRoute'));
  112. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->getRouteClass());
  113. $routes->connect('/:controller', ['action' => 'index']);
  114. $all = $this->collection->routes();
  115. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  116. }
  117. /**
  118. * Test connecting an instance routes.
  119. *
  120. * @return void
  121. */
  122. public function testConnectInstance()
  123. {
  124. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'api']);
  125. $route = new Route('/:controller');
  126. $this->assertSame($route, $routes->connect($route));
  127. $result = $this->collection->routes()[0];
  128. $this->assertSame($route, $result);
  129. }
  130. /**
  131. * Test connecting basic routes.
  132. *
  133. * @return void
  134. */
  135. public function testConnectBasic()
  136. {
  137. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'api']);
  138. $route = $routes->connect('/:controller');
  139. $this->assertInstanceOf(Route::class, $route);
  140. $this->assertSame($route, $this->collection->routes()[0]);
  141. $this->assertEquals('/l/:controller', $route->template);
  142. $expected = ['prefix' => 'api', 'action' => 'index', 'plugin' => null];
  143. $this->assertEquals($expected, $route->defaults);
  144. }
  145. /**
  146. * Test that compiling a route results in an trailing / optional pattern.
  147. *
  148. * @return void
  149. */
  150. public function testConnectTrimTrailingSlash()
  151. {
  152. $routes = new RouteBuilder($this->collection, '/articles', ['controller' => 'Articles']);
  153. $routes->connect('/', ['action' => 'index']);
  154. $expected = [
  155. 'plugin' => null,
  156. 'controller' => 'Articles',
  157. 'action' => 'index',
  158. 'pass' => [],
  159. '_matchedRoute' => '/articles',
  160. ];
  161. $this->assertEquals($expected, $this->collection->parse('/articles'));
  162. $this->assertEquals($expected, $this->collection->parse('/articles/'));
  163. }
  164. /**
  165. * Test connect() with short string syntax
  166. *
  167. * @return void
  168. */
  169. public function testConnectShortStringInvalid()
  170. {
  171. $this->expectException(RuntimeException::class);
  172. $routes = new RouteBuilder($this->collection, '/');
  173. $routes->connect('/my-articles/view', 'Articles:no');
  174. }
  175. /**
  176. * Test connect() with short string syntax
  177. *
  178. * @return void
  179. */
  180. public function testConnectShortString()
  181. {
  182. $routes = new RouteBuilder($this->collection, '/');
  183. $routes->connect('/my-articles/view', 'Articles::view');
  184. $expected = [
  185. 'pass' => [],
  186. 'controller' => 'Articles',
  187. 'action' => 'view',
  188. 'plugin' => null,
  189. '_matchedRoute' => '/my-articles/view'
  190. ];
  191. $this->assertEquals($expected, $this->collection->parse('/my-articles/view'));
  192. $url = $expected['_matchedRoute'];
  193. unset($expected['_matchedRoute']);
  194. $this->assertEquals($url, '/' . $this->collection->match($expected, []));
  195. }
  196. /**
  197. * Test connect() with short string syntax
  198. *
  199. * @return void
  200. */
  201. public function testConnectShortStringPrefix()
  202. {
  203. $routes = new RouteBuilder($this->collection, '/');
  204. $routes->connect('/admin/bookmarks', 'Admin/Bookmarks::index');
  205. $expected = [
  206. 'pass' => [],
  207. 'plugin' => null,
  208. 'prefix' => 'admin',
  209. 'controller' => 'Bookmarks',
  210. 'action' => 'index',
  211. '_matchedRoute' => '/admin/bookmarks'
  212. ];
  213. $this->assertEquals($expected, $this->collection->parse('/admin/bookmarks'));
  214. $url = $expected['_matchedRoute'];
  215. unset($expected['_matchedRoute']);
  216. $this->assertEquals($url, '/' . $this->collection->match($expected, []));
  217. }
  218. /**
  219. * Test connect() with short string syntax
  220. *
  221. * @return void
  222. */
  223. public function testConnectShortStringPlugin()
  224. {
  225. $routes = new RouteBuilder($this->collection, '/');
  226. $routes->connect('/blog/articles/view', 'Blog.Articles::view');
  227. $expected = [
  228. 'pass' => [],
  229. 'plugin' => 'Blog',
  230. 'controller' => 'Articles',
  231. 'action' => 'view',
  232. '_matchedRoute' => '/blog/articles/view'
  233. ];
  234. $this->assertEquals($expected, $this->collection->parse('/blog/articles/view'));
  235. $url = $expected['_matchedRoute'];
  236. unset($expected['_matchedRoute']);
  237. $this->assertEquals($url, '/' . $this->collection->match($expected, []));
  238. }
  239. /**
  240. * Test connect() with short string syntax
  241. *
  242. * @return void
  243. */
  244. public function testConnectShortStringPluginPrefix()
  245. {
  246. $routes = new RouteBuilder($this->collection, '/');
  247. $routes->connect('/admin/blog/articles/view', 'Vendor/Blog.Management/Admin/Articles::view');
  248. $expected = [
  249. 'pass' => [],
  250. 'plugin' => 'Vendor/Blog',
  251. 'prefix' => 'management/admin',
  252. 'controller' => 'Articles',
  253. 'action' => 'view',
  254. '_matchedRoute' => '/admin/blog/articles/view'
  255. ];
  256. $this->assertEquals($expected, $this->collection->parse('/admin/blog/articles/view'));
  257. $url = $expected['_matchedRoute'];
  258. unset($expected['_matchedRoute']);
  259. $this->assertEquals($url, '/' . $this->collection->match($expected, []));
  260. }
  261. /**
  262. * Test if a route name already exist
  263. *
  264. * @return void
  265. */
  266. public function testNameExists()
  267. {
  268. $routes = new RouteBuilder($this->collection, '/l', ['prefix' => 'api']);
  269. $this->assertFalse($routes->nameExists('myRouteName'));
  270. $routes->connect('myRouteUrl', ['action' => 'index'], ['_name' => 'myRouteName']);
  271. $this->assertTrue($routes->nameExists('myRouteName'));
  272. }
  273. /**
  274. * Test setExtensions() and getExtensions().
  275. *
  276. * @return void
  277. */
  278. public function testExtensions()
  279. {
  280. $routes = new RouteBuilder($this->collection, '/l');
  281. $this->assertSame($routes, $routes->setExtensions(['html']));
  282. $this->assertSame(['html'], $routes->getExtensions());
  283. }
  284. /**
  285. * Test extensions being connected to routes.
  286. *
  287. * @return void
  288. */
  289. public function testConnectExtensions()
  290. {
  291. $routes = new RouteBuilder(
  292. $this->collection,
  293. '/l',
  294. [],
  295. ['extensions' => ['json']]
  296. );
  297. $this->assertEquals(['json'], $routes->getExtensions());
  298. $routes->connect('/:controller');
  299. $route = $this->collection->routes()[0];
  300. $this->assertEquals(['json'], $route->options['_ext']);
  301. $routes->setExtensions(['xml', 'json']);
  302. $routes->connect('/:controller/:action');
  303. $new = $this->collection->routes()[1];
  304. $this->assertEquals(['json'], $route->options['_ext']);
  305. $this->assertEquals(['xml', 'json'], $new->options['_ext']);
  306. }
  307. /**
  308. * Test adding additional extensions will be merged with current.
  309. *
  310. * @return void
  311. */
  312. public function testConnectExtensionsAdd()
  313. {
  314. $routes = new RouteBuilder(
  315. $this->collection,
  316. '/l',
  317. [],
  318. ['extensions' => ['json']]
  319. );
  320. $this->assertEquals(['json'], $routes->getExtensions());
  321. $routes->addExtensions(['xml']);
  322. $this->assertEquals(['json', 'xml'], $routes->getExtensions());
  323. $routes->addExtensions('csv');
  324. $this->assertEquals(['json', 'xml', 'csv'], $routes->getExtensions());
  325. }
  326. /**
  327. * test that setExtensions() accepts a string.
  328. *
  329. * @return void
  330. */
  331. public function testExtensionsString()
  332. {
  333. $routes = new RouteBuilder($this->collection, '/l');
  334. $routes->setExtensions('json');
  335. $this->assertEquals(['json'], $routes->getExtensions());
  336. }
  337. /**
  338. * Test error on invalid route class
  339. *
  340. * @return void
  341. */
  342. public function testConnectErrorInvalidRouteClass()
  343. {
  344. $this->expectException(\InvalidArgumentException::class);
  345. $this->expectExceptionMessage('Route class not found, or route class is not a subclass of');
  346. $routes = new RouteBuilder(
  347. $this->collection,
  348. '/l',
  349. [],
  350. ['extensions' => ['json']]
  351. );
  352. $routes->connect('/:controller', [], ['routeClass' => '\StdClass']);
  353. }
  354. /**
  355. * Test conflicting parameters raises an exception.
  356. *
  357. * @return void
  358. */
  359. public function testConnectConflictingParameters()
  360. {
  361. $this->expectException(\BadMethodCallException::class);
  362. $this->expectExceptionMessage('You cannot define routes that conflict with the scope.');
  363. $routes = new RouteBuilder($this->collection, '/admin', ['plugin' => 'TestPlugin']);
  364. $routes->connect('/', ['plugin' => 'TestPlugin2', 'controller' => 'Dashboard', 'action' => 'view']);
  365. }
  366. /**
  367. * Test connecting redirect routes.
  368. *
  369. * @return void
  370. */
  371. public function testRedirect()
  372. {
  373. $routes = new RouteBuilder($this->collection, '/');
  374. $routes->redirect('/p/:id', ['controller' => 'posts', 'action' => 'view'], ['status' => 301]);
  375. $route = $this->collection->routes()[0];
  376. $this->assertInstanceOf(RedirectRoute::class, $route);
  377. $routes->redirect('/old', '/forums', ['status' => 301]);
  378. $route = $this->collection->routes()[1];
  379. $this->assertInstanceOf(RedirectRoute::class, $route);
  380. $this->assertEquals('/forums', $route->redirect[0]);
  381. }
  382. /**
  383. * Test using a custom route class for redirect routes.
  384. *
  385. * @return void
  386. */
  387. public function testRedirectWithCustomRouteClass()
  388. {
  389. $routes = new RouteBuilder($this->collection, '/');
  390. $routes->redirect('/old', '/forums', ['status' => 301, 'routeClass' => 'InflectedRoute']);
  391. $route = $this->collection->routes()[0];
  392. $this->assertInstanceOf(InflectedRoute::class, $route);
  393. }
  394. /**
  395. * Test creating sub-scopes with prefix()
  396. *
  397. * @return void
  398. */
  399. public function testPrefix()
  400. {
  401. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  402. $res = $routes->prefix('admin', ['param' => 'value'], function ($r) {
  403. $this->assertInstanceOf(RouteBuilder::class, $r);
  404. $this->assertCount(0, $this->collection->routes());
  405. $this->assertEquals('/path/admin', $r->path());
  406. $this->assertEquals(['prefix' => 'admin', 'key' => 'value', 'param' => 'value'], $r->params());
  407. });
  408. $this->assertNull($res);
  409. }
  410. /**
  411. * Test creating sub-scopes with prefix()
  412. *
  413. * @return void
  414. */
  415. public function testPrefixWithNoParams()
  416. {
  417. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  418. $res = $routes->prefix('admin', function ($r) {
  419. $this->assertInstanceOf(RouteBuilder::class, $r);
  420. $this->assertCount(0, $this->collection->routes());
  421. $this->assertEquals('/path/admin', $r->path());
  422. $this->assertEquals(['prefix' => 'admin', 'key' => 'value'], $r->params());
  423. });
  424. $this->assertNull($res);
  425. }
  426. /**
  427. * Test creating sub-scopes with prefix()
  428. *
  429. * @return void
  430. */
  431. public function testNestedPrefix()
  432. {
  433. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  434. $res = $routes->prefix('api', ['_namePrefix' => 'api:'], function ($r) {
  435. $this->assertEquals('/admin/api', $r->path());
  436. $this->assertEquals(['prefix' => 'admin/api'], $r->params());
  437. $this->assertEquals('api:', $r->namePrefix());
  438. });
  439. $this->assertNull($res);
  440. }
  441. /**
  442. * Test creating sub-scopes with prefix()
  443. *
  444. * @return void
  445. */
  446. public function testPathWithDotInPrefix()
  447. {
  448. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  449. $res = $routes->prefix('api', function ($r) {
  450. $r->prefix('v10', ['path' => '/v1.0'], function ($r2) {
  451. $this->assertEquals('/admin/api/v1.0', $r2->path());
  452. $this->assertEquals(['prefix' => 'admin/api/v10'], $r2->params());
  453. $r2->prefix('b1', ['path' => '/beta.1'], function ($r3) {
  454. $this->assertEquals('/admin/api/v1.0/beta.1', $r3->path());
  455. $this->assertEquals(['prefix' => 'admin/api/v10/b1'], $r3->params());
  456. });
  457. });
  458. });
  459. $this->assertNull($res);
  460. }
  461. /**
  462. * Test creating sub-scopes with plugin()
  463. *
  464. * @return void
  465. */
  466. public function testNestedPlugin()
  467. {
  468. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  469. $res = $routes->plugin('Contacts', function ($r) {
  470. $this->assertEquals('/b/contacts', $r->path());
  471. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  472. $r->connect('/:controller');
  473. $route = $this->collection->routes()[0];
  474. $this->assertEquals(
  475. ['key' => 'value', 'plugin' => 'Contacts', 'action' => 'index'],
  476. $route->defaults
  477. );
  478. });
  479. $this->assertNull($res);
  480. }
  481. /**
  482. * Test creating sub-scopes with plugin() + path option
  483. *
  484. * @return void
  485. */
  486. public function testNestedPluginPathOption()
  487. {
  488. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  489. $routes->plugin('Contacts', ['path' => '/people'], function ($r) {
  490. $this->assertEquals('/b/people', $r->path());
  491. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  492. });
  493. }
  494. /**
  495. * Test connecting resources.
  496. *
  497. * @return void
  498. */
  499. public function testResources()
  500. {
  501. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  502. $routes->resources('Articles', ['_ext' => 'json']);
  503. $all = $this->collection->routes();
  504. $this->assertCount(5, $all);
  505. $this->assertEquals('/api/articles', $all[0]->template);
  506. $this->assertEquals(
  507. ['controller', 'action', '_method', 'prefix', 'plugin'],
  508. array_keys($all[0]->defaults)
  509. );
  510. $this->assertEquals('json', $all[0]->options['_ext']);
  511. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  512. }
  513. /**
  514. * Test connecting resources with a path
  515. *
  516. * @return void
  517. */
  518. public function testResourcesPathOption()
  519. {
  520. $routes = new RouteBuilder($this->collection, '/api');
  521. $routes->resources('Articles', ['path' => 'posts'], function ($routes) {
  522. $routes->resources('Comments');
  523. });
  524. $all = $this->collection->routes();
  525. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  526. $this->assertEquals('/api/posts', $all[0]->template);
  527. $this->assertEquals('/api/posts/:id', $all[2]->template);
  528. $this->assertEquals(
  529. '/api/posts/:article_id/comments',
  530. $all[6]->template,
  531. 'parameter name should reflect resource name'
  532. );
  533. }
  534. /**
  535. * Test connecting resources with a prefix
  536. *
  537. * @return void
  538. */
  539. public function testResourcesPrefix()
  540. {
  541. $routes = new RouteBuilder($this->collection, '/api');
  542. $routes->resources('Articles', ['prefix' => 'rest']);
  543. $all = $this->collection->routes();
  544. $this->assertEquals('rest', $all[0]->defaults['prefix']);
  545. }
  546. /**
  547. * Test that resource prefixes work within a prefixed scope.
  548. *
  549. * @return void
  550. */
  551. public function testResourcesNestedPrefix()
  552. {
  553. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  554. $routes->resources('Articles', ['prefix' => 'rest']);
  555. $all = $this->collection->routes();
  556. $this->assertCount(5, $all);
  557. $this->assertEquals('/api/articles', $all[0]->template);
  558. foreach ($all as $route) {
  559. $this->assertEquals('api/rest', $route->defaults['prefix']);
  560. $this->assertEquals('Articles', $route->defaults['controller']);
  561. }
  562. }
  563. /**
  564. * Test connecting resources with the inflection option
  565. *
  566. * @return void
  567. */
  568. public function testResourcesInflection()
  569. {
  570. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  571. $routes->resources('BlogPosts', ['_ext' => 'json', 'inflect' => 'dasherize']);
  572. $all = $this->collection->routes();
  573. $this->assertCount(5, $all);
  574. $this->assertEquals('/api/blog-posts', $all[0]->template);
  575. $this->assertEquals(
  576. ['controller', 'action', '_method', 'prefix', 'plugin'],
  577. array_keys($all[0]->defaults)
  578. );
  579. $this->assertEquals('BlogPosts', $all[0]->defaults['controller']);
  580. }
  581. /**
  582. * Test connecting nested resources with the inflection option
  583. *
  584. * @return void
  585. */
  586. public function testResourcesNestedInflection()
  587. {
  588. $routes = new RouteBuilder($this->collection, '/api');
  589. $routes->resources(
  590. 'NetworkObjects',
  591. ['inflect' => 'dasherize'],
  592. function ($routes) {
  593. $routes->resources('Attributes');
  594. }
  595. );
  596. $all = $this->collection->routes();
  597. $this->assertCount(10, $all);
  598. $this->assertEquals('/api/network-objects', $all[0]->template);
  599. $this->assertEquals('/api/network-objects/:id', $all[2]->template);
  600. $this->assertEquals('/api/network-objects/:network_object_id/attributes', $all[5]->template);
  601. }
  602. /**
  603. * Test connecting resources with additional mappings
  604. *
  605. * @return void
  606. */
  607. public function testResourcesMappings()
  608. {
  609. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  610. $routes->resources('Articles', [
  611. '_ext' => 'json',
  612. 'map' => [
  613. 'delete_all' => ['action' => 'deleteAll', 'method' => 'DELETE'],
  614. 'update_many' => ['action' => 'updateAll', 'method' => 'DELETE', 'path' => '/updateAll'],
  615. ]
  616. ]);
  617. $all = $this->collection->routes();
  618. $this->assertCount(7, $all);
  619. $this->assertEquals('/api/articles/delete_all', $all[5]->template, 'Path defaults to key name.');
  620. $this->assertEquals(
  621. ['controller', 'action', '_method', 'prefix', 'plugin'],
  622. array_keys($all[5]->defaults)
  623. );
  624. $this->assertEquals('Articles', $all[5]->defaults['controller']);
  625. $this->assertEquals('deleteAll', $all[5]->defaults['action']);
  626. $this->assertEquals('/api/articles/updateAll', $all[6]->template, 'Explicit path option');
  627. $this->assertEquals(
  628. ['controller', 'action', '_method', 'prefix', 'plugin'],
  629. array_keys($all[6]->defaults)
  630. );
  631. $this->assertEquals('Articles', $all[6]->defaults['controller']);
  632. $this->assertEquals('updateAll', $all[6]->defaults['action']);
  633. }
  634. /**
  635. * Test connecting resources.
  636. *
  637. * @return void
  638. */
  639. public function testResourcesInScope()
  640. {
  641. Router::scope('/api', ['prefix' => 'api'], function ($routes) {
  642. $routes->setExtensions(['json']);
  643. $routes->resources('Articles');
  644. });
  645. $url = Router::url([
  646. 'prefix' => 'api',
  647. 'controller' => 'Articles',
  648. 'action' => 'edit',
  649. '_method' => 'PUT',
  650. 'id' => 99
  651. ]);
  652. $this->assertEquals('/api/articles/99', $url);
  653. $url = Router::url([
  654. 'prefix' => 'api',
  655. 'controller' => 'Articles',
  656. 'action' => 'edit',
  657. '_method' => 'PUT',
  658. '_ext' => 'json',
  659. 'id' => 99
  660. ]);
  661. $this->assertEquals('/api/articles/99.json', $url);
  662. }
  663. /**
  664. * Test resource parsing.
  665. *
  666. * @group deprecated
  667. * @return void
  668. */
  669. public function testResourcesParsingReadGlobals()
  670. {
  671. $this->deprecated(function () {
  672. $routes = new RouteBuilder($this->collection, '/');
  673. $routes->resources('Articles');
  674. $_SERVER['REQUEST_METHOD'] = 'GET';
  675. $result = $this->collection->parse('/articles');
  676. $this->assertEquals('Articles', $result['controller']);
  677. $this->assertEquals('index', $result['action']);
  678. $this->assertEquals([], $result['pass']);
  679. $_SERVER['REQUEST_METHOD'] = 'POST';
  680. $result = $this->collection->parse('/articles');
  681. $this->assertEquals('Articles', $result['controller']);
  682. $this->assertEquals('add', $result['action']);
  683. $this->assertEquals([], $result['pass']);
  684. });
  685. }
  686. /**
  687. * Test resource parsing.
  688. *
  689. * @return void
  690. */
  691. public function testResourcesParsing()
  692. {
  693. $routes = new RouteBuilder($this->collection, '/');
  694. $routes->resources('Articles');
  695. $result = $this->collection->parse('/articles', 'GET');
  696. $this->assertEquals('Articles', $result['controller']);
  697. $this->assertEquals('index', $result['action']);
  698. $this->assertEquals([], $result['pass']);
  699. $result = $this->collection->parse('/articles/1', 'GET');
  700. $this->assertEquals('Articles', $result['controller']);
  701. $this->assertEquals('view', $result['action']);
  702. $this->assertEquals([1], $result['pass']);
  703. $result = $this->collection->parse('/articles', 'POST');
  704. $this->assertEquals('Articles', $result['controller']);
  705. $this->assertEquals('add', $result['action']);
  706. $this->assertEquals([], $result['pass']);
  707. $result = $this->collection->parse('/articles/1', 'PUT');
  708. $this->assertEquals('Articles', $result['controller']);
  709. $this->assertEquals('edit', $result['action']);
  710. $this->assertEquals([1], $result['pass']);
  711. $result = $this->collection->parse('/articles/1', 'DELETE');
  712. $this->assertEquals('Articles', $result['controller']);
  713. $this->assertEquals('delete', $result['action']);
  714. $this->assertEquals([1], $result['pass']);
  715. }
  716. /**
  717. * Test the only option of RouteBuilder.
  718. *
  719. * @return void
  720. */
  721. public function testResourcesOnlyString()
  722. {
  723. $routes = new RouteBuilder($this->collection, '/');
  724. $routes->resources('Articles', ['only' => 'index']);
  725. $result = $this->collection->routes();
  726. $this->assertCount(1, $result);
  727. $this->assertEquals('/articles', $result[0]->template);
  728. }
  729. /**
  730. * Test the only option of RouteBuilder.
  731. *
  732. * @return void
  733. */
  734. public function testResourcesOnlyArray()
  735. {
  736. $routes = new RouteBuilder($this->collection, '/');
  737. $routes->resources('Articles', ['only' => ['index', 'delete']]);
  738. $result = $this->collection->routes();
  739. $this->assertCount(2, $result);
  740. $this->assertEquals('/articles', $result[0]->template);
  741. $this->assertEquals('index', $result[0]->defaults['action']);
  742. $this->assertEquals('GET', $result[0]->defaults['_method']);
  743. $this->assertEquals('/articles/:id', $result[1]->template);
  744. $this->assertEquals('delete', $result[1]->defaults['action']);
  745. $this->assertEquals('DELETE', $result[1]->defaults['_method']);
  746. }
  747. /**
  748. * Test the actions option of RouteBuilder.
  749. *
  750. * @return void
  751. */
  752. public function testResourcesActions()
  753. {
  754. $routes = new RouteBuilder($this->collection, '/');
  755. $routes->resources('Articles', [
  756. 'only' => ['index', 'delete'],
  757. 'actions' => ['index' => 'showList']
  758. ]);
  759. $result = $this->collection->routes();
  760. $this->assertCount(2, $result);
  761. $this->assertEquals('/articles', $result[0]->template);
  762. $this->assertEquals('showList', $result[0]->defaults['action']);
  763. $this->assertEquals('/articles/:id', $result[1]->template);
  764. $this->assertEquals('delete', $result[1]->defaults['action']);
  765. }
  766. /**
  767. * Test nesting resources
  768. *
  769. * @return void
  770. */
  771. public function testResourcesNested()
  772. {
  773. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  774. $routes->resources('Articles', function ($routes) {
  775. $this->assertEquals('/api/articles/', $routes->path());
  776. $this->assertEquals(['prefix' => 'api'], $routes->params());
  777. $routes->resources('Comments');
  778. $route = $this->collection->routes()[6];
  779. $this->assertEquals('/api/articles/:article_id/comments', $route->template);
  780. });
  781. }
  782. /**
  783. * Test connecting fallback routes.
  784. *
  785. * @return void
  786. */
  787. public function testFallbacks()
  788. {
  789. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  790. $routes->fallbacks();
  791. $all = $this->collection->routes();
  792. $this->assertEquals('/api/:controller', $all[0]->template);
  793. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  794. $this->assertInstanceOf(Route::class, $all[0]);
  795. }
  796. /**
  797. * Test connecting fallback routes with specific route class
  798. *
  799. * @return void
  800. */
  801. public function testFallbacksWithClass()
  802. {
  803. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  804. $routes->fallbacks('InflectedRoute');
  805. $all = $this->collection->routes();
  806. $this->assertEquals('/api/:controller', $all[0]->template);
  807. $this->assertEquals('/api/:controller/:action/*', $all[1]->template);
  808. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  809. }
  810. /**
  811. * Test connecting fallback routes after setting default route class.
  812. *
  813. * @return void
  814. */
  815. public function testDefaultRouteClassFallbacks()
  816. {
  817. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  818. $routes->setRouteClass('TestApp\Routing\Route\DashedRoute');
  819. $routes->fallbacks();
  820. $all = $this->collection->routes();
  821. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  822. }
  823. /**
  824. * Test adding a scope.
  825. *
  826. * @return void
  827. */
  828. public function testScope()
  829. {
  830. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  831. $routes->scope('/v1', ['version' => 1], function ($routes) {
  832. $this->assertEquals('/api/v1', $routes->path());
  833. $this->assertEquals(['prefix' => 'api', 'version' => 1], $routes->params());
  834. });
  835. }
  836. /**
  837. * Test that nested scopes inherit middleware.
  838. *
  839. * @return void
  840. */
  841. public function testScopeInheritMiddleware()
  842. {
  843. $routes = new RouteBuilder(
  844. $this->collection,
  845. '/api',
  846. ['prefix' => 'api'],
  847. ['middleware' => ['auth']]
  848. );
  849. $routes->scope('/v1', function ($routes) {
  850. $this->assertAttributeEquals(['auth'], 'middleware', $routes, 'Should inherit middleware');
  851. $this->assertEquals('/api/v1', $routes->path());
  852. $this->assertEquals(['prefix' => 'api'], $routes->params());
  853. });
  854. }
  855. /**
  856. * Test using name prefixes.
  857. *
  858. * @return void
  859. */
  860. public function testNamePrefixes()
  861. {
  862. $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
  863. $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function ($routes) {
  864. $this->assertEquals('api:v1:', $routes->namePrefix());
  865. $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
  866. $routes->namePrefix('web:');
  867. $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
  868. });
  869. $all = $this->collection->named();
  870. $this->assertArrayHasKey('api:v1:ping', $all);
  871. $this->assertArrayHasKey('web:pong', $all);
  872. }
  873. /**
  874. * Test adding middleware to the collection.
  875. *
  876. * @return void
  877. */
  878. public function testRegisterMiddleware()
  879. {
  880. $func = function () {
  881. };
  882. $routes = new RouteBuilder($this->collection, '/api');
  883. $result = $routes->registerMiddleware('test', $func);
  884. $this->assertSame($result, $routes);
  885. $this->assertTrue($this->collection->hasMiddleware('test'));
  886. $this->assertTrue($this->collection->middlewareExists('test'));
  887. }
  888. /**
  889. * Test middleware group
  890. *
  891. * @return void
  892. */
  893. public function testMiddlewareGroup()
  894. {
  895. $func = function () {
  896. };
  897. $routes = new RouteBuilder($this->collection, '/api');
  898. $routes->registerMiddleware('test', $func);
  899. $routes->registerMiddleware('test_two', $func);
  900. $result = $routes->middlewareGroup('group', ['test', 'test_two']);
  901. $this->assertSame($result, $routes);
  902. $this->assertTrue($this->collection->hasMiddlewareGroup('group'));
  903. $this->assertTrue($this->collection->middlewareExists('group'));
  904. }
  905. /**
  906. * Test overlap between middleware name and group name
  907. *
  908. * @return void
  909. */
  910. public function testMiddlewareGroupOverlap()
  911. {
  912. $this->expectException(\RuntimeException::class);
  913. $this->expectExceptionMessage('Cannot add middleware group \'test\'. A middleware by this name has already been registered.');
  914. $func = function () {
  915. };
  916. $routes = new RouteBuilder($this->collection, '/api');
  917. $routes->registerMiddleware('test', $func);
  918. $result = $routes->middlewareGroup('test', ['test']);
  919. }
  920. /**
  921. * Test applying middleware to a scope when it doesn't exist
  922. *
  923. * @return void
  924. */
  925. public function testApplyMiddlewareInvalidName()
  926. {
  927. $this->expectException(\RuntimeException::class);
  928. $this->expectExceptionMessage('Cannot apply \'bad\' middleware or middleware group. Use registerMiddleware() to register middleware');
  929. $routes = new RouteBuilder($this->collection, '/api');
  930. $routes->applyMiddleware('bad');
  931. }
  932. /**
  933. * Test applying middleware to a scope
  934. *
  935. * @return void
  936. */
  937. public function testApplyMiddleware()
  938. {
  939. $func = function () {
  940. };
  941. $routes = new RouteBuilder($this->collection, '/api');
  942. $routes->registerMiddleware('test', $func)
  943. ->registerMiddleware('test2', $func);
  944. $result = $routes->applyMiddleware('test', 'test2');
  945. $this->assertSame($result, $routes);
  946. }
  947. /**
  948. * Test that applyMiddleware() merges with previous data.
  949. *
  950. * @return void
  951. */
  952. public function testApplyMiddlewareMerges()
  953. {
  954. $func = function () {
  955. };
  956. $routes = new RouteBuilder($this->collection, '/api');
  957. $routes->registerMiddleware('test', $func)
  958. ->registerMiddleware('test2', $func);
  959. $routes->applyMiddleware('test');
  960. $routes->applyMiddleware('test2');
  961. $this->assertAttributeEquals(['test', 'test2'], 'middleware', $routes);
  962. }
  963. /**
  964. * Test applying middleware results in middleware attached to the route.
  965. *
  966. * @return void
  967. */
  968. public function testApplyMiddlewareAttachToRoutes()
  969. {
  970. $func = function () {
  971. };
  972. $routes = new RouteBuilder($this->collection, '/api');
  973. $routes->registerMiddleware('test', $func)
  974. ->registerMiddleware('test2', $func);
  975. $routes->applyMiddleware('test', 'test2');
  976. $route = $routes->get('/docs', ['controller' => 'Docs']);
  977. $this->assertSame(['test', 'test2'], $route->getMiddleware());
  978. }
  979. /**
  980. * @return array
  981. */
  982. public static function httpMethodProvider()
  983. {
  984. return [
  985. ['GET'],
  986. ['POST'],
  987. ['PUT'],
  988. ['PATCH'],
  989. ['DELETE'],
  990. ['OPTIONS'],
  991. ['HEAD'],
  992. ];
  993. }
  994. /**
  995. * Test that the HTTP method helpers create the right kind of routes.
  996. *
  997. * @dataProvider httpMethodProvider
  998. * @return void
  999. */
  1000. public function testHttpMethods($method)
  1001. {
  1002. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  1003. $route = $routes->{strtolower($method)}(
  1004. '/bookmarks/:id',
  1005. ['controller' => 'Bookmarks', 'action' => 'view'],
  1006. 'route-name'
  1007. );
  1008. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  1009. $this->assertSame($method, $route->defaults['_method']);
  1010. $this->assertSame('app:route-name', $route->options['_name']);
  1011. $this->assertSame('/bookmarks/:id', $route->template);
  1012. $this->assertEquals(
  1013. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  1014. $route->defaults
  1015. );
  1016. }
  1017. /**
  1018. * Test that the HTTP method helpers create the right kind of routes.
  1019. *
  1020. * @dataProvider httpMethodProvider
  1021. * @return void
  1022. */
  1023. public function testHttpMethodsStringTarget($method)
  1024. {
  1025. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  1026. $route = $routes->{strtolower($method)}(
  1027. '/bookmarks/:id',
  1028. 'Bookmarks::view',
  1029. 'route-name'
  1030. );
  1031. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  1032. $this->assertSame($method, $route->defaults['_method']);
  1033. $this->assertSame('app:route-name', $route->options['_name']);
  1034. $this->assertSame('/bookmarks/:id', $route->template);
  1035. $this->assertEquals(
  1036. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  1037. $route->defaults
  1038. );
  1039. }
  1040. /**
  1041. * Integration test for http method helpers and route fluent method
  1042. *
  1043. * @return void
  1044. */
  1045. public function testHttpMethodIntegration()
  1046. {
  1047. $routes = new RouteBuilder($this->collection, '/');
  1048. $routes->scope('/', function ($routes) {
  1049. $routes->get('/faq/:page', ['controller' => 'Pages', 'action' => 'faq'], 'faq')
  1050. ->setPatterns(['page' => '[a-z0-9_]+'])
  1051. ->setHost('docs.example.com');
  1052. $routes->post('/articles/:id', ['controller' => 'Articles', 'action' => 'update'], 'article:update')
  1053. ->setPatterns(['id' => '[0-9]+'])
  1054. ->setPass(['id']);
  1055. });
  1056. $this->assertCount(2, $this->collection->routes());
  1057. $this->assertEquals(['faq', 'article:update'], array_keys($this->collection->named()));
  1058. $this->assertNotEmpty($this->collection->parse('/faq/things_you_know', 'GET'));
  1059. $result = $this->collection->parse('/articles/123', 'POST');
  1060. $this->assertEquals(['123'], $result['pass']);
  1061. }
  1062. /**
  1063. * Test loading routes from a missing plugin
  1064. *
  1065. * @return void
  1066. */
  1067. public function testLoadPluginBadPlugin()
  1068. {
  1069. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  1070. $routes = new RouteBuilder($this->collection, '/');
  1071. $routes->loadPlugin('Nope');
  1072. }
  1073. /**
  1074. * Test loading routes from a missing file
  1075. *
  1076. * @return void
  1077. */
  1078. public function testLoadPluginBadFile()
  1079. {
  1080. $this->deprecated(function () {
  1081. $this->expectException(\InvalidArgumentException::class);
  1082. $this->expectExceptionMessage('Cannot load routes for the plugin named TestPlugin.');
  1083. $this->loadPlugins('TestPlugin');
  1084. $routes = new RouteBuilder($this->collection, '/');
  1085. $routes->loadPlugin('TestPlugin', 'nope.php');
  1086. });
  1087. }
  1088. /**
  1089. * Test loading routes with success
  1090. *
  1091. * @return void
  1092. */
  1093. public function testLoadPlugin()
  1094. {
  1095. $this->loadPlugins('TestPlugin');
  1096. $routes = new RouteBuilder($this->collection, '/');
  1097. $routes->loadPlugin('TestPlugin');
  1098. $this->assertCount(1, $this->collection->routes());
  1099. $this->assertNotEmpty($this->collection->parse('/test_plugin', 'GET'));
  1100. $plugin = Plugin::getCollection()->get('TestPlugin');
  1101. $this->assertFalse($plugin->isEnabled('routes'), 'Hook should be disabled preventing duplicate routes');
  1102. }
  1103. /**
  1104. * Test routeClass() still works.
  1105. *
  1106. * @group deprecated
  1107. * @return void
  1108. */
  1109. public function testRouteClassBackwardCompat()
  1110. {
  1111. $this->deprecated(function () {
  1112. $routes = new RouteBuilder($this->collection, '/l');
  1113. $this->assertNull($routes->routeClass('TestApp\Routing\Route\DashedRoute'));
  1114. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->routeClass());
  1115. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->getRouteClass());
  1116. });
  1117. }
  1118. /**
  1119. * Test extensions() still works.
  1120. *
  1121. * @group deprecated
  1122. * @return void
  1123. */
  1124. public function testExtensionsBackwardCompat()
  1125. {
  1126. $this->deprecated(function () {
  1127. $routes = new RouteBuilder($this->collection, '/l');
  1128. $this->assertNull($routes->extensions(['html']));
  1129. $this->assertSame(['html'], $routes->extensions());
  1130. $this->assertSame(['html'], $routes->getExtensions());
  1131. $this->assertNull($routes->extensions('json'));
  1132. $this->assertSame(['json'], $routes->extensions());
  1133. $this->assertSame(['json'], $routes->getExtensions());
  1134. });
  1135. }
  1136. }