IMipPluginTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\CalDAV\Schedule;
  8. use OCA\DAV\CalDAV\EventComparisonService;
  9. use OCA\DAV\CalDAV\Schedule\IMipPlugin;
  10. use OCA\DAV\CalDAV\Schedule\IMipService;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\Defaults;
  13. use OCP\IConfig;
  14. use OCP\IUser;
  15. use OCP\IUserSession;
  16. use OCP\Mail\IAttachment;
  17. use OCP\Mail\IEMailTemplate;
  18. use OCP\Mail\IMailer;
  19. use OCP\Mail\IMessage;
  20. use OCP\Mail\Provider\IManager as IMailManager;
  21. use OCP\Mail\Provider\IMessage as IMailMessageNew;
  22. use OCP\Mail\Provider\IMessageSend as IMailMessageSend;
  23. use OCP\Mail\Provider\IService as IMailService;
  24. use PHPUnit\Framework\MockObject\MockObject;
  25. use Psr\Log\LoggerInterface;
  26. use Sabre\VObject\Component\VCalendar;
  27. use Sabre\VObject\Component\VEvent;
  28. use Sabre\VObject\ITip\Message;
  29. use Test\TestCase;
  30. use function array_merge;
  31. interface IMailServiceMock extends IMailService, IMailMessageSend {
  32. // workaround for creating mock class with multiple interfaces
  33. // TODO: remove after phpUnit 10 is supported.
  34. }
  35. class IMipPluginTest extends TestCase {
  36. /** @var IMessage|MockObject */
  37. private $mailMessage;
  38. /** @var IMailer|MockObject */
  39. private $mailer;
  40. /** @var IEMailTemplate|MockObject */
  41. private $emailTemplate;
  42. /** @var IAttachment|MockObject */
  43. private $emailAttachment;
  44. /** @var ITimeFactory|MockObject */
  45. private $timeFactory;
  46. /** @var IConfig|MockObject */
  47. private $config;
  48. /** @var IUserSession|MockObject */
  49. private $userSession;
  50. /** @var IUser|MockObject */
  51. private $user;
  52. /** @var IMipPlugin */
  53. private $plugin;
  54. /** @var IMipService|MockObject */
  55. private $service;
  56. /** @var Defaults|MockObject */
  57. private $defaults;
  58. /** @var LoggerInterface|MockObject */
  59. private $logger;
  60. /** @var EventComparisonService|MockObject */
  61. private $eventComparisonService;
  62. /** @var IMailManager|MockObject */
  63. private $mailManager;
  64. /** @var IMailService|IMailMessageSend|MockObject */
  65. private $mailService;
  66. /** @var IMailMessageNew|MockObject */
  67. private $mailMessageNew;
  68. protected function setUp(): void {
  69. $this->mailMessage = $this->createMock(IMessage::class);
  70. $this->mailMessage->method('setFrom')->willReturn($this->mailMessage);
  71. $this->mailMessage->method('setReplyTo')->willReturn($this->mailMessage);
  72. $this->mailMessage->method('setTo')->willReturn($this->mailMessage);
  73. $this->mailer = $this->createMock(IMailer::class);
  74. $this->mailer->method('createMessage')->willReturn($this->mailMessage);
  75. $this->emailTemplate = $this->createMock(IEMailTemplate::class);
  76. $this->mailer->method('createEMailTemplate')->willReturn($this->emailTemplate);
  77. $this->emailAttachment = $this->createMock(IAttachment::class);
  78. $this->mailer->method('createAttachment')->willReturn($this->emailAttachment);
  79. $this->logger = $this->createMock(LoggerInterface::class);
  80. $this->timeFactory = $this->createMock(ITimeFactory::class);
  81. $this->timeFactory->method('getTime')->willReturn(1496912528); // 2017-01-01
  82. $this->config = $this->createMock(IConfig::class);
  83. $this->user = $this->createMock(IUser::class);
  84. $this->userSession = $this->createMock(IUserSession::class);
  85. $this->userSession->method('getUser')
  86. ->willReturn($this->user);
  87. $this->defaults = $this->createMock(Defaults::class);
  88. $this->defaults->method('getName')
  89. ->willReturn('Instance Name 123');
  90. $this->service = $this->createMock(IMipService::class);
  91. $this->eventComparisonService = $this->createMock(EventComparisonService::class);
  92. $this->mailManager = $this->createMock(IMailManager::class);
  93. $this->mailService = $this->createMock(IMailServiceMock::class);
  94. $this->mailMessageNew = $this->createMock(IMailMessageNew::class);
  95. $this->plugin = new IMipPlugin(
  96. $this->config,
  97. $this->mailer,
  98. $this->logger,
  99. $this->timeFactory,
  100. $this->defaults,
  101. $this->userSession,
  102. $this->service,
  103. $this->eventComparisonService,
  104. $this->mailManager,
  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->user->expects(self::any())
  189. ->method('getUID')
  190. ->willReturn('user1');
  191. $this->user->expects(self::any())
  192. ->method('getDisplayName')
  193. ->willReturn('Mr. Wizard');
  194. $this->userSession->expects(self::any())
  195. ->method('getUser')
  196. ->willReturn($this->user);
  197. $this->service->expects(self::once())
  198. ->method('getFrom');
  199. $this->service->expects(self::once())
  200. ->method('addSubjectAndHeading')
  201. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting without (!) Boromir', true);
  202. $this->service->expects(self::once())
  203. ->method('addBulletList')
  204. ->with($this->emailTemplate, $newVevent, $data);
  205. $this->service->expects(self::once())
  206. ->method('getAttendeeRsvpOrReqForParticipant')
  207. ->willReturn(true);
  208. $this->config->expects(self::once())
  209. ->method('getAppValue')
  210. ->with('dav', 'invitation_link_recipients', 'yes')
  211. ->willReturn('yes');
  212. $this->service->expects(self::once())
  213. ->method('createInvitationToken')
  214. ->with($message, $newVevent, '1496912700')
  215. ->willReturn('token');
  216. $this->service->expects(self::once())
  217. ->method('addResponseButtons')
  218. ->with($this->emailTemplate, 'token');
  219. $this->service->expects(self::once())
  220. ->method('addMoreOptionsButton')
  221. ->with($this->emailTemplate, 'token');
  222. $this->mailer->expects(self::once())
  223. ->method('send')
  224. ->willReturn([]);
  225. $this->plugin->schedule($message);
  226. $this->assertEquals('1.1', $message->getScheduleStatus());
  227. }
  228. public function testAttendeeIsResource(): void {
  229. $message = new Message();
  230. $message->method = 'REQUEST';
  231. $newVCalendar = new VCalendar();
  232. $newVevent = new VEvent($newVCalendar, 'one', array_merge([
  233. 'UID' => 'uid-1234',
  234. 'SEQUENCE' => 1,
  235. 'SUMMARY' => 'Fellowship meeting without (!) Boromir',
  236. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  237. ], []));
  238. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  239. $newVevent->add('ATTENDEE', 'mailto:' . 'the-shire@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'The Shire', 'CUTYPE' => 'ROOM']);
  240. $message->message = $newVCalendar;
  241. $message->sender = 'mailto:gandalf@wiz.ard';
  242. $message->senderName = 'Mr. Wizard';
  243. $message->recipient = 'mailto:' . 'the-shire@hobb.it';
  244. // save the old copy in the plugin
  245. $oldVCalendar = new VCalendar();
  246. $oldVEvent = new VEvent($oldVCalendar, 'one', [
  247. 'UID' => 'uid-1234',
  248. 'SEQUENCE' => 0,
  249. 'SUMMARY' => 'Fellowship meeting',
  250. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  251. ]);
  252. $oldVEvent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  253. $oldVEvent->add('ATTENDEE', 'mailto:' . 'the-shire@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'The Shire', 'CUTYPE' => 'ROOM']);
  254. $oldVEvent->add('ATTENDEE', 'mailto:' . 'boromir@tra.it.or', ['RSVP' => 'TRUE']);
  255. $oldVCalendar->add($oldVEvent);
  256. $data = ['invitee_name' => 'Mr. Wizard',
  257. 'meeting_title' => 'Fellowship meeting without (!) Boromir',
  258. 'attendee_name' => 'frodo@hobb.it'
  259. ];
  260. $attendees = $newVevent->select('ATTENDEE');
  261. $room = '';
  262. foreach ($attendees as $attendee) {
  263. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  264. $room = $attendee;
  265. }
  266. }
  267. $this->plugin->setVCalendar($oldVCalendar);
  268. $this->service->expects(self::once())
  269. ->method('getLastOccurrence')
  270. ->willReturn('1496912700');
  271. $this->mailer->expects(self::once())
  272. ->method('validateMailAddress')
  273. ->with('the-shire@hobb.it')
  274. ->willReturn(true);
  275. $this->eventComparisonService->expects(self::once())
  276. ->method('findModified')
  277. ->willReturn(['new' => [$newVevent], 'old' => [$oldVEvent]]);
  278. $this->service->expects(self::once())
  279. ->method('getCurrentAttendee')
  280. ->with($message)
  281. ->willReturn($room);
  282. $this->service->expects(self::once())
  283. ->method('isRoomOrResource')
  284. ->with($room)
  285. ->willReturn(true);
  286. $this->service->expects(self::never())
  287. ->method('buildBodyData');
  288. $this->user->expects(self::any())
  289. ->method('getUID')
  290. ->willReturn('user1');
  291. $this->user->expects(self::any())
  292. ->method('getDisplayName')
  293. ->willReturn('Mr. Wizard');
  294. $this->userSession->expects(self::any())
  295. ->method('getUser')
  296. ->willReturn($this->user);
  297. $this->service->expects(self::never())
  298. ->method('getFrom');
  299. $this->service->expects(self::never())
  300. ->method('addSubjectAndHeading');
  301. $this->service->expects(self::never())
  302. ->method('addBulletList');
  303. $this->service->expects(self::never())
  304. ->method('getAttendeeRsvpOrReqForParticipant');
  305. $this->config->expects(self::never())
  306. ->method('getAppValue');
  307. $this->service->expects(self::never())
  308. ->method('createInvitationToken');
  309. $this->service->expects(self::never())
  310. ->method('addResponseButtons');
  311. $this->service->expects(self::never())
  312. ->method('addMoreOptionsButton');
  313. $this->mailer->expects(self::never())
  314. ->method('send');
  315. $this->plugin->schedule($message);
  316. $this->assertEquals('1.0', $message->getScheduleStatus());
  317. }
  318. public function testParsingRecurrence(): void {
  319. $message = new Message();
  320. $message->method = 'REQUEST';
  321. $newVCalendar = new VCalendar();
  322. $newVevent = new VEvent($newVCalendar, 'one', [
  323. 'UID' => 'uid-1234',
  324. 'LAST-MODIFIED' => 123456,
  325. 'SEQUENCE' => 2,
  326. 'SUMMARY' => 'Fellowship meeting',
  327. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  328. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z'
  329. ]);
  330. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  331. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  332. $newvEvent2 = new VEvent($newVCalendar, 'two', [
  333. 'UID' => 'uid-1234',
  334. 'SEQUENCE' => 1,
  335. 'SUMMARY' => 'Elevenses',
  336. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  337. 'RECURRENCE-ID' => new \DateTime('2016-01-01 00:00:00')
  338. ]);
  339. $newvEvent2->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  340. $newvEvent2->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  341. $message->message = $newVCalendar;
  342. $message->sender = 'mailto:gandalf@wiz.ard';
  343. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  344. // save the old copy in the plugin
  345. $oldVCalendar = new VCalendar();
  346. $oldVEvent = new VEvent($oldVCalendar, 'one', [
  347. 'UID' => 'uid-1234',
  348. 'LAST-MODIFIED' => 123456,
  349. 'SEQUENCE' => 2,
  350. 'SUMMARY' => 'Fellowship meeting',
  351. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  352. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z'
  353. ]);
  354. $oldVEvent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  355. $oldVEvent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  356. $data = ['invitee_name' => 'Mr. Wizard',
  357. 'meeting_title' => 'Elevenses',
  358. 'attendee_name' => 'frodo@hobb.it'
  359. ];
  360. $attendees = $newVevent->select('ATTENDEE');
  361. $atnd = '';
  362. foreach ($attendees as $attendee) {
  363. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  364. $atnd = $attendee;
  365. }
  366. }
  367. $this->plugin->setVCalendar($oldVCalendar);
  368. $this->service->expects(self::once())
  369. ->method('getLastOccurrence')
  370. ->willReturn('1496912700');
  371. $this->mailer->expects(self::once())
  372. ->method('validateMailAddress')
  373. ->with('frodo@hobb.it')
  374. ->willReturn(true);
  375. $this->eventComparisonService->expects(self::once())
  376. ->method('findModified')
  377. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  378. $this->service->expects(self::once())
  379. ->method('getCurrentAttendee')
  380. ->with($message)
  381. ->willReturn($atnd);
  382. $this->service->expects(self::once())
  383. ->method('isRoomOrResource')
  384. ->with($atnd)
  385. ->willReturn(false);
  386. $this->service->expects(self::once())
  387. ->method('buildBodyData')
  388. ->with($newVevent, null)
  389. ->willReturn($data);
  390. $this->user->expects(self::any())
  391. ->method('getUID')
  392. ->willReturn('user1');
  393. $this->user->expects(self::any())
  394. ->method('getDisplayName')
  395. ->willReturn('Mr. Wizard');
  396. $this->userSession->expects(self::any())
  397. ->method('getUser')
  398. ->willReturn($this->user);
  399. $this->service->expects(self::once())
  400. ->method('getFrom');
  401. $this->service->expects(self::once())
  402. ->method('addSubjectAndHeading')
  403. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Elevenses', false);
  404. $this->service->expects(self::once())
  405. ->method('addBulletList')
  406. ->with($this->emailTemplate, $newVevent, $data);
  407. $this->service->expects(self::once())
  408. ->method('getAttendeeRsvpOrReqForParticipant')
  409. ->willReturn(true);
  410. $this->config->expects(self::once())
  411. ->method('getAppValue')
  412. ->with('dav', 'invitation_link_recipients', 'yes')
  413. ->willReturn('yes');
  414. $this->service->expects(self::once())
  415. ->method('createInvitationToken')
  416. ->with($message, $newVevent, '1496912700')
  417. ->willReturn('token');
  418. $this->service->expects(self::once())
  419. ->method('addResponseButtons')
  420. ->with($this->emailTemplate, 'token');
  421. $this->service->expects(self::once())
  422. ->method('addMoreOptionsButton')
  423. ->with($this->emailTemplate, 'token');
  424. $this->mailer->expects(self::once())
  425. ->method('send')
  426. ->willReturn([]);
  427. $this->plugin->schedule($message);
  428. $this->assertEquals('1.1', $message->getScheduleStatus());
  429. }
  430. public function testEmailValidationFailed() {
  431. $message = new Message();
  432. $message->method = 'REQUEST';
  433. $message->message = new VCalendar();
  434. $message->message->add('VEVENT', array_merge([
  435. 'UID' => 'uid-1234',
  436. 'SEQUENCE' => 0,
  437. 'SUMMARY' => 'Fellowship meeting',
  438. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  439. ], []));
  440. $message->message->VEVENT->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  441. $message->message->VEVENT->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE']);
  442. $message->sender = 'mailto:gandalf@wiz.ard';
  443. $message->senderName = 'Mr. Wizard';
  444. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  445. $this->service->expects(self::once())
  446. ->method('getLastOccurrence')
  447. ->willReturn('1496912700');
  448. $this->mailer->expects(self::once())
  449. ->method('validateMailAddress')
  450. ->with('frodo@hobb.it')
  451. ->willReturn(false);
  452. $this->plugin->schedule($message);
  453. $this->assertEquals('5.0', $message->getScheduleStatus());
  454. }
  455. public function testFailedDelivery(): void {
  456. $message = new Message();
  457. $message->method = 'REQUEST';
  458. $newVcalendar = new VCalendar();
  459. $newVevent = new VEvent($newVcalendar, 'one', array_merge([
  460. 'UID' => 'uid-1234',
  461. 'SEQUENCE' => 1,
  462. 'SUMMARY' => 'Fellowship meeting without (!) Boromir',
  463. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  464. ], []));
  465. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  466. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  467. $message->message = $newVcalendar;
  468. $message->sender = 'mailto:gandalf@wiz.ard';
  469. $message->senderName = 'Mr. Wizard';
  470. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  471. // save the old copy in the plugin
  472. $oldVcalendar = new VCalendar();
  473. $oldVevent = new VEvent($oldVcalendar, 'one', [
  474. 'UID' => 'uid-1234',
  475. 'SEQUENCE' => 0,
  476. 'SUMMARY' => 'Fellowship meeting',
  477. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  478. ]);
  479. $oldVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  480. $oldVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  481. $oldVevent->add('ATTENDEE', 'mailto:' . 'boromir@tra.it.or', ['RSVP' => 'TRUE']);
  482. $oldVcalendar->add($oldVevent);
  483. $data = ['invitee_name' => 'Mr. Wizard',
  484. 'meeting_title' => 'Fellowship meeting without (!) Boromir',
  485. 'attendee_name' => 'frodo@hobb.it'
  486. ];
  487. $attendees = $newVevent->select('ATTENDEE');
  488. $atnd = '';
  489. foreach ($attendees as $attendee) {
  490. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  491. $atnd = $attendee;
  492. }
  493. }
  494. $this->plugin->setVCalendar($oldVcalendar);
  495. $this->service->expects(self::once())
  496. ->method('getLastOccurrence')
  497. ->willReturn('1496912700');
  498. $this->mailer->expects(self::once())
  499. ->method('validateMailAddress')
  500. ->with('frodo@hobb.it')
  501. ->willReturn(true);
  502. $this->eventComparisonService->expects(self::once())
  503. ->method('findModified')
  504. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  505. $this->service->expects(self::once())
  506. ->method('getCurrentAttendee')
  507. ->with($message)
  508. ->willReturn($atnd);
  509. $this->service->expects(self::once())
  510. ->method('isRoomOrResource')
  511. ->with($atnd)
  512. ->willReturn(false);
  513. $this->service->expects(self::once())
  514. ->method('buildBodyData')
  515. ->with($newVevent, null)
  516. ->willReturn($data);
  517. $this->user->expects(self::any())
  518. ->method('getUID')
  519. ->willReturn('user1');
  520. $this->user->expects(self::any())
  521. ->method('getDisplayName')
  522. ->willReturn('Mr. Wizard');
  523. $this->userSession->expects(self::any())
  524. ->method('getUser')
  525. ->willReturn($this->user);
  526. $this->service->expects(self::once())
  527. ->method('getFrom');
  528. $this->service->expects(self::once())
  529. ->method('addSubjectAndHeading')
  530. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting without (!) Boromir', false);
  531. $this->service->expects(self::once())
  532. ->method('addBulletList')
  533. ->with($this->emailTemplate, $newVevent, $data);
  534. $this->service->expects(self::once())
  535. ->method('getAttendeeRsvpOrReqForParticipant')
  536. ->willReturn(true);
  537. $this->config->expects(self::once())
  538. ->method('getAppValue')
  539. ->with('dav', 'invitation_link_recipients', 'yes')
  540. ->willReturn('yes');
  541. $this->service->expects(self::once())
  542. ->method('createInvitationToken')
  543. ->with($message, $newVevent, '1496912700')
  544. ->willReturn('token');
  545. $this->service->expects(self::once())
  546. ->method('addResponseButtons')
  547. ->with($this->emailTemplate, 'token');
  548. $this->service->expects(self::once())
  549. ->method('addMoreOptionsButton')
  550. ->with($this->emailTemplate, 'token');
  551. $this->mailer->expects(self::once())
  552. ->method('send')
  553. ->willReturn([]);
  554. $this->mailer
  555. ->method('send')
  556. ->willThrowException(new \Exception());
  557. $this->logger->expects(self::once())
  558. ->method('error');
  559. $this->plugin->schedule($message);
  560. $this->assertEquals('5.0', $message->getScheduleStatus());
  561. }
  562. public function testMailProviderSend(): void {
  563. // construct iTip message with event and attendees
  564. $message = new Message();
  565. $message->method = 'REQUEST';
  566. $calendar = new VCalendar();
  567. $event = new VEvent($calendar, 'one', array_merge([
  568. 'UID' => 'uid-1234',
  569. 'SEQUENCE' => 1,
  570. 'SUMMARY' => 'Fellowship meeting without (!) Boromir',
  571. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  572. ], []));
  573. $event->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  574. $event->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  575. $message->message = $calendar;
  576. $message->sender = 'mailto:gandalf@wiz.ard';
  577. $message->senderName = 'Mr. Wizard';
  578. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  579. // construct
  580. foreach ($event->select('ATTENDEE') as $entry) {
  581. if (strcasecmp($entry->getValue(), $message->recipient) === 0) {
  582. $attendee = $entry;
  583. }
  584. }
  585. // construct body data return
  586. $data = ['invitee_name' => 'Mr. Wizard',
  587. 'meeting_title' => 'Fellowship meeting without (!) Boromir',
  588. 'attendee_name' => 'frodo@hobb.it'
  589. ];
  590. // construct system config mock returns
  591. $this->config->expects(self::once())
  592. ->method('getAppValue')
  593. ->with('dav', 'invitation_link_recipients', 'yes')
  594. ->willReturn('yes');
  595. // construct user mock returns
  596. $this->user->expects(self::any())
  597. ->method('getUID')
  598. ->willReturn('user1');
  599. $this->user->expects(self::any())
  600. ->method('getDisplayName')
  601. ->willReturn('Mr. Wizard');
  602. // construct user session mock returns
  603. $this->userSession->expects(self::any())
  604. ->method('getUser')
  605. ->willReturn($this->user);
  606. // construct service mock returns
  607. $this->service->expects(self::once())
  608. ->method('getLastOccurrence')
  609. ->willReturn('1496912700');
  610. $this->service->expects(self::once())
  611. ->method('getCurrentAttendee')
  612. ->with($message)
  613. ->willReturn($attendee);
  614. $this->service->expects(self::once())
  615. ->method('isRoomOrResource')
  616. ->with($attendee)
  617. ->willReturn(false);
  618. $this->service->expects(self::once())
  619. ->method('buildBodyData')
  620. ->with($event, null)
  621. ->willReturn($data);
  622. $this->service->expects(self::once())
  623. ->method('getFrom');
  624. $this->service->expects(self::once())
  625. ->method('addSubjectAndHeading')
  626. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting without (!) Boromir', false);
  627. $this->service->expects(self::once())
  628. ->method('addBulletList')
  629. ->with($this->emailTemplate, $event, $data);
  630. $this->service->expects(self::once())
  631. ->method('getAttendeeRsvpOrReqForParticipant')
  632. ->willReturn(true);
  633. $this->service->expects(self::once())
  634. ->method('createInvitationToken')
  635. ->with($message, $event, '1496912700')
  636. ->willReturn('token');
  637. $this->service->expects(self::once())
  638. ->method('addResponseButtons')
  639. ->with($this->emailTemplate, 'token');
  640. $this->service->expects(self::once())
  641. ->method('addMoreOptionsButton')
  642. ->with($this->emailTemplate, 'token');
  643. $this->eventComparisonService->expects(self::once())
  644. ->method('findModified')
  645. ->willReturn(['old' => [] ,'new' => [$event]]);
  646. // construct mail mock returns
  647. $this->mailer->expects(self::once())
  648. ->method('validateMailAddress')
  649. ->with('frodo@hobb.it')
  650. ->willReturn(true);
  651. // construct mail provider mock returns
  652. $this->mailService
  653. ->method('initiateMessage')
  654. ->willReturn($this->mailMessageNew);
  655. $this->mailService
  656. ->method('sendMessage')
  657. ->with($this->mailMessageNew);
  658. $this->mailManager
  659. ->method('findServiceByAddress')
  660. ->with('user1', 'gandalf@wiz.ard')
  661. ->willReturn($this->mailService);
  662. $this->plugin->schedule($message);
  663. $this->assertEquals('1.1', $message->getScheduleStatus());
  664. }
  665. public function testNoOldEvent(): void {
  666. $message = new Message();
  667. $message->method = 'REQUEST';
  668. $newVCalendar = new VCalendar();
  669. $newVevent = new VEvent($newVCalendar, 'VEVENT', array_merge([
  670. 'UID' => 'uid-1234',
  671. 'SEQUENCE' => 1,
  672. 'SUMMARY' => 'Fellowship meeting',
  673. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  674. ], []));
  675. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  676. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  677. $message->message = $newVCalendar;
  678. $message->sender = 'mailto:gandalf@wiz.ard';
  679. $message->senderName = 'Mr. Wizard';
  680. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  681. $data = ['invitee_name' => 'Mr. Wizard',
  682. 'meeting_title' => 'Fellowship meeting',
  683. 'attendee_name' => 'frodo@hobb.it'
  684. ];
  685. $attendees = $newVevent->select('ATTENDEE');
  686. $atnd = '';
  687. foreach ($attendees as $attendee) {
  688. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  689. $atnd = $attendee;
  690. }
  691. }
  692. $this->service->expects(self::once())
  693. ->method('getLastOccurrence')
  694. ->willReturn('1496912700');
  695. $this->mailer->expects(self::once())
  696. ->method('validateMailAddress')
  697. ->with('frodo@hobb.it')
  698. ->willReturn(true);
  699. $this->eventComparisonService->expects(self::once())
  700. ->method('findModified')
  701. ->with($newVCalendar, null)
  702. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  703. $this->service->expects(self::once())
  704. ->method('getCurrentAttendee')
  705. ->with($message)
  706. ->willReturn($atnd);
  707. $this->service->expects(self::once())
  708. ->method('isRoomOrResource')
  709. ->with($atnd)
  710. ->willReturn(false);
  711. $this->service->expects(self::once())
  712. ->method('buildBodyData')
  713. ->with($newVevent, null)
  714. ->willReturn($data);
  715. $this->user->expects(self::any())
  716. ->method('getUID')
  717. ->willReturn('user1');
  718. $this->user->expects(self::any())
  719. ->method('getDisplayName')
  720. ->willReturn('Mr. Wizard');
  721. $this->userSession->expects(self::any())
  722. ->method('getUser')
  723. ->willReturn($this->user);
  724. $this->service->expects(self::once())
  725. ->method('getFrom');
  726. $this->service->expects(self::once())
  727. ->method('addSubjectAndHeading')
  728. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting', false);
  729. $this->service->expects(self::once())
  730. ->method('addBulletList')
  731. ->with($this->emailTemplate, $newVevent, $data);
  732. $this->service->expects(self::once())
  733. ->method('getAttendeeRsvpOrReqForParticipant')
  734. ->willReturn(true);
  735. $this->config->expects(self::once())
  736. ->method('getAppValue')
  737. ->with('dav', 'invitation_link_recipients', 'yes')
  738. ->willReturn('yes');
  739. $this->service->expects(self::once())
  740. ->method('createInvitationToken')
  741. ->with($message, $newVevent, '1496912700')
  742. ->willReturn('token');
  743. $this->service->expects(self::once())
  744. ->method('addResponseButtons')
  745. ->with($this->emailTemplate, 'token');
  746. $this->service->expects(self::once())
  747. ->method('addMoreOptionsButton')
  748. ->with($this->emailTemplate, 'token');
  749. $this->mailer->expects(self::once())
  750. ->method('send')
  751. ->willReturn([]);
  752. $this->mailer
  753. ->method('send')
  754. ->willReturn([]);
  755. $this->plugin->schedule($message);
  756. $this->assertEquals('1.1', $message->getScheduleStatus());
  757. }
  758. public function testNoButtons(): void {
  759. $message = new Message();
  760. $message->method = 'REQUEST';
  761. $newVCalendar = new VCalendar();
  762. $newVevent = new VEvent($newVCalendar, 'VEVENT', array_merge([
  763. 'UID' => 'uid-1234',
  764. 'SEQUENCE' => 1,
  765. 'SUMMARY' => 'Fellowship meeting',
  766. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  767. ], []));
  768. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  769. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  770. $message->message = $newVCalendar;
  771. $message->sender = 'mailto:gandalf@wiz.ard';
  772. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  773. $data = ['invitee_name' => 'Mr. Wizard',
  774. 'meeting_title' => 'Fellowship meeting',
  775. 'attendee_name' => 'frodo@hobb.it'
  776. ];
  777. $attendees = $newVevent->select('ATTENDEE');
  778. $atnd = '';
  779. foreach ($attendees as $attendee) {
  780. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  781. $atnd = $attendee;
  782. }
  783. }
  784. $this->service->expects(self::once())
  785. ->method('getLastOccurrence')
  786. ->willReturn('1496912700');
  787. $this->mailer->expects(self::once())
  788. ->method('validateMailAddress')
  789. ->with('frodo@hobb.it')
  790. ->willReturn(true);
  791. $this->eventComparisonService->expects(self::once())
  792. ->method('findModified')
  793. ->with($newVCalendar, null)
  794. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  795. $this->service->expects(self::once())
  796. ->method('getCurrentAttendee')
  797. ->with($message)
  798. ->willReturn($atnd);
  799. $this->service->expects(self::once())
  800. ->method('isRoomOrResource')
  801. ->with($atnd)
  802. ->willReturn(false);
  803. $this->service->expects(self::once())
  804. ->method('buildBodyData')
  805. ->with($newVevent, null)
  806. ->willReturn($data);
  807. $this->user->expects(self::any())
  808. ->method('getUID')
  809. ->willReturn('user1');
  810. $this->user->expects(self::any())
  811. ->method('getDisplayName')
  812. ->willReturn('Mr. Wizard');
  813. $this->userSession->expects(self::any())
  814. ->method('getUser')
  815. ->willReturn($this->user);
  816. $this->service->expects(self::once())
  817. ->method('getFrom');
  818. $this->service->expects(self::once())
  819. ->method('addSubjectAndHeading')
  820. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting', false);
  821. $this->service->expects(self::once())
  822. ->method('addBulletList')
  823. ->with($this->emailTemplate, $newVevent, $data);
  824. $this->service->expects(self::once())
  825. ->method('getAttendeeRsvpOrReqForParticipant')
  826. ->willReturn(true);
  827. $this->config->expects(self::once())
  828. ->method('getAppValue')
  829. ->with('dav', 'invitation_link_recipients', 'yes')
  830. ->willReturn('no');
  831. $this->service->expects(self::never())
  832. ->method('createInvitationToken');
  833. $this->service->expects(self::never())
  834. ->method('addResponseButtons');
  835. $this->service->expects(self::never())
  836. ->method('addMoreOptionsButton');
  837. $this->mailer->expects(self::once())
  838. ->method('send')
  839. ->willReturn([]);
  840. $this->mailer
  841. ->method('send')
  842. ->willReturn([]);
  843. $this->plugin->schedule($message);
  844. $this->assertEquals('1.1', $message->getScheduleStatus());
  845. }
  846. }