NotifierTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. public function dataPrepare(): array {
  121. return [
  122. [
  123. 'calendar_reminder',
  124. [
  125. 'title' => 'Title of this event',
  126. 'start_atom' => '2005-08-15T15:52:01+02:00'
  127. ],
  128. 'Title of this event (in 1 hour, 52 minutes)',
  129. [
  130. 'title' => 'Title of this event',
  131. 'description' => null,
  132. 'location' => 'NC Headquarters',
  133. 'all_day' => false,
  134. 'start_atom' => '2005-08-15T15:52:01+02:00',
  135. 'start_is_floating' => false,
  136. 'start_timezone' => 'Europe/Berlin',
  137. 'end_atom' => '2005-08-15T17:52:01+02:00',
  138. 'end_is_floating' => false,
  139. 'end_timezone' => 'Europe/Berlin',
  140. 'calendar_displayname' => 'Personal',
  141. ],
  142. "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"
  143. ],
  144. [
  145. 'calendar_reminder',
  146. [
  147. 'title' => 'Title of this event',
  148. 'start_atom' => '2005-08-15T13:00:00+02:00',
  149. ],
  150. 'Title of this event (1 hour ago)',
  151. [
  152. 'title' => 'Title of this event',
  153. 'description' => null,
  154. 'location' => 'NC Headquarters',
  155. 'all_day' => false,
  156. 'start_atom' => '2005-08-15T13:00:00+02:00',
  157. 'start_is_floating' => false,
  158. 'start_timezone' => 'Europe/Berlin',
  159. 'end_atom' => '2005-08-15T15:00:00+02:00',
  160. 'end_is_floating' => false,
  161. 'end_timezone' => 'Europe/Berlin',
  162. 'calendar_displayname' => 'Personal',
  163. ],
  164. "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"
  165. ],
  166. ];
  167. }
  168. /**
  169. * @dataProvider dataPrepare
  170. *
  171. * @param string $subjectType
  172. * @param array $subjectParams
  173. * @param string $subject
  174. * @param array $messageParams
  175. * @param string $message
  176. * @throws \Exception
  177. */
  178. public function testPrepare(string $subjectType, array $subjectParams, string $subject, array $messageParams, string $message): void {
  179. /** @var INotification|MockObject $notification */
  180. $notification = $this->createMock(INotification::class);
  181. $notification->expects($this->once())
  182. ->method('getApp')
  183. ->willReturn(Application::APP_ID);
  184. $notification->expects($this->once())
  185. ->method('getSubject')
  186. ->willReturn($subjectType);
  187. $notification->expects($this->once())
  188. ->method('getSubjectParameters')
  189. ->willReturn($subjectParams);
  190. $notification->expects($this->once())
  191. ->method('getMessageParameters')
  192. ->willReturn($messageParams);
  193. $notification->expects($this->once())
  194. ->method('setParsedSubject')
  195. ->with($subject)
  196. ->willReturnSelf();
  197. $notification->expects($this->once())
  198. ->method('setParsedMessage')
  199. ->with($message)
  200. ->willReturnSelf();
  201. $this->urlGenerator->expects($this->once())
  202. ->method('imagePath')
  203. ->with('core', 'places/calendar.svg')
  204. ->willReturn('icon-url');
  205. $this->urlGenerator->expects($this->once())
  206. ->method('getAbsoluteURL')
  207. ->with('icon-url')
  208. ->willReturn('absolute-icon-url');
  209. $notification->expects($this->once())
  210. ->method('setIcon')
  211. ->with('absolute-icon-url')
  212. ->willReturnSelf();
  213. $return = $this->notifier->prepare($notification, 'en');
  214. $this->assertEquals($notification, $return);
  215. }
  216. public function testPassedEvent(): void {
  217. /** @var INotification|MockObject $notification */
  218. $notification = $this->createMock(INotification::class);
  219. $notification->expects($this->once())
  220. ->method('getApp')
  221. ->willReturn(Application::APP_ID);
  222. $notification->expects($this->once())
  223. ->method('getSubject')
  224. ->willReturn('calendar_reminder');
  225. $notification->expects($this->once())
  226. ->method('getSubjectParameters')
  227. ->willReturn([
  228. 'title' => 'Title of this event',
  229. 'start_atom' => '2005-08-15T08:00:00+02:00'
  230. ]);
  231. $notification->expects($this->once())
  232. ->method('getMessageParameters')
  233. ->willReturn([
  234. 'title' => 'Title of this event',
  235. 'description' => null,
  236. 'location' => 'NC Headquarters',
  237. 'all_day' => false,
  238. 'start_atom' => '2005-08-15T08:00:00+02:00',
  239. 'start_is_floating' => false,
  240. 'start_timezone' => 'Europe/Berlin',
  241. 'end_atom' => '2005-08-15T13:00:00+02:00',
  242. 'end_is_floating' => false,
  243. 'end_timezone' => 'Europe/Berlin',
  244. 'calendar_displayname' => 'Personal',
  245. ]);
  246. $notification->expects($this->once())
  247. ->method('setParsedSubject')
  248. ->with('Title of this event (6 hours ago)')
  249. ->willReturnSelf();
  250. $this->expectException(AlreadyProcessedException::class);
  251. $return = $this->notifier->prepare($notification, 'en');
  252. $this->assertEquals($notification, $return);
  253. }
  254. }