Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Add symfony event for public links shared by email #31632

Merged
merged 1 commit into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/ajax/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
\OC::$server->getMailer(),
\OC::$server->getLogger(),
$defaults,
\OC::$server->getURLGenerator()
\OC::$server->getURLGenerator(),
\OC::$server->getEventDispatcher()
);

$result = $mailNotification->sendInternalShareMail($recipientList, $itemSource, $itemType);
Expand Down Expand Up @@ -191,7 +192,8 @@ function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
\OC::$server->getMailer(),
\OC::$server->getLogger(),
$defaults,
\OC::$server->getURLGenerator()
\OC::$server->getURLGenerator(),
\OC::$server->getEventDispatcher()
);

$expiration = null;
Expand Down
19 changes: 18 additions & 1 deletion lib/private/Share/MailNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
use OCP\Defaults;
use OCP\Util;
use OC\Share\Filters\MailNotificationFilter;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class MailNotifications
Expand All @@ -62,6 +64,8 @@ class MailNotifications {
private $logger;
/** @var IURLGenerator */
private $urlGenerator;
/** @var EventDispatcher */
private $eventDispatcher;

/**
* @param IUser $user
Expand All @@ -76,13 +80,15 @@ public function __construct(IUser $user,
IMailer $mailer,
ILogger $logger,
Defaults $defaults,
IURLGenerator $urlGenerator) {
IURLGenerator $urlGenerator,
EventDispatcher $eventDispatcher) {
$this->l = $l10n;
$this->user = $user;
$this->mailer = $mailer;
$this->logger = $logger;
$this->defaults = $defaults;
$this->urlGenerator = $urlGenerator;
$this->eventDispatcher = $eventDispatcher;

$this->replyTo = $this->user->getEMailAddress();

Expand Down Expand Up @@ -187,6 +193,17 @@ public function sendLinkShareMail($recipient, $filename, $link, $expiration, $pe
$subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]);
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, $personalNote);

/**
* The event consumer of share.sendmail would have following data
* - link ( the public link created )
* - to ( the to recipient in mail )
* - cc ( the cc recipient in mail )
* - bcc ( the bcc recipient in mail )
*/
$ccRecipients = isset($options['cc']) ? $options['cc'] : '';
$bccRecipients = isset($options['bcc']) ? $options['bcc'] : '';
$event = new GenericEvent(null, ['link' => $link, 'to' => $recipient, 'cc' => $ccRecipients, 'bcc' => $bccRecipients]);
$this->eventDispatcher->dispatch('share.sendmail', $event);
return $this->sendLinkShareMailFromBody($recipient, $subject, $htmlBody, $textBody, $options);
}

Expand Down
188 changes: 181 additions & 7 deletions tests/lib/Share/MailNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
use OCP\IUser;
use OCP\Mail\IMailer;
use OCP\Util;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;

