Skip to content

Commit

Permalink
Refactor method and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmany committed Jan 22, 2020
1 parent d037d28 commit 560ecaa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/bundles/CoreBundle/Helper/UrlHelper.php
Expand Up @@ -286,18 +286,19 @@ private static function removeTrailingNonAlphaNumeric($string)
}

/**
* FILTER_VALIDATE_URL allow only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed]. This validation passed also special characters in URL.
* This method return true with special characters in URL, for example https://domain.tld/é.pdf
* filter_var($url, FILTER_VALIDATE_URL) allow only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed].
*
* @param string $url
*
* @return bool
*/
public static function isValidateUrl($url)
public static function isValidUrl($url)
{
$path = parse_url($url, PHP_URL_PATH);
$encodedPath = array_map('urlencode', explode('/', $path));
$url = str_replace($path, implode('/', $encodedPath), $url);

return filter_var($url, FILTER_VALIDATE_URL);
return (bool) filter_var($url, FILTER_VALIDATE_URL);
}
}
8 changes: 8 additions & 0 deletions app/bundles/CoreBundle/Tests/unit/Helper/UrlHelperTest.php
Expand Up @@ -149,4 +149,12 @@ public function testGetUrlsFromPlaintextWithSymbols()
)
);
}

public function testUrlValid()
{
$this->assertTrue(UrlHelper::isValidUrl('https://domain.tld/e'));
$this->assertTrue(UrlHelper::isValidUrl('https://domain.tld/é'));
$this->assertFalse(UrlHelper::isValidUrl('notvalidurl'));
$this->assertFalse(UrlHelper::isValidUrl('notvalidurlé'));
}
}
2 changes: 1 addition & 1 deletion app/bundles/PageBundle/Controller/PublicController.php
Expand Up @@ -480,7 +480,7 @@ public function redirectAction($redirectId)

$url = UrlHelper::sanitizeAbsoluteUrl($url);

if (!UrlHelper::isValidateUrl($url)) {
if (!UrlHelper::isValidUrl($url)) {
throw $this->createNotFoundException($this->translator->trans('mautic.core.url.error.404', ['%url%' => $url]));
}

Expand Down

0 comments on commit 560ecaa

Please sign in to comment.