IMipPluginTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017, Georg Ehrke
  5. *
  6. * @author brad2014 <brad2014@users.noreply.github.com>
  7. * @author Brad Rubenstein <brad@wbr.tech>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Thomas Citharel <nextcloud@tcit.fr>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\DAV\Tests\unit\CalDAV\Schedule;
  31. use OCA\DAV\CalDAV\EventComparisonService;
  32. use OCA\DAV\CalDAV\Schedule\IMipPlugin;
  33. use OCA\DAV\CalDAV\Schedule\IMipService;
  34. use OCP\AppFramework\Utility\ITimeFactory;
  35. use OCP\Defaults;
  36. use OCP\IConfig;
  37. use OCP\IUserManager;
  38. use OCP\Mail\IAttachment;
  39. use OCP\Mail\IEMailTemplate;
  40. use OCP\Mail\IMailer;
  41. use OCP\Mail\IMessage;
  42. use PHPUnit\Framework\MockObject\MockObject;
  43. use Psr\Log\LoggerInterface;
  44. use Sabre\VObject\Component\VCalendar;
  45. use Sabre\VObject\Component\VEvent;
  46. use Sabre\VObject\ITip\Message;
  47. use Test\TestCase;
  48. use function array_merge;
  49. class IMipPluginTest extends TestCase {
  50. /** @var IMessage|MockObject */
  51. private $mailMessage;
  52. /** @var IMailer|MockObject */
  53. private $mailer;
  54. /** @var IEMailTemplate|MockObject */
  55. private $emailTemplate;
  56. /** @var IAttachment|MockObject */
  57. private $emailAttachment;
  58. /** @var ITimeFactory|MockObject */
  59. private $timeFactory;
  60. /** @var IConfig|MockObject */
  61. private $config;
  62. /** @var IUserManager|MockObject */
  63. private $userManager;
  64. /** @var IMipPlugin */
  65. private $plugin;
  66. /** @var IMipService|MockObject */
  67. private $service;
  68. /** @var Defaults|MockObject */
  69. private $defaults;
  70. /** @var LoggerInterface|MockObject */
  71. private $logger;
  72. /** @var EventComparisonService|MockObject */
  73. private $eventComparisonService;
  74. protected function setUp(): void {
  75. $this->mailMessage = $this->createMock(IMessage::class);
  76. $this->mailMessage->method('setFrom')->willReturn($this->mailMessage);
  77. $this->mailMessage->method('setReplyTo')->willReturn($this->mailMessage);
  78. $this->mailMessage->method('setTo')->willReturn($this->mailMessage);
  79. $this->mailer = $this->createMock(IMailer::class);
  80. $this->mailer->method('createMessage')->willReturn($this->mailMessage);
  81. $this->emailTemplate = $this->createMock(IEMailTemplate::class);
  82. $this->mailer->method('createEMailTemplate')->willReturn($this->emailTemplate);
  83. $this->emailAttachment = $this->createMock(IAttachment::class);
  84. $this->mailer->method('createAttachment')->willReturn($this->emailAttachment);
  85. $this->logger = $this->createMock(LoggerInterface::class);
  86. $this->timeFactory = $this->createMock(ITimeFactory::class);
  87. $this->timeFactory->method('getTime')->willReturn(1496912528); // 2017-01-01
  88. $this->config = $this->createMock(IConfig::class);
  89. $this->userManager = $this->createMock(IUserManager::class);
  90. $this->defaults = $this->createMock(Defaults::class);
  91. $this->defaults->method('getName')
  92. ->willReturn('Instance Name 123');
  93. $this->service = $this->createMock(IMipService::class);
  94. $this->eventComparisonService = $this->createMock(EventComparisonService::class);
  95. $this->plugin = new IMipPlugin(
  96. $this->config,
  97. $this->mailer,
  98. $this->logger,
  99. $this->timeFactory,
  100. $this->defaults,
  101. $this->userManager,
  102. 'user123',
  103. $this->service,
  104. $this->eventComparisonService
  105. );
  106. }
  107. public function testDeliveryNoSignificantChange(): void {
  108. $message = new Message();
  109. $message->method = 'REQUEST';
  110. $message->message = new VCalendar();
  111. $message->message->add('VEVENT', array_merge([
  112. 'UID' => 'uid-1234',
  113. 'SEQUENCE' => 0,
  114. 'SUMMARY' => 'Fellowship meeting',
  115. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  116. ], []));
  117. $message->message->VEVENT->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  118. $message->message->VEVENT->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE']);
  119. $message->sender = 'mailto:gandalf@wiz.ard';
  120. $message->senderName = 'Mr. Wizard';
  121. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  122. $message->significantChange = false;
  123. $this->plugin->schedule($message);
  124. $this->assertEquals('1.0', $message->getScheduleStatus());
  125. }
  126. public function testParsingSingle(): void {
  127. $message = new Message();
  128. $message->method = 'REQUEST';
  129. $newVCalendar = new VCalendar();
  130. $newVevent = new VEvent($newVCalendar, 'one', array_merge([
  131. 'UID' => 'uid-1234',
  132. 'SEQUENCE' => 1,
  133. 'SUMMARY' => 'Fellowship meeting without (!) Boromir',
  134. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  135. ], []));
  136. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  137. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  138. $message->message = $newVCalendar;
  139. $message->sender = 'mailto:gandalf@wiz.ard';
  140. $message->senderName = 'Mr. Wizard';
  141. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  142. // save the old copy in the plugin
  143. $oldVCalendar = new VCalendar();
  144. $oldVEvent = new VEvent($oldVCalendar, 'one', [
  145. 'UID' => 'uid-1234',
  146. 'SEQUENCE' => 0,
  147. 'SUMMARY' => 'Fellowship meeting',
  148. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  149. ]);
  150. $oldVEvent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  151. $oldVEvent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  152. $oldVEvent->add('ATTENDEE', 'mailto:' . 'boromir@tra.it.or', ['RSVP' => 'TRUE']);
  153. $oldVCalendar->add($oldVEvent);
  154. $data = ['invitee_name' => 'Mr. Wizard',
  155. 'meeting_title' => 'Fellowship meeting without (!) Boromir',
  156. 'attendee_name' => 'frodo@hobb.it'
  157. ];
  158. $attendees = $newVevent->select('ATTENDEE');
  159. $atnd = '';
  160. foreach ($attendees as $attendee) {
  161. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  162. $atnd = $attendee;
  163. }
  164. }
  165. $this->plugin->setVCalendar($oldVCalendar);
  166. $this->service->expects(self::once())
  167. ->method('getLastOccurrence')
  168. ->willReturn('1496912700');
  169. $this->mailer->expects(self::once())
  170. ->method('validateMailAddress')
  171. ->with('frodo@hobb.it')
  172. ->willReturn(true);
  173. $this->eventComparisonService->expects(self::once())
  174. ->method('findModified')
  175. ->willReturn(['new' => [$newVevent], 'old' => [$oldVEvent]]);
  176. $this->service->expects(self::once())
  177. ->method('getCurrentAttendee')
  178. ->with($message)
  179. ->willReturn($atnd);
  180. $this->service->expects(self::once())
  181. ->method('isRoomOrResource')
  182. ->with($atnd)
  183. ->willReturn(false);
  184. $this->service->expects(self::once())
  185. ->method('buildBodyData')
  186. ->with($newVevent, $oldVEvent)
  187. ->willReturn($data);
  188. $this->userManager->expects(self::never())
  189. ->method('getDisplayName');
  190. $this->service->expects(self::once())
  191. ->method('getFrom');
  192. $this->service->expects(self::once())
  193. ->method('addSubjectAndHeading')
  194. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting without (!) Boromir', true);
  195. $this->service->expects(self::once())
  196. ->method('addBulletList')
  197. ->with($this->emailTemplate, $newVevent, $data);
  198. $this->service->expects(self::once())
  199. ->method('getAttendeeRsvpOrReqForParticipant')
  200. ->willReturn(true);
  201. $this->config->expects(self::once())
  202. ->method('getAppValue')
  203. ->with('dav', 'invitation_link_recipients', 'yes')
  204. ->willReturn('yes');
  205. $this->service->expects(self::once())
  206. ->method('createInvitationToken')
  207. ->with($message, $newVevent, '1496912700')
  208. ->willReturn('token');
  209. $this->service->expects(self::once())
  210. ->method('addResponseButtons')
  211. ->with($this->emailTemplate, 'token');
  212. $this->service->expects(self::once())
  213. ->method('addMoreOptionsButton')
  214. ->with($this->emailTemplate, 'token');
  215. $this->mailer->expects(self::once())
  216. ->method('send')
  217. ->willReturn([]);
  218. $this->plugin->schedule($message);
  219. $this->assertEquals('1.1', $message->getScheduleStatus());
  220. }
  221. public function testAttendeeIsResource(): void {
  222. $message = new Message();
  223. $message->method = 'REQUEST';
  224. $newVCalendar = new VCalendar();
  225. $newVevent = new VEvent($newVCalendar, 'one', array_merge([
  226. 'UID' => 'uid-1234',
  227. 'SEQUENCE' => 1,
  228. 'SUMMARY' => 'Fellowship meeting without (!) Boromir',
  229. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  230. ], []));
  231. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  232. $newVevent->add('ATTENDEE', 'mailto:' . 'the-shire@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'The Shire', 'CUTYPE' => 'ROOM']);
  233. $message->message = $newVCalendar;
  234. $message->sender = 'mailto:gandalf@wiz.ard';
  235. $message->senderName = 'Mr. Wizard';
  236. $message->recipient = 'mailto:' . 'the-shire@hobb.it';
  237. // save the old copy in the plugin
  238. $oldVCalendar = new VCalendar();
  239. $oldVEvent = new VEvent($oldVCalendar, 'one', [
  240. 'UID' => 'uid-1234',
  241. 'SEQUENCE' => 0,
  242. 'SUMMARY' => 'Fellowship meeting',
  243. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  244. ]);
  245. $oldVEvent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  246. $oldVEvent->add('ATTENDEE', 'mailto:' . 'the-shire@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'The Shire', 'CUTYPE' => 'ROOM']);
  247. $oldVEvent->add('ATTENDEE', 'mailto:' . 'boromir@tra.it.or', ['RSVP' => 'TRUE']);
  248. $oldVCalendar->add($oldVEvent);
  249. $data = ['invitee_name' => 'Mr. Wizard',
  250. 'meeting_title' => 'Fellowship meeting without (!) Boromir',
  251. 'attendee_name' => 'frodo@hobb.it'
  252. ];
  253. $attendees = $newVevent->select('ATTENDEE');
  254. $room = '';
  255. foreach ($attendees as $attendee) {
  256. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  257. $room = $attendee;
  258. }
  259. }
  260. $this->plugin->setVCalendar($oldVCalendar);
  261. $this->service->expects(self::once())
  262. ->method('getLastOccurrence')
  263. ->willReturn('1496912700');
  264. $this->mailer->expects(self::once())
  265. ->method('validateMailAddress')
  266. ->with('the-shire@hobb.it')
  267. ->willReturn(true);
  268. $this->eventComparisonService->expects(self::once())
  269. ->method('findModified')
  270. ->willReturn(['new' => [$newVevent], 'old' => [$oldVEvent]]);
  271. $this->service->expects(self::once())
  272. ->method('getCurrentAttendee')
  273. ->with($message)
  274. ->willReturn($room);
  275. $this->service->expects(self::once())
  276. ->method('isRoomOrResource')
  277. ->with($room)
  278. ->willReturn(true);
  279. $this->service->expects(self::never())
  280. ->method('buildBodyData');
  281. $this->userManager->expects(self::never())
  282. ->method('getDisplayName');
  283. $this->service->expects(self::never())
  284. ->method('getFrom');
  285. $this->service->expects(self::never())
  286. ->method('addSubjectAndHeading');
  287. $this->service->expects(self::never())
  288. ->method('addBulletList');
  289. $this->service->expects(self::never())
  290. ->method('getAttendeeRsvpOrReqForParticipant');
  291. $this->config->expects(self::never())
  292. ->method('getAppValue');
  293. $this->service->expects(self::never())
  294. ->method('createInvitationToken');
  295. $this->service->expects(self::never())
  296. ->method('addResponseButtons');
  297. $this->service->expects(self::never())
  298. ->method('addMoreOptionsButton');
  299. $this->mailer->expects(self::never())
  300. ->method('send');
  301. $this->plugin->schedule($message);
  302. $this->assertEquals('1.0', $message->getScheduleStatus());
  303. }
  304. public function testParsingRecurrence(): void {
  305. $message = new Message();
  306. $message->method = 'REQUEST';
  307. $newVCalendar = new VCalendar();
  308. $newVevent = new VEvent($newVCalendar, 'one', [
  309. 'UID' => 'uid-1234',
  310. 'LAST-MODIFIED' => 123456,
  311. 'SEQUENCE' => 2,
  312. 'SUMMARY' => 'Fellowship meeting',
  313. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  314. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z'
  315. ]);
  316. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  317. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  318. $newvEvent2 = new VEvent($newVCalendar, 'two', [
  319. 'UID' => 'uid-1234',
  320. 'SEQUENCE' => 1,
  321. 'SUMMARY' => 'Elevenses',
  322. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  323. 'RECURRENCE-ID' => new \DateTime('2016-01-01 00:00:00')
  324. ]);
  325. $newvEvent2->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  326. $newvEvent2->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  327. $message->message = $newVCalendar;
  328. $message->sender = 'mailto:gandalf@wiz.ard';
  329. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  330. // save the old copy in the plugin
  331. $oldVCalendar = new VCalendar();
  332. $oldVEvent = new VEvent($oldVCalendar, 'one', [
  333. 'UID' => 'uid-1234',
  334. 'LAST-MODIFIED' => 123456,
  335. 'SEQUENCE' => 2,
  336. 'SUMMARY' => 'Fellowship meeting',
  337. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  338. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z'
  339. ]);
  340. $oldVEvent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  341. $oldVEvent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  342. $data = ['invitee_name' => 'Mr. Wizard',
  343. 'meeting_title' => 'Elevenses',
  344. 'attendee_name' => 'frodo@hobb.it'
  345. ];
  346. $attendees = $newVevent->select('ATTENDEE');
  347. $atnd = '';
  348. foreach ($attendees as $attendee) {
  349. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  350. $atnd = $attendee;
  351. }
  352. }
  353. $this->plugin->setVCalendar($oldVCalendar);
  354. $this->service->expects(self::once())
  355. ->method('getLastOccurrence')
  356. ->willReturn('1496912700');
  357. $this->mailer->expects(self::once())
  358. ->method('validateMailAddress')
  359. ->with('frodo@hobb.it')
  360. ->willReturn(true);
  361. $this->eventComparisonService->expects(self::once())
  362. ->method('findModified')
  363. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  364. $this->service->expects(self::once())
  365. ->method('getCurrentAttendee')
  366. ->with($message)
  367. ->willReturn($atnd);
  368. $this->service->expects(self::once())
  369. ->method('isRoomOrResource')
  370. ->with($atnd)
  371. ->willReturn(false);
  372. $this->service->expects(self::once())
  373. ->method('buildBodyData')
  374. ->with($newVevent, null)
  375. ->willReturn($data);
  376. $this->userManager->expects(self::once())
  377. ->method('getDisplayName')
  378. ->willReturn('Mr. Wizard');
  379. $this->service->expects(self::once())
  380. ->method('getFrom');
  381. $this->service->expects(self::once())
  382. ->method('addSubjectAndHeading')
  383. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Elevenses', false);
  384. $this->service->expects(self::once())
  385. ->method('addBulletList')
  386. ->with($this->emailTemplate, $newVevent, $data);
  387. $this->service->expects(self::once())
  388. ->method('getAttendeeRsvpOrReqForParticipant')
  389. ->willReturn(true);
  390. $this->config->expects(self::once())
  391. ->method('getAppValue')
  392. ->with('dav', 'invitation_link_recipients', 'yes')
  393. ->willReturn('yes');
  394. $this->service->expects(self::once())
  395. ->method('createInvitationToken')
  396. ->with($message, $newVevent, '1496912700')
  397. ->willReturn('token');
  398. $this->service->expects(self::once())
  399. ->method('addResponseButtons')
  400. ->with($this->emailTemplate, 'token');
  401. $this->service->expects(self::once())
  402. ->method('addMoreOptionsButton')
  403. ->with($this->emailTemplate, 'token');
  404. $this->mailer->expects(self::once())
  405. ->method('send')
  406. ->willReturn([]);
  407. $this->plugin->schedule($message);
  408. $this->assertEquals('1.1', $message->getScheduleStatus());
  409. }
  410. public function testEmailValidationFailed() {
  411. $message = new Message();
  412. $message->method = 'REQUEST';
  413. $message->message = new VCalendar();
  414. $message->message->add('VEVENT', array_merge([
  415. 'UID' => 'uid-1234',
  416. 'SEQUENCE' => 0,
  417. 'SUMMARY' => 'Fellowship meeting',
  418. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  419. ], []));
  420. $message->message->VEVENT->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  421. $message->message->VEVENT->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE']);
  422. $message->sender = 'mailto:gandalf@wiz.ard';
  423. $message->senderName = 'Mr. Wizard';
  424. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  425. $this->service->expects(self::once())
  426. ->method('getLastOccurrence')
  427. ->willReturn('1496912700');
  428. $this->mailer->expects(self::once())
  429. ->method('validateMailAddress')
  430. ->with('frodo@hobb.it')
  431. ->willReturn(false);
  432. $this->plugin->schedule($message);
  433. $this->assertEquals('5.0', $message->getScheduleStatus());
  434. }
  435. public function testFailedDelivery(): void {
  436. $message = new Message();
  437. $message->method = 'REQUEST';
  438. $newVcalendar = new VCalendar();
  439. $newVevent = new VEvent($newVcalendar, 'one', array_merge([
  440. 'UID' => 'uid-1234',
  441. 'SEQUENCE' => 1,
  442. 'SUMMARY' => 'Fellowship meeting without (!) Boromir',
  443. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  444. ], []));
  445. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  446. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  447. $message->message = $newVcalendar;
  448. $message->sender = 'mailto:gandalf@wiz.ard';
  449. $message->senderName = 'Mr. Wizard';
  450. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  451. // save the old copy in the plugin
  452. $oldVcalendar = new VCalendar();
  453. $oldVevent = new VEvent($oldVcalendar, 'one', [
  454. 'UID' => 'uid-1234',
  455. 'SEQUENCE' => 0,
  456. 'SUMMARY' => 'Fellowship meeting',
  457. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  458. ]);
  459. $oldVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  460. $oldVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  461. $oldVevent->add('ATTENDEE', 'mailto:' . 'boromir@tra.it.or', ['RSVP' => 'TRUE']);
  462. $oldVcalendar->add($oldVevent);
  463. $data = ['invitee_name' => 'Mr. Wizard',
  464. 'meeting_title' => 'Fellowship meeting without (!) Boromir',
  465. 'attendee_name' => 'frodo@hobb.it'
  466. ];
  467. $attendees = $newVevent->select('ATTENDEE');
  468. $atnd = '';
  469. foreach ($attendees as $attendee) {
  470. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  471. $atnd = $attendee;
  472. }
  473. }
  474. $this->plugin->setVCalendar($oldVcalendar);
  475. $this->service->expects(self::once())
  476. ->method('getLastOccurrence')
  477. ->willReturn('1496912700');
  478. $this->mailer->expects(self::once())
  479. ->method('validateMailAddress')
  480. ->with('frodo@hobb.it')
  481. ->willReturn(true);
  482. $this->eventComparisonService->expects(self::once())
  483. ->method('findModified')
  484. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  485. $this->service->expects(self::once())
  486. ->method('getCurrentAttendee')
  487. ->with($message)
  488. ->willReturn($atnd);
  489. $this->service->expects(self::once())
  490. ->method('isRoomOrResource')
  491. ->with($atnd)
  492. ->willReturn(false);
  493. $this->service->expects(self::once())
  494. ->method('buildBodyData')
  495. ->with($newVevent, null)
  496. ->willReturn($data);
  497. $this->userManager->expects(self::never())
  498. ->method('getDisplayName');
  499. $this->service->expects(self::once())
  500. ->method('getFrom');
  501. $this->service->expects(self::once())
  502. ->method('addSubjectAndHeading')
  503. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting without (!) Boromir', false);
  504. $this->service->expects(self::once())
  505. ->method('addBulletList')
  506. ->with($this->emailTemplate, $newVevent, $data);
  507. $this->service->expects(self::once())
  508. ->method('getAttendeeRsvpOrReqForParticipant')
  509. ->willReturn(true);
  510. $this->config->expects(self::once())
  511. ->method('getAppValue')
  512. ->with('dav', 'invitation_link_recipients', 'yes')
  513. ->willReturn('yes');
  514. $this->service->expects(self::once())
  515. ->method('createInvitationToken')
  516. ->with($message, $newVevent, '1496912700')
  517. ->willReturn('token');
  518. $this->service->expects(self::once())
  519. ->method('addResponseButtons')
  520. ->with($this->emailTemplate, 'token');
  521. $this->service->expects(self::once())
  522. ->method('addMoreOptionsButton')
  523. ->with($this->emailTemplate, 'token');
  524. $this->mailer->expects(self::once())
  525. ->method('send')
  526. ->willReturn([]);
  527. $this->mailer
  528. ->method('send')
  529. ->willThrowException(new \Exception());
  530. $this->logger->expects(self::once())
  531. ->method('error');
  532. $this->plugin->schedule($message);
  533. $this->assertEquals('5.0', $message->getScheduleStatus());
  534. }
  535. public function testNoOldEvent(): void {
  536. $message = new Message();
  537. $message->method = 'REQUEST';
  538. $newVCalendar = new VCalendar();
  539. $newVevent = new VEvent($newVCalendar, 'VEVENT', array_merge([
  540. 'UID' => 'uid-1234',
  541. 'SEQUENCE' => 1,
  542. 'SUMMARY' => 'Fellowship meeting',
  543. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  544. ], []));
  545. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  546. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  547. $message->message = $newVCalendar;
  548. $message->sender = 'mailto:gandalf@wiz.ard';
  549. $message->senderName = 'Mr. Wizard';
  550. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  551. $data = ['invitee_name' => 'Mr. Wizard',
  552. 'meeting_title' => 'Fellowship meeting',
  553. 'attendee_name' => 'frodo@hobb.it'
  554. ];
  555. $attendees = $newVevent->select('ATTENDEE');
  556. $atnd = '';
  557. foreach ($attendees as $attendee) {
  558. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  559. $atnd = $attendee;
  560. }
  561. }
  562. $this->service->expects(self::once())
  563. ->method('getLastOccurrence')
  564. ->willReturn('1496912700');
  565. $this->mailer->expects(self::once())
  566. ->method('validateMailAddress')
  567. ->with('frodo@hobb.it')
  568. ->willReturn(true);
  569. $this->eventComparisonService->expects(self::once())
  570. ->method('findModified')
  571. ->with($newVCalendar, null)
  572. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  573. $this->service->expects(self::once())
  574. ->method('getCurrentAttendee')
  575. ->with($message)
  576. ->willReturn($atnd);
  577. $this->service->expects(self::once())
  578. ->method('isRoomOrResource')
  579. ->with($atnd)
  580. ->willReturn(false);
  581. $this->service->expects(self::once())
  582. ->method('buildBodyData')
  583. ->with($newVevent, null)
  584. ->willReturn($data);
  585. $this->userManager->expects(self::never())
  586. ->method('getDisplayName');
  587. $this->service->expects(self::once())
  588. ->method('getFrom');
  589. $this->service->expects(self::once())
  590. ->method('addSubjectAndHeading')
  591. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting', false);
  592. $this->service->expects(self::once())
  593. ->method('addBulletList')
  594. ->with($this->emailTemplate, $newVevent, $data);
  595. $this->service->expects(self::once())
  596. ->method('getAttendeeRsvpOrReqForParticipant')
  597. ->willReturn(true);
  598. $this->config->expects(self::once())
  599. ->method('getAppValue')
  600. ->with('dav', 'invitation_link_recipients', 'yes')
  601. ->willReturn('yes');
  602. $this->service->expects(self::once())
  603. ->method('createInvitationToken')
  604. ->with($message, $newVevent, '1496912700')
  605. ->willReturn('token');
  606. $this->service->expects(self::once())
  607. ->method('addResponseButtons')
  608. ->with($this->emailTemplate, 'token');
  609. $this->service->expects(self::once())
  610. ->method('addMoreOptionsButton')
  611. ->with($this->emailTemplate, 'token');
  612. $this->mailer->expects(self::once())
  613. ->method('send')
  614. ->willReturn([]);
  615. $this->mailer
  616. ->method('send')
  617. ->willReturn([]);
  618. $this->plugin->schedule($message);
  619. $this->assertEquals('1.1', $message->getScheduleStatus());
  620. }
  621. public function testNoButtons(): void {
  622. $message = new Message();
  623. $message->method = 'REQUEST';
  624. $newVCalendar = new VCalendar();
  625. $newVevent = new VEvent($newVCalendar, 'VEVENT', array_merge([
  626. 'UID' => 'uid-1234',
  627. 'SEQUENCE' => 1,
  628. 'SUMMARY' => 'Fellowship meeting',
  629. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  630. ], []));
  631. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  632. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  633. $message->message = $newVCalendar;
  634. $message->sender = 'mailto:gandalf@wiz.ard';
  635. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  636. $data = ['invitee_name' => 'Mr. Wizard',
  637. 'meeting_title' => 'Fellowship meeting',
  638. 'attendee_name' => 'frodo@hobb.it'
  639. ];
  640. $attendees = $newVevent->select('ATTENDEE');
  641. $atnd = '';
  642. foreach ($attendees as $attendee) {
  643. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  644. $atnd = $attendee;
  645. }
  646. }
  647. $this->service->expects(self::once())
  648. ->method('getLastOccurrence')
  649. ->willReturn('1496912700');
  650. $this->mailer->expects(self::once())
  651. ->method('validateMailAddress')
  652. ->with('frodo@hobb.it')
  653. ->willReturn(true);
  654. $this->eventComparisonService->expects(self::once())
  655. ->method('findModified')
  656. ->with($newVCalendar, null)
  657. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  658. $this->service->expects(self::once())
  659. ->method('getCurrentAttendee')
  660. ->with($message)
  661. ->willReturn($atnd);
  662. $this->service->expects(self::once())
  663. ->method('isRoomOrResource')
  664. ->with($atnd)
  665. ->willReturn(false);
  666. $this->service->expects(self::once())
  667. ->method('buildBodyData')
  668. ->with($newVevent, null)
  669. ->willReturn($data);
  670. $this->userManager->expects(self::once())
  671. ->method('getDisplayName')
  672. ->willReturn('Mr. Wizard');
  673. $this->service->expects(self::once())
  674. ->method('getFrom');
  675. $this->service->expects(self::once())
  676. ->method('addSubjectAndHeading')
  677. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting', false);
  678. $this->service->expects(self::once())
  679. ->method('addBulletList')
  680. ->with($this->emailTemplate, $newVevent, $data);
  681. $this->service->expects(self::once())
  682. ->method('getAttendeeRsvpOrReqForParticipant')
  683. ->willReturn(true);
  684. $this->config->expects(self::once())
  685. ->method('getAppValue')
  686. ->with('dav', 'invitation_link_recipients', 'yes')
  687. ->willReturn('no');
  688. $this->service->expects(self::never())
  689. ->method('createInvitationToken');
  690. $this->service->expects(self::never())
  691. ->method('addResponseButtons');
  692. $this->service->expects(self::never())
  693. ->method('addMoreOptionsButton');
  694. $this->mailer->expects(self::once())
  695. ->method('send')
  696. ->willReturn([]);
  697. $this->mailer
  698. ->method('send')
  699. ->willReturn([]);
  700. $this->plugin->schedule($message);
  701. $this->assertEquals('1.1', $message->getScheduleStatus());
  702. }
  703. }