8000 [FrameworkBundle/Mime] Assert email (header) subject which contains non-ascii characters · Issue #35062 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[FrameworkBundle/Mime] Assert email (header) subject which contains non-ascii characters #35062
Closed
@evertharmeling

Description

@evertharmeling

Symfony version(s) affected: 4.4 and 5.0

Description
When using the assertEmailHeaderSame() function on the subject header when it contains non-ascii characters results in a false assertion as the value is decoded.
Introduced in 4.4 with the FrameworkBundle see \Symfony\Bundle\FrameworkBundle\Test\MailerAssertionsTrait (and blogpost).

How to reproduce

$mailer->send(
    (new Email())
        ->to('fabien@symfony.com')
        ->from('fabien@symfony.com')
        ->subject('Foó')
);
        
...

static::assertEmailHeaderSame($email, 'subject', 'Foó');

Currently results in:

Failed asserting that the Email has header "subject" with value "Foó" (value is =?utf-8?Q?Fo=C3=B3?=).

Possible Solution
1) Change the \Symfony\Component\Mime\Test\Constraint\EmailHeaderSame::matches function to use getValue() on the HeaderInterface instead of the getBodyAsString().
Can't estimate the implications of that change.
2) Change the function assertEmailHeaderSame in \Symfony\Bundle\FrameworkBundle\Test\MailerAssertionsTrait to

public static function assertEmailHeaderSame(RawMessage $email, string $headerName, string $expectedValue, string $message = '', bool $rawValue = false): void

And pass the $rawValue to the constructor of the \Symfony\Component\Mime\Test\Constraint\EmailHeaderSame and in the matches function use

$header = $message->getHeaders()->get($this->headerName);

if ($this->rawValue() {
    return $this->expectedValue === $header->getValue();
}

return $this->expectedValue === $header->getBodyAsString();

3) Introduce a EmailSubjectSame constraint in the Mime component which is a simplified version of the \Symfony\Component\Mime\Test\Constraint\EmailHeaderSame constraint (not having to have the $headerName passed through). In the \Symfony\Bundle\FrameworkBundle\Test\MailerAssertionsTrait add a assertEmailSubjectSame fucntion, specifically using the EmailSubjectSame constraint.
This solution is also reasonable as the 'subject' is a bit of a specific header within the email, also given this scenario.

I would like give it a go in fixing, what would be the most desirable solution?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0