IMipPluginTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\CalDAV\Schedule;
  27. use OC\Mail\Mailer;
  28. use OCA\DAV\CalDAV\Schedule\IMipPlugin;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\DB\QueryBuilder\IQueryBuilder;
  31. use OCP\Defaults;
  32. use OCP\IConfig;
  33. use OCP\IDBConnection;
  34. use OCP\IL10N;
  35. use OCP\ILogger;
  36. use OCP\IURLGenerator;
  37. use OCP\L10N\IFactory;
  38. use OCP\Mail\IAttachment;
  39. use OCP\Mail\IEMailTemplate;
  40. use OCP\Mail\IMailer;
  41. use OCP\Mail\IMessage;
  42. use OCP\Security\ISecureRandom;
  43. use Sabre\VObject\Component\VCalendar;
  44. use Sabre\VObject\ITip\Message;
  45. use Test\TestCase;
  46. class IMipPluginTest extends TestCase {
  47. public function testDelivery() {
  48. $mailMessage = $this->createMock(IMessage::class);
  49. $mailMessage->method('setFrom')->willReturn($mailMessage);
  50. $mailMessage->method('setReplyTo')->willReturn($mailMessage);
  51. $mailMessage->method('setTo')->willReturn($mailMessage);
  52. /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */
  53. $mailer = $this->getMockBuilder(IMailer::class)->disableOriginalConstructor()->getMock();
  54. $emailTemplate = $this->createMock(IEMailTemplate::class);
  55. $emailAttachment = $this->createMock(IAttachment::class);
  56. $mailer->method('createEMailTemplate')->willReturn($emailTemplate);
  57. $mailer->method('createMessage')->willReturn($mailMessage);
  58. $mailer->method('createAttachment')->willReturn($emailAttachment);
  59. $mailer->expects($this->once())->method('send');
  60. /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */
  61. $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
  62. $timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock();
  63. $timeFactory->method('getTime')->willReturn(1);
  64. /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */
  65. $config = $this->createMock(IConfig::class);
  66. $l10n = $this->createMock(IL10N::class);
  67. /** @var IFactory | \PHPUnit_Framework_MockObject_MockObject $l10nFactory */
  68. $l10nFactory = $this->createMock(IFactory::class);
  69. $l10nFactory->method('get')->willReturn($l10n);
  70. /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject $urlGenerator */
  71. $urlGenerator = $this->createMock(IURLGenerator::class);
  72. /** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject $db */
  73. $db = $this->createMock(IDBConnection::class);
  74. /** @var ISecureRandom | \PHPUnit_Framework_MockObject_MockObject $random */
  75. $random = $this->createMock(ISecureRandom::class);
  76. /** @var Defaults | \PHPUnit_Framework_MockObject_MockObject $defaults */
  77. $defaults = $this->createMock(Defaults::class);
  78. $defaults->expects($this->once())
  79. ->method('getName')
  80. ->will($this->returnValue('Instance Name 123'));
  81. $random->expects($this->once())
  82. ->method('generate')
  83. ->with(60, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
  84. ->will($this->returnValue('random_token'));
  85. $queryBuilder = $this->createMock(IQueryBuilder::class);
  86. $db->expects($this->once())
  87. ->method('getQueryBuilder')
  88. ->with()
  89. ->will($this->returnValue($queryBuilder));
  90. $queryBuilder->expects($this->at(0))
  91. ->method('insert')
  92. ->with('calendar_invitations')
  93. ->will($this->returnValue($queryBuilder));
  94. $queryBuilder->expects($this->at(8))
  95. ->method('values')
  96. ->will($this->returnValue($queryBuilder));
  97. $queryBuilder->expects($this->at(9))
  98. ->method('execute');
  99. $plugin = new IMipPlugin($config, $mailer, $logger, $timeFactory, $l10nFactory, $urlGenerator, $defaults, $random, $db, 'user123');
  100. $message = new Message();
  101. $message->method = 'REQUEST';
  102. $message->message = new VCalendar();
  103. $message->message->add('VEVENT', [
  104. 'UID' => $message->uid,
  105. 'SEQUENCE' => $message->sequence,
  106. 'SUMMARY' => 'Fellowship meeting',
  107. 'DTSTART' => new \DateTime('2017-01-01 00:00:00') // 1483228800
  108. ]);
  109. $message->sender = 'mailto:gandalf@wiz.ard';
  110. $message->recipient = 'mailto:frodo@hobb.it';
  111. $emailTemplate->expects($this->once())
  112. ->method('setSubject')
  113. ->with('Invitation: Fellowship meeting');
  114. $mailMessage->expects($this->once())
  115. ->method('setTo')
  116. ->with(['frodo@hobb.it' => null]);
  117. $mailMessage->expects($this->once())
  118. ->method('setReplyTo')
  119. ->with(['gandalf@wiz.ard' => null]);
  120. $plugin->schedule($message);
  121. $this->assertEquals('1.1', $message->getScheduleStatus());
  122. }
  123. public function testFailedDelivery() {
  124. $mailMessage = $this->createMock(IMessage::class);
  125. $mailMessage->method('setFrom')->willReturn($mailMessage);
  126. $mailMessage->method('setReplyTo')->willReturn($mailMessage);
  127. $mailMessage->method('setTo')->willReturn($mailMessage);
  128. /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */
  129. $mailer = $this->getMockBuilder(IMailer::class)->disableOriginalConstructor()->getMock();
  130. $emailTemplate = $this->createMock(IEMailTemplate::class);
  131. $emailAttachment = $this->createMock(IAttachment::class);
  132. $mailer->method('createEMailTemplate')->willReturn($emailTemplate);
  133. $mailer->method('createMessage')->willReturn($mailMessage);
  134. $mailer->method('createAttachment')->willReturn($emailAttachment);
  135. $mailer->method('send')->willThrowException(new \Exception());
  136. /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */
  137. $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
  138. $timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock();
  139. $timeFactory->method('getTime')->willReturn(1);
  140. /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */
  141. $config = $this->createMock(IConfig::class);
  142. $l10n = $this->createMock(IL10N::class);
  143. /** @var IFactory | \PHPUnit_Framework_MockObject_MockObject $l10nFactory */
  144. $l10nFactory = $this->createMock(IFactory::class);
  145. $l10nFactory->method('get')->willReturn($l10n);
  146. /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject $urlGenerator */
  147. $urlGenerator = $this->createMock(IURLGenerator::class);
  148. /** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject $db */
  149. $db = $this->createMock(IDBConnection::class);
  150. /** @var ISecureRandom | \PHPUnit_Framework_MockObject_MockObject $random */
  151. $random = $this->createMock(ISecureRandom::class);
  152. /** @var Defaults | \PHPUnit_Framework_MockObject_MockObject $defaults */
  153. $defaults = $this->createMock(Defaults::class);
  154. $random->expects($this->once())
  155. ->method('generate')
  156. ->with(60, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
  157. ->will($this->returnValue('random_token'));
  158. $queryBuilder = $this->createMock(IQueryBuilder::class);
  159. $db->expects($this->once())
  160. ->method('getQueryBuilder')
  161. ->with()
  162. ->will($this->returnValue($queryBuilder));
  163. $queryBuilder->expects($this->at(0))
  164. ->method('insert')
  165. ->with('calendar_invitations')
  166. ->will($this->returnValue($queryBuilder));
  167. $queryBuilder->expects($this->at(8))
  168. ->method('values')
  169. ->will($this->returnValue($queryBuilder));
  170. $queryBuilder->expects($this->at(9))
  171. ->method('execute');
  172. $plugin = new IMipPlugin($config, $mailer, $logger, $timeFactory, $l10nFactory, $urlGenerator, $defaults, $random, $db, 'user123');
  173. $message = new Message();
  174. $message->method = 'REQUEST';
  175. $message->message = new VCalendar();
  176. $message->message->add('VEVENT', [
  177. 'UID' => $message->uid,
  178. 'SEQUENCE' => $message->sequence,
  179. 'SUMMARY' => 'Fellowship meeting',
  180. 'DTSTART' => new \DateTime('2017-01-01 00:00:00') // 1483228800
  181. ]);
  182. $message->sender = 'mailto:gandalf@wiz.ard';
  183. $message->recipient = 'mailto:frodo@hobb.it';
  184. $emailTemplate->expects($this->once())
  185. ->method('setSubject')
  186. ->with('Invitation: Fellowship meeting');
  187. $mailMessage->expects($this->once())
  188. ->method('setTo')
  189. ->with(['frodo@hobb.it' => null]);
  190. $mailMessage->expects($this->once())
  191. ->method('setReplyTo')
  192. ->with(['gandalf@wiz.ard' => null]);
  193. $plugin->schedule($message);
  194. $this->assertEquals('5.0', $message->getScheduleStatus());
  195. }
  196. /**
  197. * @dataProvider dataNoMessageSendForPastEvents
  198. */
  199. public function testNoMessageSendForPastEvents($veventParams, $expectsMail) {
  200. $mailMessage = $this->createMock(IMessage::class);
  201. $mailMessage->method('setFrom')->willReturn($mailMessage);
  202. $mailMessage->method('setReplyTo')->willReturn($mailMessage);
  203. $mailMessage->method('setTo')->willReturn($mailMessage);
  204. /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */
  205. $mailer = $this->getMockBuilder(IMailer::class)->disableOriginalConstructor()->getMock();
  206. $emailTemplate = $this->createMock(IEMailTemplate::class);
  207. $emailAttachment = $this->createMock(IAttachment::class);
  208. $mailer->method('createEMailTemplate')->willReturn($emailTemplate);
  209. $mailer->method('createMessage')->willReturn($mailMessage);
  210. $mailer->method('createAttachment')->willReturn($emailAttachment);
  211. if ($expectsMail) {
  212. $mailer->expects($this->once())->method('send');
  213. } else {
  214. $mailer->expects($this->never())->method('send');
  215. }
  216. /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */
  217. $logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
  218. $timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock();
  219. $timeFactory->method('getTime')->willReturn(1496912528);
  220. /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject $config */
  221. $config = $this->createMock(IConfig::class);
  222. $l10n = $this->createMock(IL10N::class);
  223. /** @var IFactory | \PHPUnit_Framework_MockObject_MockObject $l10nFactory */
  224. $l10nFactory = $this->createMock(IFactory::class);
  225. $l10nFactory->method('get')->willReturn($l10n);
  226. /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject $urlGenerator */
  227. $urlGenerator = $this->createMock(IURLGenerator::class);
  228. /** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject $db */
  229. $db = $this->createMock(IDBConnection::class);
  230. /** @var ISecureRandom | \PHPUnit_Framework_MockObject_MockObject $random */
  231. $random = $this->createMock(ISecureRandom::class);
  232. /** @var Defaults | \PHPUnit_Framework_MockObject_MockObject $defaults */
  233. $defaults = $this->createMock(Defaults::class);
  234. if ($expectsMail) {
  235. $random->expects($this->once())
  236. ->method('generate')
  237. ->with(60, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
  238. ->will($this->returnValue('random_token'));
  239. $queryBuilder = $this->createMock(IQueryBuilder::class);
  240. $db->expects($this->once())
  241. ->method('getQueryBuilder')
  242. ->with()
  243. ->will($this->returnValue($queryBuilder));
  244. $queryBuilder->expects($this->at(0))
  245. ->method('insert')
  246. ->with('calendar_invitations')
  247. ->will($this->returnValue($queryBuilder));
  248. $queryBuilder->expects($this->at(8))
  249. ->method('values')
  250. ->will($this->returnValue($queryBuilder));
  251. $queryBuilder->expects($this->at(9))
  252. ->method('execute');
  253. }
  254. $plugin = new IMipPlugin($config, $mailer, $logger, $timeFactory, $l10nFactory, $urlGenerator, $defaults, $random, $db, 'user123');
  255. $message = new Message();
  256. $message->method = 'REQUEST';
  257. $message->message = new VCalendar();
  258. $message->message->add('VEVENT', array_merge([
  259. 'UID' => 'uid1337',
  260. 'SEQUENCE' => 42,
  261. 'SUMMARY' => 'Fellowship meeting',
  262. ], $veventParams));
  263. $message->sender = 'mailto:gandalf@wiz.ard';
  264. $message->recipient = 'mailto:frodo@hobb.it';
  265. $plugin->schedule($message);
  266. if ($expectsMail) {
  267. $this->assertEquals('1.1', $message->getScheduleStatus());
  268. } else {
  269. $this->assertEquals(false, $message->getScheduleStatus());
  270. }
  271. }
  272. public function dataNoMessageSendForPastEvents() {
  273. return [
  274. [['DTSTART' => new \DateTime('2017-01-01 00:00:00')], false],
  275. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00')], false],
  276. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-12-31 00:00:00')], true],
  277. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P1D'], false],
  278. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => 'P52W'], true],
  279. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY'], true],
  280. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=3'], false],
  281. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20170301T000000Z'], false],
  282. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=33'], true],
  283. [['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20171001T000000Z'], true],
  284. ];
  285. }
  286. }