UrlGeneratorTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * @group DB
  17. */
  18. class UrlGeneratorTest extends \Test\TestCase {
  19. /** @var \PHPUnit_Framework_MockObject_MockObject|IConfig */
  20. private $config;
  21. /** @var \PHPUnit_Framework_MockObject_MockObject|ICacheFactory */
  22. private $cacheFactory;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject|IRequest */
  24. private $request;
  25. /** @var IURLGenerator */
  26. private $urlGenerator;
  27. /** @var string */
  28. private $originalWebRoot;
  29. public function setUp() {
  30. parent::setUp();
  31. $this->config = $this->createMock(IConfig::class);
  32. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  33. $this->request = $this->createMock(IRequest::class);
  34. $this->urlGenerator = new \OC\URLGenerator(
  35. $this->config,
  36. $this->cacheFactory,
  37. $this->request
  38. );
  39. $this->originalWebRoot = \OC::$WEBROOT;
  40. }
  41. public function tearDown() {
  42. // Reset webRoot
  43. \OC::$WEBROOT = $this->originalWebRoot;
  44. }
  45. private function mockBaseUrl() {
  46. $this->request->expects($this->once())
  47. ->method('getServerProtocol')
  48. ->willReturn('http');
  49. $this->request->expects($this->once())
  50. ->method('getServerHost')
  51. ->willReturn('localhost');
  52. }
  53. /**
  54. * @small
  55. * test linkTo URL construction
  56. * @dataProvider provideDocRootAppUrlParts
  57. */
  58. public function testLinkToDocRoot($app, $file, $args, $expectedResult) {
  59. \OC::$WEBROOT = '';
  60. $result = $this->urlGenerator->linkTo($app, $file, $args);
  61. $this->assertEquals($expectedResult, $result);
  62. }
  63. /**
  64. * @small
  65. * test linkTo URL construction in sub directory
  66. * @dataProvider provideSubDirAppUrlParts
  67. */
  68. public function testLinkToSubDir($app, $file, $args, $expectedResult) {
  69. \OC::$WEBROOT = '/owncloud';
  70. $result = $this->urlGenerator->linkTo($app, $file, $args);
  71. $this->assertEquals($expectedResult, $result);
  72. }
  73. /**
  74. * @dataProvider provideRoutes
  75. */
  76. public function testLinkToRouteAbsolute($route, $expected) {
  77. $this->mockBaseUrl();
  78. \OC::$WEBROOT = '/owncloud';
  79. $result = $this->urlGenerator->linkToRouteAbsolute($route);
  80. $this->assertEquals($expected, $result);
  81. }
  82. public function provideRoutes() {
  83. return array(
  84. array('files_ajax_list', 'http://localhost/owncloud/index.php/apps/files/ajax/list.php'),
  85. array('core.Preview.getPreview', 'http://localhost/owncloud/index.php/core/preview.png'),
  86. );
  87. }
  88. public function provideDocRootAppUrlParts() {
  89. return array(
  90. array('files', 'ajax/list.php', array(), '/index.php/apps/files/ajax/list.php'),
  91. array('files', 'ajax/list.php', array('trut' => 'trat', 'dut' => 'dat'), '/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'),
  92. array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/index.php?trut=trat&dut=dat'),
  93. );
  94. }
  95. public function provideSubDirAppUrlParts() {
  96. return array(
  97. array('files', 'ajax/list.php', array(), '/owncloud/index.php/apps/files/ajax/list.php'),
  98. array('files', 'ajax/list.php', array('trut' => 'trat', 'dut' => 'dat'), '/owncloud/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'),
  99. array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/owncloud/index.php?trut=trat&dut=dat'),
  100. );
  101. }
  102. /**
  103. * @small
  104. * test absolute URL construction
  105. * @dataProvider provideDocRootURLs
  106. */
  107. function testGetAbsoluteURLDocRoot($url, $expectedResult) {
  108. $this->mockBaseUrl();
  109. \OC::$WEBROOT = '';
  110. $result = $this->urlGenerator->getAbsoluteURL($url);
  111. $this->assertEquals($expectedResult, $result);
  112. }
  113. /**
  114. * @small
  115. * test absolute URL construction
  116. * @dataProvider provideSubDirURLs
  117. */
  118. function testGetAbsoluteURLSubDir($url, $expectedResult) {
  119. $this->mockBaseUrl();
  120. \OC::$WEBROOT = '/owncloud';
  121. $result = $this->urlGenerator->getAbsoluteURL($url);
  122. $this->assertEquals($expectedResult, $result);
  123. }
  124. public function provideDocRootURLs() {
  125. return array(
  126. array("index.php", "http://localhost/index.php"),
  127. array("/index.php", "http://localhost/index.php"),
  128. array("/apps/index.php", "http://localhost/apps/index.php"),
  129. array("apps/index.php", "http://localhost/apps/index.php"),
  130. );
  131. }
  132. public function provideSubDirURLs() {
  133. return array(
  134. array("index.php", "http://localhost/owncloud/index.php"),
  135. array("/index.php", "http://localhost/owncloud/index.php"),
  136. array("/apps/index.php", "http://localhost/owncloud/apps/index.php"),
  137. array("apps/index.php", "http://localhost/owncloud/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. }