NotifierTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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\INotification;
  36. use Test\TestCase;
  37. class NotifierTest extends TestCase {
  38. /** @var Notifier */
  39. protected $notifier;
  40. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  41. protected $factory;
  42. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  43. protected $urlGenerator;
  44. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  45. protected $l10n;
  46. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  47. protected $timeFactory;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  51. $this->l10n = $this->createMock(IL10N::class);
  52. $this->l10n->expects($this->any())
  53. ->method('t')
  54. ->willReturnCallback(function ($string, $args) {
  55. return vsprintf($string, $args);
  56. });
  57. $this->l10n->expects($this->any())
  58. ->method('l')
  59. ->willReturnCallback(function ($string, $args) {
  60. /** \DateTime $args */
  61. return $args->format(\DateTime::ATOM);
  62. });
  63. $this->l10n->expects($this->any())
  64. ->method('n')
  65. ->willReturnCallback(function ($textSingular, $textPlural, $count, $args) {
  66. $text = $count === 1 ? $textSingular : $textPlural;
  67. $text = str_replace('%n', (string)$count, $text);
  68. return vsprintf($text, $args);
  69. });
  70. $this->factory = $this->createMock(IFactory::class);
  71. $this->factory->expects($this->any())
  72. ->method('get')
  73. ->willReturn($this->l10n);
  74. $this->timeFactory = $this->createMock(ITimeFactory::class);
  75. $this->timeFactory
  76. ->method('getDateTime')
  77. ->willReturn(\DateTime::createFromFormat(\DateTime::ATOM, '2005-08-15T14:00:00+02:00'));
  78. $this->notifier = new Notifier(
  79. $this->factory,
  80. $this->urlGenerator,
  81. $this->timeFactory
  82. );
  83. }
  84. public function testGetId():void {
  85. $this->assertEquals($this->notifier->getID(), 'dav');
  86. }
  87. public function testGetName():void {
  88. $this->assertEquals($this->notifier->getName(), 'Calendar');
  89. }
  90. public function testPrepareWrongApp(): void {
  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. return [
  117. [
  118. 'calendar_reminder',
  119. [
  120. 'title' => 'Title of this event',
  121. 'start_atom' => '2005-08-15T15:52:01+02:00'
  122. ],
  123. 'Title of this event (in 1 hour, 52 minutes)',
  124. [
  125. 'title' => 'Title of this event',
  126. 'description' => null,
  127. 'location' => 'NC Headquarters',
  128. 'all_day' => false,
  129. 'start_atom' => '2005-08-15T15:52:01+02:00',
  130. 'start_is_floating' => false,
  131. 'start_timezone' => 'Europe/Berlin',
  132. 'end_atom' => '2005-08-15T17:52:01+02:00',
  133. 'end_is_floating' => false,
  134. 'end_timezone' => 'Europe/Berlin',
  135. 'calendar_displayname' => 'Personal',
  136. ],
  137. "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"
  138. ],
  139. ];
  140. }
  141. /**
  142. * @dataProvider dataPrepare
  143. *
  144. * @param string $subjectType
  145. * @param array $subjectParams
  146. * @param string $subject
  147. * @param array $messageParams
  148. * @param string $message
  149. * @throws \Exception
  150. */
  151. public function testPrepare(string $subjectType, array $subjectParams, string $subject, array $messageParams, string $message): void {
  152. /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
  153. $notification = $this->createMock(INotification::class);
  154. $notification->expects($this->once())
  155. ->method('getApp')
  156. ->willReturn(Application::APP_ID);
  157. $notification->expects($this->once())
  158. ->method('getSubject')
  159. ->willReturn($subjectType);
  160. $notification->expects($this->once())
  161. ->method('getSubjectParameters')
  162. ->willReturn($subjectParams);
  163. $notification->expects($this->once())
  164. ->method('getMessageParameters')
  165. ->willReturn($messageParams);
  166. $notification->expects($this->once())
  167. ->method('setParsedSubject')
  168. ->with($subject)
  169. ->willReturnSelf();
  170. $notification->expects($this->once())
  171. ->method('setParsedMessage')
  172. ->with($message)
  173. ->willReturnSelf();
  174. $this->urlGenerator->expects($this->once())
  175. ->method('imagePath')
  176. ->with('core', 'places/calendar.svg')
  177. ->willReturn('icon-url');
  178. $this->urlGenerator->expects($this->once())
  179. ->method('getAbsoluteURL')
  180. ->with('icon-url')
  181. ->willReturn('absolute-icon-url');
  182. $notification->expects($this->once())
  183. ->method('setIcon')
  184. ->with('absolute-icon-url')
  185. ->willReturnSelf();
  186. $return = $this->notifier->prepare($notification, 'en');
  187. $this->assertEquals($notification, $return);
  188. }
  189. }