aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/CalDAV/Activity/Provider/EventTest.php
blob: 4fd38c1aed2f218ebad744aea791d35dd07f9588 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\DAV\Tests\unit\CalDAV\Activity\Provider;

use InvalidArgumentException;
use OCA\DAV\CalDAV\Activity\Provider\Event;
use OCP\Activity\IEventMerger;
use OCP\Activity\IManager;
use OCP\App\IAppManager;
use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
use TypeError;

class EventTest extends TestCase {
	protected IUserManager&MockObject $userManager;
	protected IGroupManager&MockObject $groupManager;
	protected IURLGenerator&MockObject $url;
	protected IAppManager&MockObject $appManager;
	protected IFactory&MockObject $i10nFactory;
	protected IManager&MockObject $activityManager;
	protected IEventMerger&MockObject $eventMerger;
	protected Event&MockObject $provider;

	protected function setUp(): void {
		parent::setUp();
		$this->i10nFactory = $this->createMock(IFactory::class);
		$this->userManager = $this->createMock(IUserManager::class);
		$this->groupManager = $this->createMock(IGroupManager::class);
		$this->activityManager = $this->createMock(IManager::class);
		$this->url = $this->createMock(IURLGenerator::class);
		$this->appManager = $this->createMock(IAppManager::class);
		$this->eventMerger = $this->createMock(IEventMerger::class);
		$this->provider = $this->getMockBuilder(Event::class)
			->setConstructorArgs([
				$this->i10nFactory,
				$this->url,
				$this->activityManager,
				$this->userManager,
				$this->groupManager,
				$this->eventMerger,
				$this->appManager
			])
			->onlyMethods(['parse'])
			->getMock();
	}

	public static function dataGenerateObjectParameter(): array {
		$link = [
			'object_uri' => 'someuuid.ics',
			'calendar_uri' => 'personal',
			'owner' => 'someuser'
		];

		return [
			[23, 'c1', $link, true],
			[23, 'c1', $link, false],
			[42, 'c2', null],
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('dataGenerateObjectParameter')]
	public function testGenerateObjectParameter(int $id, string $name, ?array $link, bool $calendarAppEnabled = true): void {
		$affectedUser = 'otheruser';
		if ($link) {
			$affectedUser = $link['owner'];
			$generatedLink = [
				'objectId' => base64_encode('/remote.php/dav/calendars/' . $link['owner'] . '/' . $link['calendar_uri'] . '/' . $link['object_uri']),
			];
			$this->appManager->expects($this->once())
				->method('isEnabledForUser')
				->with('calendar')
				->willReturn($calendarAppEnabled);
			if ($calendarAppEnabled) {
				$this->url->expects($this->once())
					->method('getWebroot');
				$this->url->expects($this->once())
					->method('linkToRouteAbsolute')
					->with('calendar.view.indexdirect.edit', $generatedLink)
					->willReturn('fullLink');
			}
		}
		$objectParameter = ['id' => $id, 'name' => $name];
		if ($link) {
			$objectParameter['link'] = $link;
		}
		$result = [
			'type' => 'calendar-event',
			'id' => $id,
			'name' => $name,
		];
		if ($link && $calendarAppEnabled) {
			$result['link'] = 'fullLink';
		}
		$this->assertEquals($result, $this->invokePrivate($this->provider, 'generateObjectParameter', [$objectParameter, $affectedUser]));
	}

	public static function generateObjectParameterLinkEncodingDataProvider(): array {
		return [
			[ // Shared calendar
				[
					'object_uri' => 'someuuid.ics',
					'calendar_uri' => 'personal',
					'owner' => 'sharer'
				],
				base64_encode('/remote.php/dav/calendars/sharee/personal_shared_by_sharer/someuuid.ics'),
			],
			[ // Shared calendar with umlauts
				[
					'object_uri' => 'someuuid.ics',
					'calendar_uri' => 'umlaut_äüöß',
					'owner' => 'sharer'
				],
				base64_encode('/remote.php/dav/calendars/sharee/umlaut_%c3%a4%c3%bc%c3%b6%c3%9f_shared_by_sharer/someuuid.ics'),
			],
			[ // Shared calendar with umlauts and mixed casing
				[
					'object_uri' => 'someuuid.ics',
					'calendar_uri' => 'Umlaut_äüöß',
					'owner' => 'sharer'
				],
				base64_encode('/remote.php/dav/calendars/sharee/Umlaut_%c3%a4%c3%bc%c3%b6%c3%9f_shared_by_sharer/someuuid.ics'),
			],
			[ // Owned calendar with umlauts
				[
					'object_uri' => 'someuuid.ics',
					'calendar_uri' => 'umlaut_äüöß',
					'owner' => 'sharee'
				],
				base64_encode('/remote.php/dav/calendars/sharee/umlaut_%c3%a4%c3%bc%c3%b6%c3%9f/someuuid.ics'),
			],
			[ // Owned calendar with umlauts and mixed casing
				[
					'object_uri' => 'someuuid.ics',
					'calendar_uri' => 'Umlaut_äüöß',
					'owner' => 'sharee'
				],
				base64_encode('/remote.php/dav/calendars/sharee/Umlaut_%c3%a4%c3%bc%c3%b6%c3%9f/someuuid.ics'),
			],
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('generateObjectParameterLinkEncodingDataProvider')]
	public function testGenerateObjectParameterLinkEncoding(array $link, string $objectId): void {
		$generatedLink = [
			'objectId' => $objectId,
		];
		$this->appManager->expects($this->once())
			->method('isEnabledForUser')
			->with('calendar')
			->willReturn(true);
		$this->url->expects($this->once())
			->method('getWebroot');
		$this->url->expects($this->once())
			->method('linkToRouteAbsolute')
			->with('calendar.view.indexdirect.edit', $generatedLink)
			->willReturn('fullLink');
		$objectParameter = ['id' => 42, 'name' => 'calendar', 'link' => $link];
		$result = [
			'type' => 'calendar-event',
			'id' => 42,
			'name' => 'calendar',
			'link' => 'fullLink',
		];
		$this->assertEquals($result, $this->invokePrivate($this->provider, 'generateObjectParameter', [$objectParameter, 'sharee']));
	}

	public static function dataGenerateObjectParameterThrows(): array {
		return [
			['event', TypeError::class],
			[['name' => 'event']],
			[['id' => 42]],
		];
	}

	#[\PHPUnit\Framework\Attributes\DataProvider('dataGenerateObjectParameterThrows')]
	public function testGenerateObjectParameterThrows(string|array $eventData, string $exception = InvalidArgumentException::class): void {
		$this->expectException($exception);

		$this->invokePrivate($this->provider, 'generateObjectParameter', [$eventData, 'no_user']);
	}
}