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.

EmailProviderTest.php 13KB

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