AbstractCalDavBackend.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Citharel <nextcloud@tcit.fr>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\CalDAV;
  29. use OC\KnownUser\KnownUserService;
  30. use OCA\DAV\CalDAV\CalDavBackend;
  31. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  32. use OCA\DAV\CalDAV\Sharing\Backend as SharingBackend;
  33. use OCA\DAV\CalDAV\Sharing\Service;
  34. use OCA\DAV\Connector\Sabre\Principal;
  35. use OCA\DAV\DAV\Sharing\SharingMapper;
  36. use OCP\Accounts\IAccountManager;
  37. use OCP\App\IAppManager;
  38. use OCP\EventDispatcher\IEventDispatcher;
  39. use OCP\ICacheFactory;
  40. use OCP\IConfig;
  41. use OCP\IGroupManager;
  42. use OCP\IUserManager;
  43. use OCP\IUserSession;
  44. use OCP\L10N\IFactory;
  45. use OCP\Security\ISecureRandom;
  46. use OCP\Share\IManager as ShareManager;
  47. use PHPUnit\Framework\MockObject\MockObject;
  48. use Psr\Log\LoggerInterface;
  49. use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
  50. use Sabre\DAV\Xml\Property\Href;
  51. use Test\TestCase;
  52. /**
  53. * Class CalDavBackendTest
  54. *
  55. * @group DB
  56. *
  57. * @package OCA\DAV\Tests\unit\CalDAV
  58. */
  59. abstract class AbstractCalDavBackend extends TestCase {
  60. protected CalDavBackend $backend;
  61. protected Principal|MockObject $principal;
  62. protected IUserManager|MockObject $userManager;
  63. protected IGroupManager|MockObject $groupManager;
  64. protected IEventDispatcher|MockObject $dispatcher;
  65. private LoggerInterface|MockObject $logger;
  66. private IConfig|MockObject $config;
  67. private ISecureRandom $random;
  68. protected SharingBackend $sharingBackend;
  69. public const UNIT_TEST_USER = 'principals/users/caldav-unit-test';
  70. public const UNIT_TEST_USER1 = 'principals/users/caldav-unit-test1';
  71. public const UNIT_TEST_GROUP = 'principals/groups/caldav-unit-test-group';
  72. public const UNIT_TEST_GROUP2 = 'principals/groups/caldav-unit-test-group2';
  73. protected function setUp(): void {
  74. parent::setUp();
  75. $this->userManager = $this->createMock(IUserManager::class);
  76. $this->groupManager = $this->createMock(IGroupManager::class);
  77. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  78. $this->principal = $this->getMockBuilder(Principal::class)
  79. ->setConstructorArgs([
  80. $this->userManager,
  81. $this->groupManager,
  82. $this->createMock(IAccountManager::class),
  83. $this->createMock(ShareManager::class),
  84. $this->createMock(IUserSession::class),
  85. $this->createMock(IAppManager::class),
  86. $this->createMock(ProxyMapper::class),
  87. $this->createMock(KnownUserService::class),
  88. $this->createMock(IConfig::class),
  89. $this->createMock(IFactory::class)
  90. ])
  91. ->setMethods(['getPrincipalByPath', 'getGroupMembership', 'findByUri'])
  92. ->getMock();
  93. $this->principal->expects($this->any())->method('getPrincipalByPath')
  94. ->willReturn([
  95. 'uri' => 'principals/best-friend',
  96. '{DAV:}displayname' => 'User\'s displayname',
  97. ]);
  98. $this->principal->expects($this->any())->method('getGroupMembership')
  99. ->withAnyParameters()
  100. ->willReturn([self::UNIT_TEST_GROUP, self::UNIT_TEST_GROUP2]);
  101. $db = \OC::$server->getDatabaseConnection();
  102. $this->random = \OC::$server->getSecureRandom();
  103. $this->logger = $this->createMock(LoggerInterface::class);
  104. $this->config = $this->createMock(IConfig::class);
  105. $this->sharingBackend = new SharingBackend(
  106. $this->userManager,
  107. $this->groupManager,
  108. $this->principal,
  109. $this->createMock(ICacheFactory::class),
  110. new Service(new SharingMapper($db)),
  111. $this->logger);
  112. $this->backend = new CalDavBackend(
  113. $db,
  114. $this->principal,
  115. $this->userManager,
  116. $this->random,
  117. $this->logger,
  118. $this->dispatcher,
  119. $this->config,
  120. $this->sharingBackend,
  121. false,
  122. );
  123. $this->cleanUpBackend();
  124. }
  125. protected function tearDown(): void {
  126. $this->cleanUpBackend();
  127. parent::tearDown();
  128. }
  129. public function cleanUpBackend(): void {
  130. if (is_null($this->backend)) {
  131. return;
  132. }
  133. $this->principal->expects($this->any())->method('getGroupMembership')
  134. ->withAnyParameters()
  135. ->willReturn([self::UNIT_TEST_GROUP, self::UNIT_TEST_GROUP2]);
  136. $this->cleanupForPrincipal(self::UNIT_TEST_USER);
  137. $this->cleanupForPrincipal(self::UNIT_TEST_USER1);
  138. }
  139. private function cleanupForPrincipal($principal): void {
  140. $calendars = $this->backend->getCalendarsForUser($principal);
  141. $this->dispatcher->expects(self::any())
  142. ->method('dispatchTyped');
  143. foreach ($calendars as $calendar) {
  144. $this->backend->deleteCalendar($calendar['id'], true);
  145. }
  146. $subscriptions = $this->backend->getSubscriptionsForUser($principal);
  147. foreach ($subscriptions as $subscription) {
  148. $this->backend->deleteSubscription($subscription['id']);
  149. }
  150. }
  151. protected function createTestCalendar() {
  152. $this->dispatcher->expects(self::any())
  153. ->method('dispatchTyped');
  154. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', [
  155. '{http://apple.com/ns/ical/}calendar-color' => '#1C4587FF'
  156. ]);
  157. $calendars = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  158. $this->assertEquals(1, count($calendars));
  159. $this->assertEquals(self::UNIT_TEST_USER, $calendars[0]['principaluri']);
  160. /** @var SupportedCalendarComponentSet $components */
  161. $components = $calendars[0]['{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'];
  162. $this->assertEquals(['VEVENT','VTODO'], $components->getValue());
  163. $color = $calendars[0]['{http://apple.com/ns/ical/}calendar-color'];
  164. $this->assertEquals('#1C4587FF', $color);
  165. $this->assertEquals('Example', $calendars[0]['uri']);
  166. $this->assertEquals('Example', $calendars[0]['{DAV:}displayname']);
  167. $calendarId = $calendars[0]['id'];
  168. return $calendarId;
  169. }
  170. protected function createTestSubscription() {
  171. $this->backend->createSubscription(self::UNIT_TEST_USER, 'Example', [
  172. '{http://apple.com/ns/ical/}calendar-color' => '#1C4587FF',
  173. '{http://calendarserver.org/ns/}source' => new Href(['foo']),
  174. ]);
  175. $calendars = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
  176. $this->assertEquals(1, count($calendars));
  177. $this->assertEquals(self::UNIT_TEST_USER, $calendars[0]['principaluri']);
  178. $this->assertEquals('Example', $calendars[0]['uri']);
  179. $calendarId = $calendars[0]['id'];
  180. return $calendarId;
  181. }
  182. protected function createEvent($calendarId, $start = '20130912T130000Z', $end = '20130912T140000Z') {
  183. $randomPart = self::getUniqueID();
  184. $calData = <<<EOD
  185. BEGIN:VCALENDAR
  186. VERSION:2.0
  187. PRODID:ownCloud Calendar
  188. BEGIN:VEVENT
  189. CREATED;VALUE=DATE-TIME:20130910T125139Z
  190. UID:47d15e3ec8-$randomPart
  191. LAST-MODIFIED;VALUE=DATE-TIME:20130910T125139Z
  192. DTSTAMP;VALUE=DATE-TIME:20130910T125139Z
  193. SUMMARY:Test Event
  194. DTSTART;VALUE=DATE-TIME:$start
  195. DTEND;VALUE=DATE-TIME:$end
  196. CLASS:PUBLIC
  197. END:VEVENT
  198. END:VCALENDAR
  199. EOD;
  200. $uri0 = $this->getUniqueID('event');
  201. $this->dispatcher->expects(self::atLeastOnce())
  202. ->method('dispatchTyped');
  203. $this->backend->createCalendarObject($calendarId, $uri0, $calData);
  204. return $uri0;
  205. }
  206. protected function assertAcl($principal, $privilege, $acl) {
  207. foreach ($acl as $a) {
  208. if ($a['principal'] === $principal && $a['privilege'] === $privilege) {
  209. $this->addToAssertionCount(1);
  210. return;
  211. }
  212. }
  213. $this->fail("ACL does not contain $principal / $privilege");
  214. }
  215. protected function assertNotAcl($principal, $privilege, $acl) {
  216. foreach ($acl as $a) {
  217. if ($a['principal'] === $principal && $a['privilege'] === $privilege) {
  218. $this->fail("ACL contains $principal / $privilege");
  219. return;
  220. }
  221. }
  222. $this->addToAssertionCount(1);
  223. }
  224. protected function assertAccess($shouldHaveAcl, $principal, $privilege, $acl) {
  225. if ($shouldHaveAcl) {
  226. $this->assertAcl($principal, $privilege, $acl);
  227. } else {
  228. $this->assertNotAcl($principal, $privilege, $acl);
  229. }
  230. }
  231. }