CalendarImplTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * @copyright 2017, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\CalDAV;
  27. use OCA\DAV\CalDAV\Auth\CustomPrincipalPlugin;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CalDAV\Calendar;
  30. use OCA\DAV\CalDAV\CalendarImpl;
  31. use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
  32. use OCA\DAV\CalDAV\Schedule\Plugin;
  33. use OCA\DAV\Connector\Sabre\Server;
  34. use OCP\Calendar\Exceptions\CalendarException;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Sabre\VObject\Component\VCalendar;
  37. use Sabre\VObject\Component\VEvent;
  38. use Sabre\VObject\ITip\Message;
  39. use Sabre\VObject\Reader;
  40. /**
  41. * @group DB
  42. */
  43. class CalendarImplTest extends \Test\TestCase {
  44. /** @var CalendarImpl */
  45. private $calendarImpl;
  46. /** @var Calendar | \PHPUnit\Framework\MockObject\MockObject */
  47. private $calendar;
  48. /** @var array */
  49. private $calendarInfo;
  50. /** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject */
  51. private $backend;
  52. protected function setUp(): void {
  53. parent::setUp();
  54. $this->calendar = $this->createMock(Calendar::class);
  55. $this->calendarInfo = [
  56. 'id' => 'fancy_id_123',
  57. '{DAV:}displayname' => 'user readable name 123',
  58. '{http://apple.com/ns/ical/}calendar-color' => '#AABBCC',
  59. 'uri' => '/this/is/a/uri'
  60. ];
  61. $this->backend = $this->createMock(CalDavBackend::class);
  62. $this->calendarImpl = new CalendarImpl($this->calendar,
  63. $this->calendarInfo, $this->backend);
  64. }
  65. public function testGetKey(): void {
  66. $this->assertEquals($this->calendarImpl->getKey(), 'fancy_id_123');
  67. }
  68. public function testGetDisplayname(): void {
  69. $this->assertEquals($this->calendarImpl->getDisplayName(), 'user readable name 123');
  70. }
  71. public function testGetDisplayColor(): void {
  72. $this->assertEquals($this->calendarImpl->getDisplayColor(), '#AABBCC');
  73. }
  74. public function testSearch(): void {
  75. $this->backend->expects($this->once())
  76. ->method('search')
  77. ->with($this->calendarInfo, 'abc', ['def'], ['ghi'], 42, 1337)
  78. ->willReturn(['SEARCHRESULTS']);
  79. $result = $this->calendarImpl->search('abc', ['def'], ['ghi'], 42, 1337);
  80. $this->assertEquals($result, ['SEARCHRESULTS']);
  81. }
  82. public function testGetPermissionRead(): void {
  83. $this->calendar->expects($this->once())
  84. ->method('getACL')
  85. ->with()
  86. ->willReturn([
  87. ['privilege' => '{DAV:}read']
  88. ]);
  89. $this->assertEquals(1, $this->calendarImpl->getPermissions());
  90. }
  91. public function testGetPermissionWrite(): void {
  92. $this->calendar->expects($this->once())
  93. ->method('getACL')
  94. ->with()
  95. ->willReturn([
  96. ['privilege' => '{DAV:}write']
  97. ]);
  98. $this->assertEquals(6, $this->calendarImpl->getPermissions());
  99. }
  100. public function testGetPermissionReadWrite(): void {
  101. $this->calendar->expects($this->once())
  102. ->method('getACL')
  103. ->with()
  104. ->willReturn([
  105. ['privilege' => '{DAV:}read'],
  106. ['privilege' => '{DAV:}write']
  107. ]);
  108. $this->assertEquals(7, $this->calendarImpl->getPermissions());
  109. }
  110. public function testGetPermissionAll(): void {
  111. $this->calendar->expects($this->once())
  112. ->method('getACL')
  113. ->with()
  114. ->willReturn([
  115. ['privilege' => '{DAV:}all']
  116. ]);
  117. $this->assertEquals(31, $this->calendarImpl->getPermissions());
  118. }
  119. public function testHandleImipMessage(): void {
  120. $message = <<<EOF
  121. BEGIN:VCALENDAR
  122. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  123. METHOD:REPLY
  124. VERSION:2.0
  125. BEGIN:VEVENT
  126. ATTENDEE;PARTSTAT=ACCEPTED:mailto:lewis@stardew-tent-living.com
  127. ORGANIZER:mailto:pierre@generalstore.com
  128. UID:aUniqueUid
  129. SEQUENCE:2
  130. REQUEST-STATUS:2.0;Success
  131. END:VEVENT
  132. END:VCALENDAR
  133. EOF;
  134. /** @var CustomPrincipalPlugin|MockObject $authPlugin */
  135. $authPlugin = $this->createMock(CustomPrincipalPlugin::class);
  136. $authPlugin->expects(self::once())
  137. ->method('setCurrentPrincipal')
  138. ->with($this->calendar->getPrincipalURI());
  139. /** @var \Sabre\DAVACL\Plugin|MockObject $aclPlugin*/
  140. $aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class);
  141. /** @var Plugin|MockObject $schedulingPlugin */
  142. $schedulingPlugin = $this->createMock(Plugin::class);
  143. $iTipMessage = $this->getITipMessage($message);
  144. $iTipMessage->recipient = "mailto:lewis@stardew-tent-living.com";
  145. $server = $this->createMock(Server::class);
  146. $server->expects($this->any())
  147. ->method('getPlugin')
  148. ->willReturnMap([
  149. ['auth', $authPlugin],
  150. ['acl', $aclPlugin],
  151. ['caldav-schedule', $schedulingPlugin]
  152. ]);
  153. $server->expects(self::once())
  154. ->method('emit');
  155. $invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer', 'isExternalAttendee']);
  156. $invitationResponseServer->server = $server;
  157. $invitationResponseServer->expects($this->any())
  158. ->method('getServer')
  159. ->willReturn($server);
  160. $invitationResponseServer->expects(self::once())
  161. ->method('isExternalAttendee')
  162. ->willReturn(false);
  163. $calendarImpl = $this->getMockBuilder(CalendarImpl::class)
  164. ->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend])
  165. ->onlyMethods(['getInvitationResponseServer'])
  166. ->getMock();
  167. $calendarImpl->expects($this->once())
  168. ->method('getInvitationResponseServer')
  169. ->willReturn($invitationResponseServer);
  170. $calendarImpl->handleIMipMessage('filename.ics', $message);
  171. }
  172. public function testHandleImipMessageNoCalendarUri(): void {
  173. /** @var CustomPrincipalPlugin|MockObject $authPlugin */
  174. $authPlugin = $this->createMock(CustomPrincipalPlugin::class);
  175. $authPlugin->expects(self::once())
  176. ->method('setCurrentPrincipal')
  177. ->with($this->calendar->getPrincipalURI());
  178. unset($this->calendarInfo['uri']);
  179. /** @var Plugin|MockObject $schedulingPlugin */
  180. $schedulingPlugin = $this->createMock(Plugin::class);
  181. /** @var \Sabre\DAVACL\Plugin|MockObject $schedulingPlugin */
  182. $aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class);
  183. $server =
  184. $this->createMock(Server::class);
  185. $server->expects($this->any())
  186. ->method('getPlugin')
  187. ->willReturnMap([
  188. ['auth', $authPlugin],
  189. ['acl', $aclPlugin],
  190. ['caldav-schedule', $schedulingPlugin]
  191. ]);
  192. $server->expects(self::never())
  193. ->method('emit');
  194. $invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer']);
  195. $invitationResponseServer->server = $server;
  196. $invitationResponseServer->expects($this->any())
  197. ->method('getServer')
  198. ->willReturn($server);
  199. $calendarImpl = $this->getMockBuilder(CalendarImpl::class)
  200. ->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend])
  201. ->onlyMethods(['getInvitationResponseServer'])
  202. ->getMock();
  203. $calendarImpl->expects($this->once())
  204. ->method('getInvitationResponseServer')
  205. ->willReturn($invitationResponseServer);
  206. $message = <<<EOF
  207. BEGIN:VCALENDAR
  208. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  209. METHOD:REPLY
  210. VERSION:2.0
  211. BEGIN:VEVENT
  212. ATTENDEE;PARTSTAT=ACCEPTED:mailto:lewis@stardew-tent-living.com
  213. ORGANIZER:mailto:pierre@generalstore.com
  214. UID:aUniqueUid
  215. SEQUENCE:2
  216. REQUEST-STATUS:2.0;Success
  217. END:VEVENT
  218. END:VCALENDAR
  219. EOF;
  220. $this->expectException(CalendarException::class);
  221. $calendarImpl->handleIMipMessage('filename.ics', $message);
  222. }
  223. private function getITipMessage($calendarData): Message {
  224. $iTipMessage = new Message();
  225. /** @var VCalendar $vObject */
  226. $vObject = Reader::read($calendarData);
  227. /** @var VEvent $vEvent */
  228. $vEvent = $vObject->{'VEVENT'};
  229. $orgaizer = $vEvent->{'ORGANIZER'}->getValue();
  230. $attendee = $vEvent->{'ATTENDEE'}->getValue();
  231. $iTipMessage->method = $vObject->{'METHOD'}->getValue();
  232. $iTipMessage->recipient = $orgaizer;
  233. $iTipMessage->sender = $attendee;
  234. $iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : '';
  235. $iTipMessage->component = 'VEVENT';
  236. $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0;
  237. $iTipMessage->message = $vObject;
  238. return $iTipMessage;
  239. }
  240. }