RoutingTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. namespace Test\AppFramework\Routing;
  3. use OC\AppFramework\DependencyInjection\DIContainer;
  4. use OC\AppFramework\Routing\RouteConfig;
  5. use OC\Route\Route;
  6. use OC\Route\Router;
  7. use OCP\ILogger;
  8. use OCP\Route\IRouter;
  9. use PHPUnit\Framework\MockObject\MockObject;
  10. class RoutingTest extends \Test\TestCase {
  11. public function testSimpleRoute() {
  12. $routes = ['routes' => [
  13. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET']
  14. ]];
  15. $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
  16. }
  17. public function testSimpleRouteWithUnderScoreNames() {
  18. $routes = ['routes' => [
  19. ['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'root' => '']
  20. ]];
  21. $this->assertSimpleRoute($routes, 'admin_folders.open_current', 'DELETE', '/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent', [], [], '', true);
  22. }
  23. public function testSimpleOCSRoute() {
  24. $routes = ['ocs' => [
  25. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET']
  26. ]
  27. ];
  28. $this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
  29. }
  30. public function testSimpleRouteWithMissingVerb() {
  31. $routes = ['routes' => [
  32. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open']
  33. ]];
  34. $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
  35. }
  36. public function testSimpleOCSRouteWithMissingVerb() {
  37. $routes = ['ocs' => [
  38. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open']
  39. ]
  40. ];
  41. $this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
  42. }
  43. public function testSimpleRouteWithLowercaseVerb() {
  44. $routes = ['routes' => [
  45. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
  46. ]];
  47. $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
  48. }
  49. public function testSimpleOCSRouteWithLowercaseVerb() {
  50. $routes = ['ocs' => [
  51. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
  52. ]
  53. ];
  54. $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
  55. }
  56. public function testSimpleRouteWithRequirements() {
  57. $routes = ['routes' => [
  58. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => ['something']]
  59. ]];
  60. $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
  61. }
  62. public function testSimpleOCSRouteWithRequirements() {
  63. $routes = ['ocs' => [
  64. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => ['something']]
  65. ]
  66. ];
  67. $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
  68. }
  69. public function testSimpleRouteWithDefaults() {
  70. $routes = ['routes' => [
  71. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', [], 'defaults' => ['param' => 'foobar']]
  72. ]];
  73. $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
  74. }
  75. public function testSimpleOCSRouteWithDefaults() {
  76. $routes = ['ocs' => [
  77. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'defaults' => ['param' => 'foobar']]
  78. ]
  79. ];
  80. $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
  81. }
  82. public function testSimpleRouteWithPostfix() {
  83. $routes = ['routes' => [
  84. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something']
  85. ]];
  86. $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
  87. }
  88. public function testSimpleOCSRouteWithPostfix() {
  89. $routes = ['ocs' => [
  90. ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something']
  91. ]
  92. ];
  93. $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
  94. }
  95. public function testSimpleRouteWithBrokenName() {
  96. $this->expectException(\UnexpectedValueException::class);
  97. $routes = ['routes' => [
  98. ['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
  99. ]];
  100. /** @var IRouter|MockObject $router */
  101. $router = $this->getMockBuilder(Router::class)
  102. ->onlyMethods(['create'])
  103. ->setConstructorArgs([$this->createMock(ILogger::class)])
  104. ->getMock();
  105. // load route configuration
  106. $container = new DIContainer('app1');
  107. $config = new RouteConfig($container, $router, $routes);
  108. $config->register();
  109. }
  110. public function testSimpleOCSRouteWithBrokenName() {
  111. $this->expectException(\UnexpectedValueException::class);
  112. $routes = ['ocs' => [
  113. ['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
  114. ]];
  115. /** @var IRouter|MockObject $router */
  116. $router = $this->getMockBuilder(Router::class)
  117. ->onlyMethods(['create'])
  118. ->setConstructorArgs([$this->createMock(ILogger::class)])
  119. ->getMock();
  120. // load route configuration
  121. $container = new DIContainer('app1');
  122. $config = new RouteConfig($container, $router, $routes);
  123. $config->register();
  124. }
  125. public function testSimpleOCSRouteWithUnderScoreNames() {
  126. $routes = ['ocs' => [
  127. ['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
  128. ]];
  129. $this->assertSimpleOCSRoute($routes, 'admin_folders.open_current', 'DELETE', '/apps/app1/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
  130. }
  131. public function testOCSResource() {
  132. $routes = ['ocs-resources' => ['account' => ['url' => '/accounts']]];
  133. $this->assertOCSResource($routes, 'account', '/apps/app1/accounts', 'AccountController', 'id');
  134. }
  135. public function testOCSResourceWithUnderScoreName() {
  136. $routes = ['ocs-resources' => ['admin_accounts' => ['url' => '/admin/accounts']]];
  137. $this->assertOCSResource($routes, 'admin_accounts', '/apps/app1/admin/accounts', 'AdminAccountsController', 'id');
  138. }
  139. public function testOCSResourceWithRoot() {
  140. $routes = ['ocs-resources' => ['admin_accounts' => ['url' => '/admin/accounts', 'root' => '/core/endpoint']]];
  141. $this->assertOCSResource($routes, 'admin_accounts', '/core/endpoint/admin/accounts', 'AdminAccountsController', 'id');
  142. }
  143. public function testResource() {
  144. $routes = ['resources' => ['account' => ['url' => '/accounts']]];
  145. $this->assertResource($routes, 'account', '/apps/app1/accounts', 'AccountController', 'id');
  146. }
  147. public function testResourceWithUnderScoreName() {
  148. $routes = ['resources' => ['admin_accounts' => ['url' => '/admin/accounts']]];
  149. $this->assertResource($routes, 'admin_accounts', '/apps/app1/admin/accounts', 'AdminAccountsController', 'id');
  150. }
  151. private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements = [], array $defaults = [], $postfix = '', $allowRootUrl = false): void {
  152. if ($postfix) {
  153. $name .= $postfix;
  154. }
  155. // route mocks
  156. $container = new DIContainer('app1');
  157. $route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
  158. /** @var IRouter|MockObject $router */
  159. $router = $this->getMockBuilder(Router::class)
  160. ->onlyMethods(['create'])
  161. ->setConstructorArgs([$this->createMock(ILogger::class)])
  162. ->getMock();
  163. // we expect create to be called once:
  164. $router
  165. ->expects($this->once())
  166. ->method('create')
  167. ->with($this->equalTo('app1.' . $name), $this->equalTo($url))
  168. ->willReturn($route);
  169. // load route configuration
  170. $config = new RouteConfig($container, $router, $routes);
  171. if ($allowRootUrl) {
  172. self::invokePrivate($config, 'rootUrlApps', [['app1']]);
  173. }
  174. $config->register();
  175. }
  176. /**
  177. * @param $routes
  178. * @param string $name
  179. * @param string $verb
  180. * @param string $url
  181. * @param string $controllerName
  182. * @param string $actionName
  183. * @param array $requirements
  184. * @param array $defaults
  185. * @param string $postfix
  186. */
  187. private function assertSimpleOCSRoute($routes,
  188. $name,
  189. $verb,
  190. $url,
  191. $controllerName,
  192. $actionName,
  193. array $requirements = [],
  194. array $defaults = [],
  195. $postfix = '') {
  196. if ($postfix) {
  197. $name .= $postfix;
  198. }
  199. // route mocks
  200. $container = new DIContainer('app1');
  201. $route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
  202. /** @var IRouter|MockObject $router */
  203. $router = $this->getMockBuilder(Router::class)
  204. ->onlyMethods(['create'])
  205. ->setConstructorArgs([$this->createMock(ILogger::class)])
  206. ->getMock();
  207. // we expect create to be called once:
  208. $router
  209. ->expects($this->once())
  210. ->method('create')
  211. ->with($this->equalTo('ocs.app1.' . $name), $this->equalTo($url))
  212. ->willReturn($route);
  213. // load route configuration
  214. $config = new RouteConfig($container, $router, $routes);
  215. $config->register();
  216. }
  217. /**
  218. * @param array $yaml
  219. * @param string $resourceName
  220. * @param string $url
  221. * @param string $controllerName
  222. * @param string $paramName
  223. */
  224. private function assertOCSResource($yaml, $resourceName, $url, $controllerName, $paramName): void {
  225. /** @var IRouter|MockObject $router */
  226. $router = $this->getMockBuilder(Router::class)
  227. ->onlyMethods(['create'])
  228. ->setConstructorArgs([$this->createMock(ILogger::class)])
  229. ->getMock();
  230. // route mocks
  231. $container = new DIContainer('app1');
  232. $indexRoute = $this->mockRoute($container, 'GET', $controllerName, 'index');
  233. $showRoute = $this->mockRoute($container, 'GET', $controllerName, 'show');
  234. $createRoute = $this->mockRoute($container, 'POST', $controllerName, 'create');
  235. $updateRoute = $this->mockRoute($container, 'PUT', $controllerName, 'update');
  236. $destroyRoute = $this->mockRoute($container, 'DELETE', $controllerName, 'destroy');
  237. $urlWithParam = $url . '/{' . $paramName . '}';
  238. // we expect create to be called once:
  239. $router
  240. ->expects($this->at(0))
  241. ->method('create')
  242. ->with($this->equalTo('ocs.app1.' . $resourceName . '.index'), $this->equalTo($url))
  243. ->willReturn($indexRoute);
  244. $router
  245. ->expects($this->at(1))
  246. ->method('create')
  247. ->with($this->equalTo('ocs.app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam))
  248. ->willReturn($showRoute);
  249. $router
  250. ->expects($this->at(2))
  251. ->method('create')
  252. ->with($this->equalTo('ocs.app1.' . $resourceName . '.create'), $this->equalTo($url))
  253. ->willReturn($createRoute);
  254. $router
  255. ->expects($this->at(3))
  256. ->method('create')
  257. ->with($this->equalTo('ocs.app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam))
  258. ->willReturn($updateRoute);
  259. $router
  260. ->expects($this->at(4))
  261. ->method('create')
  262. ->with($this->equalTo('ocs.app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam))
  263. ->willReturn($destroyRoute);
  264. // load route configuration
  265. $config = new RouteConfig($container, $router, $yaml);
  266. $config->register();
  267. }
  268. /**
  269. * @param string $resourceName
  270. * @param string $url
  271. * @param string $controllerName
  272. * @param string $paramName
  273. */
  274. private function assertResource($yaml, $resourceName, $url, $controllerName, $paramName) {
  275. /** @var IRouter|MockObject $router */
  276. $router = $this->getMockBuilder(Router::class)
  277. ->onlyMethods(['create'])
  278. ->setConstructorArgs([$this->createMock(ILogger::class)])
  279. ->getMock();
  280. // route mocks
  281. $container = new DIContainer('app1');
  282. $indexRoute = $this->mockRoute($container, 'GET', $controllerName, 'index');
  283. $showRoute = $this->mockRoute($container, 'GET', $controllerName, 'show');
  284. $createRoute = $this->mockRoute($container, 'POST', $controllerName, 'create');
  285. $updateRoute = $this->mockRoute($container, 'PUT', $controllerName, 'update');
  286. $destroyRoute = $this->mockRoute($container, 'DELETE', $controllerName, 'destroy');
  287. $urlWithParam = $url . '/{' . $paramName . '}';
  288. // we expect create to be called once:
  289. $router
  290. ->expects($this->at(0))
  291. ->method('create')
  292. ->with($this->equalTo('app1.' . $resourceName . '.index'), $this->equalTo($url))
  293. ->willReturn($indexRoute);
  294. $router
  295. ->expects($this->at(1))
  296. ->method('create')
  297. ->with($this->equalTo('app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam))
  298. ->willReturn($showRoute);
  299. $router
  300. ->expects($this->at(2))
  301. ->method('create')
  302. ->with($this->equalTo('app1.' . $resourceName . '.create'), $this->equalTo($url))
  303. ->willReturn($createRoute);
  304. $router
  305. ->expects($this->at(3))
  306. ->method('create')
  307. ->with($this->equalTo('app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam))
  308. ->willReturn($updateRoute);
  309. $router
  310. ->expects($this->at(4))
  311. ->method('create')
  312. ->with($this->equalTo('app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam))
  313. ->willReturn($destroyRoute);
  314. // load route configuration
  315. $config = new RouteConfig($container, $router, $yaml);
  316. $config->register();
  317. }
  318. /**
  319. * @param DIContainer $container
  320. * @param string $verb
  321. * @param string $controllerName
  322. * @param string $actionName
  323. * @param array $requirements
  324. * @param array $defaults
  325. * @return MockObject
  326. */
  327. private function mockRoute(
  328. DIContainer $container,
  329. $verb,
  330. $controllerName,
  331. $actionName,
  332. array $requirements = [],
  333. array $defaults = []
  334. ) {
  335. $route = $this->getMockBuilder(Route::class)
  336. ->onlyMethods(['method', 'requirements', 'defaults'])
  337. ->disableOriginalConstructor()
  338. ->getMock();
  339. $route
  340. ->expects($this->once())
  341. ->method('method')
  342. ->with($this->equalTo($verb))
  343. ->willReturn($route);
  344. if (count($requirements) > 0) {
  345. $route
  346. ->expects($this->once())
  347. ->method('requirements')
  348. ->with($this->equalTo($requirements))
  349. ->willReturn($route);
  350. }
  351. $route->expects($this->once())
  352. ->method('defaults')
  353. ->with($this->callback(function (array $def) use ($defaults, $controllerName, $actionName) {
  354. $defaults['caller'] = ['app1', $controllerName, $actionName];
  355. $this->assertEquals($defaults, $def);
  356. return true;
  357. }))
  358. ->willReturn($route);
  359. return $route;
  360. }
  361. }