Skip to content

Commit

Permalink
Merge pull request #6157 from nextcloud/fix/6156/appointments-confirm…
Browse files Browse the repository at this point in the history
…ation-language

fix(appointments): Set organiser email to correct langauge
  • Loading branch information
miaulalala authored Jul 15, 2024
2 parents bf56092 + 7893c95 commit d526f7f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
23 changes: 13 additions & 10 deletions lib/Service/Appointments/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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;
Expand Down Expand Up @@ -53,7 +54,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;
Expand Down Expand Up @@ -212,7 +214,7 @@ private function addBulletList(IEMailTemplate $template,
IL10N $l10n,
Booking $booking,
AppointmentConfig $config,
bool $recipient):void {
bool $recipient): void {
$template->addBodyListItem($booking->getDisplayName(), $l10n->t('Appointment for:'));

// determain timezone depending on who is getting the message (Requestee/Requester)
Expand Down Expand Up @@ -259,10 +261,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');
}
Expand All @@ -282,21 +283,23 @@ 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() . ')', $this->l10n->t('Appointment with:'));
$template->addBodyListItem($booking->getDisplayName() . ' (' . $booking->getEmail() . ')', $l10n->t('Appointment with:'));
if (!empty($config->getDescription())) {
$template->addBodyListItem($config->getDescription(), $this->l10n->t('Description:'));
$template->addBodyListItem($config->getDescription(), $l10n->t('Description:'));
}

// Create Booking overview
$this->addBulletList($template, $this->l10n, $booking, $config, true);
$this->addBulletList($template, $l10n, $booking, $config, true);
$template->addFooter();

$attachment = $this->mailer->createAttachment($calendar, 'appointment.ics', 'text/calendar');
Expand All @@ -319,7 +322,7 @@ public function sendOrganizerBookingInformationEmail(Booking $booking, Appointme
}
}

public function sendOrganizerBookingInformationNotification(Booking $booking, AppointmentConfig $config) {
public function sendOrganizerBookingInformationNotification(Booking $booking, AppointmentConfig $config): void {
$tzid = $config->getAvailabilityAsArray()['timezoneId']; // extract time zone from appointment configuration
$dtstart = new \DateTime("now", new \DateTimeZone($booking->getTimezone())); // generate DateTime with booking time zone
$dtstart->setTimestamp($booking->getStart()); // set booking time stamp
Expand Down
28 changes: 22 additions & 6 deletions tests/php/unit/Service/Appointments/MailServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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;
Expand Down Expand Up @@ -58,6 +59,7 @@ class MailServiceTest extends TestCase {

/** @var MailService */
private $mailService;
private IConfig|MockObject $userConfig;

protected function setUp(): void {
parent::setUp();
Expand All @@ -75,6 +77,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,
Expand All @@ -85,6 +88,7 @@ protected function setUp(): void {
$this->dateFormatter,
$this->lFactory,
$this->notificationManager,
$this->userConfig
);
}

Expand Down Expand Up @@ -537,6 +541,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())
Expand All @@ -556,8 +563,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');
Expand Down Expand Up @@ -607,6 +615,9 @@ public function testSendOrganizerBookingInformationEmailDifferentTZ(): 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);
Expand Down Expand Up @@ -639,8 +650,9 @@ public function testSendOrganizerBookingInformationEmailDifferentTZ(): 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')
->with(self::anything(), self::anything(), self::anything(), new \DateTimeZone('Europe/Berlin'))
Expand Down Expand Up @@ -690,6 +702,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);
Expand Down Expand Up @@ -722,8 +737,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');
Expand Down

0 comments on commit d526f7f

Please sign in to comment.