diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index 9bfe5e733f49f..ebd4b992fde25 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -405,6 +405,10 @@ protected function sendMailNotification($filename, $emailTemplate->addHeader(); $emailTemplate->addHeading($this->l->t('%1$s shared »%2$s« with you', [$initiatorDisplayName, $filename]), false); $text = $this->l->t('%1$s shared »%2$s« with you.', [$initiatorDisplayName, $filename]); + if ($expiration instanceof \DateTime) { + $relativeDate = $expiration->diff(new \DateTime())->format('%a'); + $text .= ' ' . $this->l->t('It will expire in %1$s days.', [$relativeDate]); + } $emailTemplate->addBodyText( htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')), diff --git a/apps/sharebymail/tests/ShareByMailProviderTest.php b/apps/sharebymail/tests/ShareByMailProviderTest.php index 090e6e9284855..a1d849ccb27d2 100644 --- a/apps/sharebymail/tests/ShareByMailProviderTest.php +++ b/apps/sharebymail/tests/ShareByMailProviderTest.php @@ -1112,4 +1112,94 @@ public function testSendMailNotificationWithDifferentUserAndNoUserEmail() { null, ]); } + + public function testSendMailNotificationWithExpiration() { + $provider = $this->getInstance(); + $initiatorUser = $this->createMock(IUser::class); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('InitiatorUser') + ->willReturn($initiatorUser); + $initiatorUser + ->expects($this->once()) + ->method('getDisplayName') + ->willReturn('Mr. Initiator User'); + $message = $this->createMock(Message::class); + $this->mailer + ->expects($this->once()) + ->method('createMessage') + ->willReturn($message); + $template = $this->createMock(IEMailTemplate::class); + $this->mailer + ->expects($this->once()) + ->method('createEMailTemplate') + ->willReturn($template); + $template + ->expects($this->once()) + ->method('addHeader'); + $template + ->expects($this->once()) + ->method('addHeading') + ->with('Mr. Initiator User shared »file.txt« with you'); + $template + ->expects($this->once()) + ->method('addBodyText') + ->with( + 'Mr. Initiator User shared »file.txt« with you. It will expire in 7 days. Click the button below to open it.', + 'Mr. Initiator User shared »file.txt« with you. It will expire in 7 days.' + ); + $template + ->expects($this->once()) + ->method('addBodyButton') + ->with( + 'Open »file.txt«', + 'https://example.com/file.txt' + ); + $message + ->expects($this->once()) + ->method('setTo') + ->with(['john@doe.com']); + $this->defaults + ->expects($this->once()) + ->method('getName') + ->willReturn('UnitTestCloud'); + $message + ->expects($this->once()) + ->method('setFrom') + ->with([ + \OCP\Util::getDefaultEmailAddress('UnitTestCloud') => 'Mr. Initiator User via UnitTestCloud' + ]); + $message + ->expects($this->never()) + ->method('setReplyTo'); + $template + ->expects($this->once()) + ->method('addFooter') + ->with(''); + $template + ->expects($this->once()) + ->method('setSubject') + ->with('Mr. Initiator User shared »file.txt« with you'); + $message + ->expects($this->once()) + ->method('useTemplate') + ->with($template); + $this->mailer + ->expects($this->once()) + ->method('send') + ->with($message); + + $inOneWeek = (new \DateTime('now'))->add(new \DateInterval('P7D')); + self::invokePrivate( + $provider, + 'sendMailNotification', + [ + 'file.txt', + 'https://example.com/file.txt', + 'InitiatorUser', + 'john@doe.com', + $inOneWeek, + ]); + } }