Merge pull request #12553 from nextcloud/bugfix/noid/allow-empty-string-in-getAbsoluteURL

Allow empty string in get absolute url
This commit is contained in:
Morris Jobke 2018-11-22 10:26:23 +01:00 committed by GitHub
commit ede53e06c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 34 deletions

View File

@ -237,13 +237,13 @@ class URLGenerator implements IURLGenerator {
* @return string the absolute version of the url * @return string the absolute version of the url
*/ */
public function getAbsoluteURL(string $url): string { public function getAbsoluteURL(string $url): string {
$separator = $url[0] === '/' ? '' : '/'; $separator = strpos($url, '/') === 0 ? '' : '/';
if (\OC::$CLI && !\defined('PHPUNIT_RUN')) { if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/'); return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
} }
// The ownCloud web root can already be prepended. // The ownCloud web root can already be prepended.
if(substr($url, 0, \strlen(\OC::$WEBROOT)) === \OC::$WEBROOT) { if(\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) {
$url = substr($url, \strlen(\OC::$WEBROOT)); $url = substr($url, \strlen(\OC::$WEBROOT));
} }

View File

@ -14,8 +14,6 @@ use OCP\IURLGenerator;
/** /**
* Class UrlGeneratorTest * Class UrlGeneratorTest
*
* @group DB
*/ */
class UrlGeneratorTest extends \Test\TestCase { class UrlGeneratorTest extends \Test\TestCase {
@ -74,7 +72,7 @@ class UrlGeneratorTest extends \Test\TestCase {
* @dataProvider provideSubDirAppUrlParts * @dataProvider provideSubDirAppUrlParts
*/ */
public function testLinkToSubDir($app, $file, $args, $expectedResult) { public function testLinkToSubDir($app, $file, $args, $expectedResult) {
\OC::$WEBROOT = '/owncloud'; \OC::$WEBROOT = '/nextcloud';
$result = $this->urlGenerator->linkTo($app, $file, $args); $result = $this->urlGenerator->linkTo($app, $file, $args);
$this->assertEquals($expectedResult, $result); $this->assertEquals($expectedResult, $result);
} }
@ -84,32 +82,32 @@ class UrlGeneratorTest extends \Test\TestCase {
*/ */
public function testLinkToRouteAbsolute($route, $expected) { public function testLinkToRouteAbsolute($route, $expected) {
$this->mockBaseUrl(); $this->mockBaseUrl();
\OC::$WEBROOT = '/owncloud'; \OC::$WEBROOT = '/nextcloud';
$result = $this->urlGenerator->linkToRouteAbsolute($route); $result = $this->urlGenerator->linkToRouteAbsolute($route);
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
public function provideRoutes() { public function provideRoutes() {
return array( return [
array('files_ajax_list', 'http://localhost/owncloud/index.php/apps/files/ajax/list.php'), ['files_ajax_list', 'http://localhost/nextcloud/index.php/apps/files/ajax/list.php'],
array('core.Preview.getPreview', 'http://localhost/owncloud/index.php/core/preview.png'), ['core.Preview.getPreview', 'http://localhost/nextcloud/index.php/core/preview.png'],
); ];
} }
public function provideDocRootAppUrlParts() { public function provideDocRootAppUrlParts() {
return array( return [
array('files', 'ajax/list.php', array(), '/index.php/apps/files/ajax/list.php'), ['files', 'ajax/list.php', [], '/index.php/apps/files/ajax/list.php'],
array('files', 'ajax/list.php', array('trut' => 'trat', 'dut' => 'dat'), '/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'), ['files', 'ajax/list.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'],
array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/index.php?trut=trat&dut=dat'), ['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php?trut=trat&dut=dat'],
); ];
} }
public function provideSubDirAppUrlParts() { public function provideSubDirAppUrlParts() {
return array( return [
array('files', 'ajax/list.php', array(), '/owncloud/index.php/apps/files/ajax/list.php'), ['files', 'ajax/list.php', [], '/nextcloud/index.php/apps/files/ajax/list.php'],
array('files', 'ajax/list.php', array('trut' => 'trat', 'dut' => 'dat'), '/owncloud/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'), ['files', 'ajax/list.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'],
array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/owncloud/index.php?trut=trat&dut=dat'), ['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php?trut=trat&dut=dat'],
); ];
} }
/** /**
@ -131,34 +129,36 @@ class UrlGeneratorTest extends \Test\TestCase {
*/ */
function testGetAbsoluteURLSubDir($url, $expectedResult) { function testGetAbsoluteURLSubDir($url, $expectedResult) {
$this->mockBaseUrl(); $this->mockBaseUrl();
\OC::$WEBROOT = '/owncloud'; \OC::$WEBROOT = '/nextcloud';
$result = $this->urlGenerator->getAbsoluteURL($url); $result = $this->urlGenerator->getAbsoluteURL($url);
$this->assertEquals($expectedResult, $result); $this->assertEquals($expectedResult, $result);
} }
public function provideDocRootURLs() { public function provideDocRootURLs() {
return array( return [
array("index.php", "http://localhost/index.php"), ['index.php', 'http://localhost/index.php'],
array("/index.php", "http://localhost/index.php"), ['/index.php', 'http://localhost/index.php'],
array("/apps/index.php", "http://localhost/apps/index.php"), ['/apps/index.php', 'http://localhost/apps/index.php'],
array("apps/index.php", "http://localhost/apps/index.php"), ['apps/index.php', 'http://localhost/apps/index.php'],
); ];
} }
public function provideSubDirURLs() { public function provideSubDirURLs() {
return array( return [
array("index.php", "http://localhost/owncloud/index.php"), ['', 'http://localhost/nextcloud/'],
array("/index.php", "http://localhost/owncloud/index.php"), ['/', 'http://localhost/nextcloud/'],
array("/apps/index.php", "http://localhost/owncloud/apps/index.php"), ['index.php', 'http://localhost/nextcloud/index.php'],
array("apps/index.php", "http://localhost/owncloud/apps/index.php"), ['/index.php', 'http://localhost/nextcloud/index.php'],
); ['/apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
['apps/index.php', 'http://localhost/nextcloud/apps/index.php'],
];
} }
public function testGetBaseUrl() { public function testGetBaseUrl() {
$this->mockBaseUrl(); $this->mockBaseUrl();
\OC::$WEBROOT = '/nextcloud'; \OC::$WEBROOT = '/nextcloud';
$actual = $this->urlGenerator->getBaseUrl(); $actual = $this->urlGenerator->getBaseUrl();
$expected = "http://localhost/nextcloud"; $expected = 'http://localhost/nextcloud';
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }