From 17f85e695891bb721ec6a156ab2012a95aeb0c9b Mon Sep 17 00:00:00 2001 From: Sujith H Date: Mon, 14 Jan 2019 18:27:26 +0530 Subject: [PATCH] [stable10] Backport of Fixing email sent to recipient using recipients language code The recipient, internal to owncloud instance should recieve the email from the sender ( internal to owncloud ) in the language chosen by the recipient. Signed-off-by: Sujith H --- lib/private/Share/MailNotifications.php | 12 +++- tests/lib/Share/MailNotificationsTest.php | 68 +++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/lib/private/Share/MailNotifications.php b/lib/private/Share/MailNotifications.php index 6cf2110d8d57..6a20c87cbe59 100644 --- a/lib/private/Share/MailNotifications.php +++ b/lib/private/Share/MailNotifications.php @@ -164,8 +164,16 @@ public function sendInternalShareMail($recipientList, $itemSource, $itemType) { $filename = $filter->getFile(); $link = $filter->getLink(); - $subject = (string) $this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $unescapedFilename]); - list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, null, 'internal'); + $recipientLanguageCode = $this->config->getUserValue($recipient->getUID(), 'core', 'lang', 'en'); + $recipientL10N = \OC::$server->getL10N('core'); + if ($this->l->getLanguageCode() !== $recipientLanguageCode) { + $recipientL10N = \OC::$server->getL10N('core', $recipientLanguageCode); + $subject = (string)$recipientL10N->t('%s shared »%s« with you', [$this->senderDisplayName, $unescapedFilename]); + } else { + $subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $unescapedFilename]); + } + + list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, null, 'internal', $recipientL10N); // send it out now try { diff --git a/tests/lib/Share/MailNotificationsTest.php b/tests/lib/Share/MailNotificationsTest.php index b7e5580b4c4e..3789c49dde64 100644 --- a/tests/lib/Share/MailNotificationsTest.php +++ b/tests/lib/Share/MailNotificationsTest.php @@ -29,6 +29,7 @@ use OCP\IURLGenerator; use OCP\IUser; use OCP\Mail\IMailer; +use OCP\Share\IShare; use OCP\Util; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\GenericEvent; @@ -651,4 +652,71 @@ protected function setupMailerMock($subject, $to, $exceptionOnSend = true) { ->will($this->throwException(new \Exception('Some Exception Message'))); } } + + public function providesLanguages() { + return [ + ['es', 'en'], + ['en', 'en'] + ]; + } + + /** + * @dataProvider providesLanguages + * @param string $recipientLanguage + * @param string $senderLanguage + */ + public function testSendInternalShareWithRecipientLanguageCode($recipientLanguage, $senderLanguage) { + $this->setupMailerMock('TestUser shared ».txt« with you', ['recipient@owncloud.com' => 'Recipient'], false); + $mailNotifications = $this->getMockBuilder('OC\Share\MailNotifications') + ->setMethods(['getItemSharedWithUser']) + ->setConstructorArgs([ + $this->user, + $this->l10n, + $this->mailer, + $this->config, + $this->logger, + $this->defaults, + $this->urlGenerator, + $this->eventDispatcher + ]) + ->getMock(); + + $mailNotifications->method('getItemSharedWithUser') + ->withAnyParameters() + ->willReturn([ + ['file_target' => '/.txt', 'item_source' => 123, 'expiration' => '2017-01-01T15:03:01.012345Z'], + ]); + + $recipient = $this->createMock(IUser::class); + $recipient->expects($this->once()) + ->method('getEMailAddress') + ->willReturn('recipient@owncloud.com'); + $recipient->expects($this->once()) + ->method('getDisplayName') + ->willReturn('Recipient'); + $recipient->method('getUID') + ->willReturn('Recipient'); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with('Recipient', 'core', 'lang', 'en') + ->willReturn($recipientLanguage); + + $this->l10n->method('getLanguageCode') + ->willReturn($senderLanguage); + + $this->urlGenerator->expects($this->once()) + ->method('linkToRouteAbsolute') + ->with( + $this->equalTo('files.viewcontroller.showFile'), + $this->equalTo([ + 'fileId' => 123, + ]) + ); + + $recipientList = [$recipient]; + + $result = $mailNotifications->sendInternalShareMail($recipientList, '3', 'file'); + $this->assertSame([], $result); + } }