public function onContentCacheClear(ContentCacheClearEvent $event) { $contentInfo = $event->getContentInfo(); $versionInfo = $this->contentService->loadVersionInfo($contentInfo); foreach ($this->contentService->loadRelations($versionInfo) as $relation) { foreach ($this->locationService->loadLocations($relation->getDestinationContentInfo()) as $relatedLocation) { $event->addLocationToClear($relatedLocation); } } // Using sudo since loading reverse relations is conditioned to content/reverserelatedlist permission and we don't need this check here. /** @var \eZ\Publish\API\Repository\Values\Content\Relation[] $reverseRelations */ $reverseRelations = $this->repository->sudo(function () use($contentInfo) { return $this->contentService->loadReverseRelations($contentInfo); }); foreach ($reverseRelations as $reverseRelation) { foreach ($this->locationService->loadLocations($reverseRelation->getSourceContentInfo()) as $relatedLocation) { $event->addLocationToClear($relatedLocation); } } }
public function testOnContentCacheClear() { $contentId = 123; $contentInfo = new ContentInfo(['id' => $contentId]); $event = new ContentCacheClearEvent($contentInfo); $versionInfo = new VersionInfo(); $this->contentService->expects($this->once())->method('loadVersionInfo')->with($contentInfo)->will($this->returnValue($versionInfo)); // Relation $relatedContentInfo1 = new ContentInfo(['id' => 1]); $relatedLocation1 = new Location(); $relatedContentInfo2 = new ContentInfo(['id' => 2]); $relatedLocation2 = new Location(); $relatedLocation3 = new Location(); $relations = [new Relation(['destinationContentInfo' => $relatedContentInfo1]), new Relation(['destinationContentInfo' => $relatedContentInfo2])]; $this->contentService->expects($this->once())->method('loadRelations')->with($versionInfo)->will($this->returnValue($relations)); // Reverse relations $reverseRelatedContentInfo = new ContentInfo(); $relatedLocation4 = new Location(); $reverseRelations = [new Relation(['sourceContentInfo' => $reverseRelatedContentInfo])]; $this->contentService->expects($this->once())->method('loadReverseRelations')->with($contentInfo)->will($this->returnValue($reverseRelations)); // Relation locations loading with locationService $this->locationService->expects($this->exactly(count($relations) + count($reverseRelations)))->method('loadLocations')->will($this->returnValueMap([[$relatedContentInfo1, null, [$relatedLocation1]], [$relatedContentInfo2, null, [$relatedLocation2, $relatedLocation3]], [$reverseRelatedContentInfo, null, [$relatedLocation4]]])); $allRelatedLocations = [$relatedLocation1, $relatedLocation2, $relatedLocation3, $relatedLocation4]; $this->listener->onContentCacheClear($event); $this->assertSame($allRelatedLocations, $event->getLocationsToClear()); }