RouteBuilderTest.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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('TestPlugin');
  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 (RouteBuilder $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 adding a scope with action in the scope
  838. *
  839. * @return void
  840. */
  841. public function testScopeWithAction()
  842. {
  843. $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
  844. $routes->scope('/prices', ['controller' => 'Prices', 'action' => 'view'], function ($routes) {
  845. $routes->connect('/shared', ['shared' => true]);
  846. $routes->get('/exclusive', ['exclusive' => true]);
  847. });
  848. $all = $this->collection->routes();
  849. $this->assertCount(2, $all);
  850. $this->assertSame('view', $all[0]->defaults['action']);
  851. $this->assertArrayHasKey('shared', $all[0]->defaults);
  852. $this->assertSame('view', $all[1]->defaults['action']);
  853. $this->assertArrayHasKey('exclusive', $all[1]->defaults);
  854. }
  855. /**
  856. * Test that nested scopes inherit middleware.
  857. *
  858. * @return void
  859. */
  860. public function testScopeInheritMiddleware()
  861. {
  862. $routes = new RouteBuilder(
  863. $this->collection,
  864. '/api',
  865. ['prefix' => 'api'],
  866. ['middleware' => ['auth']]
  867. );
  868. $routes->scope('/v1', function ($routes) {
  869. $this->assertAttributeEquals(['auth'], 'middleware', $routes, 'Should inherit middleware');
  870. $this->assertEquals('/api/v1', $routes->path());
  871. $this->assertEquals(['prefix' => 'api'], $routes->params());
  872. });
  873. }
  874. /**
  875. * Test using name prefixes.
  876. *
  877. * @return void
  878. */
  879. public function testNamePrefixes()
  880. {
  881. $routes = new RouteBuilder($this->collection, '/api', [], ['namePrefix' => 'api:']);
  882. $routes->scope('/v1', ['version' => 1, '_namePrefix' => 'v1:'], function ($routes) {
  883. $this->assertEquals('api:v1:', $routes->namePrefix());
  884. $routes->connect('/ping', ['controller' => 'Pings'], ['_name' => 'ping']);
  885. $routes->namePrefix('web:');
  886. $routes->connect('/pong', ['controller' => 'Pongs'], ['_name' => 'pong']);
  887. });
  888. $all = $this->collection->named();
  889. $this->assertArrayHasKey('api:v1:ping', $all);
  890. $this->assertArrayHasKey('web:pong', $all);
  891. }
  892. /**
  893. * Test adding middleware to the collection.
  894. *
  895. * @return void
  896. */
  897. public function testRegisterMiddleware()
  898. {
  899. $func = function () {
  900. };
  901. $routes = new RouteBuilder($this->collection, '/api');
  902. $result = $routes->registerMiddleware('test', $func);
  903. $this->assertSame($result, $routes);
  904. $this->assertTrue($this->collection->hasMiddleware('test'));
  905. $this->assertTrue($this->collection->middlewareExists('test'));
  906. }
  907. /**
  908. * Test middleware group
  909. *
  910. * @return void
  911. */
  912. public function testMiddlewareGroup()
  913. {
  914. $func = function () {
  915. };
  916. $routes = new RouteBuilder($this->collection, '/api');
  917. $routes->registerMiddleware('test', $func);
  918. $routes->registerMiddleware('test_two', $func);
  919. $result = $routes->middlewareGroup('group', ['test', 'test_two']);
  920. $this->assertSame($result, $routes);
  921. $this->assertTrue($this->collection->hasMiddlewareGroup('group'));
  922. $this->assertTrue($this->collection->middlewareExists('group'));
  923. }
  924. /**
  925. * Test overlap between middleware name and group name
  926. *
  927. * @return void
  928. */
  929. public function testMiddlewareGroupOverlap()
  930. {
  931. $this->expectException(\RuntimeException::class);
  932. $this->expectExceptionMessage('Cannot add middleware group \'test\'. A middleware by this name has already been registered.');
  933. $func = function () {
  934. };
  935. $routes = new RouteBuilder($this->collection, '/api');
  936. $routes->registerMiddleware('test', $func);
  937. $result = $routes->middlewareGroup('test', ['test']);
  938. }
  939. /**
  940. * Test applying middleware to a scope when it doesn't exist
  941. *
  942. * @return void
  943. */
  944. public function testApplyMiddlewareInvalidName()
  945. {
  946. $this->expectException(\RuntimeException::class);
  947. $this->expectExceptionMessage('Cannot apply \'bad\' middleware or middleware group. Use registerMiddleware() to register middleware');
  948. $routes = new RouteBuilder($this->collection, '/api');
  949. $routes->applyMiddleware('bad');
  950. }
  951. /**
  952. * Test applying middleware to a scope
  953. *
  954. * @return void
  955. */
  956. public function testApplyMiddleware()
  957. {
  958. $func = function () {
  959. };
  960. $routes = new RouteBuilder($this->collection, '/api');
  961. $routes->registerMiddleware('test', $func)
  962. ->registerMiddleware('test2', $func);
  963. $result = $routes->applyMiddleware('test', 'test2');
  964. $this->assertSame($result, $routes);
  965. }
  966. /**
  967. * Test that applyMiddleware() merges with previous data.
  968. *
  969. * @return void
  970. */
  971. public function testApplyMiddlewareMerges()
  972. {
  973. $func = function () {
  974. };
  975. $routes = new RouteBuilder($this->collection, '/api');
  976. $routes->registerMiddleware('test', $func)
  977. ->registerMiddleware('test2', $func);
  978. $routes->applyMiddleware('test');
  979. $routes->applyMiddleware('test2');
  980. $this->assertAttributeEquals(['test', 'test2'], 'middleware', $routes);
  981. }
  982. /**
  983. * Test that applyMiddleware() uses unique middleware set
  984. *
  985. * @return void
  986. */
  987. public function testApplyMiddlewareUnique()
  988. {
  989. $func = function () {
  990. };
  991. $routes = new RouteBuilder($this->collection, '/api');
  992. $routes->registerMiddleware('test', $func)
  993. ->registerMiddleware('test2', $func);
  994. $routes->applyMiddleware('test', 'test2');
  995. $routes->applyMiddleware('test2', 'test');
  996. $this->assertAttributeEquals(['test', 'test2'], 'middleware', $routes);
  997. }
  998. /**
  999. * Test applying middleware results in middleware attached to the route.
  1000. *
  1001. * @return void
  1002. */
  1003. public function testApplyMiddlewareAttachToRoutes()
  1004. {
  1005. $func = function () {
  1006. };
  1007. $routes = new RouteBuilder($this->collection, '/api');
  1008. $routes->registerMiddleware('test', $func)
  1009. ->registerMiddleware('test2', $func);
  1010. $routes->applyMiddleware('test', 'test2');
  1011. $route = $routes->get('/docs', ['controller' => 'Docs']);
  1012. $this->assertSame(['test', 'test2'], $route->getMiddleware());
  1013. }
  1014. /**
  1015. * @return array
  1016. */
  1017. public static function httpMethodProvider()
  1018. {
  1019. return [
  1020. ['GET'],
  1021. ['POST'],
  1022. ['PUT'],
  1023. ['PATCH'],
  1024. ['DELETE'],
  1025. ['OPTIONS'],
  1026. ['HEAD'],
  1027. ];
  1028. }
  1029. /**
  1030. * Test that the HTTP method helpers create the right kind of routes.
  1031. *
  1032. * @dataProvider httpMethodProvider
  1033. * @return void
  1034. */
  1035. public function testHttpMethods($method)
  1036. {
  1037. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  1038. $route = $routes->{strtolower($method)}(
  1039. '/bookmarks/:id',
  1040. ['controller' => 'Bookmarks', 'action' => 'view'],
  1041. 'route-name'
  1042. );
  1043. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  1044. $this->assertSame($method, $route->defaults['_method']);
  1045. $this->assertSame('app:route-name', $route->options['_name']);
  1046. $this->assertSame('/bookmarks/:id', $route->template);
  1047. $this->assertEquals(
  1048. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  1049. $route->defaults
  1050. );
  1051. }
  1052. /**
  1053. * Test that the HTTP method helpers create the right kind of routes.
  1054. *
  1055. * @dataProvider httpMethodProvider
  1056. * @return void
  1057. */
  1058. public function testHttpMethodsStringTarget($method)
  1059. {
  1060. $routes = new RouteBuilder($this->collection, '/', [], ['namePrefix' => 'app:']);
  1061. $route = $routes->{strtolower($method)}(
  1062. '/bookmarks/:id',
  1063. 'Bookmarks::view',
  1064. 'route-name'
  1065. );
  1066. $this->assertInstanceOf(Route::class, $route, 'Should return a route');
  1067. $this->assertSame($method, $route->defaults['_method']);
  1068. $this->assertSame('app:route-name', $route->options['_name']);
  1069. $this->assertSame('/bookmarks/:id', $route->template);
  1070. $this->assertEquals(
  1071. ['plugin' => null, 'controller' => 'Bookmarks', 'action' => 'view', '_method' => $method],
  1072. $route->defaults
  1073. );
  1074. }
  1075. /**
  1076. * Integration test for http method helpers and route fluent method
  1077. *
  1078. * @return void
  1079. */
  1080. public function testHttpMethodIntegration()
  1081. {
  1082. $routes = new RouteBuilder($this->collection, '/');
  1083. $routes->scope('/', function ($routes) {
  1084. $routes->get('/faq/:page', ['controller' => 'Pages', 'action' => 'faq'], 'faq')
  1085. ->setPatterns(['page' => '[a-z0-9_]+'])
  1086. ->setHost('docs.example.com');
  1087. $routes->post('/articles/:id', ['controller' => 'Articles', 'action' => 'update'], 'article:update')
  1088. ->setPatterns(['id' => '[0-9]+'])
  1089. ->setPass(['id']);
  1090. });
  1091. $this->assertCount(2, $this->collection->routes());
  1092. $this->assertEquals(['faq', 'article:update'], array_keys($this->collection->named()));
  1093. $this->assertNotEmpty($this->collection->parse('/faq/things_you_know', 'GET'));
  1094. $result = $this->collection->parse('/articles/123', 'POST');
  1095. $this->assertEquals(['123'], $result['pass']);
  1096. }
  1097. /**
  1098. * Test loading routes from a missing plugin
  1099. *
  1100. * @return void
  1101. */
  1102. public function testLoadPluginBadPlugin()
  1103. {
  1104. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  1105. $routes = new RouteBuilder($this->collection, '/');
  1106. $routes->loadPlugin('Nope');
  1107. }
  1108. /**
  1109. * Test loading routes from a missing file
  1110. *
  1111. * @return void
  1112. */
  1113. public function testLoadPluginBadFile()
  1114. {
  1115. $this->deprecated(function () {
  1116. $this->expectException(\InvalidArgumentException::class);
  1117. $this->expectExceptionMessage('Cannot load routes for the plugin named TestPlugin.');
  1118. Plugin::load('TestPlugin');
  1119. $routes = new RouteBuilder($this->collection, '/');
  1120. $routes->loadPlugin('TestPlugin', 'nope.php');
  1121. });
  1122. }
  1123. /**
  1124. * Test loading routes with success
  1125. *
  1126. * @return void
  1127. */
  1128. public function testLoadPlugin()
  1129. {
  1130. Plugin::load('TestPlugin');
  1131. $routes = new RouteBuilder($this->collection, '/');
  1132. $routes->loadPlugin('TestPlugin');
  1133. $this->assertCount(1, $this->collection->routes());
  1134. $this->assertNotEmpty($this->collection->parse('/test_plugin', 'GET'));
  1135. $plugin = Plugin::getCollection()->get('TestPlugin');
  1136. $this->assertFalse($plugin->isEnabled('routes'), 'Hook should be disabled preventing duplicate routes');
  1137. }
  1138. /**
  1139. * Test routeClass() still works.
  1140. *
  1141. * @group deprecated
  1142. * @return void
  1143. */
  1144. public function testRouteClassBackwardCompat()
  1145. {
  1146. $this->deprecated(function () {
  1147. $routes = new RouteBuilder($this->collection, '/l');
  1148. $this->assertNull($routes->routeClass('TestApp\Routing\Route\DashedRoute'));
  1149. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->routeClass());
  1150. $this->assertSame('TestApp\Routing\Route\DashedRoute', $routes->getRouteClass());
  1151. });
  1152. }
  1153. /**
  1154. * Test extensions() still works.
  1155. *
  1156. * @group deprecated
  1157. * @return void
  1158. */
  1159. public function testExtensionsBackwardCompat()
  1160. {
  1161. $this->deprecated(function () {
  1162. $routes = new RouteBuilder($this->collection, '/l');
  1163. $this->assertNull($routes->extensions(['html']));
  1164. $this->assertSame(['html'], $routes->extensions());
  1165. $this->assertSame(['html'], $routes->getExtensions());
  1166. $this->assertNull($routes->extensions('json'));
  1167. $this->assertSame(['json'], $routes->extensions());
  1168. $this->assertSame(['json'], $routes->getExtensions());
  1169. });
  1170. }
  1171. }