This repository was archived by the owner on Jun 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathRecaptchaCustomValidationRuleTest.php
More file actions
88 lines (75 loc) · 2.18 KB
/
RecaptchaCustomValidationRuleTest.php
File metadata and controls
88 lines (75 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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.');
}
}