Skip to content

Commit 1138d50

Browse files
authored
IBX-5119: Added logging for invalid Content's Locations setup
For more details see https://issues.ibexa.co/browse/IBX-5119 and #364 * Added logging for invalid Content's Locations setup
1 parent 306c4d3 commit 1138d50

File tree

9 files changed

+77
-2
lines changed

9 files changed

+77
-2
lines changed

eZ/Publish/API/Repository/Values/Content/ContentInfo.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* @property-read bool $alwaysAvailable Indicates if the Content object is shown in the mainlanguage if its not present in an other requested language
2828
* @property-read string $remoteId a global unique id of the Content object
2929
* @property-read string $mainLanguageCode The main language code of the Content object. If the available flag is set to true the Content is shown in this language if the requested language does not exist.
30-
* @property-read int|null $mainLocationId Identifier of the main location.
30+
* @property-read int|null $mainLocationId @deprecated Use {@see ContentInfo::getMainLocationId} instead
3131
* @property-read int $status status of the Content object
3232
* @property-read bool $isHidden status of the Content object
3333
*/
@@ -215,6 +215,11 @@ public function getOwner(): User
215215
return $this->owner;
216216
}
217217

218+
public function getMainLocationId(): ?int
219+
{
220+
return $this->mainLocationId;
221+
}
222+
218223
public function getId(): int
219224
{
220225
return $this->id;

eZ/Publish/Core/Persistence/Cache/LocationHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,4 +502,11 @@ private function getCacheTranslationKey(array $translations = null, bool $useAlw
502502

503503
return implode('|', $translations) . '|' . (int)$useAlwaysAvailable;
504504
}
505+
506+
public function countLocationsByContent(int $contentId): int
507+
{
508+
$this->logger->logCall(__METHOD__, ['contentId' => $contentId]);
509+
510+
return $this->persistenceHandler->locationHandler()->countLocationsByContent($contentId);
511+
}
505512
}

eZ/Publish/Core/Persistence/Cache/Tests/LocationHandlerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function providerForUnCachedMethods(): array
6969
['removeSubtree', [12], [['location_path', [12], false]], null, ['lp-12']],
7070
['setSectionForSubtree', [12, 2], [['location_path', [12], false]], null, ['lp-12']],
7171
['changeMainLocation', [4, 12], [['content', [4], false]], null, ['c-4']],
72+
['countLocationsByContent', [4]],
7273
];
7374
}
7475

eZ/Publish/Core/Persistence/Legacy/Content/Location/Handler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,12 @@ public function loadAllLocations($offset, $limit)
608608

609609
return $this->locationMapper->createLocationsFromRows($rows);
610610
}
611+
612+
/**
613+
* {@inheritdoc}
614+
*/
615+
public function countLocationsByContent(int $contentId): int
616+
{
617+
return $this->locationGateway->countLocationsByContentId($contentId);
618+
}
611619
}

eZ/Publish/Core/Persistence/Legacy/Tests/Content/LocationHandlerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,23 @@ public function testCopySubtree()
677677
);
678678
}
679679

