Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion api/src/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Entity\Profile;
use App\Entity\User;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
Expand All @@ -22,13 +23,19 @@ public function __construct(
private readonly Environment $twigEnironment,
private readonly string $frontendBaseUrl,
private readonly string $senderEmail,
private readonly string $senderName = ''
private readonly string $senderName,
private readonly Security $security,
) {}

public function sendInviteToCampMail(User $byUser, Camp $camp, string $key, string $emailToInvite): void {
/** @var User $originator */
$originator = $this->security->getUser();
$originatorEmail = $originator->getEmail();
$originatorName = $originator->getDisplayName();
$email = new TemplatedEmail()
->from(new Address($this->senderEmail, $this->senderName))
->to(new Address($emailToInvite))
->replyTo(new Address($originatorEmail, $originatorName))
->subject($this->translator->trans('inviteToCamp.subject', ['campTitle' => $camp->title], self::TRANSLATE_DOMAIN, $byUser->profile->language))
->htmlTemplate($this->getTemplate('emails/campCollaborationInvite.{language}.html.twig', $byUser))
->textTemplate($this->getTemplate('emails/campCollaborationInvite.{language}.text.twig', $byUser))
Expand Down
14 changes: 13 additions & 1 deletion api/tests/Service/MailServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Service\MailService;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
Expand All @@ -23,6 +24,7 @@ class MailServiceTest extends KernelTestCase {

private Camp $camp;
private User $user;
private Security $security;

private MailService $mailer;

Expand All @@ -33,7 +35,15 @@ protected function setUp(): void {
$translator = self::getContainer()->get(TranslatorInterface::class);
$twigEnvironment = self::getContainer()->get(Environment::class);

$this->mailer = new MailService($mailer, $translator, $twigEnvironment, 'frontend.example.com', 'sender@example.com', 'SenderName');
$this->security = $this->createMock(Security::class);
$profile = new Profile();
$profile->nickname = 'Linux';
$profile->email = 'sender@ecamp3.ch';
$user = new User();
$user->profile = $profile;
$this->security->method('getUser')->willReturn($user);

$this->mailer = new MailService($mailer, $translator, $twigEnvironment, 'frontend.example.com', 'sender@example.com', 'SenderName', $this->security);

$this->user = new User();
$profile = new Profile();
Expand All @@ -52,6 +62,7 @@ public function testSendInviteToCampMailDeChScout() {
self::assertEmailCount(1);
$mailerMessage = self::getMailerMessage(0);
self::assertEmailAddressContains($mailerMessage, 'To', self::INVITE_MAIL);
self::assertEmailAddressContains($mailerMessage, 'reply-to', 'sender@ecamp3.ch');
self::assertEmailHeaderSame($mailerMessage, 'subject', '[eCamp v3] Du wurdest ins Lager "some camp title" eingeladen');

self::assertEmailHtmlBodyContains($mailerMessage, $this->camp->title);
Expand All @@ -71,6 +82,7 @@ public function testSendInvitationMailDoesNotCrashForAllLanguages(string $langua
self::assertEmailCount(1);
$mailerMessage = self::getMailerMessage(0);
self::assertEmailAddressContains($mailerMessage, 'To', self::INVITE_MAIL);
self::assertEmailAddressContains($mailerMessage, 'reply-to', 'sender@ecamp3.ch');

self::assertEmailHtmlBodyContains($mailerMessage, $this->camp->title);
self::assertEmailHtmlBodyContains($mailerMessage, $this->user->getDisplayName());
Expand Down
Loading