UrlGeneratorTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OC\Route\Router;
  9. use OCP\ICacheFactory;
  10. use OCP\IConfig;
  11. use OCP\IRequest;
  12. use OCP\IURLGenerator;
  13. use OCP\IUserSession;
  14. /**
  15. * Class UrlGeneratorTest
  16. *
  17. * @package Test
  18. */
  19. class UrlGeneratorTest extends \Test\TestCase {
  20. /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
  21. private $config;
  22. /** @var \PHPUnit\Framework\MockObject\MockObject|IUserSession */
  23. private $userSession;
  24. /** @var \PHPUnit\Framework\MockObject\MockObject|ICacheFactory */
  25. private $cacheFactory;
  26. /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
  27. private $request;
  28. /** @var \PHPUnit\Framework\MockObject\MockObject|Router */
  29. private $router;
  30. /** @var IURLGenerator */
  31. private $urlGenerator;
  32. /** @var string */
  33. private $originalWebRoot;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->config = $this->createMock(IConfig::class);
  37. $this->userSession = $this->createMock(IUserSession::class);
  38. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  39. $this->request = $this->createMock(IRequest::class);
  40. $this->router = $this->createMock(Router::class);
  41. $this->urlGenerator = new \OC\URLGenerator(
  42. $this->config,
  43. $this->userSession,
  44. $this->cacheFactory,
  45. $this->request,
  46. $this->router
  47. );
  48. $this->originalWebRoot = \OC::$WEBROOT;
  49. }
  50. protected function tearDown(): void {
  51. // Reset webRoot
  52. \OC::$WEBROOT = $this->originalWebRoot;
  53. }
  54. private function mockBaseUrl() {
  55. $this->request->expects($this->once())
  56. ->method('getServerProtocol')
  57. ->willReturn('http');
  58. $this->request->expects($this->once())
  59. ->method('getServerHost')
  60. ->willReturn('localhost');
  61. }
  62. /**
  63. * @small
  64. * test linkTo URL construction
  65. * @dataProvider provideDocRootAppUrlParts
  66. */
  67. public function testLinkToDocRoot($app, $file, $args, $expectedResult): void {
  68. \OC::$WEBROOT = '';
  69. $result = $this->urlGenerator->linkTo($app, $file, $args);
  70. $this->assertEquals($expectedResult, $result);
  71. }
  72. /**
  73. * @small
  74. * test linkTo URL construction in sub directory
  75. * @dataProvider provideSubDirAppUrlParts
  76. */
  77. public function testLinkToSubDir($app, $file, $args, $expectedResult): void {
  78. \OC::$WEBROOT = '/nextcloud';
  79. $result = $this->urlGenerator->linkTo($app, $file, $args);
  80. $this->assertEquals($expectedResult, $result);
  81. }
  82. /**
  83. * @dataProvider provideRoutes
  84. */
  85. public function testLinkToRouteAbsolute($route, $expected): void {
  86. $this->mockBaseUrl();
  87. \OC::$WEBROOT = '/nextcloud';
  88. $this->router->expects($this->once())
  89. ->method('generate')
  90. ->willReturnCallback(function ($routeName, $parameters) {
  91. if ($routeName === 'core.Preview.getPreview') {
  92. return '/index.php/core/preview.png';
  93. } elseif ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') {
  94. return '/index.php/ocm/shares';
  95. }
  96. });
  97. $result = $this->urlGenerator->linkToRouteAbsolute($route);
  98. $this->assertEquals($expected, $result);
  99. }
  100. public function provideRoutes() {
  101. return [
  102. ['core.Preview.getPreview', 'http://localhost/nextcloud/index.php/core/preview.png'],
  103. ['cloud_federation_api.requesthandlercontroller.addShare', 'http://localhost/nextcloud/index.php/ocm/shares'],
  104. ];
  105. }
  106. public function provideDocRootAppUrlParts() {
  107. return [
  108. ['files', 'ajax/download.php', [], '/index.php/apps/files/ajax/download.php'],
  109. ['files', 'ajax/download.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/files/ajax/download.php?trut=trat&dut=dat'],
  110. ['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php?trut=trat&dut=dat'],
  111. ];
  112. }
  113. public function provideSubDirAppUrlParts() {
  114. return [
  115. ['files', 'ajax/download.php', [], '/nextcloud/index.php/apps/files/ajax/download.php'],
  116. ['files', 'ajax/download.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/files/ajax/download.php?trut=trat&dut=dat'],
  117. ['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php?trut=trat&dut=dat'],
  118. ];
  119. }
  120. /**
  121. * @small
  122. * test absolute URL construction
  123. * @dataProvider provideDocRootURLs
  124. */
  125. public function testGetAbsoluteURLDocRoot($url, $expectedResult): void {
  126. $this->mockBaseUrl();
  127. \OC::$WEBROOT = '';
  128. $result = $this->urlGenerator->getAbsoluteURL($url);
  129. $this->assertEquals($expectedResult, $result);
  130. }
  131. /**
  132. * @small
  133. * test absolute URL construction
  134. * @dataProvider provideSubDirURLs
  135. */
  136. public function testGetAbsoluteURLSubDir($url, $expectedResult): void {
  137. $this->mockBaseUrl();
  138. \OC::$WEBROOT = '/nextcloud';
  139. $result = $this->urlGenerator->getAbsoluteURL($url);
  140. $this->assertEquals($expectedResult, $result);
  141. }
  142. public function provideDocRootURLs() {
  143. return [
  144. ['index.php', 'http://localhost/index.php'],
  145. ['/index.php', 'http://localhost/index.php'],
  146. ['/apps/index.php', 'http://localhost/apps/index.php'],
  147. ['apps/index.php', 'http://localhost/apps/index.php'],
  148. ];
  149. }
  150. public function provideSubDirURLs() {
  151. return [
  152. ['', 'http://localhost/nextcloud/'],
  153. ['/', 'http://localhost/nextcloud/'],
  154. ['index.php', 'http://localhost/nextcloud/index.php'],
  155. ['/index.php', 'http://localhost/nextcloud/index.php'],
  156. ['/apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
  157. ['apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
  158. ];
  159. }
  160. public function testGetBaseUrl(): void {
  161. $this->mockBaseUrl();
  162. \OC::$WEBROOT = '/nextcloud';
  163. $actual = $this->urlGenerator->getBaseUrl();
  164. $expected = 'http://localhost/nextcloud';
  165. $this->assertEquals($expected, $actual);
  166. }
  167. public function testGetWebroot(): void {
  168. \OC::$WEBROOT = '/nextcloud';
  169. $actual = $this->urlGenerator->getWebroot();
  170. $this->assertEquals(\OC::$WEBROOT, $actual);
  171. }
  172. /**
  173. * @dataProvider provideOCSRoutes
  174. */
  175. public function testLinkToOCSRouteAbsolute(string $route, bool $ignoreFrontController, string $expected): void {
  176. $this->mockBaseUrl();
  177. \OC::$WEBROOT = '/nextcloud';
  178. $this->router->expects($this->once())
  179. ->method('generate')
  180. ->willReturnCallback(function (string $routeName, array $parameters) use ($ignoreFrontController) {
  181. if ($routeName === 'ocs.core.OCS.getCapabilities') {
  182. if (!$ignoreFrontController) {
  183. return '/nextcloud/index.php/ocsapp/cloud/capabilities';
  184. }
  185. return '/nextcloud/ocsapp/cloud/capabilities';
  186. } elseif ($routeName === 'ocs.core.WhatsNew.dismiss') {
  187. if (!$ignoreFrontController) {
  188. return '/nextcloud/index.php/ocsapp/core/whatsnew';
  189. }
  190. return '/nextcloud/ocsapp/core/whatsnew';
  191. }
  192. });
  193. $result = $this->urlGenerator->linkToOCSRouteAbsolute($route);
  194. $this->assertEquals($expected, $result);
  195. }
  196. public function provideOCSRoutes(): array {
  197. return [
  198. ['core.OCS.getCapabilities', false, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
  199. ['core.OCS.getCapabilities', true, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
  200. ['core.WhatsNew.dismiss', false, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
  201. ['core.WhatsNew.dismiss', true, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
  202. ];
  203. }
  204. private function mockLinkToDefaultPageUrl(bool $ignoreFrontControllerConfig = false) {
  205. $this->config->expects($this->once())
  206. ->method('getAppValue')
  207. ->with('core', 'defaultpage')
  208. ->willReturn('');
  209. $this->config->expects($this->once())
  210. ->method('getSystemValueBool')
  211. ->with('htaccess.IgnoreFrontController', $this->anything())
  212. ->willReturn($ignoreFrontControllerConfig);
  213. }
  214. public function testLinkToDefaultPageUrlWithRedirectUrlWithoutFrontController(): void {
  215. $this->mockBaseUrl();
  216. $_REQUEST['redirect_url'] = 'myRedirectUrl.com';
  217. $this->assertSame('http://localhost' . \OC::$WEBROOT . '/myRedirectUrl.com', $this->urlGenerator->linkToDefaultPageUrl());
  218. }
  219. public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController(): void {
  220. $this->mockBaseUrl();
  221. $this->mockLinkToDefaultPageUrl();
  222. putenv('front_controller_active=false');
  223. $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
  224. $this->assertSame('http://localhost' . \OC::$WEBROOT . '/index.php/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
  225. }
  226. public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController(): void {
  227. $this->mockBaseUrl();
  228. $this->mockLinkToDefaultPageUrl();
  229. putenv('front_controller_active=true');
  230. $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
  231. $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
  232. }
  233. public function testLinkToDefaultPageUrlWithRedirectUrlWithIgnoreFrontController(): void {
  234. $this->mockBaseUrl();
  235. $this->mockLinkToDefaultPageUrl(true);
  236. putenv('front_controller_active=false');
  237. $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
  238. $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
  239. }
  240. public function imagePathProvider(): array {
  241. return [
  242. ['core', 'favicon-mask.svg', \OC::$WEBROOT . '/core/img/favicon-mask.svg'],
  243. ['files', 'folder.svg', \OC::$WEBROOT . '/apps/files/img/folder.svg'],
  244. ];
  245. }
  246. /**
  247. * @dataProvider imagePathProvider
  248. */
  249. public function testImagePath(string $appName, string $file, string $result): void {
  250. $this->assertSame($result, $this->urlGenerator->imagePath($appName, $file));
  251. }
  252. }