1
0

RoutingTest.php 16 KB

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