NotifierTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\unit\CalDAV\Reminder;
  8. use OCA\DAV\AppInfo\Application;
  9. use OCA\DAV\CalDAV\Reminder\Notifier;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\IL10N;
  12. use OCP\IURLGenerator;
  13. use OCP\L10N\IFactory;
  14. use OCP\Notification\AlreadyProcessedException;
  15. use OCP\Notification\INotification;
  16. use OCP\Notification\UnknownNotificationException;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Test\TestCase;
  19. class NotifierTest extends TestCase {
  20. /** @var Notifier */
  21. protected $notifier;
  22. /** @var IFactory|MockObject */
  23. protected $factory;
  24. /** @var IURLGenerator|MockObject */
  25. protected $urlGenerator;
  26. /** @var IL10N|MockObject */
  27. protected $l10n;
  28. /** @var ITimeFactory|MockObject */
  29. protected $timeFactory;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  33. $this->l10n = $this->createMock(IL10N::class);
  34. $this->l10n->expects($this->any())
  35. ->method('t')
  36. ->willReturnCallback(function ($string, $args) {
  37. if (!is_array($args)) {
  38. $args = [$args];
  39. }
  40. return vsprintf($string, $args);
  41. });
  42. $this->l10n->expects($this->any())
  43. ->method('l')
  44. ->willReturnCallback(function ($string, $args) {
  45. /** \DateTime $args */
  46. return $args->format(\DateTime::ATOM);
  47. });
  48. $this->l10n->expects($this->any())
  49. ->method('n')
  50. ->willReturnCallback(function ($textSingular, $textPlural, $count, $args) {
  51. $text = $count === 1 ? $textSingular : $textPlural;
  52. $text = str_replace('%n', (string)$count, $text);
  53. return vsprintf($text, $args);
  54. });
  55. $this->factory = $this->createMock(IFactory::class);
  56. $this->factory->expects($this->any())
  57. ->method('get')
  58. ->willReturn($this->l10n);
  59. $this->timeFactory = $this->createMock(ITimeFactory::class);
  60. $this->timeFactory
  61. ->method('getDateTime')
  62. ->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2005-08-15T14:00:00+02:00'));
  63. $this->notifier = new Notifier(
  64. $this->factory,
  65. $this->urlGenerator,
  66. $this->timeFactory
  67. );
  68. }
  69. public function testGetId():void {
  70. $this->assertEquals($this->notifier->getID(), 'dav');
  71. }
  72. public function testGetName():void {
  73. $this->assertEquals($this->notifier->getName(), 'Calendar');
  74. }
  75. public function testPrepareWrongApp(): void {
  76. $this->expectException(UnknownNotificationException::class);
  77. $this->expectExceptionMessage('Notification not from this app');
  78. /** @var INotification|MockObject $notification */
  79. $notification = $this->createMock(INotification::class);
  80. $notification->expects($this->once())
  81. ->method('getApp')
  82. ->willReturn('notifications');
  83. $notification->expects($this->never())
  84. ->method('getSubject');
  85. $this->notifier->prepare($notification, 'en');
  86. }
  87. public function testPrepareWrongSubject(): void {
  88. $this->expectException(UnknownNotificationException::class);
  89. $this->expectExceptionMessage('Unknown subject');
  90. /** @var INotification|MockObject $notification */
  91. $notification = $this->createMock(INotification::class);
  92. $notification->expects($this->once())
  93. ->method('getApp')
  94. ->willReturn(Application::APP_ID);
  95. $notification->expects($this->once())
  96. ->method('getSubject')
  97. ->willReturn('wrong subject');
  98. $this->notifier->prepare($notification, 'en');
  99. }
  100. private static function hasPhpDatetimeDiffBug(): bool {
  101. $d1 = \DateTime::createFromFormat(\DateTimeInterface::ATOM, '2023-11-22T11:52:00+01:00');
  102. $d2 = new \DateTime('2023-11-22T10:52:03', new \DateTimeZone('UTC'));
  103. // The difference is 3 seconds, not -1year+11months+…
  104. return $d1->diff($d2)->y < 0;
  105. }
  106. public function dataPrepare(): array {
  107. return [
  108. [
  109. 'calendar_reminder',
  110. [
  111. 'title' => 'Title of this event',
  112. 'start_atom' => '2005-08-15T15:52:01+02:00'
  113. ],
  114. self::hasPhpDatetimeDiffBug() ? 'Title of this event' : 'Title of this event (in 1 hour, 52 minutes)',
  115. [
  116. 'title' => 'Title of this event',
  117. 'description' => null,
  118. 'location' => 'NC Headquarters',
  119. 'all_day' => false,
  120. 'start_atom' => '2005-08-15T15:52:01+02:00',
  121. 'start_is_floating' => false,
  122. 'start_timezone' => 'Europe/Berlin',
  123. 'end_atom' => '2005-08-15T17:52:01+02:00',
  124. 'end_is_floating' => false,
  125. 'end_timezone' => 'Europe/Berlin',
  126. 'calendar_displayname' => 'Personal',
  127. ],
  128. "Calendar: Personal\r\nDate: 2005-08-15T15:52:01+02:00, 2005-08-15T15:52:01+02:00 - 2005-08-15T17:52:01+02:00 (Europe/Berlin)\r\nWhere: NC Headquarters"
  129. ],
  130. [
  131. 'calendar_reminder',
  132. [
  133. 'title' => 'Title of this event',
  134. 'start_atom' => '2005-08-15T13:00:00+02:00',
  135. ],
  136. self::hasPhpDatetimeDiffBug() ? 'Title of this event' : 'Title of this event (1 hour ago)',
  137. [
  138. 'title' => 'Title of this event',
  139. 'description' => null,
  140. 'location' => 'NC Headquarters',
  141. 'all_day' => false,
  142. 'start_atom' => '2005-08-15T13:00:00+02:00',
  143. 'start_is_floating' => false,
  144. 'start_timezone' => 'Europe/Berlin',
  145. 'end_atom' => '2005-08-15T15:00:00+02:00',
  146. 'end_is_floating' => false,
  147. 'end_timezone' => 'Europe/Berlin',
  148. 'calendar_displayname' => 'Personal',
  149. ],
  150. "Calendar: Personal\r\nDate: 2005-08-15T13:00:00+02:00, 2005-08-15T13:00:00+02:00 - 2005-08-15T15:00:00+02:00 (Europe/Berlin)\r\nWhere: NC Headquarters"
  151. ],
  152. ];
  153. }
  154. /**
  155. * @dataProvider dataPrepare
  156. *
  157. * @param string $subjectType
  158. * @param array $subjectParams
  159. * @param string $subject
  160. * @param array $messageParams
  161. * @param string $message
  162. * @throws \Exception
  163. */
  164. public function testPrepare(string $subjectType, array $subjectParams, string $subject, array $messageParams, string $message): void {
  165. /** @var INotification|MockObject $notification */
  166. $notification = $this->createMock(INotification::class);
  167. $notification->expects($this->once())
  168. ->method('getApp')
  169. ->willReturn(Application::APP_ID);
  170. $notification->expects($this->once())
  171. ->method('getSubject')
  172. ->willReturn($subjectType);
  173. $notification->expects($this->once())
  174. ->method('getSubjectParameters')
  175. ->willReturn($subjectParams);
  176. $notification->expects($this->once())
  177. ->method('getMessageParameters')
  178. ->willReturn($messageParams);
  179. $notification->expects($this->once())
  180. ->method('setParsedSubject')
  181. ->with($subject)
  182. ->willReturnSelf();
  183. $notification->expects($this->once())
  184. ->method('setParsedMessage')
  185. ->with($message)
  186. ->willReturnSelf();
  187. $this->urlGenerator->expects($this->once())
  188. ->method('imagePath')
  189. ->with('core', 'places/calendar.svg')
  190. ->willReturn('icon-url');
  191. $this->urlGenerator->expects($this->once())
  192. ->method('getAbsoluteURL')
  193. ->with('icon-url')
  194. ->willReturn('absolute-icon-url');
  195. $notification->expects($this->once())
  196. ->method('setIcon')
  197. ->with('absolute-icon-url')
  198. ->willReturnSelf();
  199. $return = $this->notifier->prepare($notification, 'en');
  200. $this->assertEquals($notification, $return);
  201. }
  202. public function testPassedEvent(): void {
  203. /** @var INotification|MockObject $notification */
  204. $notification = $this->createMock(INotification::class);
  205. $notification->expects($this->once())
  206. ->method('getApp')
  207. ->willReturn(Application::APP_ID);
  208. $notification->expects($this->once())
  209. ->method('getSubject')
  210. ->willReturn('calendar_reminder');
  211. $notification->expects($this->once())
  212. ->method('getSubjectParameters')
  213. ->willReturn([
  214. 'title' => 'Title of this event',
  215. 'start_atom' => '2005-08-15T08:00:00+02:00'
  216. ]);
  217. $notification->expects($this->once())
  218. ->method('getMessageParameters')
  219. ->willReturn([
  220. 'title' => 'Title of this event',
  221. 'description' => null,
  222. 'location' => 'NC Headquarters',
  223. 'all_day' => false,
  224. 'start_atom' => '2005-08-15T08:00:00+02:00',
  225. 'start_is_floating' => false,
  226. 'start_timezone' => 'Europe/Berlin',
  227. 'end_atom' => '2005-08-15T13:00:00+02:00',
  228. 'end_is_floating' => false,
  229. 'end_timezone' => 'Europe/Berlin',
  230. 'calendar_displayname' => 'Personal',
  231. ]);
  232. $notification->expects($this->once())
  233. ->method('setParsedSubject')
  234. ->with(self::hasPhpDatetimeDiffBug() ? 'Title of this event' : 'Title of this event (6 hours ago)')
  235. ->willReturnSelf();
  236. $this->expectException(AlreadyProcessedException::class);
  237. $return = $this->notifier->prepare($notification, 'en');
  238. $this->assertEquals($notification, $return);
  239. }
  240. }