AbstractCalDavBackendTest.php 5.8 KB

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