|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\Tests\unit\CalDAV; |
| 11 | + |
| 12 | +use OCA\DAV\CalDAV\CalDavBackend; |
| 13 | +use OCA\DAV\CalDAV\CalendarObject; |
| 14 | +use OCP\IL10N; |
| 15 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use Sabre\VObject\Component\VCalendar; |
| 18 | +use Sabre\VObject\Component\VEvent; |
| 19 | +use Sabre\VObject\Reader as VObjectReader; |
| 20 | +use Test\TestCase; |
| 21 | + |
| 22 | +class CalendarObjectTest extends TestCase { |
| 23 | + private readonly CalDavBackend&MockObject $calDavBackend; |
| 24 | + private readonly IL10N&MockObject $l10n; |
| 25 | + |
| 26 | + protected function setUp(): void { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $this->calDavBackend = $this->createMock(CalDavBackend::class); |
| 30 | + $this->l10n = $this->createMock(IL10N::class); |
| 31 | + |
| 32 | + $this->l10n->method('t') |
| 33 | + ->willReturnArgument(0); |
| 34 | + } |
| 35 | + |
| 36 | + public static function provideConfidentialObjectData(): array { |
| 37 | + return [ |
| 38 | + // Shared writable |
| 39 | + [ |
| 40 | + false, |
| 41 | + [ |
| 42 | + 'principaluri' => 'user1', |
| 43 | + '{http://owncloud.org/ns}owner-principal' => 'user2', |
| 44 | + ], |
| 45 | + ], |
| 46 | + [ |
| 47 | + false, |
| 48 | + [ |
| 49 | + 'principaluri' => 'user1', |
| 50 | + '{http://owncloud.org/ns}owner-principal' => 'user2', |
| 51 | + '{http://owncloud.org/ns}read-only' => 0, |
| 52 | + ], |
| 53 | + ], |
| 54 | + [ |
| 55 | + false, |
| 56 | + [ |
| 57 | + 'principaluri' => 'user1', |
| 58 | + '{http://owncloud.org/ns}owner-principal' => 'user2', |
| 59 | + '{http://owncloud.org/ns}read-only' => false, |
| 60 | + ], |
| 61 | + ], |
| 62 | + // Shared read-only |
| 63 | + [ |
| 64 | + true, |
| 65 | + [ |
| 66 | + 'principaluri' => 'user1', |
| 67 | + '{http://owncloud.org/ns}owner-principal' => 'user2', |
| 68 | + '{http://owncloud.org/ns}read-only' => 1, |
| 69 | + ], |
| 70 | + ], |
| 71 | + [ |
| 72 | + true, |
| 73 | + [ |
| 74 | + 'principaluri' => 'user1', |
| 75 | + '{http://owncloud.org/ns}owner-principal' => 'user2', |
| 76 | + '{http://owncloud.org/ns}read-only' => true, |
| 77 | + ], |
| 78 | + ], |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + #[DataProvider('provideConfidentialObjectData')] |
| 83 | + public function testGetWithConfidentialObject( |
| 84 | + bool $expectConfidential, |
| 85 | + array $calendarInfo, |
| 86 | + ): void { |
| 87 | + $ics = <<<EOF |
| 88 | +BEGIN:VCALENDAR |
| 89 | +CALSCALE:GREGORIAN |
| 90 | +VERSION:2.0 |
| 91 | +PRODID:-//IDN nextcloud.com//Calendar app 5.5.0-dev.1//EN |
| 92 | +BEGIN:VEVENT |
| 93 | +CREATED:20250820T102647Z |
| 94 | +DTSTAMP:20250820T103038Z |
| 95 | +LAST-MODIFIED:20250820T103038Z |
| 96 | +SEQUENCE:4 |
| 97 | +UID:a0f55f1f-4f0e-4db8-a54b-1e8b53846591 |
| 98 | +DTSTART;TZID=Europe/Berlin:20250822T110000 |
| 99 | +DTEND;TZID=Europe/Berlin:20250822T170000 |
| 100 | +STATUS:CONFIRMED |
| 101 | +SUMMARY:confidential-event |
| 102 | +CLASS:CONFIDENTIAL |
| 103 | +LOCATION:A location |
| 104 | +DESCRIPTION:A description |
| 105 | +END:VEVENT |
| 106 | +END:VCALENDAR |
| 107 | +EOF; |
| 108 | + VObjectReader::read($ics); |
| 109 | + |
| 110 | + $calendarObject = new CalendarObject( |
| 111 | + $this->calDavBackend, |
| 112 | + $this->l10n, |
| 113 | + $calendarInfo, |
| 114 | + [ |
| 115 | + 'uri' => 'a0f55f1f-4f0e-4db8-a54b-1e8b53846591.ics', |
| 116 | + 'calendardata' => $ics, |
| 117 | + 'classification' => 2, // CalDavBackend::CLASSIFICATION_CONFIDENTIAL |
| 118 | + ], |
| 119 | + ); |
| 120 | + |
| 121 | + $actualIcs = $calendarObject->get(); |
| 122 | + $vObject = VObjectReader::read($actualIcs); |
| 123 | + |
| 124 | + $this->assertInstanceOf(VCalendar::class, $vObject); |
| 125 | + $vEvent = $vObject->getBaseComponent('VEVENT'); |
| 126 | + $this->assertInstanceOf(VEvent::class, $vEvent); |
| 127 | + |
| 128 | + if ($expectConfidential) { |
| 129 | + $this->assertEquals('Busy', $vEvent->SUMMARY?->getValue()); |
| 130 | + $this->assertNull($vEvent->DESCRIPTION); |
| 131 | + $this->assertNull($vEvent->LOCATION); |
| 132 | + } else { |
| 133 | + $this->assertEquals('confidential-event', $vEvent->SUMMARY?->getValue()); |
| 134 | + $this->assertNotNull($vEvent->DESCRIPTION); |
| 135 | + $this->assertNotNull($vEvent->LOCATION); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments