123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- * @copyright Copyright (c) 2017, Georg Ehrke
- *
- * @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\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 {
- public function testDelivery() {
- $mailMessage = $this->createMock(IMessage::class);
- $mailMessage->method('setFrom')->willReturn($mailMessage);
- $mailMessage->method('setReplyTo')->willReturn($mailMessage);
- $mailMessage->method('setTo')->willReturn($mailMessage);
- /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */
- $mailer = $this->getMockBuilder(IMailer::class)->disableOriginalConstructor()->getMock();
- $emailTemplate = $this->createMock(IEMailTemplate::class);
- $emailAttachment = $this->createMock(IAttachment::class);
- $mailer->method('createEMailTemplate')->willReturn($emailTemplate);
- $mailer->method('createMessage')->willReturn($mailMessage);
- $mailer->method('createAttachment')->willReturn($emailAttachment);
- $mailer->expects($this->once())->method('send');
- /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */
- $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
- $timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock();
- $timeFactory->method('getTime')->willReturn(1);
- /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */
- $config = $this->createMock(IConfig::class);
- $l10n = $this->createMock(IL10N::class);
- /** @var IFactory | \PHPUnit_Framework_MockObject_MockObject $l10nFactory */
- $l10nFactory = $this->createMock(IFactory::class);
- $l10nFactory->method('get')->willReturn($l10n);
- /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject $urlGenerator */
- $urlGenerator = $this->createMock(IURLGenerator::class);
- /** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject $db */
- $db = $this->createMock(IDBConnection::class);
- /** @var ISecureRandom | \PHPUnit_Framework_MockObject_MockObject $random */
- $random = $this->createMock(ISecureRandom::class);
- /** @var Defaults | \PHPUnit_Framework_MockObject_MockObject $defaults */
- $defaults = $this->createMock(Defaults::class);
- $defaults->expects($this->once())
- ->method('getName')
- ->will($this->returnValue('Instance Name 123'));
- $random->expects($this->once())
- ->method('generate')
- ->with(60, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
- ->will($this->returnValue('random_token'));
- $queryBuilder = $this->createMock(IQueryBuilder::class);
- $db->expects($this->once())
- ->method('getQueryBuilder')
- ->with()
- ->will($this->returnValue($queryBuilder));
- $queryBuilder->expects($this->at(0))
- ->method('insert')
- ->with('calendar_invitations')
- ->will($this->returnValue($queryBuilder));
- $queryBuilder->expects($this->at(8))
- ->method('values')
- ->will($this->returnValue($queryBuilder));
- $queryBuilder->expects($this->at(9))
- ->method('execute');
- $plugin = new IMipPlugin($config, $mailer, $logger, $timeFactory, $l10nFactory, $urlGenerator, $defaults, $random, $db, 'user123');
- $message = new Message();
- $message->method = 'REQUEST';
- $message->message = new VCalendar();
- $message->message->add('VEVENT', [
- 'UID' => $message->uid,
- 'SEQUENCE' => $message->sequence,
- 'SUMMARY' => 'Fellowship meeting',
- 'DTSTART' => new \DateTime('2017-01-01 00:00:00') // 1483228800
- ]);
- $message->sender = 'mailto:gandalf@wiz.ard';
- $message->recipient = 'mailto:frodo@hobb.it';
- $emailTemplate->expects($this->once())
- ->method('setSubject')
- ->with('Invitation: Fellowship meeting');
- $mailMessage->expects($this->once())
- ->method('setTo')
- ->with(['frodo@hobb.it' => null]);
- $mailMessage->expects($this->once())
- ->method('setReplyTo')
- ->with(['gandalf@wiz.ard' => null]);
- $plugin->schedule($message);
- $this->assertEquals('1.1', $message->getScheduleStatus());
- }
- public function testFailedDelivery() {
- $mailMessage = $this->createMock(IMessage::class);
- $mailMessage->method('setFrom')->willReturn($mailMessage);
- $mailMessage->method('setReplyTo')->willReturn($mailMessage);
- $mailMessage->method('setTo')->willReturn($mailMessage);
- /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */
- $mailer = $this->getMockBuilder(IMailer::class)->disableOriginalConstructor()->getMock();
- $emailTemplate = $this->createMock(IEMailTemplate::class);
- $emailAttachment = $this->createMock(IAttachment::class);
- $mailer->method('createEMailTemplate')->willReturn($emailTemplate);
- $mailer->method('createMessage')->willReturn($mailMessage);
- $mailer->method('createAttachment')->willReturn($emailAttachment);
- $mailer->method('send')->willThrowException(new \Exception());
- /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */
- $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
- $timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock();
- $timeFactory->method('getTime')->willReturn(1);
- /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */
- $config = $this->createMock(IConfig::class);
- $l10n = $this->createMock(IL10N::class);
- /** @var IFactory | \PHPUnit_Framework_MockObject_MockObject $l10nFactory */
- $l10nFactory = $this->createMock(IFactory::class);
- $l10nFactory->method('get')->willReturn($l10n);
- /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject $urlGenerator */
- $urlGenerator = $this->createMock(IURLGenerator::class);
- /** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject $db */
- $db = $this->createMock(IDBConnection::class);
- /** @var ISecureRandom | \PHPUnit_Framework_MockObject_MockObject $random */
- $random = $this->createMock(ISecureRandom::class);
- /** @var Defaults | \PHPUnit_Framework_MockObject_MockObject $defaults */
- $defaults = $this->createMock(Defaults::class);
- $random->expects($this->once())
- ->method('generate')
- ->with(60, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
- ->will($this->returnValue('random_token'));
- $queryBuilder = $this->createMock(IQueryBuilder::class);
- $db->expects($this->once())
- ->method('getQueryBuilder')
- ->with()
- ->will($this->returnValue($queryBuilder));
- $queryBuilder->expects($this->at(0))
- ->method('insert')
- ->with('calendar_invitations')
- ->will($this->returnValue($queryBuilder));
- $queryBuilder->expects($this->at(8))
- ->method('values')
- ->will($this->returnValue($queryBuilder));
- $queryBuilder->expects($this->at(9))
- ->method('execute');
- $plugin = new IMipPlugin($config, $mailer, $logger, $timeFactory, $l10nFactory, $urlGenerator, $defaults, $random, $db, 'user123');
- $message = new Message();
- $message->method = 'REQUEST';
- $message->message = new VCalendar();
- $message->message->add('VEVENT', [
- 'UID' => $message->uid,
- 'SEQUENCE' => $message->sequence,
- 'SUMMARY' => 'Fellowship meeting',
- 'DTSTART' => new \DateTime('2017-01-01 00:00:00') // 1483228800
- ]);
- $message->sender = 'mailto:gandalf@wiz.ard';
- $message->recipient = 'mailto:frodo@hobb.it';
- $emailTemplate->expects($this->once())
- ->method('setSubject')
- ->with('Invitation: Fellowship meeting');
- $mailMessage->expects($this->once())
- ->method('setTo')
- ->with(['frodo@hobb.it' => null]);
- $mailMessage->expects($this->once())
- ->method('setReplyTo')
- ->with(['gandalf@wiz.ard' => null]);
- $plugin->schedule($message);
- $this->assertEquals('5.0', $message->getScheduleStatus());
- }
- /**
- * @dataProvider dataNoMessageSendForPastEvents
- */
- public function testNoMessageSendForPastEvents($veventParams, $expectsMail) {
- $mailMessage = $this->createMock(IMessage::class);
- $mailMessage->method('setFrom')->willReturn($mailMessage);
- $mailMessage->method('setReplyTo')->willReturn($mailMessage);
- $mailMessage->method('setTo')->willReturn($mailMessage);
- /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */
- $mailer = $this->getMockBuilder(IMailer::class)->disableOriginalConstructor()->getMock();
- $emailTemplate = $this->createMock(IEMailTemplate::class);
- $emailAttachment = $this->createMock(IAttachment::class);
- $mailer->method('createEMailTemplate')->willReturn($emailTemplate);
- $mailer->method('createMessage')->willReturn($mailMessage);
- $mailer->method('createAttachment')->willReturn($emailAttachment);
- if ($expectsMail) {
- $mailer->expects($this->once())->method('send');
- } else {
- $mailer->expects($this->never())->method('send');
- }
- /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */
- $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
- $timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock();
- $timeFactory->method('getTime')->willReturn(1496912528);
- /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */
- $config = $this->createMock(IConfig::class);
- $l10n = $this->createMock(IL10N::class);
- /** @var IFactory | \PHPUnit_Framework_MockObject_MockObject $l10nFactory */
- $l10nFactory = $this->createMock(IFactory::class);
- $l10nFactory->method('get')->willReturn($l10n);
- /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject $urlGenerator */
- $urlGenerator = $this->createMock(IURLGenerator::class);
- /** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject $db */
- $db = $this->createMock(IDBConnection::class);
- /** @var ISecureRandom | \PHPUnit_Framework_MockObject_MockObject $random */
- $random = $this->createMock(ISecureRandom::class);
- /** @var Defaults | \PHPUnit_Framework_MockObject_MockObject $defaults */
- $defaults = $this->createMock(Defaults::class);
- if ($expectsMail) {
- $random->expects($this->once())
- ->method('generate')
- ->with(60, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
- ->will($this->returnValue('random_token'));
- $queryBuilder = $this->createMock(IQueryBuilder::class);
- $db->expects($this->once())
- ->method('getQueryBuilder')
- ->with()
- ->will($this->returnValue($queryBuilder));
- $queryBuilder->expects($this->at(0))
- ->method('insert')
- ->with('calendar_invitations')
- ->will($this->returnValue($queryBuilder));
- $queryBuilder->expects($this->at(8))
- ->method('values')
- ->will($this->returnValue($queryBuilder));
- $queryBuilder->expects($this->at(9))
- ->method('execute');
- }
- $plugin = new IMipPlugin($config, $mailer, $logger, $timeFactory, $l10nFactory, $urlGenerator, $defaults, $random, $db, 'user123');
- $message = new Message();
- $message->method = 'REQUEST';
- $message->message = new VCalendar();
- $message->message->add('VEVENT', array_merge([
- 'UID' => 'uid1337',
- 'SEQUENCE' => 42,
- 'SUMMARY' => 'Fellowship meeting',
- ], $veventParams));
- $message->sender = 'mailto:gandalf@wiz.ard';
- $message->recipient = 'mailto:frodo@hobb.it';
- $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],
- ];
- }
- }
|