This commit is contained in:
Sebastian Krupinski 2024-04-25 11:05:18 -04:00 committed by GitHub
commit 4a4c1c3bc1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 163 additions and 8 deletions

View File

@ -189,6 +189,10 @@ class AppointmentConfig extends Entity implements JsonSerializable {
return $this;
}
public function getAvailabilityAsArray(): array {
return json_decode($this->getAvailability(), true);
}
#[ReturnTypeWillChange]
public function jsonSerialize() {
return [

View File

@ -130,7 +130,7 @@ class MailService {
}
// Create Booking overview
$this->addBulletList($template, $this->l10n, $booking, $config);
$this->addBulletList($template, $this->l10n, $booking, $config, false);
$bodyText = $this->l10n->t('This confirmation link expires in %s hours.', [(BookingService::EXPIRY / 3600)]);
$template->addBodyText($bodyText);
@ -200,7 +200,7 @@ class MailService {
}
// Create Booking overview
$this->addBulletList($template, $this->l10n, $booking, $config);
$this->addBulletList($template, $this->l10n, $booking, $config, false);
$bodyText = $this->l10n->t('If you wish to cancel the appointment after all, please contact your organizer by replying to this email or by visiting their profile page.');
$template->addBodyText($bodyText);
@ -230,15 +230,21 @@ class MailService {
private function addBulletList(IEMailTemplate $template,
IL10N $l10n,
Booking $booking,
AppointmentConfig $config):void {
AppointmentConfig $config,
bool $recipient):void {
$template->addBodyListItem($booking->getDisplayName(), $l10n->t('Appointment for:'));
// determain timezone depending on who is getting the message (Requestee/Requester)
$tzid = ($recipient) ? $config->getAvailabilityAsArray()['timezoneId'] : $booking->getTimezone();
$dtstart = new \DateTime("now", new \DateTimeZone($booking->getTimezone())); // generate DateTime with booking time zone
$dtstart->setTimestamp($booking->getStart()); // set booking time stamp
$l = $this->lFactory->findGenericLanguage();
$relativeDateTime = $this->dateFormatter->formatDateTimeRelativeDay(
$booking->getStart(),
$dtstart,
'long',
'short',
new \DateTimeZone($booking->getTimezone()),
new \DateTimeZone($tzid),
$this->lFactory->get('calendar', $l)
);
@ -309,7 +315,7 @@ class MailService {
}
// Create Booking overview
$this->addBulletList($template, $this->l10n, $booking, $config);
$this->addBulletList($template, $this->l10n, $booking, $config, true);
$template->addFooter();
$attachment = $this->mailer->createAttachment($calendar, 'appointment.ics', 'text/calendar');
@ -333,11 +339,16 @@ class MailService {
}
public function sendOrganizerBookingInformationNotification(Booking $booking, AppointmentConfig $config) {
$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
$relativeDateTime = $this->dateFormatter->formatDateTimeRelativeDay(
$booking->getStart(),
$dtstart,
'long',
'short',
new \DateTimeZone($booking->getTimezone()),
new \DateTimeZone($tzid),
$this->lFactory->get('calendar')
);

View File

@ -346,6 +346,20 @@ class MailServiceTest extends TestCase {
$config = new AppointmentConfig();
$config->setUserId('test');
$config->setLocation('Test');
$config->setAvailabilityAsArray(
[
"timezoneId" => "Europe/Berlin",
"slots" => [
"MO" => [["start" => 1713153660, "end" => 1713239940]],
"TU" => [["start" => 1713153660, "end" => 1713239940]],
"WE" => [["start" => 1713153660, "end" => 1713239940]],
"TH" => [["start" => 1713153660, "end" => 1713239940]],
"FR" => [["start" => 1713153660, "end" => 1713239940]],
"SA" => [["start" => 1713153660, "end" => 1713239940]],
"SU" => [["start" => 1713153660, "end" => 1713239940]]
]
]
);
$this->userManager->expects(self::once())
->method('get')
->willReturn($this->createConfiguredMock(IUser::class, [
@ -505,6 +519,20 @@ class MailServiceTest extends TestCase {
$config = new AppointmentConfig();
$config->setUserId('test');
$config->setLocation('Test');
$config->setAvailabilityAsArray(
[
"timezoneId" => "Europe/Berlin",
"slots" => [
"MO" => [["start" => 1713153660, "end" => 1713239940]],
"TU" => [["start" => 1713153660, "end" => 1713239940]],
"WE" => [["start" => 1713153660, "end" => 1713239940]],
"TH" => [["start" => 1713153660, "end" => 1713239940]],
"FR" => [["start" => 1713153660, "end" => 1713239940]],
"SA" => [["start" => 1713153660, "end" => 1713239940]],
"SU" => [["start" => 1713153660, "end" => 1713239940]]
]
]
);
$this->userManager->expects(self::once())
->method('get')
->willReturn($this->createConfiguredMock(IUser::class, [
@ -563,6 +591,90 @@ class MailServiceTest extends TestCase {
$this->mailService->sendOrganizerBookingInformationEmail($booking, $config, 'abc');
}
public function testSendOrganizerBookingInformationEmailDifferentTZ(): void {
$booking = new Booking();
$booking->setEmail('test@test.com');
$booking->setDisplayName('Test');
$booking->setStart(time());
$booking->setTimezone('America/Toronto');
$booking->setDescription('Test');
$config = new AppointmentConfig();
$config->setUserId('test');
$config->setLocation('Test');
$config->setAvailabilityAsArray(
[
"timezoneId" => "Europe/Berlin",
"slots" => [
"MO" => [["start" => 1713153660, "end" => 1713239940]],
"TU" => [["start" => 1713153660, "end" => 1713239940]],
"WE" => [["start" => 1713153660, "end" => 1713239940]],
"TH" => [["start" => 1713153660, "end" => 1713239940]],
"FR" => [["start" => 1713153660, "end" => 1713239940]],
"SA" => [["start" => 1713153660, "end" => 1713239940]],
"SU" => [["start" => 1713153660, "end" => 1713239940]]
]
]
);
$this->userManager->expects(self::once())
->method('get')
->willReturn($this->createConfiguredMock(IUser::class, [
'getEmailAddress' => 'test@test.com',
'getDisplayName' => 'Test Test'
]));
$mailMessage = $this->createMock(IMessage::class);
$this->mailer->expects(self::once())
->method('createMessage')
->willReturn($mailMessage);
$mailMessage->expects(self::once())
->method('setFrom')
->willReturn($mailMessage);
$mailMessage->expects(self::once())
->method('setTo')
->willReturn($mailMessage);
$mailMessage->expects(self::once())
->method('useTemplate')
->willReturn($mailMessage);
$emailTemplate = $this->createMock(IEMailTemplate::class);
$this->mailer->expects(self::once())
->method('createEmailTemplate')
->willReturn($emailTemplate);
$emailTemplate->expects(self::once())
->method('addHeader');
$emailTemplate->expects(self::once())
->method('setSubject');
$emailTemplate->expects(self::once())
->method('addHeading');
$emailTemplate->expects(self::exactly(5))
->method('addBodyListItem');
$emailTemplate->expects(self::once())
->method('addFooter');
$this->mailer->expects(self::once())
->method('createEmailTemplate');
$this->mailer->expects(self::once())
->method('createAttachment');
$this->l10n->expects(self::exactly(6))
->method('t');
$this->lFactory->expects(self::once())
->method('findGenericLanguage')
->willReturn('en');
$this->lFactory->expects(self::once())
->method('get');
$this->dateFormatter->expects(self::once())
->method('formatDateTimeRelativeDay')
->with(self::anything(), self::anything(), self::anything(), 'Europe/Berlin')
->willReturn('Test');
$this->mailer->expects(self::once())
->method('send')
->willReturn([]);
$this->logger->expects(self::never())
->method('warning');
$this->logger->expects(self::never())
->method('debug');
$this->mailService->sendOrganizerBookingInformationEmail($booking, $config, 'abc');
}
public function testSendOrganizerBookingInformationEmailFailed(): void {
$booking = new Booking();
$booking->setEmail('test@test.com');
@ -573,6 +685,20 @@ class MailServiceTest extends TestCase {
$config = new AppointmentConfig();
$config->setUserId('test');
$config->setLocation('Test');
$config->setAvailabilityAsArray(
[
"timezoneId" => "Europe/Berlin",
"slots" => [
"MO" => [["start" => 1713153660, "end" => 1713239940]],
"TU" => [["start" => 1713153660, "end" => 1713239940]],
"WE" => [["start" => 1713153660, "end" => 1713239940]],
"TH" => [["start" => 1713153660, "end" => 1713239940]],
"FR" => [["start" => 1713153660, "end" => 1713239940]],
"SA" => [["start" => 1713153660, "end" => 1713239940]],
"SU" => [["start" => 1713153660, "end" => 1713239940]]
]
]
);
$this->userManager->expects(self::once())
->method('get')
->willReturn($this->createConfiguredMock(IUser::class, [
@ -654,6 +780,20 @@ class MailServiceTest extends TestCase {
$config = new AppointmentConfig();
$config->setUserId('test');
$config->setLocation('Test');
$config->setAvailabilityAsArray(
[
"timezoneId" => "Europe/Berlin",
"slots" => [
"MO" => [["start" => 1713153660, "end" => 1713239940]],
"TU" => [["start" => 1713153660, "end" => 1713239940]],
"WE" => [["start" => 1713153660, "end" => 1713239940]],
"TH" => [["start" => 1713153660, "end" => 1713239940]],
"FR" => [["start" => 1713153660, "end" => 1713239940]],
"SA" => [["start" => 1713153660, "end" => 1713239940]],
"SU" => [["start" => 1713153660, "end" => 1713239940]]
]
]
);
$notification = $this->createMock(INotification::class);
$this->lFactory->expects(self::once())