IMipPluginTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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\Schedule\IMipPlugin;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\DB\QueryBuilder\IQueryBuilder;
  34. use OCP\Defaults;
  35. use OCP\IConfig;
  36. use OCP\IDBConnection;
  37. use OCP\IL10N;
  38. use OCP\ILogger;
  39. use OCP\IURLGenerator;
  40. use OCP\IUser;
  41. use OCP\IUserManager;
  42. use OCP\L10N\IFactory;
  43. use OCP\Mail\IAttachment;
  44. use OCP\Mail\IEMailTemplate;
  45. use OCP\Mail\IMailer;
  46. use OCP\Mail\IMessage;
  47. use OCP\Security\ISecureRandom;
  48. use PHPUnit\Framework\MockObject\MockObject;
  49. use Sabre\VObject\Component\VCalendar;
  50. use Sabre\VObject\ITip\Message;
  51. use Test\TestCase;
  52. class IMipPluginTest extends TestCase {
  53. /** @var IMessage|MockObject */
  54. private $mailMessage;
  55. /** @var IMailer|MockObject */
  56. private $mailer;
  57. /** @var IEMailTemplate|MockObject */
  58. private $emailTemplate;
  59. /** @var IAttachment|MockObject */
  60. private $emailAttachment;
  61. /** @var ITimeFactory|MockObject */
  62. private $timeFactory;
  63. /** @var IConfig|MockObject */
  64. private $config;
  65. /** @var IUserManager|MockObject */
  66. private $userManager;
  67. /** @var IQueryBuilder|MockObject */
  68. private $queryBuilder;
  69. /** @var IMipPlugin */
  70. private $plugin;
  71. protected function setUp(): void {
  72. $this->mailMessage = $this->createMock(IMessage::class);
  73. $this->mailMessage->method('setFrom')->willReturn($this->mailMessage);
  74. $this->mailMessage->method('setReplyTo')->willReturn($this->mailMessage);
  75. $this->mailMessage->method('setTo')->willReturn($this->mailMessage);
  76. $this->mailer = $this->getMockBuilder(IMailer::class)->disableOriginalConstructor()->getMock();
  77. $this->mailer->method('createMessage')->willReturn($this->mailMessage);
  78. $this->emailTemplate = $this->createMock(IEMailTemplate::class);
  79. $this->mailer->method('createEMailTemplate')->willReturn($this->emailTemplate);
  80. $this->emailAttachment = $this->createMock(IAttachment::class);
  81. $this->mailer->method('createAttachment')->willReturn($this->emailAttachment);
  82. /** @var ILogger|MockObject $logger */
  83. $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
  84. $this->timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock();
  85. $this->timeFactory->method('getTime')->willReturn(1496912528); // 2017-01-01
  86. $this->config = $this->createMock(IConfig::class);
  87. $this->userManager = $this->createMock(IUserManager::class);
  88. $l10n = $this->createMock(IL10N::class);
  89. $l10n->method('t')
  90. ->willReturnCallback(function ($text, $parameters = []) {
  91. return vsprintf($text, $parameters);
  92. });
  93. $l10nFactory = $this->createMock(IFactory::class);
  94. $l10nFactory->method('get')->willReturn($l10n);
  95. $urlGenerator = $this->createMock(IURLGenerator::class);
  96. $this->queryBuilder = $this->createMock(IQueryBuilder::class);
  97. $db = $this->createMock(IDBConnection::class);
  98. $db->method('getQueryBuilder')
  99. ->with()
  100. ->willReturn($this->queryBuilder);
  101. $random = $this->createMock(ISecureRandom::class);
  102. $random->method('generate')
  103. ->with(60, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
  104. ->willReturn('random_token');
  105. $defaults = $this->createMock(Defaults::class);
  106. $defaults->method('getName')
  107. ->willReturn('Instance Name 123');
  108. $this->plugin = new IMipPlugin($this->config, $this->mailer, $logger, $this->timeFactory, $l10nFactory, $urlGenerator, $defaults, $random, $db, $this->userManager, 'user123');
  109. }
  110. public function testDelivery() {
  111. $this->config
  112. ->method('getAppValue')
  113. ->with('dav', 'invitation_link_recipients', 'yes')
  114. ->willReturn('yes');
  115. $message = $this->_testMessage();
  116. $this->_expectSend();
  117. $this->plugin->schedule($message);
  118. $this->assertEquals('1.1', $message->getScheduleStatus());
  119. }
  120. public function testFailedDelivery() {
  121. $this->config
  122. ->method('getAppValue')
  123. ->with('dav', 'invitation_link_recipients', 'yes')
  124. ->willReturn('yes');
  125. $message = $this->_testMessage();
  126. $this->mailer
  127. ->method('send')
  128. ->willThrowException(new \Exception());
  129. $this->_expectSend();
  130. $this->plugin->schedule($message);
  131. $this->assertEquals('5.0', $message->getScheduleStatus());
  132. }
  133. public function testDeliveryWithNoCommonName() {
  134. $this->config
  135. ->method('getAppValue')
  136. ->with('dav', 'invitation_link_recipients', 'yes')
  137. ->willReturn('yes');
  138. $message = $this->_testMessage();
  139. $message->senderName = null;
  140. $user = $this->createMock(IUser::class);
  141. $user->method('getDisplayName')->willReturn('Mr. Wizard');
  142. $this->userManager->expects($this->once())
  143. ->method('get')
  144. ->with('user123')
  145. ->willReturn($user);
  146. $this->_expectSend();
  147. $this->plugin->schedule($message);
  148. $this->assertEquals('1.1', $message->getScheduleStatus());
  149. }
  150. /**
  151. * @dataProvider dataNoMessageSendForPastEvents
  152. */
  153. public function testNoMessageSendForPastEvents(array $veventParams, bool $expectsMail) {
  154. $this->config
  155. ->method('getAppValue')
  156. ->with('dav', 'invitation_link_recipients', 'yes')
  157. ->willReturn('yes');
  158. $message = $this->_testMessage($veventParams);
  159. $this->_expectSend('frodo@hobb.it', $expectsMail, $expectsMail);
  160. $this->plugin->schedule($message);
  161. if ($expectsMail) {
  162. $this->assertEquals('1.1', $message->getScheduleStatus());
  163. } else {
  164. $this->assertEquals(false, $message->getScheduleStatus());
  165. }
  166. }
  167. public function dataNoMessageSendForPastEvents() {
  168. return [
  169. [['DTSTART' => new \DateTime('2017-01-01 00:00:00')], false],
  170. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00')], false],
  171. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-12-31 00:00:00')], true],
  172. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P1D'], false],
  173. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P52W'], true],
  174. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY'], true],
  175. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=3'], false],
  176. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20170301T000000Z'], false],
  177. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=33'], true],
  178. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20171001T000000Z'], true],
  179. ];
  180. }
  181. /**
  182. * @dataProvider dataIncludeResponseButtons
  183. */
  184. public function testIncludeResponseButtons(string $config_setting, string $recipient, bool $has_buttons) {
  185. $message = $this->_testMessage([],$recipient);
  186. $this->_expectSend($recipient, true, $has_buttons);
  187. $this->config
  188. ->method('getAppValue')
  189. ->with('dav', 'invitation_link_recipients', 'yes')
  190. ->willReturn($config_setting);
  191. $this->plugin->schedule($message);
  192. $this->assertEquals('1.1', $message->getScheduleStatus());
  193. }
  194. public function dataIncludeResponseButtons() {
  195. return [
  196. // dav.invitation_link_recipients, recipient, $has_buttons
  197. [ 'yes', 'joe@internal.com', true],
  198. [ 'joe@internal.com', 'joe@internal.com', true],
  199. [ 'internal.com', 'joe@internal.com', true],
  200. [ 'pete@otherinternal.com,internal.com', 'joe@internal.com', true],
  201. [ 'no', 'joe@internal.com', false],
  202. [ 'internal.com', 'joe@external.com', false],
  203. [ 'jane@otherinternal.com,internal.com', 'joe@otherinternal.com', false],
  204. ];
  205. }
  206. public function testMessageSendWhenEventWithoutName() {
  207. $this->config
  208. ->method('getAppValue')
  209. ->with('dav', 'invitation_link_recipients', 'yes')
  210. ->willReturn('yes');
  211. $message = $this->_testMessage(['SUMMARY' => '']);
  212. $this->_expectSend('frodo@hobb.it', true, true,'Invitation: Untitled event');
  213. $this->emailTemplate->expects($this->once())
  214. ->method('addHeading')
  215. ->with('Mr. Wizard invited you to »Untitled event«');
  216. $this->plugin->schedule($message);
  217. $this->assertEquals('1.1', $message->getScheduleStatus());
  218. }
  219. private function _testMessage(array $attrs = [], string $recipient = 'frodo@hobb.it') {
  220. $message = new Message();
  221. $message->method = 'REQUEST';
  222. $message->message = new VCalendar();
  223. $message->message->add('VEVENT', array_merge([
  224. 'UID' => 'uid-1234',
  225. 'SEQUENCE' => 0,
  226. 'SUMMARY' => 'Fellowship meeting',
  227. 'DTSTART' => new \DateTime('2018-01-01 00:00:00')
  228. ], $attrs));
  229. $message->message->VEVENT->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  230. $message->message->VEVENT->add('ATTENDEE', 'mailto:'.$recipient, [ 'RSVP' => 'TRUE' ]);
  231. $message->sender = 'mailto:gandalf@wiz.ard';
  232. $message->senderName = 'Mr. Wizard';
  233. $message->recipient = 'mailto:'.$recipient;
  234. return $message;
  235. }
  236. private function _expectSend(string $recipient = 'frodo@hobb.it', bool $expectSend = true, bool $expectButtons = true, string $subject = 'Invitation: Fellowship meeting') {
  237. // if the event is in the past, we skip out
  238. if (!$expectSend) {
  239. $this->mailer
  240. ->expects($this->never())
  241. ->method('send');
  242. return;
  243. }
  244. $this->emailTemplate->expects($this->once())
  245. ->method('setSubject')
  246. ->with($subject);
  247. $this->mailMessage->expects($this->once())
  248. ->method('setTo')
  249. ->with([$recipient => null]);
  250. $this->mailMessage->expects($this->once())
  251. ->method('setReplyTo')
  252. ->with(['gandalf@wiz.ard' => 'Mr. Wizard']);
  253. $this->mailMessage->expects($this->once())
  254. ->method('setFrom')
  255. ->with(['invitations-noreply@localhost' => 'Mr. Wizard via Instance Name 123']);
  256. $this->mailer
  257. ->expects($this->once())
  258. ->method('send');
  259. if ($expectButtons) {
  260. $this->queryBuilder->expects($this->at(0))
  261. ->method('insert')
  262. ->with('calendar_invitations')
  263. ->willReturn($this->queryBuilder);
  264. $this->queryBuilder->expects($this->at(8))
  265. ->method('values')
  266. ->willReturn($this->queryBuilder);
  267. $this->queryBuilder->expects($this->at(9))
  268. ->method('execute');
  269. } else {
  270. $this->queryBuilder->expects($this->never())
  271. ->method('insert')
  272. ->with('calendar_invitations');
  273. }
  274. }
  275. }