/**
* Class MailNotificationsTest
*
* @group DB
*/
class MailNotificationsTest extends TestCase {
/** @var IL10N */
Expand All @@ -47,6 +51,7 @@ class MailNotificationsTest extends TestCase {
private $user;
/** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator;
private $eventDispatcher;


public function setUp() {
Expand All @@ -63,6 +68,7 @@ public function setUp() {
$this->user = $this->getMockBuilder('\OCP\IUser')
->disableOriginalConstructor()->getMock();
$this->urlGenerator = $this->createMock('\OCP\IURLGenerator');
$this->eventDispatcher = new EventDispatcher();

$this->l10n->expects($this->any())
->method('t')
Expand Down Expand Up @@ -125,12 +131,79 @@ public function testSendLinkShareMailWithoutReplyTo() {
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator
$this->urlGenerator,
$this->eventDispatcher
);

$this->assertSame([], $mailNotifications->sendLinkShareMail('[email protected]', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
}

public function testSendLinkShareMailWithRecipientAndOptions() {
$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(['[email protected]']);
$message
->expects($this->once())
->method('setHtmlBody')
->with($this->stringContains('personal note'));
$message
->expects($this->once())
->method('setPlainBody')
->with($this->stringContains('personal note'));

$message
->expects($this->once())
->method('setFrom')
->with([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(
$this->user,
$this->l10n,
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator,
$this->eventDispatcher
);

$calledEvent = [];
$this->eventDispatcher->addListener('share.sendmail', function (GenericEvent $event) use (&$calledEvent) {
$calledEvent[] = 'share.sendmail';
$calledEvent[] = $event;
});
$this->assertSame([], $mailNotifications->sendLinkShareMail('[email protected]', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600, 'personal note', ['bcc' => '[email protected],[email protected]', 'cc' => '[email protected],[email protected]']));

$this->assertEquals('share.sendmail', $calledEvent[0]);
$this->assertInstanceOf(GenericEvent::class, $calledEvent[1]);
$this->assertArrayHasKey('link', $calledEvent[1]);
$this->assertEquals('https://owncloud.com/file/?foo=bar', $calledEvent[1]->getArgument('link'));
$this->assertArrayHasKey('to', $calledEvent[1]);
$this->assertEquals('[email protected]', $calledEvent[1]->getArgument('to'));
$this->assertArrayHasKey('bcc', $calledEvent[1]);
$this->assertEquals('[email protected],[email protected]', $calledEvent[1]->getArgument('bcc'));
$this->assertArrayHasKey('cc', $calledEvent[1]);
$this->assertEquals('[email protected],[email protected]', $calledEvent[1]->getArgument('cc'));
}

public function testSendLinkShareMailPersonalNote() {
$message = $this->getMockBuilder('\OC\Mail\Message')
->disableOriginalConstructor()->getMock();
Expand Down Expand Up @@ -174,10 +247,23 @@ public function testSendLinkShareMailPersonalNote() {
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator
$this->urlGenerator,
$this->eventDispatcher
);

$calledEvent = [];
$this->eventDispatcher->addListener('share.sendmail', function (GenericEvent $event) use (&$calledEvent) {
$calledEvent[] = 'share.sendmail';
$calledEvent[] = $event;
});
$this->assertSame([], $mailNotifications->sendLinkShareMail('[email protected]', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600, 'personal note'));

$this->assertEquals('share.sendmail', $calledEvent[0]);
$this->assertInstanceOf(GenericEvent::class, $calledEvent[1]);
$this->assertArrayHasKey('link', $calledEvent[1]);
$this->assertEquals('https://owncloud.com/file/?foo=bar', $calledEvent[1]->getArgument('link'));
$this->assertArrayHasKey('to', $calledEvent[1]);
$this->assertEquals('[email protected]', $calledEvent[1]->getArgument('to'));
}

public function dataSendLinkShareMailWithReplyTo() {
Expand Down Expand Up @@ -239,7 +325,8 @@ public function testSendLinkShareMailWithReplyTo($to, array $expectedTo) {
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator
$this->urlGenerator,
$this->eventDispatcher
);
$this->assertSame([], $mailNotifications->sendLinkShareMail($to, 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
}
Expand All @@ -253,7 +340,8 @@ public function testSendLinkShareMailException() {
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator
$this->urlGenerator,
$this->eventDispatcher
);

$this->assertSame(['[email protected]'], $mailNotifications->sendLinkShareMail('[email protected]', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
Expand All @@ -271,14 +359,15 @@ public function testSendInternalShareMail() {
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator
$this->urlGenerator,
$this->eventDispatcher
])
->getMock();

$mailNotifications->method('getItemSharedWithUser')
->withAnyParameters()
->willReturn([
['file_target' => '/<welcome>.txt', 'item_source' => 123],
['file_target' => '/<welcome>.txt', 'item_source' => 123, 'expiration' => '2017-01-01T15:03:01.012345Z'],
]);

$recipient = $this->getMockBuilder('\OCP\IUser')
Expand Down Expand Up @@ -307,6 +396,90 @@ public function testSendInternalShareMail() {

}

public function testSendInternalShareMailException() {
$this->setupMailerMock('TestUser shared »&lt;welcome&gt;.txt« with you', ['[email protected]' => 'Recipient'], false);

/** @var MailNotifications | \PHPUnit_Framework_MockObject_MockObject $mailNotifications */
$mailNotifications = $this->getMockBuilder('OC\Share\MailNotifications')
->setMethods(['getItemSharedWithUser'])
->setConstructorArgs([
$this->user,
$this->l10n,
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator,
$this->eventDispatcher
])
->getMock();

$mailNotifications->method('getItemSharedWithUser')
->withAnyParameters()
->willReturn([
['file_target' => '/<welcome>.txt', 'item_source' => 123, 'expiration' => 'foo'],
]);

$recipient = $this->getMockBuilder('\OCP\IUser')
->disableOriginalConstructor()->getMock();
$recipient
->expects($this->once())
->method('getEMailAddress')
->willReturn('[email protected]');
$recipient
->expects($this->once())
->method('getDisplayName')
->willReturn('Recipient');

$this->urlGenerator->expects($this->once())
->method('linkToRouteAbsolute')
->with(
$this->equalTo('files.viewcontroller.showFile'),
$this->equalTo([
'fileId' => 123,
])
);

$this->mailer->expects($this->once())
->method('send')
->willThrowException(new \Exception());

$recipientList = [$recipient];
$result = $mailNotifications->sendInternalShareMail($recipientList, '3', 'file');
$this->assertEquals(['Recipient'], $result);
}

public function testGetItemSharedWithUser() {
$mailNotifications = new MailNotifications(
$this->user,
$this->l10n,
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator,
$this->eventDispatcher
);
/**
* The below piece of code is borrowed from
* https://github.com/owncloud/core/blob/master/tests/lib/Share/ShareTest.php#L621-L639
*/
$uid1 = $this->getUniqueID('user1_');
$uid2 = $this->getUniqueID('user2_');
$user2 = $this->createMock(IUser::class);
$user2->expects($this->once())
->method('getUID')
->willReturn($uid2);

//add dummy values to the share table
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
.' `item_type`, `item_source`, `item_target`, `share_type`,'
.' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
$args = ['test', 99, 'target1', \OCP\Share::SHARE_TYPE_USER, $uid2, $uid1];
$query->execute($args);

$result = $this->invokePrivate($mailNotifications, 'getItemSharedWithUser', [99, 'test', $user2]);
$this->assertCount(1, $result);
}

public function emptinessProvider() {
return [
[null],
Expand All @@ -327,7 +500,8 @@ public function testSendInternalShareMailNoMail($emptiness) {
$this->mailer,
$this->logger,
$this->defaults,
$this->urlGenerator
$this->urlGenerator,
$this->eventDispatcher
])
->getMock();

Expand Down