diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 341944299..e488edd79 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -49,10 +49,6 @@ use UnexpectedValueException; * "accompanying_period": AccompanyingPeriod::class * }) * @Assert\GroupSequenceProvider - * @Assert\Expression( - * "this.isConfidential && this.getUser && this.getStep === 'CONFIRMED'", - * message="If the accompanying course is confirmed and confidential, a referrer must remain assigned." - * ) */ class AccompanyingPeriod implements TrackCreationInterface, @@ -860,7 +856,7 @@ class AccompanyingPeriod implements } /** - * Validation function. + * Validation functions. */ public function isDateConsistent(ExecutionContextInterface $context) { @@ -875,6 +871,21 @@ class AccompanyingPeriod implements } } + /** + * @Assert\Callback + */ + public function canUserBeNull(ExecutionContextInterface $context) + { + if ($this->getStep() === self::STEP_CONFIRMED && $this->isConfidential() === true) + { + if (!$this->getUser()) { + $context->buildViolation('User cannot be null for an accompanying period that is confirmed and confidential') + ->atPath('user') + ->addViolation(); + } + } + } + public function isEmergency(): bool { return $this->emergency; diff --git a/src/Bundle/ChillPersonBundle/Tests/AccompanyingPeriod/AccompanyingPeriodConfidentialTest.php b/src/Bundle/ChillPersonBundle/Tests/AccompanyingPeriod/AccompanyingPeriodConfidentialTest.php index 86759d9fc..1986f67e9 100644 --- a/src/Bundle/ChillPersonBundle/Tests/AccompanyingPeriod/AccompanyingPeriodConfidentialTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/AccompanyingPeriod/AccompanyingPeriodConfidentialTest.php @@ -10,6 +10,8 @@ namespace Chill\PersonBundle\Tests\AccompanyingPeriod; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Entity\Scope; +use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -34,8 +36,13 @@ class AccompanyingPeriodConfidentialTest extends KernelTestCase $period = new AccompanyingPeriod(); $period->setConfidential(true); $period->setStep('CONFIRMED'); + $period->setIntensity('regular'); + $period->setOrigin(new Origin()); + $period->addScope(new Scope()); - $violations = self::$validator->validate($period, []); + var_dump($period); + + $violations = self::$validator->validate($period); $this->assertCount(1, $violations); } @@ -45,9 +52,12 @@ class AccompanyingPeriodConfidentialTest extends KernelTestCase $period = new AccompanyingPeriod(); $period->setConfidential(true); $period->setStep('CONFIRMED'); + $period->setIntensity('regular'); + $period->setOrigin(new Origin()); + $period->addScope(new Scope()); $period->setUser(new User()); - $violations = self::$validator->validate($period, []); + $violations = self::$validator->validate($period); $this->assertCount(0, $violations); }