123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace Test\AppFramework\Routing;
- use OC\AppFramework\DependencyInjection\DIContainer;
- use OC\AppFramework\Routing\RouteConfig;
- use OC\Route\Route;
- use OC\Route\Router;
- use OCP\App\IAppManager;
- use OCP\Diagnostics\IEventLogger;
- use OCP\IConfig;
- use OCP\IRequest;
- use OCP\Route\IRouter;
- use PHPUnit\Framework\MockObject\MockObject;
- use Psr\Container\ContainerInterface;
- use Psr\Log\LoggerInterface;
- class RoutingTest extends \Test\TestCase {
- public function testSimpleRoute(): void {
- $routes = ['routes' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET']
- ]];
- $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
- }
- public function testSimpleRouteWithUnderScoreNames(): void {
- $routes = ['routes' => [
- ['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'root' => '']
- ]];
- $this->assertSimpleRoute($routes, 'admin_folders.open_current', 'DELETE', '/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent', [], [], '', true);
- }
- public function testSimpleOCSRoute(): void {
- $routes = ['ocs' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET']
- ]
- ];
- $this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
- }
- public function testSimpleRouteWithMissingVerb(): void {
- $routes = ['routes' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open']
- ]];
- $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
- }
- public function testSimpleOCSRouteWithMissingVerb(): void {
- $routes = ['ocs' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open']
- ]
- ];
- $this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
- }
- public function testSimpleRouteWithLowercaseVerb(): void {
- $routes = ['routes' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
- ]];
- $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
- }
- public function testSimpleOCSRouteWithLowercaseVerb(): void {
- $routes = ['ocs' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
- ]
- ];
- $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
- }
- public function testSimpleRouteWithRequirements(): void {
- $routes = ['routes' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => ['something']]
- ]];
- $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
- }
- public function testSimpleOCSRouteWithRequirements(): void {
- $routes = ['ocs' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => ['something']]
- ]
- ];
- $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
- }
- public function testSimpleRouteWithDefaults(): void {
- $routes = ['routes' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', [], 'defaults' => ['param' => 'foobar']]
- ]];
- $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
- }
- public function testSimpleOCSRouteWithDefaults(): void {
- $routes = ['ocs' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'defaults' => ['param' => 'foobar']]
- ]
- ];
- $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
- }
- public function testSimpleRouteWithPostfix(): void {
- $routes = ['routes' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something']
- ]];
- $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
- }
- public function testSimpleOCSRouteWithPostfix(): void {
- $routes = ['ocs' => [
- ['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something']
- ]
- ];
- $this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
- }
- public function testSimpleRouteWithBrokenName(): void {
- $this->expectException(\UnexpectedValueException::class);
- $routes = ['routes' => [
- ['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
- ]];
- /** @var IRouter|MockObject $router */
- $router = $this->getMockBuilder(Router::class)
- ->onlyMethods(['create'])
- ->setConstructorArgs([
- $this->createMock(LoggerInterface::class),
- $this->createMock(IRequest::class),
- $this->createMock(IConfig::class),
- $this->createMock(IEventLogger::class),
- $this->createMock(ContainerInterface::class),
- $this->createMock(IAppManager::class),
- ])
- ->getMock();
- // load route configuration
- $container = new DIContainer('app1');
- $config = new RouteConfig($container, $router, $routes);
- $config->register();
- }
- public function testSimpleOCSRouteWithBrokenName(): void {
- $this->expectException(\UnexpectedValueException::class);
- $routes = ['ocs' => [
- ['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
- ]];
- /** @var IRouter|MockObject $router */
- $router = $this->getMockBuilder(Router::class)
- ->onlyMethods(['create'])
- ->setConstructorArgs([
- $this->createMock(LoggerInterface::class),
- $this->createMock(IRequest::class),
- $this->createMock(IConfig::class),
- $this->createMock(IEventLogger::class),
- $this->createMock(ContainerInterface::class),
- $this->createMock(IAppManager::class),
- ])
- ->getMock();
- // load route configuration
- $container = new DIContainer('app1');
- $config = new RouteConfig($container, $router, $routes);
- $config->register();
- }
- public function testSimpleOCSRouteWithUnderScoreNames(): void {
- $routes = ['ocs' => [
- ['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
- ]];
- $this->assertSimpleOCSRoute($routes, 'admin_folders.open_current', 'DELETE', '/apps/app1/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
- }
- public function testOCSResource(): void {
- $routes = ['ocs-resources' => ['account' => ['url' => '/accounts']]];
- $this->assertOCSResource($routes, 'account', '/apps/app1/accounts', 'AccountController', 'id');
- }
- public function testOCSResourceWithUnderScoreName(): void {
- $routes = ['ocs-resources' => ['admin_accounts' => ['url' => '/admin/accounts']]];
- $this->assertOCSResource($routes, 'admin_accounts', '/apps/app1/admin/accounts', 'AdminAccountsController', 'id');
- }
- public function testOCSResourceWithRoot(): void {
- $routes = ['ocs-resources' => ['admin_accounts' => ['url' => '/admin/accounts', 'root' => '/core/endpoint']]];
- $this->assertOCSResource($routes, 'admin_accounts', '/core/endpoint/admin/accounts', 'AdminAccountsController', 'id');
- }
- public function testResource(): void {
- $routes = ['resources' => ['account' => ['url' => '/accounts']]];
- $this->assertResource($routes, 'account', '/apps/app1/accounts', 'AccountController', 'id');
- }
- public function testResourceWithUnderScoreName(): void {
- $routes = ['resources' => ['admin_accounts' => ['url' => '/admin/accounts']]];
- $this->assertResource($routes, 'admin_accounts', '/apps/app1/admin/accounts', 'AdminAccountsController', 'id');
- }
- private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements = [], array $defaults = [], $postfix = '', $allowRootUrl = false): void {
- if ($postfix) {
- $name .= $postfix;
- }
- // route mocks
- $container = new DIContainer('app1');
- $route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
- /** @var IRouter|MockObject $router */
- $router = $this->getMockBuilder(Router::class)
- ->onlyMethods(['create'])
- ->setConstructorArgs([
- $this->createMock(LoggerInterface::class),
- $this->createMock(IRequest::class),
- $this->createMock(IConfig::class),
- $this->createMock(IEventLogger::class),
- $this->createMock(ContainerInterface::class),
- $this->createMock(IAppManager::class),
- ])
- ->getMock();
- // we expect create to be called once:
- $router
- ->expects($this->once())
- ->method('create')
- ->with($this->equalTo('app1.' . $name), $this->equalTo($url))
- ->willReturn($route);
- // load route configuration
- $config = new RouteConfig($container, $router, $routes);
- if ($allowRootUrl) {
- self::invokePrivate($config, 'rootUrlApps', [['app1']]);
- }
- $config->register();
- }
- /**
- * @param $routes
- * @param string $name
- * @param string $verb
- * @param string $url
- * @param string $controllerName
- * @param string $actionName
- * @param array $requirements
- * @param array $defaults
- * @param string $postfix
- */
- private function assertSimpleOCSRoute($routes,
- $name,
- $verb,
- $url,
- $controllerName,
- $actionName,
- array $requirements = [],
- array $defaults = [],
- $postfix = '') {
- if ($postfix) {
- $name .= $postfix;
- }
- // route mocks
- $container = new DIContainer('app1');
- $route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
- /** @var IRouter|MockObject $router */
- $router = $this->getMockBuilder(Router::class)
- ->onlyMethods(['create'])
- ->setConstructorArgs([
- $this->createMock(LoggerInterface::class),
- $this->createMock(IRequest::class),
- $this->createMock(IConfig::class),
- $this->createMock(IEventLogger::class),
- $this->createMock(ContainerInterface::class),
- $this->createMock(IAppManager::class),
- ])
- ->getMock();
- // we expect create to be called once:
- $router
- ->expects($this->once())
- ->method('create')
- ->with($this->equalTo('ocs.app1.' . $name), $this->equalTo($url))
- ->willReturn($route);
- // load route configuration
- $config = new RouteConfig($container, $router, $routes);
- $config->register();
- }
- /**
- * @param array $yaml
- * @param string $resourceName
- * @param string $url
- * @param string $controllerName
- * @param string $paramName
- */
- private function assertOCSResource($yaml, $resourceName, $url, $controllerName, $paramName): void {
- /** @var IRouter|MockObject $router */
- $router = $this->getMockBuilder(Router::class)
- ->onlyMethods(['create'])
- ->setConstructorArgs([
- $this->createMock(LoggerInterface::class),
- $this->createMock(IRequest::class),
- $this->createMock(IConfig::class),
- $this->createMock(IEventLogger::class),
- $this->createMock(ContainerInterface::class),
- $this->createMock(IAppManager::class),
- ])
- ->getMock();
- // route mocks
- $container = new DIContainer('app1');
- $indexRoute = $this->mockRoute($container, 'GET', $controllerName, 'index');
- $showRoute = $this->mockRoute($container, 'GET', $controllerName, 'show');
- $createRoute = $this->mockRoute($container, 'POST', $controllerName, 'create');
- $updateRoute = $this->mockRoute($container, 'PUT', $controllerName, 'update');
- $destroyRoute = $this->mockRoute($container, 'DELETE', $controllerName, 'destroy');
- $urlWithParam = $url . '/{' . $paramName . '}';
- // we expect create to be called five times:
- $router
- ->expects($this->exactly(5))
- ->method('create')
- ->withConsecutive(
- [$this->equalTo('ocs.app1.' . $resourceName . '.index'), $this->equalTo($url)],
- [$this->equalTo('ocs.app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam)],
- [$this->equalTo('ocs.app1.' . $resourceName . '.create'), $this->equalTo($url)],
- [$this->equalTo('ocs.app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam)],
- [$this->equalTo('ocs.app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam)],
- )->willReturnOnConsecutiveCalls(
- $indexRoute,
- $showRoute,
- $createRoute,
- $updateRoute,
- $destroyRoute,
- );
- // load route configuration
- $config = new RouteConfig($container, $router, $yaml);
- $config->register();
- }
- /**
- * @param string $resourceName
- * @param string $url
- * @param string $controllerName
- * @param string $paramName
- */
- private function assertResource($yaml, $resourceName, $url, $controllerName, $paramName) {
- /** @var IRouter|MockObject $router */
- $router = $this->getMockBuilder(Router::class)
- ->onlyMethods(['create'])
- ->setConstructorArgs([
- $this->createMock(LoggerInterface::class),
- $this->createMock(IRequest::class),
- $this->createMock(IConfig::class),
- $this->createMock(IEventLogger::class),
- $this->createMock(ContainerInterface::class),
- $this->createMock(IAppManager::class),
- ])
- ->getMock();
- // route mocks
- $container = new DIContainer('app1');
- $indexRoute = $this->mockRoute($container, 'GET', $controllerName, 'index');
- $showRoute = $this->mockRoute($container, 'GET', $controllerName, 'show');
- $createRoute = $this->mockRoute($container, 'POST', $controllerName, 'create');
- $updateRoute = $this->mockRoute($container, 'PUT', $controllerName, 'update');
- $destroyRoute = $this->mockRoute($container, 'DELETE', $controllerName, 'destroy');
- $urlWithParam = $url . '/{' . $paramName . '}';
- // we expect create to be called five times:
- $router
- ->expects($this->exactly(5))
- ->method('create')
- ->withConsecutive(
- [$this->equalTo('app1.' . $resourceName . '.index'), $this->equalTo($url)],
- [$this->equalTo('app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam)],
- [$this->equalTo('app1.' . $resourceName . '.create'), $this->equalTo($url)],
- [$this->equalTo('app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam)],
- [$this->equalTo('app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam)],
- )->willReturnOnConsecutiveCalls(
- $indexRoute,
- $showRoute,
- $createRoute,
- $updateRoute,
- $destroyRoute,
- );
- // load route configuration
- $config = new RouteConfig($container, $router, $yaml);
- $config->register();
- }
- /**
- * @param DIContainer $container
- * @param string $verb
- * @param string $controllerName
- * @param string $actionName
- * @param array $requirements
- * @param array $defaults
- * @return MockObject
- */
- private function mockRoute(
- DIContainer $container,
- $verb,
- $controllerName,
- $actionName,
- array $requirements = [],
- array $defaults = [],
- ) {
- $route = $this->getMockBuilder(Route::class)
- ->onlyMethods(['method', 'requirements', 'defaults'])
- ->disableOriginalConstructor()
- ->getMock();
- $route
- ->expects($this->once())
- ->method('method')
- ->with($this->equalTo($verb))
- ->willReturn($route);
- if (count($requirements) > 0) {
- $route
- ->expects($this->once())
- ->method('requirements')
- ->with($this->equalTo($requirements))
- ->willReturn($route);
- }
- $route->expects($this->once())
- ->method('defaults')
- ->with($this->callback(function (array $def) use ($defaults, $controllerName, $actionName) {
- $defaults['caller'] = ['app1', $controllerName, $actionName];
- $this->assertEquals($defaults, $def);
- return true;
- }))
- ->willReturn($route);
- return $route;
- }
- }
|