AbstractCalDavBackend.php 8.4 KB

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