From 2a6ab0ddf86d12517948df2d3856b8b776a3d81e Mon Sep 17 00:00:00 2001 From: Olivier Lechevalier Date: Thu, 3 Oct 2019 06:58:04 +0900 Subject: [PATCH] Add expiration date to share email Signed-off-by: Olivier Lechevalier --- apps/sharebymail/lib/ShareByMailProvider.php | 4 + .../tests/ShareByMailProviderTest.php | 90 +++++++++++++++++++ phpunit.xml | 0 3 files changed, 94 insertions(+) create mode 100644 phpunit.xml diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index 9bfe5e733f49f..bc7f219482b2f 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) { + $relativeDate = (string)$expiration->diff(new \DateTime('now'))->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, + ]); + } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000000000..e69de29bb2d1d