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-8012: UrlAliasGenerator should load location with provided languages #361

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11980,11 +11980,6 @@ parameters:
count: 1
path: src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Routing\\\\Generator\\\\UrlAliasGenerator\\:\\:getPathPrefixByRootLocationId\\(\\) has parameter \\$languages with no value type specified in iterable type array\\.$#"
count: 1
path: src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php

-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Routing\\\\Generator\\\\UrlAliasGenerator\\:\\:loadLocation\\(\\) should return Ibexa\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Location but returns Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Location\\.$#"
count: 1
Expand Down
13 changes: 8 additions & 5 deletions src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ public function setExcludedUriPrefixes(array $excludedUriPrefixes)
* Returns path corresponding to $rootLocationId.
*
* @param int $rootLocationId
* @param array $languages
* @param array<string>|null $languages
* @param string $siteaccess
*
* @return string
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*/
public function getPathPrefixByRootLocationId($rootLocationId, $languages = null, $siteaccess = null)
{
Expand All @@ -122,7 +124,7 @@ public function getPathPrefixByRootLocationId($rootLocationId, $languages = null
$this->pathPrefixMap[$siteaccess][$rootLocationId] = $this->repository
->getURLAliasService()
->reverseLookup(
$this->loadLocation($rootLocationId),
$this->loadLocation($rootLocationId, $languages),
null,
false,
$languages
Expand Down Expand Up @@ -157,15 +159,16 @@ public function isUriPrefixExcluded($uri)
* Not to be used for link generation.
*
* @param int $locationId
* @param array<string>|null $languages
*
* @return \Ibexa\Core\Repository\Values\Content\Location
*/
public function loadLocation($locationId)
public function loadLocation($locationId, ?array $languages = null)
{
return $this->repository->sudo(
static function (Repository $repository) use ($locationId) {
static function (Repository $repository) use ($locationId, $languages) {
/* @var $repository \Ibexa\Core\Repository\Repository */
return $repository->getLocationService()->loadLocation($locationId);
return $repository->getLocationService()->loadLocation($locationId, $languages);
}
);
}
Expand Down
78 changes: 78 additions & 0 deletions tests/lib/MVC/Symfony/Routing/UrlAliasGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,84 @@ public function providerTestDoGenerateWithSiteaccess()
];
}

public function testDoGenerateWithSiteAccessLoadsLocationWithLanguages(): void
{
$siteSiteAccess = 'site';
$gerSiteAccess = 'ger';
$parameters = ['siteaccess' => $gerSiteAccess];

$saRootLocations = [
$siteSiteAccess => $siteSiteAccessLocationId = 2,
$gerSiteAccess => $gerSiteAccessLocationId = 71,
];
$treeRootUrlAliases = [
$siteSiteAccessLocationId => new URLAlias(['path' => '/']),
$gerSiteAccessLocationId => new URLAlias(['path' => '/ger']),
];

$this->configResolver
->expects(self::any())
->method('getParameter')
->will(
self::returnValueMap(
[
['languages', null, $siteSiteAccess, ['eng-GB']],
['languages', null, $gerSiteAccess, ['ger-DE']],
[
'content.tree_root.location_id',
null,
$siteSiteAccess,
$saRootLocations[$siteSiteAccess],
],
[
'content.tree_root.location_id',
null,
$gerSiteAccess,
$saRootLocations[$gerSiteAccess],
],
]
)
);

$location = new Location(['id' => $gerSiteAccessLocationId]);

$this->urlAliasService
->expects(self::once())
->method('listLocationAliases')
->with($location, false, null, null, ['ger-DE'])
->willReturn(
[
new URLAlias(
['path' => $gerRootLocationAlias = '/ger-folder'],
),
],
);

$this->locationService
->expects(self::once())
->method('loadLocation')
->with($gerSiteAccessLocationId, ['ger-DE'])
->willReturn($location);

$this->urlAliasService
->expects(self::once())
->method('reverseLookup')
->with($location, null, false, ['ger-DE'])
->willReturn($treeRootUrlAliases[$location->id]);

$this->urlAliasGenerator->setSiteAccess(
new SiteAccess(
$gerSiteAccess,
'default',
)
);

self::assertSame(
$gerRootLocationAlias,
$this->urlAliasGenerator->doGenerate($location, $parameters)
);
}

public function testDoGenerateNoUrlAlias()
{
$location = new Location(['id' => 123, 'contentInfo' => new ContentInfo(['id' => 456])]);
Expand Down
Loading