NotifierTest.php 6.6 KB

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