From 3e244e67e5a8e1e4bce775a1843b64196ce87a07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vencel=20K=C3=A1tai?= Date: Fri, 9 Feb 2024 17:17:29 +0100 Subject: [PATCH] Remove fallback error message from recaptcha validator --- README.md | 6 -- config/recaptcha.php | 18 ----- src/ReCaptchaServiceProvider.php | 8 +- tests/RecaptchaCustomValidationRuleTest.php | 88 +++++++++++++++++++++ 4 files changed, 89 insertions(+), 31 deletions(-) create mode 100644 tests/RecaptchaCustomValidationRuleTest.php diff --git a/README.md b/README.md index 5f3a976..5b36d7a 100644 --- a/README.md +++ b/README.md @@ -126,10 +126,6 @@ return [ 'explicit' => false, // true|false // @since v4.3.0 'api_domain' => "www.google.com", // default value is "www.google.com" - // @since v5.1.0 - 'empty_message' => false, - // @since v5.1.0 - 'error_message_key' => 'validation.recaptcha', // @since v4.0.0 'tag_attributes' => [ 'theme' => 'light', // "light"|"dark" @@ -154,8 +150,6 @@ return [ | `default_form_id` | `string` | the default form ID. Only for "invisible" reCAPTCHA | `'biscolab-recaptcha-invisible-form'` | | `explicit` | `bool` | deferring the render can be achieved by specifying your onload callback function and adding parameters to the JavaScript resource. It has no effect with v3 and invisible (supported values: true|false) | `false` | | `api_domain` | `string` | customize API domain. Default value is `'www.google.com'`, but, if not accessible you ca set that value to `'www.recaptcha.net'`. More info about [Can I use reCAPTCHA globally?](https://developers.google.com/recaptcha/docs/faq#can-i-use-recaptcha-globally) | `'www.google.com'` | -| `empty_message` | `bool` | set default error message to `null` | `false` | -| `error_message_key` | `string` | set default error message translation key | `'validation.recaptcha'` | #### (array) tag_attributes diff --git a/config/recaptcha.php b/config/recaptcha.php index f1b23b8..ea777b9 100644 --- a/config/recaptcha.php +++ b/config/recaptcha.php @@ -112,24 +112,6 @@ */ 'api_domain' => 'www.google.com', - /** - * - * Set `true` when the error message must be null - * @since v5.1.0 - * Default false - * - */ - 'empty_message' => false, - - /** - * - * Set either the error message or the errom message translation key - * @since v5.1.0 - * Default 'validation.recaptcha' - * - */ - 'error_message_key' => 'validation.recaptcha', - /** * * g-recaptcha tag attributes and grecaptcha.render parameters (v2 only) diff --git a/src/ReCaptchaServiceProvider.php b/src/ReCaptchaServiceProvider.php index f7b2e96..c913bf5 100755 --- a/src/ReCaptchaServiceProvider.php +++ b/src/ReCaptchaServiceProvider.php @@ -47,15 +47,9 @@ public function boot() */ public function addValidationRule() { - $message = null; - - if (!config('recaptcha.empty_message')) { - $message = trans(config('recaptcha.error_message_key')); - } Validator::extendImplicit(recaptchaRuleName(), function ($attribute, $value) { - return app('recaptcha')->validate($value); - }, $message); + }); } /** diff --git a/tests/RecaptchaCustomValidationRuleTest.php b/tests/RecaptchaCustomValidationRuleTest.php new file mode 100644 index 0000000..509974c --- /dev/null +++ b/tests/RecaptchaCustomValidationRuleTest.php @@ -0,0 +1,88 @@ +once() + ->andReturn(true); + + $this->assertTrue($this->validator->passes()); + } + + /** + * @test + */ + public function testValidationRuleUsesTheDefaultErrorKey() + { + try { + $this->failValidation(); + } catch (ValidationException $exception) { + $this->assertEquals( + 'validation.' . ReCaptchaBuilder::DEFAULT_RECAPTCHA_RULE_NAME, + $exception->getMessage() + ); + } + } + + /** + * @test + */ + public function testValidationRuleTranslatesTheErrorMessage() + { + Lang::addLines([ + 'validation.' . ReCaptchaBuilder::DEFAULT_RECAPTCHA_RULE_NAME => 'Translated recaptcha error' + ], $this->app->getLocale()); + + try { + $this->failValidation(); + } catch (ValidationException $exception) { + $this->assertEquals( + 'Translated recaptcha error', + $exception->getMessage() + ); + } + } + + protected function setUp(): void + { + parent::setUp(); + + $this->validator = ValidatorFacade::make( + [ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME => 'test'], + [ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME => ReCaptchaBuilder::DEFAULT_RECAPTCHA_RULE_NAME] + ); + } + + /** + * @throws ValidationException + */ + private function failValidation(): void + { + ReCaptcha::shouldReceive('validate') + ->once() + ->andReturn(false); + + $this->validator->validate(); + + $this->fail('Expecting validation to throw an exception.'); + } +}