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