1
0

UrlGeneratorTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. public function setUp() {
  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. }
  38. private function mockBaseUrl() {
  39. $this->request->expects($this->once())
  40. ->method('getServerProtocol')
  41. ->willReturn('http');
  42. $this->request->expects($this->once())
  43. ->method('getServerHost')
  44. ->willReturn('localhost');
  45. }
  46. /**
  47. * @small
  48. * test linkTo URL construction
  49. * @dataProvider provideDocRootAppUrlParts
  50. */
  51. public function testLinkToDocRoot($app, $file, $args, $expectedResult) {
  52. \OC::$WEBROOT = '';
  53. $result = $this->urlGenerator->linkTo($app, $file, $args);
  54. $this->assertEquals($expectedResult, $result);
  55. }
  56. /**
  57. * @small
  58. * test linkTo URL construction in sub directory
  59. * @dataProvider provideSubDirAppUrlParts
  60. */
  61. public function testLinkToSubDir($app, $file, $args, $expectedResult) {
  62. \OC::$WEBROOT = '/owncloud';
  63. $result = $this->urlGenerator->linkTo($app, $file, $args);
  64. $this->assertEquals($expectedResult, $result);
  65. }
  66. /**
  67. * @dataProvider provideRoutes
  68. */
  69. public function testLinkToRouteAbsolute($route, $expected) {
  70. $this->mockBaseUrl();
  71. \OC::$WEBROOT = '/owncloud';
  72. $result = $this->urlGenerator->linkToRouteAbsolute($route);
  73. $this->assertEquals($expected, $result);
  74. }
  75. public function provideRoutes() {
  76. return array(
  77. array('files_ajax_list', 'http://localhost/owncloud/index.php/apps/files/ajax/list.php'),
  78. array('core.Preview.getPreview', 'http://localhost/owncloud/index.php/core/preview.png'),
  79. );
  80. }
  81. public function provideDocRootAppUrlParts() {
  82. return array(
  83. array('files', 'ajax/list.php', array(), '/index.php/apps/files/ajax/list.php'),
  84. array('files', 'ajax/list.php', array('trut' => 'trat', 'dut' => 'dat'), '/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'),
  85. array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/index.php?trut=trat&dut=dat'),
  86. );
  87. }
  88. public function provideSubDirAppUrlParts() {
  89. return array(
  90. array('files', 'ajax/list.php', array(), '/owncloud/index.php/apps/files/ajax/list.php'),
  91. array('files', 'ajax/list.php', array('trut' => 'trat', 'dut' => 'dat'), '/owncloud/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'),
  92. array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/owncloud/index.php?trut=trat&dut=dat'),
  93. );
  94. }
  95. /**
  96. * @small
  97. * test absolute URL construction
  98. * @dataProvider provideDocRootURLs
  99. */
  100. function testGetAbsoluteURLDocRoot($url, $expectedResult) {
  101. $this->mockBaseUrl();
  102. \OC::$WEBROOT = '';
  103. $result = $this->urlGenerator->getAbsoluteURL($url);
  104. $this->assertEquals($expectedResult, $result);
  105. }
  106. /**
  107. * @small
  108. * test absolute URL construction
  109. * @dataProvider provideSubDirURLs
  110. */
  111. function testGetAbsoluteURLSubDir($url, $expectedResult) {
  112. $this->mockBaseUrl();
  113. \OC::$WEBROOT = '/owncloud';
  114. $result = $this->urlGenerator->getAbsoluteURL($url);
  115. $this->assertEquals($expectedResult, $result);
  116. }
  117. public function provideDocRootURLs() {
  118. return array(
  119. array("index.php", "http://localhost/index.php"),
  120. array("/index.php", "http://localhost/index.php"),
  121. array("/apps/index.php", "http://localhost/apps/index.php"),
  122. array("apps/index.php", "http://localhost/apps/index.php"),
  123. );
  124. }
  125. public function provideSubDirURLs() {
  126. return array(
  127. array("index.php", "http://localhost/owncloud/index.php"),
  128. array("/index.php", "http://localhost/owncloud/index.php"),
  129. array("/apps/index.php", "http://localhost/owncloud/apps/index.php"),
  130. array("apps/index.php", "http://localhost/owncloud/apps/index.php"),
  131. );
  132. }
  133. public function testGetBaseUrl() {
  134. $this->mockBaseUrl();
  135. \OC::$WEBROOT = '/nextcloud';
  136. $actual = $this->urlGenerator->getBaseUrl();
  137. $expected = "http://localhost/nextcloud";
  138. $this->assertEquals($expected, $actual);
  139. }
  140. }