Skip to content

Commit

Permalink
IBX-6504: Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barw4 committed Oct 20, 2023
1 parent 05d8a8e commit 0e29887
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
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

0 comments on commit 0e29887

Please sign in to comment.