PublicCalendarTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV;
  7. use OCA\DAV\CalDAV\CalDavBackend;
  8. use OCA\DAV\CalDAV\PublicCalendar;
  9. use OCP\IConfig;
  10. use PHPUnit\Framework\MockObject\MockObject;
  11. use Psr\Log\LoggerInterface;
  12. use Sabre\VObject\Reader;
  13. class PublicCalendarTest extends CalendarTest {
  14. /**
  15. * @dataProvider providesConfidentialClassificationData
  16. * @param int $expectedChildren
  17. * @param bool $isShared
  18. */
  19. public function testPrivateClassification($expectedChildren, $isShared): void {
  20. $calObject0 = ['uri' => 'event-0', 'classification' => CalDavBackend::CLASSIFICATION_PUBLIC];
  21. $calObject1 = ['uri' => 'event-1', 'classification' => CalDavBackend::CLASSIFICATION_CONFIDENTIAL];
  22. $calObject2 = ['uri' => 'event-2', 'classification' => CalDavBackend::CLASSIFICATION_PRIVATE];
  23. /** @var MockObject | CalDavBackend $backend */
  24. $backend = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
  25. $backend->expects($this->any())->method('getCalendarObjects')->willReturn([
  26. $calObject0, $calObject1, $calObject2
  27. ]);
  28. $backend->expects($this->any())->method('getMultipleCalendarObjects')
  29. ->with(666, ['event-0', 'event-1', 'event-2'])
  30. ->willReturn([
  31. $calObject0, $calObject1, $calObject2
  32. ]);
  33. $backend->expects($this->any())->method('getCalendarObject')
  34. ->willReturn($calObject2)->with(666, 'event-2');
  35. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  36. $calendarInfo = [
  37. '{http://owncloud.org/ns}owner-principal' => 'user2',
  38. 'principaluri' => 'user2',
  39. 'id' => 666,
  40. 'uri' => 'cal',
  41. ];
  42. /** @var MockObject | IConfig $config */
  43. $config = $this->createMock(IConfig::class);
  44. /** @var MockObject | LoggerInterface $logger */
  45. $logger = $this->createMock(LoggerInterface::class);
  46. $c = new PublicCalendar($backend, $calendarInfo, $this->l10n, $config, $logger);
  47. $children = $c->getChildren();
  48. $this->assertEquals(2, count($children));
  49. $children = $c->getMultipleChildren(['event-0', 'event-1', 'event-2']);
  50. $this->assertEquals(2, count($children));
  51. $this->assertFalse($c->childExists('event-2'));
  52. }
  53. /**
  54. * @dataProvider providesConfidentialClassificationData
  55. * @param int $expectedChildren
  56. * @param bool $isShared
  57. */
  58. public function testConfidentialClassification($expectedChildren, $isShared): void {
  59. $start = '20160609';
  60. $end = '20160610';
  61. $calData = <<<EOD
  62. BEGIN:VCALENDAR
  63. PRODID:-//ownCloud calendar v1.2.2
  64. BEGIN:VEVENT
  65. CREATED:20160602T133732
  66. DTSTAMP:20160602T133732
  67. LAST-MODIFIED:20160602T133732
  68. UID:wej2z68l9h
  69. SUMMARY:Test Event
  70. LOCATION:Somewhere ...
  71. ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;CUTYPE=INDIVIDUAL;CN=de
  72. epdiver:MAILTO:thomas.mueller@tmit.eu
  73. ORGANIZER;CN=deepdiver:MAILTO:thomas.mueller@tmit.eu
  74. DESCRIPTION:maybe ....
  75. DTSTART;TZID=Europe/Berlin;VALUE=DATE:$start
  76. DTEND;TZID=Europe/Berlin;VALUE=DATE:$end
  77. RRULE:FREQ=DAILY
  78. BEGIN:VALARM
  79. ACTION:AUDIO
  80. TRIGGER:-PT15M
  81. END:VALARM
  82. END:VEVENT
  83. BEGIN:VTIMEZONE
  84. TZID:Europe/Berlin
  85. BEGIN:DAYLIGHT
  86. DTSTART:19810329T020000
  87. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  88. TZNAME:MESZ
  89. TZOFFSETFROM:+0100
  90. TZOFFSETTO:+0200
  91. END:DAYLIGHT
  92. BEGIN:STANDARD
  93. DTSTART:19961027T030000
  94. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  95. TZNAME:MEZ
  96. TZOFFSETFROM:+0200
  97. TZOFFSETTO:+0100
  98. END:STANDARD
  99. END:VTIMEZONE
  100. END:VCALENDAR
  101. EOD;
  102. $calObject0 = ['uri' => 'event-0', 'classification' => CalDavBackend::CLASSIFICATION_PUBLIC];
  103. $calObject1 = ['uri' => 'event-1', 'classification' => CalDavBackend::CLASSIFICATION_CONFIDENTIAL, 'calendardata' => $calData];
  104. $calObject2 = ['uri' => 'event-2', 'classification' => CalDavBackend::CLASSIFICATION_PRIVATE];
  105. /** @var MockObject | CalDavBackend $backend */
  106. $backend = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
  107. $backend->expects($this->any())->method('getCalendarObjects')->willReturn([
  108. $calObject0, $calObject1, $calObject2
  109. ]);
  110. $backend->expects($this->any())->method('getMultipleCalendarObjects')
  111. ->with(666, ['event-0', 'event-1', 'event-2'])
  112. ->willReturn([
  113. $calObject0, $calObject1, $calObject2
  114. ]);
  115. $backend->expects($this->any())->method('getCalendarObject')
  116. ->willReturn($calObject1)->with(666, 'event-1');
  117. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  118. $calendarInfo = [
  119. '{http://owncloud.org/ns}owner-principal' => 'user1',
  120. 'principaluri' => 'user2',
  121. 'id' => 666,
  122. 'uri' => 'cal',
  123. ];
  124. /** @var MockObject | IConfig $config */
  125. $config = $this->createMock(IConfig::class);
  126. /** @var MockObject | LoggerInterface $logger */
  127. $logger = $this->createMock(LoggerInterface::class);
  128. $c = new PublicCalendar($backend, $calendarInfo, $this->l10n, $config, $logger);
  129. $this->assertEquals(count($c->getChildren()), 2);
  130. // test private event
  131. $privateEvent = $c->getChild('event-1');
  132. $calData = $privateEvent->get();
  133. $event = Reader::read($calData);
  134. $this->assertEquals($start, $event->VEVENT->DTSTART->getValue());
  135. $this->assertEquals($end, $event->VEVENT->DTEND->getValue());
  136. $this->assertEquals('Busy', $event->VEVENT->SUMMARY->getValue());
  137. $this->assertArrayNotHasKey('ATTENDEE', $event->VEVENT);
  138. $this->assertArrayNotHasKey('LOCATION', $event->VEVENT);
  139. $this->assertArrayNotHasKey('DESCRIPTION', $event->VEVENT);
  140. $this->assertArrayNotHasKey('ORGANIZER', $event->VEVENT);
  141. }
  142. }