InflectedRouteTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.2.1
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Routing\Route;
  16. use Cake\Routing\Router;
  17. use Cake\Routing\Route\InflectedRoute;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Test case for InflectedRoute
  21. */
  22. class InflectedRouteTest extends TestCase
  23. {
  24. /**
  25. * test that routes match their pattern.
  26. *
  27. * @return void
  28. */
  29. public function testMatchBasic()
  30. {
  31. $route = new InflectedRoute('/:controller/:action/:id', ['plugin' => null]);
  32. $result = $route->match(['controller' => 'Posts', 'action' => 'my_view', 'plugin' => null]);
  33. $this->assertFalse($result);
  34. $result = $route->match([
  35. 'plugin' => null,
  36. 'controller' => 'Posts',
  37. 'action' => 'my_view',
  38. 0
  39. ]);
  40. $this->assertFalse($result);
  41. $result = $route->match([
  42. 'plugin' => null,
  43. 'controller' => 'MyPosts',
  44. 'action' => 'my_view',
  45. 'id' => 1
  46. ]);
  47. $this->assertEquals('/my_posts/my_view/1', $result);
  48. $route = new InflectedRoute('/', ['controller' => 'Pages', 'action' => 'my_view', 'home']);
  49. $result = $route->match(['controller' => 'Pages', 'action' => 'my_view', 'home']);
  50. $this->assertEquals('/', $result);
  51. $result = $route->match(['controller' => 'Pages', 'action' => 'display', 'about']);
  52. $this->assertFalse($result);
  53. $route = new InflectedRoute('/blog/:action', ['controller' => 'Posts']);
  54. $result = $route->match(['controller' => 'Posts', 'action' => 'my_view']);
  55. $this->assertEquals('/blog/my_view', $result);
  56. $result = $route->match(['controller' => 'Posts', 'action' => 'my_view', 'id' => 2]);
  57. $this->assertEquals('/blog/my_view?id=2', $result);
  58. $result = $route->match(['controller' => 'Posts', 'action' => 'my_view', 1]);
  59. $this->assertFalse($result);
  60. $route = new InflectedRoute('/foo/:controller/:action', ['action' => 'index']);
  61. $result = $route->match(['controller' => 'Posts', 'action' => 'my_view']);
  62. $this->assertEquals('/foo/posts/my_view', $result);
  63. $route = new InflectedRoute('/:plugin/:id/*', ['controller' => 'Posts', 'action' => 'my_view']);
  64. $result = $route->match([
  65. 'plugin' => 'TestPlugin',
  66. 'controller' => 'Posts',
  67. 'action' => 'my_view',
  68. 'id' => '1'
  69. ]);
  70. $this->assertEquals('/test_plugin/1/', $result);
  71. $result = $route->match([
  72. 'plugin' => 'TestPlugin',
  73. 'controller' => 'Posts',
  74. 'action' => 'my_view',
  75. 'id' => '1',
  76. '0'
  77. ]);
  78. $this->assertEquals('/test_plugin/1/0', $result);
  79. $result = $route->match([
  80. 'plugin' => 'TestPlugin',
  81. 'controller' => 'Nodes',
  82. 'action' => 'my_view',
  83. 'id' => 1
  84. ]);
  85. $this->assertFalse($result);
  86. $result = $route->match([
  87. 'plugin' => 'TestPlugin',
  88. 'controller' => 'Posts',
  89. 'action' => 'edit',
  90. 'id' => 1
  91. ]);
  92. $this->assertFalse($result);
  93. $route = new InflectedRoute('/admin/subscriptions/:action/*', [
  94. 'controller' => 'Subscribe', 'prefix' => 'admin'
  95. ]);
  96. $result = $route->match([
  97. 'controller' => 'Subscribe',
  98. 'prefix' => 'admin',
  99. 'action' => 'edit_admin_e',
  100. 1
  101. ]);
  102. $expected = '/admin/subscriptions/edit_admin_e/1';
  103. $this->assertEquals($expected, $result);
  104. $route = new InflectedRoute('/:controller/:action-:id');
  105. $result = $route->match([
  106. 'controller' => 'MyPosts',
  107. 'action' => 'my_view',
  108. 'id' => 1
  109. ]);
  110. $this->assertEquals('/my_posts/my_view-1', $result);
  111. $route = new InflectedRoute('/:controller/:action/:slug-:id', [], ['id' => Router::ID]);
  112. $result = $route->match([
  113. 'controller' => 'MyPosts',
  114. 'action' => 'my_view',
  115. 'id' => 1,
  116. 'slug' => 'the-slug'
  117. ]);
  118. $this->assertEquals('/my_posts/my_view/the-slug-1', $result);
  119. }
  120. /**
  121. * test the parse method of InflectedRoute.
  122. *
  123. * @return void
  124. */
  125. public function testParse()
  126. {
  127. $route = new InflectedRoute('/:controller/:action/:id', [], ['id' => Router::ID]);
  128. $route->compile();
  129. $result = $route->parse('/my_posts/my_view/1', 'GET');
  130. $this->assertEquals('MyPosts', $result['controller']);
  131. $this->assertEquals('my_view', $result['action']);
  132. $this->assertEquals('1', $result['id']);
  133. $route = new InflectedRoute('/:controller/:action-:id');
  134. $route->compile();
  135. $result = $route->parse('/my_posts/my_view-1', 'GET');
  136. $this->assertEquals('MyPosts', $result['controller']);
  137. $this->assertEquals('my_view', $result['action']);
  138. $this->assertEquals('1', $result['id']);
  139. $route = new InflectedRoute('/:controller/:action/:slug-:id', [], ['id' => Router::ID]);
  140. $route->compile();
  141. $result = $route->parse('/my_posts/my_view/the-slug-1', 'GET');
  142. $this->assertEquals('MyPosts', $result['controller']);
  143. $this->assertEquals('my_view', $result['action']);
  144. $this->assertEquals('1', $result['id']);
  145. $this->assertEquals('the-slug', $result['slug']);
  146. $route = new InflectedRoute(
  147. '/admin/:controller',
  148. ['prefix' => 'admin', 'action' => 'index']
  149. );
  150. $route->compile();
  151. $result = $route->parse('/admin/', 'GET');
  152. $this->assertFalse($result);
  153. $result = $route->parse('/admin/my_posts', 'GET');
  154. $this->assertEquals('MyPosts', $result['controller']);
  155. $this->assertEquals('index', $result['action']);
  156. $route = new InflectedRoute(
  157. '/media/search/*',
  158. ['controller' => 'Media', 'action' => 'search_it']
  159. );
  160. $result = $route->parse('/media/search', 'GET');
  161. $this->assertEquals('Media', $result['controller']);
  162. $this->assertEquals('search_it', $result['action']);
  163. $this->assertEquals([], $result['pass']);
  164. $result = $route->parse('/media/search/tv_shows', 'GET');
  165. $this->assertEquals('Media', $result['controller']);
  166. $this->assertEquals('search_it', $result['action']);
  167. $this->assertEquals(['tv_shows'], $result['pass']);
  168. }
  169. /**
  170. * Test that parse() checks methods.
  171. *
  172. * @return void
  173. */
  174. public function testParseMethodMatch()
  175. {
  176. $route = new InflectedRoute('/:controller/:action', ['_method' => 'POST']);
  177. $this->assertFalse($route->parse('/blog_posts/add_new', 'GET'));
  178. $result = $route->parse('/blog_posts/add_new', 'POST');
  179. $this->assertEquals('BlogPosts', $result['controller']);
  180. $this->assertEquals('add_new', $result['action']);
  181. }
  182. /**
  183. * @return void
  184. */
  185. public function testMatchThenParse()
  186. {
  187. $route = new InflectedRoute('/plugin/:controller/:action', [
  188. 'plugin' => 'Vendor/PluginName'
  189. ]);
  190. $url = $route->match([
  191. 'plugin' => 'Vendor/PluginName',
  192. 'controller' => 'ControllerName',
  193. 'action' => 'action_name'
  194. ]);
  195. $expectedUrl = '/plugin/controller_name/action_name';
  196. $this->assertEquals($expectedUrl, $url);
  197. $result = $route->parse($expectedUrl, 'GET');
  198. $this->assertEquals('ControllerName', $result['controller']);
  199. $this->assertEquals('action_name', $result['action']);
  200. $this->assertEquals('Vendor/PluginName', $result['plugin']);
  201. }
  202. }