You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PushProviderTest.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\NotificationProvider;
  29. use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\IConfig;
  32. use OCP\IL10N;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\L10N\IFactory as L10NFactory;
  36. use OCP\Notification\IManager;
  37. use OCP\Notification\INotification;
  38. class PushProviderTest extends AbstractNotificationProviderTest {
  39. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  40. private $manager;
  41. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  42. private $timeFactory;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->manager = $this->createMock(IManager::class);
  46. $this->timeFactory = $this->createMock(ITimeFactory::class);
  47. $this->provider = new PushProvider(
  48. $this->config,
  49. $this->manager,
  50. $this->logger,
  51. $this->l10nFactory,
  52. $this->urlGenerator,
  53. $this->timeFactory
  54. );
  55. }
  56. public function testNotificationType():void {
  57. $this->assertEquals(PushProvider::NOTIFICATION_TYPE, 'DISPLAY');
  58. }
  59. public function testNotSend(): void {
  60. $this->config->expects($this->once())
  61. ->method('getAppValue')
  62. ->with('dav', 'sendEventRemindersPush', 'no')
  63. ->willReturn('no');
  64. $this->manager->expects($this->never())
  65. ->method('createNotification');
  66. $this->manager->expects($this->never())
  67. ->method('notify');
  68. $user1 = $this->createMock(IUser::class);
  69. $user1->method('getUID')
  70. ->willReturn('uid1');
  71. $user2 = $this->createMock(IUser::class);
  72. $user2->method('getUID')
  73. ->willReturn('uid2');
  74. $user3 = $this->createMock(IUser::class);
  75. $user3->method('getUID')
  76. ->willReturn('uid3');
  77. $users = [$user1, $user2, $user3];
  78. $this->provider->send($this->vcalendar->VEVENT, $this->calendarDisplayName, $users);
  79. }
  80. public function testSend(): void {
  81. $this->config->expects($this->once())
  82. ->method('getAppValue')
  83. ->with('dav', 'sendEventRemindersPush', 'no')
  84. ->willReturn('yes');
  85. $user1 = $this->createMock(IUser::class);
  86. $user1->method('getUID')
  87. ->willReturn('uid1');
  88. $user2 = $this->createMock(IUser::class);
  89. $user2->method('getUID')
  90. ->willReturn('uid2');
  91. $user3 = $this->createMock(IUser::class);
  92. $user3->method('getUID')
  93. ->willReturn('uid3');
  94. $users = [$user1, $user2, $user3];
  95. $dateTime = new \DateTime('@946684800');
  96. $this->timeFactory->method('getDateTime')
  97. ->with()
  98. ->willReturn($dateTime);
  99. $notification1 = $this->createNotificationMock('uid1', $dateTime);
  100. $notification2 = $this->createNotificationMock('uid2', $dateTime);
  101. $notification3 = $this->createNotificationMock('uid3', $dateTime);
  102. $this->manager->expects($this->at(0))
  103. ->method('createNotification')
  104. ->with()
  105. ->willReturn($notification1);
  106. $this->manager->expects($this->at(2))
  107. ->method('createNotification')
  108. ->with()
  109. ->willReturn($notification2);
  110. $this->manager->expects($this->at(4))
  111. ->method('createNotification')
  112. ->with()
  113. ->willReturn($notification3);
  114. $this->manager->expects($this->at(1))
  115. ->method('notify')
  116. ->with($notification1);
  117. $this->manager->expects($this->at(3))
  118. ->method('notify')
  119. ->with($notification2);
  120. $this->manager->expects($this->at(5))
  121. ->method('notify')
  122. ->with($notification3);
  123. $this->provider->send($this->vcalendar->VEVENT, $this->calendarDisplayName, $users);
  124. }
  125. /**
  126. * @param string $uid
  127. * @param \DateTime $dt
  128. */
  129. private function createNotificationMock(string $uid, \DateTime $dt):INotification {
  130. $notification = $this->createMock(INotification::class);
  131. $notification
  132. ->expects($this->once())
  133. ->method('setApp')
  134. ->with('dav')
  135. ->willReturn($notification);
  136. $notification->expects($this->once())
  137. ->method('setUser')
  138. ->with($uid)
  139. ->willReturn($notification);
  140. $notification->expects($this->once())
  141. ->method('setDateTime')
  142. ->with($dt)
  143. ->willReturn($notification);
  144. $notification->expects($this->once())
  145. ->method('setObject')
  146. ->with('dav', hash('sha256', 'uid1234', false))
  147. ->willReturn($notification);
  148. $notification->expects($this->once())
  149. ->method('setSubject')
  150. ->with('calendar_reminder', [
  151. 'title' => 'Fellowship meeting',
  152. 'start_atom' => '2017-01-01T00:00:00+00:00',
  153. ])
  154. ->willReturn($notification);
  155. $notification
  156. ->expects($this->once())
  157. ->method('setMessage')
  158. ->with('calendar_reminder', [
  159. 'title' => 'Fellowship meeting',
  160. 'start_atom' => '2017-01-01T00:00:00+00:00',
  161. 'description' => null,
  162. 'location' => null,
  163. 'all_day' => false,
  164. 'start_is_floating' => false,
  165. 'start_timezone' => 'UTC',
  166. 'end_atom' => '2017-01-01T00:00:00+00:00',
  167. 'end_is_floating' => false,
  168. 'end_timezone' => 'UTC',
  169. 'calendar_displayname' => 'Personal',
  170. ])
  171. ->willReturn($notification);
  172. return $notification;
  173. }
  174. }