From 80bc1c2d6caad27514c9e8eb8f5feba3c330baea Mon Sep 17 00:00:00 2001 From: Richard Steinmetz Date: Mon, 16 Dec 2024 21:20:50 +0100 Subject: feat(ocp): calendar event builder api Signed-off-by: Richard Steinmetz --- tests/data/ics/event-builder-complete.ics | 16 +++ tests/data/ics/event-builder-without-attendees.ics | 13 ++ tests/lib/Calendar/CalendarEventBuilderTest.php | 146 +++++++++++++++++++++ tests/lib/Calendar/ManagerTest.php | 53 +++++--- 4 files changed, 212 insertions(+), 16 deletions(-) create mode 100644 tests/data/ics/event-builder-complete.ics create mode 100644 tests/data/ics/event-builder-without-attendees.ics create mode 100644 tests/lib/Calendar/CalendarEventBuilderTest.php (limited to 'tests') diff --git a/tests/data/ics/event-builder-complete.ics b/tests/data/ics/event-builder-complete.ics new file mode 100644 index 00000000000..d96a3137356 --- /dev/null +++ b/tests/data/ics/event-builder-complete.ics @@ -0,0 +1,16 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Sabre//Sabre VObject 4.5.6//EN +CALSCALE:GREGORIAN +BEGIN:VEVENT +UID:event-uid-123 +DTSTAMP:20250105T000000Z +SUMMARY:My event +DTSTART:20250105T170958Z +DTEND:20250105T171958Z +DESCRIPTION:Foo bar baz +ORGANIZER:mailto:organizer@domain.tld +ATTENDEE:mailto:attendee1@domain.tld +ATTENDEE:mailto:attendee2@domain.tld +END:VEVENT +END:VCALENDAR diff --git a/tests/data/ics/event-builder-without-attendees.ics b/tests/data/ics/event-builder-without-attendees.ics new file mode 100644 index 00000000000..95c4c44fa1f --- /dev/null +++ b/tests/data/ics/event-builder-without-attendees.ics @@ -0,0 +1,13 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Sabre//Sabre VObject 4.5.6//EN +CALSCALE:GREGORIAN +BEGIN:VEVENT +UID:event-uid-123 +DTSTAMP:20250105T000000Z +SUMMARY:My event +DTSTART:20250105T170958Z +DTEND:20250105T171958Z +DESCRIPTION:Foo bar baz +END:VEVENT +END:VCALENDAR diff --git a/tests/lib/Calendar/CalendarEventBuilderTest.php b/tests/lib/Calendar/CalendarEventBuilderTest.php new file mode 100644 index 00000000000..48684d56093 --- /dev/null +++ b/tests/lib/Calendar/CalendarEventBuilderTest.php @@ -0,0 +1,146 @@ +timeFactory = $this->createMock(ITimeFactory::class); + $this->timeFactory->method('now') + ->willReturn(new DateTimeImmutable('20250105T000000Z')); + + $this->calendarEventBuilder = new CalendarEventBuilder( + 'event-uid-123', + $this->timeFactory, + ); + } + + public function testToIcs(): void { + $this->calendarEventBuilder->setStartDate(new DateTimeImmutable('2025-01-05T17:09:58Z')); + $this->calendarEventBuilder->setEndDate(new DateTimeImmutable('2025-01-05T17:19:58Z')); + $this->calendarEventBuilder->setSummary('My event'); + $this->calendarEventBuilder->setDescription('Foo bar baz'); + $this->calendarEventBuilder->setOrganizer('mailto:organizer@domain.tld'); + $this->calendarEventBuilder->addAttendee('mailto:attendee1@domain.tld'); + $this->calendarEventBuilder->addAttendee('mailto:attendee2@domain.tld'); + + $expected = file_get_contents(\OC::$SERVERROOT . '/tests/data/ics/event-builder-complete.ics'); + $actual = $this->calendarEventBuilder->toIcs(); + $this->assertEquals($expected, $actual); + } + + public function testToIcsWithoutOrganizerAndAttendees(): void { + $this->calendarEventBuilder->setStartDate(new DateTimeImmutable('2025-01-05T17:09:58Z')); + $this->calendarEventBuilder->setEndDate(new DateTimeImmutable('2025-01-05T17:19:58Z')); + $this->calendarEventBuilder->setSummary('My event'); + $this->calendarEventBuilder->setDescription('Foo bar baz'); + + $expected = file_get_contents(\OC::$SERVERROOT . '/tests/data/ics/event-builder-without-attendees.ics'); + $actual = $this->calendarEventBuilder->toIcs(); + $this->assertEquals($expected, $actual); + } + + public function testToIcsWithoutMailtoPrefix(): void { + $this->calendarEventBuilder->setStartDate(new DateTimeImmutable('2025-01-05T17:09:58Z')); + $this->calendarEventBuilder->setEndDate(new DateTimeImmutable('2025-01-05T17:19:58Z')); + $this->calendarEventBuilder->setSummary('My event'); + $this->calendarEventBuilder->setDescription('Foo bar baz'); + $this->calendarEventBuilder->setOrganizer('organizer@domain.tld'); + $this->calendarEventBuilder->addAttendee('attendee1@domain.tld'); + $this->calendarEventBuilder->addAttendee('attendee2@domain.tld'); + + $expected = file_get_contents(\OC::$SERVERROOT . '/tests/data/ics/event-builder-complete.ics'); + $actual = $this->calendarEventBuilder->toIcs(); + $this->assertEquals($expected, $actual); + } + + public function testCreateInCalendar(): void { + $this->calendarEventBuilder->setStartDate(new DateTimeImmutable('2025-01-05T17:09:58Z')); + $this->calendarEventBuilder->setEndDate(new DateTimeImmutable('2025-01-05T17:19:58Z')); + $this->calendarEventBuilder->setSummary('My event'); + $this->calendarEventBuilder->setDescription('Foo bar baz'); + $this->calendarEventBuilder->setOrganizer('organizer@domain.tld'); + $this->calendarEventBuilder->addAttendee('attendee1@domain.tld'); + $this->calendarEventBuilder->addAttendee('mailto:attendee2@domain.tld'); + + $expectedIcs = file_get_contents(\OC::$SERVERROOT . '/tests/data/ics/event-builder-complete.ics'); + $calendar = $this->createMock(ICreateFromString::class); + $calendar->expects(self::once()) + ->method('createFromString') + ->with('event-uid-123.ics', $expectedIcs); + + $actual = $this->calendarEventBuilder->createInCalendar($calendar); + $this->assertEquals('event-uid-123.ics', $actual); + } + + public function testToIcsWithoutStartDate(): void { + $this->calendarEventBuilder->setEndDate(new DateTimeImmutable('2025-01-05T17:19:58Z')); + $this->calendarEventBuilder->setSummary('My event'); + $this->calendarEventBuilder->setDescription('Foo bar baz'); + $this->calendarEventBuilder->setOrganizer('organizer@domain.tld'); + $this->calendarEventBuilder->addAttendee('attendee1@domain.tld'); + $this->calendarEventBuilder->addAttendee('mailto:attendee2@domain.tld'); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/start date/i'); + $this->calendarEventBuilder->toIcs(); + } + + public function testToIcsWithoutEndDate(): void { + $this->calendarEventBuilder->setStartDate(new DateTimeImmutable('2025-01-05T17:09:58Z')); + $this->calendarEventBuilder->setSummary('My event'); + $this->calendarEventBuilder->setDescription('Foo bar baz'); + $this->calendarEventBuilder->setOrganizer('organizer@domain.tld'); + $this->calendarEventBuilder->addAttendee('attendee1@domain.tld'); + $this->calendarEventBuilder->addAttendee('mailto:attendee2@domain.tld'); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/end date/i'); + $this->calendarEventBuilder->toIcs(); + } + + public function testToIcsWithoutSummary(): void { + $this->calendarEventBuilder->setStartDate(new DateTimeImmutable('2025-01-05T17:09:58Z')); + $this->calendarEventBuilder->setEndDate(new DateTimeImmutable('2025-01-05T17:19:58Z')); + $this->calendarEventBuilder->setDescription('Foo bar baz'); + $this->calendarEventBuilder->setOrganizer('organizer@domain.tld'); + $this->calendarEventBuilder->addAttendee('attendee1@domain.tld'); + $this->calendarEventBuilder->addAttendee('mailto:attendee2@domain.tld'); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/summary/i'); + $this->calendarEventBuilder->toIcs(); + } + + public function testToIcsWithoutOrganizerWithAttendees(): void { + $this->calendarEventBuilder->setStartDate(new DateTimeImmutable('2025-01-05T17:09:58Z')); + $this->calendarEventBuilder->setEndDate(new DateTimeImmutable('2025-01-05T17:19:58Z')); + $this->calendarEventBuilder->setSummary('My event'); + $this->calendarEventBuilder->setDescription('Foo bar baz'); + $this->calendarEventBuilder->addAttendee('attendee1@domain.tld'); + $this->calendarEventBuilder->addAttendee('mailto:attendee2@domain.tld'); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/organizer/i'); + $this->calendarEventBuilder->toIcs(); + } +} diff --git a/tests/lib/Calendar/ManagerTest.php b/tests/lib/Calendar/ManagerTest.php index f0ca278f352..a7aeed98046 100644 --- a/tests/lib/Calendar/ManagerTest.php +++ b/tests/lib/Calendar/ManagerTest.php @@ -14,6 +14,7 @@ use OCP\Calendar\ICalendarIsShared; use OCP\Calendar\ICalendarIsWritable; use OCP\Calendar\ICreateFromString; use OCP\Calendar\IHandleImipMessage; +use OCP\Security\ISecureRandom; use PHPUnit\Framework\MockObject\MockObject; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; @@ -44,6 +45,9 @@ class ManagerTest extends TestCase { /** @var ITimeFactory&MockObject */ private $time; + /** @var ISecureRandom&MockObject */ + private ISecureRandom $secureRandom; + private VCalendar $vCalendar1a; protected function setUp(): void { @@ -53,12 +57,14 @@ class ManagerTest extends TestCase { $this->container = $this->createMock(ContainerInterface::class); $this->logger = $this->createMock(LoggerInterface::class); $this->time = $this->createMock(ITimeFactory::class); + $this->secureRandom = $this->createMock(ISecureRandom::class); $this->manager = new Manager( $this->coordinator, $this->container, $this->logger, $this->time, + $this->secureRandom, ); // construct calendar with a 1 hour event and same start/end time zones @@ -260,7 +266,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->onlyMethods(['getCalendarsForPrincipal']) ->getMock(); @@ -291,7 +298,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->onlyMethods(['getCalendarsForPrincipal']) ->getMock(); @@ -321,7 +329,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->onlyMethods(['getCalendarsForPrincipal']) ->getMock(); @@ -352,7 +361,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->onlyMethods(['getCalendarsForPrincipal']) ->getMock(); @@ -384,7 +394,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->onlyMethods(['getCalendarsForPrincipal']) ->getMock(); @@ -416,7 +427,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->onlyMethods(['getCalendarsForPrincipal']) ->getMock(); @@ -448,7 +460,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->onlyMethods(['getCalendarsForPrincipal']) ->getMock(); @@ -491,7 +504,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->onlyMethods(['getCalendarsForPrincipal']) ->getMock(); @@ -534,7 +548,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->onlyMethods(['getCalendarsForPrincipal']) ->getMock(); @@ -612,7 +627,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->setMethods([ 'getCalendarsForPrincipal' @@ -643,7 +659,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->setMethods([ 'getCalendarsForPrincipal' @@ -680,7 +697,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->setMethods([ 'getCalendarsForPrincipal' @@ -767,7 +785,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->setMethods([ 'getCalendarsForPrincipal' @@ -800,7 +819,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->setMethods([ 'getCalendarsForPrincipal' @@ -837,7 +857,8 @@ class ManagerTest extends TestCase { $this->coordinator, $this->container, $this->logger, - $this->time + $this->time, + $this->secureRandom, ]) ->setMethods([ 'getCalendarsForPrincipal' @@ -866,7 +887,7 @@ class ManagerTest extends TestCase { $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); $this->assertTrue($result); } - + private function getVCalendarReply(): Document { $data = << Date: Wed, 8 Jan 2025 11:12:37 +0100 Subject: fix(license): Move license closer to file Signed-off-by: Joas Schilling --- .reuse/dep5 | 4 ---- tests/data/ics/event-builder-complete.ics.license | 2 ++ tests/data/ics/event-builder-without-attendees.ics.license | 2 ++ 3 files changed, 4 insertions(+), 4 deletions(-) create mode 100644 tests/data/ics/event-builder-complete.ics.license create mode 100644 tests/data/ics/event-builder-without-attendees.ics.license (limited to 'tests') diff --git a/.reuse/dep5 b/.reuse/dep5 index a3a24a3344d..12689bfc426 100644 --- a/.reuse/dep5 +++ b/.reuse/dep5 @@ -334,7 +334,3 @@ License: CC0-1.0 Files: apps/theming/fonts/OpenDyslexic-Bold.otf apps/theming/fonts/OpenDyslexic-Regular.otf Copyright: 2012-2019 Abbie Gonzalez , with Reserved Font Name OpenDyslexic. License: OFL-1.1-RFN - -Files: tests/data/ics/event-builder-complete.ics tests/data/ics/event-builder-without-attendees.ics -Copyright: 2024 Nextcloud GmbH and Nextcloud contributors -License: AGPL-3.0-or-later diff --git a/tests/data/ics/event-builder-complete.ics.license b/tests/data/ics/event-builder-complete.ics.license new file mode 100644 index 00000000000..f7f52efa96f --- /dev/null +++ b/tests/data/ics/event-builder-complete.ics.license @@ -0,0 +1,2 @@ +SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors +SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/tests/data/ics/event-builder-without-attendees.ics.license b/tests/data/ics/event-builder-without-attendees.ics.license new file mode 100644 index 00000000000..f7f52efa96f --- /dev/null +++ b/tests/data/ics/event-builder-without-attendees.ics.license @@ -0,0 +1,2 @@ +SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors +SPDX-License-Identifier: AGPL-3.0-or-later -- cgit v1.2.3