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