AbstractCalDavBackend.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\CalDAV;
  8. use OC\KnownUser\KnownUserService;
  9. use OCA\DAV\CalDAV\CalDavBackend;
  10. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  11. use OCA\DAV\CalDAV\Sharing\Backend as SharingBackend;
  12. use OCA\DAV\CalDAV\Sharing\Service;
  13. use OCA\DAV\Connector\Sabre\Principal;
  14. use OCA\DAV\DAV\Sharing\SharingMapper;
  15. use OCP\Accounts\IAccountManager;
  16. use OCP\App\IAppManager;
  17. use OCP\EventDispatcher\IEventDispatcher;
  18. use OCP\ICacheFactory;
  19. use OCP\IConfig;
  20. use OCP\IDBConnection;
  21. use OCP\IGroupManager;
  22. use OCP\IUserManager;
  23. use OCP\IUserSession;
  24. use OCP\L10N\IFactory;
  25. use OCP\Security\ISecureRandom;
  26. use OCP\Share\IManager as ShareManager;
  27. use PHPUnit\Framework\MockObject\MockObject;
  28. use Psr\Log\LoggerInterface;
  29. use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
  30. use Sabre\DAV\Xml\Property\Href;
  31. use Test\TestCase;
  32. /**
  33. * Class CalDavBackendTest
  34. *
  35. * @group DB
  36. *
  37. * @package OCA\DAV\Tests\unit\CalDAV
  38. */
  39. abstract class AbstractCalDavBackend extends TestCase {
  40. protected CalDavBackend $backend;
  41. protected Principal|MockObject $principal;
  42. protected IUserManager|MockObject $userManager;
  43. protected IGroupManager|MockObject $groupManager;
  44. protected IEventDispatcher|MockObject $dispatcher;
  45. private LoggerInterface|MockObject $logger;
  46. private IConfig|MockObject $config;
  47. private ISecureRandom $random;
  48. protected SharingBackend $sharingBackend;
  49. protected IDBConnection $db;
  50. public const UNIT_TEST_USER = 'principals/users/caldav-unit-test';
  51. public const UNIT_TEST_USER1 = 'principals/users/caldav-unit-test1';
  52. public const UNIT_TEST_GROUP = 'principals/groups/caldav-unit-test-group';
  53. public const UNIT_TEST_GROUP2 = 'principals/groups/caldav-unit-test-group2';
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->userManager = $this->createMock(IUserManager::class);
  57. $this->groupManager = $this->createMock(IGroupManager::class);
  58. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  59. $this->principal = $this->getMockBuilder(Principal::class)
  60. ->setConstructorArgs([
  61. $this->userManager,
  62. $this->groupManager,
  63. $this->createMock(IAccountManager::class),
  64. $this->createMock(ShareManager::class),
  65. $this->createMock(IUserSession::class),
  66. $this->createMock(IAppManager::class),
  67. $this->createMock(ProxyMapper::class),
  68. $this->createMock(KnownUserService::class),
  69. $this->createMock(IConfig::class),
  70. $this->createMock(IFactory::class)
  71. ])
  72. ->setMethods(['getPrincipalByPath', 'getGroupMembership', 'findByUri'])
  73. ->getMock();
  74. $this->principal->expects($this->any())->method('getPrincipalByPath')
  75. ->willReturn([
  76. 'uri' => 'principals/best-friend',
  77. '{DAV:}displayname' => 'User\'s displayname',
  78. ]);
  79. $this->principal->expects($this->any())->method('getGroupMembership')
  80. ->withAnyParameters()
  81. ->willReturn([self::UNIT_TEST_GROUP, self::UNIT_TEST_GROUP2]);
  82. $this->db = \OC::$server->getDatabaseConnection();
  83. $this->random = \OC::$server->getSecureRandom();
  84. $this->logger = $this->createMock(LoggerInterface::class);
  85. $this->config = $this->createMock(IConfig::class);
  86. $this->sharingBackend = new SharingBackend(
  87. $this->userManager,
  88. $this->groupManager,
  89. $this->principal,
  90. $this->createMock(ICacheFactory::class),
  91. new Service(new SharingMapper($this->db)),
  92. $this->logger);
  93. $this->backend = new CalDavBackend(
  94. $this->db,
  95. $this->principal,
  96. $this->userManager,
  97. $this->random,
  98. $this->logger,
  99. $this->dispatcher,
  100. $this->config,
  101. $this->sharingBackend,
  102. false,
  103. );
  104. $this->cleanUpBackend();
  105. }
  106. protected function tearDown(): void {
  107. $this->cleanUpBackend();
  108. parent::tearDown();
  109. }
  110. public function cleanUpBackend(): void {
  111. if (is_null($this->backend)) {
  112. return;
  113. }
  114. $this->principal->expects($this->any())->method('getGroupMembership')
  115. ->withAnyParameters()
  116. ->willReturn([self::UNIT_TEST_GROUP, self::UNIT_TEST_GROUP2]);
  117. $this->cleanupForPrincipal(self::UNIT_TEST_USER);
  118. $this->cleanupForPrincipal(self::UNIT_TEST_USER1);
  119. }
  120. private function cleanupForPrincipal($principal): void {
  121. $calendars = $this->backend->getCalendarsForUser($principal);
  122. $this->dispatcher->expects(self::any())
  123. ->method('dispatchTyped');
  124. foreach ($calendars as $calendar) {
  125. $this->backend->deleteCalendar($calendar['id'], true);
  126. }
  127. $subscriptions = $this->backend->getSubscriptionsForUser($principal);
  128. foreach ($subscriptions as $subscription) {
  129. $this->backend->deleteSubscription($subscription['id']);
  130. }
  131. }
  132. protected function createTestCalendar() {
  133. $this->dispatcher->expects(self::any())
  134. ->method('dispatchTyped');
  135. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', [
  136. '{http://apple.com/ns/ical/}calendar-color' => '#1C4587FF'
  137. ]);
  138. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  139. $this->assertEquals(1, count($calendars));
  140. $this->assertEquals(self::UNIT_TEST_USER, $calendars[0]['principaluri']);
  141. /** @var SupportedCalendarComponentSet $components */
  142. $components = $calendars[0]['{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'];
  143. $this->assertEquals(['VEVENT','VTODO','VJOURNAL'], $components->getValue());
  144. $color = $calendars[0]['{http://apple.com/ns/ical/}calendar-color'];
  145. $this->assertEquals('#1C4587FF', $color);
  146. $this->assertEquals('Example', $calendars[0]['uri']);
  147. $this->assertEquals('Example', $calendars[0]['{DAV:}displayname']);
  148. $calendarId = $calendars[0]['id'];
  149. return $calendarId;
  150. }
  151. protected function createTestSubscription() {
  152. $this->backend->createSubscription(self::UNIT_TEST_USER, 'Example', [
  153. '{http://apple.com/ns/ical/}calendar-color' => '#1C4587FF',
  154. '{http://calendarserver.org/ns/}source' => new Href(['foo']),
  155. ]);
  156. $calendars = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  157. $this->assertEquals(1, count($calendars));
  158. $this->assertEquals(self::UNIT_TEST_USER, $calendars[0]['principaluri']);
  159. $this->assertEquals('Example', $calendars[0]['uri']);
  160. $calendarId = $calendars[0]['id'];
  161. return $calendarId;
  162. }
  163. protected function createEvent($calendarId, $start = '20130912T130000Z', $end = '20130912T140000Z') {
  164. $randomPart = self::getUniqueID();
  165. $calData = <<<EOD
  166. BEGIN:VCALENDAR
  167. VERSION:2.0
  168. PRODID:ownCloud Calendar
  169. BEGIN:VEVENT
  170. CREATED;VALUE=DATE-TIME:20130910T125139Z
  171. UID:47d15e3ec8-$randomPart
  172. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  173. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  174. SUMMARY:Test Event
  175. DTSTART;VALUE=DATE-TIME:$start
  176. DTEND;VALUE=DATE-TIME:$end
  177. CLASS:PUBLIC
  178. END:VEVENT
  179. END:VCALENDAR
  180. EOD;
  181. $uri0 = $this->getUniqueID('event');
  182. $this->dispatcher->expects(self::atLeastOnce())
  183. ->method('dispatchTyped');
  184. $this->backend->createCalendarObject($calendarId, $uri0, $calData);
  185. return $uri0;
  186. }
  187. protected function modifyEvent($calendarId, $objectId, $start = '20130912T130000Z', $end = '20130912T140000Z') {
  188. $randomPart = self::getUniqueID();
  189. $calData = <<<EOD
  190. BEGIN:VCALENDAR
  191. VERSION:2.0
  192. PRODID:ownCloud Calendar
  193. BEGIN:VEVENT
  194. CREATED;VALUE=DATE-TIME:20130910T125139Z
  195. UID:47d15e3ec8-$randomPart
  196. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  197. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  198. SUMMARY:Test Event
  199. DTSTART;VALUE=DATE-TIME:$start
  200. DTEND;VALUE=DATE-TIME:$end
  201. CLASS:PUBLIC
  202. END:VEVENT
  203. END:VCALENDAR
  204. EOD;
  205. $this->backend->updateCalendarObject($calendarId, $objectId, $calData);
  206. }
  207. protected function deleteEvent($calendarId, $objectId) {
  208. $this->backend->deleteCalendarObject($calendarId, $objectId);
  209. }
  210. protected function assertAcl($principal, $privilege, $acl) {
  211. foreach ($acl as $a) {
  212. if ($a['principal'] === $principal && $a['privilege'] === $privilege) {
  213. $this->addToAssertionCount(1);
  214. return;
  215. }
  216. }
  217. $this->fail("ACL does not contain $principal / $privilege");
  218. }
  219. protected function assertNotAcl($principal, $privilege, $acl) {
  220. foreach ($acl as $a) {
  221. if ($a['principal'] === $principal && $a['privilege'] === $privilege) {
  222. $this->fail("ACL contains $principal / $privilege");
  223. return;
  224. }
  225. }
  226. $this->addToAssertionCount(1);
  227. }
  228. protected function assertAccess($shouldHaveAcl, $principal, $privilege, $acl) {
  229. if ($shouldHaveAcl) {
  230. $this->assertAcl($principal, $privilege, $acl);
  231. } else {
  232. $this->assertNotAcl($principal, $privilege, $acl);
  233. }
  234. }
  235. }