AbstractCalDavBackend.php 8.0 KB

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