From b5c3eeb2e6875437b81febcd8583e9c1400aacaf Mon Sep 17 00:00:00 2001 From: Prokyonn Date: Mon, 17 Apr 2023 12:11:03 +0200 Subject: [PATCH] Add missing shadow logic (#244) --- .../Sulu/Structure/ContentStructureBridge.php | 30 ++++++++++--- .../Structure/ContentStructureBridgeTest.php | 45 +++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/Content/Infrastructure/Sulu/Structure/ContentStructureBridge.php b/Content/Infrastructure/Sulu/Structure/ContentStructureBridge.php index acfad6ab..638efb12 100644 --- a/Content/Infrastructure/Sulu/Structure/ContentStructureBridge.php +++ b/Content/Infrastructure/Sulu/Structure/ContentStructureBridge.php @@ -13,6 +13,8 @@ namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure; +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\Component\Content\Compat\Property; use Sulu\Component\Content\Compat\PropertyInterface; @@ -402,23 +404,35 @@ public function getNavContexts(): array } /** - * @return string[] + * @return array */ public function getShadowLocales(): array { + $content = $this->getContent(); + if ($content instanceof ShadowInterface) { + return $content->getShadowLocales() ?? []; + } + return []; } - /** - * @return mixed|null - */ - public function getShadowBaseLanguage() + public function getShadowBaseLanguage(): ?string { + $content = $this->getContent(); + if ($content instanceof ShadowInterface) { + return $content->getShadowLocale(); + } + return null; } public function getIsShadow(): bool { + $content = $this->getContent(); + if ($content instanceof ShadowInterface) { + return (bool) $content->getShadowLocale(); + } + return false; } @@ -427,6 +441,12 @@ public function getIsShadow(): bool */ public function getContentLocales(): array { + $content = $this->getContent(); + + if ($content instanceof DimensionContentInterface) { + return $content->getAvailableLocales() ?? []; + } + return []; } diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Structure/ContentStructureBridgeTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Structure/ContentStructureBridgeTest.php index 83846937..087055cb 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Structure/ContentStructureBridgeTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Structure/ContentStructureBridgeTest.php @@ -15,6 +15,8 @@ use PHPUnit\Framework\TestCase; use Prophecy\Argument; +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\ContentBundle\Content\Infrastructure\Sulu\Structure\ContentDocument; use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure\ContentStructureBridge; @@ -727,6 +729,39 @@ public function testGetIsShadow(): void $this->assertFalse($structure->getIsShadow()); } + public function testGetShadowLocalesWithShadowEnabled(): void + { + $content = $this->prophesize(TemplateInterface::class); + $content->willImplement(ShadowInterface::class); + $content->getShadowLocales()->shouldBeCalled()->willReturn(['de' => 'en', 'en' => 'en']); + + $structure = $this->createStructureBridge($content->reveal()); + + $this->assertSame(['de' => 'en', 'en' => 'en'], $structure->getShadowLocales()); + } + + public function testGetShadowBaseLanguageWithShadowEnabled(): void + { + $content = $this->prophesize(TemplateInterface::class); + $content->willImplement(ShadowInterface::class); + $content->getShadowLocale()->shouldBeCalled()->willReturn('de'); + + $structure = $this->createStructureBridge($content->reveal()); + + $this->assertSame('de', $structure->getShadowBaseLanguage()); + } + + public function testGetIsShadowWithShadowEnabled(): void + { + $content = $this->prophesize(TemplateInterface::class); + $content->willImplement(ShadowInterface::class); + $content->getShadowLocale()->shouldBeCalled()->willReturn('de'); + + $structure = $this->createStructureBridge($content->reveal()); + + $this->assertTrue($structure->getIsShadow()); + } + public function testGetContentLocales(): void { $structure = $this->createStructureBridge(); @@ -734,6 +769,16 @@ public function testGetContentLocales(): void $this->assertSame([], $structure->getContentLocales()); } + public function testGetContentLocalesWithDimensionContentInterface(): void + { + $content = $this->prophesize(TemplateInterface::class); + $content->willImplement(DimensionContentInterface::class); + $content->getAvailableLocales()->shouldBeCalled()->willReturn(['de', 'en']); + $structure = $this->createStructureBridge($content->reveal()); + + $this->assertSame(['de', 'en'], $structure->getContentLocales()); + } + public function testGetOriginalTemplate(): void { $structure = $this->createStructureBridge();