RouteBuilderTest.php 36 KB

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