EmailProviderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. list($user1, $user2, $user3, , $user5) = $users = $this->getUsers();
  69. $enL10N = $this->createMock(IL10N::class);
  70. $enL10N->method('t')
  71. ->willReturnArgument(0);
  72. $enL10N->method('l')
  73. ->willReturnArgument(0);
  74. $deL10N = $this->createMock(IL10N::class);
  75. $deL10N->method('t')
  76. ->willReturnArgument(0);
  77. $deL10N->method('l')
  78. ->willReturnArgument(0);
  79. $this->l10nFactory
  80. ->method('getUserLanguage')
  81. ->willReturnMap([
  82. [$user1, 'en'],
  83. [$user2, 'de'],
  84. [$user3, 'de'],
  85. [$user5, 'de'],
  86. ]);
  87. $this->l10nFactory
  88. ->method('findLanguage')
  89. ->willReturn('en');
  90. $this->l10nFactory
  91. ->method('languageExists')
  92. ->willReturnMap([
  93. ['dav', 'en', true],
  94. ['dav', 'de', true],
  95. ]);
  96. $this->l10nFactory
  97. ->method('get')
  98. ->willReturnMap([
  99. ['dav', 'en', null, $enL10N],
  100. ['dav', 'de', null, $deL10N],
  101. ]);
  102. $template1 = $this->getTemplateMock();
  103. $message11 = $this->getMessageMock('uid1@example.com', $template1);
  104. $template2 = $this->getTemplateMock();
  105. $message21 = $this->getMessageMock('uid2@example.com', $template2);
  106. $message22 = $this->getMessageMock('uid3@example.com', $template2);
  107. $this->mailer->expects($this->at(0))
  108. ->method('createEMailTemplate')
  109. ->with('dav.calendarReminder')
  110. ->willReturn($template1);
  111. $this->mailer->expects($this->at(1))
  112. ->method('validateMailAddress')
  113. ->with('uid1@example.com')
  114. ->willReturn(true);
  115. $this->mailer->expects($this->at(2))
  116. ->method('createMessage')
  117. ->with()
  118. ->willReturn($message11);
  119. $this->mailer->expects($this->at(3))
  120. ->method('send')
  121. ->with($message11)
  122. ->willReturn([]);
  123. $this->mailer->expects($this->at(4))
  124. ->method('createEMailTemplate')
  125. ->with('dav.calendarReminder')
  126. ->willReturn($template2);
  127. $this->mailer->expects($this->at(5))
  128. ->method('validateMailAddress')
  129. ->with('uid2@example.com')
  130. ->willReturn(true);
  131. $this->mailer->expects($this->at(6))
  132. ->method('createMessage')
  133. ->with()
  134. ->willReturn($message21);
  135. $this->mailer->expects($this->at(7))
  136. ->method('send')
  137. ->with($message21)
  138. ->willReturn([]);
  139. $this->mailer->expects($this->at(8))
  140. ->method('validateMailAddress')
  141. ->with('uid3@example.com')
  142. ->willReturn(true);
  143. $this->mailer->expects($this->at(9))
  144. ->method('createMessage')
  145. ->with()
  146. ->willReturn($message22);
  147. $this->mailer->expects($this->at(10))
  148. ->method('send')
  149. ->with($message22)
  150. ->willReturn([]);
  151. $this->mailer->expects($this->at(11))
  152. ->method('validateMailAddress')
  153. ->with('invalid')
  154. ->willReturn(false);
  155. $this->setupURLGeneratorMock(2);
  156. $vcalendar = $this->getNoAttendeeVCalendar();
  157. $this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
  158. }
  159. public function testSendWithAttendees(): void {
  160. list($user1, $user2, $user3, , $user5) = $users = $this->getUsers();
  161. $enL10N = $this->createMock(IL10N::class);
  162. $enL10N->method('t')
  163. ->willReturnArgument(0);
  164. $enL10N->method('l')
  165. ->willReturnArgument(0);
  166. $deL10N = $this->createMock(IL10N::class);
  167. $deL10N->method('t')
  168. ->willReturnArgument(0);
  169. $deL10N->method('l')
  170. ->willReturnArgument(0);
  171. $this->l10nFactory
  172. ->method('getUserLanguage')
  173. ->willReturnMap([
  174. [$user1, 'en'],
  175. [$user2, 'de'],
  176. [$user3, 'de'],
  177. [$user5, 'de'],
  178. ]);
  179. $this->l10nFactory
  180. ->method('findLanguage')
  181. ->willReturn('en');
  182. $this->l10nFactory
  183. ->method('languageExists')
  184. ->willReturnMap([
  185. ['dav', 'en', true],
  186. ['dav', 'de', true],
  187. ]);
  188. $this->l10nFactory
  189. ->method('get')
  190. ->willReturnMap([
  191. ['dav', 'en', null, $enL10N],
  192. ['dav', 'de', null, $deL10N],
  193. ]);
  194. $template1 = $this->getTemplateMock();
  195. $message11 = $this->getMessageMock('foo1@example.org', $template1);
  196. $message12 = $this->getMessageMock('uid2@example.com', $template1);
  197. $message13 = $this->getMessageMock('uid3@example.com', $template1);
  198. $template2 = $this->getTemplateMock();
  199. $message21 = $this->getMessageMock('foo3@example.org', $template2);
  200. $message22 = $this->getMessageMock('foo4@example.org', $template2);
  201. $message23 = $this->getMessageMock('uid1@example.com', $template2);
  202. $this->mailer->expects(self::exactly(2))
  203. ->method('createEMailTemplate')
  204. ->with('dav.calendarReminder')
  205. ->willReturnOnConsecutiveCalls(
  206. $template1,
  207. $template2
  208. );
  209. $this->mailer->expects($this->atLeastOnce())
  210. ->method('validateMailAddress')
  211. ->willReturnMap([
  212. ['foo1@example.org', true],
  213. ['foo3@example.org', true],
  214. ['foo4@example.org', true],
  215. ['uid1@example.com', true],
  216. ['uid2@example.com', true],
  217. ['uid3@example.com', true],
  218. ['invalid', false]
  219. ]);
  220. $this->mailer->expects($this->exactly(6))
  221. ->method('createMessage')
  222. ->with()
  223. ->willReturnOnConsecutiveCalls(
  224. $message11,
  225. $message12,
  226. $message13,
  227. $message21,
  228. $message22,
  229. $message23
  230. );
  231. $this->mailer->expects($this->exactly(6))
  232. ->method('send')
  233. ->withConsecutive(
  234. [$message11],
  235. [$message12],
  236. [$message13],
  237. [$message21],
  238. [$message22],
  239. [$message23]
  240. )->willReturn([]);
  241. $this->setupURLGeneratorMock(2);
  242. $vcalendar = $this->getAttendeeVCalendar();
  243. $this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
  244. }
  245. /**
  246. * @return IEMailTemplate
  247. */
  248. private function getTemplateMock():IEMailTemplate {
  249. $template = $this->createMock(IEMailTemplate::class);
  250. $template->expects($this->at(0))
  251. ->method('addHeader')
  252. ->with()
  253. ->willReturn($template);
  254. $template->expects($this->at(1))
  255. ->method('setSubject')
  256. ->with()
  257. ->willReturn($template);
  258. $template->expects($this->at(2))
  259. ->method('addHeading')
  260. ->with()
  261. ->willReturn($template);
  262. $template->expects($this->at(3))
  263. ->method('addBodyListItem')
  264. ->with()
  265. ->willReturn($template);
  266. $template->expects($this->at(4))
  267. ->method('addBodyListItem')
  268. ->with()
  269. ->willReturn($template);
  270. $template->expects($this->at(5))
  271. ->method('addBodyListItem')
  272. ->with()
  273. ->willReturn($template);
  274. $template->expects($this->at(6))
  275. ->method('addBodyListItem')
  276. ->with()
  277. ->willReturn($template);
  278. $template->expects($this->at(7))
  279. ->method('addFooter')
  280. ->with()
  281. ->willReturn($template);
  282. return $template;
  283. }
  284. /**
  285. * @param string $toMail
  286. * @param IEMailTemplate $templateMock
  287. * @param array|null $replyTo
  288. * @return IMessage
  289. */
  290. private function getMessageMock(string $toMail, IEMailTemplate $templateMock, array $replyTo=null):IMessage {
  291. $message = $this->createMock(IMessage::class);
  292. $i = 0;
  293. $message->expects($this->at($i++))
  294. ->method('setFrom')
  295. ->with([\OCP\Util::getDefaultEmailAddress('reminders-noreply')])
  296. ->willReturn($message);
  297. if ($replyTo) {
  298. $message->expects($this->at($i++))
  299. ->method('setReplyTo')
  300. ->with($replyTo)
  301. ->willReturn($message);
  302. }
  303. $message->expects($this->at($i++))
  304. ->method('setTo')
  305. ->with([$toMail])
  306. ->willReturn($message);
  307. $message->expects($this->at($i++))
  308. ->method('useTemplate')
  309. ->with($templateMock)
  310. ->willReturn($message);
  311. return $message;
  312. }
  313. private function getNoAttendeeVCalendar():VCalendar {
  314. $vcalendar = new VCalendar();
  315. $vcalendar->add('VEVENT', [
  316. 'SUMMARY' => 'Fellowship meeting',
  317. 'DTSTART' => new \DateTime('2017-01-01 00:00:00+00:00'), // 1483228800,
  318. 'UID' => 'uid1234',
  319. 'LOCATION' => 'Location 123',
  320. 'DESCRIPTION' => 'DESCRIPTION 456',
  321. ]);
  322. return $vcalendar;
  323. }
  324. private function getAttendeeVCalendar():VCalendar {
  325. $vcalendar = new VCalendar();
  326. $vcalendar->add('VEVENT', [
  327. 'SUMMARY' => 'Fellowship meeting',
  328. 'DTSTART' => new \DateTime('2017-01-01 00:00:00+00:00'), // 1483228800,
  329. 'UID' => 'uid1234',
  330. 'LOCATION' => 'Location 123',
  331. 'DESCRIPTION' => 'DESCRIPTION 456',
  332. ]);
  333. $vcalendar->VEVENT->add(
  334. 'ATTENDEE',
  335. 'mailto:foo1@example.org',
  336. [
  337. 'LANG' => 'de',
  338. 'PARTSTAT' => 'NEEDS-ACTION',
  339. ]
  340. );
  341. $vcalendar->VEVENT->add(
  342. 'ATTENDEE',
  343. 'mailto:foo2@example.org',
  344. [
  345. 'LANG' => 'de',
  346. 'PARTSTAT' => 'DECLINED',
  347. ]
  348. );
  349. $vcalendar->VEVENT->add(
  350. 'ATTENDEE',
  351. 'mailto:foo3@example.org',
  352. [
  353. 'LANG' => 'en',
  354. 'PARTSTAT' => 'CONFIRMED',
  355. ]
  356. );
  357. $vcalendar->VEVENT->add(
  358. 'ATTENDEE',
  359. 'mailto:foo4@example.org'
  360. );
  361. $vcalendar->VEVENT->add(
  362. 'ATTENDEE',
  363. 'tomail:foo5@example.org'
  364. );
  365. return $vcalendar;
  366. }
  367. private function setupURLGeneratorMock(int $times=1):void {
  368. for ($i = 0; $i < $times; $i++) {
  369. $this->urlGenerator
  370. ->expects($this->at(8 * $i))
  371. ->method('imagePath')
  372. ->with('core', 'actions/info.png')
  373. ->willReturn('imagePath1');
  374. $this->urlGenerator
  375. ->expects($this->at(8 * $i + 1))
  376. ->method('getAbsoluteURL')
  377. ->with('imagePath1')
  378. ->willReturn('AbsURL1');
  379. $this->urlGenerator
  380. ->expects($this->at(8 * $i + 2))
  381. ->method('imagePath')
  382. ->with('core', 'places/calendar.png')
  383. ->willReturn('imagePath2');
  384. $this->urlGenerator
  385. ->expects($this->at(8 * $i + 3))
  386. ->method('getAbsoluteURL')
  387. ->with('imagePath2')
  388. ->willReturn('AbsURL2');
  389. $this->urlGenerator
  390. ->expects($this->at(8 * $i + 4))
  391. ->method('imagePath')
  392. ->with('core', 'actions/address.png')
  393. ->willReturn('imagePath3');
  394. $this->urlGenerator
  395. ->expects($this->at(8 * $i + 5))
  396. ->method('getAbsoluteURL')
  397. ->with('imagePath3')
  398. ->willReturn('AbsURL3');
  399. $this->urlGenerator
  400. ->expects($this->at(8 * $i + 6))
  401. ->method('imagePath')
  402. ->with('core', 'actions/more.png')
  403. ->willReturn('imagePath4');
  404. $this->urlGenerator
  405. ->expects($this->at(8 * $i + 7))
  406. ->method('getAbsoluteURL')
  407. ->with('imagePath4')
  408. ->willReturn('AbsURL4');
  409. }
  410. }
  411. private function getUsers(): array {
  412. $user1 = $this->createMock(IUser::class);
  413. $user1->method('getUID')
  414. ->willReturn('uid1');
  415. $user1->method('getEMailAddress')
  416. ->willReturn('uid1@example.com');
  417. $user2 = $this->createMock(IUser::class);
  418. $user2->method('getUID')
  419. ->willReturn('uid2');
  420. $user2->method('getEMailAddress')
  421. ->willReturn('uid2@example.com');
  422. $user3 = $this->createMock(IUser::class);
  423. $user3->method('getUID')
  424. ->willReturn('uid3');
  425. $user3->method('getEMailAddress')
  426. ->willReturn('uid3@example.com');
  427. $user4 = $this->createMock(IUser::class);
  428. $user4->method('getUID')
  429. ->willReturn('uid4');
  430. $user4->method('getEMailAddress')
  431. ->willReturn(null);
  432. $user5 = $this->createMock(IUser::class);
  433. $user5->method('getUID')
  434. ->willReturn('uid5');
  435. $user5->method('getEMailAddress')
  436. ->willReturn('invalid');
  437. return [$user1, $user2, $user3, $user4, $user5];
  438. }
  439. }