Skip to content

Generated tests fail to preg_match the token #333

@jeremyVignelles

Description

@jeremyVignelles

Version: 1.22.0

I used the maker bundle to generate tests for the password reset feature.
I adapted the code so that the mail is in french, but the generated code fails to properly get the token

Test method excerpt:

        // Ensure the reset password email was sent
        // Use either assertQueuedEmailCount() || assertEmailCount() depending on your mailer setup
        self::assertQueuedEmailCount(1);
        // self::assertEmailCount(1);

...

        // Test the link sent in the email is valid
        $email = $messages[0]->toString();
        preg_match('#(/reset-password/reset/[a-zA-Z0-9]+)#', $email, $resetLink);// Matches the first characters of the link, see below

        $this->client->request(\Symfony\Component\HttpFoundation\Request::METHOD_GET, $resetLink[1]);

        self::assertResponseRedirects('/reset-password/reset');

        $this->client->followRedirect();

        // Test we can set a new password
        $this->client->submitForm('Changer votre mot de passe', [// Fails here : can't find the button... $this->client is still on a redirect page
            'change_password_form[plainPassword][first]' => 'newStrongPassword',
            'change_password_form[plainPassword][second]' => 'newStrongPassword',
        ]);

$email value excerpt:

...
Bonjour,=20
    Pour r=C3=A9initialiser votre mot de passe, veuillez clique=
r sur le lien suivant

http://localhost/reset-password/reset/qsfTOThd0P=
F4p0nwzCMVl53pkFjj8yq4r0RczL5P

Ce lien va expirer dans 1 heure.

A=
 bient=C3=B4t !
...
<p>Bonjour, <br />
    Pour r=C3=A9initialiser votre mot de passe, veuill=
ez cliquer sur le lien suivant</p>

<a href=3D"http://localhost/reset-p=
assword/reset/qsfTOThd0PF4p0nwzCMVl53pkFjj8yq4r0RczL5P">http://localhost/re=
set-password/reset/qsfTOThd0PF4p0nwzCMVl5"

Notice how the link is split in two lines.
In that case, only qsfTOThd0P matches as the token, which throws an InvalidResetPasswordTokenException because the length is not 40 in ResetPasswordHelper::validateTokenAndFetchUser

Workaround:

instead of getting toString() of a templated message, we can get the raw text body, that doesn't have the formatting issue.

        /** @var Email $email */
        $email = $messages[0];
        preg_match('#(/reset-password/reset/[a-zA-Z0-9]+)#', $email->getTextBody(), $resetLink);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions