From b2b9c7fabf3b22b2c47a019f6fd1d862d854479d Mon Sep 17 00:00:00 2001 From: Stephan Vock Date: Wed, 13 Mar 2024 11:00:19 +0000 Subject: [PATCH] Add error code to Recaptcha cosntraint --- src/Validator/Constraints/Recaptcha2.php | 6 ++++++ src/Validator/Constraints/Recaptcha2Validator.php | 5 ++++- tests/Validator/Constraints/Recaptcha2ValidatorTest.php | 6 +++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Validator/Constraints/Recaptcha2.php b/src/Validator/Constraints/Recaptcha2.php index f59ed03..9e3870b 100644 --- a/src/Validator/Constraints/Recaptcha2.php +++ b/src/Validator/Constraints/Recaptcha2.php @@ -9,6 +9,12 @@ */ final class Recaptcha2 extends Constraint { + public const INVALID_RECAPTCHA_ERROR = 'b2c483cd-90b6-4810-aa45-fd615e89f046'; + + protected const ERROR_NAMES = [ + self::INVALID_RECAPTCHA_ERROR => 'INVALID_RECAPTCHA_ERROR', + ]; + public string $message = 'Invalid ReCaptcha.'; public function validatedBy(): string diff --git a/src/Validator/Constraints/Recaptcha2Validator.php b/src/Validator/Constraints/Recaptcha2Validator.php index 6812fed..894a0c4 100644 --- a/src/Validator/Constraints/Recaptcha2Validator.php +++ b/src/Validator/Constraints/Recaptcha2Validator.php @@ -22,7 +22,10 @@ public function validate(mixed $value, Constraint $constraint): void try { $this->verifier->verify($value); } catch (RecaptchaException) { - $this->context->addViolation($constraint->message); + $this->context + ->buildViolation($constraint->message) + ->setCode(Recaptcha2::INVALID_RECAPTCHA_ERROR) + ->addViolation(); } } } diff --git a/tests/Validator/Constraints/Recaptcha2ValidatorTest.php b/tests/Validator/Constraints/Recaptcha2ValidatorTest.php index 71dd67a..9b07860 100644 --- a/tests/Validator/Constraints/Recaptcha2ValidatorTest.php +++ b/tests/Validator/Constraints/Recaptcha2ValidatorTest.php @@ -36,7 +36,7 @@ public function testValidateShouldThrowException(): void $exception = new RecaptchaException($response); $constraint = new Recaptcha2(); $this->verifier->expects(self::once())->method('verify')->will(self::throwException($exception)); - $this->context->expects(self::once())->method('addViolation'); + $this->context->expects(self::once())->method('buildViolation'); $this->validator->validate('dummy', $constraint); } @@ -45,7 +45,7 @@ public function testValidateShouldNotThrowException(): void { $constraint = new Recaptcha2(); $this->verifier->expects(self::once())->method('verify'); - $this->context->expects(self::never())->method('addViolation'); + $this->context->expects(self::never())->method('buildViolation'); $this->validator->validate('dummy', $constraint); } @@ -54,7 +54,7 @@ public function testValidateShouldAcceptEmptyValues(): void { $constraint = new Recaptcha2(); $this->verifier->expects(self::once())->method('verify'); - $this->context->expects(self::never())->method('addViolation'); + $this->context->expects(self::never())->method('buildViolation'); $this->validator->validate(null, $constraint); }