[TwigBridge] Throw an exception when one uses email as a context variable in a TemplatedEmail
This commit is contained in:
parent
610a4e978f
commit
0cc705bf7d
@ -13,6 +13,7 @@ namespace Symfony\Bridge\Twig\Mime;
|
|||||||
|
|
||||||
use League\HTMLToMarkdown\HtmlConverter;
|
use League\HTMLToMarkdown\HtmlConverter;
|
||||||
use Symfony\Component\Mime\BodyRendererInterface;
|
use Symfony\Component\Mime\BodyRendererInterface;
|
||||||
|
use Symfony\Component\Mime\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\Mime\Message;
|
use Symfony\Component\Mime\Message;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
|
|
||||||
@ -44,7 +45,12 @@ final class BodyRenderer implements BodyRendererInterface
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$vars = array_merge($this->context, $message->getContext(), [
|
$messageContext = $message->getContext();
|
||||||
|
if (isset($messageContext['email'])) {
|
||||||
|
throw new InvalidArgumentException(sprintf('A "%s" context cannot have an "email" entry as this is a reserved variable.', TemplatedEmail::class));
|
||||||
|
}
|
||||||
|
|
||||||
|
$vars = array_merge($this->context, $messageContext, [
|
||||||
'email' => new WrappedTemplatedEmail($this->twig, $message),
|
'email' => new WrappedTemplatedEmail($this->twig, $message),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -14,11 +14,12 @@ namespace Symfony\Bridge\Twig\Tests\Mime;
|
|||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Symfony\Bridge\Twig\Mime\BodyRenderer;
|
use Symfony\Bridge\Twig\Mime\BodyRenderer;
|
||||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||||
|
use Symfony\Component\Mime\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
|
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
|
||||||
use Twig\Environment;
|
use Twig\Environment;
|
||||||
use Twig\Loader\ArrayLoader;
|
use Twig\Loader\ArrayLoader;
|
||||||
|
|
||||||
class RendererTest extends TestCase
|
class BodyRendererTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testRenderTextOnly(): void
|
public function testRenderTextOnly(): void
|
||||||
{
|
{
|
||||||
@ -54,7 +55,13 @@ class RendererTest extends TestCase
|
|||||||
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
|
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private function prepareEmail(?string $text, ?string $html): TemplatedEmail
|
public function testRenderWithContextReservedEmailEntry(): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidArgumentException::class);
|
||||||
|
$this->prepareEmail('Text', '', ['email' => 'reserved!']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function prepareEmail(?string $text, ?string $html, array $context = []): TemplatedEmail
|
||||||
{
|
{
|
||||||
$twig = new Environment(new ArrayLoader([
|
$twig = new Environment(new ArrayLoader([
|
||||||
'text' => $text,
|
'text' => $text,
|
||||||
@ -63,7 +70,11 @@ class RendererTest extends TestCase
|
|||||||
'image.jpg' => 'Some image data',
|
'image.jpg' => 'Some image data',
|
||||||
]));
|
]));
|
||||||
$renderer = new BodyRenderer($twig);
|
$renderer = new BodyRenderer($twig);
|
||||||
$email = (new TemplatedEmail())->to('fabien@symfony.com')->from('helene@symfony.com');
|
$email = (new TemplatedEmail())
|
||||||
|
->to('fabien@symfony.com')
|
||||||
|
->from('helene@symfony.com')
|
||||||
|
->context($context)
|
||||||
|
;
|
||||||
if (null !== $text) {
|
if (null !== $text) {
|
||||||
$email->textTemplate('text');
|
$email->textTemplate('text');
|
||||||
}
|
}
|
Reference in New Issue
Block a user