Skip to content
Snippets Groups Projects
Verified Commit c6143563 authored by Dave Long's avatar Dave Long
Browse files

Issue #3490710 by mfb, catch, spokje: Catch potential exception when calling...

Issue #3490710 by mfb, catch, spokje: Catch potential exception when calling Request::create() in PathBasedBreadcrumbBuilder
parent 5c12effc
No related branches found
No related tags found
No related merge requests found
Pipeline #364827 canceled
......@@ -20,6 +20,7 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
......@@ -211,7 +212,12 @@ protected function getRequestForPath($path, array $exclude) {
if (!empty($exclude[$path])) {
return NULL;
}
$request = Request::create($path);
try {
$request = Request::create($path);
}
catch (BadRequestException) {
return NULL;
}
// Performance optimization: set a short accept header to reduce overhead in
// AcceptHeaderMatcher when matching the request.
$request->headers->set('Accept', 'text/html');
......
......@@ -336,6 +336,28 @@ public function testBuildWithNonProcessedPath(): void {
$this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
}
/**
* Tests the build method with an invalid path.
*
* @covers ::build
* @covers ::getRequestForPath
*/
public function testBuildWithInvalidPath(): void {
// The parse_url() function returns FALSE for '/:123/foo' so the
// Request::create() method therefore considers it to be an invalid URI.
$this->context->expects($this->once())
->method('getPathInfo')
->willReturn('/:123/foo/bar');
$breadcrumb = $this->builder->build($this->createMock('Drupal\Core\Routing\RouteMatchInterface'));
// No path matched, though at least the frontpage is displayed.
$this->assertEquals([0 => new Link('Home', new Url('<front>'))], $breadcrumb->getLinks());
$this->assertEqualsCanonicalizing(['url.path.is_front', 'url.path.parent'], $breadcrumb->getCacheContexts());
$this->assertEqualsCanonicalizing([], $breadcrumb->getCacheTags());
$this->assertEquals(Cache::PERMANENT, $breadcrumb->getCacheMaxAge());
}
/**
* Tests the applied method.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment