]> source.dussan.org Git - nextcloud-server.git/commitdiff
Properly inject IRouter into URLGenerator to properly encapsulate tests 13712/head
authorMorris Jobke <hey@morrisjobke.de>
Tue, 11 Aug 2020 20:28:29 +0000 (22:28 +0200)
committerMorris Jobke <hey@morrisjobke.de>
Wed, 19 Aug 2020 20:00:47 +0000 (22:00 +0200)
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
lib/private/Route/Router.php
lib/private/Server.php
lib/private/URLGenerator.php
tests/lib/UrlGeneratorTest.php

index ec91e08472c61cc33bb2db208e765fa388b6cff8..94c637e5e0d001de2c963140f9e896b3f042bf84 100644 (file)
@@ -73,7 +73,7 @@ class Router implements IRouter {
                $this->logger = $logger;
                $baseUrl = \OC::$WEBROOT;
                if (!(\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
-                       $baseUrl = \OC::$server->getURLGenerator()->linkTo('', 'index.php');
+                       $baseUrl .= '/index.php';
                }
                if (!\OC::$CLI && isset($_SERVER['REQUEST_METHOD'])) {
                        $method = $_SERVER['REQUEST_METHOD'];
index a934628a047de4c18b059023345a242f3abfb700..9b452f21ce17832fe7d589c56d3786fd08231672 100644 (file)
@@ -644,16 +644,7 @@ class Server extends ServerContainer implements IServerContainer {
                /** @deprecated 19.0.0 */
                $this->registerDeprecatedAlias('L10NFactory', IFactory::class);
 
-               $this->registerService(IURLGenerator::class, function (Server $c) {
-                       $config = $c->getConfig();
-                       $cacheFactory = $c->getMemCacheFactory();
-                       $request = $c->getRequest();
-                       return new \OC\URLGenerator(
-                               $config,
-                               $cacheFactory,
-                               $request
-                       );
-               });
+               $this->registerAlias(IURLGenerator::class, URLGenerator::class);
                /** @deprecated 19.0.0 */
                $this->registerDeprecatedAlias('URLGenerator', IURLGenerator::class);
 
index e90d8cf1d1c9c8c60df542603be6cac66f4311b7..4da6a91b6c9611ba881d447fc9b39026751aca8a 100644 (file)
@@ -44,6 +44,7 @@ use OCP\ICacheFactory;
 use OCP\IConfig;
 use OCP\IRequest;
 use OCP\IURLGenerator;
+use OCP\Route\IRouter;
 use RuntimeException;
 
 /**
@@ -56,18 +57,17 @@ class URLGenerator implements IURLGenerator {
        private $cacheFactory;
        /** @var IRequest */
        private $request;
+       /** @var IRouter*/
+       private $router;
 
-       /**
-        * @param IConfig $config
-        * @param ICacheFactory $cacheFactory
-        * @param IRequest $request
-        */
        public function __construct(IConfig $config,
                                                                ICacheFactory $cacheFactory,
-                                                               IRequest $request) {
+                                                               IRequest $request,
+                                                               IRouter $router) {
                $this->config = $config;
                $this->cacheFactory = $cacheFactory;
                $this->request = $request;
+               $this->router = $router;
        }
 
        /**
@@ -80,8 +80,7 @@ class URLGenerator implements IURLGenerator {
         * Returns a url to the given route.
         */
        public function linkToRoute(string $routeName, array $arguments = []): string {
-               // TODO: mock router
-               return \OC::$server->getRouter()->generate($routeName, $arguments);
+               return $this->router->generate($routeName, $arguments);
        }
 
        /**
@@ -97,7 +96,7 @@ class URLGenerator implements IURLGenerator {
        }
 
        public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
-               $route = \OC::$server->getRouter()->generate('ocs.'.$routeName, $arguments, false);
+               $route = $this->router->generate('ocs.'.$routeName, $arguments, false);
 
                $indexPhpPos = strpos($route, '/index.php/');
                if ($indexPhpPos !== false) {
index 5043dfb7a524dcac97ea56981a41f71d95644834..b5db39dbf39c3bd453cb7f791f8e318b9e9d2a75 100644 (file)
@@ -12,6 +12,7 @@ use OCP\ICacheFactory;
 use OCP\IConfig;
 use OCP\IRequest;
 use OCP\IURLGenerator;
+use OCP\Route\IRouter;
 
 /**
  * Class UrlGeneratorTest
@@ -26,6 +27,8 @@ class UrlGeneratorTest extends \Test\TestCase {
        private $cacheFactory;
        /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
        private $request;
+       /** @var \PHPUnit\Framework\MockObject\MockObject|IRouter */
+       private $router;
        /** @var IURLGenerator */
        private $urlGenerator;
        /** @var string */
@@ -36,10 +39,12 @@ class UrlGeneratorTest extends \Test\TestCase {
                $this->config = $this->createMock(IConfig::class);
                $this->cacheFactory = $this->createMock(ICacheFactory::class);
                $this->request = $this->createMock(IRequest::class);
+               $this->router = $this->createMock(IRouter::class);
                $this->urlGenerator = new \OC\URLGenerator(
                        $this->config,
                        $this->cacheFactory,
-                       $this->request
+                       $this->request,
+                       $this->router
                );
                $this->originalWebRoot = \OC::$WEBROOT;
        }
@@ -86,6 +91,15 @@ class UrlGeneratorTest extends \Test\TestCase {
        public function testLinkToRouteAbsolute($route, $expected) {
                $this->mockBaseUrl();
                \OC::$WEBROOT = '/nextcloud';
+               $this->router->expects($this->once())
+                       ->method('generate')
+                       ->willReturnCallback(function ($routeName, $parameters) {
+                               if ($routeName === 'core.Preview.getPreview') {
+                                       return '/index.php/core/preview.png';
+                               } elseif ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') {
+                                       return '/index.php/ocm/shares';
+                               }
+                       });
                $result = $this->urlGenerator->linkToRouteAbsolute($route);
                $this->assertEquals($expected, $result);
        }
@@ -171,6 +185,15 @@ class UrlGeneratorTest extends \Test\TestCase {
        public function testLinkToOCSRouteAbsolute(string $route, string $expected) {
                $this->mockBaseUrl();
                \OC::$WEBROOT = '/nextcloud';
+               $this->router->expects($this->once())
+                       ->method('generate')
+                       ->willReturnCallback(function ($routeName, $parameters) {
+                               if ($routeName === 'ocs.core.OCS.getCapabilities') {
+                                       return '/index.php/ocsapp/cloud/capabilities';
+                               } elseif ($routeName === 'ocs.core.WhatsNew.dismiss') {
+                                       return '/index.php/ocsapp/core/whatsnew';
+                               }
+                       });
                $result = $this->urlGenerator->linkToOCSRouteAbsolute($route);
                $this->assertEquals($expected, $result);
        }