Skip to content

Commit

Permalink
Add signedBy
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumazeau committed Jul 22, 2021
1 parent af64040 commit ab5361a
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 27 deletions.
19 changes: 19 additions & 0 deletions src/Domain/Entity/TermsVersionSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class TermsVersionSignature
*/
protected $version;

/**
* @var string
*
* @ORM\Column(type="string", nullable=true, length=255, name="signed_by")
*/
protected $signedBy;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -107,4 +114,16 @@ public function getVersion(): ?TermsVersion
{
return $this->version;
}

public function setSignedBy(string $signedBy): self
{
$this->signedBy = $signedBy;

return $this;
}

public function getSignedBy(): ?string
{
return $this->signedBy;
}
}
15 changes: 15 additions & 0 deletions src/Domain/Factory/TermsVersionSignatureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,33 @@
use RichId\TermsModuleBundle\Domain\Entity\TermsSubjectInterface;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersionSignature;
use RichId\TermsModuleBundle\Domain\Port\SecurityInterface;

class TermsVersionSignatureFactory
{
/** @var SecurityInterface */
protected $security;

public function __construct(SecurityInterface $security)
{
$this->security = $security;
}

public function __invoke(TermsVersion $version, TermsSubjectInterface $subject): TermsVersionSignature
{
$user = $this->security->getUser();

$entity = new TermsVersionSignature();

$entity->setVersion($version);
$entity->setSubjectType($subject->getTermsSubjectType());
$entity->setSubjectIdentifier($subject->getTermsSubjectIdentifier());
$entity->setDate(new \DateTime());

if ($user !== null) {
$entity->setSignedBy($user->getUsername());
}

return $entity;
}
}
12 changes: 12 additions & 0 deletions src/Domain/Port/SecurityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Domain\Port;

use Symfony\Component\Security\Core\User\UserInterface;

interface SecurityInterface
{
public function getUser(): ?UserInterface;
}
25 changes: 25 additions & 0 deletions src/Infrastructure/Adapter/Security.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Infrastructure\Adapter;

use RichId\TermsModuleBundle\Domain\Port\SecurityInterface;
use Symfony\Component\Security\Core\Security as SymfonySecurity;
use Symfony\Component\Security\Core\User\UserInterface;

class Security implements SecurityInterface
{
/** @var SymfonySecurity */
protected $security;

public function __construct(SymfonySecurity $security)
{
$this->security = $security;
}

public function getUser(): ?UserInterface
{
return $this->security->getUser();
}
}
2 changes: 1 addition & 1 deletion src/Infrastructure/Migrations/Version20210628151052.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up(Schema $schema): void
$this->addSql('CREATE TABLE module_terms_terms_version (id INT UNSIGNED AUTO_INCREMENT NOT NULL, terms_id INT UNSIGNED NOT NULL, version INT UNSIGNED NOT NULL, is_enabled TINYINT(1) NOT NULL, title VARCHAR(255) NOT NULL, content LONGTEXT NOT NULL, publication_date DATETIME DEFAULT NULL, index IDX_2E43CCE53742F27 (terms_id), UNIQUE INDEX terms_version_terms_id_version_UNIQUE (version, terms_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE module_terms_terms_version ADD CONSTRAINT fk_2e43cce53742f27 FOREIGN KEY (terms_id) REFERENCES module_terms_terms (id) ON DELETE RESTRICT');

$this->addSql('CREATE TABLE module_terms_terms_version_signature (id INT UNSIGNED AUTO_INCREMENT NOT NULL, version_id INT UNSIGNED NOT NULL, date DATETIME NOT NULL, subject_type VARCHAR(255) NOT NULL, subject_identifier VARCHAR(255) NOT NULL, index IDX_DD62973A4BBC2705 (version_id), UNIQUE INDEX module_terms_terms_version_signature_UNIQUE (subject_type, subject_identifier, version_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE module_terms_terms_version_signature (id INT UNSIGNED AUTO_INCREMENT NOT NULL, version_id INT UNSIGNED NOT NULL, date DATETIME NOT NULL, subject_type VARCHAR(255) NOT NULL, subject_identifier VARCHAR(255) NOT NULL, signed_by VARCHAR(255) DEFAULT NULL, index IDX_DD62973A4BBC2705 (version_id), UNIQUE INDEX module_terms_terms_version_signature_UNIQUE (subject_type, subject_identifier, version_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE module_terms_terms_version_signature ADD CONSTRAINT fk_dd62973a4bbc2705 FOREIGN KEY (version_id) REFERENCES module_terms_terms_version (id) ON DELETE RESTRICT');
}

