You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractCalDavBackend.php 7.4KB

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