From d177b73982c521c2c34b2d6eea03e9e4343232d7 Mon Sep 17 00:00:00 2001 From: Martin Lagler Date: Fri, 12 Jan 2024 11:08:05 +0100 Subject: [PATCH] Add Author properties to the website data --- .../DataMapper/AuthorDataMapper.php | 4 +- Content/Domain/Model/AuthorInterface.php | 8 +- Content/Domain/Model/AuthorTrait.php | 12 +-- .../Doctrine/MetadataLoader.php | 4 +- .../Sulu/Structure/ContentDocument.php | 82 +++++++++++++++++- .../DataMapper/AuthorDataMapperTest.php | 4 +- .../ContentMerger/Merger/AuthorMergerTest.php | 4 +- .../Normalizer/AuthorNormalizerTest.php | 6 +- .../Content/Domain/Model/AuthorTraitTest.php | 4 +- .../Sulu/Structure/ContentDocumentTest.php | 83 +++++++++++++++++++ composer.json | 2 +- 11 files changed, 188 insertions(+), 25 deletions(-) diff --git a/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapper.php b/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapper.php index 37a02a8e..998f484c 100644 --- a/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapper.php +++ b/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapper.php @@ -57,7 +57,7 @@ private function setAuthorData(AuthorInterface $dimensionContent, array $data): if (\array_key_exists('lastModified', $data)) { $dimensionContent->setLastModified( $data['lastModified'] && (\array_key_exists('lastModifiedEnabled', $data) && $data['lastModifiedEnabled']) - ? new \DateTimeImmutable($data['lastModified']) + ? new \DateTime($data['lastModified']) : null ); } @@ -65,7 +65,7 @@ private function setAuthorData(AuthorInterface $dimensionContent, array $data): if (\array_key_exists('authored', $data)) { $dimensionContent->setAuthored( $data['authored'] - ? new \DateTimeImmutable($data['authored']) + ? new \DateTime($data['authored']) : null ); } diff --git a/Content/Domain/Model/AuthorInterface.php b/Content/Domain/Model/AuthorInterface.php index 3e9d5d65..9e765a7c 100644 --- a/Content/Domain/Model/AuthorInterface.php +++ b/Content/Domain/Model/AuthorInterface.php @@ -19,15 +19,15 @@ interface AuthorInterface { public function getLastModifiedEnabled(): ?bool; - public function getLastModified(): ?\DateTimeImmutable; + public function getLastModified(): ?\DateTime; - public function setLastModified(?\DateTimeImmutable $lastModified): void; + public function setLastModified(?\DateTime $lastModified): void; public function getAuthor(): ?ContactInterface; public function setAuthor(?ContactInterface $author): void; - public function getAuthored(): ?\DateTimeImmutable; + public function getAuthored(): ?\DateTime; - public function setAuthored(?\DateTimeImmutable $authored): void; + public function setAuthored(?\DateTime $authored): void; } diff --git a/Content/Domain/Model/AuthorTrait.php b/Content/Domain/Model/AuthorTrait.php index 6c2bd53a..ff5d700f 100644 --- a/Content/Domain/Model/AuthorTrait.php +++ b/Content/Domain/Model/AuthorTrait.php @@ -26,12 +26,12 @@ trait AuthorTrait private $author; /** - * @var \DateTimeImmutable|null + * @var \DateTime|null */ private $authored; /** - * @var \DateTimeImmutable|null + * @var \DateTime|null */ private $lastModified; @@ -40,12 +40,12 @@ public function getLastModifiedEnabled(): ?bool return null !== $this->lastModified; } - public function getLastModified(): ?\DateTimeImmutable + public function getLastModified(): ?\DateTime { return $this->lastModified; } - public function setLastModified(?\DateTimeImmutable $lastModified): void + public function setLastModified(?\DateTime $lastModified): void { $this->lastModified = $lastModified; } @@ -60,12 +60,12 @@ public function setAuthor(?ContactInterface $author): void $this->author = $author; } - public function getAuthored(): ?\DateTimeImmutable + public function getAuthored(): ?\DateTime { return $this->authored; } - public function setAuthored(?\DateTimeImmutable $authored): void + public function setAuthored(?\DateTime $authored): void { $this->authored = $authored; } diff --git a/Content/Infrastructure/Doctrine/MetadataLoader.php b/Content/Infrastructure/Doctrine/MetadataLoader.php index e3a0b7e5..cf56377e 100644 --- a/Content/Infrastructure/Doctrine/MetadataLoader.php +++ b/Content/Infrastructure/Doctrine/MetadataLoader.php @@ -117,8 +117,8 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void } if ($reflection->implementsInterface(AuthorInterface::class)) { - $this->addField($metadata, 'authored', 'datetime_immutable', ['nullable' => true]); - $this->addField($metadata, 'lastModified', 'datetime_immutable', ['nullable' => true]); + $this->addField($metadata, 'authored', 'datetime', ['nullable' => true]); + $this->addField($metadata, 'lastModified', 'datetime', ['nullable' => true]); $this->addManyToOne($event, $metadata, 'author', ContactInterface::class, true); } diff --git a/Content/Infrastructure/Sulu/Structure/ContentDocument.php b/Content/Infrastructure/Sulu/Structure/ContentDocument.php index d5fa0954..5fbd05d0 100644 --- a/Content/Infrastructure/Sulu/Structure/ContentDocument.php +++ b/Content/Infrastructure/Sulu/Structure/ContentDocument.php @@ -13,12 +13,17 @@ namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure; +use Sulu\Bundle\ContactBundle\Entity\ContactInterface; +use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; use Sulu\Component\Content\Document\Behavior\ExtensionBehavior; +use Sulu\Component\Content\Document\Behavior\LocalizedAuthorBehavior; +use Sulu\Component\Persistence\Model\UserBlameInterface; +use Sulu\Component\Security\Authentication\UserInterface; -class ContentDocument implements ExtensionBehavior +class ContentDocument implements ExtensionBehavior, LocalizedAuthorBehavior { /** * @var TemplateInterface @@ -151,4 +156,79 @@ protected function createReadOnlyException(string $method): \BadMethodCallExcept ) ); } + + public function getLastModifiedEnabled(): ?bool + { + if ($this->content instanceof AuthorInterface) { + return $this->content->getLastModifiedEnabled(); + } + + return null; + } + + public function getLastModified(): ?\DateTime + { + if ($this->content instanceof AuthorInterface) { + return $this->content->getLastModified(); + } + + return null; + } + + /** + * @param \DateTime|null $lastModified + */ + public function setLastModified($lastModified): void + { + throw $this->createReadOnlyException(__METHOD__); + } + + public function getAuthored(): ?\DateTime + { + if ($this->content instanceof AuthorInterface) { + return $this->content->getAuthored(); + } + + return null; + } + + public function setAuthored($authored): void + { + throw $this->createReadOnlyException(__METHOD__); + } + + public function getAuthor(): ?ContactInterface + { + if ($this->content instanceof AuthorInterface) { + return $this->content->getAuthor(); + } + + return null; + } + + /** + * @param ContactInterface|null $contactId + */ + public function setAuthor($contactId): void + { + throw $this->createReadOnlyException(__METHOD__); + } + + public function getCreator(): ?UserInterface + { + if ($this->content instanceof UserBlameInterface) { + return $this->content->getCreator(); + } + + return null; + } + + public function getChanger(): ?UserInterface + { + if ($this->content instanceof UserBlameInterface) { + return $this->content->getChanger(); + } + + return null; + } } diff --git a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapperTest.php b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapperTest.php index 40ab710f..bfec8887 100644 --- a/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapperTest.php +++ b/Tests/Unit/Content/Application/ContentDataMapper/DataMapper/AuthorDataMapperTest.php @@ -102,7 +102,7 @@ public function testMapData(): void $this->assertSame($contact, $localizedDimensionContent->getAuthor()); $authored = $localizedDimensionContent->getAuthored(); - /** @var \DateTimeImmutable $lastModified */ + /** @var \DateTime $lastModified */ $lastModified = $localizedDimensionContent->getLastModified(); $this->assertNotNull($authored); $this->assertSame('2020-05-08T00:00:00+00:00', $authored->format('c')); @@ -122,7 +122,7 @@ public function testMapDataNull(): void $unlocalizedDimensionContent = new ExampleDimensionContent($example); $localizedDimensionContent = new ExampleDimensionContent($example); $localizedDimensionContent->setAuthor(new Contact()); - $localizedDimensionContent->setAuthored(new \DateTimeImmutable()); + $localizedDimensionContent->setAuthored(new \DateTime()); $this->contactFactory->create(Argument::cetera()) ->shouldNotBeCalled(); diff --git a/Tests/Unit/Content/Application/ContentMerger/Merger/AuthorMergerTest.php b/Tests/Unit/Content/Application/ContentMerger/Merger/AuthorMergerTest.php index d9e2133a..4d7b700f 100644 --- a/Tests/Unit/Content/Application/ContentMerger/Merger/AuthorMergerTest.php +++ b/Tests/Unit/Content/Application/ContentMerger/Merger/AuthorMergerTest.php @@ -62,8 +62,8 @@ public function testMergeSet(): void $merger = $this->getAuthorMergerInstance(); $contact = $this->prophesize(ContactInterface::class); - $authoredDate = new \DateTimeImmutable('2020-05-08T00:00:00+00:00'); - $lastModifiedDate = new \DateTimeImmutable('2020-05-08T00:00:00+00:00'); + $authoredDate = new \DateTime('2020-05-08T00:00:00+00:00'); + $lastModifiedDate = new \DateTime('2020-05-08T00:00:00+00:00'); $source = $this->prophesize(DimensionContentInterface::class); $source->willImplement(AuthorInterface::class); diff --git a/Tests/Unit/Content/Application/ContentNormalizer/Normalizer/AuthorNormalizerTest.php b/Tests/Unit/Content/Application/ContentNormalizer/Normalizer/AuthorNormalizerTest.php index 8c943a71..61b61063 100644 --- a/Tests/Unit/Content/Application/ContentNormalizer/Normalizer/AuthorNormalizerTest.php +++ b/Tests/Unit/Content/Application/ContentNormalizer/Normalizer/AuthorNormalizerTest.php @@ -59,7 +59,7 @@ public function testEnhanceNotImplementAuthorInterface(): void $data = [ 'author' => 1, - 'authored' => new \DateTimeImmutable('2020-05-08T00:00:00+00:00'), + 'authored' => new \DateTime('2020-05-08T00:00:00+00:00'), ]; $this->assertSame( @@ -76,8 +76,8 @@ public function testEnhance(): void $contact = $this->prophesize(ContactInterface::class); $contact->getId()->shouldBeCalled()->willReturn(1); $object->getAuthor()->willReturn($contact->reveal()); - $authored = new \DateTimeImmutable('2020-05-08T00:00:00+00:00'); - $lastModified = new \DateTimeImmutable('2022-05-08T00:00:00+00:00'); + $authored = new \DateTime('2020-05-08T00:00:00+00:00'); + $lastModified = new \DateTime('2022-05-08T00:00:00+00:00'); $data = [ 'author' => $contact->reveal(), diff --git a/Tests/Unit/Content/Domain/Model/AuthorTraitTest.php b/Tests/Unit/Content/Domain/Model/AuthorTraitTest.php index ca080696..2ffe0cf7 100644 --- a/Tests/Unit/Content/Domain/Model/AuthorTraitTest.php +++ b/Tests/Unit/Content/Domain/Model/AuthorTraitTest.php @@ -42,7 +42,7 @@ public function testGetSetAuthor(): void public function testGetSetAuthored(): void { $model = $this->getAuthorInstance(); - $authored = new \DateTimeImmutable('2020-05-08T00:00:00+00:00'); + $authored = new \DateTime('2020-05-08T00:00:00+00:00'); $this->assertNull($model->getAuthored()); $model->setAuthored($authored); $this->assertSame($authored, $model->getAuthored()); @@ -51,7 +51,7 @@ public function testGetSetAuthored(): void public function testGetSetLastModified(): void { $model = $this->getAuthorInstance(); - $lastModified = new \DateTimeImmutable('2024-05-08T00:00:00+00:00'); + $lastModified = new \DateTime('2024-05-08T00:00:00+00:00'); $model->setLastModified($lastModified); $this->assertTrue($model->getLastModifiedEnabled()); $this->assertSame($lastModified, $model->getLastModified()); diff --git a/Tests/Unit/Content/Infrastructure/Sulu/Structure/ContentDocumentTest.php b/Tests/Unit/Content/Infrastructure/Sulu/Structure/ContentDocumentTest.php index 7b5fcd80..59b21f41 100644 --- a/Tests/Unit/Content/Infrastructure/Sulu/Structure/ContentDocumentTest.php +++ b/Tests/Unit/Content/Infrastructure/Sulu/Structure/ContentDocumentTest.php @@ -14,11 +14,15 @@ namespace Sulu\Bundle\ContentBundle\Tests\Unit\Content\Infrastructure\Sulu\Structure; use PHPUnit\Framework\TestCase; +use Sulu\Bundle\ContactBundle\Entity\Contact; +use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface; use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface; use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Structure\ContentDocument; +use Sulu\Bundle\SecurityBundle\Entity\User; use Sulu\Component\Content\Compat\StructureInterface; +use Sulu\Component\Persistence\Model\UserBlameInterface; class ContentDocumentTest extends TestCase { @@ -170,4 +174,83 @@ public function testGetStructure(): void $this->assertNull($structure); } + + public function testGetLastModified(): void + { + $content = $this->prophesize(TemplateInterface::class); + $content->willImplement(AuthorInterface::class); + $content->getLastModifiedEnabled()->willReturn(true); + $authored = new \DateTime('2020-01-01'); + $lastModified = new \DateTime('2022-01-01'); + $content->getLastModified()->willReturn($lastModified); + $content->getAuthored()->willReturn($authored); + $document = $this->createContentDocument($content->reveal()); + + $this->assertTrue($document->getLastModifiedEnabled()); + $this->assertSame($lastModified, $document->getLastModified()); + $this->assertSame($authored, $document->getAuthored()); + } + + public function testSetLastModified(): void + { + $this->expectException(\BadMethodCallException::class); + $lastModified = new \DateTime('2020-01-01'); + $document = $this->createContentDocument(); + + $document->setLastModified($lastModified); + } + + public function testSetAuthored(): void + { + $this->expectException(\BadMethodCallException::class); + $authored = new \DateTime('2020-01-01'); + $document = $this->createContentDocument(); + + $document->setAuthored($authored); + } + + public function testSetAuthor(): void + { + $this->expectException(\BadMethodCallException::class); + $document = $this->createContentDocument(); + + $document->setAuthor(null); + } + + public function testGetAuthor(): void + { + $content = $this->prophesize(TemplateInterface::class); + $content->willImplement(AuthorInterface::class); + $author = new Contact(); + $content->getAuthor()->willReturn($author); + $document = $this->createContentDocument($content->reveal()); + + $this->assertSame($author, $document->getAuthor()); + } + + public function testGetUser(): void + { + $content = $this->prophesize(TemplateInterface::class); + $content->willImplement(UserBlameInterface::class); + $user = new User(); + $content->getCreator()->willReturn($user); + $content->getChanger()->willReturn($user); + $document = $this->createContentDocument($content->reveal()); + + $this->assertSame($user, $document->getCreator()); + $this->assertSame($user, $document->getChanger()); + } + + public function testLocalizedAuthorNull(): void + { + $content = $this->prophesize(TemplateInterface::class); + $document = $this->createContentDocument($content->reveal()); + + $this->assertNull($document->getLastModifiedEnabled()); + $this->assertNull($document->getLastModified()); + $this->assertNull($document->getAuthored()); + $this->assertNull($document->getAuthor()); + $this->assertNull($document->getCreator()); + $this->assertNull($document->getChanger()); + } } diff --git a/composer.json b/composer.json index 75224ea4..c7df5bec 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "friendsofsymfony/rest-bundle": "^2.6 || ^3.0", "massive/search-bundle": "^2.4", "ramsey/uuid": "^3.8 || ^4.0", - "sulu/sulu": "^2.4 || ^2.6@dev", + "sulu/sulu": "dev-feature/updated-date as 2.5.99", "symfony/config": "^4.4 || ^5.4 || ^6.0", "symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0", "symfony/event-dispatcher": "^4.4 || ^5.4 || ^6.0",