From c3e02eb68c649e26f652b9002f41a04c1aa29a66 Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Mon, 18 Dec 2023 15:56:20 +0100 Subject: [PATCH] Add check if shadow-base-locale is published (#653) * add check if shadow-base-locale is published * fix phpstan --- Routing/ArticleRouteDefaultProvider.php | 15 +++++ .../ArticleRouteDefaultProviderTest.php | 64 +++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/Routing/ArticleRouteDefaultProvider.php b/Routing/ArticleRouteDefaultProvider.php index 274f44c4..71f9bc6b 100644 --- a/Routing/ArticleRouteDefaultProvider.php +++ b/Routing/ArticleRouteDefaultProvider.php @@ -20,6 +20,7 @@ use Sulu\Bundle\RouteBundle\Routing\Defaults\RouteDefaultsProviderInterface; use Sulu\Component\Content\Compat\Structure\StructureBridge; use Sulu\Component\Content\Compat\StructureManagerInterface; +use Sulu\Component\Content\Document\Behavior\ShadowLocaleBehavior; use Sulu\Component\Content\Document\WorkflowStage; use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface; use Sulu\Component\Content\Metadata\StructureMetadata; @@ -127,6 +128,20 @@ public function isPublished($entityClass, $id, $locale) ] ); + if (!$object instanceof ArticleInterface) { + return false; + } + + if ($object instanceof ShadowLocaleBehavior && $object->isShadowLocaleEnabled()) { + $object = $this->documentManager->find( + $id, + $object->getShadowLocale(), + [ + 'load_ghost_content' => false, + ], + ); + } + if (!$object instanceof ArticleInterface || WorkflowStage::PUBLISHED !== $object->getWorkflowStage()) { return false; } diff --git a/Tests/Unit/Routing/ArticleRouteDefaultProviderTest.php b/Tests/Unit/Routing/ArticleRouteDefaultProviderTest.php index 6dcaf07e..f80e4a45 100644 --- a/Tests/Unit/Routing/ArticleRouteDefaultProviderTest.php +++ b/Tests/Unit/Routing/ArticleRouteDefaultProviderTest.php @@ -147,6 +147,70 @@ public function testIsPublished( $this->assertEquals($result, $this->provider->isPublished($this->entityClass, $this->entityId, $this->locale)); } + public function publishedDataProviderWithShadow() + { + $articleDocument = new ArticleDocument(); + $articleDocument->setWorkflowStage(WorkflowStage::TEST); + $articleDocument->setShadowLocale('en'); + $articleDocument->setShadowLocaleEnabled(true); + + $baseDocument = new ArticleDocument(); + $baseDocument->setWorkflowStage(WorkflowStage::TEST); + + $baseDocumentPublished = new ArticleDocument(); + $baseDocumentPublished->setWorkflowStage(WorkflowStage::PUBLISHED); + + $unknownDocument = new UnknownDocument(); + + return [ + [$articleDocument, $baseDocument, false], + [$articleDocument, $baseDocumentPublished, true], + [$articleDocument, $unknownDocument, false], + ]; + } + + /** + * @dataProvider publishedDataProviderWithShadow + */ + public function testIsPublishedWithShadow( + $document, + $baseDocument, + $result + ) { + $webspace = $this->prophesize(Webspace::class); + $webspace->getKey()->willReturn('test'); + + $this->requestAnalyzer->getWebspace()->willReturn($webspace->reveal()); + + if ($document instanceof ArticleDocument) { + $this->webspaceResolver->resolveMainWebspace($baseDocument)->willReturn('test'); + $this->webspaceResolver->resolveAdditionalWebspaces($baseDocument)->willReturn([]); + } + + $this->documentManager->find( + $this->entityId, + $this->locale, + [ + 'load_ghost_content' => false, + ] + )->willReturn($document); + + $this->documentManager->find( + $this->entityId, + 'en', + [ + 'load_ghost_content' => false, + ] + )->willReturn($baseDocument); + + $webspace = $this->prophesize(Webspace::class); + $webspace->getKey()->willReturn('test'); + + $this->requestAnalyzer->getWebspace()->willReturn($webspace->reveal()); + + $this->assertEquals($result, $this->provider->isPublished($this->entityClass, $this->entityId, $this->locale)); + } + public function testGetByEntity() { $article = $this->prophesize(ArticleDocument::class);