Skip to content

Commit

Permalink
Add Author properties to the website data
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler committed Jan 12, 2024
1 parent 1633b01 commit d177b73
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ 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
);
}

if (\array_key_exists('authored', $data)) {
$dimensionContent->setAuthored(
$data['authored']
? new \DateTimeImmutable($data['authored'])
? new \DateTime($data['authored'])
: null
);
}
Expand Down
8 changes: 4 additions & 4 deletions Content/Domain/Model/AuthorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 6 additions & 6 deletions Content/Domain/Model/AuthorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ trait AuthorTrait
private $author;

/**
* @var \DateTimeImmutable|null
* @var \DateTime|null
*/
private $authored;

/**
* @var \DateTimeImmutable|null
* @var \DateTime|null
*/
private $lastModified;

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions Content/Infrastructure/Doctrine/MetadataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
82 changes: 81 additions & 1 deletion Content/Infrastructure/Sulu/Structure/ContentDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/Content/Domain/Model/AuthorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d177b73

Please sign in to comment.