Skip to content

Commit

Permalink
#48056, edit bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumazeau committed Jul 15, 2021
1 parent 20b92d9 commit 14bf9dc
Show file tree
Hide file tree
Showing 18 changed files with 708 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/Domain/Model/TermsEdition.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TermsEdition
*
* @Assert\Type("bool")
*/
private $activateVersion;
private $needVersionActivation;

public function __construct(TermsVersion $entity)
{
Expand Down Expand Up @@ -121,14 +121,14 @@ public function setPublicationDate(?\DateTime $publicationDate): self
return $this;
}

public function getActivateVersion(): ?bool
public function needVersionActivation(): ?bool
{
return $this->activateVersion;
return $this->needVersionActivation;
}

public function setActivateVersion(?bool $activateVersion): self
public function setNeedVersionActivation(?bool $needVersionActivation): self
{
$this->activateVersion = $activateVersion;
$this->needVersionActivation = $needVersionActivation;

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/UseCase/EditTerms.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __invoke(TermsEdition $termsEdition): void
$this->entityRecoder->saveTerms($terms);
$this->entityRecoder->saveTermsVersion($termsVersion);

if ($termsEdition->getActivateVersion() === true) {
if ($termsEdition->needVersionActivation() === true) {
($this->activateTermsVersion)($termsVersion);
}

Expand Down
6 changes: 1 addition & 5 deletions src/Infrastructure/FormType/TermsVersionFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace RichId\TermsModuleBundle\Infrastructure\FormType;

use FOS\CKEditorBundle\Form\Type\CKEditorType;
use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Model\TermsEdition;
use Symfony\Component\Form\AbstractType;
Expand All @@ -18,13 +17,11 @@

class TermsVersionFormType extends AbstractType
{
public const TERMS_ENTITY = 'terms';
public const TERMS_VERSION_ENTITY = 'termsVersion';

/* @phpstan-ignore-next-line */
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$terms = $options[self::TERMS_ENTITY] ?? new Terms();
$termsVersion = $options[self::TERMS_VERSION_ENTITY] ?? new TermsVersion();

$builder
Expand Down Expand Up @@ -82,7 +79,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
]
)
->add(
'activateVersion',
'needVersionActivation',
HiddenType::class,
[
'required' => false,
Expand All @@ -95,7 +92,6 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
self::TERMS_ENTITY => null,
self::TERMS_VERSION_ENTITY => null,
'data_class' => TermsEdition::class,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

{{ form_start(form, {'attr': {'id': 'terms-edition-form'}}) }}
{{ form_row(form.isTermsPublished, {'attr': {'class': 'terms-module-form-field-small'}}) }}
{{ form_row(form.activateVersion) }}
{{ form_row(form.needVersionActivation) }}

<div class="terms-module-admin-edition-add-version-container">
<div id="terms-module-admin-edition-terms-version-tags" class="terms-module-admin-edition-terms-version-tags">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ window.removeVersion = function () {

window.activateVersion = function () {
{% if currentTermsVersion.id is not null %}
document.getElementById('{{ form.activateVersion.vars.id }}').value = true;
document.getElementById('{{ form.needVersionActivation.vars.id }}').value = true;
window.saveTerms();
{% endif %}
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function validate($value, Constraint $constraint): void

$originalTermsVersion = $value->getEntity();

if ($originalTermsVersion->isEnabled() && $value->getActivateVersion()) {
if ($originalTermsVersion->isEnabled() && $value->needVersionActivation()) {
$this->context->buildViolation(self::MESSAGE)
->atPath('activateVersion')
->atPath('needVersionActivation')
->setTranslationDomain('terms_module_validation')
->addViolation();
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Domain/Entity/TermsVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public function testBuildDefaultVersion(): void
public function testBuildFromCopy(): void
{
$termsVersion = $this->getReference(TermsVersion::class, 'v3-terms-1');
$this->assertSame(3, $termsVersion->getVersion());

$entity = TermsVersion::buildFromCopy($termsVersion);

$this->assertNull($entity->getId());
Expand All @@ -114,7 +116,7 @@ public function testBuildFromCopy(): void
$this->assertEmpty($entity->getSignatures());

$this->assertSame($termsVersion->getTerms(), $entity->getTerms());
$this->assertSame(4, $entity->getVersion());
$this->assertSame(5, $entity->getVersion());
$this->assertSame('Title Version 3', $entity->getTitle());
$this->assertSame('Content Version 3', $entity->getContent());
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Infrastructure/Adapter/EventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Event\TermsSignedEvent;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionCreatedEvent;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionDeletedEvent;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionEnabledEvent;
use RichId\TermsModuleBundle\Domain\Event\TermsVersionUpdatedEvent;
use RichId\TermsModuleBundle\Domain\Model\DummySubject;
Expand Down Expand Up @@ -53,6 +54,17 @@ public function testDispatchTermsVersionCreatedEvent(): void
$this->assertSame($event, $this->eventDispatcherStub->getEvents()[0]);
}

public function testDispatchTermsVersionDeletedEvent(): void
{
$termsVersion = $this->getReference(TermsVersion::class, 'v3-terms-1');
$event = new TermsVersionDeletedEvent($termsVersion);

$this->adapter->dispatchTermsVersionDeletedEvent($event);

$this->assertCount(1, $this->eventDispatcherStub->getEvents());
$this->assertSame($event, $this->eventDispatcherStub->getEvents()[0]);
}

public function testDispatchTermsVersionEnabledEvent(): void
{
$termsVersion = $this->getReference(TermsVersion::class, 'v3-terms-1');
Expand Down
112 changes: 112 additions & 0 deletions tests/Infrastructure/FormType/TermsVersionFormTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Tests\Infrastructure\FormType;

use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig;
use RichCongress\TestSuite\TestCase\ControllerTestCase;
use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Model\TermsEdition;
use RichId\TermsModuleBundle\Infrastructure\FormType\TermsVersionFormType;
use Symfony\Component\Form\FormFactoryInterface;

/**
* @covers \RichId\TermsModuleBundle\Infrastructure\FormType\TermsVersionFormType
* @TestConfig("kernel")
*/
final class TermsVersionFormTypeTest extends ControllerTestCase
{
/** @var FormFactoryInterface */
public $formFactory;

public function testSubmitEmpty(): void
{
$terms = new Terms();
$termsVersion = new TermsVersion();
$termsVersion->setTerms($terms);

$model = new TermsEdition($termsVersion);
$form = $this->formFactory->create(TermsVersionFormType::class, $model);

$form->submit(
[
'_token' => $this->getCsrfToken(TermsVersionFormType::class),
]
);

$this->assertTrue($form->isSynchronized());
$this->assertFalse($form->isValid());

$output = $form->getData();
$this->assertNull($output->getTitle());
$this->assertNull($output->getContent());
$this->assertNull($output->getPublicationDate());
$this->assertNull($output->isTermsPublished());
$this->assertNull($output->needVersionActivation());
$this->assertSame($termsVersion, $output->getEntity());
}

public function testSubmitValidDataMinimum(): void
{
$terms = new Terms();
$termsVersion = new TermsVersion();
$termsVersion->setTerms($terms);

$model = new TermsEdition($termsVersion);
$form = $this->formFactory->create(TermsVersionFormType::class, $model);

$form->submit(
[
'title' => 'My title',
'content' => 'My content',
'isTermsPublished' => '0',
'_token' => $this->getCsrfToken(TermsVersionFormType::class),
]
);

$this->assertTrue($form->isSynchronized());
$this->assertTrue($form->isValid());

$output = $form->getData();
$this->assertSame('My title', $output->getTitle());
$this->assertSame('My content', $output->getContent());
$this->assertFalse($output->isTermsPublished());
$this->assertNull($output->getPublicationDate());
$this->assertNull($output->needVersionActivation());
$this->assertSame($termsVersion, $output->getEntity());
}

public function testSubmitValidDataFull(): void
{
$terms = new Terms();
$termsVersion = new TermsVersion();
$termsVersion->setTerms($terms);

$model = new TermsEdition($termsVersion);
$form = $this->formFactory->create(TermsVersionFormType::class, $model);

$form->submit(
[
'title' => 'My title',
'content' => 'My content',
'publicationDate' => '2021-01-01',
'isTermsPublished' => '0',
'needVersionActivation' => 'true',
'_token' => $this->getCsrfToken(TermsVersionFormType::class),
]
);

$this->assertTrue($form->isSynchronized());
$this->assertTrue($form->isValid());

$output = $form->getData();
$this->assertSame('My title', $output->getTitle());
$this->assertSame('My content', $output->getContent());
$this->assertSame('2021-01-01', $output->getPublicationDate()->format('Y-m-d'));
$this->assertFalse($output->isTermsPublished());
$this->assertTrue($output->needVersionActivation());
$this->assertSame($termsVersion, $output->getEntity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace RichId\TermsModuleBundle\Tests\Infrastructure\ValidatorConstraints;

use RichCongress\TestFramework\TestConfiguration\Annotation\TestConfig;
use RichId\TermsModuleBundle\Domain\Entity\Terms;
use RichId\TermsModuleBundle\Domain\Entity\TermsVersion;
use RichId\TermsModuleBundle\Domain\Model\TermsEdition;
use RichId\TermsModuleBundle\Infrastructure\ValidatorConstraints\CantContentChangeIfTermsPublished;
use RichId\TermsModuleBundle\Tests\Resources\Entity\DummyUser;
use RichId\TermsModuleBundle\Tests\Resources\TestCase\ConstraintTestCase;

/**
* @covers \RichId\TermsModuleBundle\Infrastructure\ValidatorConstraints\CantContentChangeIfTermsPublished
* @TestConfig("kernel")
*/
final class CantContentChangeIfTermsPublishedTest extends ConstraintTestCase
{
/** @var CantContentChangeIfTermsPublished */
public $validator;

public function testGetTargets(): void
{
$this->assertSame('class', $this->validator->getTargets());
}

public function testValidatedBy(): void
{
$this->assertSame(CantContentChangeIfTermsPublished::class, $this->validator->validatedBy());
}

public function testValidatorWithBadClass(): void
{
$violations = $this->validate(new DummyUser());
$this->assertEmpty($violations);
}

public function testValidatorNotEnabledTermsVersion(): void
{
$terms = new Terms();
$termsVersion = new TermsVersion();
$termsVersion->setTerms($terms);
$termsVersion->setContent('Old content');

$model = new TermsEdition($termsVersion);
$model->setContent('New Content');

$violations = $this->validate($model);
$this->assertEmpty($violations);
}

public function testValidatorEnabledTermsVersionContentNotChange(): void
{
$terms = new Terms();
$termsVersion = new TermsVersion();
$termsVersion->setTerms($terms);
$termsVersion->setContent('Old content');
$termsVersion->enable();

$model = new TermsEdition($termsVersion);
$model->setContent('Old content');

$violations = $this->validate($model);
$this->assertEmpty($violations);
}

public function testValidatorEnabledTermsVersionContentChange(): void
{
$terms = new Terms();
$termsVersion = new TermsVersion();
$termsVersion->setTerms($terms);
$termsVersion->setContent('Old content');
$termsVersion->enable();

$model = new TermsEdition($termsVersion);
$model->setContent('New content');

$violations = $this->validate($model);

$this->assertCount(1, $violations);
$this->assertSame('The content cannot be changed when the version is published.', $violations[0]->getMessage());
$this->assertSame('content', $violations[0]->getPropertyPath());
}
}
Loading

0 comments on commit 14bf9dc

Please sign in to comment.