1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Karser\Recaptcha3Bundle\Tests; |
4
|
|
|
|
5
|
|
|
use Karser\Recaptcha3Bundle\Form\Recaptcha3Type; |
6
|
|
|
use Karser\Recaptcha3Bundle\Tests\fixtures\RecaptchaMock; |
7
|
|
|
use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
12
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
13
|
|
|
use Symfony\Component\Form\FormInterface; |
14
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
15
|
|
|
|
16
|
|
|
class FunctionalTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** @var TestKernel */ |
19
|
|
|
private $kernel; |
20
|
|
|
|
21
|
|
|
private $formFactory; |
22
|
|
|
private $twig; |
23
|
|
|
|
24
|
|
|
public function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$this->kernel = new TestKernel(uniqid(), false); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testFormJavascriptPresent_ifEnabled() |
30
|
|
|
{ |
31
|
|
|
//GIVEN |
32
|
|
|
$this->bootKernel('default.yml'); |
33
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
34
|
|
|
|
35
|
|
|
$template = $this->twig->createTemplate('{{ form_widget(form) }}'); |
|
|
|
|
36
|
|
|
//WHEN |
37
|
|
|
$view = $template->render(['form' => $form->createView()]); |
38
|
|
|
|
39
|
|
|
//THEN |
40
|
|
|
self::assertContains('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view); |
41
|
|
|
self::assertContains('<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=key&onload=recaptchaCallback_form_captcha" async defer></script>', $view); |
42
|
|
|
self::assertContains('var recaptchaCallback_form_captcha', $view); |
43
|
|
|
self::assertContains("document.getElementById('form_captcha').value = token;", $view); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testHyphenConvertedToUnderscore() |
47
|
|
|
{ |
48
|
|
|
//GIVEN |
49
|
|
|
$this->bootKernel('default.yml'); |
50
|
|
|
$form = $this->createContactForm($this->formFactory, [], 'capt-cha'); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$template = $this->twig->createTemplate('{{ form_widget(form) }}'); |
53
|
|
|
//WHEN |
54
|
|
|
$view = $template->render(['form' => $form->createView()]); |
55
|
|
|
|
56
|
|
|
//THEN |
57
|
|
|
self::assertContains('<input type="hidden" id="form_capt-cha" name="form[capt-cha]" />', $view); |
58
|
|
|
self::assertContains('<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=key&onload=recaptchaCallback_form_capt_cha" async defer></script>', $view); |
59
|
|
|
self::assertContains('var recaptchaCallback_form_capt_cha', $view); |
60
|
|
|
self::assertContains("document.getElementById('form_capt-cha').value = token;", $view); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testFormJavascriptAbsent_ifDisabled() |
64
|
|
|
{ |
65
|
|
|
//GIVEN |
66
|
|
|
$this->bootKernel('disabled.yml'); |
67
|
|
|
|
68
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
69
|
|
|
$template = $this->twig->createTemplate('{{ form_widget(form) }}'); |
70
|
|
|
//WHEN |
71
|
|
|
$view = $template->render(['form' => $form->createView()]); |
72
|
|
|
|
73
|
|
|
//THEN |
74
|
|
|
self::assertContains('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view); |
75
|
|
|
self::assertNotContains('<script src="https://www.google.com/recaptcha/api.js?render=key"></script>', $view); |
76
|
|
|
self::assertNotContains("document.getElementById('form_captcha').value = token;", $view); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testFormValid_ifEnabled() |
80
|
|
|
{ |
81
|
|
|
//GIVEN |
82
|
|
|
$container = $this->bootKernel('default.yml'); |
83
|
|
|
|
84
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
85
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
86
|
|
|
$recaptchaMock->nextSuccess = true; |
87
|
|
|
|
88
|
|
|
//WHEN |
89
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
90
|
|
|
|
91
|
|
|
//THEN |
92
|
|
|
$form->submit(['name' => 'John', 'captcha' => 'token']); |
93
|
|
|
self::assertTrue($form->isSubmitted()); |
94
|
|
|
self::assertTrue($form->isValid()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testFormInvalid_ifCaptchaFails() |
98
|
|
|
{ |
99
|
|
|
//GIVEN |
100
|
|
|
$container = $this->bootKernel('default.yml'); |
101
|
|
|
|
102
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
103
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
104
|
|
|
$recaptchaMock->nextSuccess = false; |
105
|
|
|
$recaptchaMock->nextErrorCodes = ['test1', 'test2']; |
106
|
|
|
|
107
|
|
|
$form = $this->createContactForm($this->formFactory, ['message' => 'Error: {{ errorCodes }}']); |
|
|
|
|
108
|
|
|
|
109
|
|
|
//WHEN |
110
|
|
|
$form->submit(['name' => 'John', 'captcha' => 'token']); |
111
|
|
|
//THEN |
112
|
|
|
$this->assertFormHasCaptchaError($form, 'Error: "test1; test2"'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testFormInvalid_ifCaptchaEmpty() |
116
|
|
|
{ |
117
|
|
|
//GIVEN |
118
|
|
|
$container = $this->bootKernel('default.yml'); |
119
|
|
|
|
120
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
121
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
122
|
|
|
$recaptchaMock->nextSuccess = false; |
123
|
|
|
|
124
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
125
|
|
|
|
126
|
|
|
//WHEN |
127
|
|
|
$form->submit(['name' => 'John', 'captcha' => '']); |
128
|
|
|
//THEN |
129
|
|
|
$this->assertFormHasCaptchaError($form, 'The captcha value is missing'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testFormInvalid_ifCaptchaNull() |
133
|
|
|
{ |
134
|
|
|
//GIVEN |
135
|
|
|
$container = $this->bootKernel('default.yml'); |
136
|
|
|
|
137
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
138
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
139
|
|
|
$recaptchaMock->nextSuccess = false; |
140
|
|
|
|
141
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
142
|
|
|
|
143
|
|
|
//WHEN |
144
|
|
|
$form->submit(['name' => 'John', 'captcha' => null]); |
145
|
|
|
//THEN |
146
|
|
|
$this->assertFormHasCaptchaError($form, 'The captcha value is missing'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testFormInvalid_ifCaptchaUndefined() |
150
|
|
|
{ |
151
|
|
|
//GIVEN |
152
|
|
|
$container = $this->bootKernel('default.yml'); |
153
|
|
|
|
154
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
155
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
156
|
|
|
$recaptchaMock->nextSuccess = false; |
157
|
|
|
|
158
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
159
|
|
|
|
160
|
|
|
//WHEN |
161
|
|
|
$form->submit(['name' => 'John']); |
162
|
|
|
//THEN |
163
|
|
|
$this->assertFormHasCaptchaError($form, 'The captcha value is missing'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testFormValid_ifCaptchaFails_butDisabled() |
167
|
|
|
{ |
168
|
|
|
//GIVEN |
169
|
|
|
$container = $this->bootKernel('disabled.yml'); |
170
|
|
|
|
171
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
172
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
173
|
|
|
$recaptchaMock->nextSuccess = false; |
174
|
|
|
|
175
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
176
|
|
|
|
177
|
|
|
//WHEN |
178
|
|
|
$form->submit(['name' => 'John', 'captcha' => 'token']); |
179
|
|
|
//THEN |
180
|
|
|
self::assertTrue($form->isSubmitted()); |
181
|
|
|
self::assertTrue($form->isValid()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
private function assertFormHasCaptchaError(FormInterface $form, string $expectedMessage) |
185
|
|
|
{ |
186
|
|
|
self::assertTrue($form->isSubmitted()); |
187
|
|
|
self::assertFalse($form->isValid()); |
188
|
|
|
self::assertSame($expectedMessage, $form->getErrors()[0]->getMessage()); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
private function bootKernel(string $config): ContainerInterface |
192
|
|
|
{ |
193
|
|
|
$this->kernel->setConfigurationFilename(__DIR__ . '/fixtures/config/'.$config); |
194
|
|
|
$this->kernel->boot(); |
195
|
|
|
$container = $this->kernel->getContainer(); |
196
|
|
|
$this->formFactory = $container->get('form.factory'); |
197
|
|
|
$this->twig = $container->get('twig'); |
198
|
|
|
|
199
|
|
|
return $container; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
private function createContactForm(FormFactoryInterface $formFactory, array $constraintParams = [], ?string $captchaId = null) |
203
|
|
|
{ |
204
|
|
|
return $formFactory->createBuilder(FormType::class) |
205
|
|
|
->add('name', TextType::class, [ |
206
|
|
|
'constraints' => [ |
207
|
|
|
new NotBlank(), |
208
|
|
|
], |
209
|
|
|
]) |
210
|
|
|
->add($captchaId ?? 'captcha', Recaptcha3Type::class, [ |
211
|
|
|
'constraints' => new Recaptcha3($constraintParams), |
212
|
|
|
]) |
213
|
|
|
->getForm(); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|