Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6504: Gracefully handled URL generation in RoutingExtension #389

Merged
merged 7 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
IBX-6504: Added unit tests
  • Loading branch information
barw4 committed Oct 20, 2023
commit 0e2988770a2124b0172772849608507f963d45a8
69 changes: 68 additions & 1 deletion eZ/Publish/Core/MVC/Symfony/Routing/Tests/UrlAliasRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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
)
);
}
}
21 changes: 18 additions & 3 deletions eZ/Publish/Core/MVC/Symfony/Routing/UrlAliasRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down