NotifierTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 Thomas Citharel <tcit@tcit.fr>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\CalDAV\Reminder;
  26. use OCA\DAV\AppInfo\Application;
  27. use OCA\DAV\CalDAV\Reminder\Notifier;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\IL10N;
  30. use OCP\IURLGenerator;
  31. use OCP\L10N\IFactory;
  32. use OCP\Notification\INotification;
  33. use Test\TestCase;
  34. class NotifierTest extends TestCase {
  35. /** @var Notifier */
  36. protected $notifier;
  37. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $factory;
  39. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $urlGenerator;
  41. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  42. protected $l10n;
  43. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  44. protected $timeFactory;
  45. protected function setUp() {
  46. parent::setUp();
  47. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  48. $this->l10n = $this->createMock(IL10N::class);
  49. $this->l10n->expects($this->any())
  50. ->method('t')
  51. ->willReturnCallback(function($string, $args) {
  52. return vsprintf($string, $args);
  53. });
  54. $this->l10n->expects($this->any())
  55. ->method('l')
  56. ->willReturnCallback(function($string, $args) {
  57. /** \DateTime $args */
  58. return $args->format(\DateTime::ATOM);
  59. });
  60. $this->l10n->expects($this->any())
  61. ->method('n')
  62. ->willReturnCallback(function($textSingular, $textPlural, $count, $args) {
  63. $text = $count === 1 ? $textSingular : $textPlural;
  64. $text = str_replace('%n', (string)$count, $text);
  65. return vsprintf($text, $args);
  66. });
  67. $this->factory = $this->createMock(IFactory::class);
  68. $this->factory->expects($this->any())
  69. ->method('get')
  70. ->willReturn($this->l10n);
  71. $this->timeFactory = $this->createMock(ITimeFactory::class);
  72. $this->timeFactory
  73. ->method('getDateTime')
  74. ->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2005-08-15T14:00:00+02:00'));
  75. $this->notifier = new Notifier(
  76. $this->factory,
  77. $this->urlGenerator,
  78. $this->timeFactory
  79. );
  80. }
  81. public function testGetId():void {
  82. $this->assertEquals($this->notifier->getID(), 'dav');
  83. }
  84. public function testGetName():void {
  85. $this->assertEquals($this->notifier->getName(), 'Calendar');
  86. }
  87. /**
  88. * @expectedException \InvalidArgumentException
  89. * @expectedExceptionMessage Notification not from this app
  90. */
  91. public function testPrepareWrongApp(): void
  92. {
  93. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  94. $notification = $this->createMock(INotification::class);
  95. $notification->expects($this->once())
  96. ->method('getApp')
  97. ->willReturn('notifications');
  98. $notification->expects($this->never())
  99. ->method('getSubject');
  100. $this->notifier->prepare($notification, 'en');
  101. }
  102. /**
  103. * @expectedException \InvalidArgumentException
  104. * @expectedExceptionMessage Unknown subject
  105. */
  106. public function testPrepareWrongSubject() {
  107. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  108. $notification = $this->createMock(INotification::class);
  109. $notification->expects($this->once())
  110. ->method('getApp')
  111. ->willReturn(Application::APP_ID);
  112. $notification->expects($this->once())
  113. ->method('getSubject')
  114. ->willReturn('wrong subject');
  115. $this->notifier->prepare($notification, 'en');
  116. }
  117. public function dataPrepare(): array
  118. {
  119. return [
  120. [
  121. 'calendar_reminder',
  122. [
  123. 'title' => 'Title of this event',
  124. 'start_atom' => '2005-08-15T15:52:01+02:00'
  125. ],
  126. 'Title of this event (in 1 hour, 52 minutes)',
  127. [
  128. 'title' => 'Title of this event',
  129. 'description' => null,
  130. 'location' => 'NC Headquarters',
  131. 'all_day' => false,
  132. 'start_atom' => '2005-08-15T15:52:01+02:00',
  133. 'start_is_floating' => false,
  134. 'start_timezone' => 'Europe/Berlin',
  135. 'end_atom' => '2005-08-15T17:52:01+02:00',
  136. 'end_is_floating' => false,
  137. 'end_timezone' => 'Europe/Berlin',
  138. 'calendar_displayname' => 'Personal',
  139. ],
  140. "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"
  141. ],
  142. ];
  143. }
  144. /**
  145. * @dataProvider dataPrepare
  146. *
  147. * @param string $subjectType
  148. * @param array $subjectParams
  149. * @param string $subject
  150. * @param array $messageParams
  151. * @param string $message
  152. * @throws \Exception
  153. */
  154. public function testPrepare(string $subjectType, array $subjectParams, string $subject, array $messageParams, string $message): void
  155. {
  156. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  157. $notification = $this->createMock(INotification::class);
  158. $notification->expects($this->once())
  159. ->method('getApp')
  160. ->willReturn(Application::APP_ID);
  161. $notification->expects($this->once())
  162. ->method('getSubject')
  163. ->willReturn($subjectType);
  164. $notification->expects($this->once())
  165. ->method('getSubjectParameters')
  166. ->willReturn($subjectParams);
  167. $notification->expects($this->once())
  168. ->method('getMessageParameters')
  169. ->willReturn($messageParams);
  170. $notification->expects($this->once())
  171. ->method('setParsedSubject')
  172. ->with($subject)
  173. ->willReturnSelf();
  174. $notification->expects($this->once())
  175. ->method('setParsedMessage')
  176. ->with($message)
  177. ->willReturnSelf();
  178. $this->urlGenerator->expects($this->once())
  179. ->method('imagePath')
  180. ->with('core', 'places/calendar.svg')
  181. ->willReturn('icon-url');
  182. $this->urlGenerator->expects($this->once())
  183. ->method('getAbsoluteURL')
  184. ->with('icon-url')
  185. ->willReturn('absolute-icon-url');
  186. $notification->expects($this->once())
  187. ->method('setIcon')
  188. ->with('absolute-icon-url')
  189. ->willReturnSelf();
  190. $return = $this->notifier->prepare($notification, 'en');
  191. $this->assertEquals($notification, $return);
  192. }
  193. }