UrlGeneratorTest.php 4.2 KB

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