Skip to content

Commit

Permalink
feature #33323 [TwigBridge] Throw an exception when one uses email as…
Browse files Browse the repository at this point in the history
… a context variable in a TemplatedEmail (fabpot)

This PR was merged into the 4.4 branch.

Discussion
----------

[TwigBridge] Throw an exception when one uses email as a context variable in a TemplatedEmail

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | refs #33310
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

Commits
-------

0cc705b [TwigBridge] Throw an exception when one uses email as a context variable in a TemplatedEmail
  • Loading branch information
fabpot committed Aug 26, 2019
2 parents 610a4e9 + 0cc705b commit 6c9b87c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/Symfony/Bridge/Twig/Mime/BodyRenderer.php
Expand Up @@ -13,6 +13,7 @@

use League\HTMLToMarkdown\HtmlConverter;
use Symfony\Component\Mime\BodyRendererInterface;
use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Message;
use Twig\Environment;

Expand Down Expand Up @@ -44,7 +45,12 @@ public function render(Message $message): void
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),
]);

Expand Down
Expand Up @@ -14,11 +14,12 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Twig\Environment;
use Twig\Loader\ArrayLoader;

class RendererTest extends TestCase
class BodyRendererTest extends TestCase
{
public function testRenderTextOnly(): void
{
Expand Down Expand Up @@ -54,7 +55,13 @@ public function testRenderTextAndHtml(): void
$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([
'text' => $text,
Expand All @@ -63,7 +70,11 @@ private function prepareEmail(?string $text, ?string $html): TemplatedEmail
'image.jpg' => 'Some image data',
]));
$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) {
$email->textTemplate('text');
}
Expand Down

0 comments on commit 6c9b87c

Please sign in to comment.