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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\User;
use OC\User\AvailabilityCoordinator;
use OC\User\OutOfOfficeData;
use OCA\DAV\CalDAV\TimezoneService;
use OCA\DAV\Db\Absence;
use OCA\DAV\Service\AbsenceService;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class AvailabilityCoordinatorTest extends TestCase {
private AvailabilityCoordinator $availabilityCoordinator;
private ICacheFactory $cacheFactory;
private ICache $cache;
private IConfig|MockObject $config;
private AbsenceService $absenceService;
private LoggerInterface $logger;
private MockObject|TimezoneService $timezoneService;
protected function setUp(): void {
parent::setUp();
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->cache = $this->createMock(ICache::class);
$this->absenceService = $this->createMock(AbsenceService::class);
$this->config = $this->createMock(IConfig::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->timezoneService = $this->createMock(TimezoneService::class);
$this->cacheFactory->expects(self::once())
->method('createLocal')
->willReturn($this->cache);
$this->availabilityCoordinator = new AvailabilityCoordinator(
$this->cacheFactory,
$this->config,
$this->absenceService,
$this->logger,
$this->timezoneService,
);
}
public function testIsEnabled(): void {
$this->config->expects(self::once())
->method('getAppValue')
->with('dav', 'hide_absence_settings', 'no')
->willReturn('no');
$isEnabled = $this->availabilityCoordinator->isEnabled();
self::assertTrue($isEnabled);
}
public function testGetOutOfOfficeDataInEffect(): void {
$absence = new Absence();
$absence->setId(420);
$absence->setUserId('user');
$absence->setFirstDay('2023-10-01');
$absence->setLastDay('2023-10-08');
$absence->setStatus('Vacation');
$absence->setMessage('On vacation');
$absence->setReplacementUserId('batman');
$absence->setReplacementUserDisplayName('Bruce Wayne');
$this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('user');
$this->cache->expects(self::exactly(2))
->method('get')
->willReturnOnConsecutiveCalls(null, null);
$this->absenceService->expects(self::once())
->method('getAbsence')
->with($user->getUID())
->willReturn($absence);
$this->cache->expects(self::exactly(2))
->method('set')
->withConsecutive([$user->getUID() . '_timezone', 'Europe/Berlin', 3600],
[$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300]);
$expected = new OutOfOfficeData(
'420',
$user,
1696111200,
1696802340,
'Vacation',
'On vacation',
'batman',
'Bruce Wayne',
);
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
self::assertEquals($expected, $actual);
}
public function testGetOutOfOfficeDataCachedAll(): void {
$absence = new Absence();
$absence->setId(420);
$absence->setUserId('user');
$absence->setFirstDay('2023-10-01');
$absence->setLastDay('2023-10-08');
$absence->setStatus('Vacation');
$absence->setMessage('On vacation');
$absence->setReplacementUserId('batman');
$absence->setReplacementUserDisplayName('Bruce Wayne');
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('user');
$this->cache->expects(self::exactly(2))
->method('get')
->willReturnOnConsecutiveCalls('UTC', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}');
$this->absenceService->expects(self::never())
->method('getAbsence');
$this->cache->expects(self::exactly(1))
->method('set');
$expected = new OutOfOfficeData(
'420',
$user,
1696118400,
1696809540,
'Vacation',
'On vacation',
'batman',
'Bruce Wayne'
);
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
self::assertEquals($expected, $actual);
}
public function testGetOutOfOfficeDataNoData(): void {
$absence = new Absence();
$absence->setId(420);
$absence->setUserId('user');
$absence->setFirstDay('2023-10-01');
$absence->setLastDay('2023-10-08');
$absence->setStatus('Vacation');
$absence->setMessage('On vacation');
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('user');
$this->cache->expects(self::exactly(2))
->method('get')
->willReturnOnConsecutiveCalls('UTC', null);
$this->absenceService->expects(self::once())
->method('getAbsence')
->willReturn(null);
$this->cache->expects(self::never())
->method('set');
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
self::assertNull($actual);
}
public function testGetOutOfOfficeDataWithInvalidCachedData(): void {
$absence = new Absence();
$absence->setId(420);
$absence->setUserId('user');
$absence->setFirstDay('2023-10-01');
$absence->setLastDay('2023-10-08');
$absence->setStatus('Vacation');
$absence->setMessage('On vacation');
$absence->setReplacementUserId('batman');
$absence->setReplacementUserDisplayName('Bruce Wayne');
$this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('user');
$this->cache->expects(self::exactly(2))
->method('get')
->willReturnOnConsecutiveCalls('UTC', '{"id":"420",}');
$this->absenceService->expects(self::once())
->method('getAbsence')
->with('user')
->willReturn($absence);
$this->cache->expects(self::once())
->method('set')
->with('user', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300);
$expected = new OutOfOfficeData(
'420',
$user,
1696118400,
1696809540,
'Vacation',
'On vacation',
'batman',
'Bruce Wayne'
);
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
self::assertEquals($expected, $actual);
}
}
|