1
0

CalendarImplTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV;
  7. use OCA\DAV\CalDAV\Auth\CustomPrincipalPlugin;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCA\DAV\CalDAV\Calendar;
  10. use OCA\DAV\CalDAV\CalendarImpl;
  11. use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
  12. use OCA\DAV\CalDAV\Schedule\Plugin;
  13. use OCA\DAV\Connector\Sabre\Server;
  14. use OCP\Calendar\Exceptions\CalendarException;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use Sabre\VObject\Component\VCalendar;
  17. use Sabre\VObject\Component\VEvent;
  18. use Sabre\VObject\ITip\Message;
  19. use Sabre\VObject\Reader;
  20. class CalendarImplTest extends \Test\TestCase {
  21. /** @var CalendarImpl */
  22. private $calendarImpl;
  23. /** @var Calendar | \PHPUnit\Framework\MockObject\MockObject */
  24. private $calendar;
  25. /** @var array */
  26. private $calendarInfo;
  27. /** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject */
  28. private $backend;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->calendar = $this->createMock(Calendar::class);
  32. $this->calendarInfo = [
  33. 'id' => 'fancy_id_123',
  34. '{DAV:}displayname' => 'user readable name 123',
  35. '{http://apple.com/ns/ical/}calendar-color' => '#AABBCC',
  36. 'uri' => '/this/is/a/uri'
  37. ];
  38. $this->backend = $this->createMock(CalDavBackend::class);
  39. $this->calendarImpl = new CalendarImpl($this->calendar,
  40. $this->calendarInfo, $this->backend);
  41. }
  42. public function testGetKey(): void {
  43. $this->assertEquals($this->calendarImpl->getKey(), 'fancy_id_123');
  44. }
  45. public function testGetDisplayname(): void {
  46. $this->assertEquals($this->calendarImpl->getDisplayName(), 'user readable name 123');
  47. }
  48. public function testGetDisplayColor(): void {
  49. $this->assertEquals($this->calendarImpl->getDisplayColor(), '#AABBCC');
  50. }
  51. public function testSearch(): void {
  52. $this->backend->expects($this->once())
  53. ->method('search')
  54. ->with($this->calendarInfo, 'abc', ['def'], ['ghi'], 42, 1337)
  55. ->willReturn(['SEARCHRESULTS']);
  56. $result = $this->calendarImpl->search('abc', ['def'], ['ghi'], 42, 1337);
  57. $this->assertEquals($result, ['SEARCHRESULTS']);
  58. }
  59. public function testGetPermissionRead(): void {
  60. $this->calendar->expects($this->once())
  61. ->method('getACL')
  62. ->with()
  63. ->willReturn([
  64. ['privilege' => '{DAV:}read']
  65. ]);
  66. $this->assertEquals(1, $this->calendarImpl->getPermissions());
  67. }
  68. public function testGetPermissionWrite(): void {
  69. $this->calendar->expects($this->once())
  70. ->method('getACL')
  71. ->with()
  72. ->willReturn([
  73. ['privilege' => '{DAV:}write']
  74. ]);
  75. $this->assertEquals(6, $this->calendarImpl->getPermissions());
  76. }
  77. public function testGetPermissionReadWrite(): void {
  78. $this->calendar->expects($this->once())
  79. ->method('getACL')
  80. ->with()
  81. ->willReturn([
  82. ['privilege' => '{DAV:}read'],
  83. ['privilege' => '{DAV:}write']
  84. ]);
  85. $this->assertEquals(7, $this->calendarImpl->getPermissions());
  86. }
  87. public function testGetPermissionAll(): void {
  88. $this->calendar->expects($this->once())
  89. ->method('getACL')
  90. ->with()
  91. ->willReturn([
  92. ['privilege' => '{DAV:}all']
  93. ]);
  94. $this->assertEquals(31, $this->calendarImpl->getPermissions());
  95. }
  96. public function testHandleImipMessage(): void {
  97. $message = <<<EOF
  98. BEGIN:VCALENDAR
  99. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  100. METHOD:REPLY
  101. VERSION:2.0
  102. BEGIN:VEVENT
  103. ATTENDEE;PARTSTAT=ACCEPTED:mailto:lewis@stardew-tent-living.com
  104. ORGANIZER:mailto:pierre@generalstore.com
  105. UID:aUniqueUid
  106. SEQUENCE:2
  107. REQUEST-STATUS:2.0;Success
  108. END:VEVENT
  109. END:VCALENDAR
  110. EOF;
  111. /** @var CustomPrincipalPlugin|MockObject $authPlugin */
  112. $authPlugin = $this->createMock(CustomPrincipalPlugin::class);
  113. $authPlugin->expects(self::once())
  114. ->method('setCurrentPrincipal')
  115. ->with($this->calendar->getPrincipalURI());
  116. /** @var \Sabre\DAVACL\Plugin|MockObject $aclPlugin*/
  117. $aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class);
  118. /** @var Plugin|MockObject $schedulingPlugin */
  119. $schedulingPlugin = $this->createMock(Plugin::class);
  120. $iTipMessage = $this->getITipMessage($message);
  121. $iTipMessage->recipient = "mailto:lewis@stardew-tent-living.com";
  122. $server = $this->createMock(Server::class);
  123. $server->expects($this->any())
  124. ->method('getPlugin')
  125. ->willReturnMap([
  126. ['auth', $authPlugin],
  127. ['acl', $aclPlugin],
  128. ['caldav-schedule', $schedulingPlugin]
  129. ]);
  130. $server->expects(self::once())
  131. ->method('emit');
  132. $invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer', 'isExternalAttendee']);
  133. $invitationResponseServer->server = $server;
  134. $invitationResponseServer->expects($this->any())
  135. ->method('getServer')
  136. ->willReturn($server);
  137. $invitationResponseServer->expects(self::once())
  138. ->method('isExternalAttendee')
  139. ->willReturn(false);
  140. $calendarImpl = $this->getMockBuilder(CalendarImpl::class)
  141. ->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend])
  142. ->onlyMethods(['getInvitationResponseServer'])
  143. ->getMock();
  144. $calendarImpl->expects($this->once())
  145. ->method('getInvitationResponseServer')
  146. ->willReturn($invitationResponseServer);
  147. $calendarImpl->handleIMipMessage('filename.ics', $message);
  148. }
  149. public function testHandleImipMessageNoCalendarUri(): void {
  150. /** @var CustomPrincipalPlugin|MockObject $authPlugin */
  151. $authPlugin = $this->createMock(CustomPrincipalPlugin::class);
  152. $authPlugin->expects(self::once())
  153. ->method('setCurrentPrincipal')
  154. ->with($this->calendar->getPrincipalURI());
  155. unset($this->calendarInfo['uri']);
  156. /** @var Plugin|MockObject $schedulingPlugin */
  157. $schedulingPlugin = $this->createMock(Plugin::class);
  158. /** @var \Sabre\DAVACL\Plugin|MockObject $schedulingPlugin */
  159. $aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class);
  160. $server =
  161. $this->createMock(Server::class);
  162. $server->expects($this->any())
  163. ->method('getPlugin')
  164. ->willReturnMap([
  165. ['auth', $authPlugin],
  166. ['acl', $aclPlugin],
  167. ['caldav-schedule', $schedulingPlugin]
  168. ]);
  169. $server->expects(self::never())
  170. ->method('emit');
  171. $invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer']);
  172. $invitationResponseServer->server = $server;
  173. $invitationResponseServer->expects($this->any())
  174. ->method('getServer')
  175. ->willReturn($server);
  176. $calendarImpl = $this->getMockBuilder(CalendarImpl::class)
  177. ->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend])
  178. ->onlyMethods(['getInvitationResponseServer'])
  179. ->getMock();
  180. $calendarImpl->expects($this->once())
  181. ->method('getInvitationResponseServer')
  182. ->willReturn($invitationResponseServer);
  183. $message = <<<EOF
  184. BEGIN:VCALENDAR
  185. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  186. METHOD:REPLY
  187. VERSION:2.0
  188. BEGIN:VEVENT
  189. ATTENDEE;PARTSTAT=ACCEPTED:mailto:lewis@stardew-tent-living.com
  190. ORGANIZER:mailto:pierre@generalstore.com
  191. UID:aUniqueUid
  192. SEQUENCE:2
  193. REQUEST-STATUS:2.0;Success
  194. END:VEVENT
  195. END:VCALENDAR
  196. EOF;
  197. $this->expectException(CalendarException::class);
  198. $calendarImpl->handleIMipMessage('filename.ics', $message);
  199. }
  200. private function getITipMessage($calendarData): Message {
  201. $iTipMessage = new Message();
  202. /** @var VCalendar $vObject */
  203. $vObject = Reader::read($calendarData);
  204. /** @var VEvent $vEvent */
  205. $vEvent = $vObject->{'VEVENT'};
  206. $orgaizer = $vEvent->{'ORGANIZER'}->getValue();
  207. $attendee = $vEvent->{'ATTENDEE'}->getValue();
  208. $iTipMessage->method = $vObject->{'METHOD'}->getValue();
  209. $iTipMessage->recipient = $orgaizer;
  210. $iTipMessage->sender = $attendee;
  211. $iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : '';
  212. $iTipMessage->component = 'VEVENT';
  213. $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0;
  214. $iTipMessage->message = $vObject;
  215. return $iTipMessage;
  216. }
  217. }