From 3a1d98fee4000ef3e5c9e358e7216c38231be1ce Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Wed, 31 May 2023 11:31:49 +0200 Subject: [PATCH 1/4] Remove preview condition for shadow entities --- .../Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php b/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php index 7819f08c..9088217e 100644 --- a/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php +++ b/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php @@ -240,12 +240,6 @@ public function createViews( $settingsToolbarActions, $dimensionContentClass ); - - foreach ($views as $view) { - if ($view instanceof PreviewFormViewBuilderInterface) { - $view->setPreviewCondition('shadowOn != true'); - } - } } return $views; From edf410ba570b84504cf372ed9c0b5e408ec1e40f Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Wed, 31 May 2023 13:12:52 +0200 Subject: [PATCH 2/4] Add shadowlocale for preview --- .../Sulu/Preview/ContentObjectProvider.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php b/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php index 7ab37ea3..c7234043 100644 --- a/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php +++ b/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php @@ -20,6 +20,7 @@ use Sulu\Bundle\ContentBundle\Content\Domain\Exception\ContentNotFoundException; use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; +use Sulu\Bundle\ContentBundle\Content\Domain\Model\ShadowInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; use Sulu\Bundle\PreviewBundle\Preview\Object\PreviewObjectProviderInterface; @@ -193,6 +194,17 @@ protected function resolveContent(ContentRichEntityInterface $contentRichEntity, ] ); + // unfortunately we can only check if it is a shadow after the dimensionContent was loaded + if ($resolvedDimensionContent instanceof ShadowInterface && $resolvedDimensionContent->getShadowLocale()) { + $resolvedDimensionContent = $this->contentResolver->resolve( + $contentRichEntity, + [ + 'locale' => $resolvedDimensionContent->getShadowLocale(), + 'stage' => DimensionContentInterface::STAGE_DRAFT, + ] + ); + } + if (!$resolvedDimensionContent->getLocale()) { // avoid 500 error when ghostLocale is loaded by still use correct locale in serialize method $resolvedDimensionContent->setLocale($locale); From b28c0db3e33fbc3e4026c2781cd4f57519e1fc49 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Wed, 31 May 2023 15:56:01 +0200 Subject: [PATCH 3/4] Add test for shadow preview --- .../Preview/ContentObjectProviderTest.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php index 86b012a4..2169fb01 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php @@ -26,6 +26,7 @@ use Sulu\Bundle\ContentBundle\Content\Domain\Exception\ContentNotFoundException; use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface; +use Sulu\Bundle\ContentBundle\Content\Domain\Model\ShadowInterface; use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Preview\ContentObjectProvider; use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Preview\PreviewDimensionContentCollection; use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Admin\ExampleAdmin; @@ -115,6 +116,63 @@ public function testGetObject(int $id = 1, string $locale = 'de'): void $this->assertSame($dimensionContent->reveal(), $result); } + public function testGetObjectWithShadow(int $id = 1, string $locale = 'de'): void + { + $queryBuilder = $this->prophesize(QueryBuilder::class); + + $this->entityManager->createQueryBuilder()->willReturn($queryBuilder->reveal())->shouldBeCalledTimes(1); + + $queryBuilder->select(Argument::type('string')) + ->willReturn($queryBuilder->reveal()) + ->shouldBeCalledTimes(1); + + $queryBuilder->from(Argument::type('string'), Argument::type('string')) + ->willReturn($queryBuilder->reveal()) + ->shouldBeCalledTimes(1); + + $queryBuilder->where(Argument::type('string')) + ->willReturn($queryBuilder->reveal()) + ->shouldBeCalledTimes(1); + + $queryBuilder->setParameter(Argument::type('string'), Argument::any()) + ->willReturn($queryBuilder->reveal()) + ->shouldBeCalledTimes(1); + + $query = $this->prophesize(AbstractQuery::class); + + $queryBuilder->getQuery()->willReturn($query->reveal())->shouldBeCalledTimes(1); + + $entity = $this->prophesize(ContentRichEntityInterface::class); + + $query->getSingleResult()->willReturn($entity->reveal())->shouldBeCalledTimes(1); + + $dimensionContent = $this->prophesize(DimensionContentInterface::class); + $dimensionContent->willImplement(ShadowInterface::class); + $dimensionContent->getShadowLocale()->willReturn('en')->shouldBeCalledTimes(2); + + $this->contentResolver->resolve( + $entity->reveal(), + [ + 'locale' => 'de', + 'stage' => DimensionContentInterface::STAGE_DRAFT + ] + )->willReturn($dimensionContent->reveal())->shouldBeCalledTimes(1); + + $dimensionContent = $this->prophesize(DimensionContentInterface::class); + $dimensionContent->getLocale()->willReturn('en'); + $this->contentResolver->resolve( + $entity->reveal(), + [ + 'locale' => 'en', + 'stage' => DimensionContentInterface::STAGE_DRAFT + ] + )->willReturn($dimensionContent->reveal())->shouldBeCalledTimes(1); + + $result = $this->contentObjectProvider->getObject((string) $id, $locale); + + $this->assertSame($dimensionContent->reveal(), $result); + } + public function testGetNonExistingObject(int $id = 1, string $locale = 'de'): void { $this->entityManager->createQueryBuilder()->willThrow(NoResultException::class)->shouldBeCalledTimes(1); From 3b8cc0fa062f2c71687b9e195a3e44982f6c6772 Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Thu, 1 Jun 2023 14:03:07 +0200 Subject: [PATCH 4/4] Add review suggestions --- .php-cs-fixer.dist.php | 1 + .../Infrastructure/Sulu/Preview/ContentObjectProvider.php | 8 +------- .../Sulu/Preview/ContentObjectProviderTest.php | 4 ++-- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 0a4305de..44a14da7 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -33,6 +33,7 @@ 'strict_param' => true, 'get_class_to_class_keyword' => false, // should be enabled as soon as support for php < 8 is dropped 'nullable_type_declaration_for_default_null_value' => true, + 'no_null_property_initialization' => false, ]) ->setFinder( PhpCsFixer\Finder::create() diff --git a/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php b/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php index c7234043..36bd4cdf 100644 --- a/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php +++ b/Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php @@ -196,13 +196,7 @@ protected function resolveContent(ContentRichEntityInterface $contentRichEntity, // unfortunately we can only check if it is a shadow after the dimensionContent was loaded if ($resolvedDimensionContent instanceof ShadowInterface && $resolvedDimensionContent->getShadowLocale()) { - $resolvedDimensionContent = $this->contentResolver->resolve( - $contentRichEntity, - [ - 'locale' => $resolvedDimensionContent->getShadowLocale(), - 'stage' => DimensionContentInterface::STAGE_DRAFT, - ] - ); + return $this->resolveContent($contentRichEntity, $resolvedDimensionContent->getShadowLocale()); } if (!$resolvedDimensionContent->getLocale()) { diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php index 2169fb01..a5b46ae0 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Preview/ContentObjectProviderTest.php @@ -154,7 +154,7 @@ public function testGetObjectWithShadow(int $id = 1, string $locale = 'de'): voi $entity->reveal(), [ 'locale' => 'de', - 'stage' => DimensionContentInterface::STAGE_DRAFT + 'stage' => DimensionContentInterface::STAGE_DRAFT, ] )->willReturn($dimensionContent->reveal())->shouldBeCalledTimes(1); @@ -164,7 +164,7 @@ public function testGetObjectWithShadow(int $id = 1, string $locale = 'de'): voi $entity->reveal(), [ 'locale' => 'en', - 'stage' => DimensionContentInterface::STAGE_DRAFT + 'stage' => DimensionContentInterface::STAGE_DRAFT, ] )->willReturn($dimensionContent->reveal())->shouldBeCalledTimes(1);