From cacefbf7bfa0bcee765a18e6438b33238baa6288 Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Fri, 15 Dec 2023 11:16:47 +0100 Subject: [PATCH 1/2] add check if shadow-base-locale is published --- Routing/ArticleRouteDefaultProvider.php | 14 ++++ .../ArticleRouteDefaultProviderTest.php | 64 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/Routing/ArticleRouteDefaultProvider.php b/Routing/ArticleRouteDefaultProvider.php index 274f44c4..a4e8af11 100644 --- a/Routing/ArticleRouteDefaultProvider.php +++ b/Routing/ArticleRouteDefaultProvider.php @@ -127,6 +127,20 @@ public function isPublished($entityClass, $id, $locale) ] ); + if (!$object instanceof ArticleInterface) { + return false; + } + + if ($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); From 73b851700f75685678789ae0a179ddc9aaec176d Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Mon, 18 Dec 2023 14:28:49 +0100 Subject: [PATCH 2/2] fix phpstan --- Routing/ArticleRouteDefaultProvider.php | 3 ++- phpstan-baseline.neon | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Routing/ArticleRouteDefaultProvider.php b/Routing/ArticleRouteDefaultProvider.php index a4e8af11..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; @@ -131,7 +132,7 @@ public function isPublished($entityClass, $id, $locale) return false; } - if ($object->isShadowLocaleEnabled()) { + if ($object instanceof ShadowLocaleBehavior && $object->isShadowLocaleEnabled()) { $object = $this->documentManager->find( $id, $object->getShadowLocale(), diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 7b70dbe0..0768ecb6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -10,6 +10,11 @@ parameters: count: 1 path: Admin/ArticleAdmin.php + - + message: "#^Method Sulu\\\\Bundle\\\\ArticleBundle\\\\Admin\\\\ArticleAdmin\\:\\:getSecurityContexts\\(\\) should return array\\\\>\\>\\> but returns array\\\\>\\>\\>\\.$#" + count: 1 + path: Admin/ArticleAdmin.php + - message: "#^Method Sulu\\\\Bundle\\\\ArticleBundle\\\\Admin\\\\ArticleAdmin\\:\\:getTypes\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 @@ -2147,7 +2152,7 @@ parameters: - message: "#^Cannot access offset 'pageNumber' on mixed\\.$#" - count: 2 + count: 1 path: Preview/ArticleObjectProvider.php -