aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2024-05-22 09:10:24 +0200
committerChristoph Wurst <christoph@winzerhof-wurst.at>2024-08-13 20:03:31 +0200
commit370a9d77ea0aadd736d42741623cf98729797d8b (patch)
treec5fbe1f728de16c29dd6a99029eaa78eeb328bbb /apps/dav/tests
parentcee227ae993f02cf0c72ebcb103db4223b1b07a8 (diff)
downloadnextcloud-server-370a9d77ea0aadd736d42741623cf98729797d8b.tar.gz
nextcloud-server-370a9d77ea0aadd736d42741623cf98729797d8b.zip
feat(dav): Add an API for upcoming events
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'apps/dav/tests')
-rw-r--r--apps/dav/tests/unit/Controller/UpcomingEventsControllerTest.php74
-rw-r--r--apps/dav/tests/unit/Service/UpcomingEventsServiceTest.php89
2 files changed, 163 insertions, 0 deletions
diff --git a/apps/dav/tests/unit/Controller/UpcomingEventsControllerTest.php b/apps/dav/tests/unit/Controller/UpcomingEventsControllerTest.php
new file mode 100644
index 00000000000..9e5c03bb245
--- /dev/null
+++ b/apps/dav/tests/unit/Controller/UpcomingEventsControllerTest.php
@@ -0,0 +1,74 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\DAV\Tests\Unit\DAV\Service;
+
+use OCA\DAV\CalDAV\UpcomingEvent;
+use OCA\DAV\CalDAV\UpcomingEventsService;
+use OCA\DAV\Controller\UpcomingEventsController;
+use OCP\IRequest;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+
+class UpcomingEventsControllerTest extends TestCase {
+
+ private IRequest|MockObject $request;
+ private UpcomingEventsService|MockObject $service;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->request = $this->createMock(IRequest::class);
+ $this->service = $this->createMock(UpcomingEventsService::class);
+ }
+
+ public function testGetEventsAnonymously() {
+ $controller = new UpcomingEventsController(
+ $this->request,
+ null,
+ $this->service,
+ );
+
+ $response = $controller->getEvents('https://cloud.example.com/call/123');
+
+ self::assertNull($response->getData());
+ self::assertSame(401, $response->getStatus());
+ }
+
+ public function testGetEventsByLocation() {
+ $controller = new UpcomingEventsController(
+ $this->request,
+ 'u1',
+ $this->service,
+ );
+ $this->service->expects(self::once())
+ ->method('getEvents')
+ ->with('u1', 'https://cloud.example.com/call/123')
+ ->willReturn([
+ new UpcomingEvent(
+ 'abc-123',
+ null,
+ 'personal',
+ 123,
+ 'Test',
+ 'https://cloud.example.com/call/123',
+ null,
+ ),
+ ]);
+
+ $response = $controller->getEvents('https://cloud.example.com/call/123');
+
+ self::assertNotNull($response->getData());
+ self::assertIsArray($response->getData());
+ self::assertCount(1, $response->getData()['events']);
+ self::assertSame(200, $response->getStatus());
+ $event1 = $response->getData()['events'][0];
+ self::assertEquals('abc-123', $event1['uri']);
+ }
+}
diff --git a/apps/dav/tests/unit/Service/UpcomingEventsServiceTest.php b/apps/dav/tests/unit/Service/UpcomingEventsServiceTest.php
new file mode 100644
index 00000000000..ecb0268c8c2
--- /dev/null
+++ b/apps/dav/tests/unit/Service/UpcomingEventsServiceTest.php
@@ -0,0 +1,89 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\DAV\Tests\Unit\DAV\Service;
+
+use DateTimeImmutable;
+use OCA\DAV\CalDAV\UpcomingEventsService;
+use OCP\App\IAppManager;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\Calendar\ICalendarQuery;
+use OCP\Calendar\IManager;
+use OCP\IURLGenerator;
+use OCP\IUserManager;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+
+class UpcomingEventsServiceTest extends TestCase {
+
+ private MockObject|IManager $calendarManager;
+ private ITimeFactory|MockObject $timeFactory;
+ private IUserManager|MockObject $userManager;
+ private IAppManager|MockObject $appManager;
+ private IURLGenerator|MockObject $urlGenerator;
+ private UpcomingEventsService $service;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->calendarManager = $this->createMock(IManager::class);
+ $this->timeFactory = $this->createMock(ITimeFactory::class);
+ $this->userManager = $this->createMock(IUserManager::class);
+ $this->appManager = $this->createMock(IAppManager::class);
+ $this->urlGenerator = $this->createMock(IURLGenerator::class);
+
+ $this->service = new UpcomingEventsService(
+ $this->calendarManager,
+ $this->timeFactory,
+ $this->userManager,
+ $this->appManager,
+ $this->urlGenerator,
+ );
+ }
+
+ public function testGetEventsByLocation(): void {
+ $now = new DateTimeImmutable('2024-07-08T18:20:20Z');
+ $this->timeFactory->method('now')
+ ->willReturn($now);
+ $query = $this->createMock(ICalendarQuery::class);
+ $this->appManager->method('isEnabledForUser')->willReturn(false);
+ $this->calendarManager->method('newQuery')
+ ->with('principals/users/user1')
+ ->willReturn($query);
+ $query->expects(self::once())
+ ->method('addSearchProperty')
+ ->with('LOCATION');
+ $query->expects(self::once())
+ ->method('setSearchPattern')
+ ->with('https://cloud.example.com/call/123');
+ $this->calendarManager->expects(self::once())
+ ->method('searchForPrincipal')
+ ->with($query)
+ ->willReturn([
+ [
+ 'uri' => 'ev1',
+ 'calendar-key' => '1',
+ 'calendar-uri' => 'personal',
+ 'objects' => [
+ 0 => [
+ 'DTSTART' => [
+ new DateTimeImmutable('now'),
+ ],
+ ],
+ ],
+ ],
+ ]);
+
+ $events = $this->service->getEvents('user1', 'https://cloud.example.com/call/123');
+
+ self::assertCount(1, $events);
+ $event1 = $events[0];
+ self::assertEquals('ev1', $event1->getUri());
+ }
+}