RouteBuilderTest.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. $this->clearPlugins();
  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. $route = $routes->redirect('/old', '/forums');
  382. $this->assertInstanceOf(RedirectRoute::class, $route);
  383. $this->assertSame($route, $this->collection->routes()[2]);
  384. }
  385. /**
  386. * Test using a custom route class for redirect routes.
  387. *
  388. * @return void
  389. */
  390. public function testRedirectWithCustomRouteClass()
  391. {
  392. $routes = new RouteBuilder($this->collection, '/');
  393. $routes->redirect('/old', '/forums', ['status' => 301, 'routeClass' => 'InflectedRoute']);
  394. $route = $this->collection->routes()[0];
  395. $this->assertInstanceOf(InflectedRoute::class, $route);
  396. }
  397. /**
  398. * Test creating sub-scopes with prefix()
  399. *
  400. * @return void
  401. */
  402. public function testPrefix()
  403. {
  404. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  405. $res = $routes->prefix('admin', ['param' => 'value'], function ($r) {
  406. $this->assertInstanceOf(RouteBuilder::class, $r);
  407. $this->assertCount(0, $this->collection->routes());
  408. $this->assertEquals('/path/admin', $r->path());
  409. $this->assertEquals(['prefix' => 'admin', 'key' => 'value', 'param' => 'value'], $r->params());
  410. });
  411. $this->assertNull($res);
  412. }
  413. /**
  414. * Test creating sub-scopes with prefix()
  415. *
  416. * @return void
  417. */
  418. public function testPrefixWithNoParams()
  419. {
  420. $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
  421. $res = $routes->prefix('admin', function ($r) {
  422. $this->assertInstanceOf(RouteBuilder::class, $r);
  423. $this->assertCount(0, $this->collection->routes());
  424. $this->assertEquals('/path/admin', $r->path());
  425. $this->assertEquals(['prefix' => 'admin', 'key' => 'value'], $r->params());
  426. });
  427. $this->assertNull($res);
  428. }
  429. /**
  430. * Test creating sub-scopes with prefix()
  431. *
  432. * @return void
  433. */
  434. public function testNestedPrefix()
  435. {
  436. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  437. $res = $routes->prefix('api', ['_namePrefix' => 'api:'], function ($r) {
  438. $this->assertEquals('/admin/api', $r->path());
  439. $this->assertEquals(['prefix' => 'admin/api'], $r->params());
  440. $this->assertEquals('api:', $r->namePrefix());
  441. });
  442. $this->assertNull($res);
  443. }
  444. /**
  445. * Test creating sub-scopes with prefix()
  446. *
  447. * @return void
  448. */
  449. public function testPathWithDotInPrefix()
  450. {
  451. $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
  452. $res = $routes->prefix('api', function ($r) {
  453. $r->prefix('v10', ['path' => '/v1.0'], function ($r2) {
  454. $this->assertEquals('/admin/api/v1.0', $r2->path());
  455. $this->assertEquals(['prefix' => 'admin/api/v10'], $r2->params());
  456. $r2->prefix('b1', ['path' => '/beta.1'], function ($r3) {
  457. $this->assertEquals('/admin/api/v1.0/beta.1', $r3->path());
  458. $this->assertEquals(['prefix' => 'admin/api/v10/b1'], $r3->params());
  459. });
  460. });
  461. });
  462. $this->assertNull($res);
  463. }
  464. /**
  465. * Test creating sub-scopes with plugin()
  466. *
  467. * @return void
  468. */
  469. public function testNestedPlugin()
  470. {
  471. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  472. $res = $routes->plugin('Contacts', function ($r) {
  473. $this->assertEquals('/b/contacts', $r->path());
  474. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  475. $r->connect('/:controller');
  476. $route = $this->collection->routes()[0];
  477. $this->assertEquals(
  478. ['key' => 'value', 'plugin' => 'Contacts', 'action' => 'index'],
  479. $route->defaults
  480. );
  481. });
  482. $this->assertNull($res);
  483. }
  484. /**
  485. * Test creating sub-scopes with plugin() + path option
  486. *
  487. * @return void
  488. */
  489. public function testNestedPluginPathOption()
  490. {
  491. $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
  492. $routes->plugin('Contacts', ['path' => '/people'], function ($r) {
  493. $this->assertEquals('/b/people', $r->path());
  494. $this->assertEquals(['plugin' => 'Contacts', 'key' => 'value'], $r->params());
  495. });
  496. }
  497. /**
  498. * Test connecting resources.
  499. *
  500. * @return void
  501. */
  502. public function testResources()
  503. {
  504. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  505. $routes->resources('Articles', ['_ext' => 'json']);
  506. $all = $this->collection->routes();
  507. $this->assertCount(5, $all);
  508. $this->assertEquals('/api/articles', $all[0]->template);
  509. $this->assertEquals(
  510. ['controller', 'action', '_method', 'prefix', 'plugin'],
  511. array_keys($all[0]->defaults)
  512. );
  513. $this->assertEquals('json', $all[0]->options['_ext']);
  514. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  515. }
  516. /**
  517. * Test connecting resources with a path
  518. *
  519. * @return void
  520. */
  521. public function testResourcesPathOption()
  522. {
  523. $routes = new RouteBuilder($this->collection, '/api');
  524. $routes->resources('Articles', ['path' => 'posts'], function (RouteBuilder $routes) {
  525. $routes->resources('Comments');
  526. });
  527. $all = $this->collection->routes();
  528. $this->assertEquals('Articles', $all[0]->defaults['controller']);
  529. $this->assertEquals('/api/posts', $all[0]->template);
  530. $this->assertEquals('/api/posts/:id', $all[2]->template);
  531. $this->assertEquals(
  532. '/api/posts/:article_id/comments',
  533. $all[6]->template,
  534. 'parameter name should reflect resource name'
  535. );
  536. }
  537. /**
  538. * Test connecting resources with a prefix
  539. *
  540. * @return void
  541. */
  542. public function testResourcesPrefix()
  543. {
  544. $routes = new RouteBuilder($this->collection, '/api');
  545. $routes->resources('Articles', ['prefix' => 'rest']);
  546. $all = $this->collection->routes();
  547. $this->assertEquals('rest', $all[0]->defaults['prefix']);
  548. }
  549. /**
  550. * Test that resource prefixes work within a prefixed scope.
  551. *
  552. * @return void
  553. */
  554. public function testResourcesNestedPrefix()
  555. {
  556. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  557. $routes->resources('Articles', ['prefix' => 'rest']);
  558. $all = $this->collection->routes();
  559. $this->assertCount(5, $all);
  560. $this->assertEquals('/api/articles', $all[0]->template);
  561. foreach ($all as $route) {
  562. $this->assertEquals('api/rest', $route->defaults['prefix']);
  563. $this->assertEquals('Articles', $route->defaults['controller']);
  564. }
  565. }
  566. /**
  567. * Test connecting resources with the inflection option
  568. *
  569. * @return void
  570. */
  571. public function testResourcesInflection()
  572. {
  573. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  574. $routes->resources('BlogPosts', ['_ext' => 'json', 'inflect' => 'dasherize']);
  575. $all = $this->collection->routes();
  576. $this->assertCount(5, $all);
  577. $this->assertEquals('/api/blog-posts', $all[0]->template);
  578. $this->assertEquals(
  579. ['controller', 'action', '_method', 'prefix', 'plugin'],
  580. array_keys($all[0]->defaults)
  581. );
  582. $this->assertEquals('BlogPosts', $all[0]->defaults['controller']);
  583. }
  584. /**
  585. * Test connecting nested resources with the inflection option
  586. *
  587. * @return void
  588. */
  589. public function testResourcesNestedInflection()
  590. {
  591. $routes = new RouteBuilder($this->collection, '/api');
  592. $routes->resources(
  593. 'NetworkObjects',
  594. ['inflect' => 'dasherize'],
  595. function (RouteBuilder $routes) {
  596. $routes->resources('Attributes');
  597. }
  598. );
  599. $all = $this->collection->routes();
  600. $this->assertCount(10, $all);
  601. $this->assertEquals('/api/network-objects', $all[0]->template);
  602. $this->assertEquals('/api/network-objects/:id', $all[2]->template);
  603. $this->assertEquals('/api/network-objects/:network_object_id/attributes', $all[5]->template);
  604. }
  605. /**
  606. * Test connecting resources with additional mappings
  607. *
  608. * @return void
  609. */
  610. public function testResourcesMappings()
  611. {
  612. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  613. $routes->resources('Articles', [
  614. '_ext' => 'json',
  615. 'map' => [
  616. 'delete_all' => ['action' => 'deleteAll', 'method' => 'DELETE'],
  617. 'update_many' => ['action' => 'updateAll', 'method' => 'DELETE', 'path' => '/updateAll'],
  618. ],
  619. ]);
  620. $all = $this->collection->routes();
  621. $this->assertCount(7, $all);
  622. $this->assertEquals('/api/articles/delete_all', $all[5]->template, 'Path defaults to key name.');
  623. $this->assertEquals(
  624. ['controller', 'action', '_method', 'prefix', 'plugin'],
  625. array_keys($all[5]->defaults)
  626. );
  627. $this->assertEquals('Articles', $all[5]->defaults['controller']);
  628. $this->assertEquals('deleteAll', $all[5]->defaults['action']);
  629. $this->assertEquals('/api/articles/updateAll', $all[6]->template, 'Explicit path option');
  630. $this->assertEquals(
  631. ['controller', 'action', '_method', 'prefix', 'plugin'],
  632. array_keys($all[6]->defaults)
  633. );
  634. $this->assertEquals('Articles', $all[6]->defaults['controller']);
  635. $this->assertEquals('updateAll', $all[6]->defaults['action']);
  636. }
  637. /**
  638. * Test connecting resources.
  639. *
  640. * @return void
  641. */
  642. public function testResourcesInScope()
  643. {
  644. Router::scope('/api', ['prefix' => 'api'], function (RouteBuilder $routes) {
  645. $routes->setExtensions(['json']);
  646. $routes->resources('Articles');
  647. });
  648. $url = Router::url([
  649. 'prefix' => 'api',
  650. 'controller' => 'Articles',
  651. 'action' => 'edit',
  652. '_method' => 'PUT',
  653. 'id' => 99,
  654. ]);
  655. $this->assertEquals('/api/articles/99', $url);
  656. $url = Router::url([
  657. 'prefix' => 'api',
  658. 'controller' => 'Articles',
  659. 'action' => 'edit',
  660. '_method' => 'PUT',
  661. '_ext' => 'json',
  662. 'id' => 99,
  663. ]);
  664. $this->assertEquals('/api/articles/99.json', $url);
  665. }
  666. /**
  667. * Test resource parsing.
  668. *
  669. * @group deprecated
  670. * @return void
  671. */
  672. public function testResourcesParsingReadGlobals()
  673. {
  674. $this->deprecated(function () {
  675. $routes = new RouteBuilder($this->collection, '/');
  676. $routes->resources('Articles');
  677. $_SERVER['REQUEST_METHOD'] = 'GET';
  678. $result = $this->collection->parse('/articles');
  679. $this->assertEquals('Articles', $result['controller']);
  680. $this->assertEquals('index', $result['action']);
  681. $this->assertEquals([], $result['pass']);
  682. $_SERVER['REQUEST_METHOD'] = 'POST';
  683. $result = $this->collection->parse('/articles');
  684. $this->assertEquals('Articles', $result['controller']);
  685. $this->assertEquals('add', $result['action']);
  686. $this->assertEquals([], $result['pass']);
  687. });
  688. }
  689. /**
  690. * Test resource parsing.
  691. *
  692. * @return void
  693. */
  694. public function testResourcesParsing()
  695. {
  696. $routes = new RouteBuilder($this->collection, '/');
  697. $routes->resources('Articles');
  698. $result = $this->collection->parse('/articles', 'GET');
  699. $this->assertEquals('Articles', $result['controller']);
  700. $this->assertEquals('index', $result['action']);
  701. $this->assertEquals([], $result['pass']);
  702. $result = $this->collection->parse('/articles/1', 'GET');
  703. $this->assertEquals('Articles', $result['controller']);
  704. $this->assertEquals('view', $result['action']);
  705. $this->assertEquals([1], $result['pass']);
  706. $result = $this->collection->parse('/articles', 'POST');
  707. $this->assertEquals('Articles', $result['controller']);
  708. $this->assertEquals('add', $result['action']);
  709. $this->assertEquals([], $result['pass']);
  710. $result = $this->collection->parse('/articles/1', 'PUT');
  711. $this->assertEquals('Articles', $result['controller']);
  712. $this->assertEquals('edit', $result['action']);
  713. $this->assertEquals([1], $result['pass']);
  714. $result = $this->collection->parse('/articles/1', 'DELETE');
  715. $this->assertEquals('Articles', $result['controller']);
  716. $this->assertEquals('delete', $result['action']);
  717. $this->assertEquals([1], $result['pass']);
  718. }
  719. /**
  720. * Test the only option of RouteBuilder.
  721. *
  722. * @return void
  723. */
  724. public function testResourcesOnlyString()
  725. {
  726. $routes = new RouteBuilder($this->collection, '/');
  727. $routes->resources('Articles', ['only' => 'index']);
  728. $result = $this->collection->routes();
  729. $this->assertCount(1, $result);
  730. $this->assertEquals('/articles', $result[0]->template);
  731. }
  732. /**
  733. * Test the only option of RouteBuilder.
  734. *
  735. * @return void
  736. */
  737. public function testResourcesOnlyArray()
  738. {
  739. $routes = new RouteBuilder($this->collection, '/');
  740. $routes->resources('Articles', ['only' => ['index', 'delete']]);
  741. $result = $this->collection->routes();
  742. $this->assertCount(2, $result);
  743. $this->assertEquals('/articles', $result[0]->template);
  744. $this->assertEquals('index', $result[0]->defaults['action']);
  745. $this->assertEquals('GET', $result[0]->defaults['_method']);
  746. $this->assertEquals('/articles/:id', $result[1]->template);
  747. $this->assertEquals('delete', $result[1]->defaults['action']);
  748. $this->assertEquals('DELETE', $result[1]->defaults['_method']);
  749. }
  750. /**
  751. * Test the actions option of RouteBuilder.
  752. *
  753. * @return void
  754. */
  755. public function testResourcesActions()
  756. {
  757. $routes = new RouteBuilder($this->collection, '/');
  758. $routes->resources('Articles', [
  759. 'only' => ['index', 'delete'],
  760. 'actions' => ['index' => 'showList'],
  761. ]);
  762. $result = $this->collection->routes();
  763. $this->assertCount(2, $result);
  764. $this->assertEquals('/articles', $result[0]->template);
  765. $this->assertEquals('showList', $result[0]->defaults['action']);
  766. $this->assertEquals('/articles/:id', $result[1]->template);
  767. $this->assertEquals('delete', $result[1]->defaults['action']);
  768. }
  769. /**
  770. * Test nesting resources
  771. *
  772. * @return void
  773. */
  774. public function testResourcesNested()
  775. {
  776. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  777. $routes->resources('Articles', function (RouteBuilder $routes) {
  778. $this->assertEquals('/api/articles/', $routes->path());
  779. $this->assertEquals(['prefix' => 'api'], $routes->params());
  780. $routes->resources('Comments');
  781. $route = $this->collection->routes()[6];
  782. $this->assertEquals('/api/articles/:article_id/comments', $route->template);
  783. });
  784. }
  785. /**
  786. * Test connecting fallback routes.
  787. *
  788. * @return void
  789. */
  790. public function testFallbacks()
  791. {
  792. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  793. $routes->fallbacks();
  794. $all = $this->collection->routes();
  795. $this->assertSame('/api/{controller}', $all[0]->template);
  796. $this->assertSame('/api/{controller}/{action}/*', $all[1]->template);
  797. $this->assertInstanceOf(Route::class, $all[0]);
  798. }
  799. /**
  800. * Test connecting fallback routes with specific route class
  801. *
  802. * @return void
  803. */
  804. public function testFallbacksWithClass()
  805. {
  806. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  807. $routes->fallbacks('InflectedRoute');
  808. $all = $this->collection->routes();
  809. $this->assertSame('/api/{controller}', $all[0]->template);
  810. $this->assertSame('/api/{controller}/{action}/*', $all[1]->template);
  811. $this->assertInstanceOf(InflectedRoute::class, $all[0]);
  812. }
  813. /**
  814. * Test connecting fallback routes after setting default route class.
  815. *
  816. * @return void
  817. */
  818. public function testDefaultRouteClassFallbacks()
  819. {
  820. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  821. $routes->setRouteClass('TestApp\Routing\Route\DashedRoute');
  822. $routes->fallbacks();
  823. $all = $this->collection->routes();
  824. $this->assertInstanceOf('TestApp\Routing\Route\DashedRoute', $all[0]);
  825. }
  826. /**
  827. * Test adding a scope.
  828. *
  829. * @return void
  830. */
  831. public function testScope()
  832. {
  833. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  834. $routes->scope('/v1', ['version' => 1], function (RouteBuilder $routes) {
  835. $this->assertEquals('/api/v1', $routes->path());
  836. $this->assertEquals(['prefix' => 'api', 'version' => 1], $routes->params());
  837. });
  838. }
  839. /**
  840. * Test adding a scope with action in the scope
  841. *
  842. * @return void
  843. */
  844. public function testScopeWithAction()
  845. {
  846. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  847. $routes->scope('/prices', ['controller' => 'Prices', 'action' => 'view'], function (RouteBuilder $routes) {
  848. $routes->connect('/shared', ['shared' => true]);
  849. $routes->get('/exclusive', ['exclusive' => true]);
  850. });
  851. $all = $this->collection->routes();
  852. $this->assertCount(2, $all);
  853. $this->assertSame('view', $all[0]->defaults['action']);
  854. $this->assertArrayHasKey('shared', $all[0]->defaults);
  855. $this->assertSame('view', $all[1]->defaults['action']);
  856. $this->assertArrayHasKey('exclusive', $all[1]->defaults);
  857. }
  858. /**
  859. * Test that nested scopes inherit middleware.
  860. *
  861. * @return void
  862. */
  863. public function testScopeInheritMiddleware()
  864. {
  865. $routes = new RouteBuilder(
  866. $this->collection,
  867. '/api',
  868. ['prefix' => 'api'],
  869. ['middleware' => ['auth']]
  870. );
  871. $routes->scope('/v1', function (RouteBuilder $routes) {
  872. $this->assertAttributeEquals(['auth'], 'middleware', $routes, 'Should inherit middleware');
  873. $this->assertEquals('/api/v1', $routes->path());
  874. $this->assertEquals(['prefix' => 'api'], $routes->params());
  875. });
  876. }
  877. /**
  878. * Test using name prefixes.
  879. *
  880. * @return void
  881. */
  882. public function testNamePrefixes()
  883. {
  884. $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
  885. $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function (RouteBuilder $routes) {
  886. $this->assertEquals('api:v1:', $routes->namePrefix());
  887. $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
  888. $routes->namePrefix('web:');
  889. $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
  890. });
  891. $all = $this->collection->named();
  892. $this->assertArrayHasKey('api:v1:ping', $all);
  893. $this->assertArrayHasKey('web:pong', $all);
  894. }
  895. /**
  896. * Test adding middleware to the collection.
  897. *
  898. * @return void
  899. */
  900. public function testRegisterMiddleware()
  901. {
  902. $func = function () {
  903. };
  904. $routes = new RouteBuilder($this->collection, '/api');
  905. $result = $routes->registerMiddleware('test', $func);
  906. $this->assertSame($result, $routes);
  907. $this->assertTrue($this->collection->hasMiddleware('test'));
  908. $this->assertTrue($this->collection->middlewareExists('test'));
  909. }
  910. /**
  911. * Test middleware group
  912. *
  913. * @return void
  914. */
  915. public function testMiddlewareGroup()
  916. {
  917. $func = function () {
  918. };
  919. $routes = new RouteBuilder($this->collection, '/api');
  920. $routes->registerMiddleware('test', $func);
  921. $routes->registerMiddleware('test_two', $func);
  922. $result = $routes->middlewareGroup('group', ['test', 'test_two']);
  923. $this->assertSame($result, $routes);
  924. $this->assertTrue($this->collection->hasMiddlewareGroup('group'));
  925. $this->assertTrue($this->collection->middlewareExists('group'));
  926. }
  927. /**
  928. * Test overlap between middleware name and group name
  929. *
  930. * @return void
  931. */
  932. public function testMiddlewareGroupOverlap()
  933. {
  934. $this->expectException(\RuntimeException::class);
  935. $this->expectExceptionMessage('Cannot add middleware group \'test\'. A middleware by this name has already been registered.');
  936. $func = function () {
  937. };
  938. $routes = new RouteBuilder($this->collection, '/api');
  939. $routes->registerMiddleware('test', $func);
  940. $result = $routes->middlewareGroup('test', ['test']);
  941. }
  942. /**
  943. * Test applying middleware to a scope when it doesn't exist
  944. *
  945. * @return void
  946. */
  947. public function testApplyMiddlewareInvalidName()
  948. {
  949. $this->expectException(\RuntimeException::class);
  950. $this->expectExceptionMessage('Cannot apply \'bad\' middleware or middleware group. Use registerMiddleware() to register middleware');
  951. $routes = new RouteBuilder($this->collection, '/api');
  952. $routes->applyMiddleware('bad');
  953. }
  954. /**
  955. * Test applying middleware to a scope
  956. *
  957. * @return void
  958. */
  959. public function testApplyMiddleware()
  960. {
  961. $func = function () {
  962. };
  963. $routes = new RouteBuilder($this->collection, '/api');
  964. $routes->registerMiddleware('test', $func)
  965. ->registerMiddleware('test2', $func);
  966. $result = $routes->applyMiddleware('test', 'test2');
  967. $this->assertSame($result, $routes);
  968. }
  969. /**
  970. * Test that applyMiddleware() merges with previous data.
  971. *
  972. * @return void
  973. */
  974. public function testApplyMiddlewareMerges()
  975. {
  976. $func = function () {
  977. };
  978. $routes = new RouteBuilder($this->collection, '/api');
  979. $routes->registerMiddleware('test', $func)
  980. ->registerMiddleware('test2', $func);
  981. $routes->applyMiddleware('test');
  982. $routes->applyMiddleware('test2');
  983. $this->assertAttributeEquals(['test', 'test2'], 'middleware', $routes);
  984. }
  985. /**
  986. * Test that applyMiddleware() uses unique middleware set
  987. *
  988. * @return void
  989. */
  990. public function testApplyMiddlewareUnique()
  991. {
  992. $func = function () {
  993. };
  994. $routes = new RouteBuilder($this->collection, '/api');
  995. $routes->registerMiddleware('test', $func)
  996. ->registerMiddleware('test2', $func);
  997. $routes->applyMiddleware('test', 'test2');
  998. $routes->applyMiddleware('test2', 'test');
  999. $this->assertAttributeEquals(['test', 'test2'], 'middleware', $routes);
  1000. }
  1001. /**
  1002. * Test applying middleware results in middleware attached to the route.
  1003. *
  1004. * @return void
  1005. */
  1006. public function testApplyMiddlewareAttachToRoutes()
  1007. {
  1008. $func = function () {
  1009. };
  1010. $routes = new RouteBuilder($this->collection, '/api');
  1011. $routes->registerMiddleware('test', $func)
  1012. ->registerMiddleware('test2', $func);
  1013. $routes->applyMiddleware('test', 'test2');
  1014. $route = $routes->get('/docs', ['controller' => 'Docs']);
  1015. $this->assertSame(['test', 'test2'], $route->getMiddleware());
  1016. }
  1017. /**
  1018. * @return array
  1019. */
  1020. public static function httpMethodProvider()
  1021. {
  1022. return [
  1023. ['GET'],
  1024. ['POST'],
  1025. ['PUT'],
  1026. ['PATCH'],
  1027. ['DELETE'],
  1028. ['OPTIONS'],
  1029. ['HEAD'],
  1030. ];
  1031. }
  1032. /**
  1033. * Test that the HTTP method helpers create the right kind of routes.
  1034. *
  1035. * @dataProvider httpMethodProvider
  1036. * @return void
  1037. */
  1038. public function testHttpMethods($method)
  1039. {
  1040. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  1041. $route = $routes->{strtolower($method)}(
  1042. '/bookmarks/:id',
  1043. ['controller' => 'Bookmarks', 'action' => 'view'],
  1044. 'route-name'
  1045. );
  1046. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  1047. $this->assertSame($method, $route->defaults['_method']);
  1048. $this->assertSame('app:route-name', $route->options['_name']);
  1049. $this->assertSame('/bookmarks/:id', $route->template);
  1050. $this->assertEquals(
  1051. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  1052. $route->defaults
  1053. );
  1054. }
  1055. /**
  1056. * Test that the HTTP method helpers create the right kind of routes.
  1057. *
  1058. * @dataProvider httpMethodProvider
  1059. * @return void
  1060. */
  1061. public function testHttpMethodsStringTarget($method)
  1062. {
  1063. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  1064. $route = $routes->{strtolower($method)}(
  1065. '/bookmarks/:id',
  1066. 'Bookmarks::view',
  1067. 'route-name'
  1068. );
  1069. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  1070. $this->assertSame($method, $route->defaults['_method']);
  1071. $this->assertSame('app:route-name', $route->options['_name']);
  1072. $this->assertSame('/bookmarks/:id', $route->template);
  1073. $this->assertEquals(
  1074. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  1075. $route->defaults
  1076. );
  1077. }
  1078. /**
  1079. * Integration test for http method helpers and route fluent method
  1080. *
  1081. * @return void
  1082. */
  1083. public function testHttpMethodIntegration()
  1084. {
  1085. $routes = new RouteBuilder($this->collection, '/');
  1086. $routes->scope('/', function (RouteBuilder $routes) {
  1087. $routes->get('/faq/:page', ['controller' => 'Pages', 'action' => 'faq'], 'faq')
  1088. ->setPatterns(['page' => '[a-z0-9_]+'])
  1089. ->setHost('docs.example.com');
  1090. $routes->post('/articles/:id', ['controller' => 'Articles', 'action' => 'update'], 'article:update')
  1091. ->setPatterns(['id' => '[0-9]+'])
  1092. ->setPass(['id']);
  1093. });
  1094. $this->assertCount(2, $this->collection->routes());
  1095. $this->assertEquals(['faq', 'article:update'], array_keys($this->collection->named()));
  1096. $this->assertNotEmpty($this->collection->parse('/faq/things_you_know', 'GET'));
  1097. $result = $this->collection->parse('/articles/123', 'POST');
  1098. $this->assertEquals(['123'], $result['pass']);
  1099. }
  1100. /**
  1101. * Test loading routes from a missing plugin
  1102. *
  1103. * @return void
  1104. */
  1105. public function testLoadPluginBadPlugin()
  1106. {
  1107. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  1108. $routes = new RouteBuilder($this->collection, '/');
  1109. $routes->loadPlugin('Nope');
  1110. }
  1111. /**
  1112. * Test loading routes from a missing file
  1113. *
  1114. * @return void
  1115. */
  1116. public function testLoadPluginBadFile()
  1117. {
  1118. $this->deprecated(function () {
  1119. $this->expectException(\InvalidArgumentException::class);
  1120. $this->expectExceptionMessage('Cannot load routes for the plugin named TestPlugin.');
  1121. $this->loadPlugins(['TestPlugin']);
  1122. $routes = new RouteBuilder($this->collection, '/');
  1123. $routes->loadPlugin('TestPlugin', 'nope.php');
  1124. });
  1125. }
  1126. /**
  1127. * Test loading routes with success
  1128. *
  1129. * @return void
  1130. */
  1131. public function testLoadPlugin()
  1132. {
  1133. $this->loadPlugins(['TestPlugin']);
  1134. $routes = new RouteBuilder($this->collection, '/');
  1135. $routes->loadPlugin('TestPlugin');
  1136. $this->assertCount(1, $this->collection->routes());
  1137. $this->assertNotEmpty($this->collection->parse('/test_plugin', 'GET'));
  1138. $plugin = Plugin::getCollection()->get('TestPlugin');
  1139. $this->assertFalse($plugin->isEnabled('routes'), 'Hook should be disabled preventing duplicate routes');
  1140. }
  1141. /**
  1142. * Test routeClass() still works.
  1143. *
  1144. * @group deprecated
  1145. * @return void
  1146. */
  1147. public function testRouteClassBackwardCompat()
  1148. {
  1149. $this->deprecated(function () {
  1150. $routes = new RouteBuilder($this->collection, '/l');
  1151. $this->assertNull($routes->routeClass('TestApp\Routing\Route\DashedRoute'));
  1152. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->routeClass());
  1153. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->getRouteClass());
  1154. });
  1155. }
  1156. /**
  1157. * Test extensions() still works.
  1158. *
  1159. * @group deprecated
  1160. * @return void
  1161. */
  1162. public function testExtensionsBackwardCompat()
  1163. {
  1164. $this->deprecated(function () {
  1165. $routes = new RouteBuilder($this->collection, '/l');
  1166. $this->assertNull($routes->extensions(['html']));
  1167. $this->assertSame(['html'], $routes->extensions());
  1168. $this->assertSame(['html'], $routes->getExtensions());
  1169. $this->assertNull($routes->extensions('json'));
  1170. $this->assertSame(['json'], $routes->extensions());
  1171. $this->assertSame(['json'], $routes->getExtensions());
  1172. });
  1173. }
  1174. }