Skip to content

Commit

Permalink
IBX-8012: Fixed handling languages by `UrlAliasGenerator::loadLocatio…
Browse files Browse the repository at this point in the history
…n` (#361)

For more details see https://issues.ibexa.co/browse/IBX-8012 and #361

Key changes:

* Added `$languages` optional parameter to `UrlAliasGenerator::loadLocation`

* Fixed `UrlAliasGenerator::getPathPrefixByRootLocationId` to load path prefix with respect to location's language
  • Loading branch information
barw4 authored May 14, 2024
1 parent 055e4cf commit e22466c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
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

0 comments on commit e22466c

Please sign in to comment.