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 6.4KB

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