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 14KB

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