Merge pull request #20114 from nextcloud/techdebt/noid/allow-some-apps-to-have-root-urls

Allow some apps to have root URLs in their own routing file
This commit is contained in:
Roeland Jago Douma 2020-04-21 16:00:34 +02:00 committed by GitHub
commit 8f650feede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 227 additions and 271 deletions

View File

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
return [
'routes' => [
[
'name' => 'RequestHandler#addShare',
'url' => '/ocm/shares',
'verb' => 'POST',
'root' => '',
],
[
'name' => 'RequestHandler#receiveNotification',
'url' => '/ocm/notifications',
'verb' => 'POST',
'root' => '',
],
],
];

View File

@ -41,6 +41,13 @@ $application->registerRoutes(
$this,
[
'routes' => [
[
'name' => 'View#showFile',
'url' => '/f/{fileid}',
'verb' => 'GET',
'root' => '',
],
[
'name' => 'API#getThumbnail',
'url' => '/api/v1/thumbnail/{x}/{y}/{file}',
@ -146,7 +153,7 @@ $application->registerRoutes(
/** @var $this \OC\Route\Router */
$this->create('files_ajax_download', 'ajax/download.php')
$this->create('files_ajax_download', 'apps/files/ajax/download.php')
->actionInclude('files/ajax/download.php');
$this->create('files_ajax_list', 'ajax/list.php')
$this->create('files_ajax_list', 'apps/files/ajax/list.php')
->actionInclude('files/ajax/list.php');

View File

@ -60,11 +60,11 @@
]
);
$this->create('files_external_oauth1', 'ajax/oauth1.php')
$this->create('files_external_oauth1', 'apps/files_external/ajax/oauth1.php')
->actionInclude('files_external/ajax/oauth1.php');
$this->create('files_external_oauth2', 'ajax/oauth2.php')
$this->create('files_external_oauth2', 'apps/files_external/ajax/oauth2.php')
->actionInclude('files_external/ajax/oauth2.php');
$this->create('files_external_list_applicable', '/applicable')
$this->create('files_external_list_applicable', '/apps/files_external/applicable')
->actionInclude('files_external/ajax/applicable.php');

View File

@ -30,6 +30,37 @@ return [
'ExternalShares' => ['url' => '/api/externalShares'],
],
'routes' => [
[
'name' => 'Share#showShare',
'url' => '/s/{token}',
'verb' => 'GET',
'root' => '',
],
[
'name' => 'Share#showAuthenticate',
'url' => '/s/{token}/authenticate/{redirect}',
'verb' => 'GET',
'root' => '',
],
[
'name' => 'Share#authenticate',
'url' => '/s/{token}/authenticate/{redirect}',
'verb' => 'POST',
'root' => '',
],
[
'name' => 'Share#downloadShare',
'url' => '/s/{token}/download',
'verb' => 'GET',
'root' => '',
],
[
'name' => 'PublicPreview#directLink',
'url' => '/s/{token}/preview',
'verb' => 'GET',
'root' => '',
],
[
'name' => 'externalShares#testRemote',
'url' => '/testremote',

View File

@ -34,8 +34,6 @@ namespace OCA\Files_Sharing\AppInfo;
use OC\AppFramework\Utility\SimpleContainer;
use OCA\Files_Sharing\Capabilities;
use OCA\Files_Sharing\Controller\ExternalSharesController;
use OCA\Files_Sharing\Controller\ShareController;
use OCA\Files_Sharing\External\Manager;
use OCA\Files_Sharing\Listener\LoadAdditionalListener;
use OCA\Files_Sharing\Listener\LoadSidebarListener;
@ -51,10 +49,7 @@ use OCA\Files_Sharing\Notification\Notifier;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files\Event\LoadSidebar;
use OCP\AppFramework\App;
use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudIdManager;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Group\Events\UserAddedEvent;
use OCP\IContainer;
@ -80,48 +75,10 @@ class Application extends App {
$mountProviderCollection = $server->getMountProviderCollection();
$notifications = $server->getNotificationManager();
/**
* Controllers
*/
$container->registerService('ShareController', function (SimpleContainer $c) use ($server) {
$federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application();
return new ShareController(
$c->query('AppName'),
$c->query('Request'),
$server->getConfig(),
$server->getURLGenerator(),
$server->getUserManager(),
$server->getLogger(),
$server->getActivityManager(),
$server->getShareManager(),
$server->getSession(),
$server->getPreviewManager(),
$server->getRootFolder(),
$federatedSharingApp->getFederatedShareProvider(),
$server->getEventDispatcher(),
$server->getL10N($c->query('AppName')),
$server->query(Defaults::class)
);
});
$container->registerService('ExternalSharesController', function (SimpleContainer $c) {
return new ExternalSharesController(
$c->query('AppName'),
$c->query('Request'),
$c->query('ExternalManager'),
$c->query('HttpClientService')
);
});
/**
* Core class wrappers
*/
$container->registerService('HttpClientService', function (SimpleContainer $c) use ($server) {
return $server->getHTTPClientService();
});
$container->registerService(ICloudIdManager::class, function (SimpleContainer $c) use ($server) {
return $server->getCloudIdManager();
});
$container->registerService('ExternalManager', function (SimpleContainer $c) use ($server) {
$container->registerService(Manager::class, function (SimpleContainer $c) use ($server) {
$user = $server->getUserSession()->getUser();
$uid = $user ? $user->getUID() : null;
return new \OCA\Files_Sharing\External\Manager(
@ -138,50 +95,21 @@ class Application extends App {
$uid
);
});
$container->registerAlias(Manager::class, 'ExternalManager');
/**
* Middleware
*/
$container->registerService('SharingCheckMiddleware', function (SimpleContainer $c) use ($server) {
return new SharingCheckMiddleware(
$c->query('AppName'),
$server->getConfig(),
$server->getAppManager(),
$server->query(IControllerMethodReflector::class),
$server->getShareManager(),
$server->getRequest()
);
});
$container->registerService(ShareInfoMiddleware::class, function () use ($server) {
return new ShareInfoMiddleware(
$server->getShareManager()
);
});
// Execute middlewares
$container->registerMiddleWare('SharingCheckMiddleware');
$container->registerMiddleWare(SharingCheckMiddleware::class);
$container->registerMiddleWare(OCSShareAPIMiddleware::class);
$container->registerMiddleWare(ShareInfoMiddleware::class);
$container->registerService('MountProvider', function (IContainer $c) {
/** @var \OCP\IServerContainer $server */
$server = $c->query('ServerContainer');
return new MountProvider(
$server->getConfig(),
$server->getShareManager(),
$server->getLogger()
);
});
$container->registerService('ExternalMountProvider', function (IContainer $c) {
/** @var \OCP\IServerContainer $server */
$server = $c->query('ServerContainer');
return new \OCA\Files_Sharing\External\MountProvider(
$server->getDatabaseConnection(),
function () use ($c) {
return $c->query('ExternalManager');
return $c->query(Manager::class);
},
$server->getCloudIdManager()
);
@ -205,7 +133,7 @@ class Application extends App {
}
protected function registerMountProviders(IMountProviderCollection $mountProviderCollection) {
$mountProviderCollection->registerProvider($this->getContainer()->query('MountProvider'));
$mountProviderCollection->registerProvider($this->getContainer()->query(MountProvider::class));
$mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider'));
}

View File

@ -40,9 +40,9 @@ $application->registerRoutes($this, [
]);
/** @var $this \OCP\Route\IRouter */
$this->create('files_versions_download', 'download.php')
$this->create('files_versions_download', 'apps/files_versions/download.php')
->actionInclude('files_versions/download.php');
$this->create('files_versions_ajax_getVersions', 'ajax/getVersions.php')
$this->create('files_versions_ajax_getVersions', 'apps/files_versions/ajax/getVersions.php')
->actionInclude('files_versions/ajax/getVersions.php');
$this->create('files_versions_ajax_rollbackVersion', 'ajax/rollbackVersion.php')
$this->create('files_versions_ajax_rollbackVersion', 'apps/files_versions/ajax/rollbackVersion.php')
->actionInclude('files_versions/ajax/rollbackVersion.php');

View File

@ -1,4 +1,6 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud GmbH.
*
@ -21,4 +23,4 @@
*
*/
$app = new \OCA\Testing\AppInfo\Application();
$app = \OC::$server->query(\OCA\Testing\AppInfo\Application::class);

View File

@ -28,19 +28,19 @@ declare(strict_types=1);
*/
/** @var $this \OCP\Route\IRouter */
$this->create('user_ldap_ajax_clearMappings', 'ajax/clearMappings.php')
$this->create('user_ldap_ajax_clearMappings', 'apps/user_ldap/ajax/clearMappings.php')
->actionInclude('user_ldap/ajax/clearMappings.php');
$this->create('user_ldap_ajax_deleteConfiguration', 'ajax/deleteConfiguration.php')
$this->create('user_ldap_ajax_deleteConfiguration', 'apps/user_ldap/ajax/deleteConfiguration.php')
->actionInclude('user_ldap/ajax/deleteConfiguration.php');
$this->create('user_ldap_ajax_getConfiguration', 'ajax/getConfiguration.php')
$this->create('user_ldap_ajax_getConfiguration', 'apps/user_ldap/ajax/getConfiguration.php')
->actionInclude('user_ldap/ajax/getConfiguration.php');
$this->create('user_ldap_ajax_getNewServerConfigPrefix', 'ajax/getNewServerConfigPrefix.php')
$this->create('user_ldap_ajax_getNewServerConfigPrefix', 'apps/user_ldap/ajax/getNewServerConfigPrefix.php')
->actionInclude('user_ldap/ajax/getNewServerConfigPrefix.php');
$this->create('user_ldap_ajax_setConfiguration', 'ajax/setConfiguration.php')
$this->create('user_ldap_ajax_setConfiguration', 'apps/user_ldap/ajax/setConfiguration.php')
->actionInclude('user_ldap/ajax/setConfiguration.php');
$this->create('user_ldap_ajax_testConfiguration', 'ajax/testConfiguration.php')
$this->create('user_ldap_ajax_testConfiguration', 'apps/user_ldap/ajax/testConfiguration.php')
->actionInclude('user_ldap/ajax/testConfiguration.php');
$this->create('user_ldap_ajax_wizard', 'ajax/wizard.php')
$this->create('user_ldap_ajax_wizard', 'apps/user_ldap/ajax/wizard.php')
->actionInclude('user_ldap/ajax/wizard.php');
$application = new \OCP\AppFramework\App('user_ldap');

View File

@ -89,18 +89,6 @@ $application->registerRoutes($this, [
// Logins for passwordless auth
['name' => 'WebAuthn#startAuthentication', 'url' => 'login/webauthn/start', 'verb' => 'POST'],
['name' => 'WebAuthn#finishAuthentication', 'url' => 'login/webauthn/finish', 'verb' => 'POST'],
// Legacy routes that need to be globally available while they are handled by an app
['name' => 'viewcontroller#showFile', 'url' => '/f/{fileid}', 'verb' => 'GET', 'app' => 'files'],
['name' => 'sharecontroller#showShare', 'url' => '/s/{token}', 'verb' => 'GET', 'app' => 'files_sharing'],
['name' => 'sharecontroller#showAuthenticate', 'url' => '/s/{token}/authenticate/{redirect}', 'verb' => 'GET', 'app' => 'files_sharing'],
['name' => 'sharecontroller#authenticate', 'url' => '/s/{token}/authenticate/{redirect}', 'verb' => 'POST', 'app' => 'files_sharing'],
['name' => 'sharecontroller#downloadShare', 'url' => '/s/{token}/download', 'verb' => 'GET', 'app' => 'files_sharing'],
['name' => 'publicpreview#directLink', 'url' => '/s/{token}/preview', 'verb' => 'GET', 'app' => 'files_sharing'],
['name' => 'requesthandlercontroller#addShare', 'url' => '/ocm/shares', 'verb' => 'POST', 'app' => 'cloud_federation_api'],
['name' => 'requesthandlercontroller#receiveNotification', 'url' => '/ocm/notifications', 'verb' => 'POST', 'app' => 'cloud_federation_api'],
['name' => 'pagecontroller#showCall', 'url' => '/call/{token}', 'verb' => 'GET', 'app' => 'spreed'],
['name' => 'pagecontroller#authenticatePassword', 'url' => '/call/{token}', 'verb' => 'POST', 'app' => 'spreed'],
],
'ocs' => [
['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],

View File

@ -33,7 +33,6 @@ declare(strict_types=1);
namespace OC\AppFramework\Routing;
use OC\AppFramework\DependencyInjection\DIContainer;
use OCP\AppFramework\App;
use OCP\Route\IRouter;
/**
@ -56,6 +55,14 @@ class RouteConfig {
/** @var string[] */
private $controllerNameCache = [];
protected $rootUrlApps = [
'cloud_federation_api',
'core',
'files_sharing',
'files',
'spreed',
];
/**
* @param \OC\AppFramework\DependencyInjection\DIContainer $container
* @param \OCP\Route\IRouter $router
@ -98,42 +105,7 @@ class RouteConfig {
private function processOCS(array $routes): void {
$ocsRoutes = $routes['ocs'] ?? [];
foreach ($ocsRoutes as $ocsRoute) {
$name = $ocsRoute['name'];
$postfix = $ocsRoute['postfix'] ?? '';
$root = $ocsRoute['root'] ?? '/apps/' . $this->appName;
$url = $root . $ocsRoute['url'];
$verb = strtoupper($ocsRoute['verb'] ?? 'GET');
$split = explode('#', $name, 2);
if (count($split) !== 2) {
throw new \UnexpectedValueException('Invalid route name');
}
list($controller, $action) = $split;
$controllerName = $this->buildControllerName($controller);
$actionName = $this->buildActionName($action);
$routeName = 'ocs.' . $this->appName . '.' . $controller . '.' . $action . $postfix;
// register the route
$handler = new RouteActionHandler($this->container, $controllerName, $actionName);
$router = $this->router->create($routeName, $url)
->method($verb)
->action($handler);
// optionally register requirements for route. This is used to
// tell the route parser how url parameters should be matched
if (array_key_exists('requirements', $ocsRoute)) {
$router->requirements($ocsRoute['requirements']);
}
// optionally register defaults for route. This is used to
// tell the route parser how url parameters should be default valued
if (array_key_exists('defaults', $ocsRoute)) {
$router->defaults($ocsRoute['defaults']);
}
$this->processRoute($ocsRoute, 'ocs.');
}
}
@ -145,53 +117,51 @@ class RouteConfig {
private function processSimpleRoutes(array $routes): void {
$simpleRoutes = $routes['routes'] ?? [];
foreach ($simpleRoutes as $simpleRoute) {
$name = $simpleRoute['name'];
$postfix = $simpleRoute['postfix'] ?? '';
$this->processRoute($simpleRoute);
}
}
$url = $simpleRoute['url'];
$verb = strtoupper($simpleRoute['verb'] ?? 'GET');
protected function processRoute(array $route, string $routeNamePrefix = ''): void {
$name = $route['name'];
$postfix = $route['postfix'] ?? '';
$defaultRoot = $this->appName === 'core' ? '' : '/apps/' . $this->appName;
$root = $route['root'] ?? $defaultRoot;
if ($routeNamePrefix === '' && !\in_array($this->appName, $this->rootUrlApps, true)) {
// Only allow root URLS for some apps
$root = $defaultRoot;
}
$split = explode('#', $name, 2);
if (count($split) !== 2) {
throw new \UnexpectedValueException('Invalid route name');
}
list($controller, $action) = $split;
$url = $root . $route['url'];
$verb = strtoupper($route['verb'] ?? 'GET');
$controllerName = $this->buildControllerName($controller);
$actionName = $this->buildActionName($action);
$appName = $simpleRoute['app'] ?? $this->appName;
$split = explode('#', $name, 2);
if (count($split) !== 2) {
throw new \UnexpectedValueException('Invalid route name');
}
list($controller, $action) = $split;
if (isset($simpleRoute['app'])) {
// Legacy routes that need to be globally available while they are handled by an app
// E.g. '/f/{id}', '/s/{token}', '/call/{token}', …
$controllerName = str_replace('controllerController', 'Controller', $controllerName);
if ($controllerName === 'PublicpreviewController') {
$controllerName = 'PublicPreviewController';
} elseif ($controllerName === 'RequesthandlerController') {
$controllerName = 'RequestHandlerController';
}
$controllerName = App::buildAppNamespace($appName) . '\\Controller\\' . $controllerName;
}
$controllerName = $this->buildControllerName($controller);
$actionName = $this->buildActionName($action);
$routeName = $appName . '.' . $controller . '.' . $action . $postfix;
$routeName = $routeNamePrefix . $this->appName . '.' . $controller . '.' . $action . $postfix;
// register the route
$handler = new RouteActionHandler($this->container, $controllerName, $actionName);
$router = $this->router->create($routeName, $url)
->method($verb)
->action($handler);
// register the route
$handler = new RouteActionHandler($this->container, $controllerName, $actionName);
// optionally register requirements for route. This is used to
// tell the route parser how url parameters should be matched
if (array_key_exists('requirements', $simpleRoute)) {
$router->requirements($simpleRoute['requirements']);
}
$router = $this->router->create($routeName, $url)
->method($verb)
->action($handler);
// optionally register defaults for route. This is used to
// tell the route parser how url parameters should be default valued
if (array_key_exists('defaults', $simpleRoute)) {
$router->defaults($simpleRoute['defaults']);
}
// optionally register requirements for route. This is used to
// tell the route parser how url parameters should be matched
if (array_key_exists('requirements', $route)) {
$router->requirements($route['requirements']);
}
// optionally register defaults for route. This is used to
// tell the route parser how url parameters should be default valued
if (array_key_exists('defaults', $route)) {
$router->defaults($route['defaults']);
}
}

View File

@ -151,7 +151,6 @@ class Router implements IRouter {
$this->useCollection($app);
$this->requireRouteFile($file, $app);
$collection = $this->getCollection($app);
$collection->addPrefix('/apps/' . $app);
$this->root->addCollection($collection);
// Also add the OCS collection
@ -336,6 +335,7 @@ class Router implements IRouter {
if ($absolute === false) {
$referenceType = UrlGenerator::ABSOLUTE_PATH;
}
$name = $this->fixLegacyRootName($name);
return $this->getGenerator()->generate($name, $parameters, $referenceType);
} catch (RouteNotFoundException $e) {
$this->logger->logException($e);
@ -343,6 +343,34 @@ class Router implements IRouter {
}
}
protected function fixLegacyRootName(string $routeName): string {
if ($routeName === 'files.viewcontroller.showFile') {
return 'files.View.showFile';
}
if ($routeName === 'files_sharing.sharecontroller.showShare') {
return 'files_sharing.Share.showShare';
}
if ($routeName === 'files_sharing.sharecontroller.showAuthenticate') {
return 'files_sharing.Share.showAuthenticate';
}
if ($routeName === 'files_sharing.sharecontroller.authenticate') {
return 'files_sharing.Share.authenticate';
}
if ($routeName === 'files_sharing.sharecontroller.downloadShare') {
return 'files_sharing.Share.downloadShare';
}
if ($routeName === 'files_sharing.publicpreview.directLink') {
return 'files_sharing.PublicPreview.directLink';
}
if ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') {
return 'cloud_federation_api.RequestHandler.addShare';
}
if ($routeName === 'cloud_federation_api.requesthandlercontroller.receiveNotification') {
return 'cloud_federation_api.RequestHandler.receiveNotification';
}
return $routeName;
}
/**
* To isolate the variable scope used inside the $file it is required in it's own method
*

View File

@ -165,9 +165,7 @@ abstract class AuthPublicShareController extends PublicShareController {
private function getRoute(string $function): string {
$app = strtolower($this->appName);
$class = (new \ReflectionClass($this))->getShortName();
if ($this->appName === 'files_sharing') {
$class = strtolower($class);
} elseif (substr($class, -10) === 'Controller') {
if (substr($class, -10) === 'Controller') {
$class = substr($class, 0, -10);
}
return $app .'.'. $class .'.'. $function;

View File

@ -29,6 +29,8 @@ $server = \OC::$server;
$isEnabled = $server->getAppManager()->isEnabledForUser('cloud_federation_api');
if ($isEnabled) {
// Make sure the routes are loaded
\OC_App::loadApp('cloud_federation_api');
$capabilities = new OCA\CloudFederationAPI\Capabilities($server->getURLGenerator());
header('Content-Type: application/json');
echo json_encode($capabilities->getCapabilities()['ocm']);

View File

@ -5,6 +5,7 @@ namespace Test\AppFramework\Routing;
use OC\AppFramework\DependencyInjection\DIContainer;
use OC\AppFramework\Routing\RouteActionHandler;
use OC\AppFramework\Routing\RouteConfig;
use OC\Route\Route;
use OC\Route\Router;
use OCP\ILogger;
use OCP\Route\IRouter;
@ -16,7 +17,15 @@ class RoutingTest extends \Test\TestCase {
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET']
]];
$this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
$this->assertSimpleRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
}
public function testSimpleRouteWithUnderScoreNames() {
$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() {
@ -33,7 +42,7 @@ class RoutingTest extends \Test\TestCase {
['name' => 'folders#open', 'url' => '/folders/{folderId}/open']
]];
$this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
$this->assertSimpleRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
}
public function testSimpleOCSRouteWithMissingVerb() {
@ -50,7 +59,7 @@ class RoutingTest extends \Test\TestCase {
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
]];
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open');
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
}
public function testSimpleOCSRouteWithLowercaseVerb() {
@ -67,7 +76,7 @@ class RoutingTest extends \Test\TestCase {
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => ['something']]
]];
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
}
public function testSimpleOCSRouteWithRequirements() {
@ -84,7 +93,7 @@ class RoutingTest extends \Test\TestCase {
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', [], 'defaults' => ['param' => 'foobar']]
]];
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
}
@ -102,7 +111,7 @@ class RoutingTest extends \Test\TestCase {
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something']
]];
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
}
public function testSimpleOCSRouteWithPostfix() {
@ -114,7 +123,7 @@ class RoutingTest extends \Test\TestCase {
$this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
}
public function testSimpleRouteWithBrokenName() {
$this->expectException(\UnexpectedValueException::class);
@ -122,10 +131,10 @@ class RoutingTest extends \Test\TestCase {
['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
]];
// router mock
$router = $this->getMockBuilder('\OC\Route\Router')
->setMethods(['create'])
->setConstructorArgs([$this->getMockBuilder(ILogger::class)->getMock()])
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(ILogger::class)])
->getMock();
// load route configuration
@ -135,7 +144,7 @@ class RoutingTest extends \Test\TestCase {
$config->register();
}
public function testSimpleOCSRouteWithBrokenName() {
$this->expectException(\UnexpectedValueException::class);
@ -143,10 +152,10 @@ class RoutingTest extends \Test\TestCase {
['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
]];
// router mock
$router = $this->getMockBuilder('\OC\Route\Router')
->setMethods(['create'])
->setConstructorArgs([$this->getMockBuilder(ILogger::class)->getMock()])
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(ILogger::class)])
->getMock();
// load route configuration
@ -156,14 +165,6 @@ class RoutingTest extends \Test\TestCase {
$config->register();
}
public function testSimpleRouteWithUnderScoreNames() {
$routes = ['routes' => [
['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
]];
$this->assertSimpleRoute($routes, 'admin_folders.open_current', 'DELETE', '/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
}
public function testSimpleOCSRouteWithUnderScoreNames() {
$routes = ['ocs' => [
['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
@ -202,14 +203,7 @@ class RoutingTest extends \Test\TestCase {
$this->assertResource($routes, 'admin_accounts', '/admin/accounts', 'AdminAccountsController', 'id');
}
/**
* @param string $name
* @param string $verb
* @param string $url
* @param string $controllerName
* @param string $actionName
*/
private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements=[], array $defaults=[], $postfix='') {
private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements = [], array $defaults = [], $postfix = '', $allowRootUrl = false): void {
if ($postfix) {
$name .= $postfix;
}
@ -218,10 +212,10 @@ class RoutingTest extends \Test\TestCase {
$container = new DIContainer('app1');
$route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
// router mock
$router = $this->getMockBuilder('\OC\Route\Router')
->setMethods(['create'])
->setConstructorArgs([$this->getMockBuilder(ILogger::class)->getMock()])
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(ILogger::class)])
->getMock();
// we expect create to be called once:
@ -233,6 +227,9 @@ class RoutingTest extends \Test\TestCase {
// load route configuration
$config = new RouteConfig($container, $router, $routes);
if ($allowRootUrl) {
self::invokePrivate($config, 'rootUrlApps', [['app1']]);
}
$config->register();
}
@ -265,10 +262,10 @@ class RoutingTest extends \Test\TestCase {
$container = new DIContainer('app1');
$route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
// router mock
$router = $this->getMockBuilder('\OC\Route\Router')
->setMethods(['create'])
->setConstructorArgs([$this->getMockBuilder(ILogger::class)->getMock()])
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(ILogger::class)])
->getMock();
// we expect create to be called once:
@ -294,8 +291,8 @@ class RoutingTest extends \Test\TestCase {
private function assertOCSResource($yaml, $resourceName, $url, $controllerName, $paramName): void {
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->setMethods(['create'])
->setConstructorArgs([$this->getMockBuilder(ILogger::class)->getMock()])
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(ILogger::class)])
->getMock();
// route mocks
@ -352,10 +349,10 @@ class RoutingTest extends \Test\TestCase {
* @param string $paramName
*/
private function assertResource($yaml, $resourceName, $url, $controllerName, $paramName) {
// router mock
$router = $this->getMockBuilder('\OC\Route\Router')
->setMethods(['create'])
->setConstructorArgs([$this->getMockBuilder(ILogger::class)->getMock()])
/** @var IRouter|MockObject $router */
$router = $this->getMockBuilder(Router::class)
->onlyMethods(['create'])
->setConstructorArgs([$this->createMock(ILogger::class)])
->getMock();
// route mocks
@ -412,7 +409,7 @@ class RoutingTest extends \Test\TestCase {
* @param string $actionName
* @param array $requirements
* @param array $defaults
* @return \PHPUnit_Framework_MockObject_MockObject
* @return MockObject
*/
private function mockRoute(
DIContainer $container,
@ -422,25 +419,25 @@ class RoutingTest extends \Test\TestCase {
array $requirements=[],
array $defaults=[]
) {
$route = $this->getMockBuilder('\OC\Route\Route')
->setMethods(['method', 'action', 'requirements', 'defaults'])
$route = $this->getMockBuilder(Route::class)
->onlyMethods(['method', 'action', 'requirements', 'defaults'])
->disableOriginalConstructor()
->getMock();
$route
->expects($this->exactly(1))
->expects($this->once())
->method('method')
->with($this->equalTo($verb))
->willReturn($route);
$route
->expects($this->exactly(1))
->expects($this->once())
->method('action')
->with($this->equalTo(new RouteActionHandler($container, $controllerName, $actionName)))
->willReturn($route);
if (count($requirements) > 0) {
$route
->expects($this->exactly(1))
->expects($this->once())
->method('requirements')
->with($this->equalTo($requirements))
->willReturn($route);
@ -448,7 +445,7 @@ class RoutingTest extends \Test\TestCase {
if (count($defaults) > 0) {
$route
->expects($this->exactly(1))
->expects($this->once())
->method('defaults')
->with($this->equalTo($defaults))
->willReturn($route);
@ -457,37 +454,3 @@ class RoutingTest extends \Test\TestCase {
return $route;
}
}
/*
#
# sample routes.yaml for ownCloud
#
# the section simple describes one route
routes:
- name: folders#open
url: /folders/{folderId}/open
verb: GET
# controller: name.split()[0]
# action: name.split()[1]
# for a resource following actions will be generated:
# - index
# - create
# - show
# - update
# - destroy
# - new
resources:
accounts:
url: /accounts
folders:
url: /accounts/{accountId}/folders
# actions can be used to define additional actions on the resource
actions:
- name: validate
verb: GET
on-collection: false
* */