AbstractCalDavBackend.php 7.7 KB

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