From 225132cafde1245ed897670e4dd5045da1d6caba Mon Sep 17 00:00:00 2001 From: Anna Larch Date: Mon, 15 Jul 2024 13:53:34 +0200 Subject: [PATCH] fix(appointments): Set organiser email to correct langauge Signed-off-by: Anna Larch [skip ci] --- lib/Service/Appointments/MailService.php | 13 +++++++----- .../Service/Appointments/MailServiceTest.php | 20 +++++++++++++++---- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/Service/Appointments/MailService.php b/lib/Service/Appointments/MailService.php index 42a0ca16f4..c5bf7f9c94 100644 --- a/lib/Service/Appointments/MailService.php +++ b/lib/Service/Appointments/MailService.php @@ -31,6 +31,7 @@ use OCA\Calendar\Db\Booking; use OCA\Calendar\Exception\ServiceException; use OCP\Defaults; +use OCP\IConfig; use OCP\IDateTimeFormatter; use OCP\IL10N; use OCP\IURLGenerator; @@ -72,7 +73,8 @@ public function __construct(IMailer $mailer, IURLGenerator $urlGenerator, IDateTimeFormatter $dateFormatter, IFactory $lFactory, - IManager $notificationManager) { + IManager $notificationManager, + private IConfig $userConfig) { $this->userManager = $userManager; $this->mailer = $mailer; $this->l10n = $l10n; @@ -272,10 +274,9 @@ private function getSysEmail(): string { return \OCP\Util::getDefaultEmailAddress('appointments-noreply'); } - public function sendOrganizerBookingInformationEmail(Booking $booking, AppointmentConfig $config, string $calendar) { + public function sendOrganizerBookingInformationEmail(Booking $booking, AppointmentConfig $config, string $calendar): void { /** @var IUser $user */ $user = $this->userManager->get($config->getUserId()); - if ($user === null) { throw new ServiceException('Could not find organizer'); } @@ -295,12 +296,14 @@ public function sendOrganizerBookingInformationEmail(Booking $booking, Appointme $template = $this->mailer->createEMailTemplate('calendar.confirmOrganizer'); $template->addHeader(); + $lang = $this->userConfig->getUserValue($user->getUID(), 'core', 'lang', null); + $l10n = $this->lFactory->get('calendar', $lang); // Subject - $subject = $this->l10n->t('You have a new appointment booking "%s" from %s', [$config->getName(), $booking->getDisplayName()]); + $subject = $l10n->t('You have a new appointment booking "%s" from %s', [$config->getName(), $booking->getDisplayName()]); $template->setSubject($subject); // Heading - $summary = $this->l10n->t('Dear %s, %s (%s) booked an appointment with you.', [$user->getDisplayName(), $booking->getDisplayName(), $booking->getEmail()]); + $summary = $l10n->t('Dear %s, %s (%s) booked an appointment with you.', [$user->getDisplayName(), $booking->getDisplayName(), $booking->getEmail()]); $template->addHeading($summary); $template->addBodyListItem($booking->getDisplayName() . ' (' . $booking->getEmail() . ')', 'Appointment with:'); diff --git a/tests/php/unit/Service/Appointments/MailServiceTest.php b/tests/php/unit/Service/Appointments/MailServiceTest.php index 0f698d6841..8e4aa82d78 100644 --- a/tests/php/unit/Service/Appointments/MailServiceTest.php +++ b/tests/php/unit/Service/Appointments/MailServiceTest.php @@ -34,6 +34,7 @@ use OCA\Calendar\Service\Appointments\MailService; use OCP\Calendar\ICalendarQuery; use OCP\Defaults; +use OCP\IConfig; use OCP\IDateTimeFormatter; use OCP\IL10N; use OCP\IUser; @@ -77,6 +78,7 @@ class MailServiceTest extends TestCase { /** @var MailService */ private $mailService; + private IConfig|MockObject $userConfig; protected function setUp(): void { parent::setUp(); @@ -94,6 +96,7 @@ protected function setUp(): void { $this->dateFormatter = $this->createMock(IDateTimeFormatter::class); $this->lFactory = $this->createMock(IFactory::class); $this->notificationManager = $this->createMock(IManager::class); + $this->userConfig = $this->createMock(IConfig::class); $this->mailService = new MailService( $this->mailer, $this->userManager, @@ -104,6 +107,7 @@ protected function setUp(): void { $this->dateFormatter, $this->lFactory, $this->notificationManager, + $this->userConfig ); } @@ -528,6 +532,9 @@ public function testSendOrganizerBookingInformationEmail(): void { $this->mailer->expects(self::once()) ->method('createEmailTemplate') ->willReturn($emailTemplate); + $this->userConfig->expects(self::once()) + ->method('getUserValue') + ->willReturn('en'); $emailTemplate->expects(self::once()) ->method('addHeader'); $emailTemplate->expects(self::once()) @@ -547,8 +554,9 @@ public function testSendOrganizerBookingInformationEmail(): void { $this->lFactory->expects(self::once()) ->method('findGenericLanguage') ->willReturn('en'); - $this->lFactory->expects(self::once()) - ->method('get'); + $this->lFactory->expects(self::exactly(2)) + ->method('get') + ->willReturn($this->l10n); $this->dateFormatter->expects(self::once()) ->method('formatDateTimeRelativeDay') ->willReturn('Test'); @@ -583,6 +591,9 @@ public function testSendOrganizerBookingInformationEmailFailed(): void { $this->mailer->expects(self::once()) ->method('createMessage') ->willReturn($mailMessage); + $this->userConfig->expects(self::once()) + ->method('getUserValue') + ->willReturn('en'); $mailMessage->expects(self::once()) ->method('setFrom') ->willReturn($mailMessage); @@ -615,8 +626,9 @@ public function testSendOrganizerBookingInformationEmailFailed(): void { $this->lFactory->expects(self::once()) ->method('findGenericLanguage') ->willReturn('en'); - $this->lFactory->expects(self::once()) - ->method('get'); + $this->lFactory->expects(self::exactly(2)) + ->method('get') + ->willReturn($this->l10n); $this->dateFormatter->expects(self::once()) ->method('formatDateTimeRelativeDay') ->willReturn('Test');