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

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