AbstractCalDavBackend.php 7.6 KB

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