Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty string in get absolute url #12553

Merged
merged 2 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/private/URLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ public function imagePath(string $app, string $image): string {
* @return string the absolute version of the url
*/
public function getAbsoluteURL(string $url): string {
$separator = $url[0] === '/' ? '' : '/';
$separator = strpos($url, '/') === 0 ? '' : '/';

if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
}
// 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));
}

Expand Down
64 changes: 32 additions & 32 deletions tests/lib/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

/**
* Class UrlGeneratorTest
*
* @group DB
*/
class UrlGeneratorTest extends \Test\TestCase {

Expand Down Expand Up @@ -74,7 +72,7 @@ public function testLinkToDocRoot($app, $file, $args, $expectedResult) {
* @dataProvider provideSubDirAppUrlParts
*/
public function testLinkToSubDir($app, $file, $args, $expectedResult) {
\OC::$WEBROOT = '/owncloud';
\OC::$WEBROOT = '/nextcloud';
$result = $this->urlGenerator->linkTo($app, $file, $args);
$this->assertEquals($expectedResult, $result);
}
Expand All @@ -84,32 +82,32 @@ public function testLinkToSubDir($app, $file, $args, $expectedResult) {
*/
public function testLinkToRouteAbsolute($route, $expected) {
$this->mockBaseUrl();
\OC::$WEBROOT = '/owncloud';
\OC::$WEBROOT = '/nextcloud';
$result = $this->urlGenerator->linkToRouteAbsolute($route);
$this->assertEquals($expected, $result);
}

public function provideRoutes() {
return array(
array('files_ajax_list', 'http://localhost/owncloud/index.php/apps/files/ajax/list.php'),
array('core.Preview.getPreview', 'http://localhost/owncloud/index.php/core/preview.png'),
);
return [
['files_ajax_list', 'http://localhost/nextcloud/index.php/apps/files/ajax/list.php'],
['core.Preview.getPreview', 'http://localhost/nextcloud/index.php/core/preview.png'],
];
}

public function provideDocRootAppUrlParts() {
return array(
array('files', 'ajax/list.php', array(), '/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'),
array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/index.php?trut=trat&dut=dat'),
);
return [
['files', 'ajax/list.php', [], '/index.php/apps/files/ajax/list.php'],
['files', 'ajax/list.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'],
['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/index.php?trut=trat&dut=dat'],
];
}

public function provideSubDirAppUrlParts() {
return array(
array('files', 'ajax/list.php', array(), '/owncloud/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'),
array('', 'index.php', array('trut' => 'trat', 'dut' => 'dat'), '/owncloud/index.php?trut=trat&dut=dat'),
);
return [
['files', 'ajax/list.php', [], '/nextcloud/index.php/apps/files/ajax/list.php'],
['files', 'ajax/list.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php/apps/files/ajax/list.php?trut=trat&dut=dat'],
['', 'index.php', ['trut' => 'trat', 'dut' => 'dat'], '/nextcloud/index.php?trut=trat&dut=dat'],
];
}

/**
Expand All @@ -131,34 +129,36 @@ function testGetAbsoluteURLDocRoot($url, $expectedResult) {
*/
function testGetAbsoluteURLSubDir($url, $expectedResult) {
$this->mockBaseUrl();
\OC::$WEBROOT = '/owncloud';
\OC::$WEBROOT = '/nextcloud';
$result = $this->urlGenerator->getAbsoluteURL($url);
$this->assertEquals($expectedResult, $result);
}

public function provideDocRootURLs() {
return array(
array("index.php", "http://localhost/index.php"),
array("/index.php", "http://localhost/index.php"),
array("/apps/index.php", "http://localhost/apps/index.php"),
array("apps/index.php", "http://localhost/apps/index.php"),
);
return [
['index.php', 'http://localhost/index.php'],
['/index.php', 'http://localhost/index.php'],
['/apps/index.php', 'http://localhost/apps/index.php'],
['apps/index.php', 'http://localhost/apps/index.php'],
];
}

public function provideSubDirURLs() {
return array(
array("index.php", "http://localhost/owncloud/index.php"),
array("/index.php", "http://localhost/owncloud/index.php"),
array("/apps/index.php", "http://localhost/owncloud/apps/index.php"),
array("apps/index.php", "http://localhost/owncloud/apps/index.php"),
);
return [
['', 'http://localhost/nextcloud/'],
['/', 'http://localhost/nextcloud/'],
['index.php', 'http://localhost/nextcloud/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() {
$this->mockBaseUrl();
\OC::$WEBROOT = '/nextcloud';
$actual = $this->urlGenerator->getBaseUrl();
$expected = "http://localhost/nextcloud";
$expected = 'http://localhost/nextcloud';
$this->assertEquals($expected, $actual);
}

Expand Down