1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Christian Gripp <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core23\AntiSpamBundle\Tests\Form\Extension; |
11
|
|
|
|
12
|
|
|
use Core23\AntiSpamBundle\Form\EventListener\AntiSpamHoneypotListener; |
13
|
|
|
use Core23\AntiSpamBundle\Form\Extension\HoneypotFormExtension; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Prophecy\Argument; |
16
|
|
|
use RuntimeException; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
19
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
20
|
|
|
use Symfony\Component\Form\FormConfigInterface; |
21
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
22
|
|
|
use Symfony\Component\Form\FormInterface; |
23
|
|
|
use Symfony\Component\Form\FormView; |
24
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
25
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
26
|
|
|
|
27
|
|
|
final class HoneypotFormExtensionTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
private $translator; |
30
|
|
|
|
31
|
|
|
protected function setUp(): void |
32
|
|
|
{ |
33
|
|
|
$this->translator = $this->prophesize(TranslatorInterface::class); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testBuildForm(): void |
37
|
|
|
{ |
38
|
|
|
$factory = $this->prophesize(FormFactoryInterface::class); |
39
|
|
|
|
40
|
|
|
$builder = $this->prophesize(FormBuilderInterface::class); |
41
|
|
|
$builder->getFormFactory() |
42
|
|
|
->willReturn($factory) |
43
|
|
|
; |
44
|
|
|
$builder->setAttribute('antispam_honeypot_factory', $factory->reveal()) |
45
|
|
|
->willReturn($builder) |
46
|
|
|
->shouldBeCalled() |
47
|
|
|
; |
48
|
|
|
$builder->addEventSubscriber(Argument::type(AntiSpamHoneypotListener::class)) |
49
|
|
|
->willReturn($builder) |
50
|
|
|
->shouldBeCalled() |
51
|
|
|
; |
52
|
|
|
|
53
|
|
|
$extension = new HoneypotFormExtension( |
54
|
|
|
$this->translator->reveal(), |
55
|
|
|
[] |
56
|
|
|
); |
57
|
|
|
$extension->buildForm($builder->reveal(), [ |
58
|
|
|
'antispam_honeypot' => true, |
59
|
|
|
'antispam_honeypot_class' => 'spamclass', |
60
|
|
|
'antispam_honeypot_field' => 'hidden-field', |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testBuildFormWithDisabledAntispam(): void |
65
|
|
|
{ |
66
|
|
|
$builder = $this->prophesize(FormBuilderInterface::class); |
67
|
|
|
|
68
|
|
|
$extension = new HoneypotFormExtension( |
69
|
|
|
$this->translator->reveal(), |
70
|
|
|
[] |
71
|
|
|
); |
72
|
|
|
$extension->buildForm($builder->reveal(), [ |
73
|
|
|
'antispam_honeypot' => false, |
74
|
|
|
'antispam_honeypot_class' => 'spamclass', |
75
|
|
|
'antispam_honeypot_field' => 'hidden-field', |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
$builder->addEventSubscriber(Argument::type(AntiSpamHoneypotListener::class)) |
79
|
|
|
->shouldNotHaveBeenCalled() |
80
|
|
|
; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testFinishView(): void |
84
|
|
|
{ |
85
|
|
|
$parenView = $this->prophesize(FormView::class); |
86
|
|
|
|
87
|
|
|
$view = $this->prophesize(FormView::class); |
88
|
|
|
|
89
|
|
|
$parentForm = $this->prophesize(FormInterface::class); |
90
|
|
|
$parentForm->createView($view) |
91
|
|
|
->willReturn($parenView) |
92
|
|
|
; |
93
|
|
|
|
94
|
|
|
$formFactory = $this->prophesize(FormFactoryInterface::class); |
95
|
|
|
$formFactory |
96
|
|
|
->createNamed('hidden-field', TextType::class, null, [ |
97
|
|
|
'mapped' => false, |
98
|
|
|
'label' => false, |
99
|
|
|
'required' => false, |
100
|
|
|
'attr' => [ |
101
|
|
|
'class' => 'spamclass', |
102
|
|
|
], |
103
|
|
|
]) |
104
|
|
|
->willReturn($parentForm) |
105
|
|
|
; |
106
|
|
|
|
107
|
|
|
$config = $this->prophesize(FormConfigInterface::class); |
108
|
|
|
$config->getAttribute('antispam_honeypot_factory') |
109
|
|
|
->willReturn($formFactory) |
110
|
|
|
; |
111
|
|
|
|
112
|
|
|
$form = $this->prophesize(FormInterface::class); |
113
|
|
|
$form->has('hidden-field') |
114
|
|
|
->willReturn(false) |
115
|
|
|
; |
116
|
|
|
$form->getConfig() |
117
|
|
|
->willReturn($config) |
118
|
|
|
; |
119
|
|
|
|
120
|
|
|
$extension = new HoneypotFormExtension( |
121
|
|
|
$this->translator->reveal(), |
122
|
|
|
[] |
123
|
|
|
); |
124
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
125
|
|
|
'compound' => true, |
126
|
|
|
'antispam_honeypot' => true, |
127
|
|
|
'antispam_honeypot_class' => 'spamclass', |
128
|
|
|
'antispam_honeypot_field' => 'hidden-field', |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
static::assertSame($parenView->reveal(), $view->children['hidden-field']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testFinishWithEmptyClass(): void |
135
|
|
|
{ |
136
|
|
|
$parenView = $this->prophesize(FormView::class); |
137
|
|
|
|
138
|
|
|
$view = $this->prophesize(FormView::class); |
139
|
|
|
|
140
|
|
|
$parentForm = $this->prophesize(FormInterface::class); |
141
|
|
|
$parentForm->createView($view) |
142
|
|
|
->willReturn($parenView) |
143
|
|
|
; |
144
|
|
|
|
145
|
|
|
$formFactory = $this->prophesize(FormFactoryInterface::class); |
146
|
|
|
$formFactory |
147
|
|
|
->createNamed('hidden-field', TextType::class, null, [ |
148
|
|
|
'mapped' => false, |
149
|
|
|
'label' => false, |
150
|
|
|
'required' => false, |
151
|
|
|
'attr' => [ |
152
|
|
|
'style' => 'display:none', |
153
|
|
|
], |
154
|
|
|
]) |
155
|
|
|
->willReturn($parentForm) |
156
|
|
|
; |
157
|
|
|
|
158
|
|
|
$config = $this->prophesize(FormConfigInterface::class); |
159
|
|
|
$config->getAttribute('antispam_honeypot_factory') |
160
|
|
|
->willReturn($formFactory) |
161
|
|
|
; |
162
|
|
|
|
163
|
|
|
$form = $this->prophesize(FormInterface::class); |
164
|
|
|
$form->has('hidden-field') |
165
|
|
|
->willReturn(false) |
166
|
|
|
; |
167
|
|
|
$form->getConfig() |
168
|
|
|
->willReturn($config) |
169
|
|
|
; |
170
|
|
|
|
171
|
|
|
$extension = new HoneypotFormExtension( |
172
|
|
|
$this->translator->reveal(), |
173
|
|
|
[] |
174
|
|
|
); |
175
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
176
|
|
|
'compound' => true, |
177
|
|
|
'antispam_honeypot' => true, |
178
|
|
|
'antispam_honeypot_field' => 'hidden-field', |
179
|
|
|
]); |
180
|
|
|
|
181
|
|
|
static::assertSame($parenView->reveal(), $view->children['hidden-field']); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testFinishWithExistingField(): void |
185
|
|
|
{ |
186
|
|
|
$this->expectException(RuntimeException::class); |
187
|
|
|
$this->expectExceptionMessage('Honeypot field "hidden-field" is already used.'); |
188
|
|
|
|
189
|
|
|
$view = $this->prophesize(FormView::class); |
190
|
|
|
|
191
|
|
|
$form = $this->prophesize(FormInterface::class); |
192
|
|
|
$form->has('hidden-field') |
193
|
|
|
->willReturn(true) |
194
|
|
|
; |
195
|
|
|
|
196
|
|
|
$extension = new HoneypotFormExtension( |
197
|
|
|
$this->translator->reveal(), |
198
|
|
|
[] |
199
|
|
|
); |
200
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
201
|
|
|
'compound' => true, |
202
|
|
|
'antispam_honeypot' => true, |
203
|
|
|
'antispam_honeypot_field' => 'hidden-field', |
204
|
|
|
]); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testFinishWithEmptyFormFactory(): void |
208
|
|
|
{ |
209
|
|
|
$this->expectException(RuntimeException::class); |
210
|
|
|
$this->expectExceptionMessage('Invalid form factory to create a honeyput.'); |
211
|
|
|
|
212
|
|
|
$view = $this->prophesize(FormView::class); |
213
|
|
|
|
214
|
|
|
$config = $this->prophesize(FormConfigInterface::class); |
215
|
|
|
$config->getAttribute('antispam_honeypot_factory') |
216
|
|
|
->willReturn(null) |
217
|
|
|
; |
218
|
|
|
|
219
|
|
|
$form = $this->prophesize(FormInterface::class); |
220
|
|
|
$form->has('hidden-field') |
221
|
|
|
->willReturn(false) |
222
|
|
|
; |
223
|
|
|
$form->getConfig() |
224
|
|
|
->willReturn($config) |
225
|
|
|
; |
226
|
|
|
|
227
|
|
|
$extension = new HoneypotFormExtension( |
228
|
|
|
$this->translator->reveal(), |
229
|
|
|
[] |
230
|
|
|
); |
231
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
232
|
|
|
'compound' => true, |
233
|
|
|
'antispam_honeypot' => true, |
234
|
|
|
'antispam_honeypot_field' => 'hidden-field', |
235
|
|
|
]); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function testFinishViewForChildForm(): void |
239
|
|
|
{ |
240
|
|
|
$view = $this->prophesize(FormView::class); |
241
|
|
|
$view->parent = $this->prophesize(FormView::class)->reveal(); |
242
|
|
|
$form = $this->prophesize(FormInterface::class); |
243
|
|
|
$form->getConfig() |
244
|
|
|
->shouldNotBeCalled() |
245
|
|
|
; |
246
|
|
|
|
247
|
|
|
$extension = new HoneypotFormExtension( |
248
|
|
|
$this->translator->reveal(), |
249
|
|
|
[] |
250
|
|
|
); |
251
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
252
|
|
|
'compound' => true, |
253
|
|
|
'antispam_honeypot' => true, |
254
|
|
|
'antispam_honeypot_class' => 'spamclass', |
255
|
|
|
'antispam_honeypot_field' => 'hidden-field', |
256
|
|
|
]); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function testFinishViewWithDisbaledAntispam(): void |
260
|
|
|
{ |
261
|
|
|
$view = $this->prophesize(FormView::class); |
262
|
|
|
$form = $this->prophesize(FormInterface::class); |
263
|
|
|
$form->getConfig() |
264
|
|
|
->shouldNotBeCalled() |
265
|
|
|
; |
266
|
|
|
|
267
|
|
|
$extension = new HoneypotFormExtension( |
268
|
|
|
$this->translator->reveal(), |
269
|
|
|
[] |
270
|
|
|
); |
271
|
|
|
$extension->finishView($view->reveal(), $form->reveal(), [ |
272
|
|
|
'compound' => true, |
273
|
|
|
'antispam_honeypot' => false, |
274
|
|
|
'antispam_honeypot_class' => 'spamclass', |
275
|
|
|
'antispam_honeypot_field' => 'hidden-field', |
276
|
|
|
]); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function testConfigureOptions(): void |
280
|
|
|
{ |
281
|
|
|
$resolver = new OptionsResolver(); |
282
|
|
|
|
283
|
|
|
$extension = new HoneypotFormExtension( |
284
|
|
|
$this->translator->reveal(), |
285
|
|
|
[ |
286
|
|
|
'global' => true, |
287
|
|
|
'class' => 'my-class', |
288
|
|
|
'field' => 'a-field', |
289
|
|
|
] |
290
|
|
|
); |
291
|
|
|
$extension->configureOptions($resolver); |
292
|
|
|
|
293
|
|
|
$result = $resolver->resolve(); |
294
|
|
|
|
295
|
|
|
static::assertTrue($result['antispam_honeypot']); |
296
|
|
|
static::assertSame('my-class', $result['antispam_honeypot_class']); |
297
|
|
|
static::assertSame('a-field', $result['antispam_honeypot_field']); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function testExtendedTypes(): void |
301
|
|
|
{ |
302
|
|
|
static::assertSame([FormType::class], HoneypotFormExtension::getExtendedTypes()); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function testExtendedType(): void |
306
|
|
|
{ |
307
|
|
|
$extension = new HoneypotFormExtension( |
308
|
|
|
$this->translator->reveal(), |
309
|
|
|
[] |
310
|
|
|
); |
311
|
|
|
|
312
|
|
|
static::assertSame(FormType::class, $extension->getExtendedType()); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|