EmailProviderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Thomas Citharel
  5. * @copyright Copyright (c) 2019, Georg Ehrke
  6. *
  7. * @author Thomas Citharel <tcit@tcit.fr>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\CalDAV\Reminder\NotificationProvider;
  26. use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
  27. use OCP\IConfig;
  28. use OCP\IL10N;
  29. use OCP\ILogger;
  30. use OCP\IURLGenerator;
  31. use OCP\L10N\IFactory as L10NFactory;
  32. use OCP\IUser;
  33. use OCP\Mail\IEMailTemplate;
  34. use OCP\Mail\IMailer;
  35. use OCP\Mail\IAttachment;
  36. use OCP\Mail\IMessage;
  37. use Sabre\VObject\Component\VCalendar;
  38. use Test\TestCase;
  39. class EmailProviderTest extends AbstractNotificationProviderTest {
  40. const USER_EMAIL = 'frodo@hobb.it';
  41. /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
  42. protected $logger;
  43. /** @var L10NFactory|\PHPUnit\Framework\MockObject\MockObject */
  44. protected $l10nFactory;
  45. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  46. protected $l10n;
  47. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  48. protected $urlGenerator;
  49. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  50. protected $config;
  51. /** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */
  52. private $mailer;
  53. public function setUp() {
  54. parent::setUp();
  55. $this->mailer = $this->createMock(IMailer::class);
  56. $this->provider = new EmailProvider(
  57. $this->config,
  58. $this->mailer,
  59. $this->logger,
  60. $this->l10nFactory,
  61. $this->urlGenerator
  62. );
  63. }
  64. public function testSendWithoutAttendees():void {
  65. $user1 = $this->createMock(IUser::class);
  66. $user1->method('getUID')
  67. ->willReturn('uid1');
  68. $user1->method('getEMailAddress')
  69. ->willReturn('uid1@example.com');
  70. $user2 = $this->createMock(IUser::class);
  71. $user2->method('getUID')
  72. ->willReturn('uid2');
  73. $user2->method('getEMailAddress')
  74. ->willReturn('uid2@example.com');
  75. $user3 = $this->createMock(IUser::class);
  76. $user3->method('getUID')
  77. ->willReturn('uid3');
  78. $user3->method('getEMailAddress')
  79. ->willReturn('uid3@example.com');
  80. $user4 = $this->createMock(IUser::class);
  81. $user4->method('getUID')
  82. ->willReturn('uid4');
  83. $user4->method('getEMailAddress')
  84. ->willReturn(null);
  85. $users = [$user1, $user2, $user3, $user4];
  86. $this->config->expects($this->at(0))
  87. ->method('getUserValue')
  88. ->with('uid1', 'core', 'lang', null)
  89. ->willReturn(null);
  90. $this->config->expects($this->at(1))
  91. ->method('getUserValue')
  92. ->with('uid2', 'core', 'lang', null)
  93. ->willReturn('de');
  94. $this->config->expects($this->at(2))
  95. ->method('getUserValue')
  96. ->with('uid3', 'core', 'lang', null)
  97. ->willReturn('de');
  98. $enL10N = $this->createMock(IL10N::class);
  99. $enL10N->method('t')
  100. ->will($this->returnArgument(0));
  101. $enL10N->method('l')
  102. ->will($this->returnArgument(0));
  103. $deL10N = $this->createMock(IL10N::class);
  104. $deL10N->method('t')
  105. ->will($this->returnArgument(0));
  106. $deL10N->method('l')
  107. ->will($this->returnArgument(0));
  108. $this->l10nFactory->expects($this->at(0))
  109. ->method('findLanguage')
  110. ->with()
  111. ->willReturn('en');
  112. $this->l10nFactory->expects($this->at(1))
  113. ->method('languageExists')
  114. ->with('dav', 'en')
  115. ->willReturn(true);
  116. $this->l10nFactory->expects($this->at(2))
  117. ->method('get')
  118. ->with('dav', 'en')
  119. ->willReturn($enL10N);
  120. $this->l10nFactory->expects($this->at(3))
  121. ->method('languageExists')
  122. ->with('dav', 'de')
  123. ->willReturn(true);
  124. $this->l10nFactory->expects($this->at(4))
  125. ->method('get')
  126. ->with('dav', 'de')
  127. ->willReturn($deL10N);
  128. $template1 = $this->getTemplateMock();
  129. $message11 = $this->getMessageMock('uid1@example.com', $template1);
  130. $template2 = $this->getTemplateMock();
  131. $message21 = $this->getMessageMock('uid2@example.com', $template2);
  132. $message22 = $this->getMessageMock('uid3@example.com', $template2);
  133. $this->mailer->expects($this->at(0))
  134. ->method('createEMailTemplate')
  135. ->with('dav.calendarReminder')
  136. ->willReturn($template1);
  137. $this->mailer->expects($this->at(1))
  138. ->method('createMessage')
  139. ->with()
  140. ->willReturn($message11);
  141. $this->mailer->expects($this->at(2))
  142. ->method('send')
  143. ->with($message11)
  144. ->willReturn([]);
  145. $this->mailer->expects($this->at(3))
  146. ->method('createEMailTemplate')
  147. ->with('dav.calendarReminder')
  148. ->willReturn($template2);
  149. $this->mailer->expects($this->at(4))
  150. ->method('createMessage')
  151. ->with()
  152. ->willReturn($message21);
  153. $this->mailer->expects($this->at(5))
  154. ->method('send')
  155. ->with($message21)
  156. ->willReturn([]);
  157. $this->mailer->expects($this->at(6))
  158. ->method('createMessage')
  159. ->with()
  160. ->willReturn($message22);
  161. $this->mailer->expects($this->at(7))
  162. ->method('send')
  163. ->with($message22)
  164. ->willReturn([]);
  165. $this->setupURLGeneratorMock(2);
  166. $vcalendar = $this->getNoAttendeeVCalendar();
  167. $this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
  168. }
  169. public function testSendWithAttendees(): void {
  170. $user1 = $this->createMock(IUser::class);
  171. $user1->method('getUID')
  172. ->willReturn('uid1');
  173. $user1->method('getEMailAddress')
  174. ->willReturn('uid1@example.com');
  175. $user2 = $this->createMock(IUser::class);
  176. $user2->method('getUID')
  177. ->willReturn('uid2');
  178. $user2->method('getEMailAddress')
  179. ->willReturn('uid2@example.com');
  180. $user3 = $this->createMock(IUser::class);
  181. $user3->method('getUID')
  182. ->willReturn('uid3');
  183. $user3->method('getEMailAddress')
  184. ->willReturn('uid3@example.com');
  185. $user4 = $this->createMock(IUser::class);
  186. $user4->method('getUID')
  187. ->willReturn('uid4');
  188. $user4->method('getEMailAddress')
  189. ->willReturn(null);
  190. $users = [$user1, $user2, $user3, $user4];
  191. $this->config->expects($this->at(0))
  192. ->method('getUserValue')
  193. ->with('uid1', 'core', 'lang', null)
  194. ->willReturn(null);
  195. $this->config->expects($this->at(1))
  196. ->method('getUserValue')
  197. ->with('uid2', 'core', 'lang', null)
  198. ->willReturn('de');
  199. $this->config->expects($this->at(2))
  200. ->method('getUserValue')
  201. ->with('uid3', 'core', 'lang', null)
  202. ->willReturn('de');
  203. $enL10N = $this->createMock(IL10N::class);
  204. $enL10N->method('t')
  205. ->will($this->returnArgument(0));
  206. $enL10N->method('l')
  207. ->will($this->returnArgument(0));
  208. $deL10N = $this->createMock(IL10N::class);
  209. $deL10N->method('t')
  210. ->will($this->returnArgument(0));
  211. $deL10N->method('l')
  212. ->will($this->returnArgument(0));
  213. $this->l10nFactory->expects($this->at(0))
  214. ->method('findLanguage')
  215. ->with()
  216. ->willReturn('en');
  217. $this->l10nFactory->expects($this->at(1))
  218. ->method('languageExists')
  219. ->with('dav', 'de')
  220. ->willReturn(true);
  221. $this->l10nFactory->expects($this->at(2))
  222. ->method('get')
  223. ->with('dav', 'de')
  224. ->willReturn($enL10N);
  225. $this->l10nFactory->expects($this->at(3))
  226. ->method('languageExists')
  227. ->with('dav', 'en')
  228. ->willReturn(true);
  229. $this->l10nFactory->expects($this->at(4))
  230. ->method('get')
  231. ->with('dav', 'en')
  232. ->willReturn($deL10N);
  233. $template1 = $this->getTemplateMock();
  234. $message11 = $this->getMessageMock('foo1@example.org', $template1);
  235. $message12 = $this->getMessageMock('uid2@example.com', $template1);
  236. $message13 = $this->getMessageMock('uid3@example.com', $template1);
  237. $template2 = $this->getTemplateMock();
  238. $message21 = $this->getMessageMock('foo3@example.org', $template2);
  239. $message22 = $this->getMessageMock('foo4@example.org', $template2);
  240. $message23 = $this->getMessageMock('uid1@example.com', $template2);
  241. $this->mailer->expects($this->at(0))
  242. ->method('createEMailTemplate')
  243. ->with('dav.calendarReminder')
  244. ->willReturn($template1);
  245. $this->mailer->expects($this->at(1))
  246. ->method('createMessage')
  247. ->with()
  248. ->willReturn($message11);
  249. $this->mailer->expects($this->at(2))
  250. ->method('send')
  251. ->with($message11)
  252. ->willReturn([]);
  253. $this->mailer->expects($this->at(3))
  254. ->method('createMessage')
  255. ->with()
  256. ->willReturn($message12);
  257. $this->mailer->expects($this->at(4))
  258. ->method('send')
  259. ->with($message12)
  260. ->willReturn([]);
  261. $this->mailer->expects($this->at(5))
  262. ->method('createMessage')
  263. ->with()
  264. ->willReturn($message13);
  265. $this->mailer->expects($this->at(6))
  266. ->method('send')
  267. ->with($message13)
  268. ->willReturn([]);
  269. $this->mailer->expects($this->at(7))
  270. ->method('createEMailTemplate')
  271. ->with('dav.calendarReminder')
  272. ->willReturn($template2);
  273. $this->mailer->expects($this->at(8))
  274. ->method('createMessage')
  275. ->with()
  276. ->willReturn($message21);
  277. $this->mailer->expects($this->at(9))
  278. ->method('send')
  279. ->with($message21)
  280. ->willReturn([]);
  281. $this->mailer->expects($this->at(10))
  282. ->method('createMessage')
  283. ->with()
  284. ->willReturn($message22);
  285. $this->mailer->expects($this->at(11))
  286. ->method('send')
  287. ->with($message22)
  288. ->willReturn([]);
  289. $this->mailer->expects($this->at(12))
  290. ->method('createMessage')
  291. ->with()
  292. ->willReturn($message23);
  293. $this->mailer->expects($this->at(13))
  294. ->method('send')
  295. ->with($message23)
  296. ->willReturn([]);
  297. $this->setupURLGeneratorMock(2);
  298. $vcalendar = $this->getAttendeeVCalendar();
  299. $this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
  300. }
  301. /**
  302. * @return IEMailTemplate
  303. */
  304. private function getTemplateMock():IEMailTemplate {
  305. $template = $this->createMock(IEMailTemplate::class);
  306. $template->expects($this->at(0))
  307. ->method('addHeader')
  308. ->with()
  309. ->willReturn($template);
  310. $template->expects($this->at(1))
  311. ->method('setSubject')
  312. ->with()
  313. ->willReturn($template);
  314. $template->expects($this->at(2))
  315. ->method('addHeading')
  316. ->with()
  317. ->willReturn($template);
  318. $template->expects($this->at(3))
  319. ->method('addBodyListItem')
  320. ->with()
  321. ->willReturn($template);
  322. $template->expects($this->at(4))
  323. ->method('addBodyListItem')
  324. ->with()
  325. ->willReturn($template);
  326. $template->expects($this->at(5))
  327. ->method('addBodyListItem')
  328. ->with()
  329. ->willReturn($template);
  330. $template->expects($this->at(6))
  331. ->method('addBodyListItem')
  332. ->with()
  333. ->willReturn($template);
  334. $template->expects($this->at(7))
  335. ->method('addFooter')
  336. ->with()
  337. ->willReturn($template);
  338. return $template;
  339. }
  340. /**
  341. * @param array $toMail
  342. * @param IEMailTemplate $templateMock
  343. * @param array $replyTo
  344. * @return IMessage
  345. */
  346. private function getMessageMock(string $toMail, IEMailTemplate $templateMock, array $replyTo=null):IMessage {
  347. $message = $this->createMock(IMessage::class);
  348. $i = 0;
  349. $message->expects($this->at($i++))
  350. ->method('setFrom')
  351. ->with([\OCP\Util::getDefaultEmailAddress('reminders-noreply')])
  352. ->willReturn($message);
  353. if ($replyTo) {
  354. $message->expects($this->at($i++))
  355. ->method('setReplyTo')
  356. ->with($replyTo)
  357. ->willReturn($message);
  358. }
  359. $message->expects($this->at($i++))
  360. ->method('setTo')
  361. ->with([$toMail])
  362. ->willReturn($message);
  363. $message->expects($this->at($i++))
  364. ->method('useTemplate')
  365. ->with($templateMock)
  366. ->willReturn($message);
  367. return $message;
  368. }
  369. private function getNoAttendeeVCalendar():VCalendar {
  370. $vcalendar = new VCalendar();
  371. $vcalendar->add('VEVENT', [
  372. 'SUMMARY' => 'Fellowship meeting',
  373. 'DTSTART' => new \DateTime('2017-01-01 00:00:00+00:00'), // 1483228800,
  374. 'UID' => 'uid1234',
  375. 'LOCATION' => 'Location 123',
  376. 'DESCRIPTION' => 'DESCRIPTION 456',
  377. ]);
  378. return $vcalendar;
  379. }
  380. private function getAttendeeVCalendar():VCalendar {
  381. $vcalendar = new VCalendar();
  382. $vcalendar->add('VEVENT', [
  383. 'SUMMARY' => 'Fellowship meeting',
  384. 'DTSTART' => new \DateTime('2017-01-01 00:00:00+00:00'), // 1483228800,
  385. 'UID' => 'uid1234',
  386. 'LOCATION' => 'Location 123',
  387. 'DESCRIPTION' => 'DESCRIPTION 456',
  388. ]);
  389. $vcalendar->VEVENT->add(
  390. 'ATTENDEE',
  391. 'mailto:foo1@example.org',
  392. [
  393. 'LANG' => 'de',
  394. 'PARTSTAT' => 'NEEDS-ACTION',
  395. ]
  396. );
  397. $vcalendar->VEVENT->add(
  398. 'ATTENDEE',
  399. 'mailto:foo2@example.org',
  400. [
  401. 'LANG' => 'de',
  402. 'PARTSTAT' => 'DECLINED',
  403. ]
  404. );
  405. $vcalendar->VEVENT->add(
  406. 'ATTENDEE',
  407. 'mailto:foo3@example.org',
  408. [
  409. 'LANG' => 'en',
  410. 'PARTSTAT' => 'CONFIRMED',
  411. ]
  412. );
  413. $vcalendar->VEVENT->add(
  414. 'ATTENDEE',
  415. 'mailto:foo4@example.org'
  416. );
  417. $vcalendar->VEVENT->add(
  418. 'ATTENDEE',
  419. 'tomail:foo5@example.org'
  420. );
  421. return $vcalendar;
  422. }
  423. private function setupURLGeneratorMock(int $times=1):void {
  424. for ($i = 0; $i < $times; $i++) {
  425. $this->urlGenerator
  426. ->expects($this->at(8 * $i))
  427. ->method('imagePath')
  428. ->with('core', 'actions/info.svg')
  429. ->willReturn('imagePath1');
  430. $this->urlGenerator
  431. ->expects($this->at(8 * $i + 1))
  432. ->method('getAbsoluteURL')
  433. ->with('imagePath1')
  434. ->willReturn('AbsURL1');
  435. $this->urlGenerator
  436. ->expects($this->at(8 * $i + 2))
  437. ->method('imagePath')
  438. ->with('core', 'places/calendar.svg')
  439. ->willReturn('imagePath2');
  440. $this->urlGenerator
  441. ->expects($this->at(8 * $i + 3))
  442. ->method('getAbsoluteURL')
  443. ->with('imagePath2')
  444. ->willReturn('AbsURL2');
  445. $this->urlGenerator
  446. ->expects($this->at(8 * $i + 4))
  447. ->method('imagePath')
  448. ->with('core', 'actions/address.svg')
  449. ->willReturn('imagePath3');
  450. $this->urlGenerator
  451. ->expects($this->at(8 * $i + 5))
  452. ->method('getAbsoluteURL')
  453. ->with('imagePath3')
  454. ->willReturn('AbsURL3');
  455. $this->urlGenerator
  456. ->expects($this->at(8 * $i + 6))
  457. ->method('imagePath')
  458. ->with('core', 'actions/more.svg')
  459. ->willReturn('imagePath4');
  460. $this->urlGenerator
  461. ->expects($this->at(8 * $i + 7))
  462. ->method('getAbsoluteURL')
  463. ->with('imagePath4')
  464. ->willReturn('AbsURL4');
  465. }
  466. }
  467. }