]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add tests and inject IRequest
authorJulius Härtl <jus@bitgrid.net>
Thu, 15 Jun 2017 19:51:51 +0000 (21:51 +0200)
committerJulius Härtl <jus@bitgrid.net>
Sun, 2 Jul 2017 12:03:35 +0000 (14:03 +0200)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
lib/private/Server.php
lib/private/URLGenerator.php
tests/lib/UrlGeneratorTest.php

index 159fab20be479c3304a622e5ec81d56252ba278d..489683aa12723d0fad24a0d74ceee0ffd9bb4334 100644 (file)
@@ -414,9 +414,11 @@ class Server extends ServerContainer implements IServerContainer {
                $this->registerService(\OCP\IURLGenerator::class, function (Server $c) {
                        $config = $c->getConfig();
                        $cacheFactory = $c->getMemCacheFactory();
+                       $request = $c->getRequest();
                        return new \OC\URLGenerator(
                                $config,
-                               $cacheFactory
+                               $cacheFactory,
+                               $request
                        );
                });
                $this->registerAlias('URLGenerator', \OCP\IURLGenerator::class);
@@ -433,13 +435,15 @@ class Server extends ServerContainer implements IServerContainer {
                $this->registerAlias('UserCache', \OCP\ICache::class);
 
                $this->registerService(Factory::class, function (Server $c) {
+
                        $arrayCacheFactory = new \OC\Memcache\Factory('', $c->getLogger(),
                                '\\OC\\Memcache\\ArrayCache',
                                '\\OC\\Memcache\\ArrayCache',
                                '\\OC\\Memcache\\ArrayCache'
                        );
                        $config = $c->getConfig();
-                       $urlGenerator = new URLGenerator($config, $arrayCacheFactory);
+                       $request = $c->getRequest();
+                       $urlGenerator = new URLGenerator($config, $arrayCacheFactory, $request);
 
                        if ($config->getSystemValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
                                $v = \OC_App::getAppVersions();
index 6db675e7d7705b2462319764bfbc5f26ca105b2c..073d40b0de8239db7bbf7adad82c68c893160101 100644 (file)
@@ -35,7 +35,9 @@ namespace OC;
 
 use OCP\ICacheFactory;
 use OCP\IConfig;
+use OCP\IRequest;
 use OCP\IURLGenerator;
+use OCP\Route\IRoute;
 use RuntimeException;
 
 /**
@@ -46,15 +48,20 @@ class URLGenerator implements IURLGenerator {
        private $config;
        /** @var ICacheFactory */
        private $cacheFactory;
+       /** @var IRequest */
+       private $request;
 
        /**
         * @param IConfig $config
         * @param ICacheFactory $cacheFactory
+        * @param IRequest $request
         */
        public function __construct(IConfig $config,
-                                                               ICacheFactory $cacheFactory) {
+                                                               ICacheFactory $cacheFactory,
+                                                               IRequest $request) {
                $this->config = $config;
                $this->cacheFactory = $cacheFactory;
+               $this->request = $request;
        }
 
        /**
@@ -244,7 +251,6 @@ class URLGenerator implements IURLGenerator {
         * @return string base url of the current request
         */
        public function getBaseUrl() {
-               $request = \OC::$server->getRequest();
-               return $request->getServerProtocol() . '://' . $request->getServerHost() . \OC::$WEBROOT;
+               return $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
        }
 }
index 28fd2d336d27333ca709f67057b36e38ef63ff04..69067f51e081c26dcde790cf42d3a399ffe63ccf 100644 (file)
@@ -9,6 +9,8 @@
 namespace Test;
 use OCP\ICacheFactory;
 use OCP\IConfig;
+use OCP\IRequest;
+use OCP\IURLGenerator;
 
 /**
  * Class UrlGeneratorTest
@@ -17,6 +19,37 @@ use OCP\IConfig;
  */
 class UrlGeneratorTest extends \Test\TestCase {
 
+       /** @var \PHPUnit_Framework_MockObject_MockObject|IConfig */
+       private $config;
+       /** @var \PHPUnit_Framework_MockObject_MockObject|ICacheFactory */
+       private $cacheFactory;
+       /** @var \PHPUnit_Framework_MockObject_MockObject|IRequest */
+       private $request;
+       /** @var IURLGenerator */
+       private $urlGenerator;
+
+       public function setUp() {
+               parent::setUp();
+               $this->config = $this->createMock(IConfig::class);
+               $this->cacheFactory = $this->createMock(ICacheFactory::class);
+               $this->request = $this->createMock(IRequest::class);
+               $this->urlGenerator = new \OC\URLGenerator(
+                       $this->config,
+                       $this->cacheFactory,
+                       $this->request
+               );
+       }
+
+       private function mockBaseUrl() {
+               $this->request->expects($this->once())
+                       ->method('getServerProtocol')
+                       ->willReturn('http');
+               $this->request->expects($this->once())
+                       ->method('getServerHost')
+                       ->willReturn('localhost');
+
+       }
+
        /**
         * @small
         * test linkTo URL construction
@@ -24,11 +57,7 @@ class UrlGeneratorTest extends \Test\TestCase {
         */
        public function testLinkToDocRoot($app, $file, $args, $expectedResult) {
                \OC::$WEBROOT = '';
-               $config = $this->createMock(IConfig::class);
-               $cacheFactory = $this->createMock(ICacheFactory::class);
-               $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
-               $result = $urlGenerator->linkTo($app, $file, $args);
-
+               $result = $this->urlGenerator->linkTo($app, $file, $args);
                $this->assertEquals($expectedResult, $result);
        }
 
@@ -39,11 +68,7 @@ class UrlGeneratorTest extends \Test\TestCase {
         */
        public function testLinkToSubDir($app, $file, $args, $expectedResult) {
                \OC::$WEBROOT = '/owncloud';
-               $config = $this->createMock(IConfig::class);
-               $cacheFactory = $this->createMock(ICacheFactory::class);
-               $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
-               $result = $urlGenerator->linkTo($app, $file, $args);
-
+               $result = $this->urlGenerator->linkTo($app, $file, $args);
                $this->assertEquals($expectedResult, $result);
        }
 
@@ -51,13 +76,10 @@ class UrlGeneratorTest extends \Test\TestCase {
         * @dataProvider provideRoutes
         */
        public function testLinkToRouteAbsolute($route, $expected) {
+               $this->mockBaseUrl();
                \OC::$WEBROOT = '/owncloud';
-               $config = $this->createMock(IConfig::class);
-               $cacheFactory = $this->createMock(ICacheFactory::class);
-               $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
-               $result = $urlGenerator->linkToRouteAbsolute($route);
+               $result = $this->urlGenerator->linkToRouteAbsolute($route);
                $this->assertEquals($expected, $result);
-
        }
 
        public function provideRoutes() {
@@ -89,13 +111,9 @@ class UrlGeneratorTest extends \Test\TestCase {
         * @dataProvider provideDocRootURLs
         */
        function testGetAbsoluteURLDocRoot($url, $expectedResult) {
-
+               $this->mockBaseUrl();
                \OC::$WEBROOT = '';
-               $config = $this->createMock(IConfig::class);
-               $cacheFactory = $this->createMock(ICacheFactory::class);
-               $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
-               $result = $urlGenerator->getAbsoluteURL($url);
-
+               $result = $this->urlGenerator->getAbsoluteURL($url);
                $this->assertEquals($expectedResult, $result);
        }
 
@@ -105,13 +123,9 @@ class UrlGeneratorTest extends \Test\TestCase {
         * @dataProvider provideSubDirURLs
         */
        function testGetAbsoluteURLSubDir($url, $expectedResult) {
-
+               $this->mockBaseUrl();
                \OC::$WEBROOT = '/owncloud';
-               $config = $this->createMock(IConfig::class);
-               $cacheFactory = $this->createMock(ICacheFactory::class);
-               $urlGenerator = new \OC\URLGenerator($config, $cacheFactory);
-               $result = $urlGenerator->getAbsoluteURL($url);
-
+               $result = $this->urlGenerator->getAbsoluteURL($url);
                $this->assertEquals($expectedResult, $result);
        }
 
@@ -132,5 +146,14 @@ class UrlGeneratorTest extends \Test\TestCase {
                        array("apps/index.php", "http://localhost/owncloud/apps/index.php"),
                        );
        }
+
+       public function testGetBaseUrl() {
+               $this->mockBaseUrl();
+               \OC::$WEBROOT = '/nextcloud';
+               $actual = $this->urlGenerator->getBaseUrl();
+               $expected = "http://localhost/nextcloud";
+               $this->assertEquals($expected, $actual);
+       }
+
 }