InflectedRouteTest.php 7.7 KB

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