$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'];
/** @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);
use OCP\IConfig;
use OCP\IRequest;
use OCP\IURLGenerator;
+use OCP\Route\IRouter;
use RuntimeException;
/**
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;
}
/**
* 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);
}
/**
}
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) {
use OCP\IConfig;
use OCP\IRequest;
use OCP\IURLGenerator;
+use OCP\Route\IRouter;
/**
* Class UrlGeneratorTest
private $cacheFactory;
/** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
private $request;
+ /** @var \PHPUnit\Framework\MockObject\MockObject|IRouter */
+ private $router;
/** @var IURLGenerator */
private $urlGenerator;
/** @var string */
$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;
}
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);
}
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);
}