123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- * @copyright Copyright (c) 2017, Georg Ehrke
- *
- * @author brad2014 <brad2014@users.noreply.github.com>
- * @author Brad Rubenstein <brad@wbr.tech>
- * @author Georg Ehrke <oc.list@georgehrke.com>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- *
- * @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/>
- *
- */
- namespace OCA\DAV\Tests\unit\CalDAV\Schedule;
- use OC\Mail\Mailer;
- use OCA\DAV\CalDAV\Schedule\IMipPlugin;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\DB\QueryBuilder\IQueryBuilder;
- use OCP\Defaults;
- use OCP\IConfig;
- use OCP\IDBConnection;
- use OCP\IL10N;
- use OCP\ILogger;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\L10N\IFactory;
- use OCP\Mail\IAttachment;
- use OCP\Mail\IEMailTemplate;
- use OCP\Mail\IMailer;
- use OCP\Mail\IMessage;
- use OCP\Security\ISecureRandom;
- use Sabre\VObject\Component\VCalendar;
- use Sabre\VObject\ITip\Message;
- use Test\TestCase;
- class IMipPluginTest extends TestCase {
- protected function setUp(): void {
- $this->mailMessage = $this->createMock(IMessage::class);
- $this->mailMessage->method('setFrom')->willReturn($this->mailMessage);
- $this->mailMessage->method('setReplyTo')->willReturn($this->mailMessage);
- $this->mailMessage->method('setTo')->willReturn($this->mailMessage);
- $this->mailer = $this->getMockBuilder(IMailer::class)->disableOriginalConstructor()->getMock();
- $this->mailer->method('createMessage')->willReturn($this->mailMessage);
- $this->emailTemplate = $this->createMock(IEMailTemplate::class);
- $this->mailer->method('createEMailTemplate')->willReturn($this->emailTemplate);
- $this->emailAttachment = $this->createMock(IAttachment::class);
- $this->mailer->method('createAttachment')->willReturn($this->emailAttachment);
- $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
- $this->timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock();
- $this->timeFactory->method('getTime')->willReturn(1496912528); // 2017-01-01
- $this->config = $this->createMock(IConfig::class);
- $this->userManager = $this->createMock(IUserManager::class);
- $l10n = $this->createMock(IL10N::class);
- $l10n->method('t')
- ->willReturnCallback(function($text, $parameters = []) {
- return vsprintf($text, $parameters);
- });
- $l10nFactory = $this->createMock(IFactory::class);
- $l10nFactory->method('get')->willReturn($l10n);
- $urlGenerator = $this->createMock(IURLGenerator::class);
- $this->queryBuilder = $this->createMock(IQueryBuilder::class);
- $db = $this->createMock(IDBConnection::class);
- $db->method('getQueryBuilder')
- ->with()
- ->willReturn($this->queryBuilder);
- $random = $this->createMock(ISecureRandom::class);
- $random->method('generate')
- ->with(60, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
- ->willReturn('random_token');
- $defaults = $this->createMock(Defaults::class);
- $defaults->method('getName')
- ->willReturn('Instance Name 123');
- $this->plugin = new IMipPlugin($this->config, $this->mailer, $logger, $this->timeFactory, $l10nFactory, $urlGenerator, $defaults, $random, $db, $this->userManager, 'user123');
- }
- public function testDelivery() {
- $this->config
- ->method('getAppValue')
- ->with('dav', 'invitation_link_recipients', 'yes')
- ->willReturn('yes');
- $message = $this->_testMessage();
- $this->_expectSend();
- $this->plugin->schedule($message);
- $this->assertEquals('1.1', $message->getScheduleStatus());
- }
- public function testFailedDelivery() {
- $this->config
- ->method('getAppValue')
- ->with('dav', 'invitation_link_recipients', 'yes')
- ->willReturn('yes');
- $message = $this->_testMessage();
- $this->mailer
- ->method('send')
- ->willThrowException(new \Exception());
- $this->_expectSend();
- $this->plugin->schedule($message);
- $this->assertEquals('5.0', $message->getScheduleStatus());
- }
- public function testDeliveryWithNoCommonName() {
- $this->config
- ->method('getAppValue')
- ->with('dav', 'invitation_link_recipients', 'yes')
- ->willReturn('yes');
- $message = $this->_testMessage();
- $message->senderName = null;
- $user = $this->createMock(IUser::class);
- $user->method('getDisplayName')->willReturn('Mr. Wizard');
- $this->userManager->expects($this->once())
- ->method('get')
- ->with('user123')
- ->willReturn($user);
- $this->_expectSend();
- $this->plugin->schedule($message);
- $this->assertEquals('1.1', $message->getScheduleStatus());
- }
- /**
- * @dataProvider dataNoMessageSendForPastEvents
- */
- public function testNoMessageSendForPastEvents($veventParams, $expectsMail) {
- $this->config
- ->method('getAppValue')
- ->with('dav', 'invitation_link_recipients', 'yes')
- ->willReturn('yes');
- $message = $this->_testMessage( $veventParams );
- $this->_expectSend('frodo@hobb.it', $expectsMail, $expectsMail);
- $this->plugin->schedule($message);
- if ($expectsMail) {
- $this->assertEquals('1.1', $message->getScheduleStatus());
- } else {
- $this->assertEquals(false, $message->getScheduleStatus());
- }
- }
- public function dataNoMessageSendForPastEvents() {
- return [
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00')], false],
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00')], false],
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-12-31 00:00:00')], true],
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P1D'], false],
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P52W'], true],
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY'], true],
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=3'], false],
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20170301T000000Z'], false],
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=33'], true],
- [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20171001T000000Z'], true],
- ];
- }
- /**
- * @dataProvider dataIncludeResponseButtons
- */
- public function testIncludeResponseButtons( $config_setting, $recipient, $has_buttons ) {
- $message = $this->_testMessage([],$recipient);
- $this->_expectSend($recipient, true, $has_buttons);
- $this->config
- ->method('getAppValue')
- ->with('dav', 'invitation_link_recipients', 'yes')
- ->willReturn($config_setting);
- $this->plugin->schedule($message);
- $this->assertEquals('1.1', $message->getScheduleStatus());
- }
- public function dataIncludeResponseButtons() {
- return [
- // dav.invitation_link_recipients, recipient, $has_buttons
- [ 'yes', 'joe@internal.com', true],
- [ 'joe@internal.com', 'joe@internal.com', true],
- [ 'internal.com', 'joe@internal.com', true],
- [ 'pete@otherinternal.com,internal.com', 'joe@internal.com', true],
- [ 'no', 'joe@internal.com', false],
- [ 'internal.com', 'joe@external.com', false],
- [ 'jane@otherinternal.com,internal.com', 'joe@otherinternal.com', false],
- ];
- }
- private function _testMessage($attrs = [], $recipient = 'frodo@hobb.it' ) {
- $message = new Message();
- $message->method = 'REQUEST';
- $message->message = new VCalendar();
- $message->message->add('VEVENT', array_merge([
- 'UID' => 'uid-1234',
- 'SEQUENCE' => 0,
- 'SUMMARY' => 'Fellowship meeting',
- 'DTSTART' => new \DateTime('2018-01-01 00:00:00')
- ], $attrs));
- $message->message->VEVENT->add( 'ORGANIZER', 'mailto:gandalf@wiz.ard' );
- $message->message->VEVENT->add( 'ATTENDEE', 'mailto:'.$recipient, [ 'RSVP' => 'TRUE' ] );
- $message->sender = 'mailto:gandalf@wiz.ard';
- $message->senderName = 'Mr. Wizard';
- $message->recipient = 'mailto:'.$recipient;
- return $message;
- }
- private function _expectSend( $recipient = 'frodo@hobb.it', $expectSend = true, $expectButtons = true ) {
- // if the event is in the past, we skip out
- if (!$expectSend) {
- $this->mailer
- ->expects($this->never())
- ->method('send');
- return;
- }
- $this->emailTemplate->expects($this->once())
- ->method('setSubject')
- ->with('Invitation: Fellowship meeting');
- $this->mailMessage->expects($this->once())
- ->method('setTo')
- ->with([$recipient => null]);
- $this->mailMessage->expects($this->once())
- ->method('setReplyTo')
- ->with(['gandalf@wiz.ard' => 'Mr. Wizard']);
- $this->mailMessage->expects($this->once())
- ->method('setFrom')
- ->with(['invitations-noreply@localhost' => 'Mr. Wizard via Instance Name 123']);
- $this->mailer
- ->expects($this->once())
- ->method('send');
- if ($expectButtons) {
- $this->queryBuilder->expects($this->at(0))
- ->method('insert')
- ->with('calendar_invitations')
- ->willReturn($this->queryBuilder);
- $this->queryBuilder->expects($this->at(8))
- ->method('values')
- ->willReturn($this->queryBuilder);
- $this->queryBuilder->expects($this->at(9))
- ->method('execute');
- }
- else {
- $this->queryBuilder->expects($this->never())
- ->method('insert')
- ->with('calendar_invitations');
- }
- }
- }
|