123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright Copyright (c) 2019, Thomas Citharel
- * @copyright Copyright (c) 2019, Georg Ehrke
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Georg Ehrke <oc.list@georgehrke.com>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Citharel <nextcloud@tcit.fr>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * 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
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace OCA\DAV\Tests\unit\CalDAV\Reminder\NotificationProvider;
- use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
- use OCP\IConfig;
- use OCP\IL10N;
- use OCP\ILogger;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\L10N\IFactory as L10NFactory;
- use OCP\Mail\IEMailTemplate;
- use OCP\Mail\IMailer;
- use OCP\Mail\IMessage;
- use PHPUnit\Framework\MockObject\MockObject;
- use Sabre\VObject\Component\VCalendar;
- class EmailProviderTest extends AbstractNotificationProviderTest {
- public const USER_EMAIL = 'frodo@hobb.it';
- /** @var ILogger|MockObject */
- protected $logger;
- /** @var L10NFactory|MockObject */
- protected $l10nFactory;
- /** @var IL10N|MockObject */
- protected $l10n;
- /** @var IURLGenerator|MockObject */
- protected $urlGenerator;
- /** @var IConfig|MockObject */
- protected $config;
- /** @var IMailer|MockObject */
- private $mailer;
- protected function setUp(): void {
- parent::setUp();
- $this->mailer = $this->createMock(IMailer::class);
- $this->provider = new EmailProvider(
- $this->config,
- $this->mailer,
- $this->logger,
- $this->l10nFactory,
- $this->urlGenerator
- );
- }
- public function testSendWithoutAttendees():void {
- list($user1, $user2, $user3, , $user5) = $users = $this->getUsers();
- $enL10N = $this->createMock(IL10N::class);
- $enL10N->method('t')
- ->willReturnArgument(0);
- $enL10N->method('l')
- ->willReturnArgument(0);
- $deL10N = $this->createMock(IL10N::class);
- $deL10N->method('t')
- ->willReturnArgument(0);
- $deL10N->method('l')
- ->willReturnArgument(0);
- $this->l10nFactory
- ->method('getUserLanguage')
- ->willReturnMap([
- [$user1, 'en'],
- [$user2, 'de'],
- [$user3, 'de'],
- [$user5, 'de'],
- ]);
- $this->l10nFactory
- ->method('findLanguage')
- ->willReturn('en');
- $this->l10nFactory
- ->method('languageExists')
- ->willReturnMap([
- ['dav', 'en', true],
- ['dav', 'de', true],
- ]);
- $this->l10nFactory
- ->method('get')
- ->willReturnMap([
- ['dav', 'en', null, $enL10N],
- ['dav', 'de', null, $deL10N],
- ]);
- $template1 = $this->getTemplateMock();
- $message11 = $this->getMessageMock('uid1@example.com', $template1);
- $template2 = $this->getTemplateMock();
- $message21 = $this->getMessageMock('uid2@example.com', $template2);
- $message22 = $this->getMessageMock('uid3@example.com', $template2);
- $this->mailer->expects($this->at(0))
- ->method('createEMailTemplate')
- ->with('dav.calendarReminder')
- ->willReturn($template1);
- $this->mailer->expects($this->at(1))
- ->method('validateMailAddress')
- ->with('uid1@example.com')
- ->willReturn(true);
- $this->mailer->expects($this->at(2))
- ->method('createMessage')
- ->with()
- ->willReturn($message11);
- $this->mailer->expects($this->at(3))
- ->method('send')
- ->with($message11)
- ->willReturn([]);
- $this->mailer->expects($this->at(4))
- ->method('createEMailTemplate')
- ->with('dav.calendarReminder')
- ->willReturn($template2);
- $this->mailer->expects($this->at(5))
- ->method('validateMailAddress')
- ->with('uid2@example.com')
- ->willReturn(true);
- $this->mailer->expects($this->at(6))
- ->method('createMessage')
- ->with()
- ->willReturn($message21);
- $this->mailer->expects($this->at(7))
- ->method('send')
- ->with($message21)
- ->willReturn([]);
- $this->mailer->expects($this->at(8))
- ->method('validateMailAddress')
- ->with('uid3@example.com')
- ->willReturn(true);
- $this->mailer->expects($this->at(9))
- ->method('createMessage')
- ->with()
- ->willReturn($message22);
- $this->mailer->expects($this->at(10))
- ->method('send')
- ->with($message22)
- ->willReturn([]);
- $this->mailer->expects($this->at(11))
- ->method('validateMailAddress')
- ->with('invalid')
- ->willReturn(false);
- $this->setupURLGeneratorMock(2);
- $vcalendar = $this->getNoAttendeeVCalendar();
- $this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
- }
- public function testSendWithAttendees(): void {
- list($user1, $user2, $user3, , $user5) = $users = $this->getUsers();
- $enL10N = $this->createMock(IL10N::class);
- $enL10N->method('t')
- ->willReturnArgument(0);
- $enL10N->method('l')
- ->willReturnArgument(0);
- $deL10N = $this->createMock(IL10N::class);
- $deL10N->method('t')
- ->willReturnArgument(0);
- $deL10N->method('l')
- ->willReturnArgument(0);
- $this->l10nFactory
- ->method('getUserLanguage')
- ->willReturnMap([
- [$user1, 'en'],
- [$user2, 'de'],
- [$user3, 'de'],
- [$user5, 'de'],
- ]);
- $this->l10nFactory
- ->method('findLanguage')
- ->willReturn('en');
- $this->l10nFactory
- ->method('languageExists')
- ->willReturnMap([
- ['dav', 'en', true],
- ['dav', 'de', true],
- ]);
- $this->l10nFactory
- ->method('get')
- ->willReturnMap([
- ['dav', 'en', null, $enL10N],
- ['dav', 'de', null, $deL10N],
- ]);
- $template1 = $this->getTemplateMock();
- $message11 = $this->getMessageMock('foo1@example.org', $template1);
- $message12 = $this->getMessageMock('uid2@example.com', $template1);
- $message13 = $this->getMessageMock('uid3@example.com', $template1);
- $template2 = $this->getTemplateMock();
- $message21 = $this->getMessageMock('foo3@example.org', $template2);
- $message22 = $this->getMessageMock('foo4@example.org', $template2);
- $message23 = $this->getMessageMock('uid1@example.com', $template2);
- $this->mailer->expects(self::exactly(2))
- ->method('createEMailTemplate')
- ->with('dav.calendarReminder')
- ->willReturnOnConsecutiveCalls(
- $template1,
- $template2
- );
- $this->mailer->expects($this->atLeastOnce())
- ->method('validateMailAddress')
- ->willReturnMap([
- ['foo1@example.org', true],
- ['foo3@example.org', true],
- ['foo4@example.org', true],
- ['uid1@example.com', true],
- ['uid2@example.com', true],
- ['uid3@example.com', true],
- ['invalid', false]
- ]);
- $this->mailer->expects($this->exactly(6))
- ->method('createMessage')
- ->with()
- ->willReturnOnConsecutiveCalls(
- $message11,
- $message12,
- $message13,
- $message21,
- $message22,
- $message23
- );
- $this->mailer->expects($this->exactly(6))
- ->method('send')
- ->withConsecutive(
- [$message11],
- [$message12],
- [$message13],
- [$message21],
- [$message22],
- [$message23]
- )->willReturn([]);
- $this->setupURLGeneratorMock(2);
- $vcalendar = $this->getAttendeeVCalendar();
- $this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
- }
- /**
- * @return IEMailTemplate
- */
- private function getTemplateMock():IEMailTemplate {
- $template = $this->createMock(IEMailTemplate::class);
- $template->expects($this->at(0))
- ->method('addHeader')
- ->with()
- ->willReturn($template);
- $template->expects($this->at(1))
- ->method('setSubject')
- ->with()
- ->willReturn($template);
- $template->expects($this->at(2))
- ->method('addHeading')
- ->with()
- ->willReturn($template);
- $template->expects($this->at(3))
- ->method('addBodyListItem')
- ->with()
- ->willReturn($template);
- $template->expects($this->at(4))
- ->method('addBodyListItem')
- ->with()
- ->willReturn($template);
- $template->expects($this->at(5))
- ->method('addBodyListItem')
- ->with()
- ->willReturn($template);
- $template->expects($this->at(6))
- ->method('addBodyListItem')
- ->with()
- ->willReturn($template);
- $template->expects($this->at(7))
- ->method('addFooter')
- ->with()
- ->willReturn($template);
- return $template;
- }
- /**
- * @param string $toMail
- * @param IEMailTemplate $templateMock
- * @param array|null $replyTo
- * @return IMessage
- */
- private function getMessageMock(string $toMail, IEMailTemplate $templateMock, array $replyTo=null):IMessage {
- $message = $this->createMock(IMessage::class);
- $i = 0;
- $message->expects($this->at($i++))
- ->method('setFrom')
- ->with([\OCP\Util::getDefaultEmailAddress('reminders-noreply')])
- ->willReturn($message);
- if ($replyTo) {
- $message->expects($this->at($i++))
- ->method('setReplyTo')
- ->with($replyTo)
- ->willReturn($message);
- }
- $message->expects($this->at($i++))
- ->method('setTo')
- ->with([$toMail])
- ->willReturn($message);
- $message->expects($this->at($i++))
- ->method('useTemplate')
- ->with($templateMock)
- ->willReturn($message);
- return $message;
- }
- private function getNoAttendeeVCalendar():VCalendar {
- $vcalendar = new VCalendar();
- $vcalendar->add('VEVENT', [
- 'SUMMARY' => 'Fellowship meeting',
- 'DTSTART' => new \DateTime('2017-01-01 00:00:00+00:00'), // 1483228800,
- 'UID' => 'uid1234',
- 'LOCATION' => 'Location 123',
- 'DESCRIPTION' => 'DESCRIPTION 456',
- ]);
- return $vcalendar;
- }
- private function getAttendeeVCalendar():VCalendar {
- $vcalendar = new VCalendar();
- $vcalendar->add('VEVENT', [
- 'SUMMARY' => 'Fellowship meeting',
- 'DTSTART' => new \DateTime('2017-01-01 00:00:00+00:00'), // 1483228800,
- 'UID' => 'uid1234',
- 'LOCATION' => 'Location 123',
- 'DESCRIPTION' => 'DESCRIPTION 456',
- ]);
- $vcalendar->VEVENT->add(
- 'ATTENDEE',
- 'mailto:foo1@example.org',
- [
- 'LANG' => 'de',
- 'PARTSTAT' => 'NEEDS-ACTION',
- ]
- );
- $vcalendar->VEVENT->add(
- 'ATTENDEE',
- 'mailto:foo2@example.org',
- [
- 'LANG' => 'de',
- 'PARTSTAT' => 'DECLINED',
- ]
- );
- $vcalendar->VEVENT->add(
- 'ATTENDEE',
- 'mailto:foo3@example.org',
- [
- 'LANG' => 'en',
- 'PARTSTAT' => 'CONFIRMED',
- ]
- );
- $vcalendar->VEVENT->add(
- 'ATTENDEE',
- 'mailto:foo4@example.org'
- );
- $vcalendar->VEVENT->add(
- 'ATTENDEE',
- 'tomail:foo5@example.org'
- );
- return $vcalendar;
- }
- private function setupURLGeneratorMock(int $times=1):void {
- for ($i = 0; $i < $times; $i++) {
- $this->urlGenerator
- ->expects($this->at(8 * $i))
- ->method('imagePath')
- ->with('core', 'actions/info.png')
- ->willReturn('imagePath1');
- $this->urlGenerator
- ->expects($this->at(8 * $i + 1))
- ->method('getAbsoluteURL')
- ->with('imagePath1')
- ->willReturn('AbsURL1');
- $this->urlGenerator
- ->expects($this->at(8 * $i + 2))
- ->method('imagePath')
- ->with('core', 'places/calendar.png')
- ->willReturn('imagePath2');
- $this->urlGenerator
- ->expects($this->at(8 * $i + 3))
- ->method('getAbsoluteURL')
- ->with('imagePath2')
- ->willReturn('AbsURL2');
- $this->urlGenerator
- ->expects($this->at(8 * $i + 4))
- ->method('imagePath')
- ->with('core', 'actions/address.png')
- ->willReturn('imagePath3');
- $this->urlGenerator
- ->expects($this->at(8 * $i + 5))
- ->method('getAbsoluteURL')
- ->with('imagePath3')
- ->willReturn('AbsURL3');
- $this->urlGenerator
- ->expects($this->at(8 * $i + 6))
- ->method('imagePath')
- ->with('core', 'actions/more.png')
- ->willReturn('imagePath4');
- $this->urlGenerator
- ->expects($this->at(8 * $i + 7))
- ->method('getAbsoluteURL')
- ->with('imagePath4')
- ->willReturn('AbsURL4');
- }
- }
- private function getUsers(): array {
- $user1 = $this->createMock(IUser::class);
- $user1->method('getUID')
- ->willReturn('uid1');
- $user1->method('getEMailAddress')
- ->willReturn('uid1@example.com');
- $user2 = $this->createMock(IUser::class);
- $user2->method('getUID')
- ->willReturn('uid2');
- $user2->method('getEMailAddress')
- ->willReturn('uid2@example.com');
- $user3 = $this->createMock(IUser::class);
- $user3->method('getUID')
- ->willReturn('uid3');
- $user3->method('getEMailAddress')
- ->willReturn('uid3@example.com');
- $user4 = $this->createMock(IUser::class);
- $user4->method('getUID')
- ->willReturn('uid4');
- $user4->method('getEMailAddress')
- ->willReturn(null);
- $user5 = $this->createMock(IUser::class);
- $user5->method('getUID')
- ->willReturn('uid5');
- $user5->method('getEMailAddress')
- ->willReturn('invalid');
- return [$user1, $user2, $user3, $user4, $user5];
- }
- }
|