Skip to content

Commit

Permalink
Add check if shadow-base-locale is published (#653)
Browse files Browse the repository at this point in the history
* add check if shadow-base-locale is published

* fix phpstan
  • Loading branch information
wachterjohannes authored Dec 18, 2023
1 parent a37febd commit c3e02eb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Routing/ArticleRouteDefaultProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,6 +128,20 @@ public function isPublished($entityClass, $id, $locale)
]
);

if (!$object instanceof ArticleInterface) {
return false;
}

if ($object instanceof ShadowLocaleBehavior && $object->isShadowLocaleEnabled()) {
$object = $this->documentManager->find(
$id,
$object->getShadowLocale(),
[
'load_ghost_content' => false,
],
);
}

if (!$object instanceof ArticleInterface || WorkflowStage::PUBLISHED !== $object->getWorkflowStage()) {
return false;
}
Expand Down
64 changes: 64 additions & 0 deletions Tests/Unit/Routing/ArticleRouteDefaultProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c3e02eb

Please sign in to comment.