PushProviderTest.php 6.4 KB

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