PushProviderTest.php 5.3 KB

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