Expand Down
2 changes: 2 additions & 0 deletions src/Infrastructure/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<service id="RichId\TermsModuleBundle\Domain\Port\EventDispatcherInterface" alias="RichId\TermsModuleBundle\Infrastructure\Adapter\EventDispatcher"/>
<service id="RichId\TermsModuleBundle\Domain\Port\LoggerInterface" alias="RichId\TermsModuleBundle\Infrastructure\Adapter\Logger"/>
<service id="RichId\TermsModuleBundle\Domain\Port\ResponseBuilderInterface" alias="RichId\TermsModuleBundle\Infrastructure\Adapter\ResponseBuilder"/>
<service id="RichId\TermsModuleBundle\Domain\Port\SecurityInterface" alias="RichId\TermsModuleBundle\Infrastructure\Adapter\Security"/>
<service id="RichId\TermsModuleBundle\Domain\Port\TermsRepositoryInterface" alias="RichId\TermsModuleBundle\Infrastructure\Adapter\TermsRepository"/>
<service id="RichId\TermsModuleBundle\Domain\Port\TermsVersionRepositoryInterface" alias="RichId\TermsModuleBundle\Infrastructure\Adapter\TermsVersionRepository"/>
<service id="RichId\TermsModuleBundle\Domain\Port\ValidatorInterface" alias="RichId\TermsModuleBundle\Infrastructure\Adapter\Validator"/>
Expand All @@ -57,6 +58,7 @@
<service id="RichId\TermsModuleBundle\Infrastructure\Adapter\EventDispatcher" />
<service id="RichId\TermsModuleBundle\Infrastructure\Adapter\Logger" />
<service id="RichId\TermsModuleBundle\Infrastructure\Adapter\ResponseBuilder" />
<service id="RichId\TermsModuleBundle\Infrastructure\Adapter\Security" />
<service id="RichId\TermsModuleBundle\Infrastructure\Adapter\TermsRepository" />
<service id="RichId\TermsModuleBundle\Infrastructure\Adapter\TermsVersionRepository" />
<service id="RichId\TermsModuleBundle\Infrastructure\Adapter\Validator" />
Expand Down
2 changes: 2 additions & 0 deletions tests/Domain/Entity/TermsVersionSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public function testEntity(): void
$entity->setVersion($termsVersion);
$entity->setSubjectType('user');
$entity->setSubjectIdentifier('42');
$entity->setSignedBy('user_1');
$entity->setDate($date);

$this->assertNull($entity->getId());
$this->assertSame($termsVersion, $entity->getVersion());
$this->assertSame('user', $entity->getSubjectType());
$this->assertSame('42', $entity->getSubjectIdentifier());
$this->assertSame('user_1', $entity->getSignedBy());
$this->assertSame($date, $entity->getDate());
}

Expand Down
40 changes: 14 additions & 26 deletions tests/Domain/Factory/TermsVersionSignatureFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

namespace RichId\TermsModuleBundle\Tests\Domain\Factory;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig;
use RichCongress\TestSuite\TestCase\TestCase;
use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Factory\TermsVersionSignatureFactory;
use RichId\TermsModuleBundle\Domain\Model\DummySubject;
use RichId\TermsModuleBundle\Tests\Resources\Entity\DummyUser;
use RichId\TermsModuleBundle\Tests\Resources\Fixtures\DummyUserFixtures;

/**
* @covers \RichId\TermsModuleBundle\Domain\Factory\TermsVersionSignatureFactory
* @TestConfig("kernel")
* @TestConfig("fixtures")
*/
final class TermsVersionSignatureFactoryTest extends TestCase
{
/** @var TermsVersionSignatureFactory */
public $factory;

public function testSign(): void
public function testFactory(): void
{
$termsVersion = new TermsVersion();

Expand All @@ -32,34 +32,22 @@ public function testSign(): void
$this->assertSame('user', $entity->getSubjectType());
$this->assertSame('42', $entity->getSubjectIdentifier());
$this->assertSame($termsVersion, $entity->getVersion());
$this->assertNull($entity->getSignedBy());
}

public function testSignUniqueForTermsVersionAndSubject(): void
public function testFactoryLoggedUser(): void
{
$terms = new Terms();
$terms->setSlug('slug');
$terms->setName('My Terms');

$this->getManager()->persist($terms);
$this->getManager()->flush();

$termsVersion = new TermsVersion();
$termsVersion->setVersion(1);
$termsVersion->setTerms($terms);
$termsVersion->setTitle('title');
$termsVersion->setContent('content');

$this->getManager()->persist($termsVersion);
$this->getManager()->flush();

$entity1 = ($this->factory)($termsVersion, DummySubject::create('user', '42'));
$this->getManager()->persist($entity1);
$this->getManager()->flush();
$this->authenticate(DummyUser::class, DummyUserFixtures::USER);

$this->expectException(UniqueConstraintViolationException::class);
$entity = ($this->factory)($termsVersion, DummySubject::create('user', '42'));

$entity2 = ($this->factory)($termsVersion, DummySubject::create('user', '42'));
$this->getManager()->persist($entity2);
$this->getManager()->flush();
$this->assertNull($entity->getId());
$this->assertInstanceOf(\DateTime::class, $entity->getDate());
$this->assertSame('user', $entity->getSubjectType());
$this->assertSame('42', $entity->getSubjectIdentifier());
$this->assertSame($termsVersion, $entity->getVersion());
$this->assertSame('my_user_1', $entity->getSignedBy());
}
}
35 changes: 35 additions & 0 deletions tests/Infrastructure/Adapter/SecurityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Tests\Infrastructure\Adapter;

use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig;
use RichCongress\TestSuite\TestCase\TestCase;
use RichId\TermsModuleBundle\Infrastructure\Adapter\Security;
use RichId\TermsModuleBundle\Tests\Resources\Entity\DummyUser;
use RichId\TermsModuleBundle\Tests\Resources\Fixtures\DummyUserFixtures;

/**
* @covers \RichId\TermsModuleBundle\Infrastructure\Adapter\Security
* @TestConfig("fixtures")
*/
final class SecurityTest extends TestCase
{
/** @var Security */
public $adapter;

public function testGetUserNotLogged(): void
{
$user = $this->adapter->getUser();
$this->assertNull($user);
}

public function testGetUserLogged(): void
{
$this->authenticate(DummyUser::class, DummyUserFixtures::USER);

$user = $this->adapter->getUser();
$this->assertInstanceOf(DummyUser::class, $user);
}
}

0 comments on commit ab5361a

Please sign in to comment.