Skip to content

Commit

Permalink
Enable preview for shadow entities (#249)
Browse files Browse the repository at this point in the history
* Remove preview condition for shadow entities

* Add shadowlocale for preview

* Add test for shadow preview

* Add review suggestions
  • Loading branch information
Prokyonn authored Jun 1, 2023
1 parent d5a6cdf commit 450583f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,6 @@ public function createViews(
$settingsToolbarActions,
$dimensionContentClass
);

foreach ($views as $view) {
if ($view instanceof PreviewFormViewBuilderInterface) {
$view->setPreviewCondition('shadowOn != true');
}
}
}

return $views;
Expand Down
6 changes: 6 additions & 0 deletions Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -193,6 +194,11 @@ 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()) {
return $this->resolveContent($contentRichEntity, $resolvedDimensionContent->getShadowLocale());
}

if (!$resolvedDimensionContent->getLocale()) {
// avoid 500 error when ghostLocale is loaded by still use correct locale in serialize method
$resolvedDimensionContent->setLocale($locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 450583f

Please sign in to comment.