Skip to content

Commit

Permalink
Copy published date to live DimensionContent (#138)
Browse files Browse the repository at this point in the history
* Copy published date to live DimensionContent

* fix review issues

* add tests
  • Loading branch information
luca-rath authored May 13, 2020
1 parent ebfcb34 commit 509af1d
Show file tree
Hide file tree
Showing 15 changed files with 363 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function map(

if ($localizedObject) {
if (!$localizedObject instanceof ExcerptInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedDimensionContent" from type "%s" but "%s" given.', ExcerptInterface::class, \get_class($localizedObject)));
throw new \RuntimeException(sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', ExcerptInterface::class, \get_class($localizedObject)));
}

$this->setExcerptData($localizedObject, $data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function map(

if ($localizedObject) {
if (!$localizedObject instanceof SeoInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedDimensionContent" from type "%s" but "%s" given.', SeoInterface::class, \get_class($localizedObject)));
throw new \RuntimeException(sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', SeoInterface::class, \get_class($localizedObject)));
}

$this->setSeoData($localizedObject, $data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function map(

if ($localizedObject) {
if (!$localizedObject instanceof TemplateInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedDimensionContent" from type "%s" but "%s" given.', TemplateInterface::class, \get_class($localizedObject)));
throw new \RuntimeException(sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', TemplateInterface::class, \get_class($localizedObject)));
}

$localizedObject->setTemplateKey($template);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;

class WorkflowDataMapper implements DataMapperInterface
{
public function map(
array $data,
object $unlocalizedObject,
?object $localizedObject = null
): void {
if (!$unlocalizedObject instanceof WorkflowInterface) {
return;
}

if ($localizedObject) {
if (!$localizedObject instanceof WorkflowInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', WorkflowInterface::class, \get_class($localizedObject)));
}

$this->setWorkflowData($localizedObject, $data);

return;
}

$this->setWorkflowData($unlocalizedObject, $data);
}

/**
* @param mixed[] $data
*/
private function setWorkflowData(WorkflowInterface $object, array $data): void
{
if (!$object instanceof DimensionContentInterface
|| DimensionInterface::STAGE_LIVE !== $object->getDimension()->getStage()) {
return;
}

$published = $data['published'] ?? null;

if (!$published) {
return;
}

$object->setWorkflowPublished(new \DateTimeImmutable($published));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\TransitionEvent;

Expand All @@ -41,6 +42,12 @@ public function onPublish(TransitionEvent $transitionEvent): void
return;
}

if ($dimensionContent instanceof WorkflowInterface) {
if (!$dimensionContent->getWorkflowPublished()) {
$dimensionContent->setWorkflowPublished(new \DateTimeImmutable());
}
}

$context = $transitionEvent->getContext();

$dimensionContentCollection = $context['dimensionContentCollection'] ?? null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ public function __construct(

public function onUnpublish(TransitionEvent $transitionEvent): void
{
if (!$transitionEvent->getSubject() instanceof DimensionContentInterface) {
$dimensionContent = $transitionEvent->getSubject();

if (!$dimensionContent instanceof DimensionContentInterface) {
return;
}

if ($dimensionContent instanceof WorkflowInterface) {
$dimensionContent->setWorkflowPublished(null);
}

$context = $transitionEvent->getContext();

$dimensionAttributes = $context['dimensionAttributes'] ?? null;
Expand Down
8 changes: 0 additions & 8 deletions Content/Domain/Model/WorkflowTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ public function getWorkflowPlace(): string
public function setWorkflowPlace(string $workflowPlace): void
{
$this->workflowPlace = $workflowPlace;

if (WorkflowInterface::WORKFLOW_PLACE_PUBLISHED === $workflowPlace && !$this->workflowPublished) {
$this->setWorkflowPublished(new \DateTimeImmutable());
}

if (WorkflowInterface::WORKFLOW_PLACE_UNPUBLISHED === $workflowPlace && $this->workflowPublished) {
$this->setWorkflowPublished(null);
}
}

public function getWorkflowPublished(): ?\DateTimeImmutable
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/data-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<tag name="sulu_content.data_mapper" priority="32"/>
</service>

<service id="sulu_content.workflow_data_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper\WorkflowDataMapper">
<tag name="sulu_content.data_mapper" priority="16"/>
</service>

<service id="sulu_content.route_data_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper\RoutableDataMapper">
<argument type="service" id="sulu_page.structure.factory"/>
<argument type="service" id="sulu_route.generator.route_generator"/>
Expand Down
28 changes: 27 additions & 1 deletion Tests/Functional/Integration/ExampleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function setUp(): void
$this->client = $this->createAuthenticatedClient();
}

public function testPostPublish(): void
public function testPostPublish(): int
{
self::purgeDatabase();
self::initPhpcr();
Expand All @@ -42,6 +42,7 @@ public function testPostPublish(): void
'template' => 'example-2',
'title' => 'Test Example',
'url' => '/my-example',
'published' => '2020-05-08T00:00:00+00:00', // Should be ignored
'images' => null,
'seoTitle' => 'Seo Title',
'seoDescription' => 'Seo Description',
Expand All @@ -60,8 +61,11 @@ public function testPostPublish(): void
]);

$response = $this->client->getResponse();
$content = json_decode((string) $response->getContent(), true);
$id = $content['id'] ?? null;

$this->assertResponseContent('example_post_publish.json', $response, 201);
$this->assertNotSame('2020-05-08T00:00:00+00:00', $content['published']);

self::ensureKernelShutdown();

Expand All @@ -73,6 +77,28 @@ public function testPostPublish(): void
$content = $response->getContent();
$this->assertIsString($content);
$this->assertStringContainsString('EXAMPLE DEFAULT TEMPLATE', $content);

return $id;
}

/**
* @depends testPostPublish
*/
public function testPostTriggerUnpublish(int $id): void
{
$this->client->request('POST', '/admin/api/examples/' . $id . '?locale=en&action=unpublish');

$response = $this->client->getResponse();

$this->assertResponseContent('example_post_trigger_unpublish.json', $response, 200);

self::ensureKernelShutdown();

$websiteClient = $this->createWebsiteClient();
$websiteClient->request('GET', '/en/my-example');

$response = $websiteClient->getResponse();
$this->assertHttpStatusCode(404, $response);
}

public function testPost(): int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"excerptCategories": [],
"excerptDescription": "Excerpt Description",
"excerptIcon": null,
"excerptImage": null,
"excerptMore": "Excerpt More",
"excerptTags": [
"Tag 1",
"Tag 2"
],
"excerptTitle": "Excerpt Title",
"id": "@integer@",
"images": null,
"published": null,
"publishedState": false,
"seoCanonicalUrl": "https:\/\/sulu.io\/",
"seoDescription": "Seo Description",
"seoHideInSitemap": true,
"seoKeywords": "Seo Keyword 1, Seo Keyword 2",
"seoNoFollow": true,
"seoNoIndex": true,
"seoTitle": "Seo Title",
"template": "example-2",
"title": "Test Example",
"url": "\/my-example",
"workflowPlace": "unpublished"
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testMapLocalizedNoTemplateInstance(): void

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage(sprintf(
'Expected "$localizedDimensionContent" from type "%s" but "%s" given.',
'Expected "$localizedObject" from type "%s" but "%s" given.',
TemplateInterface::class,
\get_class($localizedDimensionContent->reveal())
));
Expand Down
Loading

0 comments on commit 509af1d

Please sign in to comment.