You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IMipPluginTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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\IURLGenerator;
  39. use OCP\IUser;
  40. use OCP\IUserManager;
  41. use OCP\L10N\IFactory;
  42. use OCP\Mail\IAttachment;
  43. use OCP\Mail\IEMailTemplate;
  44. use OCP\Mail\IMailer;
  45. use OCP\Mail\IMessage;
  46. use OCP\Security\ISecureRandom;
  47. use PHPUnit\Framework\MockObject\MockObject;
  48. use Psr\Log\LoggerInterface;
  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 LoggerInterface|MockObject $logger */
  83. $logger = $this->getMockBuilder(LoggerInterface::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. ->expects($this->at(1))
  113. ->method('getAppValue')
  114. ->with('dav', 'invitation_link_recipients', 'yes')
  115. ->willReturn('yes');
  116. $this->mailer->method('validateMailAddress')->willReturn(true);
  117. $message = $this->_testMessage();
  118. $this->_expectSend();
  119. $this->plugin->schedule($message);
  120. $this->assertEquals('1.1', $message->getScheduleStatus());
  121. }
  122. public function testFailedDelivery() {
  123. $this->config
  124. ->expects($this->at(1))
  125. ->method('getAppValue')
  126. ->with('dav', 'invitation_link_recipients', 'yes')
  127. ->willReturn('yes');
  128. $this->mailer->method('validateMailAddress')->willReturn(true);
  129. $message = $this->_testMessage();
  130. $this->mailer
  131. ->method('send')
  132. ->willThrowException(new \Exception());
  133. $this->_expectSend();
  134. $this->plugin->schedule($message);
  135. $this->assertEquals('5.0', $message->getScheduleStatus());
  136. }
  137. public function testInvalidEmailDelivery() {
  138. $this->mailer->method('validateMailAddress')->willReturn(false);
  139. $message = $this->_testMessage();
  140. $this->plugin->schedule($message);
  141. $this->assertEquals('5.0', $message->getScheduleStatus());
  142. }
  143. public function testDeliveryWithNoCommonName() {
  144. $this->config
  145. ->expects($this->at(1))
  146. ->method('getAppValue')
  147. ->with('dav', 'invitation_link_recipients', 'yes')
  148. ->willReturn('yes');
  149. $this->mailer->method('validateMailAddress')->willReturn(true);
  150. $message = $this->_testMessage();
  151. $message->senderName = null;
  152. $user = $this->createMock(IUser::class);
  153. $user->method('getDisplayName')->willReturn('Mr. Wizard');
  154. $this->userManager->expects($this->once())
  155. ->method('get')
  156. ->with('user123')
  157. ->willReturn($user);
  158. $this->_expectSend();
  159. $this->plugin->schedule($message);
  160. $this->assertEquals('1.1', $message->getScheduleStatus());
  161. }
  162. /**
  163. * @dataProvider dataNoMessageSendForPastEvents
  164. */
  165. public function testNoMessageSendForPastEvents(array $veventParams, bool $expectsMail) {
  166. $this->config
  167. ->method('getAppValue')
  168. ->willReturn('yes');
  169. $this->mailer->method('validateMailAddress')->willReturn(true);
  170. $message = $this->_testMessage($veventParams);
  171. $this->_expectSend('frodo@hobb.it', $expectsMail, $expectsMail);
  172. $this->plugin->schedule($message);
  173. if ($expectsMail) {
  174. $this->assertEquals('1.1', $message->getScheduleStatus());
  175. } else {
  176. $this->assertEquals(false, $message->getScheduleStatus());
  177. }
  178. }
  179. public function dataNoMessageSendForPastEvents() {
  180. return [
  181. [['DTSTART' => new \DateTime('2017-01-01 00:00:00')], false],
  182. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00')], false],
  183. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-12-31 00:00:00')], true],
  184. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P1D'], false],
  185. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P52W'], true],
  186. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY'], true],
  187. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=3'], false],
  188. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20170301T000000Z'], false],
  189. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=33'], true],
  190. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20171001T000000Z'], true],
  191. ];
  192. }
  193. /**
  194. * @dataProvider dataIncludeResponseButtons
  195. */
  196. public function testIncludeResponseButtons(string $config_setting, string $recipient, bool $has_buttons) {
  197. $message = $this->_testMessage([],$recipient);
  198. $this->mailer->method('validateMailAddress')->willReturn(true);
  199. $this->_expectSend($recipient, true, $has_buttons);
  200. $this->config
  201. ->expects($this->at(1))
  202. ->method('getAppValue')
  203. ->with('dav', 'invitation_link_recipients', 'yes')
  204. ->willReturn($config_setting);
  205. $this->plugin->schedule($message);
  206. $this->assertEquals('1.1', $message->getScheduleStatus());
  207. }
  208. public function dataIncludeResponseButtons() {
  209. return [
  210. // dav.invitation_link_recipients, recipient, $has_buttons
  211. [ 'yes', 'joe@internal.com', true],
  212. [ 'joe@internal.com', 'joe@internal.com', true],
  213. [ 'internal.com', 'joe@internal.com', true],
  214. [ 'pete@otherinternal.com,internal.com', 'joe@internal.com', true],
  215. [ 'no', 'joe@internal.com', false],
  216. [ 'internal.com', 'joe@external.com', false],
  217. [ 'jane@otherinternal.com,internal.com', 'joe@otherinternal.com', false],
  218. ];
  219. }
  220. public function testMessageSendWhenEventWithoutName() {
  221. $this->config
  222. ->method('getAppValue')
  223. ->willReturn('yes');
  224. $this->mailer->method('validateMailAddress')->willReturn(true);
  225. $message = $this->_testMessage(['SUMMARY' => '']);
  226. $this->_expectSend('frodo@hobb.it', true, true,'Invitation: Untitled event');
  227. $this->emailTemplate->expects($this->once())
  228. ->method('addHeading')
  229. ->with('Invitation');
  230. $this->plugin->schedule($message);
  231. $this->assertEquals('1.1', $message->getScheduleStatus());
  232. }
  233. private function _testMessage(array $attrs = [], string $recipient = 'frodo@hobb.it') {
  234. $message = new Message();
  235. $message->method = 'REQUEST';
  236. $message->message = new VCalendar();
  237. $message->message->add('VEVENT', array_merge([
  238. 'UID' => 'uid-1234',
  239. 'SEQUENCE' => 0,
  240. 'SUMMARY' => 'Fellowship meeting',
  241. 'DTSTART' => new \DateTime('2018-01-01 00:00:00')
  242. ], $attrs));
  243. $message->message->VEVENT->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  244. $message->message->VEVENT->add('ATTENDEE', 'mailto:'.$recipient, [ 'RSVP' => 'TRUE' ]);
  245. $message->sender = 'mailto:gandalf@wiz.ard';
  246. $message->senderName = 'Mr. Wizard';
  247. $message->recipient = 'mailto:'.$recipient;
  248. return $message;
  249. }
  250. private function _expectSend(string $recipient = 'frodo@hobb.it', bool $expectSend = true, bool $expectButtons = true, string $subject = 'Invitation: Fellowship meeting') {
  251. // if the event is in the past, we skip out
  252. if (!$expectSend) {
  253. $this->mailer
  254. ->expects($this->never())
  255. ->method('send');
  256. return;
  257. }
  258. $this->emailTemplate->expects($this->once())
  259. ->method('setSubject')
  260. ->with($subject);
  261. $this->mailMessage->expects($this->once())
  262. ->method('setTo')
  263. ->with([$recipient => null]);
  264. $this->mailMessage->expects($this->once())
  265. ->method('setReplyTo')
  266. ->with(['gandalf@wiz.ard' => 'Mr. Wizard']);
  267. $this->mailMessage->expects($this->once())
  268. ->method('setFrom')
  269. ->with(['invitations-noreply@localhost' => 'Mr. Wizard via Instance Name 123']);
  270. $this->mailer
  271. ->expects($this->once())
  272. ->method('send');
  273. if ($expectButtons) {
  274. $this->queryBuilder->expects($this->at(0))
  275. ->method('insert')
  276. ->with('calendar_invitations')
  277. ->willReturn($this->queryBuilder);
  278. $this->queryBuilder->expects($this->at(8))
  279. ->method('values')
  280. ->willReturn($this->queryBuilder);
  281. $this->queryBuilder->expects($this->at(9))
  282. ->method('execute');
  283. } else {
  284. $this->queryBuilder->expects($this->never())
  285. ->method('insert')
  286. ->with('calendar_invitations');
  287. }
  288. }
  289. }