Skip to content

Commit e22466c

Browse files
authored
IBX-8012: Fixed handling languages by UrlAliasGenerator::loadLocation (#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
1 parent 055e4cf commit e22466c

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11980,11 +11980,6 @@ parameters:
1198011980
count: 1
1198111981
path: src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php
1198211982

11983-
-
11984-
message: "#^Method Ibexa\\\\Core\\\\MVC\\\\Symfony\\\\Routing\\\\Generator\\\\UrlAliasGenerator\\:\\:getPathPrefixByRootLocationId\\(\\) has parameter \\$languages with no value type specified in iterable type array\\.$#"
11985-
count: 1
11986-
path: src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php
11987-
1198811983
-
1198911984
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\\.$#"
1199011985
count: 1

src/lib/MVC/Symfony/Routing/Generator/UrlAliasGenerator.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ public function setExcludedUriPrefixes(array $excludedUriPrefixes)
103103
* Returns path corresponding to $rootLocationId.
104104
*
105105
* @param int $rootLocationId
106-
* @param array $languages
106+
* @param array<string>|null $languages
107107
* @param string $siteaccess
108108
*
109109
* @return string
110+
*
111+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
110112
*/
111113
public function getPathPrefixByRootLocationId($rootLocationId, $languages = null, $siteaccess = null)
112114
{
@@ -122,7 +124,7 @@ public function getPathPrefixByRootLocationId($rootLocationId, $languages = null
122124
$this->pathPrefixMap[$siteaccess][$rootLocationId] = $this->repository
123125
->getURLAliasService()
124126
->reverseLookup(
125-
$this->loadLocation($rootLocationId),
127+
$this->loadLocation($rootLocationId, $languages),
126128
null,
127129
false,
128130
$languages
@@ -157,15 +159,16 @@ public function isUriPrefixExcluded($uri)
157159
* Not to be used for link generation.
158160
*
159161
* @param int $locationId
162+
* @param array<string>|null $languages
160163
*
161164
* @return \Ibexa\Core\Repository\Values\Content\Location
162165
*/
163-
public function loadLocation($locationId)
166+
public function loadLocation($locationId, ?array $languages = null)
164167
{
165168
return $this->repository->sudo(
166-
static function (Repository $repository) use ($locationId) {
169+
static function (Repository $repository) use ($locationId, $languages) {
167170
/* @var $repository \Ibexa\Core\Repository\Repository */
168-
return $repository->getLocationService()->loadLocation($locationId);
171+
return $repository->getLocationService()->loadLocation($locationId, $languages);
169172
}
170173
);
171174
}

tests/lib/MVC/Symfony/Routing/UrlAliasGeneratorTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,84 @@ public function providerTestDoGenerateWithSiteaccess()
319319
];
320320
}
321321

322+
public function testDoGenerateWithSiteAccessLoadsLocationWithLanguages(): void
323+
{
324+
$siteSiteAccess = 'site';
325+
$gerSiteAccess = 'ger';
326+
$parameters = ['siteaccess' => $gerSiteAccess];
327+
328+
$saRootLocations = [
329+
$siteSiteAccess => $siteSiteAccessLocationId = 2,
330+
$gerSiteAccess => $gerSiteAccessLocationId = 71,
331+
];
332+
$treeRootUrlAliases = [
333+
$siteSiteAccessLocationId => new URLAlias(['path' => '/']),
334+
$gerSiteAccessLocationId => new URLAlias(['path' => '/ger']),
335+
];
336+
337+
$this->configResolver
338+
->expects(self::any())
339+
->method('getParameter')
340+
->will(
341+
self::returnValueMap(
342+
[
343+
['languages', null, $siteSiteAccess, ['eng-GB']],
344+
['languages', null, $gerSiteAccess, ['ger-DE']],
345+
[
346+
'content.tree_root.location_id',
347+
null,
348+
$siteSiteAccess,
349+
$saRootLocations[$siteSiteAccess],
350+
],
351+
[
352+
'content.tree_root.location_id',
353+
null,
354+
$gerSiteAccess,
355+
$saRootLocations[$gerSiteAccess],
356+
],
357+
]
358+
)
359+
);
360+
361+
$location = new Location(['id' => $gerSiteAccessLocationId]);
362+
363+
$this->urlAliasService
364+
->expects(self::once())
365+
->method('listLocationAliases')
366+
->with($location, false, null, null, ['ger-DE'])
367+
->willReturn(
368+
[
369+
new URLAlias(
370+
['path' => $gerRootLocationAlias = '/ger-folder'],
371+
),
372+
],
373+
);
374+
375+
$this->locationService
376+
->expects(self::once())
377+
->method('loadLocation')
378+
->with($gerSiteAccessLocationId, ['ger-DE'])
379+
->willReturn($location);
380+
381+
$this->urlAliasService
382+
->expects(self::once())
383+
->method('reverseLookup')
384+
->with($location, null, false, ['ger-DE'])
385+
->willReturn($treeRootUrlAliases[$location->id]);
386+
387+
$this->urlAliasGenerator->setSiteAccess(
388+
new SiteAccess(
389+
$gerSiteAccess,
390+
'default',
391+
)
392+
);
393+
394+
self::assertSame(
395+
$gerRootLocationAlias,
396+
$this->urlAliasGenerator->doGenerate($location, $parameters)
397+
);
398+
}
399+
322400
public function testDoGenerateNoUrlAlias()
323401
{
324402
$location = new Location(['id' => 123, 'contentInfo' => new ContentInfo(['id' => 456])]);

0 commit comments

Comments
 (0)