PushProviderTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\unit\CalDAV\Reminder\NotificationProvider;
  8. use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\IUser;
  11. use OCP\Notification\IManager;
  12. use OCP\Notification\INotification;
  13. class PushProviderTest extends AbstractNotificationProviderTest {
  14. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  15. private $manager;
  16. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  17. private $timeFactory;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->manager = $this->createMock(IManager::class);
  21. $this->timeFactory = $this->createMock(ITimeFactory::class);
  22. $this->provider = new PushProvider(
  23. $this->config,
  24. $this->manager,
  25. $this->logger,
  26. $this->l10nFactory,
  27. $this->urlGenerator,
  28. $this->timeFactory
  29. );
  30. }
  31. public function testNotificationType():void {
  32. $this->assertEquals(PushProvider::NOTIFICATION_TYPE, 'DISPLAY');
  33. }
  34. public function testNotSend(): void {
  35. $this->config->expects($this->once())
  36. ->method('getAppValue')
  37. ->with('dav', 'sendEventRemindersPush', 'yes')
  38. ->willReturn('no');
  39. $this->manager->expects($this->never())
  40. ->method('createNotification');
  41. $this->manager->expects($this->never())
  42. ->method('notify');
  43. $user1 = $this->createMock(IUser::class);
  44. $user1->method('getUID')
  45. ->willReturn('uid1');
  46. $user2 = $this->createMock(IUser::class);
  47. $user2->method('getUID')
  48. ->willReturn('uid2');
  49. $user3 = $this->createMock(IUser::class);
  50. $user3->method('getUID')
  51. ->willReturn('uid3');
  52. $users = [$user1, $user2, $user3];
  53. $this->provider->send($this->vcalendar->VEVENT, $this->calendarDisplayName, [], $users);
  54. }
  55. public function testSend(): void {
  56. $this->config->expects($this->once())
  57. ->method('getAppValue')
  58. ->with('dav', 'sendEventRemindersPush', 'yes')
  59. ->willReturn('yes');
  60. $user1 = $this->createMock(IUser::class);
  61. $user1->method('getUID')
  62. ->willReturn('uid1');
  63. $user2 = $this->createMock(IUser::class);
  64. $user2->method('getUID')
  65. ->willReturn('uid2');
  66. $user3 = $this->createMock(IUser::class);
  67. $user3->method('getUID')
  68. ->willReturn('uid3');
  69. $users = [$user1, $user2, $user3];
  70. $dateTime = new \DateTime('@946684800');
  71. $this->timeFactory->method('getDateTime')
  72. ->with()
  73. ->willReturn($dateTime);
  74. $notification1 = $this->createNotificationMock('uid1', $dateTime);
  75. $notification2 = $this->createNotificationMock('uid2', $dateTime);
  76. $notification3 = $this->createNotificationMock('uid3', $dateTime);
  77. $this->manager->expects($this->exactly(3))
  78. ->method('createNotification')
  79. ->with()
  80. ->willReturnOnConsecutiveCalls(
  81. $notification1,
  82. $notification2,
  83. $notification3
  84. );
  85. $this->manager->expects($this->exactly(3))
  86. ->method('notify')
  87. ->withConsecutive(
  88. [$notification1],
  89. [$notification2],
  90. [$notification3],
  91. );
  92. $this->provider->send($this->vcalendar->VEVENT, $this->calendarDisplayName, [], $users);
  93. }
  94. /**
  95. * @param string $uid
  96. * @param \DateTime $dt
  97. */
  98. private function createNotificationMock(string $uid, \DateTime $dt):INotification {
  99. $notification = $this->createMock(INotification::class);
  100. $notification
  101. ->expects($this->once())
  102. ->method('setApp')
  103. ->with('dav')
  104. ->willReturn($notification);
  105. $notification->expects($this->once())
  106. ->method('setUser')
  107. ->with($uid)
  108. ->willReturn($notification);
  109. $notification->expects($this->once())
  110. ->method('setDateTime')
  111. ->with($dt)
  112. ->willReturn($notification);
  113. $notification->expects($this->once())
  114. ->method('setObject')
  115. ->with('dav', hash('sha256', 'uid1234', false))
  116. ->willReturn($notification);
  117. $notification->expects($this->once())
  118. ->method('setSubject')
  119. ->with('calendar_reminder', [
  120. 'title' => 'Fellowship meeting',
  121. 'start_atom' => '2017-01-01T00:00:00+00:00',
  122. ])
  123. ->willReturn($notification);
  124. $notification
  125. ->expects($this->once())
  126. ->method('setMessage')
  127. ->with('calendar_reminder', [
  128. 'title' => 'Fellowship meeting',
  129. 'start_atom' => '2017-01-01T00:00:00+00:00',
  130. 'description' => null,
  131. 'location' => null,
  132. 'all_day' => false,
  133. 'start_is_floating' => false,
  134. 'start_timezone' => 'UTC',
  135. 'end_atom' => '2017-01-01T00:00:00+00:00',
  136. 'end_is_floating' => false,
  137. 'end_timezone' => 'UTC',
  138. 'calendar_displayname' => 'Personal',
  139. ])
  140. ->willReturn($notification);
  141. return $notification;
  142. }
  143. }