You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UrlGeneratorTest.php 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/download.php', [], '/index.php/apps/files/ajax/download.php'],
  110. ['files', 'ajax/download.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/files/ajax/download.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/download.php', [], '/nextcloud/index.php/apps/files/ajax/download.php'],
  117. ['files', 'ajax/download.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/files/ajax/download.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, bool $ignoreFrontController, string $expected): void {
  177. $this->mockBaseUrl();
  178. \OC::$WEBROOT = '/nextcloud';
  179. $this->router->expects($this->once())
  180. ->method('generate')
  181. ->willReturnCallback(function (string $routeName, array $parameters) use ($ignoreFrontController) {
  182. if ($routeName === 'ocs.core.OCS.getCapabilities') {
  183. if (!$ignoreFrontController) {
  184. return '/nextcloud/index.php/ocsapp/cloud/capabilities';
  185. }
  186. return '/nextcloud/ocsapp/cloud/capabilities';
  187. } elseif ($routeName === 'ocs.core.WhatsNew.dismiss') {
  188. if (!$ignoreFrontController) {
  189. return '/nextcloud/index.php/ocsapp/core/whatsnew';
  190. }
  191. return '/nextcloud/ocsapp/core/whatsnew';
  192. }
  193. });
  194. $result = $this->urlGenerator->linkToOCSRouteAbsolute($route);
  195. $this->assertEquals($expected, $result);
  196. }
  197. public function provideOCSRoutes(): array {
  198. return [
  199. ['core.OCS.getCapabilities', false, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
  200. ['core.OCS.getCapabilities', true, 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
  201. ['core.WhatsNew.dismiss', false, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
  202. ['core.WhatsNew.dismiss', true, 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
  203. ];
  204. }
  205. private function mockLinkToDefaultPageUrl(bool $ignoreFrontControllerConfig = false) {
  206. $this->config->expects($this->once())
  207. ->method('getAppValue')
  208. ->with('core', 'defaultpage')
  209. ->willReturn('');
  210. $this->config->expects($this->once())
  211. ->method('getSystemValueBool')
  212. ->with('htaccess.IgnoreFrontController', $this->anything())
  213. ->willReturn($ignoreFrontControllerConfig);
  214. }
  215. public function testLinkToDefaultPageUrlWithRedirectUrlWithoutFrontController() {
  216. $this->mockBaseUrl();
  217. $_REQUEST['redirect_url'] = 'myRedirectUrl.com';
  218. $this->assertSame('http://localhost' . \OC::$WEBROOT . '/myRedirectUrl.com', $this->urlGenerator->linkToDefaultPageUrl());
  219. }
  220. public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithoutFrontController() {
  221. $this->mockBaseUrl();
  222. $this->mockLinkToDefaultPageUrl();
  223. putenv('front_controller_active=false');
  224. $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
  225. $this->assertSame('http://localhost' . \OC::$WEBROOT . '/index.php/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
  226. }
  227. public function testLinkToDefaultPageUrlWithRedirectUrlRedirectBypassWithFrontController() {
  228. $this->mockBaseUrl();
  229. $this->mockLinkToDefaultPageUrl();
  230. putenv('front_controller_active=true');
  231. $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
  232. $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
  233. }
  234. public function testLinkToDefaultPageUrlWithRedirectUrlWithIgnoreFrontController() {
  235. $this->mockBaseUrl();
  236. $this->mockLinkToDefaultPageUrl(true);
  237. putenv('front_controller_active=false');
  238. $_REQUEST['redirect_url'] = 'myRedirectUrl.com@foo.com:a';
  239. $this->assertSame('http://localhost' . \OC::$WEBROOT . '/apps/dashboard/', $this->urlGenerator->linkToDefaultPageUrl());
  240. }
  241. public function imagePathProvider(): array {
  242. return [
  243. ['core', 'favicon-mask.svg', \OC::$WEBROOT . '/core/img/favicon-mask.svg'],
  244. ['files', 'folder.svg', \OC::$WEBROOT . '/apps/files/img/folder.svg'],
  245. ];
  246. }
  247. /**
  248. * @dataProvider imagePathProvider
  249. */
  250. public function testImagePath(string $appName, string $file, string $result): void {
  251. $this->assertSame($result, $this->urlGenerator->imagePath($appName, $file));
  252. }
  253. }