Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit 02d6e91

Browse files
committed
Allow symfony6.
1 parent c7e375a commit 02d6e91

4 files changed

Lines changed: 394 additions & 8 deletions

File tree

.github/workflows/build.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ jobs:
1414
strategy:
1515
fail-fast: true
1616
matrix:
17-
php: [7.1, 7.2, 7.3, 7.4, 8.0]
18-
symfony: [2.8, 3.4, 4.4, 5.2]
17+
php: [7.1, 7.2, 7.3, 7.4, 8.0, 8.1]
18+
symfony: [2.8, 3.4, 4.4, 5.2, 6.0]
1919
exclude:
2020
- php: 7.1
2121
symfony: 5.2
2222
- php: 8.0
2323
symfony: 3.4
24+
- php: 7.1
25+
symfony: 6.0
26+
- php: 7.2
27+
symfony: 6.0
28+
- php: 7.3
29+
symfony: 6.0
30+
- php: 7.4
31+
symfony: 6.0
2432
steps:
2533
- name: Checkout
2634
uses: actions/checkout@v2

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
"require": {
1818
"php": "^7.1 || ^8.0",
1919
"google/recaptcha": "^1.1",
20-
"symfony/form": "^2.8 || ^3.0 || ^4.0 || ^5.0",
21-
"symfony/framework-bundle": "^2.8 || ^3.0 || ^4.0 || ^5.0",
22-
"symfony/security-bundle": "^2.8 || ^3.0 || ^4.0 || ^5.0",
23-
"symfony/validator": "^2.8 || ^3.0 || ^4.0 || ^5.0",
24-
"symfony/yaml": "^2.8 || ^3.0 || ^4.0 || ^5.0",
20+
"symfony/form": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
21+
"symfony/framework-bundle": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
22+
"symfony/security-bundle": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
23+
"symfony/validator": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
24+
"symfony/yaml": "^2.8 || ^3.0 || ^4.0 || ^5.0 || ^6.0",
2525
"twig/twig": "^1.40 || ^2.9 || ^3.0"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "^7 || ^8"
28+
"phpunit/phpunit": "^7 || ^8 || ^9.5"
2929
},
3030
"autoload": {
3131
"psr-4": {
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace EWZ\Tests\Bundle\RecaptchaBundle\Validator\Constraints;
4+
5+
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue;
6+
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueValidator;
7+
use PHPUnit\Framework\TestCase;
8+
use ReCaptcha\ReCaptcha;
9+
use ReCaptcha\Response;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\RequestStack;
12+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
13+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
14+
15+
class IsTrueValidatorTest extends TestCase
16+
{
17+
18+
public function tesNotEnabled(): void
19+
{
20+
$reCaptcha = $this->createMock(ReCaptcha::class);
21+
$reCaptcha->expects(self::never())
22+
->method('verify');
23+
$requestStack = $this->createMock(RequestStack::class);
24+
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
25+
$context = $this->createMock(ExecutionContextInterface::class);
26+
$context->expects(self::never())
27+
->method('addViolation');
28+
$context->expects(self::never())
29+
->method('buildViolation');
30+
31+
$authorizationChecker->expects(self::never())
32+
->method('isGranted');
33+
34+
$validator = new IsTrueValidator(false, $reCaptcha, $requestStack, true, $authorizationChecker, []);
35+
$validator->initialize($context);
36+
$validator->validate('', new IsTrue());
37+
}
38+
39+
public function testTrustedRolesAreNotValidated(): void
40+
{
41+
$trustedRoles = ['ROLE_TEST'];
42+
$reCaptcha = $this->createMock(ReCaptcha::class);
43+
$reCaptcha->expects(self::never())
44+
->method('verify');
45+
$requestStack = $this->createMock(RequestStack::class);
46+
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
47+
$context = $this->createMock(ExecutionContextInterface::class);
48+
$context->expects(self::never())
49+
->method('addViolation');
50+
$context->expects(self::never())
51+
->method('buildViolation');
52+
53+
$authorizationChecker->expects(self::once())
54+
->method('isGranted')
55+
->with($trustedRoles)
56+
->willReturn(true);
57+
58+
if (\is_callable([$requestStack, 'getMainRequest'])) {
59+
$requestStack->expects(self::never())
60+
->method('getMainRequest');
61+
} else {
62+
$requestStack->expects(self::never())
63+
->method('getMasterRequest');
64+
}
65+
66+
$validator = new IsTrueValidator(true, $reCaptcha, $requestStack, true, $authorizationChecker, $trustedRoles);
67+
$validator->validate('', new IsTrue());
68+
}
69+
70+
public function testResponseNotSuccess(): void
71+
{
72+
$trustedRoles = ['ROLE_TEST'];
73+
$clientIp = '127.0.0.1';
74+
$recaptchaAnswer = 'encoded response';
75+
$constraint = new IsTrue();
76+
$reCaptcha = $this->createMock(ReCaptcha::class);
77+
$requestStack = $this->createMock(RequestStack::class);
78+
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
79+
$context = $this->createMock(ExecutionContextInterface::class);
80+
$context->expects(self::never())
81+
->method('buildViolation');
82+
83+
$authorizationChecker->expects(self::once())
84+
->method('isGranted')
85+
->with($trustedRoles)
86+
->willReturn(false);
87+
88+
$request = $this->createMock(Request::class);
89+
$request->expects(self::once())
90+
->method('getClientIp')
91+
->willReturn($clientIp);
92+
$request->expects(self::once())
93+
->method('get')
94+
->with('g-recaptcha-response')
95+
->willReturn($recaptchaAnswer);
96+
97+
if (\is_callable([$requestStack, 'getMainRequest'])) {
98+
$requestStack->expects(self::once())
99+
->method('getMainRequest')
100+
->willReturn($request);
101+
} else {
102+
$requestStack->expects(self::once())
103+
->method('getMasterRequest')
104+
->willReturn($request);
105+
}
106+
107+
$response = $this->createMock(Response::class);
108+
$response->expects(self::once())
109+
->method('isSuccess')
110+
->willReturn(false);
111+
112+
$reCaptcha->expects(self::once())
113+
->method('verify')
114+
->with($recaptchaAnswer, $clientIp)
115+
->willReturn($response);
116+
117+
$context->expects(self::once())
118+
->method('addViolation')
119+
->with($constraint->message);
120+
121+
$validator = new IsTrueValidator(true, $reCaptcha, $requestStack, true, $authorizationChecker, $trustedRoles);
122+
$validator->initialize($context);
123+
$validator->validate('', $constraint);
124+
}
125+
126+
public function testInvalidHostWithVerifyHost(): void
127+
{
128+
$trustedRoles = ['ROLE_TEST'];
129+
$clientIp = '127.0.0.1';
130+
$recaptchaAnswer = 'encoded response';
131+
$constraint = new IsTrue();
132+
$reCaptcha = $this->createMock(ReCaptcha::class);
133+
$requestStack = $this->createMock(RequestStack::class);
134+
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
135+
$context = $this->createMock(ExecutionContextInterface::class);
136+
$context->expects(self::never())
137+
->method('buildViolation');
138+
139+
$authorizationChecker->expects(self::once())
140+
->method('isGranted')
141+
->with($trustedRoles)
142+
->willReturn(false);
143+
144+
$request = $this->createMock(Request::class);
145+
$request->expects(self::once())
146+
->method('getClientIp')
147+
->willReturn($clientIp);
148+
$request->expects(self::once())
149+
->method('get')
150+
->with('g-recaptcha-response')
151+
->willReturn($recaptchaAnswer);
152+
$request->expects(self::once())
153+
->method('getHost')
154+
->willReturn('host1');
155+
156+
if (\is_callable([$requestStack, 'getMainRequest'])) {
157+
$requestStack->expects(self::once())
158+
->method('getMainRequest')
159+
->willReturn($request);
160+
} else {
161+
$requestStack->expects(self::once())
162+
->method('getMasterRequest')
163+
->willReturn($request);
164+
}
165+
166+
$response = $this->createMock(Response::class);
167+
$response->expects(self::once())
168+
->method('isSuccess')
169+
->willReturn(true);
170+
$response->expects(self::once())
171+
->method('getHostname')
172+
->willReturn('host2');
173+
174+
$reCaptcha->expects(self::once())
175+
->method('verify')
176+
->with($recaptchaAnswer, $clientIp)
177+
->willReturn($response);
178+
179+
$context->expects(self::once())
180+
->method('addViolation')
181+
->with($constraint->invalidHostMessage);
182+
183+
$validator = new IsTrueValidator(true, $reCaptcha, $requestStack, true, $authorizationChecker, $trustedRoles);
184+
$validator->initialize($context);
185+
$validator->validate('', $constraint);
186+
}
187+
188+
public function testInvalidHostWithoutVerifyHost(): void
189+
{
190+
$trustedRoles = ['ROLE_TEST'];
191+
$clientIp = '127.0.0.1';
192+
$recaptchaAnswer = 'encoded response';
193+
$constraint = new IsTrue();
194+
$reCaptcha = $this->createMock(ReCaptcha::class);
195+
$requestStack = $this->createMock(RequestStack::class);
196+
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
197+
$context = $this->createMock(ExecutionContextInterface::class);
198+
$context->expects(self::never())
199+
->method('buildViolation');
200+
201+
$authorizationChecker->expects(self::once())
202+
->method('isGranted')
203+
->with($trustedRoles)
204+
->willReturn(false);
205+
206+
$request = $this->createMock(Request::class);
207+
$request->expects(self::once())
208+
->method('getClientIp')
209+
->willReturn($clientIp);
210+
$request->expects(self::once())
211+
->method('get')
212+
->with('g-recaptcha-response')
213+
->willReturn($recaptchaAnswer);
214+
$request->expects(self::never())
215+
->method('getHost');
216+
217+
if (\is_callable([$requestStack, 'getMainRequest'])) {
218+
$requestStack->expects(self::once())
219+
->method('getMainRequest')
220+
->willReturn($request);
221+
} else {
222+
$requestStack->expects(self::once())
223+
->method('getMasterRequest')
224+
->willReturn($request);
225+
}
226+
227+
$response = $this->createMock(Response::class);
228+
$response->expects(self::once())
229+
->method('isSuccess')
230+
->willReturn(true);
231+
$response->expects(self::never())
232+
->method('getHostname');
233+
234+
$reCaptcha->expects(self::once())
235+
->method('verify')
236+
->with($recaptchaAnswer, $clientIp)
237+
->willReturn($response);
238+
239+
$context->expects(self::never())
240+
->method('addViolation');
241+
242+
$validator = new IsTrueValidator(true, $reCaptcha, $requestStack, false, $authorizationChecker, $trustedRoles);
243+
$validator->initialize($context);
244+
$validator->validate('', $constraint);
245+
}
246+
247+
public function testValidWithVerifyHost(): void
248+
{
249+
$trustedRoles = ['ROLE_TEST'];
250+
$clientIp = '127.0.0.1';
251+
$recaptchaAnswer = 'encoded response';
252+
$host = 'host';
253+
$constraint = new IsTrue();
254+
$reCaptcha = $this->createMock(ReCaptcha::class);
255+
$requestStack = $this->createMock(RequestStack::class);
256+
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
257+
$context = $this->createMock(ExecutionContextInterface::class);
258+
$context->expects(self::never())
259+
->method('buildViolation');
260+
$context->expects(self::never())
261+
->method('addViolation');
262+
263+
$authorizationChecker->expects(self::once())
264+
->method('isGranted')
265+
->with($trustedRoles)
266+
->willReturn(false);
267+
268+
$request = $this->createMock(Request::class);
269+
$request->expects(self::once())
270+
->method('getClientIp')
271+
->willReturn($clientIp);
272+
$request->expects(self::once())
273+
->method('get')
274+
->with('g-recaptcha-response')
275+
->willReturn($recaptchaAnswer);
276+
$request->expects(self::once())
277+
->method('getHost')
278+
->willReturn($host);
279+
280+
if (\is_callable([$requestStack, 'getMainRequest'])) {
281+
$requestStack->expects(self::once())
282+
->method('getMainRequest')
283+
->willReturn($request);
284+
} else {
285+
$requestStack->expects(self::once())
286+
->method('getMasterRequest')
287+
->willReturn($request);
288+
}
289+
290+
$response = $this->createMock(Response::class);
291+
$response->expects(self::once())
292+
->method('isSuccess')
293+
->willReturn(true);
294+
$response->expects(self::once())
295+
->method('getHostname')
296+
->willReturn($host);
297+
298+
$reCaptcha->expects(self::once())
299+
->method('verify')
300+
->with($recaptchaAnswer, $clientIp)
301+
->willReturn($response);
302+
303+
$validator = new IsTrueValidator(true, $reCaptcha, $requestStack, true, $authorizationChecker, $trustedRoles);
304+
$validator->initialize($context);
305+
$validator->validate('', $constraint);
306+
}
307+
308+
}

0 commit comments

Comments
 (0)