Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Remove fallback error message from recaptcha validator #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down
18 changes: 0 additions & 18 deletions config/recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 1 addition & 7 deletions src/ReCaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

/**
Expand Down
88 changes: 88 additions & 0 deletions tests/RecaptchaCustomValidationRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Biscolab\ReCaptcha\Tests;

use Biscolab\ReCaptcha\Facades\ReCaptcha;
use Biscolab\ReCaptcha\ReCaptchaBuilder;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Validator as ValidatorFacade;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Validator;

class RecaptchaCustomValidationRuleTest extends TestCase
{
/**
* @var Validator
*/
private $validator;

/**
* @test
*/
public function testValidationRuleValidatesResponse()
{
ReCaptcha::shouldReceive('validate')
->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.');
}
}