UrlGeneratorTest.php 5.3 KB

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