Skip to content

Commit

Permalink
bug #53663 [TwigBridge] separate child and parent context in Notifica…
Browse files Browse the repository at this point in the history
…tionEmail on writes (xabbuh)

This PR was merged into the 5.4 branch.

Discussion
----------

[TwigBridge] separate child and parent context in NotificationEmail on writes

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #53655
| License       | MIT

Commits
-------

78bbfb0 separate child and parent context in NotificationEmail on writes
  • Loading branch information
nicolas-grekas committed Jan 29, 2024
2 parents 46694f6 + 78bbfb0 commit 4a124ac
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Symfony/Bridge/Twig/Mime/NotificationEmail.php
Expand Up @@ -178,6 +178,23 @@ public function getHtmlTemplate(): ?string
return '@email/'.$this->theme.'/notification/body.html.twig';
}

public function context(array $context)
{
$parentContext = [];

foreach ($context as $key => $value) {
if (\array_key_exists($key, $this->context)) {
$this->context[$key] = $value;
} else {
$parentContext[$key] = $value;
}
}

parent::context($parentContext);

return $this;
}

public function getContext(): array
{
return array_merge($this->context, parent::getContext());
Expand Down
50 changes: 50 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Mime/NotificationEmailTest.php
Expand Up @@ -128,4 +128,54 @@ public function testPublicMailSubject()
$headers = $email->getPreparedHeaders();
$this->assertSame('Foo', $headers->get('Subject')->getValue());
}

public function testContext()
{
$email = new NotificationEmail();
$email->context(['some' => 'context']);

$this->assertSame([
'importance' => NotificationEmail::IMPORTANCE_LOW,
'content' => '',
'exception' => false,
'action_text' => null,
'action_url' => null,
'markdown' => false,
'raw' => false,
'footer_text' => 'Notification e-mail sent by Symfony',
'some' => 'context',
], $email->getContext());

$context = $email->getContext();
$context['foo'] = 'bar';
$email->context($context);

$this->assertSame([
'importance' => NotificationEmail::IMPORTANCE_LOW,
'content' => '',
'exception' => false,
'action_text' => null,
'action_url' => null,
'markdown' => false,
'raw' => false,
'footer_text' => 'Notification e-mail sent by Symfony',
'some' => 'context',
'foo' => 'bar',
], $email->getContext());

$email->action('Action Text', 'Action URL');

$this->assertSame([
'importance' => NotificationEmail::IMPORTANCE_LOW,
'content' => '',
'exception' => false,
'action_text' => 'Action Text',
'action_url' => 'Action URL',
'markdown' => false,
'raw' => false,
'footer_text' => 'Notification e-mail sent by Symfony',
'some' => 'context',
'foo' => 'bar',
], $email->getContext());
}
}

0 comments on commit 4a124ac

Please sign in to comment.