From 0e2988770a2124b0172772849608507f963d45a8 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Fri, 20 Oct 2023 12:20:51 +0200 Subject: [PATCH] IBX-6504: Added unit tests --- .../Routing/Tests/UrlAliasRouterTest.php | 69 ++++++++++++++++++- .../MVC/Symfony/Routing/UrlAliasRouter.php | 21 +++++- 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/eZ/Publish/Core/MVC/Symfony/Routing/Tests/UrlAliasRouterTest.php b/eZ/Publish/Core/MVC/Symfony/Routing/Tests/UrlAliasRouterTest.php index a0ace6b9bf..bc834cdf35 100644 --- a/eZ/Publish/Core/MVC/Symfony/Routing/Tests/UrlAliasRouterTest.php +++ b/eZ/Publish/Core/MVC/Symfony/Routing/Tests/UrlAliasRouterTest.php @@ -777,7 +777,7 @@ public function testGenerateWithContentId() public function testGenerateWithContentIdWithMissingMainLocation() { - $this->expectException(\LogicException::class); + $this->expectException(\TypeError::class); $contentId = 456; $contentInfo = new ContentInfo(['id' => $contentId, 'mainLocationId' => null]); @@ -795,4 +795,71 @@ public function testGenerateWithContentIdWithMissingMainLocation() $referenceType ); } + + public function testGenerateForLocationIdWithForcedLanguageCode(): void + { + $locationId = 22; + $languageCode = 'ger-DE'; + $location = new Location(['id' => $locationId]); + $parameters = ['forcedLanguageCode' => $languageCode]; + $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH; + $generatedLink = '/foo/bar'; + + $this->locationService + ->expects(self::once()) + ->method('loadLocation') + ->with($locationId, [$languageCode]) + ->willReturn($location); + $this->urlALiasGenerator + ->expects(self::once()) + ->method('generate') + ->with($location, [], $referenceType) + ->willReturn($generatedLink); + + self::assertSame( + $generatedLink, + $this->router->generate( + UrlAliasRouter::URL_ALIAS_ROUTE_NAME, + $parameters + ['locationId' => $locationId], + $referenceType + ) + ); + } + + public function testGenerateForContentIdWithForcedLanguageCode(): void + { + $locationId = 23; + $contentId = 34; + $languageCode = 'ger-DE'; + $location = new Location(['id' => $locationId]); + $contentInfo = new ContentInfo(['id' => $contentId, 'mainLocationId' => $locationId]); + $parameters = ['forcedLanguageCode' => $languageCode]; + $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH; + $generatedLink = '/foo/bar'; + + $this->contentService + ->expects(self::once()) + ->method('loadContentInfo') + ->with($contentId) + ->will(self::returnValue($contentInfo)); + $this->locationService + ->expects(self::once()) + ->method('loadLocation') + ->with($contentInfo->mainLocationId, [$languageCode]) + ->willReturn($location); + $this->urlALiasGenerator + ->expects(self::once()) + ->method('generate') + ->with($location, [], $referenceType) + ->willReturn($generatedLink); + + self::assertSame( + $generatedLink, + $this->router->generate( + UrlAliasRouter::URL_ALIAS_ROUTE_NAME, + $parameters + ['contentId' => $contentId], + $referenceType + ) + ); + } } diff --git a/eZ/Publish/Core/MVC/Symfony/Routing/UrlAliasRouter.php b/eZ/Publish/Core/MVC/Symfony/Routing/UrlAliasRouter.php index f76e3a8985..b78f044941 100644 --- a/eZ/Publish/Core/MVC/Symfony/Routing/UrlAliasRouter.php +++ b/eZ/Publish/Core/MVC/Symfony/Routing/UrlAliasRouter.php @@ -318,21 +318,36 @@ public function generate(string $name, array $parameters = [], int $referenceTyp $parameters['locationId'], isset($parameters['forcedLanguageCode']) ? [$parameters['forcedLanguageCode']] : null ); - unset($parameters['location'], $parameters['locationId'], $parameters['viewType'], $parameters['layout']); + unset( + $parameters['location'], + $parameters['locationId'], + $parameters['viewType'], + $parameters['layout'], + $parameters['forcedLanguageCode'], + ); return $this->generator->generate($location, $parameters, $referenceType); } if (isset($parameters['contentId'])) { $contentInfo = $this->contentService->loadContentInfo($parameters['contentId']); - unset($parameters['contentId'], $parameters['viewType'], $parameters['layout']); + $location = $this->locationService->loadLocation( + $contentInfo->mainLocationId, + isset($parameters['forcedLanguageCode']) ? [$parameters['forcedLanguageCode']] : null + ); + unset( + $parameters['contentId'], + $parameters['viewType'], + $parameters['layout'], + $parameters['forcedLanguageCode'], + ); if (empty($contentInfo->mainLocationId)) { throw new LogicException('Cannot generate a UrlAlias route for content without main Location.'); } return $this->generator->generate( - $this->locationService->loadLocation($contentInfo->mainLocationId), + $location, $parameters, $referenceType );