From 14bf9dcb9ac9a404d1e3a0aa9d850ffde1d0b938 Mon Sep 17 00:00:00 2001 From: Dumazeau Date: Thu, 15 Jul 2021 16:42:56 +0200 Subject: [PATCH] #48056, edit bundle --- src/Domain/Model/TermsEdition.php | 10 +- src/Domain/UseCase/EditTerms.php | 2 +- .../FormType/TermsVersionFormType.php | 6 +- .../views/admin/edit/_partial/_body.html.twig | 2 +- .../admin/edit/_partial/js/_ajax.js.twig | 2 +- .../CantEnableVersionIfAlreadyEnabled.php | 4 +- tests/Domain/Entity/TermsVersionTest.php | 4 +- .../Adapter/EventDispatcherTest.php | 12 ++ .../FormType/TermsVersionFormTypeTest.php | 112 +++++++++++++++ .../CantContentChangeIfTermsPublishedTest.php | 86 +++++++++++ .../CantEnableVersionIfAlreadyEnabledTest.php | 82 +++++++++++ ...licationDateChangeIfTermsPublishedTest.php | 133 ++++++++++++++++++ .../CantTitleChangeIfTermsPublishedTest.php | 86 +++++++++++ .../CantUnpublishLockedPublishedTermsTest.php | 114 +++++++++++++++ tests/Resources/Kernel/config/bundles.php | 1 + tests/Resources/Kernel/config/services.yaml | 2 + .../Resources/Stubs/ValidationContextStub.php | 25 ++++ .../Resources/TestCase/ConstraintTestCase.php | 41 ++++++ 18 files changed, 708 insertions(+), 16 deletions(-) create mode 100644 tests/Infrastructure/FormType/TermsVersionFormTypeTest.php create mode 100644 tests/Infrastructure/ValidatorConstraints/CantContentChangeIfTermsPublishedTest.php create mode 100644 tests/Infrastructure/ValidatorConstraints/CantEnableVersionIfAlreadyEnabledTest.php create mode 100644 tests/Infrastructure/ValidatorConstraints/CantPublicationDateChangeIfTermsPublishedTest.php create mode 100644 tests/Infrastructure/ValidatorConstraints/CantTitleChangeIfTermsPublishedTest.php create mode 100644 tests/Infrastructure/ValidatorConstraints/CantUnpublishLockedPublishedTermsTest.php create mode 100644 tests/Resources/Stubs/ValidationContextStub.php create mode 100644 tests/Resources/TestCase/ConstraintTestCase.php diff --git a/src/Domain/Model/TermsEdition.php b/src/Domain/Model/TermsEdition.php index 59aa0c4..0ced607 100644 --- a/src/Domain/Model/TermsEdition.php +++ b/src/Domain/Model/TermsEdition.php @@ -56,7 +56,7 @@ class TermsEdition * * @Assert\Type("bool") */ - private $activateVersion; + private $needVersionActivation; public function __construct(TermsVersion $entity) { @@ -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; } diff --git a/src/Domain/UseCase/EditTerms.php b/src/Domain/UseCase/EditTerms.php index 669485e..9cd2545 100644 --- a/src/Domain/UseCase/EditTerms.php +++ b/src/Domain/UseCase/EditTerms.php @@ -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); } diff --git a/src/Infrastructure/FormType/TermsVersionFormType.php b/src/Infrastructure/FormType/TermsVersionFormType.php index d95c1ba..43d8d36 100644 --- a/src/Infrastructure/FormType/TermsVersionFormType.php +++ b/src/Infrastructure/FormType/TermsVersionFormType.php @@ -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; @@ -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 @@ -82,7 +79,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ] ) ->add( - 'activateVersion', + 'needVersionActivation', HiddenType::class, [ 'required' => false, @@ -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, ] diff --git a/src/Infrastructure/Resources/views/admin/edit/_partial/_body.html.twig b/src/Infrastructure/Resources/views/admin/edit/_partial/_body.html.twig index 564707b..f26336a 100644 --- a/src/Infrastructure/Resources/views/admin/edit/_partial/_body.html.twig +++ b/src/Infrastructure/Resources/views/admin/edit/_partial/_body.html.twig @@ -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) }}
diff --git a/src/Infrastructure/Resources/views/admin/edit/_partial/js/_ajax.js.twig b/src/Infrastructure/Resources/views/admin/edit/_partial/js/_ajax.js.twig index f4f5951..3dbfdb8 100644 --- a/src/Infrastructure/Resources/views/admin/edit/_partial/js/_ajax.js.twig +++ b/src/Infrastructure/Resources/views/admin/edit/_partial/js/_ajax.js.twig @@ -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 %} }; diff --git a/src/Infrastructure/ValidatorConstraints/CantEnableVersionIfAlreadyEnabled.php b/src/Infrastructure/ValidatorConstraints/CantEnableVersionIfAlreadyEnabled.php index b2b1352..26a1e8e 100644 --- a/src/Infrastructure/ValidatorConstraints/CantEnableVersionIfAlreadyEnabled.php +++ b/src/Infrastructure/ValidatorConstraints/CantEnableVersionIfAlreadyEnabled.php @@ -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(); } diff --git a/tests/Domain/Entity/TermsVersionTest.php b/tests/Domain/Entity/TermsVersionTest.php index e6fc0b3..faffd97 100644 --- a/tests/Domain/Entity/TermsVersionTest.php +++ b/tests/Domain/Entity/TermsVersionTest.php @@ -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()); @@ -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()); } diff --git a/tests/Infrastructure/Adapter/EventDispatcherTest.php b/tests/Infrastructure/Adapter/EventDispatcherTest.php index 6d66d71..559e74f 100644 --- a/tests/Infrastructure/Adapter/EventDispatcherTest.php +++ b/tests/Infrastructure/Adapter/EventDispatcherTest.php @@ -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; @@ -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'); diff --git a/tests/Infrastructure/FormType/TermsVersionFormTypeTest.php b/tests/Infrastructure/FormType/TermsVersionFormTypeTest.php new file mode 100644 index 0000000..13b1e64 --- /dev/null +++ b/tests/Infrastructure/FormType/TermsVersionFormTypeTest.php @@ -0,0 +1,112 @@ +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()); + } +} diff --git a/tests/Infrastructure/ValidatorConstraints/CantContentChangeIfTermsPublishedTest.php b/tests/Infrastructure/ValidatorConstraints/CantContentChangeIfTermsPublishedTest.php new file mode 100644 index 0000000..2f87cf7 --- /dev/null +++ b/tests/Infrastructure/ValidatorConstraints/CantContentChangeIfTermsPublishedTest.php @@ -0,0 +1,86 @@ +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()); + } +} diff --git a/tests/Infrastructure/ValidatorConstraints/CantEnableVersionIfAlreadyEnabledTest.php b/tests/Infrastructure/ValidatorConstraints/CantEnableVersionIfAlreadyEnabledTest.php new file mode 100644 index 0000000..fd2ac38 --- /dev/null +++ b/tests/Infrastructure/ValidatorConstraints/CantEnableVersionIfAlreadyEnabledTest.php @@ -0,0 +1,82 @@ +assertSame('class', $this->validator->getTargets()); + } + + public function testValidatedBy(): void + { + $this->assertSame(CantEnableVersionIfAlreadyEnabled::class, $this->validator->validatedBy()); + } + + public function testValidatorWithBadClass(): void + { + $violations = $this->validate(new DummyUser()); + $this->assertEmpty($violations); + } + + public function testValidatorWithoutActivation(): void + { + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + $termsVersion->enable(); + + $model = new TermsEdition($termsVersion); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorNeedVersionActivationNotEnabled(): void + { + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + + $model = new TermsEdition($termsVersion); + $model->setNeedVersionActivation(true); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorNeedVersionActivationAlreadyEnabled(): void + { + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + $termsVersion->enable(); + + $model = new TermsEdition($termsVersion); + $model->setNeedVersionActivation(true); + + $violations = $this->validate($model); + + $this->assertCount(1, $violations); + $this->assertSame('The version is already activated.', $violations[0]->getMessage()); + $this->assertSame('needVersionActivation', $violations[0]->getPropertyPath()); + } +} diff --git a/tests/Infrastructure/ValidatorConstraints/CantPublicationDateChangeIfTermsPublishedTest.php b/tests/Infrastructure/ValidatorConstraints/CantPublicationDateChangeIfTermsPublishedTest.php new file mode 100644 index 0000000..b90083e --- /dev/null +++ b/tests/Infrastructure/ValidatorConstraints/CantPublicationDateChangeIfTermsPublishedTest.php @@ -0,0 +1,133 @@ +assertSame('class', $this->validator->getTargets()); + } + + public function testValidatedBy(): void + { + $this->assertSame(CantPublicationDateChangeIfTermsPublished::class, $this->validator->validatedBy()); + } + + public function testValidatorWithBadClass(): void + { + $violations = $this->validate(new DummyUser()); + $this->assertEmpty($violations); + } + + public function testValidatorNotEnabledTermsVersion(): void + { + $date1 = new \DateTime(); + $date2 = new \DateTime('- 1 day'); + + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + $termsVersion->setPublicationDate($date1); + + $model = new TermsEdition($termsVersion); + $model->setPublicationDate($date2); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorEnabledTermsVersionPublicationDateNotChange(): void + { + $date = new \DateTime(); + + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + $termsVersion->setPublicationDate($date); + $termsVersion->enable(); + + $model = new TermsEdition($termsVersion); + $model->setPublicationDate($date); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorEnabledTermsVersionPublicationDateChange(): void + { + $date1 = new \DateTime(); + $date2 = new \DateTime('- 1 day'); + + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + $termsVersion->setPublicationDate($date1); + $termsVersion->enable(); + + $model = new TermsEdition($termsVersion); + $model->setPublicationDate($date2); + + $violations = $this->validate($model); + + $this->assertCount(1, $violations); + $this->assertSame('The publication date cannot be changed when the version is published.', $violations[0]->getMessage()); + $this->assertSame('publicationDate', $violations[0]->getPropertyPath()); + } + + public function testValidatorEnabledTermsVersionPublicationDateChangeToNull(): void + { + $date = new \DateTime(); + + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + $termsVersion->setPublicationDate($date); + $termsVersion->enable(); + + $model = new TermsEdition($termsVersion); + $model->setPublicationDate(null); + + $violations = $this->validate($model); + + $this->assertCount(1, $violations); + $this->assertSame('The publication date cannot be changed when the version is published.', $violations[0]->getMessage()); + $this->assertSame('publicationDate', $violations[0]->getPropertyPath()); + } + + public function testValidatorEnabledTermsVersionPublicationDateChangeFromNull(): void + { + $date = new \DateTime(); + + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + $termsVersion->enable(); + + $model = new TermsEdition($termsVersion); + $model->setPublicationDate($date); + + $violations = $this->validate($model); + + $this->assertCount(1, $violations); + $this->assertSame('The publication date cannot be changed when the version is published.', $violations[0]->getMessage()); + $this->assertSame('publicationDate', $violations[0]->getPropertyPath()); + } +} diff --git a/tests/Infrastructure/ValidatorConstraints/CantTitleChangeIfTermsPublishedTest.php b/tests/Infrastructure/ValidatorConstraints/CantTitleChangeIfTermsPublishedTest.php new file mode 100644 index 0000000..ef77840 --- /dev/null +++ b/tests/Infrastructure/ValidatorConstraints/CantTitleChangeIfTermsPublishedTest.php @@ -0,0 +1,86 @@ +assertSame('class', $this->validator->getTargets()); + } + + public function testValidatedBy(): void + { + $this->assertSame(CantTitleChangeIfTermsPublished::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->setTitle('Old title'); + + $model = new TermsEdition($termsVersion); + $model->setTitle('New title'); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorEnabledTermsVersionTitleNotChange(): void + { + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + $termsVersion->setTitle('Old title'); + $termsVersion->enable(); + + $model = new TermsEdition($termsVersion); + $model->setTitle('Old title'); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorEnabledTermsVersionTitleChange(): void + { + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + $termsVersion->setTitle('Old title'); + $termsVersion->enable(); + + $model = new TermsEdition($termsVersion); + $model->setTitle('New title'); + + $violations = $this->validate($model); + + $this->assertCount(1, $violations); + $this->assertSame('The title cannot be changed when the version is published.', $violations[0]->getMessage()); + $this->assertSame('title', $violations[0]->getPropertyPath()); + } +} diff --git a/tests/Infrastructure/ValidatorConstraints/CantUnpublishLockedPublishedTermsTest.php b/tests/Infrastructure/ValidatorConstraints/CantUnpublishLockedPublishedTermsTest.php new file mode 100644 index 0000000..5b6a079 --- /dev/null +++ b/tests/Infrastructure/ValidatorConstraints/CantUnpublishLockedPublishedTermsTest.php @@ -0,0 +1,114 @@ +assertSame('class', $this->validator->getTargets()); + } + + public function testValidatedBy(): void + { + $this->assertSame(CantUnpublishLockedPublishedTerms::class, $this->validator->validatedBy()); + } + + public function testValidatorWithBadClass(): void + { + $violations = $this->validate(new DummyUser()); + $this->assertEmpty($violations); + } + + public function testValidatorTermsAlwaysPublished(): void + { + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + + $model = new TermsEdition($termsVersion); + $model->setIsTermsPublished(true); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorUnpublishedUnpublishedVersion(): void + { + $terms = new Terms(); + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + + $model = new TermsEdition($termsVersion); + $model->setIsTermsPublished(false); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorUnpublishedUnpublishedVersionLocked(): void + { + $terms = new Terms(); + $terms->setIsDepublicationLocked(true); + + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + + $model = new TermsEdition($termsVersion); + $model->setIsTermsPublished(false); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorUnpublishedPublishedVersion(): void + { + $terms = new Terms(); + $terms->setIsPublished(true); + + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + + $model = new TermsEdition($termsVersion); + $model->setIsTermsPublished(false); + + $violations = $this->validate($model); + $this->assertEmpty($violations); + } + + public function testValidatorUnpublishedPublishedVersionLocked(): void + { + $terms = new Terms(); + $terms->setIsPublished(true); + $terms->setIsDepublicationLocked(true); + + $termsVersion = new TermsVersion(); + $termsVersion->setTerms($terms); + + $model = new TermsEdition($termsVersion); + $model->setIsTermsPublished(false); + + $violations = $this->validate($model); + + $this->assertCount(1, $violations); + $this->assertSame('It is not possible to unpublish a locked terms.', $violations[0]->getMessage()); + $this->assertSame('isTermsPublished', $violations[0]->getPropertyPath()); + } +} diff --git a/tests/Resources/Kernel/config/bundles.php b/tests/Resources/Kernel/config/bundles.php index 50c95f6..0d94eaa 100644 --- a/tests/Resources/Kernel/config/bundles.php +++ b/tests/Resources/Kernel/config/bundles.php @@ -8,4 +8,5 @@ Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true], RichCongress\RecurrentFixturesTestBundle\RichCongressRecurrentFixturesTestBundle::class => ['all' => true], RichId\TermsModuleBundle\Infrastructure\RichIdTermsModuleBundle::class => ['all' => true], + FOS\CKEditorBundle\FOSCKEditorBundle::class => ['all' => true], ]; diff --git a/tests/Resources/Kernel/config/services.yaml b/tests/Resources/Kernel/config/services.yaml index bfd4e95..4fc3a30 100755 --- a/tests/Resources/Kernel/config/services.yaml +++ b/tests/Resources/Kernel/config/services.yaml @@ -9,6 +9,7 @@ services: RichId\TermsModuleBundle\Infrastructure\Adapter\EntityRemover: ~ RichId\TermsModuleBundle\Infrastructure\Adapter\TermsVersionRepository: ~ + RichId\TermsModuleBundle\Infrastructure\FormType\TermsVersionFormType: ~ RichId\TermsModuleBundle\Infrastructure\TwigExtension\TermsExtension: ~ RichId\TermsModuleBundle\Tests\Resources\Controller\FrontRoute: ~ @@ -19,6 +20,7 @@ services: RichId\TermsModuleBundle\Tests\Resources\Stubs\EntityManagerStub: ~ RichId\TermsModuleBundle\Tests\Resources\Stubs\EventDispatcherStub: ~ RichId\TermsModuleBundle\Tests\Resources\Stubs\LoggerStub: ~ + RichId\TermsModuleBundle\Tests\Resources\Stubs\ValidationContextStub: ~ RichId\TermsModuleBundle\Tests\Resources\EventListener\Terms4SignedEventListener: tags: diff --git a/tests/Resources/Stubs/ValidationContextStub.php b/tests/Resources/Stubs/ValidationContextStub.php new file mode 100644 index 0000000..9538e53 --- /dev/null +++ b/tests/Resources/Stubs/ValidationContextStub.php @@ -0,0 +1,25 @@ +getValidator(); + + parent::__construct($validator, '', $translator); + } + + public function countViolations(): int + { + return $this->getViolations()->count(); + } +} diff --git a/tests/Resources/TestCase/ConstraintTestCase.php b/tests/Resources/TestCase/ConstraintTestCase.php new file mode 100644 index 0000000..6a91b5a --- /dev/null +++ b/tests/Resources/TestCase/ConstraintTestCase.php @@ -0,0 +1,41 @@ +context->setConstraint($this->validator); /* @phpstan-ignore-line */ + $this->validator->initialize($this->context); /* @phpstan-ignore-line */ + } + + /** + * @param mixed $value + * @param mixed|null $object + * + * @return ConstraintViolationListInterface + */ + public function validate($value, $object = null): ConstraintViolationListInterface + { + $this->context->setNode($value, $object, null, ''); + $violations = $this->validator->validate($value, $this->validator); /* @phpstan-ignore-line */ + + return $violations ?? $this->context->getViolations(); + } +}