Skip to content

Commit

Permalink
[BUGFIX] Fix type error in UrlHelper
Browse files Browse the repository at this point in the history
If some url part is not set the getters have to return
an emtpy string.

Fixes: #2756
  • Loading branch information
DrWh0286 authored and dkd-kaehm committed Aug 17, 2021
1 parent a99275a commit 17f1653
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
10 changes: 8 additions & 2 deletions Classes/Report/SiteHandlingStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,19 @@ protected function fetchInvalidPartsOfUri(UriInterface $uri): string
/* @var UrlHelper $solrUriHelper */
$solrUriHelper = GeneralUtility::makeInstance(UrlHelper::class, $uri);
try {
$solrUriHelper->getScheme();
$scheme = $solrUriHelper->getScheme();
if (empty($scheme) ) {
$invalidParts .= 'scheme';
}
} catch (\TypeError $error) {
$invalidParts .= 'scheme';
}

try {
$solrUriHelper->getHost();
$host = $solrUriHelper->getHost();
if (empty($host)) {
$invalidParts .= ', host';
}
} catch (\TypeError $error) {
$invalidParts .= ', host';
}
Expand Down
8 changes: 4 additions & 4 deletions Classes/System/Url/UrlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function setHost(string $host)
*/
public function getHost(): string
{
return $this->getUrlPart('host');
return $this->getUrlPart('host') ?? '';
}

/**
Expand All @@ -121,7 +121,7 @@ public function setPort(string $port)
*/
public function getPort(): string
{
return $this->getUrlPart('port');
return $this->getUrlPart('port') ?? '';
}

/**
Expand All @@ -139,7 +139,7 @@ public function setScheme(string $scheme)
*/
public function getScheme(): string
{
return $this->getUrlPart('scheme');
return $this->getUrlPart('scheme') ?? '';
}

/**
Expand All @@ -157,7 +157,7 @@ public function setPath($path)
*/
public function getPath(): string
{
return $this->getUrlPart('path');
return $this->getUrlPart('path') ?? '';
}

/**
Expand Down
36 changes: 36 additions & 0 deletions Tests/Unit/System/Url/UrlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,40 @@ public function testGetUnmodifiedUrl($uri)
$urlHelper = new UrlHelper($uri);
$this->assertSame($uri, $urlHelper->getUrl(), 'Could not get unmodified url');
}

/**
* @test
*/
public function ifNoSchemeIsGivenGetSchemeReturnsAnEmptyString(): void
{
$urlHelper = new UrlHelper('www.google.de');
$this->assertSame('', $urlHelper->getScheme());
}

/**
* @test
*/
public function ifNoPathIsGivenGetPathReturnsAnEmptyString(): void
{
$urlHelper = new UrlHelper('https://www.google.de');
$this->assertSame('', $urlHelper->getPath());
}

/**
* @test
*/
public function ifNoPortIsGivenGetPortReturnsAnEmptyString(): void
{
$urlHelper = new UrlHelper('https://www.google.de');
$this->assertSame('', $urlHelper->getPort());
}

/**
* @test
*/
public function ifNoHostIsGivenGetHostReturnsAnEmptyString(): void
{
$urlHelper = new UrlHelper('/my/path/to/a/site');
$this->assertSame('', $urlHelper->getHost());
}
}

0 comments on commit 17f1653

Please sign in to comment.