| 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); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 34 | |||||||
| 35 | $template = $this->twig->createTemplate('{{ form_widget(form) }}'); |
||||||
|
0 ignored issues
–
show
The method
createTemplate() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 36 | //WHEN |
||||||
| 37 | $view = $template->render(['form' => $form->createView()]); |
||||||
| 38 | |||||||
| 39 | //THEN |
||||||
| 40 | self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view); |
||||||
|
0 ignored issues
–
show
The method
assertStringContainsString() does not exist on Karser\Recaptcha3Bundle\Tests\FunctionalTest.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 41 | self::assertStringContainsString('<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=key&onload=recaptchaCallback_form_captcha" async defer nonce=""></script>', $view); |
||||||
| 42 | self::assertStringContainsString('var recaptchaCallback_form_captcha', $view); |
||||||
| 43 | self::assertStringContainsString("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'); |
||||||
|
0 ignored issues
–
show
It seems like
$this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 51 | |||||||
| 52 | $template = $this->twig->createTemplate('{{ form_widget(form) }}'); |
||||||
| 53 | //WHEN |
||||||
| 54 | $view = $template->render(['form' => $form->createView()]); |
||||||
| 55 | |||||||
| 56 | //THEN |
||||||
| 57 | self::assertStringContainsString('<input type="hidden" id="form_capt-cha" name="form[capt-cha]" />', $view); |
||||||
| 58 | self::assertStringContainsString('<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=key&onload=recaptchaCallback_form_capt_cha" async defer nonce=""></script>', $view); |
||||||
| 59 | self::assertStringContainsString('var recaptchaCallback_form_capt_cha', $view); |
||||||
| 60 | self::assertStringContainsString("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); |
||||||
|
0 ignored issues
–
show
It seems like
$this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 69 | $template = $this->twig->createTemplate('{{ form_widget(form) }}'); |
||||||
| 70 | //WHEN |
||||||
| 71 | $view = $template->render(['form' => $form->createView()]); |
||||||
| 72 | |||||||
| 73 | //THEN |
||||||
| 74 | self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view); |
||||||
| 75 | self::assertStringNotContainsString('<script src="https://www.google.com/recaptcha/api.js?render=key"></script>', $view); |
||||||
|
0 ignored issues
–
show
The method
assertStringNotContainsString() does not exist on Karser\Recaptcha3Bundle\Tests\FunctionalTest. Did you maybe mean assertStringNotMatchesFormat()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 76 | self::assertStringNotContainsString("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); |
||||||
|
0 ignored issues
–
show
It seems like
$this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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 }}']); |
||||||
|
0 ignored issues
–
show
It seems like
$this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
It seems like
$this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
It seems like
$this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
It seems like
$this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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); |
||||||
|
0 ignored issues
–
show
It seems like
$this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 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 | public function testFormJavascriptNoncePresent_ifSet() |
||||||
| 185 | { |
||||||
| 186 | //GIVEN |
||||||
| 187 | $this->bootKernel('default.yml'); |
||||||
| 188 | $form = $this->createContactForm($this->formFactory, [], null, 'csp_nonce'); |
||||||
|
0 ignored issues
–
show
It seems like
$this->formFactory can also be of type null; however, parameter $formFactory of Karser\Recaptcha3Bundle\...st::createContactForm() does only seem to accept Symfony\Component\Form\FormFactoryInterface, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 189 | |||||||
| 190 | $template = $this->twig->createTemplate('{{ form_widget(form) }}'); |
||||||
| 191 | //WHEN |
||||||
| 192 | $view = $template->render(['form' => $form->createView()]); |
||||||
| 193 | |||||||
| 194 | //THEN |
||||||
| 195 | self::assertStringContainsString('<input type="hidden" id="form_captcha" name="form[captcha]" />', $view); |
||||||
| 196 | self::assertStringContainsString('<script type="text/javascript" nonce="csp_nonce">', $view); |
||||||
| 197 | self::assertStringContainsString('<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?render=key&onload=recaptchaCallback_form_captcha" async defer nonce="csp_nonce"></script>', $view); |
||||||
| 198 | self::assertStringContainsString('var recaptchaCallback_form_captcha', $view); |
||||||
| 199 | self::assertStringContainsString("document.getElementById('form_captcha').value = token;", $view); |
||||||
| 200 | } |
||||||
| 201 | |||||||
| 202 | private function assertFormHasCaptchaError(FormInterface $form, string $expectedMessage) |
||||||
| 203 | { |
||||||
| 204 | self::assertTrue($form->isSubmitted()); |
||||||
| 205 | self::assertFalse($form->isValid()); |
||||||
| 206 | self::assertSame($expectedMessage, $form->getErrors()[0]->getMessage()); |
||||||
|
0 ignored issues
–
show
The method
getMessage() does not exist on Symfony\Component\Form\FormErrorIterator.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 207 | } |
||||||
| 208 | |||||||
| 209 | private function bootKernel(string $config): ContainerInterface |
||||||
| 210 | { |
||||||
| 211 | $this->kernel->setConfigurationFilename(__DIR__ . '/fixtures/config/'.$config); |
||||||
| 212 | $this->kernel->boot(); |
||||||
| 213 | $container = $this->kernel->getContainer(); |
||||||
| 214 | $this->formFactory = $container->get('form.factory'); |
||||||
| 215 | $this->twig = $container->get('twig'); |
||||||
| 216 | |||||||
| 217 | return $container; |
||||||
| 218 | } |
||||||
| 219 | |||||||
| 220 | private function createContactForm(FormFactoryInterface $formFactory, array $constraintParams = [], ?string $captchaId = null, ?string $nonce = null) |
||||||
| 221 | { |
||||||
| 222 | return $formFactory->createBuilder(FormType::class) |
||||||
| 223 | ->add('name', TextType::class, [ |
||||||
| 224 | 'constraints' => [ |
||||||
| 225 | new NotBlank(), |
||||||
| 226 | ], |
||||||
| 227 | ]) |
||||||
| 228 | ->add($captchaId ?? 'captcha', Recaptcha3Type::class, [ |
||||||
| 229 | 'constraints' => new Recaptcha3($constraintParams), |
||||||
| 230 | 'script_nonce_csp' => $nonce |
||||||
| 231 | ]) |
||||||
| 232 | ->getForm(); |
||||||
| 233 | } |
||||||
| 234 | } |
||||||
| 235 |