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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 OCP\ICacheFactory;
  10. use OCP\IConfig;
  11. use OCP\IRequest;
  12. use OCP\IURLGenerator;
  13. use OCP\Route\IRouter;
  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|ICacheFactory */
  23. private $cacheFactory;
  24. /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
  25. private $request;
  26. /** @var \PHPUnit\Framework\MockObject\MockObject|IRouter */
  27. private $router;
  28. /** @var IURLGenerator */
  29. private $urlGenerator;
  30. /** @var string */
  31. private $originalWebRoot;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->config = $this->createMock(IConfig::class);
  35. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  36. $this->request = $this->createMock(IRequest::class);
  37. $this->router = $this->createMock(IRouter::class);
  38. $this->urlGenerator = new \OC\URLGenerator(
  39. $this->config,
  40. $this->cacheFactory,
  41. $this->request,
  42. $this->router
  43. );
  44. $this->originalWebRoot = \OC::$WEBROOT;
  45. }
  46. protected function tearDown(): void {
  47. // Reset webRoot
  48. \OC::$WEBROOT = $this->originalWebRoot;
  49. }
  50. private function mockBaseUrl() {
  51. $this->request->expects($this->once())
  52. ->method('getServerProtocol')
  53. ->willReturn('http');
  54. $this->request->expects($this->once())
  55. ->method('getServerHost')
  56. ->willReturn('localhost');
  57. }
  58. /**
  59. * @small
  60. * test linkTo URL construction
  61. * @dataProvider provideDocRootAppUrlParts
  62. */
  63. public function testLinkToDocRoot($app, $file, $args, $expectedResult) {
  64. \OC::$WEBROOT = '';
  65. $result = $this->urlGenerator->linkTo($app, $file, $args);
  66. $this->assertEquals($expectedResult, $result);
  67. }
  68. /**
  69. * @small
  70. * test linkTo URL construction in sub directory
  71. * @dataProvider provideSubDirAppUrlParts
  72. */
  73. public function testLinkToSubDir($app, $file, $args, $expectedResult) {
  74. \OC::$WEBROOT = '/nextcloud';
  75. $result = $this->urlGenerator->linkTo($app, $file, $args);
  76. $this->assertEquals($expectedResult, $result);
  77. }
  78. /**
  79. * @dataProvider provideRoutes
  80. */
  81. public function testLinkToRouteAbsolute($route, $expected) {
  82. $this->mockBaseUrl();
  83. \OC::$WEBROOT = '/nextcloud';
  84. $this->router->expects($this->once())
  85. ->method('generate')
  86. ->willReturnCallback(function ($routeName, $parameters) {
  87. if ($routeName === 'core.Preview.getPreview') {
  88. return '/index.php/core/preview.png';
  89. } elseif ($routeName === 'cloud_federation_api.requesthandlercontroller.addShare') {
  90. return '/index.php/ocm/shares';
  91. }
  92. });
  93. $result = $this->urlGenerator->linkToRouteAbsolute($route);
  94. $this->assertEquals($expected, $result);
  95. }
  96. public function provideRoutes() {
  97. return [
  98. ['core.Preview.getPreview', 'http://localhost/nextcloud/index.php/core/preview.png'],
  99. ['cloud_federation_api.requesthandlercontroller.addShare', 'http://localhost/nextcloud/index.php/ocm/shares'],
  100. ];
  101. }
  102. public function provideDocRootAppUrlParts() {
  103. return [
  104. ['files', 'ajax/list.php', [], '/index.php/apps/files/ajax/list.php'],
  105. ['files', 'ajax/list.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'],
  106. ['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php?trut=trat&dut=dat'],
  107. ];
  108. }
  109. public function provideSubDirAppUrlParts() {
  110. return [
  111. ['files', 'ajax/list.php', [], '/nextcloud/index.php/apps/files/ajax/list.php'],
  112. ['files', 'ajax/list.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'],
  113. ['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php?trut=trat&dut=dat'],
  114. ];
  115. }
  116. /**
  117. * @small
  118. * test absolute URL construction
  119. * @dataProvider provideDocRootURLs
  120. */
  121. public function testGetAbsoluteURLDocRoot($url, $expectedResult) {
  122. $this->mockBaseUrl();
  123. \OC::$WEBROOT = '';
  124. $result = $this->urlGenerator->getAbsoluteURL($url);
  125. $this->assertEquals($expectedResult, $result);
  126. }
  127. /**
  128. * @small
  129. * test absolute URL construction
  130. * @dataProvider provideSubDirURLs
  131. */
  132. public function testGetAbsoluteURLSubDir($url, $expectedResult) {
  133. $this->mockBaseUrl();
  134. \OC::$WEBROOT = '/nextcloud';
  135. $result = $this->urlGenerator->getAbsoluteURL($url);
  136. $this->assertEquals($expectedResult, $result);
  137. }
  138. public function provideDocRootURLs() {
  139. return [
  140. ['index.php', 'http://localhost/index.php'],
  141. ['/index.php', 'http://localhost/index.php'],
  142. ['/apps/index.php', 'http://localhost/apps/index.php'],
  143. ['apps/index.php', 'http://localhost/apps/index.php'],
  144. ];
  145. }
  146. public function provideSubDirURLs() {
  147. return [
  148. ['', 'http://localhost/nextcloud/'],
  149. ['/', 'http://localhost/nextcloud/'],
  150. ['index.php', 'http://localhost/nextcloud/index.php'],
  151. ['/index.php', 'http://localhost/nextcloud/index.php'],
  152. ['/apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
  153. ['apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
  154. ];
  155. }
  156. public function testGetBaseUrl() {
  157. $this->mockBaseUrl();
  158. \OC::$WEBROOT = '/nextcloud';
  159. $actual = $this->urlGenerator->getBaseUrl();
  160. $expected = 'http://localhost/nextcloud';
  161. $this->assertEquals($expected, $actual);
  162. }
  163. /**
  164. * @dataProvider provideOCSRoutes
  165. */
  166. public function testLinkToOCSRouteAbsolute(string $route, string $expected) {
  167. $this->mockBaseUrl();
  168. \OC::$WEBROOT = '/nextcloud';
  169. $this->router->expects($this->once())
  170. ->method('generate')
  171. ->willReturnCallback(function ($routeName, $parameters) {
  172. if ($routeName === 'ocs.core.OCS.getCapabilities') {
  173. return '/index.php/ocsapp/cloud/capabilities';
  174. } elseif ($routeName === 'ocs.core.WhatsNew.dismiss') {
  175. return '/index.php/ocsapp/core/whatsnew';
  176. }
  177. });
  178. $result = $this->urlGenerator->linkToOCSRouteAbsolute($route);
  179. $this->assertEquals($expected, $result);
  180. }
  181. public function provideOCSRoutes() {
  182. return [
  183. ['core.OCS.getCapabilities', 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
  184. ['core.WhatsNew.dismiss', 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
  185. ];
  186. }
  187. }