diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 0eccf4c762..7157994fc8 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -211,22 +211,15 @@ public function getUrl($name, $parameters = [], $options = []) { // Generate URL. $options['absolute'] = TRUE; - $empty = [ - '#cache' => [ - 'tags' => ['route_match'], - ], - ]; try { $generated_url = $this->urlGenerator->generateFromRoute($name, $parameters, $options, TRUE); } - catch (RouteNotFoundException $e) { - return $empty; - } - catch (MissingMandatoryParametersException $e) { - return $empty; - } - catch (InvalidParameterException $e) { - return $empty; + catch (RouteNotFoundException | MissingMandatoryParametersException | InvalidParameterException $e) { + return [ + '#cache' => [ + 'tags' => ['route_match'], + ], + ]; } // Return as render array, so we can bubble the bubbleable metadata. diff --git a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php index caf03f674b..36dbc57832 100644 --- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php @@ -376,51 +376,29 @@ public function testLinkWithOverriddenAttributes() { $this->assertEquals(['foo', 'bar'], $build['#url']->getOption('attributes')['class']); } - /** - * @covers ::getUrl - */ - public function testUrlWithRouteNotFound() { - $this->urlGenerator->method('generateFromRoute')->with('this.route_does_not_exist', [], [ - 'absolute' => TRUE, - ], TRUE)->willThrowException(new RouteNotFoundException()); - $build = $this->systemUnderTest->getUrl('this.route_does_not_exist'); - $this->assertEquals([ - '#cache' => [ - 'tags' => ['route_match'], - ], - ], $build); - } - /** * @covers ::getUrl + * @dataProvider providerTestGetUrlException */ - public function testUrlWithMissingParameters() { - $this->urlGenerator->method('generateFromRoute')->with('this.route_expects_parameters', [], [ + public function testGetUrlWithException($exception, $expected) { + $this->urlGenerator->method('generateFromRoute')->with('this.route_does_not_exist', [], [ 'absolute' => TRUE, - ], TRUE)->willThrowException(new MissingMandatoryParametersException()); - $build = $this->systemUnderTest->getUrl('this.route_expects_parameters'); - $this->assertEquals([ - '#cache' => [ - 'tags' => ['route_match'], - ], - ], $build); + ], TRUE)->willThrowException(new $exception()); + $build = $this->systemUnderTest->getUrl('this.route_does_not_exist'); + $this->assertEquals($expected, $build); } - /** - * @covers ::getUrl - */ - public function testUrlWithInvalidParameters() { - $this->urlGenerator->method('generateFromRoute')->with('this.route_expects_integer_parameters', [ - 'param' => 'string', - ], [ - 'absolute' => TRUE, - ], TRUE)->willThrowException(new InvalidParameterException()); - $build = $this->systemUnderTest->getUrl('this.route_expects_integer_parameters', ['param' => 'string']); - $this->assertEquals([ + public function providerTestGetUrlException() { + $build = [ '#cache' => [ 'tags' => ['route_match'], ], - ], $build); + ]; + return [ + [RouteNotFoundException::class, $build], + [MissingMandatoryParametersException::class, $build], + [InvalidParameterException::class, $build], + ]; } }