NotifierTest.php 9.2 KB

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