UrlGeneratorTest.php 8.8 KB

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