From 29f898de28ba658e7d341f6ba4490d654c39416a Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Wed, 24 Apr 2024 17:35:17 +0200 Subject: [PATCH] IBX-8012: `UrlAliasGenerator` should load location with provided `languages` --- phpstan-baseline.neon | 5 -- .../Routing/Generator/UrlAliasGenerator.php | 13 ++-- .../Symfony/Routing/UrlAliasGeneratorTest.php | 78 +++++++++++++++++++ 3 files changed, 86 insertions(+), 10 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 64c97358fb..eddd2385ff 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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 diff --git a/src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php b/src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php index 2066456640..3da4acdd54 100644 --- a/src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php +++ b/src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php @@ -103,10 +103,12 @@ public function setExcludedUriPrefixes(array $excludedUriPrefixes) * Returns path corresponding to $rootLocationId. * * @param int $rootLocationId - * @param array $languages + * @param array|null $languages * @param string $siteaccess * * @return string + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException */ public function getPathPrefixByRootLocationId($rootLocationId, $languages = null, $siteaccess = null) { @@ -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 @@ -157,15 +159,16 @@ public function isUriPrefixExcluded($uri) * Not to be used for link generation. * * @param int $locationId + * @param array|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); } ); } diff --git a/tests/lib/MVC/Symfony/Routing/UrlAliasGeneratorTest.php b/tests/lib/MVC/Symfony/Routing/UrlAliasGeneratorTest.php index 774ae160fb..c7d8d2e00f 100644 --- a/tests/lib/MVC/Symfony/Routing/UrlAliasGeneratorTest.php +++ b/tests/lib/MVC/Symfony/Routing/UrlAliasGeneratorTest.php @@ -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])]);