aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/Service/UpcomingEventsServiceTest.php
blob: fdfe37d8918af21b31af5ccfaabc108069883966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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 IManager&MockObject $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());
	}
}