2015-12-18 13:56:25 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2017-06-08 12:50:17 +03:00
|
|
|
* @copyright Copyright (c) 2017, Georg Ehrke
|
2016-07-21 17:49:16 +03:00
|
|
|
*
|
2017-11-06 22:15:27 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-12-18 13:56:25 +03:00
|
|
|
* @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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\CalDAV\Schedule;
|
2015-12-18 13:56:25 +03:00
|
|
|
|
|
|
|
use OC\Mail\Mailer;
|
|
|
|
use OCA\DAV\CalDAV\Schedule\IMipPlugin;
|
2017-06-08 12:50:17 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2018-06-19 22:01:14 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2017-11-14 17:01:53 +03:00
|
|
|
use OCP\Defaults;
|
2017-11-02 18:38:16 +03:00
|
|
|
use OCP\IConfig;
|
2018-06-19 22:01:14 +03:00
|
|
|
use OCP\IDBConnection;
|
2017-11-02 18:38:16 +03:00
|
|
|
use OCP\IL10N;
|
2015-12-18 13:56:25 +03:00
|
|
|
use OCP\ILogger;
|
2017-11-02 18:38:16 +03:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\L10N\IFactory;
|
|
|
|
use OCP\Mail\IAttachment;
|
|
|
|
use OCP\Mail\IEMailTemplate;
|
|
|
|
use OCP\Mail\IMailer;
|
|
|
|
use OCP\Mail\IMessage;
|
2018-06-19 22:01:14 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2015-12-18 13:56:25 +03:00
|
|
|
use Sabre\VObject\Component\VCalendar;
|
|
|
|
use Sabre\VObject\ITip\Message;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class IMipPluginTest extends TestCase {
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
public function setUp(): void {
|
2019-02-28 04:25:27 +03:00
|
|
|
$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);
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
|
2019-02-28 04:25:27 +03:00
|
|
|
|
|
|
|
$this->timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock();
|
|
|
|
$this->timeFactory->method('getTime')->willReturn(1496912528); // 2017-01-01
|
|
|
|
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
|
|
|
2017-11-02 18:38:16 +03:00
|
|
|
$l10n = $this->createMock(IL10N::class);
|
|
|
|
$l10nFactory = $this->createMock(IFactory::class);
|
|
|
|
$l10nFactory->method('get')->willReturn($l10n);
|
2019-02-28 04:25:27 +03:00
|
|
|
|
2017-11-02 18:38:16 +03:00
|
|
|
$urlGenerator = $this->createMock(IURLGenerator::class);
|
2019-02-28 04:25:27 +03:00
|
|
|
|
|
|
|
$this->queryBuilder = $this->createMock(IQueryBuilder::class);
|
2018-06-19 22:01:14 +03:00
|
|
|
$db = $this->createMock(IDBConnection::class);
|
2019-02-28 04:25:27 +03:00
|
|
|
$db->method('getQueryBuilder')
|
|
|
|
->with()
|
|
|
|
->will($this->returnValue($this->queryBuilder));
|
2015-12-18 13:56:25 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
$random = $this->createMock(ISecureRandom::class);
|
|
|
|
$random->method('generate')
|
2018-06-19 22:01:14 +03:00
|
|
|
->with(60, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
|
|
|
|
->will($this->returnValue('random_token'));
|
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
$defaults = $this->createMock(Defaults::class);
|
|
|
|
$defaults->method('getName')
|
|
|
|
->will($this->returnValue('Instance Name 123'));
|
2019-02-28 04:22:53 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
$this->plugin = new IMipPlugin($this->config, $this->mailer, $logger, $this->timeFactory, $l10nFactory, $urlGenerator, $defaults, $random, $db, 'user123');
|
|
|
|
}
|
2015-12-18 13:56:25 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
public function testDelivery() {
|
|
|
|
$this->config
|
2019-07-18 02:47:15 +03:00
|
|
|
->method('getAppValue')
|
|
|
|
->with('dav', 'invitation_link_recipients', 'yes')
|
|
|
|
->willReturn('yes');
|
2017-11-02 18:38:16 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
$message = $this->_testMessage();
|
|
|
|
$this->_expectSend();
|
|
|
|
$this->plugin->schedule($message);
|
2015-12-18 13:56:25 +03:00
|
|
|
$this->assertEquals('1.1', $message->getScheduleStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailedDelivery() {
|
2019-02-28 04:25:27 +03:00
|
|
|
$this->config
|
2019-07-18 02:47:15 +03:00
|
|
|
->method('getAppValue')
|
|
|
|
->with('dav', 'invitation_link_recipients', 'yes')
|
|
|
|
->willReturn('yes');
|
2019-02-28 04:22:53 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
$message = $this->_testMessage();
|
|
|
|
$this->mailer
|
|
|
|
->method('send')
|
|
|
|
->willThrowException(new \Exception());
|
|
|
|
$this->_expectSend();
|
|
|
|
$this->plugin->schedule($message);
|
2015-12-18 13:56:25 +03:00
|
|
|
$this->assertEquals('5.0', $message->getScheduleStatus());
|
|
|
|
}
|
|
|
|
|
2017-06-08 12:50:17 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataNoMessageSendForPastEvents
|
|
|
|
*/
|
|
|
|
public function testNoMessageSendForPastEvents($veventParams, $expectsMail) {
|
2019-02-28 04:25:27 +03:00
|
|
|
|
|
|
|
$this->config
|
2019-07-18 02:47:15 +03:00
|
|
|
->method('getAppValue')
|
|
|
|
->with('dav', 'invitation_link_recipients', 'yes')
|
|
|
|
->willReturn('yes');
|
2017-06-08 12:50:17 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
$message = $this->_testMessage( $veventParams );
|
2018-06-19 22:01:14 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
$this->_expectSend('frodo@hobb.it', $expectsMail, $expectsMail);
|
2017-06-08 12:50:17 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
$this->plugin->schedule($message);
|
2017-06-08 12:50:17 +03:00
|
|
|
|
|
|
|
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],
|
2017-06-09 13:13:02 +03:00
|
|
|
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P1D'], false],
|
|
|
|
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P52W'], true],
|
2017-06-08 12:50:17 +03:00
|
|
|
[['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],
|
|
|
|
];
|
|
|
|
}
|
2019-02-28 04:22:53 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataIncludeResponseButtons
|
|
|
|
*/
|
|
|
|
public function testIncludeResponseButtons( $config_setting, $recipient, $has_buttons ) {
|
2019-02-28 04:25:27 +03:00
|
|
|
$message = $this->_testMessage([],$recipient);
|
|
|
|
|
|
|
|
$this->_expectSend($recipient, true, $has_buttons);
|
|
|
|
$this->config
|
2019-07-18 02:47:15 +03:00
|
|
|
->method('getAppValue')
|
|
|
|
->with('dav', 'invitation_link_recipients', 'yes')
|
2019-02-28 04:22:53 +03:00
|
|
|
->willReturn($config_setting);
|
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
$this->plugin->schedule($message);
|
|
|
|
$this->assertEquals('1.1', $message->getScheduleStatus());
|
|
|
|
}
|
2019-02-28 04:22:53 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
public function dataIncludeResponseButtons() {
|
|
|
|
return [
|
|
|
|
// dav.invitation_link_recipients, recipient, $has_buttons
|
2019-07-18 02:47:15 +03:00
|
|
|
[ 'yes', 'joe@internal.com', true],
|
2019-02-28 04:25:27 +03:00
|
|
|
[ 'joe@internal.com', 'joe@internal.com', true],
|
|
|
|
[ 'internal.com', 'joe@internal.com', true],
|
2019-07-18 02:47:15 +03:00
|
|
|
[ 'pete@otherinternal.com,internal.com', 'joe@internal.com', true],
|
|
|
|
[ 'no', 'joe@internal.com', false],
|
2019-02-28 04:25:27 +03:00
|
|
|
[ 'internal.com', 'joe@external.com', false],
|
2019-07-18 02:47:15 +03:00
|
|
|
[ 'jane@otherinternal.com,internal.com', 'joe@otherinternal.com', false],
|
2019-02-28 04:25:27 +03:00
|
|
|
];
|
|
|
|
}
|
2019-02-28 04:22:53 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
private function _testMessage($attrs = [], $recipient = 'frodo@hobb.it' ) {
|
2019-02-28 04:22:53 +03:00
|
|
|
$message = new Message();
|
|
|
|
$message->method = 'REQUEST';
|
|
|
|
$message->message = new VCalendar();
|
2019-02-28 04:25:27 +03:00
|
|
|
$message->message->add('VEVENT', array_merge([
|
|
|
|
'UID' => 'uid-1234',
|
|
|
|
'SEQUENCE' => 0,
|
2019-02-28 04:22:53 +03:00
|
|
|
'SUMMARY' => 'Fellowship meeting',
|
2019-02-28 04:25:27 +03:00
|
|
|
'DTSTART' => new \DateTime('2018-01-01 00:00:00')
|
|
|
|
], $attrs));
|
2019-02-28 04:22:53 +03:00
|
|
|
$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->recipient = 'mailto:'.$recipient;
|
2019-02-28 04:25:27 +03:00
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2019-02-28 04:22:53 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
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())
|
2019-02-28 04:22:53 +03:00
|
|
|
->method('setSubject')
|
|
|
|
->with('Invitation: Fellowship meeting');
|
2019-02-28 04:25:27 +03:00
|
|
|
$this->mailMessage->expects($this->once())
|
2019-02-28 04:22:53 +03:00
|
|
|
->method('setTo')
|
|
|
|
->with([$recipient => null]);
|
2019-02-28 04:25:27 +03:00
|
|
|
$this->mailMessage->expects($this->once())
|
2019-02-28 04:22:53 +03:00
|
|
|
->method('setReplyTo')
|
|
|
|
->with(['gandalf@wiz.ard' => null]);
|
2019-02-28 04:25:27 +03:00
|
|
|
$this->mailer
|
|
|
|
->expects($this->once())
|
|
|
|
->method('send');
|
2019-02-28 04:22:53 +03:00
|
|
|
|
2019-02-28 04:25:27 +03:00
|
|
|
if ($expectButtons) {
|
|
|
|
$this->queryBuilder->expects($this->at(0))
|
|
|
|
->method('insert')
|
|
|
|
->with('calendar_invitations')
|
|
|
|
->will($this->returnValue($this->queryBuilder));
|
|
|
|
$this->queryBuilder->expects($this->at(8))
|
|
|
|
->method('values')
|
|
|
|
->will($this->returnValue($this->queryBuilder));
|
|
|
|
$this->queryBuilder->expects($this->at(9))
|
|
|
|
->method('execute');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->queryBuilder->expects($this->never())
|
|
|
|
->method('insert')
|
|
|
|
->with('calendar_invitations');
|
|
|
|
}
|
2019-02-28 04:22:53 +03:00
|
|
|
}
|
2015-12-18 13:56:25 +03:00
|
|
|
}
|