Visitar URL original
Handle empty strings in the `StringResolver` class (see #7112) · contao/core-bundle@4aa0e49 · GitHub
Skip to content

Commit 4aa0e49

Browse files
authored
Handle empty strings in the StringResolver class (see #7112)
Description ----------- We encountered an `ErrorException` when the page of external redirect type has an insert tag that returns an empty string. The problem in fact happens in the `UrlHelper` component of Symfony, but I think we should fix it on our side nevertheless. Stack trace: ``` ErrorException: Warning: Uninitialized string offset 0 #36 /vendor/symfony/http-foundation/UrlHelper.php(42): Symfony\Component\HttpFoundation\UrlHelper::getAbsoluteUrl #35 /vendor/contao/core-bundle/src/Routing/Content/StringResolver.php(40): Contao\CoreBundle\Routing\Content\StringResolver::resolve #34 /vendor/contao/core-bundle/src/Routing/ContentUrlGenerator.php(150): Contao\CoreBundle\Routing\ContentUrlGenerator::resolveContent #33 /vendor/contao/core-bundle/src/Routing/ContentUrlGenerator.php(161): Contao\CoreBundle\Routing\ContentUrlGenerator::resolveContent #32 /vendor/contao/core-bundle/src/Routing/ContentUrlGenerator.php(70): Contao\CoreBundle\Routing\ContentUrlGenerator::generate #31 /vendor/contao/core-bundle/contao/modules/Module.php(349): Contao\Module::renderNavigation #30 /vendor/contao/core-bundle/contao/modules/Module.php(326): Contao\Module::renderNavigation #29 /vendor/contao/core-bundle/contao/modules/Module.php(326): Contao\Module::renderNavigation #28 /vendor/contao/core-bundle/contao/modules/ModuleNavigation.php(82): Contao\ModuleNavigation::compile #27 /vendor/contao/core-bundle/contao/modules/Module.php(213): Contao\Module::generate #26 /vendor/contao/core-bundle/contao/modules/ModuleNavigation.php(45): Contao\ModuleNavigation::generate #25 /vendor/contao/core-bundle/contao/library/Contao/Controller.php(410): Contao\Controller::getFrontendModule … #1 /vendor/friendsofsymfony/http-cache/src/SymfonyCache/EventDispatchingHttpCache.php(96): Contao\ManagerBundle\HttpKernel\ContaoCache::handle #0 /public/index.php(42): null ``` Commits ------- 756336e7 Fix a potential issue if the insert tags return an empty string in th… 0a9330c2 Throw an exception if the insert tag returns an empty value 46de08ef CS + Unit test 89521539 Rename the test method
1 parent 176f1b5 commit 4aa0e49

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎src/Routing/Content/StringResolver.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Contao\CoreBundle\Routing\Content;
1414

15+
use Contao\CoreBundle\Exception\ForwardPageNotFoundException;
1516
use Contao\CoreBundle\InsertTag\InsertTagParser;
1617
use Contao\PageModel;
1718
use Symfony\Component\HttpFoundation\RequestStack;
@@ -36,6 +37,10 @@ public function resolve(object $content): ContentUrlResult|null
3637

3738
$url = $this->insertTagParser->replaceInline($content->value);
3839

40+
if ('' === $url) {
41+
throw new ForwardPageNotFoundException();
42+
}
43+
3944
if (!parse_url($url, PHP_URL_SCHEME)) {
4045
$url = $this->urlHelper->getAbsoluteUrl($url);
4146
}

‎tests/Routing/Content/StringResolverTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Contao\CoreBundle\Tests\Routing\Content;
1414

1515
use Contao\ArticleModel;
16+
use Contao\CoreBundle\Exception\ForwardPageNotFoundException;
1617
use Contao\CoreBundle\InsertTag\InsertTagParser;
1718
use Contao\CoreBundle\Routing\Content\StringResolver;
1819
use Contao\CoreBundle\Routing\Content\StringUrl;
@@ -43,6 +44,29 @@ public function testAbstainsIfContentIsNotAStringUrl(): void
4344
$this->assertNull($result);
4445
}
4546

47+
public function testThrowsExceptionIfInsertTagIsEmpty(): void
48+
{
49+
$content = new StringUrl('{{link_url::42}}');
50+
51+
$insertTagParser = $this->createMock(InsertTagParser::class);
52+
$insertTagParser
53+
->expects($this->once())
54+
->method('replaceInline')
55+
->with($content->value)
56+
->willReturn('')
57+
;
58+
59+
$requestStack = new RequestStack();
60+
$requestContext = new RequestContext();
61+
$urlHelper = new UrlHelper($requestStack, $requestContext);
62+
63+
$resolver = new StringResolver($insertTagParser, $urlHelper, $requestStack, $requestContext);
64+
65+
$this->expectException(ForwardPageNotFoundException::class);
66+
67+
$resolver->resolve($content);
68+
}
69+
4670
/**
4771
* @dataProvider stringUrlProvider
4872
*/

0 commit comments

Comments
 (0)