Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable preview for shadow entities #249

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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