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 src="https://www.google.com/recaptcha/api.js?render=key&onload=recaptchaCallback_form_captcha" async defer></script>', $view); |
42
|
|
|
self::assertContains("document.getElementById('form_captcha').value = token;", $view); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testFormJavascriptAbsent_ifDisabled() |
46
|
|
|
{ |
47
|
|
|
//GIVEN |
48
|
|
|
$this->bootKernel('disabled.yml'); |
49
|
|
|
|
50
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
51
|
|
|
$template = $this->twig->createTemplate('{{ form_widget(form) }}'); |
52
|
|
|
//WHEN |
53
|
|
|
$view = $template->render(['form' => $form->createView()]); |
54
|
|
|
|
55
|
|
|
//THEN |
56
|
|
|
self::assertContains('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view); |
57
|
|
|
self::assertNotContains('<script src="https://www.google.com/recaptcha/api.js?render=key"></script>', $view); |
58
|
|
|
self::assertNotContains("document.getElementById('form_captcha').value = token;", $view); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testFormValid_ifEnabled() |
62
|
|
|
{ |
63
|
|
|
//GIVEN |
64
|
|
|
$container = $this->bootKernel('default.yml'); |
65
|
|
|
|
66
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
67
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
68
|
|
|
$recaptchaMock->nextSuccess = true; |
69
|
|
|
|
70
|
|
|
//WHEN |
71
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
72
|
|
|
|
73
|
|
|
//THEN |
74
|
|
|
$form->submit(['name' => 'John', 'captcha' => 'token']); |
75
|
|
|
self::assertTrue($form->isSubmitted()); |
76
|
|
|
self::assertTrue($form->isValid()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testFormInvalid_ifCaptchaFails() |
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 = false; |
87
|
|
|
|
88
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
89
|
|
|
|
90
|
|
|
//WHEN |
91
|
|
|
$form->submit(['name' => 'John', 'captcha' => 'token']); |
92
|
|
|
//THEN |
93
|
|
|
$this->assertFormHasCaptchaError($form); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testFormInvalid_ifCaptchaEmpty() |
97
|
|
|
{ |
98
|
|
|
//GIVEN |
99
|
|
|
$container = $this->bootKernel('default.yml'); |
100
|
|
|
|
101
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
102
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
103
|
|
|
$recaptchaMock->nextSuccess = false; |
104
|
|
|
|
105
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
106
|
|
|
|
107
|
|
|
//WHEN |
108
|
|
|
$form->submit(['name' => 'John', 'captcha' => '']); |
109
|
|
|
//THEN |
110
|
|
|
$this->assertFormHasCaptchaError($form); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testFormInvalid_ifCaptchaNull() |
114
|
|
|
{ |
115
|
|
|
//GIVEN |
116
|
|
|
$container = $this->bootKernel('default.yml'); |
117
|
|
|
|
118
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
119
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
120
|
|
|
$recaptchaMock->nextSuccess = false; |
121
|
|
|
|
122
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
123
|
|
|
|
124
|
|
|
//WHEN |
125
|
|
|
$form->submit(['name' => 'John', 'captcha' => null]); |
126
|
|
|
//THEN |
127
|
|
|
$this->assertFormHasCaptchaError($form); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testFormInvalid_ifCaptchaUndefined() |
131
|
|
|
{ |
132
|
|
|
//GIVEN |
133
|
|
|
$container = $this->bootKernel('default.yml'); |
134
|
|
|
|
135
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
136
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
137
|
|
|
$recaptchaMock->nextSuccess = false; |
138
|
|
|
|
139
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
140
|
|
|
|
141
|
|
|
//WHEN |
142
|
|
|
$form->submit(['name' => 'John']); |
143
|
|
|
//THEN |
144
|
|
|
$this->assertFormHasCaptchaError($form); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testFormValid_ifCaptchaFails_butDisabled() |
148
|
|
|
{ |
149
|
|
|
//GIVEN |
150
|
|
|
$container = $this->bootKernel('disabled.yml'); |
151
|
|
|
|
152
|
|
|
/** @var RecaptchaMock $recaptchaMock */ |
153
|
|
|
$recaptchaMock = $container->get('karser_recaptcha3.google.recaptcha'); |
154
|
|
|
$recaptchaMock->nextSuccess = false; |
155
|
|
|
|
156
|
|
|
$form = $this->createContactForm($this->formFactory); |
|
|
|
|
157
|
|
|
|
158
|
|
|
//WHEN |
159
|
|
|
$form->submit(['name' => 'John', 'captcha' => 'token']); |
160
|
|
|
//THEN |
161
|
|
|
self::assertTrue($form->isSubmitted()); |
162
|
|
|
self::assertTrue($form->isValid()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private function assertFormHasCaptchaError(FormInterface $form) |
166
|
|
|
{ |
167
|
|
|
self::assertTrue($form->isSubmitted()); |
168
|
|
|
self::assertFalse($form->isValid()); |
169
|
|
|
self::assertSame('Your computer or network may be sending automated queries', |
170
|
|
|
$form->getErrors()[0]->getMessage()); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function bootKernel(string $config): ContainerInterface |
174
|
|
|
{ |
175
|
|
|
$this->kernel->setConfigurationFilename(__DIR__ . '/fixtures/config/'.$config); |
176
|
|
|
$this->kernel->boot(); |
177
|
|
|
$container = $this->kernel->getContainer(); |
178
|
|
|
$this->formFactory = $container->get('form.factory'); |
179
|
|
|
$this->twig = $container->get('twig'); |
180
|
|
|
|
181
|
|
|
return $container; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
private function createContactForm(FormFactoryInterface $formFactory) |
185
|
|
|
{ |
186
|
|
|
return $formFactory->createBuilder(FormType::class) |
187
|
|
|
->add('name', TextType::class, [ |
188
|
|
|
'constraints' => [ |
189
|
|
|
new NotBlank(), |
190
|
|
|
], |
191
|
|
|
]) |
192
|
|
|
->add('captcha', Recaptcha3Type::class, [ |
193
|
|
|
'constraints' => new Recaptcha3(), |
194
|
|
|
]) |
195
|
|
|
->getForm(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|