680+
/**
681+
* @covers \eZ\Publish\Core\Persistence\Legacy\Content\Location::countLocationsByContent
682+
*/
683+
public function testCountLocationsByContent(): void
684+
{
685+
$handler = $this->getLocationHandler();
686+
687+
$contentId = 41;
688+
689+
$this->locationGateway
690+
->expects(self::once())
691+
->method('countLocationsByContentId')
692+
->with($contentId);
693+
694+
$handler->countLocationsByContent($contentId);
695+
}
696+
680697
/**
681698
* Returns the handler to test with $methods mocked.
682699
*

eZ/Publish/Core/Repository/Mapper/ContentDomainMapper.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,20 @@
3636
use eZ\Publish\SPI\Persistence\Content\Type\Handler as TypeHandler;
3737
use eZ\Publish\SPI\Persistence\Content\VersionInfo as SPIVersionInfo;
3838
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\ThumbnailStrategy;
39+
use Psr\Log\LoggerAwareInterface;
40+
use Psr\Log\LoggerAwareTrait;
41+
use Psr\Log\LoggerInterface;
42+
use Psr\Log\NullLogger;
3943

4044
/**
4145
* ContentDomainMapper is an internal service.
4246
*
4347
* @internal Meant for internal use by Repository.
4448
*/
45-
class ContentDomainMapper extends ProxyAwareDomainMapper
49+
class ContentDomainMapper extends ProxyAwareDomainMapper implements LoggerAwareInterface
4650
{
51+
use LoggerAwareTrait;
52+
4753
public const MAX_LOCATION_PRIORITY = 2147483647;
4854
public const MIN_LOCATION_PRIORITY = -2147483648;
4955

@@ -76,6 +82,7 @@ public function __construct(
7682
LanguageHandler $contentLanguageHandler,
7783
FieldTypeRegistry $fieldTypeRegistry,
7884
ThumbnailStrategy $thumbnailStrategy,
85+
?LoggerInterface $logger = null,
7986
?ProxyDomainMapperInterface $proxyFactory = null
8087
) {
8188
$this->contentHandler = $contentHandler;
@@ -85,6 +92,7 @@ public function __construct(
8592
$this->contentLanguageHandler = $contentLanguageHandler;
8693
$this->fieldTypeRegistry = $fieldTypeRegistry;
8794
$this->thumbnailStrategy = $thumbnailStrategy;
95+
$this->logger = $logger ?? new NullLogger();
8896
parent::__construct($proxyFactory);
8997
}
9098

@@ -119,6 +127,19 @@ public function buildContentDomainObject(
119127

120128
$versionInfo = $this->buildVersionInfoDomainObject($spiContent->versionInfo, $prioritizedLanguages);
121129

130+
$contentInfo = $versionInfo->getContentInfo();
131+
$mainLocation = $contentInfo->getMainLocation();
132+
133+
// For performance reasons 'countLocationsByContent' is moved to if
134+
if ($mainLocation === null && $this->locationHandler->countLocationsByContent($contentInfo->getId()) > 0) {
135+
$this->logger->error(
136+
sprintf(
137+
'Main location for content of ID = %d doesn\'t exist yet this content has locations assigned.',
138+
$contentInfo->getId()
139+
)
140+
);
141+
}
142+
122143
return new Content(
123144
[
124145
'thumbnail' => $this->thumbnailStrategy->getThumbnail($contentType, $internalFields, $versionInfo),

eZ/Publish/Core/Repository/Tests/Service/Mock/DomainMapperTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use eZ\Publish\SPI\Persistence\Content\ContentInfo as SPIContentInfo;
2222
use eZ\Publish\SPI\Persistence\Content\Location;
2323
use eZ\Publish\SPI\Persistence\Content\VersionInfo as SPIVersionInfo;
24+
use Psr\Log\LoggerInterface;
2425

2526
/**
2627
* Mock test case for internal ContentDomainMapper.
@@ -276,6 +277,7 @@ protected function getContentDomainMapper(): ContentDomainMapper
276277
$this->getLanguageHandlerMock(),
277278
$this->getFieldTypeRegistryMock(),
278279
$this->getThumbnailStrategy(),
280+
$this->getLoggerMock(),
279281
$this->getProxyFactoryMock()
280282
);
281283
}
@@ -308,4 +310,9 @@ protected function getProxyFactoryMock(): ProxyDomainMapperInterface
308310
{
309311
return $this->createMock(ProxyDomainMapperInterface::class);
310312
}
313+
314+
protected function getLoggerMock(): LoggerInterface
315+
{
316+
return $this->createMock(LoggerInterface::class);
317+
}
311318
}

eZ/Publish/Core/settings/repository/inner.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ services:
190190
$contentLanguageHandler: '@ezpublish.spi.persistence.language_handler'
191191
$fieldTypeRegistry: '@eZ\Publish\Core\FieldType\FieldTypeRegistry'
192192
$thumbnailStrategy: '@eZ\Publish\Core\Repository\Strategy\ContentThumbnail\ThumbnailChainStrategy'
193+
calls:
194+
- [setLogger, ['@?logger']]
195+
tags:
196+
- { name: 'monolog.logger', channel: 'ibexa.core' }
193197

194198
eZ\Publish\Core\Repository\Mapper\RoleDomainMapper:
195199
arguments:

eZ/Publish/SPI/Persistence/Content/Location/Handler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,9 @@ public function countAllLocations();
252252
* @return \eZ\Publish\SPI\Persistence\Content\Location[]
253253
*/
254254
public function loadAllLocations($offset, $limit);
255+
256+
/**
257+
* Counts locations for a given content represented by its id.
258+
*/
259+
public function countLocationsByContent(int $contentId): int;
255260
}

0 commit comments

Comments
 (0)