PushProviderTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * @author Richard Steinmetz <richard@steinmetz.cloud>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\DAV\Tests\unit\CalDAV\Reminder\NotificationProvider;
  30. use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\IConfig;
  33. use OCP\IL10N;
  34. use OCP\IURLGenerator;
  35. use OCP\IUser;
  36. use OCP\L10N\IFactory as L10NFactory;
  37. use OCP\Notification\IManager;
  38. use OCP\Notification\INotification;
  39. class PushProviderTest extends AbstractNotificationProviderTest {
  40. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  41. private $manager;
  42. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  43. private $timeFactory;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->manager = $this->createMock(IManager::class);
  47. $this->timeFactory = $this->createMock(ITimeFactory::class);
  48. $this->provider = new PushProvider(
  49. $this->config,
  50. $this->manager,
  51. $this->logger,
  52. $this->l10nFactory,
  53. $this->urlGenerator,
  54. $this->timeFactory
  55. );
  56. }
  57. public function testNotificationType():void {
  58. $this->assertEquals(PushProvider::NOTIFICATION_TYPE, 'DISPLAY');
  59. }
  60. public function testNotSend(): void {
  61. $this->config->expects($this->once())
  62. ->method('getAppValue')
  63. ->with('dav', 'sendEventRemindersPush', 'no')
  64. ->willReturn('no');
  65. $this->manager->expects($this->never())
  66. ->method('createNotification');
  67. $this->manager->expects($this->never())
  68. ->method('notify');
  69. $user1 = $this->createMock(IUser::class);
  70. $user1->method('getUID')
  71. ->willReturn('uid1');
  72. $user2 = $this->createMock(IUser::class);
  73. $user2->method('getUID')
  74. ->willReturn('uid2');
  75. $user3 = $this->createMock(IUser::class);
  76. $user3->method('getUID')
  77. ->willReturn('uid3');
  78. $users = [$user1, $user2, $user3];
  79. $this->provider->send($this->vcalendar->VEVENT, $this->calendarDisplayName, [], $users);
  80. }
  81. public function testSend(): void {
  82. $this->config->expects($this->once())
  83. ->method('getAppValue')
  84. ->with('dav', 'sendEventRemindersPush', 'no')
  85. ->willReturn('yes');
  86. $user1 = $this->createMock(IUser::class);
  87. $user1->method('getUID')
  88. ->willReturn('uid1');
  89. $user2 = $this->createMock(IUser::class);
  90. $user2->method('getUID')
  91. ->willReturn('uid2');
  92. $user3 = $this->createMock(IUser::class);
  93. $user3->method('getUID')
  94. ->willReturn('uid3');
  95. $users = [$user1, $user2, $user3];
  96. $dateTime = new \DateTime('@946684800');
  97. $this->timeFactory->method('getDateTime')
  98. ->with()
  99. ->willReturn($dateTime);
  100. $notification1 = $this->createNotificationMock('uid1', $dateTime);
  101. $notification2 = $this->createNotificationMock('uid2', $dateTime);
  102. $notification3 = $this->createNotificationMock('uid3', $dateTime);
  103. $this->manager->expects($this->at(0))
  104. ->method('createNotification')
  105. ->with()
  106. ->willReturn($notification1);
  107. $this->manager->expects($this->at(2))
  108. ->method('createNotification')
  109. ->with()
  110. ->willReturn($notification2);
  111. $this->manager->expects($this->at(4))
  112. ->method('createNotification')
  113. ->with()
  114. ->willReturn($notification3);
  115. $this->manager->expects($this->at(1))
  116. ->method('notify')
  117. ->with($notification1);
  118. $this->manager->expects($this->at(3))
  119. ->method('notify')
  120. ->with($notification2);
  121. $this->manager->expects($this->at(5))
  122. ->method('notify')
  123. ->with($notification3);
  124. $this->provider->send($this->vcalendar->VEVENT, $this->calendarDisplayName, [], $users);
  125. }
  126. /**
  127. * @param string $uid
  128. * @param \DateTime $dt
  129. */
  130. private function createNotificationMock(string $uid, \DateTime $dt):INotification {
  131. $notification = $this->createMock(INotification::class);
  132. $notification
  133. ->expects($this->once())
  134. ->method('setApp')
  135. ->with('dav')
  136. ->willReturn($notification);
  137. $notification->expects($this->once())
  138. ->method('setUser')
  139. ->with($uid)
  140. ->willReturn($notification);
  141. $notification->expects($this->once())
  142. ->method('setDateTime')
  143. ->with($dt)
  144. ->willReturn($notification);
  145. $notification->expects($this->once())
  146. ->method('setObject')
  147. ->with('dav', hash('sha256', 'uid1234', false))
  148. ->willReturn($notification);
  149. $notification->expects($this->once())
  150. ->method('setSubject')
  151. ->with('calendar_reminder', [
  152. 'title' => 'Fellowship meeting',
  153. 'start_atom' => '2017-01-01T00:00:00+00:00',
  154. ])
  155. ->willReturn($notification);
  156. $notification
  157. ->expects($this->once())
  158. ->method('setMessage')
  159. ->with('calendar_reminder', [
  160. 'title' => 'Fellowship meeting',
  161. 'start_atom' => '2017-01-01T00:00:00+00:00',
  162. 'description' => null,
  163. 'location' => null,
  164. 'all_day' => false,
  165. 'start_is_floating' => false,
  166. 'start_timezone' => 'UTC',
  167. 'end_atom' => '2017-01-01T00:00:00+00:00',
  168. 'end_is_floating' => false,
  169. 'end_timezone' => 'UTC',
  170. 'calendar_displayname' => 'Personal',
  171. ])
  172. ->willReturn($notification);
  173. return $notification;
  174. }
  175. }