urlgenerator.php 4.0 KB

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