2015-04-10 18:21:52 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
use OC\Share\MailNotifications;
|
|
|
|
use OCP\IL10N;
|
2015-12-01 14:05:40 +03:00
|
|
|
use OCP\IUser;
|
2015-04-10 18:21:52 +03:00
|
|
|
use OCP\Mail\IMailer;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\Defaults;
|
2016-03-14 17:19:55 +03:00
|
|
|
use OCP\IURLGenerator;
|
2015-04-10 18:21:52 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class MailNotificationsTest
|
|
|
|
*/
|
|
|
|
class MailNotificationsTest extends \Test\TestCase {
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
2015-12-01 14:05:40 +03:00
|
|
|
/** @var IMailer | PHPUnit_Framework_MockObject_MockObject */
|
2015-04-10 18:21:52 +03:00
|
|
|
private $mailer;
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2015-12-01 14:05:40 +03:00
|
|
|
/** @var Defaults | PHPUnit_Framework_MockObject_MockObject */
|
2015-04-10 18:21:52 +03:00
|
|
|
private $defaults;
|
2015-12-01 14:05:40 +03:00
|
|
|
/** @var IUser | PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $user;
|
2016-03-14 17:19:55 +03:00
|
|
|
/** @var IURLGenerator | PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $urlGenerator;
|
2015-04-10 18:21:52 +03:00
|
|
|
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->l10n = $this->getMockBuilder('\OCP\IL10N')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->logger = $this->getMockBuilder('\OCP\ILogger')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->defaults = $this->getMockBuilder('\OCP\Defaults')
|
2015-12-01 14:05:40 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$this->user = $this->getMockBuilder('\OCP\IUser')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-03-14 17:19:55 +03:00
|
|
|
$this->urlGenerator = $this->getMock('\OCP\IURLGenerator');
|
2015-04-10 18:21:52 +03:00
|
|
|
|
|
|
|
$this->l10n->expects($this->any())
|
|
|
|
->method('t')
|
|
|
|
->will($this->returnCallback(function($text, $parameters = array()) {
|
|
|
|
return vsprintf($text, $parameters);
|
|
|
|
}));
|
2015-12-01 14:05:40 +03:00
|
|
|
|
|
|
|
$this->defaults
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('UnitTestCloud'));
|
|
|
|
|
|
|
|
$this->user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getEMailAddress')
|
|
|
|
->willReturn('sharer@owncloud.com');
|
|
|
|
$this->user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getDisplayName')
|
|
|
|
->willReturn('TestUser');
|
|
|
|
|
2015-04-10 18:21:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendLinkShareMailWithoutReplyTo() {
|
|
|
|
$message = $this->getMockBuilder('\OC\Mail\Message')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setSubject')
|
|
|
|
->with('TestUser shared »MyFile« with you');
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setTo')
|
|
|
|
->with(['lukas@owncloud.com']);
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setHtmlBody');
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setPlainBody');
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setFrom')
|
|
|
|
->with([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);
|
|
|
|
|
|
|
|
$this->mailer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('createMessage')
|
|
|
|
->will($this->returnValue($message));
|
|
|
|
$this->mailer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('send')
|
|
|
|
->with($message)
|
|
|
|
->will($this->returnValue([]));
|
|
|
|
|
|
|
|
$mailNotifications = new MailNotifications(
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->user,
|
2015-04-10 18:21:52 +03:00
|
|
|
$this->l10n,
|
|
|
|
$this->mailer,
|
|
|
|
$this->logger,
|
2016-03-14 17:19:55 +03:00
|
|
|
$this->defaults,
|
|
|
|
$this->urlGenerator
|
2015-04-10 18:21:52 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame([], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
|
|
|
|
}
|
|
|
|
|
2015-12-07 12:49:34 +03:00
|
|
|
public function dataSendLinkShareMailWithReplyTo() {
|
|
|
|
return [
|
2015-12-07 14:01:02 +03:00
|
|
|
['lukas@owncloud.com', ['lukas@owncloud.com']],
|
|
|
|
['lukas@owncloud.com nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
|
|
|
|
['lukas@owncloud.com,nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
|
|
|
|
['lukas@owncloud.com, nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
|
|
|
|
['lukas@owncloud.com;nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
|
|
|
|
['lukas@owncloud.com; nickvergessen@owncloud.com', ['lukas@owncloud.com', 'nickvergessen@owncloud.com']],
|
2015-12-07 12:49:34 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSendLinkShareMailWithReplyTo
|
|
|
|
* @param string $to
|
2015-12-07 14:01:02 +03:00
|
|
|
* @param array $expectedTo
|
2015-12-07 12:49:34 +03:00
|
|
|
*/
|
2015-12-07 14:01:02 +03:00
|
|
|
public function testSendLinkShareMailWithReplyTo($to, array $expectedTo) {
|
2015-04-10 18:21:52 +03:00
|
|
|
$message = $this->getMockBuilder('\OC\Mail\Message')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setSubject')
|
|
|
|
->with('TestUser shared »MyFile« with you');
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setTo')
|
2015-12-07 14:01:02 +03:00
|
|
|
->with($expectedTo);
|
2015-04-10 18:21:52 +03:00
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setHtmlBody');
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setPlainBody');
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setFrom')
|
|
|
|
->with([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setReplyTo')
|
|
|
|
->with(['sharer@owncloud.com']);
|
|
|
|
|
|
|
|
$this->mailer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('createMessage')
|
|
|
|
->will($this->returnValue($message));
|
|
|
|
$this->mailer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('send')
|
|
|
|
->with($message)
|
|
|
|
->will($this->returnValue([]));
|
|
|
|
|
|
|
|
$mailNotifications = new MailNotifications(
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->user,
|
2015-04-10 18:21:52 +03:00
|
|
|
$this->l10n,
|
|
|
|
$this->mailer,
|
|
|
|
$this->logger,
|
2016-03-14 17:19:55 +03:00
|
|
|
$this->defaults,
|
|
|
|
$this->urlGenerator
|
2015-04-10 18:21:52 +03:00
|
|
|
);
|
2015-12-07 12:49:34 +03:00
|
|
|
$this->assertSame([], $mailNotifications->sendLinkShareMail($to, 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
|
2015-04-10 18:21:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendLinkShareMailException() {
|
2015-12-02 16:04:27 +03:00
|
|
|
$this->setupMailerMock('TestUser shared »MyFile« with you', ['lukas@owncloud.com']);
|
2015-04-10 18:21:52 +03:00
|
|
|
|
|
|
|
$mailNotifications = new MailNotifications(
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->user,
|
2015-04-10 18:21:52 +03:00
|
|
|
$this->l10n,
|
|
|
|
$this->mailer,
|
|
|
|
$this->logger,
|
2016-03-14 17:19:55 +03:00
|
|
|
$this->defaults,
|
|
|
|
$this->urlGenerator
|
2015-04-10 18:21:52 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(['lukas@owncloud.com'], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
|
|
|
|
}
|
|
|
|
|
2015-12-02 16:04:27 +03:00
|
|
|
public function testSendInternalShareMail() {
|
|
|
|
$this->setupMailerMock('TestUser shared »welcome.txt« with you', ['recipient@owncloud.com' => 'Recipient'], false);
|
|
|
|
|
|
|
|
/** @var MailNotifications | PHPUnit_Framework_MockObject_MockObject $mailNotifications */
|
|
|
|
$mailNotifications = $this->getMock('OC\Share\MailNotifications',['getItemSharedWithUser'], [
|
|
|
|
$this->user,
|
|
|
|
$this->l10n,
|
|
|
|
$this->mailer,
|
|
|
|
$this->logger,
|
2016-03-14 17:19:55 +03:00
|
|
|
$this->defaults,
|
|
|
|
$this->urlGenerator
|
|
|
|
]);
|
2015-12-02 16:04:27 +03:00
|
|
|
|
|
|
|
$mailNotifications->method('getItemSharedWithUser')
|
|
|
|
->withAnyParameters()
|
|
|
|
->willReturn([
|
2016-05-06 15:41:31 +03:00
|
|
|
['file_target' => '/welcome.txt', 'item_source' => 123],
|
2015-12-02 16:04:27 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$recipient = $this->getMockBuilder('\OCP\IUser')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$recipient
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getEMailAddress')
|
|
|
|
->willReturn('recipient@owncloud.com');
|
|
|
|
$recipient
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getDisplayName')
|
|
|
|
->willReturn('Recipient');
|
|
|
|
|
2016-03-14 17:19:55 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
|
|
|
->with(
|
2016-05-06 15:41:31 +03:00
|
|
|
$this->equalTo('files.viewcontroller.showFile'),
|
2016-03-14 17:19:55 +03:00
|
|
|
$this->equalTo([
|
2016-05-06 15:41:31 +03:00
|
|
|
'fileId' => 123,
|
2016-03-14 17:19:55 +03:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
2015-12-02 16:04:27 +03:00
|
|
|
$recipientList = [$recipient];
|
|
|
|
$result = $mailNotifications->sendInternalShareMail($recipientList, '3', 'file');
|
|
|
|
$this->assertSame([], $result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-08 18:01:20 +03:00
|
|
|
/**
|
|
|
|
* @param string $subject
|
|
|
|
*/
|
2015-12-02 16:04:27 +03:00
|
|
|
protected function setupMailerMock($subject, $to, $exceptionOnSend = true) {
|
|
|
|
$message = $this->getMockBuilder('\OC\Mail\Message')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setSubject')
|
|
|
|
->with($subject);
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setTo')
|
|
|
|
->with($to);
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setHtmlBody');
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setPlainBody');
|
|
|
|
$message
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setFrom')
|
|
|
|
->with([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);
|
|
|
|
|
|
|
|
$this->mailer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('createMessage')
|
|
|
|
->will($this->returnValue($message));
|
|
|
|
if ($exceptionOnSend) {
|
|
|
|
$this->mailer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('send')
|
|
|
|
->with($message)
|
|
|
|
->will($this->throwException(new Exception('Some Exception Message')));
|
|
|
|
}
|
|
|
|
}
|
2015-04-10 18:21:52 +03:00
|
|
|
}
|