Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test Captcha #193

Open
engharb opened this issue Sep 17, 2018 · 2 comments
Open

How to test Captcha #193

engharb opened this issue Sep 17, 2018 · 2 comments

Comments

@engharb
Copy link

engharb commented Sep 17, 2018

Hi,

I am running PhpUnit test but I do not know how to set the captcha value in the form before submitting the form?

May I can read the Symfony session but later the session will no longer valid/stored.

Any idea?

@Seb33300
Copy link

Seb33300 commented Jun 4, 2019

You should disable it in test environment.

# config/packages/test/gregwar_captcha.yaml
gregwar_captcha:
    disabled: true

@irozgar
Copy link

irozgar commented Aug 19, 2020

Wouldn't be better to test getting the phrase from the session in the container? This way the integration of the captcha with your forms could be tested too.

This is how I do it in a contact form:

$crawler = $client->request(Request::METHOD_GET, '/contact');

self::assertStatusCodeEquals(Response::HTTP_OK, $client);

$session = static::$kernel->getContainer()->get('session');
$captcha = $session->get('_captcha_captcha');

$form = $crawler->selectButton(self::SEND_MESSAGE_LABEL)->form();
$form->get('contact_request[name]')->setValue('John Smith');
$form->get('contact_request[email]')->setValue('[email protected]');
$form->get('contact_request[subject]')->setValue('A test email');
$form->get('contact_request[body]')->setValue('This email is sent from a test');
$form->get('contact_request[captcha]')->setValue($captcha['phrase']);
$client->submit($form);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants