CalendarImplTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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() {
  66. $this->assertEquals($this->calendarImpl->getKey(), 'fancy_id_123');
  67. }
  68. public function testGetDisplayname() {
  69. $this->assertEquals($this->calendarImpl->getDisplayName(), 'user readable name 123');
  70. }
  71. public function testGetDisplayColor() {
  72. $this->assertEquals($this->calendarImpl->getDisplayColor(), '#AABBCC');
  73. }
  74. public function testSearch() {
  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() {
  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() {
  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() {
  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() {
  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. $schedulingPlugin->expects(self::once())
  146. ->method('scheduleLocalDelivery')
  147. ->with($iTipMessage);
  148. $server = $this->createMock(Server::class);
  149. $server->expects($this->any())
  150. ->method('getPlugin')
  151. ->willReturnMap([
  152. ['auth', $authPlugin],
  153. ['acl', $aclPlugin],
  154. ['caldav-schedule', $schedulingPlugin]
  155. ]);
  156. $invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer', 'isExternalAttendee']);
  157. $invitationResponseServer->server = $server;
  158. $invitationResponseServer->expects($this->any())
  159. ->method('getServer')
  160. ->willReturn($server);
  161. $invitationResponseServer->expects(self::once())
  162. ->method('isExternalAttendee')
  163. ->willReturn(false);
  164. $calendarImpl = $this->getMockBuilder(CalendarImpl::class)
  165. ->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend])
  166. ->onlyMethods(['getInvitationResponseServer'])
  167. ->getMock();
  168. $calendarImpl->expects($this->once())
  169. ->method('getInvitationResponseServer')
  170. ->willReturn($invitationResponseServer);
  171. $calendarImpl->handleIMipMessage('filename.ics', $message);
  172. }
  173. public function testHandleImipMessageNoCalendarUri(): void {
  174. /** @var CustomPrincipalPlugin|MockObject $authPlugin */
  175. $authPlugin = $this->createMock(CustomPrincipalPlugin::class);
  176. $authPlugin->expects(self::once())
  177. ->method('setCurrentPrincipal')
  178. ->with($this->calendar->getPrincipalURI());
  179. unset($this->calendarInfo['uri']);
  180. /** @var Plugin|MockObject $schedulingPlugin */
  181. $schedulingPlugin = $this->createMock(Plugin::class);
  182. /** @var \Sabre\DAVACL\Plugin|MockObject $schedulingPlugin */
  183. $aclPlugin = $this->createMock(\Sabre\DAVACL\Plugin::class);
  184. $server =
  185. $this->createMock(Server::class);
  186. $server->expects($this->any())
  187. ->method('getPlugin')
  188. ->willReturnMap([
  189. ['auth', $authPlugin],
  190. ['acl', $aclPlugin],
  191. ['caldav-schedule', $schedulingPlugin]
  192. ]);
  193. $invitationResponseServer = $this->createPartialMock(InvitationResponseServer::class, ['getServer']);
  194. $invitationResponseServer->server = $server;
  195. $invitationResponseServer->expects($this->any())
  196. ->method('getServer')
  197. ->willReturn($server);
  198. $calendarImpl = $this->getMockBuilder(CalendarImpl::class)
  199. ->setConstructorArgs([$this->calendar, $this->calendarInfo, $this->backend])
  200. ->onlyMethods(['getInvitationResponseServer'])
  201. ->getMock();
  202. $calendarImpl->expects($this->once())
  203. ->method('getInvitationResponseServer')
  204. ->willReturn($invitationResponseServer);
  205. $message = <<<EOF
  206. BEGIN:VCALENDAR
  207. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  208. METHOD:REPLY
  209. VERSION:2.0
  210. BEGIN:VEVENT
  211. ATTENDEE;PARTSTAT=ACCEPTED:mailto:lewis@stardew-tent-living.com
  212. ORGANIZER:mailto:pierre@generalstore.com
  213. UID:aUniqueUid
  214. SEQUENCE:2
  215. REQUEST-STATUS:2.0;Success
  216. END:VEVENT
  217. END:VCALENDAR
  218. EOF;
  219. $this->expectException(CalendarException::class);
  220. $calendarImpl->handleIMipMessage('filename.ics', $message);
  221. }
  222. private function getITipMessage($calendarData): Message {
  223. $iTipMessage = new Message();
  224. /** @var VCalendar $vObject */
  225. $vObject = Reader::read($calendarData);
  226. /** @var VEvent $vEvent */
  227. $vEvent = $vObject->{'VEVENT'};
  228. $orgaizer = $vEvent->{'ORGANIZER'}->getValue();
  229. $attendee = $vEvent->{'ATTENDEE'}->getValue();
  230. $iTipMessage->method = $vObject->{'METHOD'}->getValue();
  231. $iTipMessage->recipient = $orgaizer;
  232. $iTipMessage->sender = $attendee;
  233. $iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : '';
  234. $iTipMessage->component = 'VEVENT';
  235. $iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0;
  236. $iTipMessage->message = $vObject;
  237. return $iTipMessage;
  238. }
  239. }