IMipPluginTest.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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\IAppConfig;
  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 IAppConfig|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(IAppConfig::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('getValueString')
  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('getValueString');
  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('getValueString')
  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(): void {
  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('getValueString')
  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('getValueString')
  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 testMailProviderDisabled(): void {
  666. $message = new Message();
  667. $message->method = 'REQUEST';
  668. $newVCalendar = new VCalendar();
  669. $newVevent = new VEvent($newVCalendar, 'one', array_merge([
  670. 'UID' => 'uid-1234',
  671. 'SEQUENCE' => 1,
  672. 'SUMMARY' => 'Fellowship meeting without (!) Boromir',
  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. // save the old copy in the plugin
  682. $oldVCalendar = new VCalendar();
  683. $oldVEvent = new VEvent($oldVCalendar, 'one', [
  684. 'UID' => 'uid-1234',
  685. 'SEQUENCE' => 0,
  686. 'SUMMARY' => 'Fellowship meeting',
  687. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  688. ]);
  689. $oldVEvent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  690. $oldVEvent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  691. $oldVEvent->add('ATTENDEE', 'mailto:' . 'boromir@tra.it.or', ['RSVP' => 'TRUE']);
  692. $oldVCalendar->add($oldVEvent);
  693. $data = ['invitee_name' => 'Mr. Wizard',
  694. 'meeting_title' => 'Fellowship meeting without (!) Boromir',
  695. 'attendee_name' => 'frodo@hobb.it'
  696. ];
  697. $attendees = $newVevent->select('ATTENDEE');
  698. $atnd = '';
  699. foreach ($attendees as $attendee) {
  700. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  701. $atnd = $attendee;
  702. }
  703. }
  704. $this->plugin->setVCalendar($oldVCalendar);
  705. $this->service->expects(self::once())
  706. ->method('getLastOccurrence')
  707. ->willReturn(1496912700);
  708. $this->mailer->expects(self::once())
  709. ->method('validateMailAddress')
  710. ->with('frodo@hobb.it')
  711. ->willReturn(true);
  712. $this->eventComparisonService->expects(self::once())
  713. ->method('findModified')
  714. ->willReturn(['new' => [$newVevent], 'old' => [$oldVEvent]]);
  715. $this->service->expects(self::once())
  716. ->method('getCurrentAttendee')
  717. ->with($message)
  718. ->willReturn($atnd);
  719. $this->service->expects(self::once())
  720. ->method('isRoomOrResource')
  721. ->with($atnd)
  722. ->willReturn(false);
  723. $this->service->expects(self::once())
  724. ->method('buildBodyData')
  725. ->with($newVevent, $oldVEvent)
  726. ->willReturn($data);
  727. $this->user->expects(self::any())
  728. ->method('getUID')
  729. ->willReturn('user1');
  730. $this->user->expects(self::any())
  731. ->method('getDisplayName')
  732. ->willReturn('Mr. Wizard');
  733. $this->userSession->expects(self::any())
  734. ->method('getUser')
  735. ->willReturn($this->user);
  736. $this->service->expects(self::once())
  737. ->method('getFrom');
  738. $this->service->expects(self::once())
  739. ->method('addSubjectAndHeading')
  740. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting without (!) Boromir', true);
  741. $this->service->expects(self::once())
  742. ->method('addBulletList')
  743. ->with($this->emailTemplate, $newVevent, $data);
  744. $this->service->expects(self::once())
  745. ->method('getAttendeeRsvpOrReqForParticipant')
  746. ->willReturn(true);
  747. $this->config->expects(self::once())
  748. ->method('getValueString')
  749. ->with('dav', 'invitation_link_recipients', 'yes')
  750. ->willReturn('yes');
  751. $this->config->expects(self::once())
  752. ->method('getValueBool')
  753. ->with('core', 'mail_providers_enabled', true)
  754. ->willReturn(false);
  755. $this->service->expects(self::once())
  756. ->method('createInvitationToken')
  757. ->with($message, $newVevent, 1496912700)
  758. ->willReturn('token');
  759. $this->service->expects(self::once())
  760. ->method('addResponseButtons')
  761. ->with($this->emailTemplate, 'token');
  762. $this->service->expects(self::once())
  763. ->method('addMoreOptionsButton')
  764. ->with($this->emailTemplate, 'token');
  765. $this->mailer->expects(self::once())
  766. ->method('send')
  767. ->willReturn([]);
  768. $this->plugin->schedule($message);
  769. $this->assertEquals('1.1', $message->getScheduleStatus());
  770. }
  771. public function testNoOldEvent(): void {
  772. $message = new Message();
  773. $message->method = 'REQUEST';
  774. $newVCalendar = new VCalendar();
  775. $newVevent = new VEvent($newVCalendar, 'VEVENT', array_merge([
  776. 'UID' => 'uid-1234',
  777. 'SEQUENCE' => 1,
  778. 'SUMMARY' => 'Fellowship meeting',
  779. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  780. ], []));
  781. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  782. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  783. $message->message = $newVCalendar;
  784. $message->sender = 'mailto:gandalf@wiz.ard';
  785. $message->senderName = 'Mr. Wizard';
  786. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  787. $data = ['invitee_name' => 'Mr. Wizard',
  788. 'meeting_title' => 'Fellowship meeting',
  789. 'attendee_name' => 'frodo@hobb.it'
  790. ];
  791. $attendees = $newVevent->select('ATTENDEE');
  792. $atnd = '';
  793. foreach ($attendees as $attendee) {
  794. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  795. $atnd = $attendee;
  796. }
  797. }
  798. $this->service->expects(self::once())
  799. ->method('getLastOccurrence')
  800. ->willReturn(1496912700);
  801. $this->mailer->expects(self::once())
  802. ->method('validateMailAddress')
  803. ->with('frodo@hobb.it')
  804. ->willReturn(true);
  805. $this->eventComparisonService->expects(self::once())
  806. ->method('findModified')
  807. ->with($newVCalendar, null)
  808. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  809. $this->service->expects(self::once())
  810. ->method('getCurrentAttendee')
  811. ->with($message)
  812. ->willReturn($atnd);
  813. $this->service->expects(self::once())
  814. ->method('isRoomOrResource')
  815. ->with($atnd)
  816. ->willReturn(false);
  817. $this->service->expects(self::once())
  818. ->method('buildBodyData')
  819. ->with($newVevent, null)
  820. ->willReturn($data);
  821. $this->user->expects(self::any())
  822. ->method('getUID')
  823. ->willReturn('user1');
  824. $this->user->expects(self::any())
  825. ->method('getDisplayName')
  826. ->willReturn('Mr. Wizard');
  827. $this->userSession->expects(self::any())
  828. ->method('getUser')
  829. ->willReturn($this->user);
  830. $this->service->expects(self::once())
  831. ->method('getFrom');
  832. $this->service->expects(self::once())
  833. ->method('addSubjectAndHeading')
  834. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting', false);
  835. $this->service->expects(self::once())
  836. ->method('addBulletList')
  837. ->with($this->emailTemplate, $newVevent, $data);
  838. $this->service->expects(self::once())
  839. ->method('getAttendeeRsvpOrReqForParticipant')
  840. ->willReturn(true);
  841. $this->config->expects(self::once())
  842. ->method('getValueString')
  843. ->with('dav', 'invitation_link_recipients', 'yes')
  844. ->willReturn('yes');
  845. $this->service->expects(self::once())
  846. ->method('createInvitationToken')
  847. ->with($message, $newVevent, 1496912700)
  848. ->willReturn('token');
  849. $this->service->expects(self::once())
  850. ->method('addResponseButtons')
  851. ->with($this->emailTemplate, 'token');
  852. $this->service->expects(self::once())
  853. ->method('addMoreOptionsButton')
  854. ->with($this->emailTemplate, 'token');
  855. $this->mailer->expects(self::once())
  856. ->method('send')
  857. ->willReturn([]);
  858. $this->mailer
  859. ->method('send')
  860. ->willReturn([]);
  861. $this->plugin->schedule($message);
  862. $this->assertEquals('1.1', $message->getScheduleStatus());
  863. }
  864. public function testNoButtons(): void {
  865. $message = new Message();
  866. $message->method = 'REQUEST';
  867. $newVCalendar = new VCalendar();
  868. $newVevent = new VEvent($newVCalendar, 'VEVENT', array_merge([
  869. 'UID' => 'uid-1234',
  870. 'SEQUENCE' => 1,
  871. 'SUMMARY' => 'Fellowship meeting',
  872. 'DTSTART' => new \DateTime('2016-01-01 00:00:00')
  873. ], []));
  874. $newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  875. $newVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  876. $message->message = $newVCalendar;
  877. $message->sender = 'mailto:gandalf@wiz.ard';
  878. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  879. $data = ['invitee_name' => 'Mr. Wizard',
  880. 'meeting_title' => 'Fellowship meeting',
  881. 'attendee_name' => 'frodo@hobb.it'
  882. ];
  883. $attendees = $newVevent->select('ATTENDEE');
  884. $atnd = '';
  885. foreach ($attendees as $attendee) {
  886. if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
  887. $atnd = $attendee;
  888. }
  889. }
  890. $this->service->expects(self::once())
  891. ->method('getLastOccurrence')
  892. ->willReturn(1496912700);
  893. $this->mailer->expects(self::once())
  894. ->method('validateMailAddress')
  895. ->with('frodo@hobb.it')
  896. ->willReturn(true);
  897. $this->eventComparisonService->expects(self::once())
  898. ->method('findModified')
  899. ->with($newVCalendar, null)
  900. ->willReturn(['old' => [] ,'new' => [$newVevent]]);
  901. $this->service->expects(self::once())
  902. ->method('getCurrentAttendee')
  903. ->with($message)
  904. ->willReturn($atnd);
  905. $this->service->expects(self::once())
  906. ->method('isRoomOrResource')
  907. ->with($atnd)
  908. ->willReturn(false);
  909. $this->service->expects(self::once())
  910. ->method('buildBodyData')
  911. ->with($newVevent, null)
  912. ->willReturn($data);
  913. $this->user->expects(self::any())
  914. ->method('getUID')
  915. ->willReturn('user1');
  916. $this->user->expects(self::any())
  917. ->method('getDisplayName')
  918. ->willReturn('Mr. Wizard');
  919. $this->userSession->expects(self::any())
  920. ->method('getUser')
  921. ->willReturn($this->user);
  922. $this->service->expects(self::once())
  923. ->method('getFrom');
  924. $this->service->expects(self::once())
  925. ->method('addSubjectAndHeading')
  926. ->with($this->emailTemplate, 'request', 'Mr. Wizard', 'Fellowship meeting', false);
  927. $this->service->expects(self::once())
  928. ->method('addBulletList')
  929. ->with($this->emailTemplate, $newVevent, $data);
  930. $this->service->expects(self::once())
  931. ->method('getAttendeeRsvpOrReqForParticipant')
  932. ->willReturn(true);
  933. $this->config->expects(self::once())
  934. ->method('getValueString')
  935. ->with('dav', 'invitation_link_recipients', 'yes')
  936. ->willReturn('no');
  937. $this->service->expects(self::never())
  938. ->method('createInvitationToken');
  939. $this->service->expects(self::never())
  940. ->method('addResponseButtons');
  941. $this->service->expects(self::never())
  942. ->method('addMoreOptionsButton');
  943. $this->mailer->expects(self::once())
  944. ->method('send')
  945. ->willReturn([]);
  946. $this->mailer
  947. ->method('send')
  948. ->willReturn([]);
  949. $this->plugin->schedule($message);
  950. $this->assertEquals('1.1', $message->getScheduleStatus());
  951. }
  952. public function testImipDisabledForEvent(): void {
  953. // construct iTip message with event and attendees
  954. $calendar = new VCalendar();
  955. $calendar->add('VEVENT', ['UID' => 'uid-1234']);
  956. $event = $calendar->VEVENT;
  957. $event->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  958. $event->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  959. $event->add('X-NC-DISABLE-SCHEDULING', 'true');
  960. $message = new Message();
  961. $message->method = 'REQUEST';
  962. $message->message = $calendar;
  963. $message->sender = 'mailto:gandalf@wiz.ard';
  964. $message->senderName = 'Mr. Wizard';
  965. $message->recipient = 'mailto:' . 'frodo@hobb.it';
  966. $this->plugin->schedule($message);
  967. $this->assertEquals('1.0;We got the message, but iMip messages are disabled for this event', $message->scheduleStatus);
  968. }
  969. }