@@ -12,3134 +12,3134 @@ |
||
12 | 12 | use OCP\IEmojiHelper; |
13 | 13 | |
14 | 14 | class EmojiHelperTest extends TestCase { |
15 | - /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ |
|
16 | - private $db; |
|
15 | + /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ |
|
16 | + private $db; |
|
17 | 17 | |
18 | - private IEmojiHelper $helper; |
|
18 | + private IEmojiHelper $helper; |
|
19 | 19 | |
20 | - protected function setUp(): void { |
|
21 | - parent::setUp(); |
|
20 | + protected function setUp(): void { |
|
21 | + parent::setUp(); |
|
22 | 22 | |
23 | - $this->db = $this->createMock(IDBConnection::class); |
|
24 | - $this->helper = new EmojiHelper($this->db); |
|
25 | - } |
|
23 | + $this->db = $this->createMock(IDBConnection::class); |
|
24 | + $this->helper = new EmojiHelper($this->db); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * @param bool $supports4ByteText |
|
29 | - * @param bool $expected |
|
30 | - * |
|
31 | - * @dataProvider doesPlatformSupportEmojiDataProvider |
|
32 | - */ |
|
33 | - public function testDoesPlatformSupportEmoji(bool $supports4ByteText, bool $expected): void { |
|
34 | - $this->db->expects($this->once()) |
|
35 | - ->method('supports4ByteText') |
|
36 | - ->willReturn($supports4ByteText); |
|
27 | + /** |
|
28 | + * @param bool $supports4ByteText |
|
29 | + * @param bool $expected |
|
30 | + * |
|
31 | + * @dataProvider doesPlatformSupportEmojiDataProvider |
|
32 | + */ |
|
33 | + public function testDoesPlatformSupportEmoji(bool $supports4ByteText, bool $expected): void { |
|
34 | + $this->db->expects($this->once()) |
|
35 | + ->method('supports4ByteText') |
|
36 | + ->willReturn($supports4ByteText); |
|
37 | 37 | |
38 | - $this->assertEquals($expected, $this->helper->doesPlatformSupportEmoji()); |
|
39 | - } |
|
38 | + $this->assertEquals($expected, $this->helper->doesPlatformSupportEmoji()); |
|
39 | + } |
|
40 | 40 | |
41 | - public static function doesPlatformSupportEmojiDataProvider(): array { |
|
42 | - return [ |
|
43 | - [true, true], |
|
44 | - [false, false], |
|
45 | - ]; |
|
46 | - } |
|
41 | + public static function doesPlatformSupportEmojiDataProvider(): array { |
|
42 | + return [ |
|
43 | + [true, true], |
|
44 | + [false, false], |
|
45 | + ]; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param string $emoji |
|
50 | - * @param bool $expected |
|
51 | - * |
|
52 | - * @dataProvider isValidSingleEmojiDataProvider |
|
53 | - */ |
|
54 | - public function testIsValidSingleEmoji(string $emoji, bool $expected): void { |
|
55 | - $actual = $this->helper->isValidSingleEmoji($emoji); |
|
48 | + /** |
|
49 | + * @param string $emoji |
|
50 | + * @param bool $expected |
|
51 | + * |
|
52 | + * @dataProvider isValidSingleEmojiDataProvider |
|
53 | + */ |
|
54 | + public function testIsValidSingleEmoji(string $emoji, bool $expected): void { |
|
55 | + $actual = $this->helper->isValidSingleEmoji($emoji); |
|
56 | 56 | |
57 | - $this->assertEquals($expected, $actual); |
|
58 | - } |
|
57 | + $this->assertEquals($expected, $actual); |
|
58 | + } |
|
59 | 59 | |
60 | - public static function isValidSingleEmojiDataProvider(): array { |
|
61 | - return [ |
|
62 | - [' |
@@ -36,1591 +36,1591 @@ discard block |
||
36 | 36 | } |
37 | 37 | |
38 | 38 | class ManagerTest extends TestCase { |
39 | - /** @var Coordinator&MockObject */ |
|
40 | - private $coordinator; |
|
41 | - |
|
42 | - /** @var ContainerInterface&MockObject */ |
|
43 | - private $container; |
|
44 | - |
|
45 | - /** @var LoggerInterface&MockObject */ |
|
46 | - private $logger; |
|
47 | - |
|
48 | - /** @var Manager */ |
|
49 | - private $manager; |
|
50 | - |
|
51 | - /** @var ITimeFactory&MockObject */ |
|
52 | - private $time; |
|
53 | - |
|
54 | - /** @var ISecureRandom&MockObject */ |
|
55 | - private ISecureRandom $secureRandom; |
|
56 | - |
|
57 | - private IUserManager&MockObject $userManager; |
|
58 | - private ServerFactory&MockObject $serverFactory; |
|
59 | - |
|
60 | - private VCalendar $vCalendar1a; |
|
61 | - private VCalendar $vCalendar2a; |
|
62 | - private VCalendar $vCalendar3a; |
|
63 | - |
|
64 | - protected function setUp(): void { |
|
65 | - parent::setUp(); |
|
66 | - |
|
67 | - $this->coordinator = $this->createMock(Coordinator::class); |
|
68 | - $this->container = $this->createMock(ContainerInterface::class); |
|
69 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
70 | - $this->time = $this->createMock(ITimeFactory::class); |
|
71 | - $this->secureRandom = $this->createMock(ISecureRandom::class); |
|
72 | - $this->userManager = $this->createMock(IUserManager::class); |
|
73 | - $this->serverFactory = $this->createMock(ServerFactory::class); |
|
74 | - |
|
75 | - $this->manager = new Manager( |
|
76 | - $this->coordinator, |
|
77 | - $this->container, |
|
78 | - $this->logger, |
|
79 | - $this->time, |
|
80 | - $this->secureRandom, |
|
81 | - $this->userManager, |
|
82 | - $this->serverFactory, |
|
83 | - ); |
|
84 | - |
|
85 | - // construct calendar with a 1 hour event and same start/end time zones |
|
86 | - $this->vCalendar1a = new VCalendar(); |
|
87 | - /** @var VEvent $vEvent */ |
|
88 | - $vEvent = $this->vCalendar1a->add('VEVENT', []); |
|
89 | - $vEvent->UID->setValue('96a0e6b1-d886-4a55-a60d-152b31401dcc'); |
|
90 | - $vEvent->add('DTSTART', '20240701T080000', ['TZID' => 'America/Toronto']); |
|
91 | - $vEvent->add('DTEND', '20240701T090000', ['TZID' => 'America/Toronto']); |
|
92 | - $vEvent->add('SUMMARY', 'Test Event'); |
|
93 | - $vEvent->add('SEQUENCE', 3); |
|
94 | - $vEvent->add('STATUS', 'CONFIRMED'); |
|
95 | - $vEvent->add('TRANSP', 'OPAQUE'); |
|
96 | - $vEvent->add('ORGANIZER', 'mailto:[email protected]', ['CN' => 'Organizer']); |
|
97 | - $vEvent->add('ATTENDEE', 'mailto:[email protected]', [ |
|
98 | - 'CN' => 'Attendee One', |
|
99 | - 'CUTYPE' => 'INDIVIDUAL', |
|
100 | - 'PARTSTAT' => 'NEEDS-ACTION', |
|
101 | - 'ROLE' => 'REQ-PARTICIPANT', |
|
102 | - 'RSVP' => 'TRUE' |
|
103 | - ]); |
|
104 | - |
|
105 | - // construct calendar with a event for reply |
|
106 | - $this->vCalendar2a = new VCalendar(); |
|
107 | - /** @var VEvent $vEvent */ |
|
108 | - $vEvent = $this->vCalendar2a->add('VEVENT', []); |
|
109 | - $vEvent->UID->setValue('dcc733bf-b2b2-41f2-a8cf-550ae4b67aff'); |
|
110 | - $vEvent->add('DTSTART', '20210820'); |
|
111 | - $vEvent->add('DTEND', '20220821'); |
|
112 | - $vEvent->add('SUMMARY', 'berry basket'); |
|
113 | - $vEvent->add('SEQUENCE', 3); |
|
114 | - $vEvent->add('STATUS', 'CONFIRMED'); |
|
115 | - $vEvent->add('TRANSP', 'OPAQUE'); |
|
116 | - $vEvent->add('ORGANIZER', 'mailto:[email protected]', ['CN' => 'admin']); |
|
117 | - $vEvent->add('ATTENDEE', 'mailto:[email protected]', [ |
|
118 | - 'CN' => '[email protected]', |
|
119 | - 'CUTYPE' => 'INDIVIDUAL', |
|
120 | - 'ROLE' => 'REQ-PARTICIPANT', |
|
121 | - 'PARTSTAT' => 'ACCEPTED', |
|
122 | - ]); |
|
123 | - |
|
124 | - // construct calendar with a event for reply |
|
125 | - $this->vCalendar3a = new VCalendar(); |
|
126 | - /** @var VEvent $vEvent */ |
|
127 | - $vEvent = $this->vCalendar3a->add('VEVENT', []); |
|
128 | - $vEvent->UID->setValue('dcc733bf-b2b2-41f2-a8cf-550ae4b67aff'); |
|
129 | - $vEvent->add('DTSTART', '20210820'); |
|
130 | - $vEvent->add('DTEND', '20220821'); |
|
131 | - $vEvent->add('SUMMARY', 'berry basket'); |
|
132 | - $vEvent->add('SEQUENCE', 3); |
|
133 | - $vEvent->add('STATUS', 'CANCELLED'); |
|
134 | - $vEvent->add('TRANSP', 'OPAQUE'); |
|
135 | - $vEvent->add('ORGANIZER', 'mailto:[email protected]', ['CN' => 'admin']); |
|
136 | - $vEvent->add('ATTENDEE', 'mailto:[email protected]', [ |
|
137 | - 'CN' => '[email protected]', |
|
138 | - 'CUTYPE' => 'INDIVIDUAL', |
|
139 | - 'ROLE' => 'REQ-PARTICIPANT', |
|
140 | - 'PARTSTAT' => 'ACCEPTED', |
|
141 | - ]); |
|
142 | - |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @dataProvider searchProvider |
|
147 | - */ |
|
148 | - public function testSearch($search1, $search2, $expected): void { |
|
149 | - /** @var ICalendar | MockObject $calendar1 */ |
|
150 | - $calendar1 = $this->createMock(ICalendar::class); |
|
151 | - $calendar1->method('getKey')->willReturn('simple:1'); |
|
152 | - $calendar1->expects($this->once()) |
|
153 | - ->method('search') |
|
154 | - ->with('', [], [], null, null) |
|
155 | - ->willReturn($search1); |
|
156 | - |
|
157 | - /** @var ICalendar | MockObject $calendar2 */ |
|
158 | - $calendar2 = $this->createMock(ICalendar::class); |
|
159 | - $calendar2->method('getKey')->willReturn('simple:2'); |
|
160 | - $calendar2->expects($this->once()) |
|
161 | - ->method('search') |
|
162 | - ->with('', [], [], null, null) |
|
163 | - ->willReturn($search2); |
|
164 | - |
|
165 | - $this->manager->registerCalendar($calendar1); |
|
166 | - $this->manager->registerCalendar($calendar2); |
|
167 | - |
|
168 | - $result = $this->manager->search(''); |
|
169 | - $this->assertEquals($expected, $result); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @dataProvider searchProvider |
|
174 | - */ |
|
175 | - public function testSearchOptions($search1, $search2, $expected): void { |
|
176 | - /** @var ICalendar | MockObject $calendar1 */ |
|
177 | - $calendar1 = $this->createMock(ICalendar::class); |
|
178 | - $calendar1->method('getKey')->willReturn('simple:1'); |
|
179 | - $calendar1->expects($this->once()) |
|
180 | - ->method('search') |
|
181 | - ->with('searchTerm', ['SUMMARY', 'DESCRIPTION'], |
|
182 | - ['timerange' => ['start' => null, 'end' => null]], 5, 20) |
|
183 | - ->willReturn($search1); |
|
184 | - |
|
185 | - /** @var ICalendar | MockObject $calendar2 */ |
|
186 | - $calendar2 = $this->createMock(ICalendar::class); |
|
187 | - $calendar2->method('getKey')->willReturn('simple:2'); |
|
188 | - $calendar2->expects($this->once()) |
|
189 | - ->method('search') |
|
190 | - ->with('searchTerm', ['SUMMARY', 'DESCRIPTION'], |
|
191 | - ['timerange' => ['start' => null, 'end' => null]], 5, 20) |
|
192 | - ->willReturn($search2); |
|
193 | - |
|
194 | - $this->manager->registerCalendar($calendar1); |
|
195 | - $this->manager->registerCalendar($calendar2); |
|
196 | - |
|
197 | - $result = $this->manager->search('searchTerm', ['SUMMARY', 'DESCRIPTION'], |
|
198 | - ['timerange' => ['start' => null, 'end' => null]], 5, 20); |
|
199 | - $this->assertEquals($expected, $result); |
|
200 | - } |
|
201 | - |
|
202 | - public static function searchProvider(): array { |
|
203 | - $search1 = [ |
|
204 | - [ |
|
205 | - 'id' => 1, |
|
206 | - 'data' => 'foobar', |
|
207 | - ], |
|
208 | - [ |
|
209 | - 'id' => 2, |
|
210 | - 'data' => 'barfoo', |
|
211 | - ] |
|
212 | - ]; |
|
213 | - $search2 = [ |
|
214 | - [ |
|
215 | - 'id' => 3, |
|
216 | - 'data' => 'blablub', |
|
217 | - ], |
|
218 | - [ |
|
219 | - 'id' => 4, |
|
220 | - 'data' => 'blubbla', |
|
221 | - ] |
|
222 | - ]; |
|
223 | - |
|
224 | - $expected = [ |
|
225 | - [ |
|
226 | - 'id' => 1, |
|
227 | - 'data' => 'foobar', |
|
228 | - 'calendar-key' => 'simple:1', |
|
229 | - ], |
|
230 | - [ |
|
231 | - 'id' => 2, |
|
232 | - 'data' => 'barfoo', |
|
233 | - 'calendar-key' => 'simple:1', |
|
234 | - ], |
|
235 | - [ |
|
236 | - 'id' => 3, |
|
237 | - 'data' => 'blablub', |
|
238 | - 'calendar-key' => 'simple:2', |
|
239 | - ], |
|
240 | - [ |
|
241 | - 'id' => 4, |
|
242 | - 'data' => 'blubbla', |
|
243 | - 'calendar-key' => 'simple:2', |
|
244 | - ] |
|
245 | - ]; |
|
246 | - |
|
247 | - return [ |
|
248 | - [ |
|
249 | - $search1, |
|
250 | - $search2, |
|
251 | - $expected |
|
252 | - ] |
|
253 | - ]; |
|
254 | - } |
|
255 | - |
|
256 | - public function testRegisterUnregister(): void { |
|
257 | - /** @var ICalendar | MockObject $calendar1 */ |
|
258 | - $calendar1 = $this->createMock(ICalendar::class); |
|
259 | - $calendar1->method('getKey')->willReturn('key1'); |
|
260 | - |
|
261 | - /** @var ICalendar | MockObject $calendar2 */ |
|
262 | - $calendar2 = $this->createMock(ICalendar::class); |
|
263 | - $calendar2->method('getKey')->willReturn('key2'); |
|
264 | - |
|
265 | - $this->manager->registerCalendar($calendar1); |
|
266 | - $this->manager->registerCalendar($calendar2); |
|
267 | - |
|
268 | - $result = $this->manager->getCalendars(); |
|
269 | - $this->assertCount(2, $result); |
|
270 | - $this->assertContains($calendar1, $result); |
|
271 | - $this->assertContains($calendar2, $result); |
|
272 | - |
|
273 | - $this->manager->unregisterCalendar($calendar1); |
|
274 | - |
|
275 | - $result = $this->manager->getCalendars(); |
|
276 | - $this->assertCount(1, $result); |
|
277 | - $this->assertContains($calendar2, $result); |
|
278 | - } |
|
279 | - |
|
280 | - public function testGetCalendars(): void { |
|
281 | - /** @var ICalendar | MockObject $calendar1 */ |
|
282 | - $calendar1 = $this->createMock(ICalendar::class); |
|
283 | - $calendar1->method('getKey')->willReturn('key1'); |
|
284 | - |
|
285 | - /** @var ICalendar | MockObject $calendar2 */ |
|
286 | - $calendar2 = $this->createMock(ICalendar::class); |
|
287 | - $calendar2->method('getKey')->willReturn('key2'); |
|
288 | - |
|
289 | - $this->manager->registerCalendar($calendar1); |
|
290 | - $this->manager->registerCalendar($calendar2); |
|
291 | - |
|
292 | - $result = $this->manager->getCalendars(); |
|
293 | - $this->assertCount(2, $result); |
|
294 | - $this->assertContains($calendar1, $result); |
|
295 | - $this->assertContains($calendar2, $result); |
|
296 | - |
|
297 | - $this->manager->clear(); |
|
298 | - |
|
299 | - $result = $this->manager->getCalendars(); |
|
300 | - |
|
301 | - $this->assertCount(0, $result); |
|
302 | - } |
|
303 | - |
|
304 | - public function testEnabledIfNot(): void { |
|
305 | - $isEnabled = $this->manager->isEnabled(); |
|
306 | - $this->assertFalse($isEnabled); |
|
307 | - } |
|
308 | - |
|
309 | - public function testIfEnabledIfSo(): void { |
|
310 | - /** @var ICalendar | MockObject $calendar */ |
|
311 | - $calendar = $this->createMock(ICalendar::class); |
|
312 | - $this->manager->registerCalendar($calendar); |
|
313 | - |
|
314 | - $isEnabled = $this->manager->isEnabled(); |
|
315 | - $this->assertTrue($isEnabled); |
|
316 | - } |
|
317 | - |
|
318 | - public function testHandleImipRequestWithNoCalendars(): void { |
|
319 | - // construct calendar manager returns |
|
320 | - /** @var Manager&MockObject $manager */ |
|
321 | - $manager = $this->getMockBuilder(Manager::class) |
|
322 | - ->setConstructorArgs([ |
|
323 | - $this->coordinator, |
|
324 | - $this->container, |
|
325 | - $this->logger, |
|
326 | - $this->time, |
|
327 | - $this->secureRandom, |
|
328 | - $this->userManager, |
|
329 | - $this->serverFactory, |
|
330 | - ]) |
|
331 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
332 | - ->getMock(); |
|
333 | - $manager->expects(self::once()) |
|
334 | - ->method('getCalendarsForPrincipal') |
|
335 | - ->willReturn([]); |
|
336 | - // construct logger returns |
|
337 | - $this->logger->expects(self::once())->method('warning') |
|
338 | - ->with('iMip message could not be processed because user has no calendars'); |
|
339 | - // construct parameters |
|
340 | - $principalUri = 'principals/user/attendee1'; |
|
341 | - $sender = '[email protected]'; |
|
342 | - $recipient = '[email protected]'; |
|
343 | - $calendar = $this->vCalendar1a; |
|
344 | - $calendar->add('METHOD', 'REQUEST'); |
|
345 | - // Act |
|
346 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
347 | - // Assert |
|
348 | - $this->assertFalse($result); |
|
349 | - } |
|
350 | - |
|
351 | - public function testHandleImipRequestWithInvalidData(): void { |
|
352 | - // construct mock user calendar |
|
353 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
354 | - // construct mock calendar manager and returns |
|
355 | - /** @var Manager&MockObject $manager */ |
|
356 | - $manager = $this->getMockBuilder(Manager::class) |
|
357 | - ->setConstructorArgs([ |
|
358 | - $this->coordinator, |
|
359 | - $this->container, |
|
360 | - $this->logger, |
|
361 | - $this->time, |
|
362 | - $this->secureRandom, |
|
363 | - $this->userManager, |
|
364 | - $this->serverFactory, |
|
365 | - ]) |
|
366 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
367 | - ->getMock(); |
|
368 | - $manager->expects(self::once()) |
|
369 | - ->method('getCalendarsForPrincipal') |
|
370 | - ->willReturn([$userCalendar]); |
|
371 | - // construct logger returns |
|
372 | - $this->logger->expects(self::once())->method('error') |
|
373 | - ->with('iMip message could not be processed because an error occurred while parsing the iMip message'); |
|
374 | - // construct parameters |
|
375 | - $principalUri = 'principals/user/attendee1'; |
|
376 | - $sender = '[email protected]'; |
|
377 | - $recipient = '[email protected]'; |
|
378 | - // Act |
|
379 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, 'Invalid data'); |
|
380 | - // Assert |
|
381 | - $this->assertFalse($result); |
|
382 | - } |
|
383 | - |
|
384 | - public function testHandleImipRequestWithNoMethod(): void { |
|
385 | - // construct mock user calendar |
|
386 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
387 | - // construct mock calendar manager and returns |
|
388 | - /** @var Manager&MockObject $manager */ |
|
389 | - $manager = $this->getMockBuilder(Manager::class) |
|
390 | - ->setConstructorArgs([ |
|
391 | - $this->coordinator, |
|
392 | - $this->container, |
|
393 | - $this->logger, |
|
394 | - $this->time, |
|
395 | - $this->secureRandom, |
|
396 | - $this->userManager, |
|
397 | - $this->serverFactory, |
|
398 | - ]) |
|
399 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
400 | - ->getMock(); |
|
401 | - $manager->expects(self::once()) |
|
402 | - ->method('getCalendarsForPrincipal') |
|
403 | - ->willReturn([$userCalendar]); |
|
404 | - // construct logger returns |
|
405 | - $this->logger->expects(self::once())->method('warning') |
|
406 | - ->with('iMip message contains an incorrect or invalid method'); |
|
407 | - // construct parameters |
|
408 | - $principalUri = 'principals/user/attendee1'; |
|
409 | - $sender = '[email protected]'; |
|
410 | - $recipient = '[email protected]'; |
|
411 | - $calendar = $this->vCalendar1a; |
|
412 | - // Act |
|
413 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
414 | - // Assert |
|
415 | - $this->assertFalse($result); |
|
416 | - } |
|
417 | - |
|
418 | - public function testHandleImipRequestWithInvalidMethod(): void { |
|
419 | - // construct mock user calendar |
|
420 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
421 | - // construct mock calendar manager and returns |
|
422 | - /** @var Manager&MockObject $manager */ |
|
423 | - $manager = $this->getMockBuilder(Manager::class) |
|
424 | - ->setConstructorArgs([ |
|
425 | - $this->coordinator, |
|
426 | - $this->container, |
|
427 | - $this->logger, |
|
428 | - $this->time, |
|
429 | - $this->secureRandom, |
|
430 | - $this->userManager, |
|
431 | - $this->serverFactory, |
|
432 | - ]) |
|
433 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
434 | - ->getMock(); |
|
435 | - $manager->expects(self::once()) |
|
436 | - ->method('getCalendarsForPrincipal') |
|
437 | - ->willReturn([$userCalendar]); |
|
438 | - // construct logger returns |
|
439 | - $this->logger->expects(self::once())->method('warning') |
|
440 | - ->with('iMip message contains an incorrect or invalid method'); |
|
441 | - // construct parameters |
|
442 | - $principalUri = 'principals/user/attendee1'; |
|
443 | - $sender = '[email protected]'; |
|
444 | - $recipient = '[email protected]'; |
|
445 | - $calendar = $this->vCalendar1a; |
|
446 | - $calendar->add('METHOD', 'CANCEL'); |
|
447 | - // Act |
|
448 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
449 | - // Assert |
|
450 | - $this->assertFalse($result); |
|
451 | - } |
|
452 | - |
|
453 | - public function testHandleImipRequestWithNoEvent(): void { |
|
454 | - // construct mock user calendar |
|
455 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
456 | - // construct mock calendar manager and returns |
|
457 | - /** @var Manager&MockObject $manager */ |
|
458 | - $manager = $this->getMockBuilder(Manager::class) |
|
459 | - ->setConstructorArgs([ |
|
460 | - $this->coordinator, |
|
461 | - $this->container, |
|
462 | - $this->logger, |
|
463 | - $this->time, |
|
464 | - $this->secureRandom, |
|
465 | - $this->userManager, |
|
466 | - $this->serverFactory, |
|
467 | - ]) |
|
468 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
469 | - ->getMock(); |
|
470 | - $manager->expects(self::once()) |
|
471 | - ->method('getCalendarsForPrincipal') |
|
472 | - ->willReturn([$userCalendar]); |
|
473 | - // construct logger returns |
|
474 | - $this->logger->expects(self::once())->method('warning') |
|
475 | - ->with('iMip message contains no event'); |
|
476 | - // construct parameters |
|
477 | - $principalUri = 'principals/user/attendee1'; |
|
478 | - $sender = '[email protected]'; |
|
479 | - $recipient = '[email protected]'; |
|
480 | - $calendar = $this->vCalendar1a; |
|
481 | - $calendar->add('METHOD', 'REQUEST'); |
|
482 | - $calendar->remove('VEVENT'); |
|
483 | - // Act |
|
484 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
485 | - // Assert |
|
486 | - $this->assertFalse($result); |
|
487 | - } |
|
488 | - |
|
489 | - public function testHandleImipRequestWithNoUid(): void { |
|
490 | - // construct mock user calendar |
|
491 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
492 | - // construct mock calendar manager and returns |
|
493 | - /** @var Manager&MockObject $manager */ |
|
494 | - $manager = $this->getMockBuilder(Manager::class) |
|
495 | - ->setConstructorArgs([ |
|
496 | - $this->coordinator, |
|
497 | - $this->container, |
|
498 | - $this->logger, |
|
499 | - $this->time, |
|
500 | - $this->secureRandom, |
|
501 | - $this->userManager, |
|
502 | - $this->serverFactory, |
|
503 | - ]) |
|
504 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
505 | - ->getMock(); |
|
506 | - $manager->expects(self::once()) |
|
507 | - ->method('getCalendarsForPrincipal') |
|
508 | - ->willReturn([$userCalendar]); |
|
509 | - // construct logger returns |
|
510 | - $this->logger->expects(self::once())->method('warning') |
|
511 | - ->with('iMip message event dose not contains a UID'); |
|
512 | - // construct parameters |
|
513 | - $principalUri = 'principals/user/attendee1'; |
|
514 | - $sender = '[email protected]'; |
|
515 | - $recipient = '[email protected]'; |
|
516 | - $calendar = $this->vCalendar1a; |
|
517 | - $calendar->add('METHOD', 'REQUEST'); |
|
518 | - $calendar->VEVENT->remove('UID'); |
|
519 | - // Act |
|
520 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
521 | - // Assert |
|
522 | - $this->assertFalse($result); |
|
523 | - } |
|
524 | - |
|
525 | - public function testHandleImipRequestWithNoOrganizer(): void { |
|
526 | - // construct mock user calendar |
|
527 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
528 | - // construct mock calendar manager and returns |
|
529 | - /** @var Manager&MockObject $manager */ |
|
530 | - $manager = $this->getMockBuilder(Manager::class) |
|
531 | - ->setConstructorArgs([ |
|
532 | - $this->coordinator, |
|
533 | - $this->container, |
|
534 | - $this->logger, |
|
535 | - $this->time, |
|
536 | - $this->secureRandom, |
|
537 | - $this->userManager, |
|
538 | - $this->serverFactory, |
|
539 | - ]) |
|
540 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
541 | - ->getMock(); |
|
542 | - $manager->expects(self::once()) |
|
543 | - ->method('getCalendarsForPrincipal') |
|
544 | - ->willReturn([$userCalendar]); |
|
545 | - // construct logger returns |
|
546 | - $this->logger->expects(self::once())->method('warning') |
|
547 | - ->with('iMip message event dose not contains an organizer'); |
|
548 | - // construct parameters |
|
549 | - $principalUri = 'principals/user/attendee1'; |
|
550 | - $sender = '[email protected]'; |
|
551 | - $recipient = '[email protected]'; |
|
552 | - $calendar = $this->vCalendar1a; |
|
553 | - $calendar->add('METHOD', 'REQUEST'); |
|
554 | - $calendar->VEVENT->remove('ORGANIZER'); |
|
555 | - // Act |
|
556 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
557 | - // Assert |
|
558 | - $this->assertFalse($result); |
|
559 | - } |
|
560 | - |
|
561 | - public function testHandleImipRequestWithNoAttendee(): void { |
|
562 | - // construct mock user calendar |
|
563 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
564 | - // construct mock calendar manager and returns |
|
565 | - /** @var Manager&MockObject $manager */ |
|
566 | - $manager = $this->getMockBuilder(Manager::class) |
|
567 | - ->setConstructorArgs([ |
|
568 | - $this->coordinator, |
|
569 | - $this->container, |
|
570 | - $this->logger, |
|
571 | - $this->time, |
|
572 | - $this->secureRandom, |
|
573 | - $this->userManager, |
|
574 | - $this->serverFactory, |
|
575 | - ]) |
|
576 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
577 | - ->getMock(); |
|
578 | - $manager->expects(self::once()) |
|
579 | - ->method('getCalendarsForPrincipal') |
|
580 | - ->willReturn([$userCalendar]); |
|
581 | - // construct logger returns |
|
582 | - $this->logger->expects(self::once())->method('warning') |
|
583 | - ->with('iMip message event dose not contains any attendees'); |
|
584 | - // construct parameters |
|
585 | - $principalUri = 'principals/user/attendee1'; |
|
586 | - $sender = '[email protected]'; |
|
587 | - $recipient = '[email protected]'; |
|
588 | - $calendar = $this->vCalendar1a; |
|
589 | - $calendar->add('METHOD', 'REQUEST'); |
|
590 | - $calendar->VEVENT->remove('ATTENDEE'); |
|
591 | - // Act |
|
592 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
593 | - // Assert |
|
594 | - $this->assertFalse($result); |
|
595 | - } |
|
596 | - |
|
597 | - public function testHandleImipRequestWithInvalidAttendee(): void { |
|
598 | - // construct mock user calendar |
|
599 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
600 | - // construct mock calendar manager and returns |
|
601 | - /** @var Manager&MockObject $manager */ |
|
602 | - $manager = $this->getMockBuilder(Manager::class) |
|
603 | - ->setConstructorArgs([ |
|
604 | - $this->coordinator, |
|
605 | - $this->container, |
|
606 | - $this->logger, |
|
607 | - $this->time, |
|
608 | - $this->secureRandom, |
|
609 | - $this->userManager, |
|
610 | - $this->serverFactory, |
|
611 | - ]) |
|
612 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
613 | - ->getMock(); |
|
614 | - $manager->expects(self::once()) |
|
615 | - ->method('getCalendarsForPrincipal') |
|
616 | - ->willReturn([$userCalendar]); |
|
617 | - // construct logger returns |
|
618 | - $this->logger->expects(self::once())->method('warning') |
|
619 | - ->with('iMip message event does not contain a attendee that matches the recipient'); |
|
620 | - // construct parameters |
|
621 | - $principalUri = 'principals/user/attendee1'; |
|
622 | - $sender = '[email protected]'; |
|
623 | - $recipient = '[email protected]'; |
|
624 | - $calendar = $this->vCalendar1a; |
|
625 | - $calendar->add('METHOD', 'REQUEST'); |
|
626 | - // Act |
|
627 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
628 | - // Assert |
|
629 | - $this->assertFalse($result); |
|
630 | - } |
|
631 | - |
|
632 | - public function testHandleImipRequestWithNoMatch(): void { |
|
633 | - // construct mock user calendar |
|
634 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
635 | - $userCalendar->expects(self::once()) |
|
636 | - ->method('isDeleted') |
|
637 | - ->willReturn(false); |
|
638 | - $userCalendar->expects(self::once()) |
|
639 | - ->method('isWritable') |
|
640 | - ->willReturn(true); |
|
641 | - $userCalendar->expects(self::once()) |
|
642 | - ->method('isShared') |
|
643 | - ->willReturn(false); |
|
644 | - $userCalendar->expects(self::once()) |
|
645 | - ->method('search') |
|
646 | - ->willReturn([]); |
|
647 | - // construct mock calendar manager and returns |
|
648 | - /** @var Manager&MockObject $manager */ |
|
649 | - $manager = $this->getMockBuilder(Manager::class) |
|
650 | - ->setConstructorArgs([ |
|
651 | - $this->coordinator, |
|
652 | - $this->container, |
|
653 | - $this->logger, |
|
654 | - $this->time, |
|
655 | - $this->secureRandom, |
|
656 | - $this->userManager, |
|
657 | - $this->serverFactory, |
|
658 | - ]) |
|
659 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
660 | - ->getMock(); |
|
661 | - $manager->expects(self::once()) |
|
662 | - ->method('getCalendarsForPrincipal') |
|
663 | - ->willReturn([$userCalendar]); |
|
664 | - // construct logger returns |
|
665 | - $this->logger->expects(self::once())->method('warning') |
|
666 | - ->with('iMip message event could not be processed because no corresponding event was found in any calendar'); |
|
667 | - // construct parameters |
|
668 | - $principalUri = 'principals/user/attendee1'; |
|
669 | - $sender = '[email protected]'; |
|
670 | - $recipient = '[email protected]'; |
|
671 | - $calendar = $this->vCalendar1a; |
|
672 | - $calendar->add('METHOD', 'REQUEST'); |
|
673 | - // Act |
|
674 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
675 | - // Assert |
|
676 | - $this->assertFalse($result); |
|
677 | - } |
|
678 | - |
|
679 | - public function testHandleImipRequest(): void { |
|
680 | - // construct mock user calendar |
|
681 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
682 | - $userCalendar->expects(self::once()) |
|
683 | - ->method('isDeleted') |
|
684 | - ->willReturn(false); |
|
685 | - $userCalendar->expects(self::once()) |
|
686 | - ->method('isWritable') |
|
687 | - ->willReturn(true); |
|
688 | - $userCalendar->expects(self::once()) |
|
689 | - ->method('isShared') |
|
690 | - ->willReturn(false); |
|
691 | - $userCalendar->expects(self::once()) |
|
692 | - ->method('search') |
|
693 | - ->willReturn([['uri' => 'principals/user/attendee1/personal']]); |
|
694 | - // construct mock calendar manager and returns |
|
695 | - /** @var Manager&MockObject $manager */ |
|
696 | - $manager = $this->getMockBuilder(Manager::class) |
|
697 | - ->setConstructorArgs([ |
|
698 | - $this->coordinator, |
|
699 | - $this->container, |
|
700 | - $this->logger, |
|
701 | - $this->time, |
|
702 | - $this->secureRandom, |
|
703 | - $this->userManager, |
|
704 | - $this->serverFactory, |
|
705 | - ]) |
|
706 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
707 | - ->getMock(); |
|
708 | - $manager->expects(self::once()) |
|
709 | - ->method('getCalendarsForPrincipal') |
|
710 | - ->willReturn([$userCalendar]); |
|
711 | - // construct parameters |
|
712 | - $principalUri = 'principals/user/attendee1'; |
|
713 | - $sender = '[email protected]'; |
|
714 | - $recipient = '[email protected]'; |
|
715 | - $calendar = $this->vCalendar1a; |
|
716 | - $calendar->add('METHOD', 'REQUEST'); |
|
717 | - // construct user calendar returns |
|
718 | - $userCalendar->expects(self::once()) |
|
719 | - ->method('handleIMipMessage') |
|
720 | - ->with('', $calendar->serialize()); |
|
721 | - // Act |
|
722 | - $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
723 | - // Assert |
|
724 | - $this->assertTrue($result); |
|
725 | - } |
|
726 | - |
|
727 | - public function testHandleImipReplyWithNoCalendars(): void { |
|
728 | - // construct calendar manager returns |
|
729 | - /** @var Manager&MockObject $manager */ |
|
730 | - $manager = $this->getMockBuilder(Manager::class) |
|
731 | - ->setConstructorArgs([ |
|
732 | - $this->coordinator, |
|
733 | - $this->container, |
|
734 | - $this->logger, |
|
735 | - $this->time, |
|
736 | - $this->secureRandom, |
|
737 | - $this->userManager, |
|
738 | - $this->serverFactory, |
|
739 | - ]) |
|
740 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
741 | - ->getMock(); |
|
742 | - $manager->expects(self::once()) |
|
743 | - ->method('getCalendarsForPrincipal') |
|
744 | - ->willReturn([]); |
|
745 | - // construct logger returns |
|
746 | - $this->logger->expects(self::once())->method('warning') |
|
747 | - ->with('iMip message could not be processed because user has no calendars'); |
|
748 | - // construct parameters |
|
749 | - $principalUri = 'principals/user/linus'; |
|
750 | - $sender = '[email protected]'; |
|
751 | - $recipient = '[email protected]'; |
|
752 | - $calendar = $this->vCalendar2a; |
|
753 | - $calendar->add('METHOD', 'REPLY'); |
|
754 | - // Act |
|
755 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
756 | - // Assert |
|
757 | - $this->assertFalse($result); |
|
758 | - } |
|
759 | - |
|
760 | - public function testHandleImipReplyWithInvalidData(): void { |
|
761 | - // construct mock user calendar |
|
762 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
763 | - // construct mock calendar manager and returns |
|
764 | - /** @var Manager&MockObject $manager */ |
|
765 | - $manager = $this->getMockBuilder(Manager::class) |
|
766 | - ->setConstructorArgs([ |
|
767 | - $this->coordinator, |
|
768 | - $this->container, |
|
769 | - $this->logger, |
|
770 | - $this->time, |
|
771 | - $this->secureRandom, |
|
772 | - $this->userManager, |
|
773 | - $this->serverFactory, |
|
774 | - ]) |
|
775 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
776 | - ->getMock(); |
|
777 | - $manager->expects(self::once()) |
|
778 | - ->method('getCalendarsForPrincipal') |
|
779 | - ->willReturn([$userCalendar]); |
|
780 | - // construct logger returns |
|
781 | - $this->logger->expects(self::once())->method('error') |
|
782 | - ->with('iMip message could not be processed because an error occurred while parsing the iMip message'); |
|
783 | - // construct parameters |
|
784 | - $principalUri = 'principals/user/attendee1'; |
|
785 | - $sender = '[email protected]'; |
|
786 | - $recipient = '[email protected]'; |
|
787 | - // Act |
|
788 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, 'Invalid data'); |
|
789 | - // Assert |
|
790 | - $this->assertFalse($result); |
|
791 | - } |
|
792 | - |
|
793 | - public function testHandleImipReplyWithNoMethod(): void { |
|
794 | - // construct mock user calendar |
|
795 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
796 | - // construct mock calendar manager and returns |
|
797 | - /** @var Manager&MockObject $manager */ |
|
798 | - $manager = $this->getMockBuilder(Manager::class) |
|
799 | - ->setConstructorArgs([ |
|
800 | - $this->coordinator, |
|
801 | - $this->container, |
|
802 | - $this->logger, |
|
803 | - $this->time, |
|
804 | - $this->secureRandom, |
|
805 | - $this->userManager, |
|
806 | - $this->serverFactory, |
|
807 | - ]) |
|
808 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
809 | - ->getMock(); |
|
810 | - $manager->expects(self::once()) |
|
811 | - ->method('getCalendarsForPrincipal') |
|
812 | - ->willReturn([$userCalendar]); |
|
813 | - // construct logger returns |
|
814 | - $this->logger->expects(self::once())->method('warning') |
|
815 | - ->with('iMip message contains an incorrect or invalid method'); |
|
816 | - // construct parameters |
|
817 | - $principalUri = 'principals/user/linus'; |
|
818 | - $sender = '[email protected]'; |
|
819 | - $recipient = '[email protected]'; |
|
820 | - $calendar = $this->vCalendar2a; |
|
821 | - // Act |
|
822 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
823 | - // Assert |
|
824 | - $this->assertFalse($result); |
|
825 | - } |
|
826 | - |
|
827 | - public function testHandleImipReplyWithInvalidMethod(): void { |
|
828 | - // construct mock user calendar |
|
829 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
830 | - // construct mock calendar manager and returns |
|
831 | - /** @var Manager&MockObject $manager */ |
|
832 | - $manager = $this->getMockBuilder(Manager::class) |
|
833 | - ->setConstructorArgs([ |
|
834 | - $this->coordinator, |
|
835 | - $this->container, |
|
836 | - $this->logger, |
|
837 | - $this->time, |
|
838 | - $this->secureRandom, |
|
839 | - $this->userManager, |
|
840 | - $this->serverFactory, |
|
841 | - ]) |
|
842 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
843 | - ->getMock(); |
|
844 | - $manager->expects(self::once()) |
|
845 | - ->method('getCalendarsForPrincipal') |
|
846 | - ->willReturn([$userCalendar]); |
|
847 | - // construct logger returns |
|
848 | - $this->logger->expects(self::once())->method('warning') |
|
849 | - ->with('iMip message contains an incorrect or invalid method'); |
|
850 | - // construct parameters |
|
851 | - $principalUri = 'principals/user/linus'; |
|
852 | - $sender = '[email protected]'; |
|
853 | - $recipient = '[email protected]'; |
|
854 | - $calendar = $this->vCalendar2a; |
|
855 | - $calendar->add('METHOD', 'UNKNOWN'); |
|
856 | - // Act |
|
857 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
858 | - // Assert |
|
859 | - $this->assertFalse($result); |
|
860 | - } |
|
861 | - |
|
862 | - public function testHandleImipReplyWithNoEvent(): void { |
|
863 | - // construct mock user calendar |
|
864 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
865 | - // construct mock calendar manager and returns |
|
866 | - /** @var Manager&MockObject $manager */ |
|
867 | - $manager = $this->getMockBuilder(Manager::class) |
|
868 | - ->setConstructorArgs([ |
|
869 | - $this->coordinator, |
|
870 | - $this->container, |
|
871 | - $this->logger, |
|
872 | - $this->time, |
|
873 | - $this->secureRandom, |
|
874 | - $this->userManager, |
|
875 | - $this->serverFactory, |
|
876 | - ]) |
|
877 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
878 | - ->getMock(); |
|
879 | - $manager->expects(self::once()) |
|
880 | - ->method('getCalendarsForPrincipal') |
|
881 | - ->willReturn([$userCalendar]); |
|
882 | - // construct logger returns |
|
883 | - $this->logger->expects(self::once())->method('warning') |
|
884 | - ->with('iMip message contains no event'); |
|
885 | - // construct parameters |
|
886 | - $principalUri = 'principals/user/linus'; |
|
887 | - $sender = '[email protected]'; |
|
888 | - $recipient = '[email protected]'; |
|
889 | - $calendar = $this->vCalendar2a; |
|
890 | - $calendar->add('METHOD', 'REPLY'); |
|
891 | - $calendar->remove('VEVENT'); |
|
892 | - // Act |
|
893 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
894 | - // Assert |
|
895 | - $this->assertFalse($result); |
|
896 | - } |
|
897 | - |
|
898 | - public function testHandleImipReplyWithNoUid(): void { |
|
899 | - // construct mock user calendar |
|
900 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
901 | - // construct mock calendar manager and returns |
|
902 | - /** @var Manager&MockObject $manager */ |
|
903 | - $manager = $this->getMockBuilder(Manager::class) |
|
904 | - ->setConstructorArgs([ |
|
905 | - $this->coordinator, |
|
906 | - $this->container, |
|
907 | - $this->logger, |
|
908 | - $this->time, |
|
909 | - $this->secureRandom, |
|
910 | - $this->userManager, |
|
911 | - $this->serverFactory, |
|
912 | - ]) |
|
913 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
914 | - ->getMock(); |
|
915 | - $manager->expects(self::once()) |
|
916 | - ->method('getCalendarsForPrincipal') |
|
917 | - ->willReturn([$userCalendar]); |
|
918 | - // construct logger returns |
|
919 | - $this->logger->expects(self::once())->method('warning') |
|
920 | - ->with('iMip message event dose not contains a UID'); |
|
921 | - // construct parameters |
|
922 | - $principalUri = 'principals/user/linus'; |
|
923 | - $sender = '[email protected]'; |
|
924 | - $recipient = '[email protected]'; |
|
925 | - $calendar = $this->vCalendar2a; |
|
926 | - $calendar->add('METHOD', 'REPLY'); |
|
927 | - $calendar->VEVENT->remove('UID'); |
|
928 | - // Act |
|
929 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
930 | - // Assert |
|
931 | - $this->assertFalse($result); |
|
932 | - } |
|
933 | - |
|
934 | - public function testHandleImipReplyWithNoOrganizer(): void { |
|
935 | - // construct mock user calendar |
|
936 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
937 | - // construct mock calendar manager and returns |
|
938 | - /** @var Manager&MockObject $manager */ |
|
939 | - $manager = $this->getMockBuilder(Manager::class) |
|
940 | - ->setConstructorArgs([ |
|
941 | - $this->coordinator, |
|
942 | - $this->container, |
|
943 | - $this->logger, |
|
944 | - $this->time, |
|
945 | - $this->secureRandom, |
|
946 | - $this->userManager, |
|
947 | - $this->serverFactory, |
|
948 | - ]) |
|
949 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
950 | - ->getMock(); |
|
951 | - $manager->expects(self::once()) |
|
952 | - ->method('getCalendarsForPrincipal') |
|
953 | - ->willReturn([$userCalendar]); |
|
954 | - // construct logger returns |
|
955 | - $this->logger->expects(self::once())->method('warning') |
|
956 | - ->with('iMip message event dose not contains an organizer'); |
|
957 | - // construct parameters |
|
958 | - $principalUri = 'principals/user/linus'; |
|
959 | - $sender = '[email protected]'; |
|
960 | - $recipient = '[email protected]'; |
|
961 | - $calendar = $this->vCalendar2a; |
|
962 | - $calendar->add('METHOD', 'REPLY'); |
|
963 | - $calendar->VEVENT->remove('ORGANIZER'); |
|
964 | - // Act |
|
965 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
966 | - // Assert |
|
967 | - $this->assertFalse($result); |
|
968 | - } |
|
969 | - |
|
970 | - public function testHandleImipReplyWithNoAttendee(): void { |
|
971 | - // construct mock user calendar |
|
972 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
973 | - // construct mock calendar manager and returns |
|
974 | - /** @var Manager&MockObject $manager */ |
|
975 | - $manager = $this->getMockBuilder(Manager::class) |
|
976 | - ->setConstructorArgs([ |
|
977 | - $this->coordinator, |
|
978 | - $this->container, |
|
979 | - $this->logger, |
|
980 | - $this->time, |
|
981 | - $this->secureRandom, |
|
982 | - $this->userManager, |
|
983 | - $this->serverFactory, |
|
984 | - ]) |
|
985 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
986 | - ->getMock(); |
|
987 | - $manager->expects(self::once()) |
|
988 | - ->method('getCalendarsForPrincipal') |
|
989 | - ->willReturn([$userCalendar]); |
|
990 | - // construct logger returns |
|
991 | - $this->logger->expects(self::once())->method('warning') |
|
992 | - ->with('iMip message event dose not contains any attendees'); |
|
993 | - // construct parameters |
|
994 | - $principalUri = 'principals/user/linus'; |
|
995 | - $sender = '[email protected]'; |
|
996 | - $recipient = '[email protected]'; |
|
997 | - $calendar = $this->vCalendar2a; |
|
998 | - $calendar->add('METHOD', 'REPLY'); |
|
999 | - $calendar->VEVENT->remove('ATTENDEE'); |
|
1000 | - // Act |
|
1001 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
1002 | - // Assert |
|
1003 | - $this->assertFalse($result); |
|
1004 | - } |
|
1005 | - |
|
1006 | - public function testHandleImipReplyDateInThePast(): void { |
|
1007 | - // construct mock user calendar |
|
1008 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1009 | - // construct mock calendar manager and returns |
|
1010 | - /** @var Manager&MockObject $manager */ |
|
1011 | - $manager = $this->getMockBuilder(Manager::class) |
|
1012 | - ->setConstructorArgs([ |
|
1013 | - $this->coordinator, |
|
1014 | - $this->container, |
|
1015 | - $this->logger, |
|
1016 | - $this->time, |
|
1017 | - $this->secureRandom, |
|
1018 | - $this->userManager, |
|
1019 | - $this->serverFactory, |
|
1020 | - ]) |
|
1021 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1022 | - ->getMock(); |
|
1023 | - $manager->expects(self::once()) |
|
1024 | - ->method('getCalendarsForPrincipal') |
|
1025 | - ->willReturn([$userCalendar]); |
|
1026 | - // construct logger and time returns |
|
1027 | - $this->logger->expects(self::once())->method('warning') |
|
1028 | - ->with('iMip message event could not be processed because the event is in the past'); |
|
1029 | - $this->time->expects(self::once()) |
|
1030 | - ->method('getTime') |
|
1031 | - ->willReturn(time()); |
|
1032 | - // construct parameters |
|
1033 | - $principalUri = 'principals/user/linus'; |
|
1034 | - $sender = '[email protected]'; |
|
1035 | - $recipient = '[email protected]'; |
|
1036 | - $calendarData = clone $this->vCalendar2a; |
|
1037 | - $calendarData->add('METHOD', 'REPLY'); |
|
1038 | - $calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past |
|
1039 | - // Act |
|
1040 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); |
|
1041 | - // Assert |
|
1042 | - $this->assertFalse($result); |
|
1043 | - } |
|
1044 | - |
|
1045 | - public function testHandleImipReplyEventNotFound(): void { |
|
1046 | - // construct mock user calendar |
|
1047 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1048 | - $userCalendar->expects(self::once()) |
|
1049 | - ->method('search') |
|
1050 | - ->willReturn([]); |
|
1051 | - // construct mock calendar manager and returns |
|
1052 | - /** @var Manager&MockObject $manager */ |
|
1053 | - $manager = $this->getMockBuilder(Manager::class) |
|
1054 | - ->setConstructorArgs([ |
|
1055 | - $this->coordinator, |
|
1056 | - $this->container, |
|
1057 | - $this->logger, |
|
1058 | - $this->time, |
|
1059 | - $this->secureRandom, |
|
1060 | - $this->userManager, |
|
1061 | - $this->serverFactory, |
|
1062 | - ]) |
|
1063 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1064 | - ->getMock(); |
|
1065 | - $manager->expects(self::once()) |
|
1066 | - ->method('getCalendarsForPrincipal') |
|
1067 | - ->willReturn([$userCalendar]); |
|
1068 | - // construct time returns |
|
1069 | - $this->time->expects(self::once()) |
|
1070 | - ->method('getTime') |
|
1071 | - ->willReturn(1628374233); |
|
1072 | - // construct parameters |
|
1073 | - $principalUri = 'principals/user/linus'; |
|
1074 | - $sender = '[email protected]'; |
|
1075 | - $recipient = '[email protected]'; |
|
1076 | - $calendarData = clone $this->vCalendar2a; |
|
1077 | - $calendarData->add('METHOD', 'REPLY'); |
|
1078 | - // construct logger return |
|
1079 | - $this->logger->expects(self::once())->method('warning') |
|
1080 | - ->with('iMip message event could not be processed because no corresponding event was found in any calendar ' . $principalUri . 'and UID' . $calendarData->VEVENT->UID->getValue()); |
|
1081 | - // Act |
|
1082 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); |
|
1083 | - // Assert |
|
1084 | - $this->assertFalse($result); |
|
1085 | - } |
|
1086 | - |
|
1087 | - public function testHandleImipReply(): void { |
|
1088 | - /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ |
|
1089 | - $manager = $this->getMockBuilder(Manager::class) |
|
1090 | - ->setConstructorArgs([ |
|
1091 | - $this->coordinator, |
|
1092 | - $this->container, |
|
1093 | - $this->logger, |
|
1094 | - $this->time, |
|
1095 | - $this->secureRandom, |
|
1096 | - $this->userManager, |
|
1097 | - $this->serverFactory, |
|
1098 | - ]) |
|
1099 | - ->onlyMethods([ |
|
1100 | - 'getCalendarsForPrincipal' |
|
1101 | - ]) |
|
1102 | - ->getMock(); |
|
1103 | - $calendar = $this->createMock(ITestCalendar::class); |
|
1104 | - $principalUri = 'principals/user/linus'; |
|
1105 | - $sender = '[email protected]'; |
|
1106 | - $recipient = '[email protected]'; |
|
1107 | - $calendarData = clone $this->vCalendar2a; |
|
1108 | - $calendarData->add('METHOD', 'REPLY'); |
|
1109 | - |
|
1110 | - $this->time->expects(self::once()) |
|
1111 | - ->method('getTime') |
|
1112 | - ->willReturn(1628374233); |
|
1113 | - $manager->expects(self::once()) |
|
1114 | - ->method('getCalendarsForPrincipal') |
|
1115 | - ->willReturn([$calendar]); |
|
1116 | - $calendar->expects(self::once()) |
|
1117 | - ->method('search') |
|
1118 | - ->willReturn([['uri' => 'testname.ics']]); |
|
1119 | - $calendar->expects(self::once()) |
|
1120 | - ->method('handleIMipMessage') |
|
1121 | - ->with('testname.ics', $calendarData->serialize()); |
|
1122 | - // Act |
|
1123 | - $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); |
|
1124 | - // Assert |
|
1125 | - $this->assertTrue($result); |
|
1126 | - } |
|
1127 | - |
|
1128 | - public function testHandleImipCancelWithNoCalendars(): void { |
|
1129 | - // construct calendar manager returns |
|
1130 | - /** @var Manager&MockObject $manager */ |
|
1131 | - $manager = $this->getMockBuilder(Manager::class) |
|
1132 | - ->setConstructorArgs([ |
|
1133 | - $this->coordinator, |
|
1134 | - $this->container, |
|
1135 | - $this->logger, |
|
1136 | - $this->time, |
|
1137 | - $this->secureRandom, |
|
1138 | - $this->userManager, |
|
1139 | - $this->serverFactory, |
|
1140 | - ]) |
|
1141 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1142 | - ->getMock(); |
|
1143 | - $manager->expects(self::once()) |
|
1144 | - ->method('getCalendarsForPrincipal') |
|
1145 | - ->willReturn([]); |
|
1146 | - // construct logger returns |
|
1147 | - $this->logger->expects(self::once())->method('warning') |
|
1148 | - ->with('iMip message could not be processed because user has no calendars'); |
|
1149 | - // construct parameters |
|
1150 | - $principalUri = 'principals/user/pierre'; |
|
1151 | - $sender = '[email protected]'; |
|
1152 | - $recipient = '[email protected]'; |
|
1153 | - $replyTo = null; |
|
1154 | - $calendar = $this->vCalendar3a; |
|
1155 | - $calendar->add('METHOD', 'CANCEL'); |
|
1156 | - // Act |
|
1157 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1158 | - // Assert |
|
1159 | - $this->assertFalse($result); |
|
1160 | - } |
|
1161 | - |
|
1162 | - public function testHandleImipCancelWithInvalidData(): void { |
|
1163 | - // construct mock user calendar |
|
1164 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1165 | - // construct mock calendar manager and returns |
|
1166 | - /** @var Manager&MockObject $manager */ |
|
1167 | - $manager = $this->getMockBuilder(Manager::class) |
|
1168 | - ->setConstructorArgs([ |
|
1169 | - $this->coordinator, |
|
1170 | - $this->container, |
|
1171 | - $this->logger, |
|
1172 | - $this->time, |
|
1173 | - $this->secureRandom, |
|
1174 | - $this->userManager, |
|
1175 | - $this->serverFactory, |
|
1176 | - ]) |
|
1177 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1178 | - ->getMock(); |
|
1179 | - $manager->expects(self::once()) |
|
1180 | - ->method('getCalendarsForPrincipal') |
|
1181 | - ->willReturn([$userCalendar]); |
|
1182 | - // construct logger returns |
|
1183 | - $this->logger->expects(self::once())->method('error') |
|
1184 | - ->with('iMip message could not be processed because an error occurred while parsing the iMip message'); |
|
1185 | - // construct parameters |
|
1186 | - $principalUri = 'principals/user/attendee1'; |
|
1187 | - $sender = '[email protected]'; |
|
1188 | - $recipient = '[email protected]'; |
|
1189 | - $replyTo = null; |
|
1190 | - // Act |
|
1191 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, 'Invalid data'); |
|
1192 | - // Assert |
|
1193 | - $this->assertFalse($result); |
|
1194 | - } |
|
1195 | - |
|
1196 | - |
|
1197 | - public function testHandleImipCancelWithNoMethod(): void { |
|
1198 | - // construct mock user calendar |
|
1199 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1200 | - // construct mock calendar manager and returns |
|
1201 | - /** @var Manager&MockObject $manager */ |
|
1202 | - $manager = $this->getMockBuilder(Manager::class) |
|
1203 | - ->setConstructorArgs([ |
|
1204 | - $this->coordinator, |
|
1205 | - $this->container, |
|
1206 | - $this->logger, |
|
1207 | - $this->time, |
|
1208 | - $this->secureRandom, |
|
1209 | - $this->userManager, |
|
1210 | - $this->serverFactory, |
|
1211 | - ]) |
|
1212 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1213 | - ->getMock(); |
|
1214 | - $manager->expects(self::once()) |
|
1215 | - ->method('getCalendarsForPrincipal') |
|
1216 | - ->willReturn([$userCalendar]); |
|
1217 | - // construct logger returns |
|
1218 | - $this->logger->expects(self::once())->method('warning') |
|
1219 | - ->with('iMip message contains an incorrect or invalid method'); |
|
1220 | - // construct parameters |
|
1221 | - $principalUri = 'principals/user/pierre'; |
|
1222 | - $sender = '[email protected]'; |
|
1223 | - $recipient = '[email protected]'; |
|
1224 | - $replyTo = null; |
|
1225 | - $calendar = $this->vCalendar3a; |
|
1226 | - // Act |
|
1227 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1228 | - // Assert |
|
1229 | - $this->assertFalse($result); |
|
1230 | - } |
|
1231 | - |
|
1232 | - public function testHandleImipCancelWithInvalidMethod(): void { |
|
1233 | - // construct mock user calendar |
|
1234 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1235 | - // construct mock calendar manager and returns |
|
1236 | - /** @var Manager&MockObject $manager */ |
|
1237 | - $manager = $this->getMockBuilder(Manager::class) |
|
1238 | - ->setConstructorArgs([ |
|
1239 | - $this->coordinator, |
|
1240 | - $this->container, |
|
1241 | - $this->logger, |
|
1242 | - $this->time, |
|
1243 | - $this->secureRandom, |
|
1244 | - $this->userManager, |
|
1245 | - $this->serverFactory, |
|
1246 | - ]) |
|
1247 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1248 | - ->getMock(); |
|
1249 | - $manager->expects(self::once()) |
|
1250 | - ->method('getCalendarsForPrincipal') |
|
1251 | - ->willReturn([$userCalendar]); |
|
1252 | - // construct logger returns |
|
1253 | - $this->logger->expects(self::once())->method('warning') |
|
1254 | - ->with('iMip message contains an incorrect or invalid method'); |
|
1255 | - // construct parameters |
|
1256 | - $principalUri = 'principals/user/pierre'; |
|
1257 | - $sender = '[email protected]'; |
|
1258 | - $recipient = '[email protected]'; |
|
1259 | - $replyTo = null; |
|
1260 | - $calendar = $this->vCalendar3a; |
|
1261 | - $calendar->add('METHOD', 'UNKNOWN'); |
|
1262 | - // Act |
|
1263 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1264 | - // Assert |
|
1265 | - $this->assertFalse($result); |
|
1266 | - } |
|
1267 | - |
|
1268 | - public function testHandleImipCancelWithNoEvent(): void { |
|
1269 | - // construct mock user calendar |
|
1270 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1271 | - // construct mock calendar manager and returns |
|
1272 | - /** @var Manager&MockObject $manager */ |
|
1273 | - $manager = $this->getMockBuilder(Manager::class) |
|
1274 | - ->setConstructorArgs([ |
|
1275 | - $this->coordinator, |
|
1276 | - $this->container, |
|
1277 | - $this->logger, |
|
1278 | - $this->time, |
|
1279 | - $this->secureRandom, |
|
1280 | - $this->userManager, |
|
1281 | - $this->serverFactory, |
|
1282 | - ]) |
|
1283 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1284 | - ->getMock(); |
|
1285 | - $manager->expects(self::once()) |
|
1286 | - ->method('getCalendarsForPrincipal') |
|
1287 | - ->willReturn([$userCalendar]); |
|
1288 | - // construct logger returns |
|
1289 | - $this->logger->expects(self::once())->method('warning') |
|
1290 | - ->with('iMip message contains no event'); |
|
1291 | - // construct parameters |
|
1292 | - $principalUri = 'principals/user/pierre'; |
|
1293 | - $sender = '[email protected]'; |
|
1294 | - $recipient = '[email protected]'; |
|
1295 | - $replyTo = null; |
|
1296 | - $calendar = $this->vCalendar3a; |
|
1297 | - $calendar->add('METHOD', 'CANCEL'); |
|
1298 | - $calendar->remove('VEVENT'); |
|
1299 | - // Act |
|
1300 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1301 | - // Assert |
|
1302 | - $this->assertFalse($result); |
|
1303 | - } |
|
1304 | - |
|
1305 | - public function testHandleImipCancelWithNoUid(): void { |
|
1306 | - // construct mock user calendar |
|
1307 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1308 | - // construct mock calendar manager and returns |
|
1309 | - /** @var Manager&MockObject $manager */ |
|
1310 | - $manager = $this->getMockBuilder(Manager::class) |
|
1311 | - ->setConstructorArgs([ |
|
1312 | - $this->coordinator, |
|
1313 | - $this->container, |
|
1314 | - $this->logger, |
|
1315 | - $this->time, |
|
1316 | - $this->secureRandom, |
|
1317 | - $this->userManager, |
|
1318 | - $this->serverFactory, |
|
1319 | - ]) |
|
1320 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1321 | - ->getMock(); |
|
1322 | - $manager->expects(self::once()) |
|
1323 | - ->method('getCalendarsForPrincipal') |
|
1324 | - ->willReturn([$userCalendar]); |
|
1325 | - // construct logger returns |
|
1326 | - $this->logger->expects(self::once())->method('warning') |
|
1327 | - ->with('iMip message event dose not contains a UID'); |
|
1328 | - // construct parameters |
|
1329 | - $principalUri = 'principals/user/pierre'; |
|
1330 | - $sender = '[email protected]'; |
|
1331 | - $recipient = '[email protected]'; |
|
1332 | - $replyTo = null; |
|
1333 | - $calendar = $this->vCalendar3a; |
|
1334 | - $calendar->add('METHOD', 'CANCEL'); |
|
1335 | - $calendar->VEVENT->remove('UID'); |
|
1336 | - // Act |
|
1337 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1338 | - // Assert |
|
1339 | - $this->assertFalse($result); |
|
1340 | - } |
|
1341 | - |
|
1342 | - public function testHandleImipCancelWithNoOrganizer(): void { |
|
1343 | - // construct mock user calendar |
|
1344 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1345 | - // construct mock calendar manager and returns |
|
1346 | - /** @var Manager&MockObject $manager */ |
|
1347 | - $manager = $this->getMockBuilder(Manager::class) |
|
1348 | - ->setConstructorArgs([ |
|
1349 | - $this->coordinator, |
|
1350 | - $this->container, |
|
1351 | - $this->logger, |
|
1352 | - $this->time, |
|
1353 | - $this->secureRandom, |
|
1354 | - $this->userManager, |
|
1355 | - $this->serverFactory, |
|
1356 | - ]) |
|
1357 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1358 | - ->getMock(); |
|
1359 | - $manager->expects(self::once()) |
|
1360 | - ->method('getCalendarsForPrincipal') |
|
1361 | - ->willReturn([$userCalendar]); |
|
1362 | - // construct logger returns |
|
1363 | - $this->logger->expects(self::once())->method('warning') |
|
1364 | - ->with('iMip message event dose not contains an organizer'); |
|
1365 | - // construct parameters |
|
1366 | - $principalUri = 'principals/user/pierre'; |
|
1367 | - $sender = '[email protected]'; |
|
1368 | - $recipient = '[email protected]'; |
|
1369 | - $replyTo = null; |
|
1370 | - $calendar = $this->vCalendar3a; |
|
1371 | - $calendar->add('METHOD', 'CANCEL'); |
|
1372 | - $calendar->VEVENT->remove('ORGANIZER'); |
|
1373 | - // Act |
|
1374 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1375 | - // Assert |
|
1376 | - $this->assertFalse($result); |
|
1377 | - } |
|
1378 | - |
|
1379 | - public function testHandleImipCancelWithNoAttendee(): void { |
|
1380 | - // construct mock user calendar |
|
1381 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1382 | - // construct mock calendar manager and returns |
|
1383 | - /** @var Manager&MockObject $manager */ |
|
1384 | - $manager = $this->getMockBuilder(Manager::class) |
|
1385 | - ->setConstructorArgs([ |
|
1386 | - $this->coordinator, |
|
1387 | - $this->container, |
|
1388 | - $this->logger, |
|
1389 | - $this->time, |
|
1390 | - $this->secureRandom, |
|
1391 | - $this->userManager, |
|
1392 | - $this->serverFactory, |
|
1393 | - ]) |
|
1394 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1395 | - ->getMock(); |
|
1396 | - $manager->expects(self::once()) |
|
1397 | - ->method('getCalendarsForPrincipal') |
|
1398 | - ->willReturn([$userCalendar]); |
|
1399 | - // construct logger returns |
|
1400 | - $this->logger->expects(self::once())->method('warning') |
|
1401 | - ->with('iMip message event dose not contains any attendees'); |
|
1402 | - // construct parameters |
|
1403 | - $principalUri = 'principals/user/pierre'; |
|
1404 | - $sender = '[email protected]'; |
|
1405 | - $recipient = '[email protected]'; |
|
1406 | - $replyTo = null; |
|
1407 | - $calendar = $this->vCalendar3a; |
|
1408 | - $calendar->add('METHOD', 'CANCEL'); |
|
1409 | - $calendar->VEVENT->remove('ATTENDEE'); |
|
1410 | - // Act |
|
1411 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1412 | - // Assert |
|
1413 | - $this->assertFalse($result); |
|
1414 | - } |
|
1415 | - |
|
1416 | - public function testHandleImipCancelAttendeeNotRecipient(): void { |
|
1417 | - // construct mock user calendar |
|
1418 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1419 | - // construct mock calendar manager and returns |
|
1420 | - /** @var Manager&MockObject $manager */ |
|
1421 | - $manager = $this->getMockBuilder(Manager::class) |
|
1422 | - ->setConstructorArgs([ |
|
1423 | - $this->coordinator, |
|
1424 | - $this->container, |
|
1425 | - $this->logger, |
|
1426 | - $this->time, |
|
1427 | - $this->secureRandom, |
|
1428 | - $this->userManager, |
|
1429 | - $this->serverFactory, |
|
1430 | - ]) |
|
1431 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1432 | - ->getMock(); |
|
1433 | - $manager->expects(self::once()) |
|
1434 | - ->method('getCalendarsForPrincipal') |
|
1435 | - ->willReturn([$userCalendar]); |
|
1436 | - // construct logger returns |
|
1437 | - $this->logger->expects(self::once())->method('warning') |
|
1438 | - ->with('iMip message event could not be processed because recipient must be an ATTENDEE of this event'); |
|
1439 | - // construct parameters |
|
1440 | - $principalUri = 'principals/user/pierre'; |
|
1441 | - $sender = '[email protected]'; |
|
1442 | - $recipient = '[email protected]'; |
|
1443 | - $replyTo = null; |
|
1444 | - $calendarData = clone $this->vCalendar3a; |
|
1445 | - $calendarData->add('METHOD', 'CANCEL'); |
|
1446 | - // Act |
|
1447 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1448 | - // Assert |
|
1449 | - $this->assertFalse($result); |
|
1450 | - } |
|
1451 | - |
|
1452 | - public function testHandleImipCancelDateInThePast(): void { |
|
1453 | - // construct mock user calendar |
|
1454 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1455 | - // construct mock calendar manager and returns |
|
1456 | - /** @var Manager&MockObject $manager */ |
|
1457 | - $manager = $this->getMockBuilder(Manager::class) |
|
1458 | - ->setConstructorArgs([ |
|
1459 | - $this->coordinator, |
|
1460 | - $this->container, |
|
1461 | - $this->logger, |
|
1462 | - $this->time, |
|
1463 | - $this->secureRandom, |
|
1464 | - $this->userManager, |
|
1465 | - $this->serverFactory, |
|
1466 | - ]) |
|
1467 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1468 | - ->getMock(); |
|
1469 | - $manager->expects(self::once()) |
|
1470 | - ->method('getCalendarsForPrincipal') |
|
1471 | - ->willReturn([$userCalendar]); |
|
1472 | - // construct logger and time returns |
|
1473 | - $this->logger->expects(self::once())->method('warning') |
|
1474 | - ->with('iMip message event could not be processed because the event is in the past'); |
|
1475 | - $this->time->expects(self::once()) |
|
1476 | - ->method('getTime') |
|
1477 | - ->willReturn(time()); |
|
1478 | - // construct parameters |
|
1479 | - $principalUri = 'principals/user/pierre'; |
|
1480 | - $sender = '[email protected]'; |
|
1481 | - $recipient = '[email protected]'; |
|
1482 | - $replyTo = null; |
|
1483 | - $calendarData = clone $this->vCalendar3a; |
|
1484 | - $calendarData->add('METHOD', 'CANCEL'); |
|
1485 | - $calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past |
|
1486 | - // Act |
|
1487 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1488 | - // Assert |
|
1489 | - $this->assertFalse($result); |
|
1490 | - } |
|
1491 | - |
|
1492 | - public function testHandleImipCancelEventNotFound(): void { |
|
1493 | - // construct mock user calendar |
|
1494 | - $userCalendar = $this->createMock(ITestCalendar::class); |
|
1495 | - $userCalendar->expects(self::once()) |
|
1496 | - ->method('search') |
|
1497 | - ->willReturn([]); |
|
1498 | - // construct mock calendar manager and returns |
|
1499 | - /** @var Manager&MockObject $manager */ |
|
1500 | - $manager = $this->getMockBuilder(Manager::class) |
|
1501 | - ->setConstructorArgs([ |
|
1502 | - $this->coordinator, |
|
1503 | - $this->container, |
|
1504 | - $this->logger, |
|
1505 | - $this->time, |
|
1506 | - $this->secureRandom, |
|
1507 | - $this->userManager, |
|
1508 | - $this->serverFactory, |
|
1509 | - ]) |
|
1510 | - ->onlyMethods(['getCalendarsForPrincipal']) |
|
1511 | - ->getMock(); |
|
1512 | - $manager->expects(self::once()) |
|
1513 | - ->method('getCalendarsForPrincipal') |
|
1514 | - ->willReturn([$userCalendar]); |
|
1515 | - // construct time returns |
|
1516 | - $this->time->expects(self::once()) |
|
1517 | - ->method('getTime') |
|
1518 | - ->willReturn(1628374233); |
|
1519 | - // construct parameters |
|
1520 | - $principalUri = 'principals/user/pierre'; |
|
1521 | - $sender = '[email protected]'; |
|
1522 | - $recipient = '[email protected]'; |
|
1523 | - $replyTo = null; |
|
1524 | - $calendarData = clone $this->vCalendar3a; |
|
1525 | - $calendarData->add('METHOD', 'CANCEL'); |
|
1526 | - // construct logger return |
|
1527 | - $this->logger->expects(self::once())->method('warning') |
|
1528 | - ->with('iMip message event could not be processed because no corresponding event was found in any calendar ' . $principalUri . 'and UID' . $calendarData->VEVENT->UID->getValue()); |
|
1529 | - // Act |
|
1530 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1531 | - // Assert |
|
1532 | - $this->assertFalse($result); |
|
1533 | - } |
|
1534 | - |
|
1535 | - public function testHandleImipCancelOrganiserInReplyTo(): void { |
|
1536 | - /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ |
|
1537 | - $manager = $this->getMockBuilder(Manager::class) |
|
1538 | - ->setConstructorArgs([ |
|
1539 | - $this->coordinator, |
|
1540 | - $this->container, |
|
1541 | - $this->logger, |
|
1542 | - $this->time, |
|
1543 | - $this->secureRandom, |
|
1544 | - $this->userManager, |
|
1545 | - $this->serverFactory, |
|
1546 | - ]) |
|
1547 | - ->onlyMethods([ |
|
1548 | - 'getCalendarsForPrincipal' |
|
1549 | - ]) |
|
1550 | - ->getMock(); |
|
1551 | - |
|
1552 | - $principalUri = 'principals/user/pierre'; |
|
1553 | - $sender = '[email protected]'; |
|
1554 | - $recipient = '[email protected]'; |
|
1555 | - $replyTo = '[email protected]'; |
|
1556 | - $calendar = $this->createMock(ITestCalendar::class); |
|
1557 | - $calendarData = clone $this->vCalendar3a; |
|
1558 | - $calendarData->add('METHOD', 'CANCEL'); |
|
1559 | - |
|
1560 | - $this->time->expects(self::once()) |
|
1561 | - ->method('getTime') |
|
1562 | - ->willReturn(1628374233); |
|
1563 | - $manager->expects(self::once()) |
|
1564 | - ->method('getCalendarsForPrincipal') |
|
1565 | - ->with($principalUri) |
|
1566 | - ->willReturn([$calendar]); |
|
1567 | - $calendar->expects(self::once()) |
|
1568 | - ->method('search') |
|
1569 | - ->willReturn([['uri' => 'testname.ics']]); |
|
1570 | - $calendar->expects(self::once()) |
|
1571 | - ->method('handleIMipMessage') |
|
1572 | - ->with('testname.ics', $calendarData->serialize()); |
|
1573 | - // Act |
|
1574 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1575 | - // Assert |
|
1576 | - $this->assertTrue($result); |
|
1577 | - } |
|
1578 | - |
|
1579 | - public function testHandleImipCancel(): void { |
|
1580 | - /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ |
|
1581 | - $manager = $this->getMockBuilder(Manager::class) |
|
1582 | - ->setConstructorArgs([ |
|
1583 | - $this->coordinator, |
|
1584 | - $this->container, |
|
1585 | - $this->logger, |
|
1586 | - $this->time, |
|
1587 | - $this->secureRandom, |
|
1588 | - $this->userManager, |
|
1589 | - $this->serverFactory, |
|
1590 | - ]) |
|
1591 | - ->onlyMethods([ |
|
1592 | - 'getCalendarsForPrincipal' |
|
1593 | - ]) |
|
1594 | - ->getMock(); |
|
1595 | - $principalUri = 'principals/user/pierre'; |
|
1596 | - $sender = '[email protected]'; |
|
1597 | - $recipient = '[email protected]'; |
|
1598 | - $replyTo = null; |
|
1599 | - $calendar = $this->createMock(ITestCalendar::class); |
|
1600 | - $calendarData = clone $this->vCalendar3a; |
|
1601 | - $calendarData->add('METHOD', 'CANCEL'); |
|
1602 | - |
|
1603 | - $this->time->expects(self::once()) |
|
1604 | - ->method('getTime') |
|
1605 | - ->willReturn(1628374233); |
|
1606 | - $manager->expects(self::once()) |
|
1607 | - ->method('getCalendarsForPrincipal') |
|
1608 | - ->with($principalUri) |
|
1609 | - ->willReturn([$calendar]); |
|
1610 | - $calendar->expects(self::once()) |
|
1611 | - ->method('search') |
|
1612 | - ->willReturn([['uri' => 'testname.ics']]); |
|
1613 | - $calendar->expects(self::once()) |
|
1614 | - ->method('handleIMipMessage') |
|
1615 | - ->with('testname.ics', $calendarData->serialize()); |
|
1616 | - // Act |
|
1617 | - $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1618 | - // Assert |
|
1619 | - $this->assertTrue($result); |
|
1620 | - } |
|
1621 | - |
|
1622 | - private function getFreeBusyResponse(): string { |
|
1623 | - return <<<EOF |
|
39 | + /** @var Coordinator&MockObject */ |
|
40 | + private $coordinator; |
|
41 | + |
|
42 | + /** @var ContainerInterface&MockObject */ |
|
43 | + private $container; |
|
44 | + |
|
45 | + /** @var LoggerInterface&MockObject */ |
|
46 | + private $logger; |
|
47 | + |
|
48 | + /** @var Manager */ |
|
49 | + private $manager; |
|
50 | + |
|
51 | + /** @var ITimeFactory&MockObject */ |
|
52 | + private $time; |
|
53 | + |
|
54 | + /** @var ISecureRandom&MockObject */ |
|
55 | + private ISecureRandom $secureRandom; |
|
56 | + |
|
57 | + private IUserManager&MockObject $userManager; |
|
58 | + private ServerFactory&MockObject $serverFactory; |
|
59 | + |
|
60 | + private VCalendar $vCalendar1a; |
|
61 | + private VCalendar $vCalendar2a; |
|
62 | + private VCalendar $vCalendar3a; |
|
63 | + |
|
64 | + protected function setUp(): void { |
|
65 | + parent::setUp(); |
|
66 | + |
|
67 | + $this->coordinator = $this->createMock(Coordinator::class); |
|
68 | + $this->container = $this->createMock(ContainerInterface::class); |
|
69 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
70 | + $this->time = $this->createMock(ITimeFactory::class); |
|
71 | + $this->secureRandom = $this->createMock(ISecureRandom::class); |
|
72 | + $this->userManager = $this->createMock(IUserManager::class); |
|
73 | + $this->serverFactory = $this->createMock(ServerFactory::class); |
|
74 | + |
|
75 | + $this->manager = new Manager( |
|
76 | + $this->coordinator, |
|
77 | + $this->container, |
|
78 | + $this->logger, |
|
79 | + $this->time, |
|
80 | + $this->secureRandom, |
|
81 | + $this->userManager, |
|
82 | + $this->serverFactory, |
|
83 | + ); |
|
84 | + |
|
85 | + // construct calendar with a 1 hour event and same start/end time zones |
|
86 | + $this->vCalendar1a = new VCalendar(); |
|
87 | + /** @var VEvent $vEvent */ |
|
88 | + $vEvent = $this->vCalendar1a->add('VEVENT', []); |
|
89 | + $vEvent->UID->setValue('96a0e6b1-d886-4a55-a60d-152b31401dcc'); |
|
90 | + $vEvent->add('DTSTART', '20240701T080000', ['TZID' => 'America/Toronto']); |
|
91 | + $vEvent->add('DTEND', '20240701T090000', ['TZID' => 'America/Toronto']); |
|
92 | + $vEvent->add('SUMMARY', 'Test Event'); |
|
93 | + $vEvent->add('SEQUENCE', 3); |
|
94 | + $vEvent->add('STATUS', 'CONFIRMED'); |
|
95 | + $vEvent->add('TRANSP', 'OPAQUE'); |
|
96 | + $vEvent->add('ORGANIZER', 'mailto:[email protected]', ['CN' => 'Organizer']); |
|
97 | + $vEvent->add('ATTENDEE', 'mailto:[email protected]', [ |
|
98 | + 'CN' => 'Attendee One', |
|
99 | + 'CUTYPE' => 'INDIVIDUAL', |
|
100 | + 'PARTSTAT' => 'NEEDS-ACTION', |
|
101 | + 'ROLE' => 'REQ-PARTICIPANT', |
|
102 | + 'RSVP' => 'TRUE' |
|
103 | + ]); |
|
104 | + |
|
105 | + // construct calendar with a event for reply |
|
106 | + $this->vCalendar2a = new VCalendar(); |
|
107 | + /** @var VEvent $vEvent */ |
|
108 | + $vEvent = $this->vCalendar2a->add('VEVENT', []); |
|
109 | + $vEvent->UID->setValue('dcc733bf-b2b2-41f2-a8cf-550ae4b67aff'); |
|
110 | + $vEvent->add('DTSTART', '20210820'); |
|
111 | + $vEvent->add('DTEND', '20220821'); |
|
112 | + $vEvent->add('SUMMARY', 'berry basket'); |
|
113 | + $vEvent->add('SEQUENCE', 3); |
|
114 | + $vEvent->add('STATUS', 'CONFIRMED'); |
|
115 | + $vEvent->add('TRANSP', 'OPAQUE'); |
|
116 | + $vEvent->add('ORGANIZER', 'mailto:[email protected]', ['CN' => 'admin']); |
|
117 | + $vEvent->add('ATTENDEE', 'mailto:[email protected]', [ |
|
118 | + 'CN' => '[email protected]', |
|
119 | + 'CUTYPE' => 'INDIVIDUAL', |
|
120 | + 'ROLE' => 'REQ-PARTICIPANT', |
|
121 | + 'PARTSTAT' => 'ACCEPTED', |
|
122 | + ]); |
|
123 | + |
|
124 | + // construct calendar with a event for reply |
|
125 | + $this->vCalendar3a = new VCalendar(); |
|
126 | + /** @var VEvent $vEvent */ |
|
127 | + $vEvent = $this->vCalendar3a->add('VEVENT', []); |
|
128 | + $vEvent->UID->setValue('dcc733bf-b2b2-41f2-a8cf-550ae4b67aff'); |
|
129 | + $vEvent->add('DTSTART', '20210820'); |
|
130 | + $vEvent->add('DTEND', '20220821'); |
|
131 | + $vEvent->add('SUMMARY', 'berry basket'); |
|
132 | + $vEvent->add('SEQUENCE', 3); |
|
133 | + $vEvent->add('STATUS', 'CANCELLED'); |
|
134 | + $vEvent->add('TRANSP', 'OPAQUE'); |
|
135 | + $vEvent->add('ORGANIZER', 'mailto:[email protected]', ['CN' => 'admin']); |
|
136 | + $vEvent->add('ATTENDEE', 'mailto:[email protected]', [ |
|
137 | + 'CN' => '[email protected]', |
|
138 | + 'CUTYPE' => 'INDIVIDUAL', |
|
139 | + 'ROLE' => 'REQ-PARTICIPANT', |
|
140 | + 'PARTSTAT' => 'ACCEPTED', |
|
141 | + ]); |
|
142 | + |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @dataProvider searchProvider |
|
147 | + */ |
|
148 | + public function testSearch($search1, $search2, $expected): void { |
|
149 | + /** @var ICalendar | MockObject $calendar1 */ |
|
150 | + $calendar1 = $this->createMock(ICalendar::class); |
|
151 | + $calendar1->method('getKey')->willReturn('simple:1'); |
|
152 | + $calendar1->expects($this->once()) |
|
153 | + ->method('search') |
|
154 | + ->with('', [], [], null, null) |
|
155 | + ->willReturn($search1); |
|
156 | + |
|
157 | + /** @var ICalendar | MockObject $calendar2 */ |
|
158 | + $calendar2 = $this->createMock(ICalendar::class); |
|
159 | + $calendar2->method('getKey')->willReturn('simple:2'); |
|
160 | + $calendar2->expects($this->once()) |
|
161 | + ->method('search') |
|
162 | + ->with('', [], [], null, null) |
|
163 | + ->willReturn($search2); |
|
164 | + |
|
165 | + $this->manager->registerCalendar($calendar1); |
|
166 | + $this->manager->registerCalendar($calendar2); |
|
167 | + |
|
168 | + $result = $this->manager->search(''); |
|
169 | + $this->assertEquals($expected, $result); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @dataProvider searchProvider |
|
174 | + */ |
|
175 | + public function testSearchOptions($search1, $search2, $expected): void { |
|
176 | + /** @var ICalendar | MockObject $calendar1 */ |
|
177 | + $calendar1 = $this->createMock(ICalendar::class); |
|
178 | + $calendar1->method('getKey')->willReturn('simple:1'); |
|
179 | + $calendar1->expects($this->once()) |
|
180 | + ->method('search') |
|
181 | + ->with('searchTerm', ['SUMMARY', 'DESCRIPTION'], |
|
182 | + ['timerange' => ['start' => null, 'end' => null]], 5, 20) |
|
183 | + ->willReturn($search1); |
|
184 | + |
|
185 | + /** @var ICalendar | MockObject $calendar2 */ |
|
186 | + $calendar2 = $this->createMock(ICalendar::class); |
|
187 | + $calendar2->method('getKey')->willReturn('simple:2'); |
|
188 | + $calendar2->expects($this->once()) |
|
189 | + ->method('search') |
|
190 | + ->with('searchTerm', ['SUMMARY', 'DESCRIPTION'], |
|
191 | + ['timerange' => ['start' => null, 'end' => null]], 5, 20) |
|
192 | + ->willReturn($search2); |
|
193 | + |
|
194 | + $this->manager->registerCalendar($calendar1); |
|
195 | + $this->manager->registerCalendar($calendar2); |
|
196 | + |
|
197 | + $result = $this->manager->search('searchTerm', ['SUMMARY', 'DESCRIPTION'], |
|
198 | + ['timerange' => ['start' => null, 'end' => null]], 5, 20); |
|
199 | + $this->assertEquals($expected, $result); |
|
200 | + } |
|
201 | + |
|
202 | + public static function searchProvider(): array { |
|
203 | + $search1 = [ |
|
204 | + [ |
|
205 | + 'id' => 1, |
|
206 | + 'data' => 'foobar', |
|
207 | + ], |
|
208 | + [ |
|
209 | + 'id' => 2, |
|
210 | + 'data' => 'barfoo', |
|
211 | + ] |
|
212 | + ]; |
|
213 | + $search2 = [ |
|
214 | + [ |
|
215 | + 'id' => 3, |
|
216 | + 'data' => 'blablub', |
|
217 | + ], |
|
218 | + [ |
|
219 | + 'id' => 4, |
|
220 | + 'data' => 'blubbla', |
|
221 | + ] |
|
222 | + ]; |
|
223 | + |
|
224 | + $expected = [ |
|
225 | + [ |
|
226 | + 'id' => 1, |
|
227 | + 'data' => 'foobar', |
|
228 | + 'calendar-key' => 'simple:1', |
|
229 | + ], |
|
230 | + [ |
|
231 | + 'id' => 2, |
|
232 | + 'data' => 'barfoo', |
|
233 | + 'calendar-key' => 'simple:1', |
|
234 | + ], |
|
235 | + [ |
|
236 | + 'id' => 3, |
|
237 | + 'data' => 'blablub', |
|
238 | + 'calendar-key' => 'simple:2', |
|
239 | + ], |
|
240 | + [ |
|
241 | + 'id' => 4, |
|
242 | + 'data' => 'blubbla', |
|
243 | + 'calendar-key' => 'simple:2', |
|
244 | + ] |
|
245 | + ]; |
|
246 | + |
|
247 | + return [ |
|
248 | + [ |
|
249 | + $search1, |
|
250 | + $search2, |
|
251 | + $expected |
|
252 | + ] |
|
253 | + ]; |
|
254 | + } |
|
255 | + |
|
256 | + public function testRegisterUnregister(): void { |
|
257 | + /** @var ICalendar | MockObject $calendar1 */ |
|
258 | + $calendar1 = $this->createMock(ICalendar::class); |
|
259 | + $calendar1->method('getKey')->willReturn('key1'); |
|
260 | + |
|
261 | + /** @var ICalendar | MockObject $calendar2 */ |
|
262 | + $calendar2 = $this->createMock(ICalendar::class); |
|
263 | + $calendar2->method('getKey')->willReturn('key2'); |
|
264 | + |
|
265 | + $this->manager->registerCalendar($calendar1); |
|
266 | + $this->manager->registerCalendar($calendar2); |
|
267 | + |
|
268 | + $result = $this->manager->getCalendars(); |
|
269 | + $this->assertCount(2, $result); |
|
270 | + $this->assertContains($calendar1, $result); |
|
271 | + $this->assertContains($calendar2, $result); |
|
272 | + |
|
273 | + $this->manager->unregisterCalendar($calendar1); |
|
274 | + |
|
275 | + $result = $this->manager->getCalendars(); |
|
276 | + $this->assertCount(1, $result); |
|
277 | + $this->assertContains($calendar2, $result); |
|
278 | + } |
|
279 | + |
|
280 | + public function testGetCalendars(): void { |
|
281 | + /** @var ICalendar | MockObject $calendar1 */ |
|
282 | + $calendar1 = $this->createMock(ICalendar::class); |
|
283 | + $calendar1->method('getKey')->willReturn('key1'); |
|
284 | + |
|
285 | + /** @var ICalendar | MockObject $calendar2 */ |
|
286 | + $calendar2 = $this->createMock(ICalendar::class); |
|
287 | + $calendar2->method('getKey')->willReturn('key2'); |
|
288 | + |
|
289 | + $this->manager->registerCalendar($calendar1); |
|
290 | + $this->manager->registerCalendar($calendar2); |
|
291 | + |
|
292 | + $result = $this->manager->getCalendars(); |
|
293 | + $this->assertCount(2, $result); |
|
294 | + $this->assertContains($calendar1, $result); |
|
295 | + $this->assertContains($calendar2, $result); |
|
296 | + |
|
297 | + $this->manager->clear(); |
|
298 | + |
|
299 | + $result = $this->manager->getCalendars(); |
|
300 | + |
|
301 | + $this->assertCount(0, $result); |
|
302 | + } |
|
303 | + |
|
304 | + public function testEnabledIfNot(): void { |
|
305 | + $isEnabled = $this->manager->isEnabled(); |
|
306 | + $this->assertFalse($isEnabled); |
|
307 | + } |
|
308 | + |
|
309 | + public function testIfEnabledIfSo(): void { |
|
310 | + /** @var ICalendar | MockObject $calendar */ |
|
311 | + $calendar = $this->createMock(ICalendar::class); |
|
312 | + $this->manager->registerCalendar($calendar); |
|
313 | + |
|
314 | + $isEnabled = $this->manager->isEnabled(); |
|
315 | + $this->assertTrue($isEnabled); |
|
316 | + } |
|
317 | + |
|
318 | + public function testHandleImipRequestWithNoCalendars(): void { |
|
319 | + // construct calendar manager returns |
|
320 | + /** @var Manager&MockObject $manager */ |
|
321 | + $manager = $this->getMockBuilder(Manager::class) |
|
322 | + ->setConstructorArgs([ |
|
323 | + $this->coordinator, |
|
324 | + $this->container, |
|
325 | + $this->logger, |
|
326 | + $this->time, |
|
327 | + $this->secureRandom, |
|
328 | + $this->userManager, |
|
329 | + $this->serverFactory, |
|
330 | + ]) |
|
331 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
332 | + ->getMock(); |
|
333 | + $manager->expects(self::once()) |
|
334 | + ->method('getCalendarsForPrincipal') |
|
335 | + ->willReturn([]); |
|
336 | + // construct logger returns |
|
337 | + $this->logger->expects(self::once())->method('warning') |
|
338 | + ->with('iMip message could not be processed because user has no calendars'); |
|
339 | + // construct parameters |
|
340 | + $principalUri = 'principals/user/attendee1'; |
|
341 | + $sender = '[email protected]'; |
|
342 | + $recipient = '[email protected]'; |
|
343 | + $calendar = $this->vCalendar1a; |
|
344 | + $calendar->add('METHOD', 'REQUEST'); |
|
345 | + // Act |
|
346 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
347 | + // Assert |
|
348 | + $this->assertFalse($result); |
|
349 | + } |
|
350 | + |
|
351 | + public function testHandleImipRequestWithInvalidData(): void { |
|
352 | + // construct mock user calendar |
|
353 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
354 | + // construct mock calendar manager and returns |
|
355 | + /** @var Manager&MockObject $manager */ |
|
356 | + $manager = $this->getMockBuilder(Manager::class) |
|
357 | + ->setConstructorArgs([ |
|
358 | + $this->coordinator, |
|
359 | + $this->container, |
|
360 | + $this->logger, |
|
361 | + $this->time, |
|
362 | + $this->secureRandom, |
|
363 | + $this->userManager, |
|
364 | + $this->serverFactory, |
|
365 | + ]) |
|
366 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
367 | + ->getMock(); |
|
368 | + $manager->expects(self::once()) |
|
369 | + ->method('getCalendarsForPrincipal') |
|
370 | + ->willReturn([$userCalendar]); |
|
371 | + // construct logger returns |
|
372 | + $this->logger->expects(self::once())->method('error') |
|
373 | + ->with('iMip message could not be processed because an error occurred while parsing the iMip message'); |
|
374 | + // construct parameters |
|
375 | + $principalUri = 'principals/user/attendee1'; |
|
376 | + $sender = '[email protected]'; |
|
377 | + $recipient = '[email protected]'; |
|
378 | + // Act |
|
379 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, 'Invalid data'); |
|
380 | + // Assert |
|
381 | + $this->assertFalse($result); |
|
382 | + } |
|
383 | + |
|
384 | + public function testHandleImipRequestWithNoMethod(): void { |
|
385 | + // construct mock user calendar |
|
386 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
387 | + // construct mock calendar manager and returns |
|
388 | + /** @var Manager&MockObject $manager */ |
|
389 | + $manager = $this->getMockBuilder(Manager::class) |
|
390 | + ->setConstructorArgs([ |
|
391 | + $this->coordinator, |
|
392 | + $this->container, |
|
393 | + $this->logger, |
|
394 | + $this->time, |
|
395 | + $this->secureRandom, |
|
396 | + $this->userManager, |
|
397 | + $this->serverFactory, |
|
398 | + ]) |
|
399 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
400 | + ->getMock(); |
|
401 | + $manager->expects(self::once()) |
|
402 | + ->method('getCalendarsForPrincipal') |
|
403 | + ->willReturn([$userCalendar]); |
|
404 | + // construct logger returns |
|
405 | + $this->logger->expects(self::once())->method('warning') |
|
406 | + ->with('iMip message contains an incorrect or invalid method'); |
|
407 | + // construct parameters |
|
408 | + $principalUri = 'principals/user/attendee1'; |
|
409 | + $sender = '[email protected]'; |
|
410 | + $recipient = '[email protected]'; |
|
411 | + $calendar = $this->vCalendar1a; |
|
412 | + // Act |
|
413 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
414 | + // Assert |
|
415 | + $this->assertFalse($result); |
|
416 | + } |
|
417 | + |
|
418 | + public function testHandleImipRequestWithInvalidMethod(): void { |
|
419 | + // construct mock user calendar |
|
420 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
421 | + // construct mock calendar manager and returns |
|
422 | + /** @var Manager&MockObject $manager */ |
|
423 | + $manager = $this->getMockBuilder(Manager::class) |
|
424 | + ->setConstructorArgs([ |
|
425 | + $this->coordinator, |
|
426 | + $this->container, |
|
427 | + $this->logger, |
|
428 | + $this->time, |
|
429 | + $this->secureRandom, |
|
430 | + $this->userManager, |
|
431 | + $this->serverFactory, |
|
432 | + ]) |
|
433 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
434 | + ->getMock(); |
|
435 | + $manager->expects(self::once()) |
|
436 | + ->method('getCalendarsForPrincipal') |
|
437 | + ->willReturn([$userCalendar]); |
|
438 | + // construct logger returns |
|
439 | + $this->logger->expects(self::once())->method('warning') |
|
440 | + ->with('iMip message contains an incorrect or invalid method'); |
|
441 | + // construct parameters |
|
442 | + $principalUri = 'principals/user/attendee1'; |
|
443 | + $sender = '[email protected]'; |
|
444 | + $recipient = '[email protected]'; |
|
445 | + $calendar = $this->vCalendar1a; |
|
446 | + $calendar->add('METHOD', 'CANCEL'); |
|
447 | + // Act |
|
448 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
449 | + // Assert |
|
450 | + $this->assertFalse($result); |
|
451 | + } |
|
452 | + |
|
453 | + public function testHandleImipRequestWithNoEvent(): void { |
|
454 | + // construct mock user calendar |
|
455 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
456 | + // construct mock calendar manager and returns |
|
457 | + /** @var Manager&MockObject $manager */ |
|
458 | + $manager = $this->getMockBuilder(Manager::class) |
|
459 | + ->setConstructorArgs([ |
|
460 | + $this->coordinator, |
|
461 | + $this->container, |
|
462 | + $this->logger, |
|
463 | + $this->time, |
|
464 | + $this->secureRandom, |
|
465 | + $this->userManager, |
|
466 | + $this->serverFactory, |
|
467 | + ]) |
|
468 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
469 | + ->getMock(); |
|
470 | + $manager->expects(self::once()) |
|
471 | + ->method('getCalendarsForPrincipal') |
|
472 | + ->willReturn([$userCalendar]); |
|
473 | + // construct logger returns |
|
474 | + $this->logger->expects(self::once())->method('warning') |
|
475 | + ->with('iMip message contains no event'); |
|
476 | + // construct parameters |
|
477 | + $principalUri = 'principals/user/attendee1'; |
|
478 | + $sender = '[email protected]'; |
|
479 | + $recipient = '[email protected]'; |
|
480 | + $calendar = $this->vCalendar1a; |
|
481 | + $calendar->add('METHOD', 'REQUEST'); |
|
482 | + $calendar->remove('VEVENT'); |
|
483 | + // Act |
|
484 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
485 | + // Assert |
|
486 | + $this->assertFalse($result); |
|
487 | + } |
|
488 | + |
|
489 | + public function testHandleImipRequestWithNoUid(): void { |
|
490 | + // construct mock user calendar |
|
491 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
492 | + // construct mock calendar manager and returns |
|
493 | + /** @var Manager&MockObject $manager */ |
|
494 | + $manager = $this->getMockBuilder(Manager::class) |
|
495 | + ->setConstructorArgs([ |
|
496 | + $this->coordinator, |
|
497 | + $this->container, |
|
498 | + $this->logger, |
|
499 | + $this->time, |
|
500 | + $this->secureRandom, |
|
501 | + $this->userManager, |
|
502 | + $this->serverFactory, |
|
503 | + ]) |
|
504 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
505 | + ->getMock(); |
|
506 | + $manager->expects(self::once()) |
|
507 | + ->method('getCalendarsForPrincipal') |
|
508 | + ->willReturn([$userCalendar]); |
|
509 | + // construct logger returns |
|
510 | + $this->logger->expects(self::once())->method('warning') |
|
511 | + ->with('iMip message event dose not contains a UID'); |
|
512 | + // construct parameters |
|
513 | + $principalUri = 'principals/user/attendee1'; |
|
514 | + $sender = '[email protected]'; |
|
515 | + $recipient = '[email protected]'; |
|
516 | + $calendar = $this->vCalendar1a; |
|
517 | + $calendar->add('METHOD', 'REQUEST'); |
|
518 | + $calendar->VEVENT->remove('UID'); |
|
519 | + // Act |
|
520 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
521 | + // Assert |
|
522 | + $this->assertFalse($result); |
|
523 | + } |
|
524 | + |
|
525 | + public function testHandleImipRequestWithNoOrganizer(): void { |
|
526 | + // construct mock user calendar |
|
527 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
528 | + // construct mock calendar manager and returns |
|
529 | + /** @var Manager&MockObject $manager */ |
|
530 | + $manager = $this->getMockBuilder(Manager::class) |
|
531 | + ->setConstructorArgs([ |
|
532 | + $this->coordinator, |
|
533 | + $this->container, |
|
534 | + $this->logger, |
|
535 | + $this->time, |
|
536 | + $this->secureRandom, |
|
537 | + $this->userManager, |
|
538 | + $this->serverFactory, |
|
539 | + ]) |
|
540 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
541 | + ->getMock(); |
|
542 | + $manager->expects(self::once()) |
|
543 | + ->method('getCalendarsForPrincipal') |
|
544 | + ->willReturn([$userCalendar]); |
|
545 | + // construct logger returns |
|
546 | + $this->logger->expects(self::once())->method('warning') |
|
547 | + ->with('iMip message event dose not contains an organizer'); |
|
548 | + // construct parameters |
|
549 | + $principalUri = 'principals/user/attendee1'; |
|
550 | + $sender = '[email protected]'; |
|
551 | + $recipient = '[email protected]'; |
|
552 | + $calendar = $this->vCalendar1a; |
|
553 | + $calendar->add('METHOD', 'REQUEST'); |
|
554 | + $calendar->VEVENT->remove('ORGANIZER'); |
|
555 | + // Act |
|
556 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
557 | + // Assert |
|
558 | + $this->assertFalse($result); |
|
559 | + } |
|
560 | + |
|
561 | + public function testHandleImipRequestWithNoAttendee(): void { |
|
562 | + // construct mock user calendar |
|
563 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
564 | + // construct mock calendar manager and returns |
|
565 | + /** @var Manager&MockObject $manager */ |
|
566 | + $manager = $this->getMockBuilder(Manager::class) |
|
567 | + ->setConstructorArgs([ |
|
568 | + $this->coordinator, |
|
569 | + $this->container, |
|
570 | + $this->logger, |
|
571 | + $this->time, |
|
572 | + $this->secureRandom, |
|
573 | + $this->userManager, |
|
574 | + $this->serverFactory, |
|
575 | + ]) |
|
576 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
577 | + ->getMock(); |
|
578 | + $manager->expects(self::once()) |
|
579 | + ->method('getCalendarsForPrincipal') |
|
580 | + ->willReturn([$userCalendar]); |
|
581 | + // construct logger returns |
|
582 | + $this->logger->expects(self::once())->method('warning') |
|
583 | + ->with('iMip message event dose not contains any attendees'); |
|
584 | + // construct parameters |
|
585 | + $principalUri = 'principals/user/attendee1'; |
|
586 | + $sender = '[email protected]'; |
|
587 | + $recipient = '[email protected]'; |
|
588 | + $calendar = $this->vCalendar1a; |
|
589 | + $calendar->add('METHOD', 'REQUEST'); |
|
590 | + $calendar->VEVENT->remove('ATTENDEE'); |
|
591 | + // Act |
|
592 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
593 | + // Assert |
|
594 | + $this->assertFalse($result); |
|
595 | + } |
|
596 | + |
|
597 | + public function testHandleImipRequestWithInvalidAttendee(): void { |
|
598 | + // construct mock user calendar |
|
599 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
600 | + // construct mock calendar manager and returns |
|
601 | + /** @var Manager&MockObject $manager */ |
|
602 | + $manager = $this->getMockBuilder(Manager::class) |
|
603 | + ->setConstructorArgs([ |
|
604 | + $this->coordinator, |
|
605 | + $this->container, |
|
606 | + $this->logger, |
|
607 | + $this->time, |
|
608 | + $this->secureRandom, |
|
609 | + $this->userManager, |
|
610 | + $this->serverFactory, |
|
611 | + ]) |
|
612 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
613 | + ->getMock(); |
|
614 | + $manager->expects(self::once()) |
|
615 | + ->method('getCalendarsForPrincipal') |
|
616 | + ->willReturn([$userCalendar]); |
|
617 | + // construct logger returns |
|
618 | + $this->logger->expects(self::once())->method('warning') |
|
619 | + ->with('iMip message event does not contain a attendee that matches the recipient'); |
|
620 | + // construct parameters |
|
621 | + $principalUri = 'principals/user/attendee1'; |
|
622 | + $sender = '[email protected]'; |
|
623 | + $recipient = '[email protected]'; |
|
624 | + $calendar = $this->vCalendar1a; |
|
625 | + $calendar->add('METHOD', 'REQUEST'); |
|
626 | + // Act |
|
627 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
628 | + // Assert |
|
629 | + $this->assertFalse($result); |
|
630 | + } |
|
631 | + |
|
632 | + public function testHandleImipRequestWithNoMatch(): void { |
|
633 | + // construct mock user calendar |
|
634 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
635 | + $userCalendar->expects(self::once()) |
|
636 | + ->method('isDeleted') |
|
637 | + ->willReturn(false); |
|
638 | + $userCalendar->expects(self::once()) |
|
639 | + ->method('isWritable') |
|
640 | + ->willReturn(true); |
|
641 | + $userCalendar->expects(self::once()) |
|
642 | + ->method('isShared') |
|
643 | + ->willReturn(false); |
|
644 | + $userCalendar->expects(self::once()) |
|
645 | + ->method('search') |
|
646 | + ->willReturn([]); |
|
647 | + // construct mock calendar manager and returns |
|
648 | + /** @var Manager&MockObject $manager */ |
|
649 | + $manager = $this->getMockBuilder(Manager::class) |
|
650 | + ->setConstructorArgs([ |
|
651 | + $this->coordinator, |
|
652 | + $this->container, |
|
653 | + $this->logger, |
|
654 | + $this->time, |
|
655 | + $this->secureRandom, |
|
656 | + $this->userManager, |
|
657 | + $this->serverFactory, |
|
658 | + ]) |
|
659 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
660 | + ->getMock(); |
|
661 | + $manager->expects(self::once()) |
|
662 | + ->method('getCalendarsForPrincipal') |
|
663 | + ->willReturn([$userCalendar]); |
|
664 | + // construct logger returns |
|
665 | + $this->logger->expects(self::once())->method('warning') |
|
666 | + ->with('iMip message event could not be processed because no corresponding event was found in any calendar'); |
|
667 | + // construct parameters |
|
668 | + $principalUri = 'principals/user/attendee1'; |
|
669 | + $sender = '[email protected]'; |
|
670 | + $recipient = '[email protected]'; |
|
671 | + $calendar = $this->vCalendar1a; |
|
672 | + $calendar->add('METHOD', 'REQUEST'); |
|
673 | + // Act |
|
674 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
675 | + // Assert |
|
676 | + $this->assertFalse($result); |
|
677 | + } |
|
678 | + |
|
679 | + public function testHandleImipRequest(): void { |
|
680 | + // construct mock user calendar |
|
681 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
682 | + $userCalendar->expects(self::once()) |
|
683 | + ->method('isDeleted') |
|
684 | + ->willReturn(false); |
|
685 | + $userCalendar->expects(self::once()) |
|
686 | + ->method('isWritable') |
|
687 | + ->willReturn(true); |
|
688 | + $userCalendar->expects(self::once()) |
|
689 | + ->method('isShared') |
|
690 | + ->willReturn(false); |
|
691 | + $userCalendar->expects(self::once()) |
|
692 | + ->method('search') |
|
693 | + ->willReturn([['uri' => 'principals/user/attendee1/personal']]); |
|
694 | + // construct mock calendar manager and returns |
|
695 | + /** @var Manager&MockObject $manager */ |
|
696 | + $manager = $this->getMockBuilder(Manager::class) |
|
697 | + ->setConstructorArgs([ |
|
698 | + $this->coordinator, |
|
699 | + $this->container, |
|
700 | + $this->logger, |
|
701 | + $this->time, |
|
702 | + $this->secureRandom, |
|
703 | + $this->userManager, |
|
704 | + $this->serverFactory, |
|
705 | + ]) |
|
706 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
707 | + ->getMock(); |
|
708 | + $manager->expects(self::once()) |
|
709 | + ->method('getCalendarsForPrincipal') |
|
710 | + ->willReturn([$userCalendar]); |
|
711 | + // construct parameters |
|
712 | + $principalUri = 'principals/user/attendee1'; |
|
713 | + $sender = '[email protected]'; |
|
714 | + $recipient = '[email protected]'; |
|
715 | + $calendar = $this->vCalendar1a; |
|
716 | + $calendar->add('METHOD', 'REQUEST'); |
|
717 | + // construct user calendar returns |
|
718 | + $userCalendar->expects(self::once()) |
|
719 | + ->method('handleIMipMessage') |
|
720 | + ->with('', $calendar->serialize()); |
|
721 | + // Act |
|
722 | + $result = $manager->handleIMipRequest($principalUri, $sender, $recipient, $calendar->serialize()); |
|
723 | + // Assert |
|
724 | + $this->assertTrue($result); |
|
725 | + } |
|
726 | + |
|
727 | + public function testHandleImipReplyWithNoCalendars(): void { |
|
728 | + // construct calendar manager returns |
|
729 | + /** @var Manager&MockObject $manager */ |
|
730 | + $manager = $this->getMockBuilder(Manager::class) |
|
731 | + ->setConstructorArgs([ |
|
732 | + $this->coordinator, |
|
733 | + $this->container, |
|
734 | + $this->logger, |
|
735 | + $this->time, |
|
736 | + $this->secureRandom, |
|
737 | + $this->userManager, |
|
738 | + $this->serverFactory, |
|
739 | + ]) |
|
740 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
741 | + ->getMock(); |
|
742 | + $manager->expects(self::once()) |
|
743 | + ->method('getCalendarsForPrincipal') |
|
744 | + ->willReturn([]); |
|
745 | + // construct logger returns |
|
746 | + $this->logger->expects(self::once())->method('warning') |
|
747 | + ->with('iMip message could not be processed because user has no calendars'); |
|
748 | + // construct parameters |
|
749 | + $principalUri = 'principals/user/linus'; |
|
750 | + $sender = '[email protected]'; |
|
751 | + $recipient = '[email protected]'; |
|
752 | + $calendar = $this->vCalendar2a; |
|
753 | + $calendar->add('METHOD', 'REPLY'); |
|
754 | + // Act |
|
755 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
756 | + // Assert |
|
757 | + $this->assertFalse($result); |
|
758 | + } |
|
759 | + |
|
760 | + public function testHandleImipReplyWithInvalidData(): void { |
|
761 | + // construct mock user calendar |
|
762 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
763 | + // construct mock calendar manager and returns |
|
764 | + /** @var Manager&MockObject $manager */ |
|
765 | + $manager = $this->getMockBuilder(Manager::class) |
|
766 | + ->setConstructorArgs([ |
|
767 | + $this->coordinator, |
|
768 | + $this->container, |
|
769 | + $this->logger, |
|
770 | + $this->time, |
|
771 | + $this->secureRandom, |
|
772 | + $this->userManager, |
|
773 | + $this->serverFactory, |
|
774 | + ]) |
|
775 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
776 | + ->getMock(); |
|
777 | + $manager->expects(self::once()) |
|
778 | + ->method('getCalendarsForPrincipal') |
|
779 | + ->willReturn([$userCalendar]); |
|
780 | + // construct logger returns |
|
781 | + $this->logger->expects(self::once())->method('error') |
|
782 | + ->with('iMip message could not be processed because an error occurred while parsing the iMip message'); |
|
783 | + // construct parameters |
|
784 | + $principalUri = 'principals/user/attendee1'; |
|
785 | + $sender = '[email protected]'; |
|
786 | + $recipient = '[email protected]'; |
|
787 | + // Act |
|
788 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, 'Invalid data'); |
|
789 | + // Assert |
|
790 | + $this->assertFalse($result); |
|
791 | + } |
|
792 | + |
|
793 | + public function testHandleImipReplyWithNoMethod(): void { |
|
794 | + // construct mock user calendar |
|
795 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
796 | + // construct mock calendar manager and returns |
|
797 | + /** @var Manager&MockObject $manager */ |
|
798 | + $manager = $this->getMockBuilder(Manager::class) |
|
799 | + ->setConstructorArgs([ |
|
800 | + $this->coordinator, |
|
801 | + $this->container, |
|
802 | + $this->logger, |
|
803 | + $this->time, |
|
804 | + $this->secureRandom, |
|
805 | + $this->userManager, |
|
806 | + $this->serverFactory, |
|
807 | + ]) |
|
808 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
809 | + ->getMock(); |
|
810 | + $manager->expects(self::once()) |
|
811 | + ->method('getCalendarsForPrincipal') |
|
812 | + ->willReturn([$userCalendar]); |
|
813 | + // construct logger returns |
|
814 | + $this->logger->expects(self::once())->method('warning') |
|
815 | + ->with('iMip message contains an incorrect or invalid method'); |
|
816 | + // construct parameters |
|
817 | + $principalUri = 'principals/user/linus'; |
|
818 | + $sender = '[email protected]'; |
|
819 | + $recipient = '[email protected]'; |
|
820 | + $calendar = $this->vCalendar2a; |
|
821 | + // Act |
|
822 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
823 | + // Assert |
|
824 | + $this->assertFalse($result); |
|
825 | + } |
|
826 | + |
|
827 | + public function testHandleImipReplyWithInvalidMethod(): void { |
|
828 | + // construct mock user calendar |
|
829 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
830 | + // construct mock calendar manager and returns |
|
831 | + /** @var Manager&MockObject $manager */ |
|
832 | + $manager = $this->getMockBuilder(Manager::class) |
|
833 | + ->setConstructorArgs([ |
|
834 | + $this->coordinator, |
|
835 | + $this->container, |
|
836 | + $this->logger, |
|
837 | + $this->time, |
|
838 | + $this->secureRandom, |
|
839 | + $this->userManager, |
|
840 | + $this->serverFactory, |
|
841 | + ]) |
|
842 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
843 | + ->getMock(); |
|
844 | + $manager->expects(self::once()) |
|
845 | + ->method('getCalendarsForPrincipal') |
|
846 | + ->willReturn([$userCalendar]); |
|
847 | + // construct logger returns |
|
848 | + $this->logger->expects(self::once())->method('warning') |
|
849 | + ->with('iMip message contains an incorrect or invalid method'); |
|
850 | + // construct parameters |
|
851 | + $principalUri = 'principals/user/linus'; |
|
852 | + $sender = '[email protected]'; |
|
853 | + $recipient = '[email protected]'; |
|
854 | + $calendar = $this->vCalendar2a; |
|
855 | + $calendar->add('METHOD', 'UNKNOWN'); |
|
856 | + // Act |
|
857 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
858 | + // Assert |
|
859 | + $this->assertFalse($result); |
|
860 | + } |
|
861 | + |
|
862 | + public function testHandleImipReplyWithNoEvent(): void { |
|
863 | + // construct mock user calendar |
|
864 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
865 | + // construct mock calendar manager and returns |
|
866 | + /** @var Manager&MockObject $manager */ |
|
867 | + $manager = $this->getMockBuilder(Manager::class) |
|
868 | + ->setConstructorArgs([ |
|
869 | + $this->coordinator, |
|
870 | + $this->container, |
|
871 | + $this->logger, |
|
872 | + $this->time, |
|
873 | + $this->secureRandom, |
|
874 | + $this->userManager, |
|
875 | + $this->serverFactory, |
|
876 | + ]) |
|
877 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
878 | + ->getMock(); |
|
879 | + $manager->expects(self::once()) |
|
880 | + ->method('getCalendarsForPrincipal') |
|
881 | + ->willReturn([$userCalendar]); |
|
882 | + // construct logger returns |
|
883 | + $this->logger->expects(self::once())->method('warning') |
|
884 | + ->with('iMip message contains no event'); |
|
885 | + // construct parameters |
|
886 | + $principalUri = 'principals/user/linus'; |
|
887 | + $sender = '[email protected]'; |
|
888 | + $recipient = '[email protected]'; |
|
889 | + $calendar = $this->vCalendar2a; |
|
890 | + $calendar->add('METHOD', 'REPLY'); |
|
891 | + $calendar->remove('VEVENT'); |
|
892 | + // Act |
|
893 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
894 | + // Assert |
|
895 | + $this->assertFalse($result); |
|
896 | + } |
|
897 | + |
|
898 | + public function testHandleImipReplyWithNoUid(): void { |
|
899 | + // construct mock user calendar |
|
900 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
901 | + // construct mock calendar manager and returns |
|
902 | + /** @var Manager&MockObject $manager */ |
|
903 | + $manager = $this->getMockBuilder(Manager::class) |
|
904 | + ->setConstructorArgs([ |
|
905 | + $this->coordinator, |
|
906 | + $this->container, |
|
907 | + $this->logger, |
|
908 | + $this->time, |
|
909 | + $this->secureRandom, |
|
910 | + $this->userManager, |
|
911 | + $this->serverFactory, |
|
912 | + ]) |
|
913 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
914 | + ->getMock(); |
|
915 | + $manager->expects(self::once()) |
|
916 | + ->method('getCalendarsForPrincipal') |
|
917 | + ->willReturn([$userCalendar]); |
|
918 | + // construct logger returns |
|
919 | + $this->logger->expects(self::once())->method('warning') |
|
920 | + ->with('iMip message event dose not contains a UID'); |
|
921 | + // construct parameters |
|
922 | + $principalUri = 'principals/user/linus'; |
|
923 | + $sender = '[email protected]'; |
|
924 | + $recipient = '[email protected]'; |
|
925 | + $calendar = $this->vCalendar2a; |
|
926 | + $calendar->add('METHOD', 'REPLY'); |
|
927 | + $calendar->VEVENT->remove('UID'); |
|
928 | + // Act |
|
929 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
930 | + // Assert |
|
931 | + $this->assertFalse($result); |
|
932 | + } |
|
933 | + |
|
934 | + public function testHandleImipReplyWithNoOrganizer(): void { |
|
935 | + // construct mock user calendar |
|
936 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
937 | + // construct mock calendar manager and returns |
|
938 | + /** @var Manager&MockObject $manager */ |
|
939 | + $manager = $this->getMockBuilder(Manager::class) |
|
940 | + ->setConstructorArgs([ |
|
941 | + $this->coordinator, |
|
942 | + $this->container, |
|
943 | + $this->logger, |
|
944 | + $this->time, |
|
945 | + $this->secureRandom, |
|
946 | + $this->userManager, |
|
947 | + $this->serverFactory, |
|
948 | + ]) |
|
949 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
950 | + ->getMock(); |
|
951 | + $manager->expects(self::once()) |
|
952 | + ->method('getCalendarsForPrincipal') |
|
953 | + ->willReturn([$userCalendar]); |
|
954 | + // construct logger returns |
|
955 | + $this->logger->expects(self::once())->method('warning') |
|
956 | + ->with('iMip message event dose not contains an organizer'); |
|
957 | + // construct parameters |
|
958 | + $principalUri = 'principals/user/linus'; |
|
959 | + $sender = '[email protected]'; |
|
960 | + $recipient = '[email protected]'; |
|
961 | + $calendar = $this->vCalendar2a; |
|
962 | + $calendar->add('METHOD', 'REPLY'); |
|
963 | + $calendar->VEVENT->remove('ORGANIZER'); |
|
964 | + // Act |
|
965 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
966 | + // Assert |
|
967 | + $this->assertFalse($result); |
|
968 | + } |
|
969 | + |
|
970 | + public function testHandleImipReplyWithNoAttendee(): void { |
|
971 | + // construct mock user calendar |
|
972 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
973 | + // construct mock calendar manager and returns |
|
974 | + /** @var Manager&MockObject $manager */ |
|
975 | + $manager = $this->getMockBuilder(Manager::class) |
|
976 | + ->setConstructorArgs([ |
|
977 | + $this->coordinator, |
|
978 | + $this->container, |
|
979 | + $this->logger, |
|
980 | + $this->time, |
|
981 | + $this->secureRandom, |
|
982 | + $this->userManager, |
|
983 | + $this->serverFactory, |
|
984 | + ]) |
|
985 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
986 | + ->getMock(); |
|
987 | + $manager->expects(self::once()) |
|
988 | + ->method('getCalendarsForPrincipal') |
|
989 | + ->willReturn([$userCalendar]); |
|
990 | + // construct logger returns |
|
991 | + $this->logger->expects(self::once())->method('warning') |
|
992 | + ->with('iMip message event dose not contains any attendees'); |
|
993 | + // construct parameters |
|
994 | + $principalUri = 'principals/user/linus'; |
|
995 | + $sender = '[email protected]'; |
|
996 | + $recipient = '[email protected]'; |
|
997 | + $calendar = $this->vCalendar2a; |
|
998 | + $calendar->add('METHOD', 'REPLY'); |
|
999 | + $calendar->VEVENT->remove('ATTENDEE'); |
|
1000 | + // Act |
|
1001 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendar->serialize()); |
|
1002 | + // Assert |
|
1003 | + $this->assertFalse($result); |
|
1004 | + } |
|
1005 | + |
|
1006 | + public function testHandleImipReplyDateInThePast(): void { |
|
1007 | + // construct mock user calendar |
|
1008 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1009 | + // construct mock calendar manager and returns |
|
1010 | + /** @var Manager&MockObject $manager */ |
|
1011 | + $manager = $this->getMockBuilder(Manager::class) |
|
1012 | + ->setConstructorArgs([ |
|
1013 | + $this->coordinator, |
|
1014 | + $this->container, |
|
1015 | + $this->logger, |
|
1016 | + $this->time, |
|
1017 | + $this->secureRandom, |
|
1018 | + $this->userManager, |
|
1019 | + $this->serverFactory, |
|
1020 | + ]) |
|
1021 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1022 | + ->getMock(); |
|
1023 | + $manager->expects(self::once()) |
|
1024 | + ->method('getCalendarsForPrincipal') |
|
1025 | + ->willReturn([$userCalendar]); |
|
1026 | + // construct logger and time returns |
|
1027 | + $this->logger->expects(self::once())->method('warning') |
|
1028 | + ->with('iMip message event could not be processed because the event is in the past'); |
|
1029 | + $this->time->expects(self::once()) |
|
1030 | + ->method('getTime') |
|
1031 | + ->willReturn(time()); |
|
1032 | + // construct parameters |
|
1033 | + $principalUri = 'principals/user/linus'; |
|
1034 | + $sender = '[email protected]'; |
|
1035 | + $recipient = '[email protected]'; |
|
1036 | + $calendarData = clone $this->vCalendar2a; |
|
1037 | + $calendarData->add('METHOD', 'REPLY'); |
|
1038 | + $calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past |
|
1039 | + // Act |
|
1040 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); |
|
1041 | + // Assert |
|
1042 | + $this->assertFalse($result); |
|
1043 | + } |
|
1044 | + |
|
1045 | + public function testHandleImipReplyEventNotFound(): void { |
|
1046 | + // construct mock user calendar |
|
1047 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1048 | + $userCalendar->expects(self::once()) |
|
1049 | + ->method('search') |
|
1050 | + ->willReturn([]); |
|
1051 | + // construct mock calendar manager and returns |
|
1052 | + /** @var Manager&MockObject $manager */ |
|
1053 | + $manager = $this->getMockBuilder(Manager::class) |
|
1054 | + ->setConstructorArgs([ |
|
1055 | + $this->coordinator, |
|
1056 | + $this->container, |
|
1057 | + $this->logger, |
|
1058 | + $this->time, |
|
1059 | + $this->secureRandom, |
|
1060 | + $this->userManager, |
|
1061 | + $this->serverFactory, |
|
1062 | + ]) |
|
1063 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1064 | + ->getMock(); |
|
1065 | + $manager->expects(self::once()) |
|
1066 | + ->method('getCalendarsForPrincipal') |
|
1067 | + ->willReturn([$userCalendar]); |
|
1068 | + // construct time returns |
|
1069 | + $this->time->expects(self::once()) |
|
1070 | + ->method('getTime') |
|
1071 | + ->willReturn(1628374233); |
|
1072 | + // construct parameters |
|
1073 | + $principalUri = 'principals/user/linus'; |
|
1074 | + $sender = '[email protected]'; |
|
1075 | + $recipient = '[email protected]'; |
|
1076 | + $calendarData = clone $this->vCalendar2a; |
|
1077 | + $calendarData->add('METHOD', 'REPLY'); |
|
1078 | + // construct logger return |
|
1079 | + $this->logger->expects(self::once())->method('warning') |
|
1080 | + ->with('iMip message event could not be processed because no corresponding event was found in any calendar ' . $principalUri . 'and UID' . $calendarData->VEVENT->UID->getValue()); |
|
1081 | + // Act |
|
1082 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); |
|
1083 | + // Assert |
|
1084 | + $this->assertFalse($result); |
|
1085 | + } |
|
1086 | + |
|
1087 | + public function testHandleImipReply(): void { |
|
1088 | + /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ |
|
1089 | + $manager = $this->getMockBuilder(Manager::class) |
|
1090 | + ->setConstructorArgs([ |
|
1091 | + $this->coordinator, |
|
1092 | + $this->container, |
|
1093 | + $this->logger, |
|
1094 | + $this->time, |
|
1095 | + $this->secureRandom, |
|
1096 | + $this->userManager, |
|
1097 | + $this->serverFactory, |
|
1098 | + ]) |
|
1099 | + ->onlyMethods([ |
|
1100 | + 'getCalendarsForPrincipal' |
|
1101 | + ]) |
|
1102 | + ->getMock(); |
|
1103 | + $calendar = $this->createMock(ITestCalendar::class); |
|
1104 | + $principalUri = 'principals/user/linus'; |
|
1105 | + $sender = '[email protected]'; |
|
1106 | + $recipient = '[email protected]'; |
|
1107 | + $calendarData = clone $this->vCalendar2a; |
|
1108 | + $calendarData->add('METHOD', 'REPLY'); |
|
1109 | + |
|
1110 | + $this->time->expects(self::once()) |
|
1111 | + ->method('getTime') |
|
1112 | + ->willReturn(1628374233); |
|
1113 | + $manager->expects(self::once()) |
|
1114 | + ->method('getCalendarsForPrincipal') |
|
1115 | + ->willReturn([$calendar]); |
|
1116 | + $calendar->expects(self::once()) |
|
1117 | + ->method('search') |
|
1118 | + ->willReturn([['uri' => 'testname.ics']]); |
|
1119 | + $calendar->expects(self::once()) |
|
1120 | + ->method('handleIMipMessage') |
|
1121 | + ->with('testname.ics', $calendarData->serialize()); |
|
1122 | + // Act |
|
1123 | + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); |
|
1124 | + // Assert |
|
1125 | + $this->assertTrue($result); |
|
1126 | + } |
|
1127 | + |
|
1128 | + public function testHandleImipCancelWithNoCalendars(): void { |
|
1129 | + // construct calendar manager returns |
|
1130 | + /** @var Manager&MockObject $manager */ |
|
1131 | + $manager = $this->getMockBuilder(Manager::class) |
|
1132 | + ->setConstructorArgs([ |
|
1133 | + $this->coordinator, |
|
1134 | + $this->container, |
|
1135 | + $this->logger, |
|
1136 | + $this->time, |
|
1137 | + $this->secureRandom, |
|
1138 | + $this->userManager, |
|
1139 | + $this->serverFactory, |
|
1140 | + ]) |
|
1141 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1142 | + ->getMock(); |
|
1143 | + $manager->expects(self::once()) |
|
1144 | + ->method('getCalendarsForPrincipal') |
|
1145 | + ->willReturn([]); |
|
1146 | + // construct logger returns |
|
1147 | + $this->logger->expects(self::once())->method('warning') |
|
1148 | + ->with('iMip message could not be processed because user has no calendars'); |
|
1149 | + // construct parameters |
|
1150 | + $principalUri = 'principals/user/pierre'; |
|
1151 | + $sender = '[email protected]'; |
|
1152 | + $recipient = '[email protected]'; |
|
1153 | + $replyTo = null; |
|
1154 | + $calendar = $this->vCalendar3a; |
|
1155 | + $calendar->add('METHOD', 'CANCEL'); |
|
1156 | + // Act |
|
1157 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1158 | + // Assert |
|
1159 | + $this->assertFalse($result); |
|
1160 | + } |
|
1161 | + |
|
1162 | + public function testHandleImipCancelWithInvalidData(): void { |
|
1163 | + // construct mock user calendar |
|
1164 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1165 | + // construct mock calendar manager and returns |
|
1166 | + /** @var Manager&MockObject $manager */ |
|
1167 | + $manager = $this->getMockBuilder(Manager::class) |
|
1168 | + ->setConstructorArgs([ |
|
1169 | + $this->coordinator, |
|
1170 | + $this->container, |
|
1171 | + $this->logger, |
|
1172 | + $this->time, |
|
1173 | + $this->secureRandom, |
|
1174 | + $this->userManager, |
|
1175 | + $this->serverFactory, |
|
1176 | + ]) |
|
1177 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1178 | + ->getMock(); |
|
1179 | + $manager->expects(self::once()) |
|
1180 | + ->method('getCalendarsForPrincipal') |
|
1181 | + ->willReturn([$userCalendar]); |
|
1182 | + // construct logger returns |
|
1183 | + $this->logger->expects(self::once())->method('error') |
|
1184 | + ->with('iMip message could not be processed because an error occurred while parsing the iMip message'); |
|
1185 | + // construct parameters |
|
1186 | + $principalUri = 'principals/user/attendee1'; |
|
1187 | + $sender = '[email protected]'; |
|
1188 | + $recipient = '[email protected]'; |
|
1189 | + $replyTo = null; |
|
1190 | + // Act |
|
1191 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, 'Invalid data'); |
|
1192 | + // Assert |
|
1193 | + $this->assertFalse($result); |
|
1194 | + } |
|
1195 | + |
|
1196 | + |
|
1197 | + public function testHandleImipCancelWithNoMethod(): void { |
|
1198 | + // construct mock user calendar |
|
1199 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1200 | + // construct mock calendar manager and returns |
|
1201 | + /** @var Manager&MockObject $manager */ |
|
1202 | + $manager = $this->getMockBuilder(Manager::class) |
|
1203 | + ->setConstructorArgs([ |
|
1204 | + $this->coordinator, |
|
1205 | + $this->container, |
|
1206 | + $this->logger, |
|
1207 | + $this->time, |
|
1208 | + $this->secureRandom, |
|
1209 | + $this->userManager, |
|
1210 | + $this->serverFactory, |
|
1211 | + ]) |
|
1212 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1213 | + ->getMock(); |
|
1214 | + $manager->expects(self::once()) |
|
1215 | + ->method('getCalendarsForPrincipal') |
|
1216 | + ->willReturn([$userCalendar]); |
|
1217 | + // construct logger returns |
|
1218 | + $this->logger->expects(self::once())->method('warning') |
|
1219 | + ->with('iMip message contains an incorrect or invalid method'); |
|
1220 | + // construct parameters |
|
1221 | + $principalUri = 'principals/user/pierre'; |
|
1222 | + $sender = '[email protected]'; |
|
1223 | + $recipient = '[email protected]'; |
|
1224 | + $replyTo = null; |
|
1225 | + $calendar = $this->vCalendar3a; |
|
1226 | + // Act |
|
1227 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1228 | + // Assert |
|
1229 | + $this->assertFalse($result); |
|
1230 | + } |
|
1231 | + |
|
1232 | + public function testHandleImipCancelWithInvalidMethod(): void { |
|
1233 | + // construct mock user calendar |
|
1234 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1235 | + // construct mock calendar manager and returns |
|
1236 | + /** @var Manager&MockObject $manager */ |
|
1237 | + $manager = $this->getMockBuilder(Manager::class) |
|
1238 | + ->setConstructorArgs([ |
|
1239 | + $this->coordinator, |
|
1240 | + $this->container, |
|
1241 | + $this->logger, |
|
1242 | + $this->time, |
|
1243 | + $this->secureRandom, |
|
1244 | + $this->userManager, |
|
1245 | + $this->serverFactory, |
|
1246 | + ]) |
|
1247 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1248 | + ->getMock(); |
|
1249 | + $manager->expects(self::once()) |
|
1250 | + ->method('getCalendarsForPrincipal') |
|
1251 | + ->willReturn([$userCalendar]); |
|
1252 | + // construct logger returns |
|
1253 | + $this->logger->expects(self::once())->method('warning') |
|
1254 | + ->with('iMip message contains an incorrect or invalid method'); |
|
1255 | + // construct parameters |
|
1256 | + $principalUri = 'principals/user/pierre'; |
|
1257 | + $sender = '[email protected]'; |
|
1258 | + $recipient = '[email protected]'; |
|
1259 | + $replyTo = null; |
|
1260 | + $calendar = $this->vCalendar3a; |
|
1261 | + $calendar->add('METHOD', 'UNKNOWN'); |
|
1262 | + // Act |
|
1263 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1264 | + // Assert |
|
1265 | + $this->assertFalse($result); |
|
1266 | + } |
|
1267 | + |
|
1268 | + public function testHandleImipCancelWithNoEvent(): void { |
|
1269 | + // construct mock user calendar |
|
1270 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1271 | + // construct mock calendar manager and returns |
|
1272 | + /** @var Manager&MockObject $manager */ |
|
1273 | + $manager = $this->getMockBuilder(Manager::class) |
|
1274 | + ->setConstructorArgs([ |
|
1275 | + $this->coordinator, |
|
1276 | + $this->container, |
|
1277 | + $this->logger, |
|
1278 | + $this->time, |
|
1279 | + $this->secureRandom, |
|
1280 | + $this->userManager, |
|
1281 | + $this->serverFactory, |
|
1282 | + ]) |
|
1283 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1284 | + ->getMock(); |
|
1285 | + $manager->expects(self::once()) |
|
1286 | + ->method('getCalendarsForPrincipal') |
|
1287 | + ->willReturn([$userCalendar]); |
|
1288 | + // construct logger returns |
|
1289 | + $this->logger->expects(self::once())->method('warning') |
|
1290 | + ->with('iMip message contains no event'); |
|
1291 | + // construct parameters |
|
1292 | + $principalUri = 'principals/user/pierre'; |
|
1293 | + $sender = '[email protected]'; |
|
1294 | + $recipient = '[email protected]'; |
|
1295 | + $replyTo = null; |
|
1296 | + $calendar = $this->vCalendar3a; |
|
1297 | + $calendar->add('METHOD', 'CANCEL'); |
|
1298 | + $calendar->remove('VEVENT'); |
|
1299 | + // Act |
|
1300 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1301 | + // Assert |
|
1302 | + $this->assertFalse($result); |
|
1303 | + } |
|
1304 | + |
|
1305 | + public function testHandleImipCancelWithNoUid(): void { |
|
1306 | + // construct mock user calendar |
|
1307 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1308 | + // construct mock calendar manager and returns |
|
1309 | + /** @var Manager&MockObject $manager */ |
|
1310 | + $manager = $this->getMockBuilder(Manager::class) |
|
1311 | + ->setConstructorArgs([ |
|
1312 | + $this->coordinator, |
|
1313 | + $this->container, |
|
1314 | + $this->logger, |
|
1315 | + $this->time, |
|
1316 | + $this->secureRandom, |
|
1317 | + $this->userManager, |
|
1318 | + $this->serverFactory, |
|
1319 | + ]) |
|
1320 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1321 | + ->getMock(); |
|
1322 | + $manager->expects(self::once()) |
|
1323 | + ->method('getCalendarsForPrincipal') |
|
1324 | + ->willReturn([$userCalendar]); |
|
1325 | + // construct logger returns |
|
1326 | + $this->logger->expects(self::once())->method('warning') |
|
1327 | + ->with('iMip message event dose not contains a UID'); |
|
1328 | + // construct parameters |
|
1329 | + $principalUri = 'principals/user/pierre'; |
|
1330 | + $sender = '[email protected]'; |
|
1331 | + $recipient = '[email protected]'; |
|
1332 | + $replyTo = null; |
|
1333 | + $calendar = $this->vCalendar3a; |
|
1334 | + $calendar->add('METHOD', 'CANCEL'); |
|
1335 | + $calendar->VEVENT->remove('UID'); |
|
1336 | + // Act |
|
1337 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1338 | + // Assert |
|
1339 | + $this->assertFalse($result); |
|
1340 | + } |
|
1341 | + |
|
1342 | + public function testHandleImipCancelWithNoOrganizer(): void { |
|
1343 | + // construct mock user calendar |
|
1344 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1345 | + // construct mock calendar manager and returns |
|
1346 | + /** @var Manager&MockObject $manager */ |
|
1347 | + $manager = $this->getMockBuilder(Manager::class) |
|
1348 | + ->setConstructorArgs([ |
|
1349 | + $this->coordinator, |
|
1350 | + $this->container, |
|
1351 | + $this->logger, |
|
1352 | + $this->time, |
|
1353 | + $this->secureRandom, |
|
1354 | + $this->userManager, |
|
1355 | + $this->serverFactory, |
|
1356 | + ]) |
|
1357 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1358 | + ->getMock(); |
|
1359 | + $manager->expects(self::once()) |
|
1360 | + ->method('getCalendarsForPrincipal') |
|
1361 | + ->willReturn([$userCalendar]); |
|
1362 | + // construct logger returns |
|
1363 | + $this->logger->expects(self::once())->method('warning') |
|
1364 | + ->with('iMip message event dose not contains an organizer'); |
|
1365 | + // construct parameters |
|
1366 | + $principalUri = 'principals/user/pierre'; |
|
1367 | + $sender = '[email protected]'; |
|
1368 | + $recipient = '[email protected]'; |
|
1369 | + $replyTo = null; |
|
1370 | + $calendar = $this->vCalendar3a; |
|
1371 | + $calendar->add('METHOD', 'CANCEL'); |
|
1372 | + $calendar->VEVENT->remove('ORGANIZER'); |
|
1373 | + // Act |
|
1374 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1375 | + // Assert |
|
1376 | + $this->assertFalse($result); |
|
1377 | + } |
|
1378 | + |
|
1379 | + public function testHandleImipCancelWithNoAttendee(): void { |
|
1380 | + // construct mock user calendar |
|
1381 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1382 | + // construct mock calendar manager and returns |
|
1383 | + /** @var Manager&MockObject $manager */ |
|
1384 | + $manager = $this->getMockBuilder(Manager::class) |
|
1385 | + ->setConstructorArgs([ |
|
1386 | + $this->coordinator, |
|
1387 | + $this->container, |
|
1388 | + $this->logger, |
|
1389 | + $this->time, |
|
1390 | + $this->secureRandom, |
|
1391 | + $this->userManager, |
|
1392 | + $this->serverFactory, |
|
1393 | + ]) |
|
1394 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1395 | + ->getMock(); |
|
1396 | + $manager->expects(self::once()) |
|
1397 | + ->method('getCalendarsForPrincipal') |
|
1398 | + ->willReturn([$userCalendar]); |
|
1399 | + // construct logger returns |
|
1400 | + $this->logger->expects(self::once())->method('warning') |
|
1401 | + ->with('iMip message event dose not contains any attendees'); |
|
1402 | + // construct parameters |
|
1403 | + $principalUri = 'principals/user/pierre'; |
|
1404 | + $sender = '[email protected]'; |
|
1405 | + $recipient = '[email protected]'; |
|
1406 | + $replyTo = null; |
|
1407 | + $calendar = $this->vCalendar3a; |
|
1408 | + $calendar->add('METHOD', 'CANCEL'); |
|
1409 | + $calendar->VEVENT->remove('ATTENDEE'); |
|
1410 | + // Act |
|
1411 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendar->serialize()); |
|
1412 | + // Assert |
|
1413 | + $this->assertFalse($result); |
|
1414 | + } |
|
1415 | + |
|
1416 | + public function testHandleImipCancelAttendeeNotRecipient(): void { |
|
1417 | + // construct mock user calendar |
|
1418 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1419 | + // construct mock calendar manager and returns |
|
1420 | + /** @var Manager&MockObject $manager */ |
|
1421 | + $manager = $this->getMockBuilder(Manager::class) |
|
1422 | + ->setConstructorArgs([ |
|
1423 | + $this->coordinator, |
|
1424 | + $this->container, |
|
1425 | + $this->logger, |
|
1426 | + $this->time, |
|
1427 | + $this->secureRandom, |
|
1428 | + $this->userManager, |
|
1429 | + $this->serverFactory, |
|
1430 | + ]) |
|
1431 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1432 | + ->getMock(); |
|
1433 | + $manager->expects(self::once()) |
|
1434 | + ->method('getCalendarsForPrincipal') |
|
1435 | + ->willReturn([$userCalendar]); |
|
1436 | + // construct logger returns |
|
1437 | + $this->logger->expects(self::once())->method('warning') |
|
1438 | + ->with('iMip message event could not be processed because recipient must be an ATTENDEE of this event'); |
|
1439 | + // construct parameters |
|
1440 | + $principalUri = 'principals/user/pierre'; |
|
1441 | + $sender = '[email protected]'; |
|
1442 | + $recipient = '[email protected]'; |
|
1443 | + $replyTo = null; |
|
1444 | + $calendarData = clone $this->vCalendar3a; |
|
1445 | + $calendarData->add('METHOD', 'CANCEL'); |
|
1446 | + // Act |
|
1447 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1448 | + // Assert |
|
1449 | + $this->assertFalse($result); |
|
1450 | + } |
|
1451 | + |
|
1452 | + public function testHandleImipCancelDateInThePast(): void { |
|
1453 | + // construct mock user calendar |
|
1454 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1455 | + // construct mock calendar manager and returns |
|
1456 | + /** @var Manager&MockObject $manager */ |
|
1457 | + $manager = $this->getMockBuilder(Manager::class) |
|
1458 | + ->setConstructorArgs([ |
|
1459 | + $this->coordinator, |
|
1460 | + $this->container, |
|
1461 | + $this->logger, |
|
1462 | + $this->time, |
|
1463 | + $this->secureRandom, |
|
1464 | + $this->userManager, |
|
1465 | + $this->serverFactory, |
|
1466 | + ]) |
|
1467 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1468 | + ->getMock(); |
|
1469 | + $manager->expects(self::once()) |
|
1470 | + ->method('getCalendarsForPrincipal') |
|
1471 | + ->willReturn([$userCalendar]); |
|
1472 | + // construct logger and time returns |
|
1473 | + $this->logger->expects(self::once())->method('warning') |
|
1474 | + ->with('iMip message event could not be processed because the event is in the past'); |
|
1475 | + $this->time->expects(self::once()) |
|
1476 | + ->method('getTime') |
|
1477 | + ->willReturn(time()); |
|
1478 | + // construct parameters |
|
1479 | + $principalUri = 'principals/user/pierre'; |
|
1480 | + $sender = '[email protected]'; |
|
1481 | + $recipient = '[email protected]'; |
|
1482 | + $replyTo = null; |
|
1483 | + $calendarData = clone $this->vCalendar3a; |
|
1484 | + $calendarData->add('METHOD', 'CANCEL'); |
|
1485 | + $calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past |
|
1486 | + // Act |
|
1487 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1488 | + // Assert |
|
1489 | + $this->assertFalse($result); |
|
1490 | + } |
|
1491 | + |
|
1492 | + public function testHandleImipCancelEventNotFound(): void { |
|
1493 | + // construct mock user calendar |
|
1494 | + $userCalendar = $this->createMock(ITestCalendar::class); |
|
1495 | + $userCalendar->expects(self::once()) |
|
1496 | + ->method('search') |
|
1497 | + ->willReturn([]); |
|
1498 | + // construct mock calendar manager and returns |
|
1499 | + /** @var Manager&MockObject $manager */ |
|
1500 | + $manager = $this->getMockBuilder(Manager::class) |
|
1501 | + ->setConstructorArgs([ |
|
1502 | + $this->coordinator, |
|
1503 | + $this->container, |
|
1504 | + $this->logger, |
|
1505 | + $this->time, |
|
1506 | + $this->secureRandom, |
|
1507 | + $this->userManager, |
|
1508 | + $this->serverFactory, |
|
1509 | + ]) |
|
1510 | + ->onlyMethods(['getCalendarsForPrincipal']) |
|
1511 | + ->getMock(); |
|
1512 | + $manager->expects(self::once()) |
|
1513 | + ->method('getCalendarsForPrincipal') |
|
1514 | + ->willReturn([$userCalendar]); |
|
1515 | + // construct time returns |
|
1516 | + $this->time->expects(self::once()) |
|
1517 | + ->method('getTime') |
|
1518 | + ->willReturn(1628374233); |
|
1519 | + // construct parameters |
|
1520 | + $principalUri = 'principals/user/pierre'; |
|
1521 | + $sender = '[email protected]'; |
|
1522 | + $recipient = '[email protected]'; |
|
1523 | + $replyTo = null; |
|
1524 | + $calendarData = clone $this->vCalendar3a; |
|
1525 | + $calendarData->add('METHOD', 'CANCEL'); |
|
1526 | + // construct logger return |
|
1527 | + $this->logger->expects(self::once())->method('warning') |
|
1528 | + ->with('iMip message event could not be processed because no corresponding event was found in any calendar ' . $principalUri . 'and UID' . $calendarData->VEVENT->UID->getValue()); |
|
1529 | + // Act |
|
1530 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1531 | + // Assert |
|
1532 | + $this->assertFalse($result); |
|
1533 | + } |
|
1534 | + |
|
1535 | + public function testHandleImipCancelOrganiserInReplyTo(): void { |
|
1536 | + /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ |
|
1537 | + $manager = $this->getMockBuilder(Manager::class) |
|
1538 | + ->setConstructorArgs([ |
|
1539 | + $this->coordinator, |
|
1540 | + $this->container, |
|
1541 | + $this->logger, |
|
1542 | + $this->time, |
|
1543 | + $this->secureRandom, |
|
1544 | + $this->userManager, |
|
1545 | + $this->serverFactory, |
|
1546 | + ]) |
|
1547 | + ->onlyMethods([ |
|
1548 | + 'getCalendarsForPrincipal' |
|
1549 | + ]) |
|
1550 | + ->getMock(); |
|
1551 | + |
|
1552 | + $principalUri = 'principals/user/pierre'; |
|
1553 | + $sender = '[email protected]'; |
|
1554 | + $recipient = '[email protected]'; |
|
1555 | + $replyTo = '[email protected]'; |
|
1556 | + $calendar = $this->createMock(ITestCalendar::class); |
|
1557 | + $calendarData = clone $this->vCalendar3a; |
|
1558 | + $calendarData->add('METHOD', 'CANCEL'); |
|
1559 | + |
|
1560 | + $this->time->expects(self::once()) |
|
1561 | + ->method('getTime') |
|
1562 | + ->willReturn(1628374233); |
|
1563 | + $manager->expects(self::once()) |
|
1564 | + ->method('getCalendarsForPrincipal') |
|
1565 | + ->with($principalUri) |
|
1566 | + ->willReturn([$calendar]); |
|
1567 | + $calendar->expects(self::once()) |
|
1568 | + ->method('search') |
|
1569 | + ->willReturn([['uri' => 'testname.ics']]); |
|
1570 | + $calendar->expects(self::once()) |
|
1571 | + ->method('handleIMipMessage') |
|
1572 | + ->with('testname.ics', $calendarData->serialize()); |
|
1573 | + // Act |
|
1574 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1575 | + // Assert |
|
1576 | + $this->assertTrue($result); |
|
1577 | + } |
|
1578 | + |
|
1579 | + public function testHandleImipCancel(): void { |
|
1580 | + /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ |
|
1581 | + $manager = $this->getMockBuilder(Manager::class) |
|
1582 | + ->setConstructorArgs([ |
|
1583 | + $this->coordinator, |
|
1584 | + $this->container, |
|
1585 | + $this->logger, |
|
1586 | + $this->time, |
|
1587 | + $this->secureRandom, |
|
1588 | + $this->userManager, |
|
1589 | + $this->serverFactory, |
|
1590 | + ]) |
|
1591 | + ->onlyMethods([ |
|
1592 | + 'getCalendarsForPrincipal' |
|
1593 | + ]) |
|
1594 | + ->getMock(); |
|
1595 | + $principalUri = 'principals/user/pierre'; |
|
1596 | + $sender = '[email protected]'; |
|
1597 | + $recipient = '[email protected]'; |
|
1598 | + $replyTo = null; |
|
1599 | + $calendar = $this->createMock(ITestCalendar::class); |
|
1600 | + $calendarData = clone $this->vCalendar3a; |
|
1601 | + $calendarData->add('METHOD', 'CANCEL'); |
|
1602 | + |
|
1603 | + $this->time->expects(self::once()) |
|
1604 | + ->method('getTime') |
|
1605 | + ->willReturn(1628374233); |
|
1606 | + $manager->expects(self::once()) |
|
1607 | + ->method('getCalendarsForPrincipal') |
|
1608 | + ->with($principalUri) |
|
1609 | + ->willReturn([$calendar]); |
|
1610 | + $calendar->expects(self::once()) |
|
1611 | + ->method('search') |
|
1612 | + ->willReturn([['uri' => 'testname.ics']]); |
|
1613 | + $calendar->expects(self::once()) |
|
1614 | + ->method('handleIMipMessage') |
|
1615 | + ->with('testname.ics', $calendarData->serialize()); |
|
1616 | + // Act |
|
1617 | + $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); |
|
1618 | + // Assert |
|
1619 | + $this->assertTrue($result); |
|
1620 | + } |
|
1621 | + |
|
1622 | + private function getFreeBusyResponse(): string { |
|
1623 | + return <<<EOF |
|
1624 | 1624 | <?xml version="1.0" encoding="utf-8"?> |
1625 | 1625 | <cal:schedule-response xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:cal="urn:ietf:params:xml:ns:caldav" xmlns:cs="http://calendarserver.org/ns/" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns"> |
1626 | 1626 | <cal:response> |
@@ -1698,139 +1698,139 @@ discard block |
||
1698 | 1698 | </cal:response> |
1699 | 1699 | </cal:schedule-response> |
1700 | 1700 | EOF; |
1701 | - } |
|
1702 | - |
|
1703 | - public function testCheckAvailability(): void { |
|
1704 | - $organizer = $this->createMock(IUser::class); |
|
1705 | - $organizer->expects(self::once()) |
|
1706 | - ->method('getUID') |
|
1707 | - ->willReturn('admin'); |
|
1708 | - $organizer->expects(self::once()) |
|
1709 | - ->method('getEMailAddress') |
|
1710 | - ->willReturn('[email protected]'); |
|
1711 | - |
|
1712 | - $user1 = $this->createMock(IUser::class); |
|
1713 | - $user2 = $this->createMock(IUser::class); |
|
1714 | - |
|
1715 | - $this->userManager->expects(self::exactly(3)) |
|
1716 | - ->method('getByEmail') |
|
1717 | - ->willReturnMap([ |
|
1718 | - ['[email protected]', [$user1]], |
|
1719 | - ['[email protected]', [$user2]], |
|
1720 | - ['[email protected]', []], |
|
1721 | - ]); |
|
1722 | - |
|
1723 | - $authPlugin = $this->createMock(CustomPrincipalPlugin::class); |
|
1724 | - $authPlugin->expects(self::once()) |
|
1725 | - ->method('setCurrentPrincipal') |
|
1726 | - ->with('principals/users/admin'); |
|
1727 | - |
|
1728 | - $server = $this->createMock(\OCA\DAV\Connector\Sabre\Server::class); |
|
1729 | - $server->expects(self::once()) |
|
1730 | - ->method('getPlugin') |
|
1731 | - ->with('auth') |
|
1732 | - ->willReturn($authPlugin); |
|
1733 | - $server->expects(self::once()) |
|
1734 | - ->method('invokeMethod') |
|
1735 | - ->willReturnCallback(function ( |
|
1736 | - RequestInterface $request, |
|
1737 | - ResponseInterface $response, |
|
1738 | - bool $sendResponse, |
|
1739 | - ) { |
|
1740 | - $requestBody = file_get_contents(__DIR__ . '/../../data/ics/free-busy-request.ics'); |
|
1741 | - $this->assertEquals('POST', $request->getMethod()); |
|
1742 | - $this->assertEquals('calendars/admin/outbox', $request->getPath()); |
|
1743 | - $this->assertEquals('text/calendar', $request->getHeader('Content-Type')); |
|
1744 | - $this->assertEquals('0', $request->getHeader('Depth')); |
|
1745 | - $this->assertEquals($requestBody, $request->getBodyAsString()); |
|
1746 | - $this->assertFalse($sendResponse); |
|
1747 | - $response->setStatus(200); |
|
1748 | - $response->setBody($this->getFreeBusyResponse()); |
|
1749 | - }); |
|
1750 | - |
|
1751 | - $this->serverFactory->expects(self::once()) |
|
1752 | - ->method('createAttendeeAvailabilityServer') |
|
1753 | - ->willReturn($server); |
|
1754 | - |
|
1755 | - $start = new DateTimeImmutable('2025-01-16T06:00:00Z'); |
|
1756 | - $end = new DateTimeImmutable('2025-01-17T06:00:00Z'); |
|
1757 | - $actual = $this->manager->checkAvailability($start, $end, $organizer, [ |
|
1758 | - '[email protected]', |
|
1759 | - '[email protected]', |
|
1760 | - '[email protected]', |
|
1761 | - ]); |
|
1762 | - $expected = [ |
|
1763 | - new AvailabilityResult('[email protected]', false), |
|
1764 | - new AvailabilityResult('[email protected]', true), |
|
1765 | - new AvailabilityResult('[email protected]', false), |
|
1766 | - ]; |
|
1767 | - $this->assertEquals($expected, $actual); |
|
1768 | - } |
|
1769 | - |
|
1770 | - public function testCheckAvailabilityWithMailtoPrefix(): void { |
|
1771 | - $organizer = $this->createMock(IUser::class); |
|
1772 | - $organizer->expects(self::once()) |
|
1773 | - ->method('getUID') |
|
1774 | - ->willReturn('admin'); |
|
1775 | - $organizer->expects(self::once()) |
|
1776 | - ->method('getEMailAddress') |
|
1777 | - ->willReturn('[email protected]'); |
|
1778 | - |
|
1779 | - $user1 = $this->createMock(IUser::class); |
|
1780 | - $user2 = $this->createMock(IUser::class); |
|
1781 | - |
|
1782 | - $this->userManager->expects(self::exactly(3)) |
|
1783 | - ->method('getByEmail') |
|
1784 | - ->willReturnMap([ |
|
1785 | - ['[email protected]', [$user1]], |
|
1786 | - ['[email protected]', [$user2]], |
|
1787 | - ['[email protected]', []], |
|
1788 | - ]); |
|
1789 | - |
|
1790 | - $authPlugin = $this->createMock(CustomPrincipalPlugin::class); |
|
1791 | - $authPlugin->expects(self::once()) |
|
1792 | - ->method('setCurrentPrincipal') |
|
1793 | - ->with('principals/users/admin'); |
|
1794 | - |
|
1795 | - $server = $this->createMock(\OCA\DAV\Connector\Sabre\Server::class); |
|
1796 | - $server->expects(self::once()) |
|
1797 | - ->method('getPlugin') |
|
1798 | - ->with('auth') |
|
1799 | - ->willReturn($authPlugin); |
|
1800 | - $server->expects(self::once()) |
|
1801 | - ->method('invokeMethod') |
|
1802 | - ->willReturnCallback(function ( |
|
1803 | - RequestInterface $request, |
|
1804 | - ResponseInterface $response, |
|
1805 | - bool $sendResponse, |
|
1806 | - ) { |
|
1807 | - $requestBody = file_get_contents(__DIR__ . '/../../data/ics/free-busy-request.ics'); |
|
1808 | - $this->assertEquals('POST', $request->getMethod()); |
|
1809 | - $this->assertEquals('calendars/admin/outbox', $request->getPath()); |
|
1810 | - $this->assertEquals('text/calendar', $request->getHeader('Content-Type')); |
|
1811 | - $this->assertEquals('0', $request->getHeader('Depth')); |
|
1812 | - $this->assertEquals($requestBody, $request->getBodyAsString()); |
|
1813 | - $this->assertFalse($sendResponse); |
|
1814 | - $response->setStatus(200); |
|
1815 | - $response->setBody($this->getFreeBusyResponse()); |
|
1816 | - }); |
|
1817 | - |
|
1818 | - $this->serverFactory->expects(self::once()) |
|
1819 | - ->method('createAttendeeAvailabilityServer') |
|
1820 | - ->willReturn($server); |
|
1821 | - |
|
1822 | - $start = new DateTimeImmutable('2025-01-16T06:00:00Z'); |
|
1823 | - $end = new DateTimeImmutable('2025-01-17T06:00:00Z'); |
|
1824 | - $actual = $this->manager->checkAvailability($start, $end, $organizer, [ |
|
1825 | - 'mailto:[email protected]', |
|
1826 | - 'mailto:[email protected]', |
|
1827 | - 'mailto:[email protected]', |
|
1828 | - ]); |
|
1829 | - $expected = [ |
|
1830 | - new AvailabilityResult('[email protected]', false), |
|
1831 | - new AvailabilityResult('[email protected]', true), |
|
1832 | - new AvailabilityResult('[email protected]', false), |
|
1833 | - ]; |
|
1834 | - $this->assertEquals($expected, $actual); |
|
1835 | - } |
|
1701 | + } |
|
1702 | + |
|
1703 | + public function testCheckAvailability(): void { |
|
1704 | + $organizer = $this->createMock(IUser::class); |
|
1705 | + $organizer->expects(self::once()) |
|
1706 | + ->method('getUID') |
|
1707 | + ->willReturn('admin'); |
|
1708 | + $organizer->expects(self::once()) |
|
1709 | + ->method('getEMailAddress') |
|
1710 | + ->willReturn('[email protected]'); |
|
1711 | + |
|
1712 | + $user1 = $this->createMock(IUser::class); |
|
1713 | + $user2 = $this->createMock(IUser::class); |
|
1714 | + |
|
1715 | + $this->userManager->expects(self::exactly(3)) |
|
1716 | + ->method('getByEmail') |
|
1717 | + ->willReturnMap([ |
|
1718 | + ['[email protected]', [$user1]], |
|
1719 | + ['[email protected]', [$user2]], |
|
1720 | + ['[email protected]', []], |
|
1721 | + ]); |
|
1722 | + |
|
1723 | + $authPlugin = $this->createMock(CustomPrincipalPlugin::class); |
|
1724 | + $authPlugin->expects(self::once()) |
|
1725 | + ->method('setCurrentPrincipal') |
|
1726 | + ->with('principals/users/admin'); |
|
1727 | + |
|
1728 | + $server = $this->createMock(\OCA\DAV\Connector\Sabre\Server::class); |
|
1729 | + $server->expects(self::once()) |
|
1730 | + ->method('getPlugin') |
|
1731 | + ->with('auth') |
|
1732 | + ->willReturn($authPlugin); |
|
1733 | + $server->expects(self::once()) |
|
1734 | + ->method('invokeMethod') |
|
1735 | + ->willReturnCallback(function ( |
|
1736 | + RequestInterface $request, |
|
1737 | + ResponseInterface $response, |
|
1738 | + bool $sendResponse, |
|
1739 | + ) { |
|
1740 | + $requestBody = file_get_contents(__DIR__ . '/../../data/ics/free-busy-request.ics'); |
|
1741 | + $this->assertEquals('POST', $request->getMethod()); |
|
1742 | + $this->assertEquals('calendars/admin/outbox', $request->getPath()); |
|
1743 | + $this->assertEquals('text/calendar', $request->getHeader('Content-Type')); |
|
1744 | + $this->assertEquals('0', $request->getHeader('Depth')); |
|
1745 | + $this->assertEquals($requestBody, $request->getBodyAsString()); |
|
1746 | + $this->assertFalse($sendResponse); |
|
1747 | + $response->setStatus(200); |
|
1748 | + $response->setBody($this->getFreeBusyResponse()); |
|
1749 | + }); |
|
1750 | + |
|
1751 | + $this->serverFactory->expects(self::once()) |
|
1752 | + ->method('createAttendeeAvailabilityServer') |
|
1753 | + ->willReturn($server); |
|
1754 | + |
|
1755 | + $start = new DateTimeImmutable('2025-01-16T06:00:00Z'); |
|
1756 | + $end = new DateTimeImmutable('2025-01-17T06:00:00Z'); |
|
1757 | + $actual = $this->manager->checkAvailability($start, $end, $organizer, [ |
|
1758 | + '[email protected]', |
|
1759 | + '[email protected]', |
|
1760 | + '[email protected]', |
|
1761 | + ]); |
|
1762 | + $expected = [ |
|
1763 | + new AvailabilityResult('[email protected]', false), |
|
1764 | + new AvailabilityResult('[email protected]', true), |
|
1765 | + new AvailabilityResult('[email protected]', false), |
|
1766 | + ]; |
|
1767 | + $this->assertEquals($expected, $actual); |
|
1768 | + } |
|
1769 | + |
|
1770 | + public function testCheckAvailabilityWithMailtoPrefix(): void { |
|
1771 | + $organizer = $this->createMock(IUser::class); |
|
1772 | + $organizer->expects(self::once()) |
|
1773 | + ->method('getUID') |
|
1774 | + ->willReturn('admin'); |
|
1775 | + $organizer->expects(self::once()) |
|
1776 | + ->method('getEMailAddress') |
|
1777 | + ->willReturn('[email protected]'); |
|
1778 | + |
|
1779 | + $user1 = $this->createMock(IUser::class); |
|
1780 | + $user2 = $this->createMock(IUser::class); |
|
1781 | + |
|
1782 | + $this->userManager->expects(self::exactly(3)) |
|
1783 | + ->method('getByEmail') |
|
1784 | + ->willReturnMap([ |
|
1785 | + ['[email protected]', [$user1]], |
|
1786 | + ['[email protected]', [$user2]], |
|
1787 | + ['[email protected]', []], |
|
1788 | + ]); |
|
1789 | + |
|
1790 | + $authPlugin = $this->createMock(CustomPrincipalPlugin::class); |
|
1791 | + $authPlugin->expects(self::once()) |
|
1792 | + ->method('setCurrentPrincipal') |
|
1793 | + ->with('principals/users/admin'); |
|
1794 | + |
|
1795 | + $server = $this->createMock(\OCA\DAV\Connector\Sabre\Server::class); |
|
1796 | + $server->expects(self::once()) |
|
1797 | + ->method('getPlugin') |
|
1798 | + ->with('auth') |
|
1799 | + ->willReturn($authPlugin); |
|
1800 | + $server->expects(self::once()) |
|
1801 | + ->method('invokeMethod') |
|
1802 | + ->willReturnCallback(function ( |
|
1803 | + RequestInterface $request, |
|
1804 | + ResponseInterface $response, |
|
1805 | + bool $sendResponse, |
|
1806 | + ) { |
|
1807 | + $requestBody = file_get_contents(__DIR__ . '/../../data/ics/free-busy-request.ics'); |
|
1808 | + $this->assertEquals('POST', $request->getMethod()); |
|
1809 | + $this->assertEquals('calendars/admin/outbox', $request->getPath()); |
|
1810 | + $this->assertEquals('text/calendar', $request->getHeader('Content-Type')); |
|
1811 | + $this->assertEquals('0', $request->getHeader('Depth')); |
|
1812 | + $this->assertEquals($requestBody, $request->getBodyAsString()); |
|
1813 | + $this->assertFalse($sendResponse); |
|
1814 | + $response->setStatus(200); |
|
1815 | + $response->setBody($this->getFreeBusyResponse()); |
|
1816 | + }); |
|
1817 | + |
|
1818 | + $this->serverFactory->expects(self::once()) |
|
1819 | + ->method('createAttendeeAvailabilityServer') |
|
1820 | + ->willReturn($server); |
|
1821 | + |
|
1822 | + $start = new DateTimeImmutable('2025-01-16T06:00:00Z'); |
|
1823 | + $end = new DateTimeImmutable('2025-01-17T06:00:00Z'); |
|
1824 | + $actual = $this->manager->checkAvailability($start, $end, $organizer, [ |
|
1825 | + 'mailto:[email protected]', |
|
1826 | + 'mailto:[email protected]', |
|
1827 | + 'mailto:[email protected]', |
|
1828 | + ]); |
|
1829 | + $expected = [ |
|
1830 | + new AvailabilityResult('[email protected]', false), |
|
1831 | + new AvailabilityResult('[email protected]', true), |
|
1832 | + new AvailabilityResult('[email protected]', false), |
|
1833 | + ]; |
|
1834 | + $this->assertEquals($expected, $actual); |
|
1835 | + } |
|
1836 | 1836 | } |
@@ -16,77 +16,77 @@ |
||
16 | 16 | * This class provides tests for the MemoryInfo class. |
17 | 17 | */ |
18 | 18 | class MemoryInfoTest extends TestCase { |
19 | - /** |
|
20 | - * The "memory_limit" value before tests. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - private $iniSettingBeforeTest; |
|
19 | + /** |
|
20 | + * The "memory_limit" value before tests. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + private $iniSettingBeforeTest; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @beforeClass |
|
28 | - */ |
|
29 | - public function backupMemoryInfoIniSetting() { |
|
30 | - $this->iniSettingBeforeTest = ini_get('memory_limit'); |
|
31 | - } |
|
26 | + /** |
|
27 | + * @beforeClass |
|
28 | + */ |
|
29 | + public function backupMemoryInfoIniSetting() { |
|
30 | + $this->iniSettingBeforeTest = ini_get('memory_limit'); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * @afterClass |
|
35 | - */ |
|
36 | - public function restoreMemoryInfoIniSetting() { |
|
37 | - ini_set('memory_limit', $this->iniSettingBeforeTest); |
|
38 | - } |
|
33 | + /** |
|
34 | + * @afterClass |
|
35 | + */ |
|
36 | + public function restoreMemoryInfoIniSetting() { |
|
37 | + ini_set('memory_limit', $this->iniSettingBeforeTest); |
|
38 | + } |
|
39 | 39 | |
40 | - public static function getMemoryLimitTestData(): array { |
|
41 | - return [ |
|
42 | - 'unlimited' => ['-1', -1,], |
|
43 | - '524288000 bytes' => ['524288000', 524288000,], |
|
44 | - '500M' => ['500M', 524288000,], |
|
45 | - '512000K' => ['512000K', 524288000,], |
|
46 | - '2G' => ['2G', 2147483648,], |
|
47 | - ]; |
|
48 | - } |
|
40 | + public static function getMemoryLimitTestData(): array { |
|
41 | + return [ |
|
42 | + 'unlimited' => ['-1', -1,], |
|
43 | + '524288000 bytes' => ['524288000', 524288000,], |
|
44 | + '500M' => ['500M', 524288000,], |
|
45 | + '512000K' => ['512000K', 524288000,], |
|
46 | + '2G' => ['2G', 2147483648,], |
|
47 | + ]; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Tests that getMemoryLimit works as expected. |
|
52 | - * |
|
53 | - * @param string $iniValue The "memory_limit" ini data. |
|
54 | - * @param int|float $expected The expected detected memory limit. |
|
55 | - * @dataProvider getMemoryLimitTestData |
|
56 | - */ |
|
57 | - public function testMemoryLimit(string $iniValue, int|float $expected): void { |
|
58 | - ini_set('memory_limit', $iniValue); |
|
59 | - $memoryInfo = new MemoryInfo(); |
|
60 | - self::assertEquals($expected, $memoryInfo->getMemoryLimit()); |
|
61 | - } |
|
50 | + /** |
|
51 | + * Tests that getMemoryLimit works as expected. |
|
52 | + * |
|
53 | + * @param string $iniValue The "memory_limit" ini data. |
|
54 | + * @param int|float $expected The expected detected memory limit. |
|
55 | + * @dataProvider getMemoryLimitTestData |
|
56 | + */ |
|
57 | + public function testMemoryLimit(string $iniValue, int|float $expected): void { |
|
58 | + ini_set('memory_limit', $iniValue); |
|
59 | + $memoryInfo = new MemoryInfo(); |
|
60 | + self::assertEquals($expected, $memoryInfo->getMemoryLimit()); |
|
61 | + } |
|
62 | 62 | |
63 | - public static function getSufficientMemoryTestData(): array { |
|
64 | - return [ |
|
65 | - 'unlimited' => [-1, true,], |
|
66 | - '512M' => [512 * 1024 * 1024, true,], |
|
67 | - '1G' => [1024 * 1024 * 1024, true,], |
|
68 | - '256M' => [256 * 1024 * 1024, false,], |
|
69 | - ]; |
|
70 | - } |
|
63 | + public static function getSufficientMemoryTestData(): array { |
|
64 | + return [ |
|
65 | + 'unlimited' => [-1, true,], |
|
66 | + '512M' => [512 * 1024 * 1024, true,], |
|
67 | + '1G' => [1024 * 1024 * 1024, true,], |
|
68 | + '256M' => [256 * 1024 * 1024, false,], |
|
69 | + ]; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Tests that isMemoryLimitSufficient returns the correct values. |
|
74 | - * |
|
75 | - * @param int $memoryLimit The memory limit |
|
76 | - * @param bool $expected If the memory limit is sufficient. |
|
77 | - * @dataProvider getSufficientMemoryTestData |
|
78 | - */ |
|
79 | - public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected): void { |
|
80 | - /* @var MemoryInfo|MockObject $memoryInfo */ |
|
81 | - $memoryInfo = $this->getMockBuilder(MemoryInfo::class) |
|
82 | - ->onlyMethods(['getMemoryLimit',]) |
|
83 | - ->getMock(); |
|
72 | + /** |
|
73 | + * Tests that isMemoryLimitSufficient returns the correct values. |
|
74 | + * |
|
75 | + * @param int $memoryLimit The memory limit |
|
76 | + * @param bool $expected If the memory limit is sufficient. |
|
77 | + * @dataProvider getSufficientMemoryTestData |
|
78 | + */ |
|
79 | + public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected): void { |
|
80 | + /* @var MemoryInfo|MockObject $memoryInfo */ |
|
81 | + $memoryInfo = $this->getMockBuilder(MemoryInfo::class) |
|
82 | + ->onlyMethods(['getMemoryLimit',]) |
|
83 | + ->getMock(); |
|
84 | 84 | |
85 | - $memoryInfo |
|
86 | - ->method('getMemoryLimit') |
|
87 | - ->willReturn($memoryLimit); |
|
85 | + $memoryInfo |
|
86 | + ->method('getMemoryLimit') |
|
87 | + ->willReturn($memoryLimit); |
|
88 | 88 | |
89 | - $isMemoryLimitSufficient = $memoryInfo->isMemoryLimitSufficient(); |
|
90 | - self::assertEquals($expected, $isMemoryLimitSufficient); |
|
91 | - } |
|
89 | + $isMemoryLimitSufficient = $memoryInfo->isMemoryLimitSufficient(); |
|
90 | + self::assertEquals($expected, $isMemoryLimitSufficient); |
|
91 | + } |
|
92 | 92 | } |
@@ -39,11 +39,11 @@ discard block |
||
39 | 39 | |
40 | 40 | public static function getMemoryLimitTestData(): array { |
41 | 41 | return [ |
42 | - 'unlimited' => ['-1', -1,], |
|
43 | - '524288000 bytes' => ['524288000', 524288000,], |
|
44 | - '500M' => ['500M', 524288000,], |
|
45 | - '512000K' => ['512000K', 524288000,], |
|
46 | - '2G' => ['2G', 2147483648,], |
|
42 | + 'unlimited' => ['-1', -1, ], |
|
43 | + '524288000 bytes' => ['524288000', 524288000, ], |
|
44 | + '500M' => ['500M', 524288000, ], |
|
45 | + '512000K' => ['512000K', 524288000, ], |
|
46 | + '2G' => ['2G', 2147483648, ], |
|
47 | 47 | ]; |
48 | 48 | } |
49 | 49 | |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | * @param int|float $expected The expected detected memory limit. |
55 | 55 | * @dataProvider getMemoryLimitTestData |
56 | 56 | */ |
57 | - public function testMemoryLimit(string $iniValue, int|float $expected): void { |
|
57 | + public function testMemoryLimit(string $iniValue, int | float $expected): void { |
|
58 | 58 | ini_set('memory_limit', $iniValue); |
59 | 59 | $memoryInfo = new MemoryInfo(); |
60 | 60 | self::assertEquals($expected, $memoryInfo->getMemoryLimit()); |
@@ -62,10 +62,10 @@ discard block |
||
62 | 62 | |
63 | 63 | public static function getSufficientMemoryTestData(): array { |
64 | 64 | return [ |
65 | - 'unlimited' => [-1, true,], |
|
66 | - '512M' => [512 * 1024 * 1024, true,], |
|
67 | - '1G' => [1024 * 1024 * 1024, true,], |
|
68 | - '256M' => [256 * 1024 * 1024, false,], |
|
65 | + 'unlimited' => [-1, true, ], |
|
66 | + '512M' => [512 * 1024 * 1024, true, ], |
|
67 | + '1G' => [1024 * 1024 * 1024, true, ], |
|
68 | + '256M' => [256 * 1024 * 1024, false, ], |
|
69 | 69 | ]; |
70 | 70 | } |
71 | 71 | |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | public function testIsMemoryLimitSufficient(int $memoryLimit, bool $expected): void { |
80 | 80 | /* @var MemoryInfo|MockObject $memoryInfo */ |
81 | 81 | $memoryInfo = $this->getMockBuilder(MemoryInfo::class) |
82 | - ->onlyMethods(['getMemoryLimit',]) |
|
82 | + ->onlyMethods(['getMemoryLimit', ]) |
|
83 | 83 | ->getMock(); |
84 | 84 | |
85 | 85 | $memoryInfo |
@@ -31,956 +31,956 @@ |
||
31 | 31 | } |
32 | 32 | |
33 | 33 | class ManagerTest extends TestCase { |
34 | - /** @var Manager|MockObject */ |
|
35 | - protected $userManager; |
|
36 | - /** @var IEventDispatcher|MockObject */ |
|
37 | - protected $dispatcher; |
|
38 | - /** @var LoggerInterface|MockObject */ |
|
39 | - protected $logger; |
|
40 | - /** @var ICacheFactory|MockObject */ |
|
41 | - private $cache; |
|
42 | - /** @var IRemoteAddress|MockObject */ |
|
43 | - private $remoteIpAddress; |
|
44 | - |
|
45 | - protected function setUp(): void { |
|
46 | - parent::setUp(); |
|
47 | - |
|
48 | - $this->userManager = $this->createMock(Manager::class); |
|
49 | - $this->dispatcher = $this->createMock(IEventDispatcher::class); |
|
50 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
51 | - $this->cache = $this->createMock(ICacheFactory::class); |
|
52 | - |
|
53 | - $this->remoteIpAddress = $this->createMock(IRemoteAddress::class); |
|
54 | - $this->remoteIpAddress->method('allowsAdminActions')->willReturn(true); |
|
55 | - } |
|
56 | - |
|
57 | - private function getTestUser($userId) { |
|
58 | - $mockUser = $this->createMock(IUser::class); |
|
59 | - $mockUser->expects($this->any()) |
|
60 | - ->method('getUID') |
|
61 | - ->willReturn($userId); |
|
62 | - $mockUser->expects($this->any()) |
|
63 | - ->method('getDisplayName') |
|
64 | - ->willReturn($userId); |
|
65 | - return $mockUser; |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @param null|int $implementedActions |
|
70 | - * @return \PHPUnit\Framework\MockObject\MockObject |
|
71 | - */ |
|
72 | - private function getTestBackend($implementedActions = null) { |
|
73 | - if ($implementedActions === null) { |
|
74 | - $implementedActions = |
|
75 | - GroupInterface::ADD_TO_GROUP | |
|
76 | - GroupInterface::REMOVE_FROM_GOUP | |
|
77 | - GroupInterface::COUNT_USERS | |
|
78 | - GroupInterface::CREATE_GROUP | |
|
79 | - GroupInterface::DELETE_GROUP; |
|
80 | - } |
|
81 | - // need to declare it this way due to optional methods |
|
82 | - // thanks to the implementsActions logic |
|
83 | - $backend = $this->getMockBuilder(TestBackend::class) |
|
84 | - ->disableOriginalConstructor() |
|
85 | - ->onlyMethods([ |
|
86 | - 'getGroupDetails', |
|
87 | - 'implementsActions', |
|
88 | - 'getUserGroups', |
|
89 | - 'inGroup', |
|
90 | - 'getGroups', |
|
91 | - 'groupExists', |
|
92 | - 'groupsExists', |
|
93 | - 'usersInGroup', |
|
94 | - 'createGroup', |
|
95 | - 'addToGroup', |
|
96 | - 'removeFromGroup', |
|
97 | - 'searchInGroup', |
|
98 | - ]) |
|
99 | - ->getMock(); |
|
100 | - $backend->expects($this->any()) |
|
101 | - ->method('implementsActions') |
|
102 | - ->willReturnCallback(function ($actions) use ($implementedActions) { |
|
103 | - return (bool)($actions & $implementedActions); |
|
104 | - }); |
|
105 | - return $backend; |
|
106 | - } |
|
107 | - |
|
108 | - public function testGet(): void { |
|
109 | - /** |
|
110 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
111 | - */ |
|
112 | - $backend = $this->getTestBackend(); |
|
113 | - $backend->expects($this->any()) |
|
114 | - ->method('groupExists') |
|
115 | - ->with('group1') |
|
116 | - ->willReturn(true); |
|
117 | - |
|
118 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
119 | - $manager->addBackend($backend); |
|
120 | - |
|
121 | - $group = $manager->get('group1'); |
|
122 | - $this->assertNotNull($group); |
|
123 | - $this->assertEquals('group1', $group->getGID()); |
|
124 | - } |
|
125 | - |
|
126 | - public function testGetNoBackend(): void { |
|
127 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
128 | - |
|
129 | - $this->assertNull($manager->get('group1')); |
|
130 | - } |
|
131 | - |
|
132 | - public function testGetNotExists(): void { |
|
133 | - /** |
|
134 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
135 | - */ |
|
136 | - $backend = $this->getTestBackend(); |
|
137 | - $backend->expects($this->once()) |
|
138 | - ->method('groupExists') |
|
139 | - ->with('group1') |
|
140 | - ->willReturn(false); |
|
141 | - |
|
142 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
143 | - $manager->addBackend($backend); |
|
144 | - |
|
145 | - $this->assertNull($manager->get('group1')); |
|
146 | - } |
|
147 | - |
|
148 | - public function testGetDeleted(): void { |
|
149 | - $backend = new \Test\Util\Group\Dummy(); |
|
150 | - $backend->createGroup('group1'); |
|
151 | - |
|
152 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
153 | - $manager->addBackend($backend); |
|
154 | - |
|
155 | - $group = $manager->get('group1'); |
|
156 | - $group->delete(); |
|
157 | - $this->assertNull($manager->get('group1')); |
|
158 | - } |
|
159 | - |
|
160 | - public function testGetMultipleBackends(): void { |
|
161 | - /** |
|
162 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1 |
|
163 | - */ |
|
164 | - $backend1 = $this->getTestBackend(); |
|
165 | - $backend1->expects($this->any()) |
|
166 | - ->method('groupExists') |
|
167 | - ->with('group1') |
|
168 | - ->willReturn(false); |
|
169 | - |
|
170 | - /** |
|
171 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2 |
|
172 | - */ |
|
173 | - $backend2 = $this->getTestBackend(); |
|
174 | - $backend2->expects($this->any()) |
|
175 | - ->method('groupExists') |
|
176 | - ->with('group1') |
|
177 | - ->willReturn(true); |
|
178 | - |
|
179 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
180 | - $manager->addBackend($backend1); |
|
181 | - $manager->addBackend($backend2); |
|
182 | - |
|
183 | - $group = $manager->get('group1'); |
|
184 | - $this->assertNotNull($group); |
|
185 | - $this->assertEquals('group1', $group->getGID()); |
|
186 | - } |
|
187 | - |
|
188 | - public function testCreate(): void { |
|
189 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
190 | - $backendGroupCreated = false; |
|
191 | - $backend = $this->getTestBackend(); |
|
192 | - $backend->expects($this->any()) |
|
193 | - ->method('groupExists') |
|
194 | - ->with('group1') |
|
195 | - ->willReturnCallback(function () use (&$backendGroupCreated) { |
|
196 | - return $backendGroupCreated; |
|
197 | - }); |
|
198 | - $backend->expects($this->once()) |
|
199 | - ->method('createGroup') |
|
200 | - ->willReturnCallback(function () use (&$backendGroupCreated) { |
|
201 | - $backendGroupCreated = true; |
|
202 | - return true; |
|
203 | - }); |
|
204 | - |
|
205 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
206 | - $manager->addBackend($backend); |
|
207 | - |
|
208 | - $group = $manager->createGroup('group1'); |
|
209 | - $this->assertEquals('group1', $group->getGID()); |
|
210 | - } |
|
211 | - |
|
212 | - public function testCreateFailure(): void { |
|
213 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
214 | - $backendGroupCreated = false; |
|
215 | - $backend = $this->getTestBackend( |
|
216 | - GroupInterface::ADD_TO_GROUP | |
|
217 | - GroupInterface::REMOVE_FROM_GOUP | |
|
218 | - GroupInterface::COUNT_USERS | |
|
219 | - GroupInterface::CREATE_GROUP | |
|
220 | - GroupInterface::DELETE_GROUP | |
|
221 | - GroupInterface::GROUP_DETAILS |
|
222 | - ); |
|
223 | - $backend->expects($this->any()) |
|
224 | - ->method('groupExists') |
|
225 | - ->with('group1') |
|
226 | - ->willReturn(false); |
|
227 | - $backend->expects($this->once()) |
|
228 | - ->method('createGroup') |
|
229 | - ->willReturn(false); |
|
230 | - $backend->expects($this->once()) |
|
231 | - ->method('getGroupDetails') |
|
232 | - ->willReturn([]); |
|
233 | - |
|
234 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
235 | - $manager->addBackend($backend); |
|
236 | - |
|
237 | - $group = $manager->createGroup('group1'); |
|
238 | - $this->assertEquals(null, $group); |
|
239 | - } |
|
240 | - |
|
241 | - public function testCreateTooLong(): void { |
|
242 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
243 | - $backendGroupCreated = false; |
|
244 | - $backend = $this->getTestBackend( |
|
245 | - GroupInterface::ADD_TO_GROUP | |
|
246 | - GroupInterface::REMOVE_FROM_GOUP | |
|
247 | - GroupInterface::COUNT_USERS | |
|
248 | - GroupInterface::CREATE_GROUP | |
|
249 | - GroupInterface::DELETE_GROUP | |
|
250 | - GroupInterface::GROUP_DETAILS |
|
251 | - ); |
|
252 | - $groupName = str_repeat('x', 256); |
|
253 | - $backend->expects($this->any()) |
|
254 | - ->method('groupExists') |
|
255 | - ->with($groupName) |
|
256 | - ->willReturn(false); |
|
257 | - |
|
258 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
259 | - $manager->addBackend($backend); |
|
260 | - |
|
261 | - $this->expectException(\Exception::class); |
|
262 | - $group = $manager->createGroup($groupName); |
|
263 | - } |
|
264 | - |
|
265 | - public function testCreateExists(): void { |
|
266 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
267 | - $backend = $this->getTestBackend(); |
|
268 | - $backend->expects($this->any()) |
|
269 | - ->method('groupExists') |
|
270 | - ->with('group1') |
|
271 | - ->willReturn(true); |
|
272 | - $backend->expects($this->never()) |
|
273 | - ->method('createGroup'); |
|
274 | - |
|
275 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
276 | - $manager->addBackend($backend); |
|
277 | - |
|
278 | - $group = $manager->createGroup('group1'); |
|
279 | - $this->assertEquals('group1', $group->getGID()); |
|
280 | - } |
|
281 | - |
|
282 | - public function testSearch(): void { |
|
283 | - /** |
|
284 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
285 | - */ |
|
286 | - $backend = $this->getTestBackend(); |
|
287 | - $backend->expects($this->once()) |
|
288 | - ->method('getGroups') |
|
289 | - ->with('1') |
|
290 | - ->willReturn(['group1']); |
|
291 | - $backend->expects($this->once()) |
|
292 | - ->method('getGroupDetails') |
|
293 | - ->willReturnMap([ |
|
294 | - ['group1', ['displayName' => 'group1']], |
|
295 | - ]); |
|
296 | - |
|
297 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
298 | - $manager->addBackend($backend); |
|
299 | - |
|
300 | - $groups = $manager->search('1'); |
|
301 | - $this->assertCount(1, $groups); |
|
302 | - $group1 = reset($groups); |
|
303 | - $this->assertEquals('group1', $group1->getGID()); |
|
304 | - } |
|
305 | - |
|
306 | - public function testSearchMultipleBackends(): void { |
|
307 | - /** |
|
308 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1 |
|
309 | - */ |
|
310 | - $backend1 = $this->getTestBackend(); |
|
311 | - $backend1->expects($this->once()) |
|
312 | - ->method('getGroups') |
|
313 | - ->with('1') |
|
314 | - ->willReturn(['group1']); |
|
315 | - $backend1->expects($this->any()) |
|
316 | - ->method('getGroupDetails') |
|
317 | - ->willReturnMap([ |
|
318 | - ['group1', ['displayName' => 'group1']], |
|
319 | - ['group12', []], |
|
320 | - ]); |
|
321 | - |
|
322 | - /** |
|
323 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2 |
|
324 | - */ |
|
325 | - $backend2 = $this->getTestBackend(); |
|
326 | - $backend2->expects($this->once()) |
|
327 | - ->method('getGroups') |
|
328 | - ->with('1') |
|
329 | - ->willReturn(['group12', 'group1']); |
|
330 | - $backend2->expects($this->any()) |
|
331 | - ->method('getGroupDetails') |
|
332 | - ->willReturnMap([ |
|
333 | - ['group12', ['displayName' => 'group12']], |
|
334 | - ['group1', ['displayName' => 'group1']], |
|
335 | - ]); |
|
336 | - |
|
337 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
338 | - $manager->addBackend($backend1); |
|
339 | - $manager->addBackend($backend2); |
|
340 | - |
|
341 | - $groups = $manager->search('1'); |
|
342 | - $this->assertCount(2, $groups); |
|
343 | - $group1 = reset($groups); |
|
344 | - $group12 = next($groups); |
|
345 | - $this->assertEquals('group1', $group1->getGID()); |
|
346 | - $this->assertEquals('group12', $group12->getGID()); |
|
347 | - } |
|
348 | - |
|
349 | - public function testSearchMultipleBackendsLimitAndOffset(): void { |
|
350 | - /** |
|
351 | - * @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend1 |
|
352 | - */ |
|
353 | - $backend1 = $this->getTestBackend(); |
|
354 | - $backend1->expects($this->once()) |
|
355 | - ->method('getGroups') |
|
356 | - ->with('1', 2, 1) |
|
357 | - ->willReturn(['group1']); |
|
358 | - $backend1->expects($this->any()) |
|
359 | - ->method('getGroupDetails') |
|
360 | - ->willReturnMap([ |
|
361 | - [1, []], |
|
362 | - [2, []], |
|
363 | - ['group1', ['displayName' => 'group1']], |
|
364 | - ['group12', []], |
|
365 | - ]); |
|
366 | - |
|
367 | - /** |
|
368 | - * @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend2 |
|
369 | - */ |
|
370 | - $backend2 = $this->getTestBackend(); |
|
371 | - $backend2->expects($this->once()) |
|
372 | - ->method('getGroups') |
|
373 | - ->with('1', 2, 1) |
|
374 | - ->willReturn(['group12']); |
|
375 | - $backend2->expects($this->any()) |
|
376 | - ->method('getGroupDetails') |
|
377 | - ->willReturnMap([ |
|
378 | - [1, []], |
|
379 | - [2, []], |
|
380 | - ['group1', []], |
|
381 | - ['group12', ['displayName' => 'group12']], |
|
382 | - ]); |
|
383 | - |
|
384 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
385 | - $manager->addBackend($backend1); |
|
386 | - $manager->addBackend($backend2); |
|
387 | - |
|
388 | - $groups = $manager->search('1', 2, 1); |
|
389 | - $this->assertCount(2, $groups); |
|
390 | - $group1 = reset($groups); |
|
391 | - $group12 = next($groups); |
|
392 | - $this->assertEquals('group1', $group1->getGID()); |
|
393 | - $this->assertEquals('group12', $group12->getGID()); |
|
394 | - } |
|
395 | - |
|
396 | - public function testSearchResultExistsButGroupDoesNot(): void { |
|
397 | - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
398 | - $backend = $this->createMock(Database::class); |
|
399 | - $backend->expects($this->once()) |
|
400 | - ->method('getGroups') |
|
401 | - ->with('1') |
|
402 | - ->willReturn(['group1']); |
|
403 | - $backend->expects($this->never()) |
|
404 | - ->method('groupExists'); |
|
405 | - $backend->expects($this->once()) |
|
406 | - ->method('getGroupsDetails') |
|
407 | - ->with(['group1']) |
|
408 | - ->willReturn([]); |
|
409 | - |
|
410 | - /** @var \OC\User\Manager $userManager */ |
|
411 | - $userManager = $this->createMock(Manager::class); |
|
412 | - |
|
413 | - $manager = new \OC\Group\Manager($userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
414 | - $manager->addBackend($backend); |
|
415 | - |
|
416 | - $groups = $manager->search('1'); |
|
417 | - $this->assertEmpty($groups); |
|
418 | - } |
|
419 | - |
|
420 | - public function testGetUserGroups(): void { |
|
421 | - /** |
|
422 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
423 | - */ |
|
424 | - $backend = $this->getTestBackend(); |
|
425 | - $backend->expects($this->once()) |
|
426 | - ->method('getUserGroups') |
|
427 | - ->with('user1') |
|
428 | - ->willReturn(['group1']); |
|
429 | - $backend->expects($this->any()) |
|
430 | - ->method('groupExists') |
|
431 | - ->with('group1') |
|
432 | - ->willReturn(true); |
|
433 | - |
|
434 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
435 | - $manager->addBackend($backend); |
|
436 | - |
|
437 | - $groups = $manager->getUserGroups($this->getTestUser('user1')); |
|
438 | - $this->assertCount(1, $groups); |
|
439 | - $group1 = reset($groups); |
|
440 | - $this->assertEquals('group1', $group1->getGID()); |
|
441 | - } |
|
442 | - |
|
443 | - public function testGetUserGroupIds(): void { |
|
444 | - /** |
|
445 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
446 | - */ |
|
447 | - $backend = $this->getTestBackend(); |
|
448 | - $backend->method('getUserGroups') |
|
449 | - ->with('myUID') |
|
450 | - ->willReturn(['123', 'abc']); |
|
451 | - |
|
452 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
453 | - $manager->addBackend($backend); |
|
454 | - |
|
455 | - /** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */ |
|
456 | - $user = $this->createMock(IUser::class); |
|
457 | - $user->method('getUID') |
|
458 | - ->willReturn('myUID'); |
|
459 | - |
|
460 | - $groups = $manager->getUserGroupIds($user); |
|
461 | - $this->assertCount(2, $groups); |
|
462 | - |
|
463 | - foreach ($groups as $group) { |
|
464 | - $this->assertIsString($group); |
|
465 | - } |
|
466 | - } |
|
467 | - |
|
468 | - public function testGetUserGroupsWithDeletedGroup(): void { |
|
469 | - /** |
|
470 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
471 | - */ |
|
472 | - $backend = $this->createMock(Database::class); |
|
473 | - $backend->expects($this->once()) |
|
474 | - ->method('getUserGroups') |
|
475 | - ->with('user1') |
|
476 | - ->willReturn(['group1']); |
|
477 | - $backend->expects($this->any()) |
|
478 | - ->method('groupExists') |
|
479 | - ->with('group1') |
|
480 | - ->willReturn(false); |
|
481 | - |
|
482 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
483 | - $manager->addBackend($backend); |
|
484 | - |
|
485 | - /** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */ |
|
486 | - $user = $this->createMock(IUser::class); |
|
487 | - $user->expects($this->atLeastOnce()) |
|
488 | - ->method('getUID') |
|
489 | - ->willReturn('user1'); |
|
490 | - |
|
491 | - $groups = $manager->getUserGroups($user); |
|
492 | - $this->assertEmpty($groups); |
|
493 | - } |
|
494 | - |
|
495 | - public function testInGroup(): void { |
|
496 | - /** |
|
497 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
498 | - */ |
|
499 | - $backend = $this->getTestBackend(); |
|
500 | - $backend->expects($this->once()) |
|
501 | - ->method('getUserGroups') |
|
502 | - ->with('user1') |
|
503 | - ->willReturn(['group1', 'admin', 'group2']); |
|
504 | - $backend->expects($this->any()) |
|
505 | - ->method('groupExists') |
|
506 | - ->willReturn(true); |
|
507 | - |
|
508 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
509 | - $manager->addBackend($backend); |
|
510 | - |
|
511 | - $this->assertTrue($manager->isInGroup('user1', 'group1')); |
|
512 | - } |
|
513 | - |
|
514 | - public function testIsAdmin(): void { |
|
515 | - /** |
|
516 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
517 | - */ |
|
518 | - $backend = $this->getTestBackend(); |
|
519 | - $backend->expects($this->once()) |
|
520 | - ->method('getUserGroups') |
|
521 | - ->with('user1') |
|
522 | - ->willReturn(['group1', 'admin', 'group2']); |
|
523 | - $backend->expects($this->any()) |
|
524 | - ->method('groupExists') |
|
525 | - ->willReturn(true); |
|
526 | - |
|
527 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
528 | - $manager->addBackend($backend); |
|
529 | - |
|
530 | - $this->assertTrue($manager->isAdmin('user1')); |
|
531 | - } |
|
532 | - |
|
533 | - public function testNotAdmin(): void { |
|
534 | - /** |
|
535 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
536 | - */ |
|
537 | - $backend = $this->getTestBackend(); |
|
538 | - $backend->expects($this->once()) |
|
539 | - ->method('getUserGroups') |
|
540 | - ->with('user1') |
|
541 | - ->willReturn(['group1', 'group2']); |
|
542 | - $backend->expects($this->any()) |
|
543 | - ->method('groupExists') |
|
544 | - ->willReturn(true); |
|
545 | - |
|
546 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
547 | - $manager->addBackend($backend); |
|
548 | - |
|
549 | - $this->assertFalse($manager->isAdmin('user1')); |
|
550 | - } |
|
551 | - |
|
552 | - public function testGetUserGroupsMultipleBackends(): void { |
|
553 | - /** |
|
554 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1 |
|
555 | - */ |
|
556 | - $backend1 = $this->getTestBackend(); |
|
557 | - $backend1->expects($this->once()) |
|
558 | - ->method('getUserGroups') |
|
559 | - ->with('user1') |
|
560 | - ->willReturn(['group1']); |
|
561 | - $backend1->expects($this->any()) |
|
562 | - ->method('groupExists') |
|
563 | - ->willReturn(true); |
|
564 | - |
|
565 | - /** |
|
566 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2 |
|
567 | - */ |
|
568 | - $backend2 = $this->getTestBackend(); |
|
569 | - $backend2->expects($this->once()) |
|
570 | - ->method('getUserGroups') |
|
571 | - ->with('user1') |
|
572 | - ->willReturn(['group1', 'group2']); |
|
573 | - $backend1->expects($this->any()) |
|
574 | - ->method('groupExists') |
|
575 | - ->willReturn(true); |
|
576 | - |
|
577 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
578 | - $manager->addBackend($backend1); |
|
579 | - $manager->addBackend($backend2); |
|
580 | - |
|
581 | - $groups = $manager->getUserGroups($this->getTestUser('user1')); |
|
582 | - $this->assertCount(2, $groups); |
|
583 | - $group1 = reset($groups); |
|
584 | - $group2 = next($groups); |
|
585 | - $this->assertEquals('group1', $group1->getGID()); |
|
586 | - $this->assertEquals('group2', $group2->getGID()); |
|
587 | - } |
|
588 | - |
|
589 | - public function testDisplayNamesInGroupWithOneUserBackend(): void { |
|
590 | - /** |
|
591 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
592 | - */ |
|
593 | - $backend = $this->getTestBackend(); |
|
594 | - $backend->expects($this->exactly(1)) |
|
595 | - ->method('groupExists') |
|
596 | - ->with('testgroup') |
|
597 | - ->willReturn(true); |
|
598 | - |
|
599 | - $backend->expects($this->any()) |
|
600 | - ->method('inGroup') |
|
601 | - ->willReturnCallback(function ($uid, $gid) { |
|
602 | - switch ($uid) { |
|
603 | - case 'user1': return false; |
|
604 | - case 'user2': return true; |
|
605 | - case 'user3': return false; |
|
606 | - case 'user33': return true; |
|
607 | - default: |
|
608 | - return null; |
|
609 | - } |
|
610 | - }); |
|
611 | - |
|
612 | - $this->userManager->expects($this->any()) |
|
613 | - ->method('searchDisplayName') |
|
614 | - ->with('user3') |
|
615 | - ->willReturnCallback(function ($search, $limit, $offset) { |
|
616 | - switch ($offset) { |
|
617 | - case 0: return ['user3' => $this->getTestUser('user3'), |
|
618 | - 'user33' => $this->getTestUser('user33')]; |
|
619 | - case 2: return []; |
|
620 | - } |
|
621 | - return null; |
|
622 | - }); |
|
623 | - $this->userManager->expects($this->any()) |
|
624 | - ->method('get') |
|
625 | - ->willReturnCallback(function ($uid) { |
|
626 | - switch ($uid) { |
|
627 | - case 'user1': return $this->getTestUser('user1'); |
|
628 | - case 'user2': return $this->getTestUser('user2'); |
|
629 | - case 'user3': return $this->getTestUser('user3'); |
|
630 | - case 'user33': return $this->getTestUser('user33'); |
|
631 | - default: |
|
632 | - return null; |
|
633 | - } |
|
634 | - }); |
|
635 | - |
|
636 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
637 | - $manager->addBackend($backend); |
|
638 | - |
|
639 | - $users = $manager->displayNamesInGroup('testgroup', 'user3'); |
|
640 | - $this->assertCount(1, $users); |
|
641 | - $this->assertFalse(isset($users['user1'])); |
|
642 | - $this->assertFalse(isset($users['user2'])); |
|
643 | - $this->assertFalse(isset($users['user3'])); |
|
644 | - $this->assertTrue(isset($users['user33'])); |
|
645 | - } |
|
646 | - |
|
647 | - public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified(): void { |
|
648 | - /** |
|
649 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
650 | - */ |
|
651 | - $backend = $this->getTestBackend(); |
|
652 | - $backend->expects($this->exactly(1)) |
|
653 | - ->method('groupExists') |
|
654 | - ->with('testgroup') |
|
655 | - ->willReturn(true); |
|
656 | - |
|
657 | - $backend->expects($this->any()) |
|
658 | - ->method('inGroup') |
|
659 | - ->willReturnCallback(function ($uid, $gid) { |
|
660 | - switch ($uid) { |
|
661 | - case 'user1': return false; |
|
662 | - case 'user2': return true; |
|
663 | - case 'user3': return false; |
|
664 | - case 'user33': return true; |
|
665 | - case 'user333': return true; |
|
666 | - default: |
|
667 | - return null; |
|
668 | - } |
|
669 | - }); |
|
670 | - |
|
671 | - $this->userManager->expects($this->any()) |
|
672 | - ->method('searchDisplayName') |
|
673 | - ->with('user3') |
|
674 | - ->willReturnCallback(function ($search, $limit, $offset) { |
|
675 | - switch ($offset) { |
|
676 | - case 0: return ['user3' => $this->getTestUser('user3'), |
|
677 | - 'user33' => $this->getTestUser('user33')]; |
|
678 | - case 2: return ['user333' => $this->getTestUser('user333')]; |
|
679 | - } |
|
680 | - return null; |
|
681 | - }); |
|
682 | - $this->userManager->expects($this->any()) |
|
683 | - ->method('get') |
|
684 | - ->willReturnCallback(function ($uid) { |
|
685 | - switch ($uid) { |
|
686 | - case 'user1': return $this->getTestUser('user1'); |
|
687 | - case 'user2': return $this->getTestUser('user2'); |
|
688 | - case 'user3': return $this->getTestUser('user3'); |
|
689 | - case 'user33': return $this->getTestUser('user33'); |
|
690 | - case 'user333': return $this->getTestUser('user333'); |
|
691 | - default: |
|
692 | - return null; |
|
693 | - } |
|
694 | - }); |
|
695 | - |
|
696 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
697 | - $manager->addBackend($backend); |
|
698 | - |
|
699 | - $users = $manager->displayNamesInGroup('testgroup', 'user3', 1); |
|
700 | - $this->assertCount(1, $users); |
|
701 | - $this->assertFalse(isset($users['user1'])); |
|
702 | - $this->assertFalse(isset($users['user2'])); |
|
703 | - $this->assertFalse(isset($users['user3'])); |
|
704 | - $this->assertTrue(isset($users['user33'])); |
|
705 | - $this->assertFalse(isset($users['user333'])); |
|
706 | - } |
|
707 | - |
|
708 | - public function testDisplayNamesInGroupWithOneUserBackendWithLimitAndOffsetSpecified(): void { |
|
709 | - /** |
|
710 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
711 | - */ |
|
712 | - $backend = $this->getTestBackend(); |
|
713 | - $backend->expects($this->exactly(1)) |
|
714 | - ->method('groupExists') |
|
715 | - ->with('testgroup') |
|
716 | - ->willReturn(true); |
|
717 | - |
|
718 | - $backend->expects($this->any()) |
|
719 | - ->method('inGroup') |
|
720 | - ->willReturnCallback(function ($uid) { |
|
721 | - switch ($uid) { |
|
722 | - case 'user1': return false; |
|
723 | - case 'user2': return true; |
|
724 | - case 'user3': return false; |
|
725 | - case 'user33': return true; |
|
726 | - case 'user333': return true; |
|
727 | - default: |
|
728 | - return null; |
|
729 | - } |
|
730 | - }); |
|
731 | - |
|
732 | - $this->userManager->expects($this->any()) |
|
733 | - ->method('searchDisplayName') |
|
734 | - ->with('user3') |
|
735 | - ->willReturnCallback(function ($search, $limit, $offset) { |
|
736 | - switch ($offset) { |
|
737 | - case 0: |
|
738 | - return [ |
|
739 | - 'user3' => $this->getTestUser('user3'), |
|
740 | - 'user33' => $this->getTestUser('user33'), |
|
741 | - 'user333' => $this->getTestUser('user333') |
|
742 | - ]; |
|
743 | - } |
|
744 | - return null; |
|
745 | - }); |
|
746 | - $this->userManager->expects($this->any()) |
|
747 | - ->method('get') |
|
748 | - ->willReturnCallback(function ($uid) { |
|
749 | - switch ($uid) { |
|
750 | - case 'user1': return $this->getTestUser('user1'); |
|
751 | - case 'user2': return $this->getTestUser('user2'); |
|
752 | - case 'user3': return $this->getTestUser('user3'); |
|
753 | - case 'user33': return $this->getTestUser('user33'); |
|
754 | - case 'user333': return $this->getTestUser('user333'); |
|
755 | - default: |
|
756 | - return null; |
|
757 | - } |
|
758 | - }); |
|
759 | - |
|
760 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
761 | - $manager->addBackend($backend); |
|
762 | - |
|
763 | - $users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1); |
|
764 | - $this->assertCount(1, $users); |
|
765 | - $this->assertFalse(isset($users['user1'])); |
|
766 | - $this->assertFalse(isset($users['user2'])); |
|
767 | - $this->assertFalse(isset($users['user3'])); |
|
768 | - $this->assertFalse(isset($users['user33'])); |
|
769 | - $this->assertTrue(isset($users['user333'])); |
|
770 | - } |
|
771 | - |
|
772 | - public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty(): void { |
|
773 | - /** |
|
774 | - * @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend |
|
775 | - */ |
|
776 | - $backend = $this->getTestBackend(); |
|
777 | - $backend->expects($this->exactly(1)) |
|
778 | - ->method('groupExists') |
|
779 | - ->with('testgroup') |
|
780 | - ->willReturn(true); |
|
781 | - |
|
782 | - $backend->expects($this->once()) |
|
783 | - ->method('searchInGroup') |
|
784 | - ->with('testgroup', '', -1, 0) |
|
785 | - ->willReturn(['user2' => $this->getTestUser('user2'), 'user33' => $this->getTestUser('user33')]); |
|
786 | - |
|
787 | - $this->userManager->expects($this->never())->method('get'); |
|
788 | - |
|
789 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
790 | - $manager->addBackend($backend); |
|
791 | - |
|
792 | - $users = $manager->displayNamesInGroup('testgroup', ''); |
|
793 | - $this->assertCount(2, $users); |
|
794 | - $this->assertFalse(isset($users['user1'])); |
|
795 | - $this->assertTrue(isset($users['user2'])); |
|
796 | - $this->assertFalse(isset($users['user3'])); |
|
797 | - $this->assertTrue(isset($users['user33'])); |
|
798 | - } |
|
799 | - |
|
800 | - public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified(): void { |
|
801 | - /** |
|
802 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
803 | - */ |
|
804 | - $backend = $this->getTestBackend(); |
|
805 | - $backend->expects($this->exactly(1)) |
|
806 | - ->method('groupExists') |
|
807 | - ->with('testgroup') |
|
808 | - ->willReturn(true); |
|
809 | - |
|
810 | - $backend->expects($this->once()) |
|
811 | - ->method('searchInGroup') |
|
812 | - ->with('testgroup', '', 1, 0) |
|
813 | - ->willReturn([new User('user2', null, $this->dispatcher)]); |
|
814 | - |
|
815 | - $this->userManager->expects($this->never())->method('get'); |
|
816 | - |
|
817 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
818 | - $manager->addBackend($backend); |
|
819 | - |
|
820 | - $users = $manager->displayNamesInGroup('testgroup', '', 1); |
|
821 | - $this->assertCount(1, $users); |
|
822 | - $this->assertFalse(isset($users['user1'])); |
|
823 | - $this->assertTrue(isset($users['user2'])); |
|
824 | - $this->assertFalse(isset($users['user3'])); |
|
825 | - $this->assertFalse(isset($users['user33'])); |
|
826 | - } |
|
827 | - |
|
828 | - public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified(): void { |
|
829 | - /** |
|
830 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
831 | - */ |
|
832 | - $backend = $this->getTestBackend(); |
|
833 | - $backend->expects($this->exactly(1)) |
|
834 | - ->method('groupExists') |
|
835 | - ->with('testgroup') |
|
836 | - ->willReturn(true); |
|
837 | - |
|
838 | - $backend->expects($this->once()) |
|
839 | - ->method('searchInGroup') |
|
840 | - ->with('testgroup', '', 1, 1) |
|
841 | - ->willReturn(['user33' => $this->getTestUser('user33')]); |
|
842 | - |
|
843 | - $this->userManager->expects($this->never())->method('get'); |
|
844 | - |
|
845 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
846 | - $manager->addBackend($backend); |
|
847 | - |
|
848 | - $users = $manager->displayNamesInGroup('testgroup', '', 1, 1); |
|
849 | - $this->assertCount(1, $users); |
|
850 | - $this->assertFalse(isset($users['user1'])); |
|
851 | - $this->assertFalse(isset($users['user2'])); |
|
852 | - $this->assertFalse(isset($users['user3'])); |
|
853 | - $this->assertTrue(isset($users['user33'])); |
|
854 | - } |
|
855 | - |
|
856 | - public function testGetUserGroupsWithAddUser(): void { |
|
857 | - /** |
|
858 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
859 | - */ |
|
860 | - $backend = $this->getTestBackend(); |
|
861 | - $expectedGroups = []; |
|
862 | - $backend->expects($this->any()) |
|
863 | - ->method('getUserGroups') |
|
864 | - ->with('user1') |
|
865 | - ->willReturnCallback(function () use (&$expectedGroups) { |
|
866 | - return $expectedGroups; |
|
867 | - }); |
|
868 | - $backend->expects($this->any()) |
|
869 | - ->method('groupExists') |
|
870 | - ->with('group1') |
|
871 | - ->willReturn(true); |
|
872 | - |
|
873 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
874 | - $manager->addBackend($backend); |
|
875 | - |
|
876 | - // prime cache |
|
877 | - $user1 = $this->getTestUser('user1'); |
|
878 | - $groups = $manager->getUserGroups($user1); |
|
879 | - $this->assertEquals([], $groups); |
|
880 | - |
|
881 | - // add user |
|
882 | - $group = $manager->get('group1'); |
|
883 | - $group->addUser($user1); |
|
884 | - $expectedGroups[] = 'group1'; |
|
885 | - |
|
886 | - // check result |
|
887 | - $groups = $manager->getUserGroups($user1); |
|
888 | - $this->assertCount(1, $groups); |
|
889 | - $group1 = reset($groups); |
|
890 | - $this->assertEquals('group1', $group1->getGID()); |
|
891 | - } |
|
892 | - |
|
893 | - public function testGetUserGroupsWithRemoveUser(): void { |
|
894 | - /** |
|
895 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
896 | - */ |
|
897 | - $backend = $this->getTestBackend(); |
|
898 | - $expectedGroups = ['group1']; |
|
899 | - $backend->expects($this->any()) |
|
900 | - ->method('getUserGroups') |
|
901 | - ->with('user1') |
|
902 | - ->willReturnCallback(function () use (&$expectedGroups) { |
|
903 | - return $expectedGroups; |
|
904 | - }); |
|
905 | - $backend->expects($this->any()) |
|
906 | - ->method('groupExists') |
|
907 | - ->with('group1') |
|
908 | - ->willReturn(true); |
|
909 | - $backend->expects($this->once()) |
|
910 | - ->method('inGroup') |
|
911 | - ->willReturn(true); |
|
912 | - $backend->expects($this->once()) |
|
913 | - ->method('removeFromGroup') |
|
914 | - ->willReturn(true); |
|
915 | - |
|
916 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
917 | - $manager->addBackend($backend); |
|
918 | - |
|
919 | - // prime cache |
|
920 | - $user1 = $this->getTestUser('user1'); |
|
921 | - $groups = $manager->getUserGroups($user1); |
|
922 | - $this->assertCount(1, $groups); |
|
923 | - $group1 = reset($groups); |
|
924 | - $this->assertEquals('group1', $group1->getGID()); |
|
925 | - |
|
926 | - // remove user |
|
927 | - $group = $manager->get('group1'); |
|
928 | - $group->removeUser($user1); |
|
929 | - $expectedGroups = []; |
|
930 | - |
|
931 | - // check result |
|
932 | - $groups = $manager->getUserGroups($user1); |
|
933 | - $this->assertEquals($expectedGroups, $groups); |
|
934 | - } |
|
935 | - |
|
936 | - public function testGetUserIdGroups(): void { |
|
937 | - /** |
|
938 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
939 | - */ |
|
940 | - $backend = $this->getTestBackend(); |
|
941 | - $backend->expects($this->any()) |
|
942 | - ->method('getUserGroups') |
|
943 | - ->with('user1') |
|
944 | - ->willReturn(null); |
|
945 | - |
|
946 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
947 | - $manager->addBackend($backend); |
|
948 | - |
|
949 | - $groups = $manager->getUserIdGroups('user1'); |
|
950 | - $this->assertEquals([], $groups); |
|
951 | - } |
|
952 | - |
|
953 | - public function testGroupDisplayName(): void { |
|
954 | - /** |
|
955 | - * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
956 | - */ |
|
957 | - $backend = $this->getTestBackend( |
|
958 | - GroupInterface::ADD_TO_GROUP | |
|
959 | - GroupInterface::REMOVE_FROM_GOUP | |
|
960 | - GroupInterface::COUNT_USERS | |
|
961 | - GroupInterface::CREATE_GROUP | |
|
962 | - GroupInterface::DELETE_GROUP | |
|
963 | - GroupInterface::GROUP_DETAILS |
|
964 | - ); |
|
965 | - $backend->expects($this->any()) |
|
966 | - ->method('getGroupDetails') |
|
967 | - ->willReturnMap([ |
|
968 | - ['group1', ['gid' => 'group1', 'displayName' => 'Group One']], |
|
969 | - ['group2', ['gid' => 'group2']], |
|
970 | - ]); |
|
971 | - $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
972 | - $manager->addBackend($backend); |
|
973 | - |
|
974 | - // group with display name |
|
975 | - $group = $manager->get('group1'); |
|
976 | - $this->assertNotNull($group); |
|
977 | - $this->assertEquals('group1', $group->getGID()); |
|
978 | - $this->assertEquals('Group One', $group->getDisplayName()); |
|
979 | - |
|
980 | - // group without display name |
|
981 | - $group = $manager->get('group2'); |
|
982 | - $this->assertNotNull($group); |
|
983 | - $this->assertEquals('group2', $group->getGID()); |
|
984 | - $this->assertEquals('group2', $group->getDisplayName()); |
|
985 | - } |
|
34 | + /** @var Manager|MockObject */ |
|
35 | + protected $userManager; |
|
36 | + /** @var IEventDispatcher|MockObject */ |
|
37 | + protected $dispatcher; |
|
38 | + /** @var LoggerInterface|MockObject */ |
|
39 | + protected $logger; |
|
40 | + /** @var ICacheFactory|MockObject */ |
|
41 | + private $cache; |
|
42 | + /** @var IRemoteAddress|MockObject */ |
|
43 | + private $remoteIpAddress; |
|
44 | + |
|
45 | + protected function setUp(): void { |
|
46 | + parent::setUp(); |
|
47 | + |
|
48 | + $this->userManager = $this->createMock(Manager::class); |
|
49 | + $this->dispatcher = $this->createMock(IEventDispatcher::class); |
|
50 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
51 | + $this->cache = $this->createMock(ICacheFactory::class); |
|
52 | + |
|
53 | + $this->remoteIpAddress = $this->createMock(IRemoteAddress::class); |
|
54 | + $this->remoteIpAddress->method('allowsAdminActions')->willReturn(true); |
|
55 | + } |
|
56 | + |
|
57 | + private function getTestUser($userId) { |
|
58 | + $mockUser = $this->createMock(IUser::class); |
|
59 | + $mockUser->expects($this->any()) |
|
60 | + ->method('getUID') |
|
61 | + ->willReturn($userId); |
|
62 | + $mockUser->expects($this->any()) |
|
63 | + ->method('getDisplayName') |
|
64 | + ->willReturn($userId); |
|
65 | + return $mockUser; |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @param null|int $implementedActions |
|
70 | + * @return \PHPUnit\Framework\MockObject\MockObject |
|
71 | + */ |
|
72 | + private function getTestBackend($implementedActions = null) { |
|
73 | + if ($implementedActions === null) { |
|
74 | + $implementedActions = |
|
75 | + GroupInterface::ADD_TO_GROUP | |
|
76 | + GroupInterface::REMOVE_FROM_GOUP | |
|
77 | + GroupInterface::COUNT_USERS | |
|
78 | + GroupInterface::CREATE_GROUP | |
|
79 | + GroupInterface::DELETE_GROUP; |
|
80 | + } |
|
81 | + // need to declare it this way due to optional methods |
|
82 | + // thanks to the implementsActions logic |
|
83 | + $backend = $this->getMockBuilder(TestBackend::class) |
|
84 | + ->disableOriginalConstructor() |
|
85 | + ->onlyMethods([ |
|
86 | + 'getGroupDetails', |
|
87 | + 'implementsActions', |
|
88 | + 'getUserGroups', |
|
89 | + 'inGroup', |
|
90 | + 'getGroups', |
|
91 | + 'groupExists', |
|
92 | + 'groupsExists', |
|
93 | + 'usersInGroup', |
|
94 | + 'createGroup', |
|
95 | + 'addToGroup', |
|
96 | + 'removeFromGroup', |
|
97 | + 'searchInGroup', |
|
98 | + ]) |
|
99 | + ->getMock(); |
|
100 | + $backend->expects($this->any()) |
|
101 | + ->method('implementsActions') |
|
102 | + ->willReturnCallback(function ($actions) use ($implementedActions) { |
|
103 | + return (bool)($actions & $implementedActions); |
|
104 | + }); |
|
105 | + return $backend; |
|
106 | + } |
|
107 | + |
|
108 | + public function testGet(): void { |
|
109 | + /** |
|
110 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
111 | + */ |
|
112 | + $backend = $this->getTestBackend(); |
|
113 | + $backend->expects($this->any()) |
|
114 | + ->method('groupExists') |
|
115 | + ->with('group1') |
|
116 | + ->willReturn(true); |
|
117 | + |
|
118 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
119 | + $manager->addBackend($backend); |
|
120 | + |
|
121 | + $group = $manager->get('group1'); |
|
122 | + $this->assertNotNull($group); |
|
123 | + $this->assertEquals('group1', $group->getGID()); |
|
124 | + } |
|
125 | + |
|
126 | + public function testGetNoBackend(): void { |
|
127 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
128 | + |
|
129 | + $this->assertNull($manager->get('group1')); |
|
130 | + } |
|
131 | + |
|
132 | + public function testGetNotExists(): void { |
|
133 | + /** |
|
134 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
135 | + */ |
|
136 | + $backend = $this->getTestBackend(); |
|
137 | + $backend->expects($this->once()) |
|
138 | + ->method('groupExists') |
|
139 | + ->with('group1') |
|
140 | + ->willReturn(false); |
|
141 | + |
|
142 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
143 | + $manager->addBackend($backend); |
|
144 | + |
|
145 | + $this->assertNull($manager->get('group1')); |
|
146 | + } |
|
147 | + |
|
148 | + public function testGetDeleted(): void { |
|
149 | + $backend = new \Test\Util\Group\Dummy(); |
|
150 | + $backend->createGroup('group1'); |
|
151 | + |
|
152 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
153 | + $manager->addBackend($backend); |
|
154 | + |
|
155 | + $group = $manager->get('group1'); |
|
156 | + $group->delete(); |
|
157 | + $this->assertNull($manager->get('group1')); |
|
158 | + } |
|
159 | + |
|
160 | + public function testGetMultipleBackends(): void { |
|
161 | + /** |
|
162 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1 |
|
163 | + */ |
|
164 | + $backend1 = $this->getTestBackend(); |
|
165 | + $backend1->expects($this->any()) |
|
166 | + ->method('groupExists') |
|
167 | + ->with('group1') |
|
168 | + ->willReturn(false); |
|
169 | + |
|
170 | + /** |
|
171 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2 |
|
172 | + */ |
|
173 | + $backend2 = $this->getTestBackend(); |
|
174 | + $backend2->expects($this->any()) |
|
175 | + ->method('groupExists') |
|
176 | + ->with('group1') |
|
177 | + ->willReturn(true); |
|
178 | + |
|
179 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
180 | + $manager->addBackend($backend1); |
|
181 | + $manager->addBackend($backend2); |
|
182 | + |
|
183 | + $group = $manager->get('group1'); |
|
184 | + $this->assertNotNull($group); |
|
185 | + $this->assertEquals('group1', $group->getGID()); |
|
186 | + } |
|
187 | + |
|
188 | + public function testCreate(): void { |
|
189 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
190 | + $backendGroupCreated = false; |
|
191 | + $backend = $this->getTestBackend(); |
|
192 | + $backend->expects($this->any()) |
|
193 | + ->method('groupExists') |
|
194 | + ->with('group1') |
|
195 | + ->willReturnCallback(function () use (&$backendGroupCreated) { |
|
196 | + return $backendGroupCreated; |
|
197 | + }); |
|
198 | + $backend->expects($this->once()) |
|
199 | + ->method('createGroup') |
|
200 | + ->willReturnCallback(function () use (&$backendGroupCreated) { |
|
201 | + $backendGroupCreated = true; |
|
202 | + return true; |
|
203 | + }); |
|
204 | + |
|
205 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
206 | + $manager->addBackend($backend); |
|
207 | + |
|
208 | + $group = $manager->createGroup('group1'); |
|
209 | + $this->assertEquals('group1', $group->getGID()); |
|
210 | + } |
|
211 | + |
|
212 | + public function testCreateFailure(): void { |
|
213 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
214 | + $backendGroupCreated = false; |
|
215 | + $backend = $this->getTestBackend( |
|
216 | + GroupInterface::ADD_TO_GROUP | |
|
217 | + GroupInterface::REMOVE_FROM_GOUP | |
|
218 | + GroupInterface::COUNT_USERS | |
|
219 | + GroupInterface::CREATE_GROUP | |
|
220 | + GroupInterface::DELETE_GROUP | |
|
221 | + GroupInterface::GROUP_DETAILS |
|
222 | + ); |
|
223 | + $backend->expects($this->any()) |
|
224 | + ->method('groupExists') |
|
225 | + ->with('group1') |
|
226 | + ->willReturn(false); |
|
227 | + $backend->expects($this->once()) |
|
228 | + ->method('createGroup') |
|
229 | + ->willReturn(false); |
|
230 | + $backend->expects($this->once()) |
|
231 | + ->method('getGroupDetails') |
|
232 | + ->willReturn([]); |
|
233 | + |
|
234 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
235 | + $manager->addBackend($backend); |
|
236 | + |
|
237 | + $group = $manager->createGroup('group1'); |
|
238 | + $this->assertEquals(null, $group); |
|
239 | + } |
|
240 | + |
|
241 | + public function testCreateTooLong(): void { |
|
242 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
243 | + $backendGroupCreated = false; |
|
244 | + $backend = $this->getTestBackend( |
|
245 | + GroupInterface::ADD_TO_GROUP | |
|
246 | + GroupInterface::REMOVE_FROM_GOUP | |
|
247 | + GroupInterface::COUNT_USERS | |
|
248 | + GroupInterface::CREATE_GROUP | |
|
249 | + GroupInterface::DELETE_GROUP | |
|
250 | + GroupInterface::GROUP_DETAILS |
|
251 | + ); |
|
252 | + $groupName = str_repeat('x', 256); |
|
253 | + $backend->expects($this->any()) |
|
254 | + ->method('groupExists') |
|
255 | + ->with($groupName) |
|
256 | + ->willReturn(false); |
|
257 | + |
|
258 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
259 | + $manager->addBackend($backend); |
|
260 | + |
|
261 | + $this->expectException(\Exception::class); |
|
262 | + $group = $manager->createGroup($groupName); |
|
263 | + } |
|
264 | + |
|
265 | + public function testCreateExists(): void { |
|
266 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
267 | + $backend = $this->getTestBackend(); |
|
268 | + $backend->expects($this->any()) |
|
269 | + ->method('groupExists') |
|
270 | + ->with('group1') |
|
271 | + ->willReturn(true); |
|
272 | + $backend->expects($this->never()) |
|
273 | + ->method('createGroup'); |
|
274 | + |
|
275 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
276 | + $manager->addBackend($backend); |
|
277 | + |
|
278 | + $group = $manager->createGroup('group1'); |
|
279 | + $this->assertEquals('group1', $group->getGID()); |
|
280 | + } |
|
281 | + |
|
282 | + public function testSearch(): void { |
|
283 | + /** |
|
284 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
285 | + */ |
|
286 | + $backend = $this->getTestBackend(); |
|
287 | + $backend->expects($this->once()) |
|
288 | + ->method('getGroups') |
|
289 | + ->with('1') |
|
290 | + ->willReturn(['group1']); |
|
291 | + $backend->expects($this->once()) |
|
292 | + ->method('getGroupDetails') |
|
293 | + ->willReturnMap([ |
|
294 | + ['group1', ['displayName' => 'group1']], |
|
295 | + ]); |
|
296 | + |
|
297 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
298 | + $manager->addBackend($backend); |
|
299 | + |
|
300 | + $groups = $manager->search('1'); |
|
301 | + $this->assertCount(1, $groups); |
|
302 | + $group1 = reset($groups); |
|
303 | + $this->assertEquals('group1', $group1->getGID()); |
|
304 | + } |
|
305 | + |
|
306 | + public function testSearchMultipleBackends(): void { |
|
307 | + /** |
|
308 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1 |
|
309 | + */ |
|
310 | + $backend1 = $this->getTestBackend(); |
|
311 | + $backend1->expects($this->once()) |
|
312 | + ->method('getGroups') |
|
313 | + ->with('1') |
|
314 | + ->willReturn(['group1']); |
|
315 | + $backend1->expects($this->any()) |
|
316 | + ->method('getGroupDetails') |
|
317 | + ->willReturnMap([ |
|
318 | + ['group1', ['displayName' => 'group1']], |
|
319 | + ['group12', []], |
|
320 | + ]); |
|
321 | + |
|
322 | + /** |
|
323 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2 |
|
324 | + */ |
|
325 | + $backend2 = $this->getTestBackend(); |
|
326 | + $backend2->expects($this->once()) |
|
327 | + ->method('getGroups') |
|
328 | + ->with('1') |
|
329 | + ->willReturn(['group12', 'group1']); |
|
330 | + $backend2->expects($this->any()) |
|
331 | + ->method('getGroupDetails') |
|
332 | + ->willReturnMap([ |
|
333 | + ['group12', ['displayName' => 'group12']], |
|
334 | + ['group1', ['displayName' => 'group1']], |
|
335 | + ]); |
|
336 | + |
|
337 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
338 | + $manager->addBackend($backend1); |
|
339 | + $manager->addBackend($backend2); |
|
340 | + |
|
341 | + $groups = $manager->search('1'); |
|
342 | + $this->assertCount(2, $groups); |
|
343 | + $group1 = reset($groups); |
|
344 | + $group12 = next($groups); |
|
345 | + $this->assertEquals('group1', $group1->getGID()); |
|
346 | + $this->assertEquals('group12', $group12->getGID()); |
|
347 | + } |
|
348 | + |
|
349 | + public function testSearchMultipleBackendsLimitAndOffset(): void { |
|
350 | + /** |
|
351 | + * @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend1 |
|
352 | + */ |
|
353 | + $backend1 = $this->getTestBackend(); |
|
354 | + $backend1->expects($this->once()) |
|
355 | + ->method('getGroups') |
|
356 | + ->with('1', 2, 1) |
|
357 | + ->willReturn(['group1']); |
|
358 | + $backend1->expects($this->any()) |
|
359 | + ->method('getGroupDetails') |
|
360 | + ->willReturnMap([ |
|
361 | + [1, []], |
|
362 | + [2, []], |
|
363 | + ['group1', ['displayName' => 'group1']], |
|
364 | + ['group12', []], |
|
365 | + ]); |
|
366 | + |
|
367 | + /** |
|
368 | + * @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend2 |
|
369 | + */ |
|
370 | + $backend2 = $this->getTestBackend(); |
|
371 | + $backend2->expects($this->once()) |
|
372 | + ->method('getGroups') |
|
373 | + ->with('1', 2, 1) |
|
374 | + ->willReturn(['group12']); |
|
375 | + $backend2->expects($this->any()) |
|
376 | + ->method('getGroupDetails') |
|
377 | + ->willReturnMap([ |
|
378 | + [1, []], |
|
379 | + [2, []], |
|
380 | + ['group1', []], |
|
381 | + ['group12', ['displayName' => 'group12']], |
|
382 | + ]); |
|
383 | + |
|
384 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
385 | + $manager->addBackend($backend1); |
|
386 | + $manager->addBackend($backend2); |
|
387 | + |
|
388 | + $groups = $manager->search('1', 2, 1); |
|
389 | + $this->assertCount(2, $groups); |
|
390 | + $group1 = reset($groups); |
|
391 | + $group12 = next($groups); |
|
392 | + $this->assertEquals('group1', $group1->getGID()); |
|
393 | + $this->assertEquals('group12', $group12->getGID()); |
|
394 | + } |
|
395 | + |
|
396 | + public function testSearchResultExistsButGroupDoesNot(): void { |
|
397 | + /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */ |
|
398 | + $backend = $this->createMock(Database::class); |
|
399 | + $backend->expects($this->once()) |
|
400 | + ->method('getGroups') |
|
401 | + ->with('1') |
|
402 | + ->willReturn(['group1']); |
|
403 | + $backend->expects($this->never()) |
|
404 | + ->method('groupExists'); |
|
405 | + $backend->expects($this->once()) |
|
406 | + ->method('getGroupsDetails') |
|
407 | + ->with(['group1']) |
|
408 | + ->willReturn([]); |
|
409 | + |
|
410 | + /** @var \OC\User\Manager $userManager */ |
|
411 | + $userManager = $this->createMock(Manager::class); |
|
412 | + |
|
413 | + $manager = new \OC\Group\Manager($userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
414 | + $manager->addBackend($backend); |
|
415 | + |
|
416 | + $groups = $manager->search('1'); |
|
417 | + $this->assertEmpty($groups); |
|
418 | + } |
|
419 | + |
|
420 | + public function testGetUserGroups(): void { |
|
421 | + /** |
|
422 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
423 | + */ |
|
424 | + $backend = $this->getTestBackend(); |
|
425 | + $backend->expects($this->once()) |
|
426 | + ->method('getUserGroups') |
|
427 | + ->with('user1') |
|
428 | + ->willReturn(['group1']); |
|
429 | + $backend->expects($this->any()) |
|
430 | + ->method('groupExists') |
|
431 | + ->with('group1') |
|
432 | + ->willReturn(true); |
|
433 | + |
|
434 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
435 | + $manager->addBackend($backend); |
|
436 | + |
|
437 | + $groups = $manager->getUserGroups($this->getTestUser('user1')); |
|
438 | + $this->assertCount(1, $groups); |
|
439 | + $group1 = reset($groups); |
|
440 | + $this->assertEquals('group1', $group1->getGID()); |
|
441 | + } |
|
442 | + |
|
443 | + public function testGetUserGroupIds(): void { |
|
444 | + /** |
|
445 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
446 | + */ |
|
447 | + $backend = $this->getTestBackend(); |
|
448 | + $backend->method('getUserGroups') |
|
449 | + ->with('myUID') |
|
450 | + ->willReturn(['123', 'abc']); |
|
451 | + |
|
452 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
453 | + $manager->addBackend($backend); |
|
454 | + |
|
455 | + /** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */ |
|
456 | + $user = $this->createMock(IUser::class); |
|
457 | + $user->method('getUID') |
|
458 | + ->willReturn('myUID'); |
|
459 | + |
|
460 | + $groups = $manager->getUserGroupIds($user); |
|
461 | + $this->assertCount(2, $groups); |
|
462 | + |
|
463 | + foreach ($groups as $group) { |
|
464 | + $this->assertIsString($group); |
|
465 | + } |
|
466 | + } |
|
467 | + |
|
468 | + public function testGetUserGroupsWithDeletedGroup(): void { |
|
469 | + /** |
|
470 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
471 | + */ |
|
472 | + $backend = $this->createMock(Database::class); |
|
473 | + $backend->expects($this->once()) |
|
474 | + ->method('getUserGroups') |
|
475 | + ->with('user1') |
|
476 | + ->willReturn(['group1']); |
|
477 | + $backend->expects($this->any()) |
|
478 | + ->method('groupExists') |
|
479 | + ->with('group1') |
|
480 | + ->willReturn(false); |
|
481 | + |
|
482 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
483 | + $manager->addBackend($backend); |
|
484 | + |
|
485 | + /** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */ |
|
486 | + $user = $this->createMock(IUser::class); |
|
487 | + $user->expects($this->atLeastOnce()) |
|
488 | + ->method('getUID') |
|
489 | + ->willReturn('user1'); |
|
490 | + |
|
491 | + $groups = $manager->getUserGroups($user); |
|
492 | + $this->assertEmpty($groups); |
|
493 | + } |
|
494 | + |
|
495 | + public function testInGroup(): void { |
|
496 | + /** |
|
497 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
498 | + */ |
|
499 | + $backend = $this->getTestBackend(); |
|
500 | + $backend->expects($this->once()) |
|
501 | + ->method('getUserGroups') |
|
502 | + ->with('user1') |
|
503 | + ->willReturn(['group1', 'admin', 'group2']); |
|
504 | + $backend->expects($this->any()) |
|
505 | + ->method('groupExists') |
|
506 | + ->willReturn(true); |
|
507 | + |
|
508 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
509 | + $manager->addBackend($backend); |
|
510 | + |
|
511 | + $this->assertTrue($manager->isInGroup('user1', 'group1')); |
|
512 | + } |
|
513 | + |
|
514 | + public function testIsAdmin(): void { |
|
515 | + /** |
|
516 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
517 | + */ |
|
518 | + $backend = $this->getTestBackend(); |
|
519 | + $backend->expects($this->once()) |
|
520 | + ->method('getUserGroups') |
|
521 | + ->with('user1') |
|
522 | + ->willReturn(['group1', 'admin', 'group2']); |
|
523 | + $backend->expects($this->any()) |
|
524 | + ->method('groupExists') |
|
525 | + ->willReturn(true); |
|
526 | + |
|
527 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
528 | + $manager->addBackend($backend); |
|
529 | + |
|
530 | + $this->assertTrue($manager->isAdmin('user1')); |
|
531 | + } |
|
532 | + |
|
533 | + public function testNotAdmin(): void { |
|
534 | + /** |
|
535 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
536 | + */ |
|
537 | + $backend = $this->getTestBackend(); |
|
538 | + $backend->expects($this->once()) |
|
539 | + ->method('getUserGroups') |
|
540 | + ->with('user1') |
|
541 | + ->willReturn(['group1', 'group2']); |
|
542 | + $backend->expects($this->any()) |
|
543 | + ->method('groupExists') |
|
544 | + ->willReturn(true); |
|
545 | + |
|
546 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
547 | + $manager->addBackend($backend); |
|
548 | + |
|
549 | + $this->assertFalse($manager->isAdmin('user1')); |
|
550 | + } |
|
551 | + |
|
552 | + public function testGetUserGroupsMultipleBackends(): void { |
|
553 | + /** |
|
554 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1 |
|
555 | + */ |
|
556 | + $backend1 = $this->getTestBackend(); |
|
557 | + $backend1->expects($this->once()) |
|
558 | + ->method('getUserGroups') |
|
559 | + ->with('user1') |
|
560 | + ->willReturn(['group1']); |
|
561 | + $backend1->expects($this->any()) |
|
562 | + ->method('groupExists') |
|
563 | + ->willReturn(true); |
|
564 | + |
|
565 | + /** |
|
566 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2 |
|
567 | + */ |
|
568 | + $backend2 = $this->getTestBackend(); |
|
569 | + $backend2->expects($this->once()) |
|
570 | + ->method('getUserGroups') |
|
571 | + ->with('user1') |
|
572 | + ->willReturn(['group1', 'group2']); |
|
573 | + $backend1->expects($this->any()) |
|
574 | + ->method('groupExists') |
|
575 | + ->willReturn(true); |
|
576 | + |
|
577 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
578 | + $manager->addBackend($backend1); |
|
579 | + $manager->addBackend($backend2); |
|
580 | + |
|
581 | + $groups = $manager->getUserGroups($this->getTestUser('user1')); |
|
582 | + $this->assertCount(2, $groups); |
|
583 | + $group1 = reset($groups); |
|
584 | + $group2 = next($groups); |
|
585 | + $this->assertEquals('group1', $group1->getGID()); |
|
586 | + $this->assertEquals('group2', $group2->getGID()); |
|
587 | + } |
|
588 | + |
|
589 | + public function testDisplayNamesInGroupWithOneUserBackend(): void { |
|
590 | + /** |
|
591 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
592 | + */ |
|
593 | + $backend = $this->getTestBackend(); |
|
594 | + $backend->expects($this->exactly(1)) |
|
595 | + ->method('groupExists') |
|
596 | + ->with('testgroup') |
|
597 | + ->willReturn(true); |
|
598 | + |
|
599 | + $backend->expects($this->any()) |
|
600 | + ->method('inGroup') |
|
601 | + ->willReturnCallback(function ($uid, $gid) { |
|
602 | + switch ($uid) { |
|
603 | + case 'user1': return false; |
|
604 | + case 'user2': return true; |
|
605 | + case 'user3': return false; |
|
606 | + case 'user33': return true; |
|
607 | + default: |
|
608 | + return null; |
|
609 | + } |
|
610 | + }); |
|
611 | + |
|
612 | + $this->userManager->expects($this->any()) |
|
613 | + ->method('searchDisplayName') |
|
614 | + ->with('user3') |
|
615 | + ->willReturnCallback(function ($search, $limit, $offset) { |
|
616 | + switch ($offset) { |
|
617 | + case 0: return ['user3' => $this->getTestUser('user3'), |
|
618 | + 'user33' => $this->getTestUser('user33')]; |
|
619 | + case 2: return []; |
|
620 | + } |
|
621 | + return null; |
|
622 | + }); |
|
623 | + $this->userManager->expects($this->any()) |
|
624 | + ->method('get') |
|
625 | + ->willReturnCallback(function ($uid) { |
|
626 | + switch ($uid) { |
|
627 | + case 'user1': return $this->getTestUser('user1'); |
|
628 | + case 'user2': return $this->getTestUser('user2'); |
|
629 | + case 'user3': return $this->getTestUser('user3'); |
|
630 | + case 'user33': return $this->getTestUser('user33'); |
|
631 | + default: |
|
632 | + return null; |
|
633 | + } |
|
634 | + }); |
|
635 | + |
|
636 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
637 | + $manager->addBackend($backend); |
|
638 | + |
|
639 | + $users = $manager->displayNamesInGroup('testgroup', 'user3'); |
|
640 | + $this->assertCount(1, $users); |
|
641 | + $this->assertFalse(isset($users['user1'])); |
|
642 | + $this->assertFalse(isset($users['user2'])); |
|
643 | + $this->assertFalse(isset($users['user3'])); |
|
644 | + $this->assertTrue(isset($users['user33'])); |
|
645 | + } |
|
646 | + |
|
647 | + public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified(): void { |
|
648 | + /** |
|
649 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
650 | + */ |
|
651 | + $backend = $this->getTestBackend(); |
|
652 | + $backend->expects($this->exactly(1)) |
|
653 | + ->method('groupExists') |
|
654 | + ->with('testgroup') |
|
655 | + ->willReturn(true); |
|
656 | + |
|
657 | + $backend->expects($this->any()) |
|
658 | + ->method('inGroup') |
|
659 | + ->willReturnCallback(function ($uid, $gid) { |
|
660 | + switch ($uid) { |
|
661 | + case 'user1': return false; |
|
662 | + case 'user2': return true; |
|
663 | + case 'user3': return false; |
|
664 | + case 'user33': return true; |
|
665 | + case 'user333': return true; |
|
666 | + default: |
|
667 | + return null; |
|
668 | + } |
|
669 | + }); |
|
670 | + |
|
671 | + $this->userManager->expects($this->any()) |
|
672 | + ->method('searchDisplayName') |
|
673 | + ->with('user3') |
|
674 | + ->willReturnCallback(function ($search, $limit, $offset) { |
|
675 | + switch ($offset) { |
|
676 | + case 0: return ['user3' => $this->getTestUser('user3'), |
|
677 | + 'user33' => $this->getTestUser('user33')]; |
|
678 | + case 2: return ['user333' => $this->getTestUser('user333')]; |
|
679 | + } |
|
680 | + return null; |
|
681 | + }); |
|
682 | + $this->userManager->expects($this->any()) |
|
683 | + ->method('get') |
|
684 | + ->willReturnCallback(function ($uid) { |
|
685 | + switch ($uid) { |
|
686 | + case 'user1': return $this->getTestUser('user1'); |
|
687 | + case 'user2': return $this->getTestUser('user2'); |
|
688 | + case 'user3': return $this->getTestUser('user3'); |
|
689 | + case 'user33': return $this->getTestUser('user33'); |
|
690 | + case 'user333': return $this->getTestUser('user333'); |
|
691 | + default: |
|
692 | + return null; |
|
693 | + } |
|
694 | + }); |
|
695 | + |
|
696 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
697 | + $manager->addBackend($backend); |
|
698 | + |
|
699 | + $users = $manager->displayNamesInGroup('testgroup', 'user3', 1); |
|
700 | + $this->assertCount(1, $users); |
|
701 | + $this->assertFalse(isset($users['user1'])); |
|
702 | + $this->assertFalse(isset($users['user2'])); |
|
703 | + $this->assertFalse(isset($users['user3'])); |
|
704 | + $this->assertTrue(isset($users['user33'])); |
|
705 | + $this->assertFalse(isset($users['user333'])); |
|
706 | + } |
|
707 | + |
|
708 | + public function testDisplayNamesInGroupWithOneUserBackendWithLimitAndOffsetSpecified(): void { |
|
709 | + /** |
|
710 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
711 | + */ |
|
712 | + $backend = $this->getTestBackend(); |
|
713 | + $backend->expects($this->exactly(1)) |
|
714 | + ->method('groupExists') |
|
715 | + ->with('testgroup') |
|
716 | + ->willReturn(true); |
|
717 | + |
|
718 | + $backend->expects($this->any()) |
|
719 | + ->method('inGroup') |
|
720 | + ->willReturnCallback(function ($uid) { |
|
721 | + switch ($uid) { |
|
722 | + case 'user1': return false; |
|
723 | + case 'user2': return true; |
|
724 | + case 'user3': return false; |
|
725 | + case 'user33': return true; |
|
726 | + case 'user333': return true; |
|
727 | + default: |
|
728 | + return null; |
|
729 | + } |
|
730 | + }); |
|
731 | + |
|
732 | + $this->userManager->expects($this->any()) |
|
733 | + ->method('searchDisplayName') |
|
734 | + ->with('user3') |
|
735 | + ->willReturnCallback(function ($search, $limit, $offset) { |
|
736 | + switch ($offset) { |
|
737 | + case 0: |
|
738 | + return [ |
|
739 | + 'user3' => $this->getTestUser('user3'), |
|
740 | + 'user33' => $this->getTestUser('user33'), |
|
741 | + 'user333' => $this->getTestUser('user333') |
|
742 | + ]; |
|
743 | + } |
|
744 | + return null; |
|
745 | + }); |
|
746 | + $this->userManager->expects($this->any()) |
|
747 | + ->method('get') |
|
748 | + ->willReturnCallback(function ($uid) { |
|
749 | + switch ($uid) { |
|
750 | + case 'user1': return $this->getTestUser('user1'); |
|
751 | + case 'user2': return $this->getTestUser('user2'); |
|
752 | + case 'user3': return $this->getTestUser('user3'); |
|
753 | + case 'user33': return $this->getTestUser('user33'); |
|
754 | + case 'user333': return $this->getTestUser('user333'); |
|
755 | + default: |
|
756 | + return null; |
|
757 | + } |
|
758 | + }); |
|
759 | + |
|
760 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
761 | + $manager->addBackend($backend); |
|
762 | + |
|
763 | + $users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1); |
|
764 | + $this->assertCount(1, $users); |
|
765 | + $this->assertFalse(isset($users['user1'])); |
|
766 | + $this->assertFalse(isset($users['user2'])); |
|
767 | + $this->assertFalse(isset($users['user3'])); |
|
768 | + $this->assertFalse(isset($users['user33'])); |
|
769 | + $this->assertTrue(isset($users['user333'])); |
|
770 | + } |
|
771 | + |
|
772 | + public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty(): void { |
|
773 | + /** |
|
774 | + * @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend |
|
775 | + */ |
|
776 | + $backend = $this->getTestBackend(); |
|
777 | + $backend->expects($this->exactly(1)) |
|
778 | + ->method('groupExists') |
|
779 | + ->with('testgroup') |
|
780 | + ->willReturn(true); |
|
781 | + |
|
782 | + $backend->expects($this->once()) |
|
783 | + ->method('searchInGroup') |
|
784 | + ->with('testgroup', '', -1, 0) |
|
785 | + ->willReturn(['user2' => $this->getTestUser('user2'), 'user33' => $this->getTestUser('user33')]); |
|
786 | + |
|
787 | + $this->userManager->expects($this->never())->method('get'); |
|
788 | + |
|
789 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
790 | + $manager->addBackend($backend); |
|
791 | + |
|
792 | + $users = $manager->displayNamesInGroup('testgroup', ''); |
|
793 | + $this->assertCount(2, $users); |
|
794 | + $this->assertFalse(isset($users['user1'])); |
|
795 | + $this->assertTrue(isset($users['user2'])); |
|
796 | + $this->assertFalse(isset($users['user3'])); |
|
797 | + $this->assertTrue(isset($users['user33'])); |
|
798 | + } |
|
799 | + |
|
800 | + public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified(): void { |
|
801 | + /** |
|
802 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
803 | + */ |
|
804 | + $backend = $this->getTestBackend(); |
|
805 | + $backend->expects($this->exactly(1)) |
|
806 | + ->method('groupExists') |
|
807 | + ->with('testgroup') |
|
808 | + ->willReturn(true); |
|
809 | + |
|
810 | + $backend->expects($this->once()) |
|
811 | + ->method('searchInGroup') |
|
812 | + ->with('testgroup', '', 1, 0) |
|
813 | + ->willReturn([new User('user2', null, $this->dispatcher)]); |
|
814 | + |
|
815 | + $this->userManager->expects($this->never())->method('get'); |
|
816 | + |
|
817 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
818 | + $manager->addBackend($backend); |
|
819 | + |
|
820 | + $users = $manager->displayNamesInGroup('testgroup', '', 1); |
|
821 | + $this->assertCount(1, $users); |
|
822 | + $this->assertFalse(isset($users['user1'])); |
|
823 | + $this->assertTrue(isset($users['user2'])); |
|
824 | + $this->assertFalse(isset($users['user3'])); |
|
825 | + $this->assertFalse(isset($users['user33'])); |
|
826 | + } |
|
827 | + |
|
828 | + public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified(): void { |
|
829 | + /** |
|
830 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
831 | + */ |
|
832 | + $backend = $this->getTestBackend(); |
|
833 | + $backend->expects($this->exactly(1)) |
|
834 | + ->method('groupExists') |
|
835 | + ->with('testgroup') |
|
836 | + ->willReturn(true); |
|
837 | + |
|
838 | + $backend->expects($this->once()) |
|
839 | + ->method('searchInGroup') |
|
840 | + ->with('testgroup', '', 1, 1) |
|
841 | + ->willReturn(['user33' => $this->getTestUser('user33')]); |
|
842 | + |
|
843 | + $this->userManager->expects($this->never())->method('get'); |
|
844 | + |
|
845 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
846 | + $manager->addBackend($backend); |
|
847 | + |
|
848 | + $users = $manager->displayNamesInGroup('testgroup', '', 1, 1); |
|
849 | + $this->assertCount(1, $users); |
|
850 | + $this->assertFalse(isset($users['user1'])); |
|
851 | + $this->assertFalse(isset($users['user2'])); |
|
852 | + $this->assertFalse(isset($users['user3'])); |
|
853 | + $this->assertTrue(isset($users['user33'])); |
|
854 | + } |
|
855 | + |
|
856 | + public function testGetUserGroupsWithAddUser(): void { |
|
857 | + /** |
|
858 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
859 | + */ |
|
860 | + $backend = $this->getTestBackend(); |
|
861 | + $expectedGroups = []; |
|
862 | + $backend->expects($this->any()) |
|
863 | + ->method('getUserGroups') |
|
864 | + ->with('user1') |
|
865 | + ->willReturnCallback(function () use (&$expectedGroups) { |
|
866 | + return $expectedGroups; |
|
867 | + }); |
|
868 | + $backend->expects($this->any()) |
|
869 | + ->method('groupExists') |
|
870 | + ->with('group1') |
|
871 | + ->willReturn(true); |
|
872 | + |
|
873 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
874 | + $manager->addBackend($backend); |
|
875 | + |
|
876 | + // prime cache |
|
877 | + $user1 = $this->getTestUser('user1'); |
|
878 | + $groups = $manager->getUserGroups($user1); |
|
879 | + $this->assertEquals([], $groups); |
|
880 | + |
|
881 | + // add user |
|
882 | + $group = $manager->get('group1'); |
|
883 | + $group->addUser($user1); |
|
884 | + $expectedGroups[] = 'group1'; |
|
885 | + |
|
886 | + // check result |
|
887 | + $groups = $manager->getUserGroups($user1); |
|
888 | + $this->assertCount(1, $groups); |
|
889 | + $group1 = reset($groups); |
|
890 | + $this->assertEquals('group1', $group1->getGID()); |
|
891 | + } |
|
892 | + |
|
893 | + public function testGetUserGroupsWithRemoveUser(): void { |
|
894 | + /** |
|
895 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
896 | + */ |
|
897 | + $backend = $this->getTestBackend(); |
|
898 | + $expectedGroups = ['group1']; |
|
899 | + $backend->expects($this->any()) |
|
900 | + ->method('getUserGroups') |
|
901 | + ->with('user1') |
|
902 | + ->willReturnCallback(function () use (&$expectedGroups) { |
|
903 | + return $expectedGroups; |
|
904 | + }); |
|
905 | + $backend->expects($this->any()) |
|
906 | + ->method('groupExists') |
|
907 | + ->with('group1') |
|
908 | + ->willReturn(true); |
|
909 | + $backend->expects($this->once()) |
|
910 | + ->method('inGroup') |
|
911 | + ->willReturn(true); |
|
912 | + $backend->expects($this->once()) |
|
913 | + ->method('removeFromGroup') |
|
914 | + ->willReturn(true); |
|
915 | + |
|
916 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
917 | + $manager->addBackend($backend); |
|
918 | + |
|
919 | + // prime cache |
|
920 | + $user1 = $this->getTestUser('user1'); |
|
921 | + $groups = $manager->getUserGroups($user1); |
|
922 | + $this->assertCount(1, $groups); |
|
923 | + $group1 = reset($groups); |
|
924 | + $this->assertEquals('group1', $group1->getGID()); |
|
925 | + |
|
926 | + // remove user |
|
927 | + $group = $manager->get('group1'); |
|
928 | + $group->removeUser($user1); |
|
929 | + $expectedGroups = []; |
|
930 | + |
|
931 | + // check result |
|
932 | + $groups = $manager->getUserGroups($user1); |
|
933 | + $this->assertEquals($expectedGroups, $groups); |
|
934 | + } |
|
935 | + |
|
936 | + public function testGetUserIdGroups(): void { |
|
937 | + /** |
|
938 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
939 | + */ |
|
940 | + $backend = $this->getTestBackend(); |
|
941 | + $backend->expects($this->any()) |
|
942 | + ->method('getUserGroups') |
|
943 | + ->with('user1') |
|
944 | + ->willReturn(null); |
|
945 | + |
|
946 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
947 | + $manager->addBackend($backend); |
|
948 | + |
|
949 | + $groups = $manager->getUserIdGroups('user1'); |
|
950 | + $this->assertEquals([], $groups); |
|
951 | + } |
|
952 | + |
|
953 | + public function testGroupDisplayName(): void { |
|
954 | + /** |
|
955 | + * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend |
|
956 | + */ |
|
957 | + $backend = $this->getTestBackend( |
|
958 | + GroupInterface::ADD_TO_GROUP | |
|
959 | + GroupInterface::REMOVE_FROM_GOUP | |
|
960 | + GroupInterface::COUNT_USERS | |
|
961 | + GroupInterface::CREATE_GROUP | |
|
962 | + GroupInterface::DELETE_GROUP | |
|
963 | + GroupInterface::GROUP_DETAILS |
|
964 | + ); |
|
965 | + $backend->expects($this->any()) |
|
966 | + ->method('getGroupDetails') |
|
967 | + ->willReturnMap([ |
|
968 | + ['group1', ['gid' => 'group1', 'displayName' => 'Group One']], |
|
969 | + ['group2', ['gid' => 'group2']], |
|
970 | + ]); |
|
971 | + $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); |
|
972 | + $manager->addBackend($backend); |
|
973 | + |
|
974 | + // group with display name |
|
975 | + $group = $manager->get('group1'); |
|
976 | + $this->assertNotNull($group); |
|
977 | + $this->assertEquals('group1', $group->getGID()); |
|
978 | + $this->assertEquals('Group One', $group->getDisplayName()); |
|
979 | + |
|
980 | + // group without display name |
|
981 | + $group = $manager->get('group2'); |
|
982 | + $this->assertNotNull($group); |
|
983 | + $this->assertEquals('group2', $group->getGID()); |
|
984 | + $this->assertEquals('group2', $group->getDisplayName()); |
|
985 | + } |
|
986 | 986 | } |
@@ -11,245 +11,245 @@ |
||
11 | 11 | use OC_Helper; |
12 | 12 | |
13 | 13 | class LegacyHelperTest extends \Test\TestCase { |
14 | - /** @var string */ |
|
15 | - private $originalWebRoot; |
|
16 | - |
|
17 | - protected function setUp(): void { |
|
18 | - $this->originalWebRoot = \OC::$WEBROOT; |
|
19 | - } |
|
20 | - |
|
21 | - protected function tearDown(): void { |
|
22 | - // Reset webRoot |
|
23 | - \OC::$WEBROOT = $this->originalWebRoot; |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * @dataProvider humanFileSizeProvider |
|
28 | - */ |
|
29 | - public function testHumanFileSize($expected, $input): void { |
|
30 | - $result = OC_Helper::humanFileSize($input); |
|
31 | - $this->assertEquals($expected, $result); |
|
32 | - } |
|
33 | - |
|
34 | - public static function humanFileSizeProvider(): array { |
|
35 | - return [ |
|
36 | - ['0 B', 0], |
|
37 | - ['1 KB', 1024], |
|
38 | - ['9.5 MB', 10000000], |
|
39 | - ['1.3 GB', 1395864371], |
|
40 | - ['465.7 GB', 500000000000], |
|
41 | - ['454.7 TB', 500000000000000], |
|
42 | - ['444.1 PB', 500000000000000000], |
|
43 | - ]; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * @dataProvider providesComputerFileSize |
|
48 | - */ |
|
49 | - public function testComputerFileSize($expected, $input): void { |
|
50 | - $result = OC_Helper::computerFileSize($input); |
|
51 | - $this->assertEquals($expected, $result); |
|
52 | - } |
|
53 | - |
|
54 | - public static function providesComputerFileSize(): array { |
|
55 | - return [ |
|
56 | - [0.0, '0 B'], |
|
57 | - [1024.0, '1 KB'], |
|
58 | - [1395864371.0, '1.3 GB'], |
|
59 | - [9961472.0, '9.5 MB'], |
|
60 | - [500041567437.0, '465.7 GB'], |
|
61 | - [false, '12 GB etfrhzui'] |
|
62 | - ]; |
|
63 | - } |
|
64 | - |
|
65 | - public function testMb_array_change_key_case(): void { |
|
66 | - $arrayStart = [ |
|
67 | - 'Foo' => 'bar', |
|
68 | - 'Bar' => 'foo', |
|
69 | - ]; |
|
70 | - $arrayResult = [ |
|
71 | - 'foo' => 'bar', |
|
72 | - 'bar' => 'foo', |
|
73 | - ]; |
|
74 | - $result = OC_Helper::mb_array_change_key_case($arrayStart); |
|
75 | - $expected = $arrayResult; |
|
76 | - $this->assertEquals($result, $expected); |
|
77 | - |
|
78 | - $arrayStart = [ |
|
79 | - 'foo' => 'bar', |
|
80 | - 'bar' => 'foo', |
|
81 | - ]; |
|
82 | - $arrayResult = [ |
|
83 | - 'FOO' => 'bar', |
|
84 | - 'BAR' => 'foo', |
|
85 | - ]; |
|
86 | - $result = OC_Helper::mb_array_change_key_case($arrayStart, MB_CASE_UPPER); |
|
87 | - $expected = $arrayResult; |
|
88 | - $this->assertEquals($result, $expected); |
|
89 | - } |
|
90 | - |
|
91 | - public function testRecursiveArraySearch(): void { |
|
92 | - $haystack = [ |
|
93 | - 'Foo' => 'own', |
|
94 | - 'Bar' => 'Cloud', |
|
95 | - ]; |
|
96 | - |
|
97 | - $result = OC_Helper::recursiveArraySearch($haystack, 'own'); |
|
98 | - $expected = 'Foo'; |
|
99 | - $this->assertEquals($result, $expected); |
|
100 | - |
|
101 | - $result = OC_Helper::recursiveArraySearch($haystack, 'NotFound'); |
|
102 | - $this->assertFalse($result); |
|
103 | - } |
|
104 | - |
|
105 | - public function testBuildNotExistingFileNameForView(): void { |
|
106 | - $viewMock = $this->createMock(View::class); |
|
107 | - $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock)); |
|
108 | - $this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); |
|
109 | - |
|
110 | - $viewMock = $this->createMock(View::class); |
|
111 | - $viewMock->expects($this->exactly(2)) |
|
112 | - ->method('file_exists') |
|
113 | - ->willReturnMap([ |
|
114 | - // Conflict on filename.ext |
|
115 | - ['dir/filename.ext', true], |
|
116 | - ['dir/filename (2).ext', false], |
|
117 | - ]); |
|
118 | - $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); |
|
119 | - |
|
120 | - $viewMock = $this->createMock(View::class); |
|
121 | - $viewMock->expects($this->exactly(3)) |
|
122 | - ->method('file_exists') |
|
123 | - ->willReturnMap([ |
|
124 | - // Conflict on filename.ext |
|
125 | - ['dir/filename.ext', true], |
|
126 | - ['dir/filename (2).ext', true], |
|
127 | - ['dir/filename (3).ext', false], |
|
128 | - ]); |
|
129 | - $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); |
|
130 | - |
|
131 | - $viewMock = $this->createMock(View::class); |
|
132 | - $viewMock->expects($this->exactly(2)) |
|
133 | - ->method('file_exists') |
|
134 | - ->willReturnMap([ |
|
135 | - ['dir/filename (1).ext', true], |
|
136 | - ['dir/filename (2).ext', false], |
|
137 | - ]); |
|
138 | - $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock)); |
|
139 | - |
|
140 | - $viewMock = $this->createMock(View::class); |
|
141 | - $viewMock->expects($this->exactly(2)) |
|
142 | - ->method('file_exists') |
|
143 | - ->willReturnMap([ |
|
144 | - ['dir/filename (2).ext', true], |
|
145 | - ['dir/filename (3).ext', false], |
|
146 | - ]); |
|
147 | - $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock)); |
|
148 | - |
|
149 | - $viewMock = $this->createMock(View::class); |
|
150 | - $viewMock->expects($this->exactly(3)) |
|
151 | - ->method('file_exists') |
|
152 | - ->willReturnMap([ |
|
153 | - ['dir/filename (2).ext', true], |
|
154 | - ['dir/filename (3).ext', true], |
|
155 | - ['dir/filename (4).ext', false], |
|
156 | - ]); |
|
157 | - $this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock)); |
|
158 | - |
|
159 | - $viewMock = $this->createMock(View::class); |
|
160 | - $viewMock->expects($this->exactly(2)) |
|
161 | - ->method('file_exists') |
|
162 | - ->willReturnMap([ |
|
163 | - ['dir/filename(1).ext', true], |
|
164 | - ['dir/filename(2).ext', false], |
|
165 | - ]); |
|
166 | - $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock)); |
|
167 | - |
|
168 | - $viewMock = $this->createMock(View::class); |
|
169 | - $viewMock->expects($this->exactly(2)) |
|
170 | - ->method('file_exists') |
|
171 | - ->willReturnMap([ |
|
172 | - ['dir/filename(1) (1).ext', true], |
|
173 | - ['dir/filename(1) (2).ext', false], |
|
174 | - ]); |
|
175 | - $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock)); |
|
176 | - |
|
177 | - $viewMock = $this->createMock(View::class); |
|
178 | - $viewMock->expects($this->exactly(3)) |
|
179 | - ->method('file_exists') |
|
180 | - ->willReturnMap([ |
|
181 | - ['dir/filename(1) (1).ext', true], |
|
182 | - ['dir/filename(1) (2).ext', true], |
|
183 | - ['dir/filename(1) (3).ext', false], |
|
184 | - ]); |
|
185 | - $this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock)); |
|
186 | - |
|
187 | - $viewMock = $this->createMock(View::class); |
|
188 | - $viewMock->expects($this->exactly(2)) |
|
189 | - ->method('file_exists') |
|
190 | - ->willReturnMap([ |
|
191 | - ['dir/filename(1) (2) (3).ext', true], |
|
192 | - ['dir/filename(1) (2) (4).ext', false], |
|
193 | - ]); |
|
194 | - $this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock)); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @dataProvider streamCopyDataProvider |
|
199 | - */ |
|
200 | - public function testStreamCopy($expectedCount, $expectedResult, $source, $target): void { |
|
201 | - if (is_string($source)) { |
|
202 | - $source = fopen($source, 'r'); |
|
203 | - } |
|
204 | - if (is_string($target)) { |
|
205 | - $target = fopen($target, 'w'); |
|
206 | - } |
|
207 | - |
|
208 | - [$count, $result] = \OC_Helper::streamCopy($source, $target); |
|
209 | - |
|
210 | - if (is_resource($source)) { |
|
211 | - fclose($source); |
|
212 | - } |
|
213 | - if (is_resource($target)) { |
|
214 | - fclose($target); |
|
215 | - } |
|
216 | - |
|
217 | - $this->assertSame($expectedCount, $count); |
|
218 | - $this->assertSame($expectedResult, $result); |
|
219 | - } |
|
220 | - |
|
221 | - |
|
222 | - public static function streamCopyDataProvider(): array { |
|
223 | - return [ |
|
224 | - [0, false, false, false], |
|
225 | - [0, false, \OC::$SERVERROOT . '/tests/data/lorem.txt', false], |
|
226 | - [filesize(\OC::$SERVERROOT . '/tests/data/lorem.txt'), true, \OC::$SERVERROOT . '/tests/data/lorem.txt', \OC::$SERVERROOT . '/tests/data/lorem-copy.txt'], |
|
227 | - [3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'], |
|
228 | - ]; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Tests recursive folder deletion with rmdirr() |
|
233 | - */ |
|
234 | - public function testRecursiveFolderDeletion(): void { |
|
235 | - $baseDir = \OC::$server->getTempManager()->getTemporaryFolder() . '/'; |
|
236 | - mkdir($baseDir . 'a/b/c/d/e', 0777, true); |
|
237 | - mkdir($baseDir . 'a/b/c1/d/e', 0777, true); |
|
238 | - mkdir($baseDir . 'a/b/c2/d/e', 0777, true); |
|
239 | - mkdir($baseDir . 'a/b1/c1/d/e', 0777, true); |
|
240 | - mkdir($baseDir . 'a/b2/c1/d/e', 0777, true); |
|
241 | - mkdir($baseDir . 'a/b3/c1/d/e', 0777, true); |
|
242 | - mkdir($baseDir . 'a1/b', 0777, true); |
|
243 | - mkdir($baseDir . 'a1/c', 0777, true); |
|
244 | - file_put_contents($baseDir . 'a/test.txt', 'Hello file!'); |
|
245 | - file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!'); |
|
246 | - file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!'); |
|
247 | - \OC_Helper::rmdirr($baseDir . 'a'); |
|
248 | - |
|
249 | - $this->assertFalse(file_exists($baseDir . 'a')); |
|
250 | - $this->assertTrue(file_exists($baseDir . 'a1')); |
|
251 | - |
|
252 | - \OC_Helper::rmdirr($baseDir); |
|
253 | - $this->assertFalse(file_exists($baseDir)); |
|
254 | - } |
|
14 | + /** @var string */ |
|
15 | + private $originalWebRoot; |
|
16 | + |
|
17 | + protected function setUp(): void { |
|
18 | + $this->originalWebRoot = \OC::$WEBROOT; |
|
19 | + } |
|
20 | + |
|
21 | + protected function tearDown(): void { |
|
22 | + // Reset webRoot |
|
23 | + \OC::$WEBROOT = $this->originalWebRoot; |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * @dataProvider humanFileSizeProvider |
|
28 | + */ |
|
29 | + public function testHumanFileSize($expected, $input): void { |
|
30 | + $result = OC_Helper::humanFileSize($input); |
|
31 | + $this->assertEquals($expected, $result); |
|
32 | + } |
|
33 | + |
|
34 | + public static function humanFileSizeProvider(): array { |
|
35 | + return [ |
|
36 | + ['0 B', 0], |
|
37 | + ['1 KB', 1024], |
|
38 | + ['9.5 MB', 10000000], |
|
39 | + ['1.3 GB', 1395864371], |
|
40 | + ['465.7 GB', 500000000000], |
|
41 | + ['454.7 TB', 500000000000000], |
|
42 | + ['444.1 PB', 500000000000000000], |
|
43 | + ]; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * @dataProvider providesComputerFileSize |
|
48 | + */ |
|
49 | + public function testComputerFileSize($expected, $input): void { |
|
50 | + $result = OC_Helper::computerFileSize($input); |
|
51 | + $this->assertEquals($expected, $result); |
|
52 | + } |
|
53 | + |
|
54 | + public static function providesComputerFileSize(): array { |
|
55 | + return [ |
|
56 | + [0.0, '0 B'], |
|
57 | + [1024.0, '1 KB'], |
|
58 | + [1395864371.0, '1.3 GB'], |
|
59 | + [9961472.0, '9.5 MB'], |
|
60 | + [500041567437.0, '465.7 GB'], |
|
61 | + [false, '12 GB etfrhzui'] |
|
62 | + ]; |
|
63 | + } |
|
64 | + |
|
65 | + public function testMb_array_change_key_case(): void { |
|
66 | + $arrayStart = [ |
|
67 | + 'Foo' => 'bar', |
|
68 | + 'Bar' => 'foo', |
|
69 | + ]; |
|
70 | + $arrayResult = [ |
|
71 | + 'foo' => 'bar', |
|
72 | + 'bar' => 'foo', |
|
73 | + ]; |
|
74 | + $result = OC_Helper::mb_array_change_key_case($arrayStart); |
|
75 | + $expected = $arrayResult; |
|
76 | + $this->assertEquals($result, $expected); |
|
77 | + |
|
78 | + $arrayStart = [ |
|
79 | + 'foo' => 'bar', |
|
80 | + 'bar' => 'foo', |
|
81 | + ]; |
|
82 | + $arrayResult = [ |
|
83 | + 'FOO' => 'bar', |
|
84 | + 'BAR' => 'foo', |
|
85 | + ]; |
|
86 | + $result = OC_Helper::mb_array_change_key_case($arrayStart, MB_CASE_UPPER); |
|
87 | + $expected = $arrayResult; |
|
88 | + $this->assertEquals($result, $expected); |
|
89 | + } |
|
90 | + |
|
91 | + public function testRecursiveArraySearch(): void { |
|
92 | + $haystack = [ |
|
93 | + 'Foo' => 'own', |
|
94 | + 'Bar' => 'Cloud', |
|
95 | + ]; |
|
96 | + |
|
97 | + $result = OC_Helper::recursiveArraySearch($haystack, 'own'); |
|
98 | + $expected = 'Foo'; |
|
99 | + $this->assertEquals($result, $expected); |
|
100 | + |
|
101 | + $result = OC_Helper::recursiveArraySearch($haystack, 'NotFound'); |
|
102 | + $this->assertFalse($result); |
|
103 | + } |
|
104 | + |
|
105 | + public function testBuildNotExistingFileNameForView(): void { |
|
106 | + $viewMock = $this->createMock(View::class); |
|
107 | + $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock)); |
|
108 | + $this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); |
|
109 | + |
|
110 | + $viewMock = $this->createMock(View::class); |
|
111 | + $viewMock->expects($this->exactly(2)) |
|
112 | + ->method('file_exists') |
|
113 | + ->willReturnMap([ |
|
114 | + // Conflict on filename.ext |
|
115 | + ['dir/filename.ext', true], |
|
116 | + ['dir/filename (2).ext', false], |
|
117 | + ]); |
|
118 | + $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); |
|
119 | + |
|
120 | + $viewMock = $this->createMock(View::class); |
|
121 | + $viewMock->expects($this->exactly(3)) |
|
122 | + ->method('file_exists') |
|
123 | + ->willReturnMap([ |
|
124 | + // Conflict on filename.ext |
|
125 | + ['dir/filename.ext', true], |
|
126 | + ['dir/filename (2).ext', true], |
|
127 | + ['dir/filename (3).ext', false], |
|
128 | + ]); |
|
129 | + $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); |
|
130 | + |
|
131 | + $viewMock = $this->createMock(View::class); |
|
132 | + $viewMock->expects($this->exactly(2)) |
|
133 | + ->method('file_exists') |
|
134 | + ->willReturnMap([ |
|
135 | + ['dir/filename (1).ext', true], |
|
136 | + ['dir/filename (2).ext', false], |
|
137 | + ]); |
|
138 | + $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock)); |
|
139 | + |
|
140 | + $viewMock = $this->createMock(View::class); |
|
141 | + $viewMock->expects($this->exactly(2)) |
|
142 | + ->method('file_exists') |
|
143 | + ->willReturnMap([ |
|
144 | + ['dir/filename (2).ext', true], |
|
145 | + ['dir/filename (3).ext', false], |
|
146 | + ]); |
|
147 | + $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock)); |
|
148 | + |
|
149 | + $viewMock = $this->createMock(View::class); |
|
150 | + $viewMock->expects($this->exactly(3)) |
|
151 | + ->method('file_exists') |
|
152 | + ->willReturnMap([ |
|
153 | + ['dir/filename (2).ext', true], |
|
154 | + ['dir/filename (3).ext', true], |
|
155 | + ['dir/filename (4).ext', false], |
|
156 | + ]); |
|
157 | + $this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock)); |
|
158 | + |
|
159 | + $viewMock = $this->createMock(View::class); |
|
160 | + $viewMock->expects($this->exactly(2)) |
|
161 | + ->method('file_exists') |
|
162 | + ->willReturnMap([ |
|
163 | + ['dir/filename(1).ext', true], |
|
164 | + ['dir/filename(2).ext', false], |
|
165 | + ]); |
|
166 | + $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock)); |
|
167 | + |
|
168 | + $viewMock = $this->createMock(View::class); |
|
169 | + $viewMock->expects($this->exactly(2)) |
|
170 | + ->method('file_exists') |
|
171 | + ->willReturnMap([ |
|
172 | + ['dir/filename(1) (1).ext', true], |
|
173 | + ['dir/filename(1) (2).ext', false], |
|
174 | + ]); |
|
175 | + $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock)); |
|
176 | + |
|
177 | + $viewMock = $this->createMock(View::class); |
|
178 | + $viewMock->expects($this->exactly(3)) |
|
179 | + ->method('file_exists') |
|
180 | + ->willReturnMap([ |
|
181 | + ['dir/filename(1) (1).ext', true], |
|
182 | + ['dir/filename(1) (2).ext', true], |
|
183 | + ['dir/filename(1) (3).ext', false], |
|
184 | + ]); |
|
185 | + $this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock)); |
|
186 | + |
|
187 | + $viewMock = $this->createMock(View::class); |
|
188 | + $viewMock->expects($this->exactly(2)) |
|
189 | + ->method('file_exists') |
|
190 | + ->willReturnMap([ |
|
191 | + ['dir/filename(1) (2) (3).ext', true], |
|
192 | + ['dir/filename(1) (2) (4).ext', false], |
|
193 | + ]); |
|
194 | + $this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock)); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @dataProvider streamCopyDataProvider |
|
199 | + */ |
|
200 | + public function testStreamCopy($expectedCount, $expectedResult, $source, $target): void { |
|
201 | + if (is_string($source)) { |
|
202 | + $source = fopen($source, 'r'); |
|
203 | + } |
|
204 | + if (is_string($target)) { |
|
205 | + $target = fopen($target, 'w'); |
|
206 | + } |
|
207 | + |
|
208 | + [$count, $result] = \OC_Helper::streamCopy($source, $target); |
|
209 | + |
|
210 | + if (is_resource($source)) { |
|
211 | + fclose($source); |
|
212 | + } |
|
213 | + if (is_resource($target)) { |
|
214 | + fclose($target); |
|
215 | + } |
|
216 | + |
|
217 | + $this->assertSame($expectedCount, $count); |
|
218 | + $this->assertSame($expectedResult, $result); |
|
219 | + } |
|
220 | + |
|
221 | + |
|
222 | + public static function streamCopyDataProvider(): array { |
|
223 | + return [ |
|
224 | + [0, false, false, false], |
|
225 | + [0, false, \OC::$SERVERROOT . '/tests/data/lorem.txt', false], |
|
226 | + [filesize(\OC::$SERVERROOT . '/tests/data/lorem.txt'), true, \OC::$SERVERROOT . '/tests/data/lorem.txt', \OC::$SERVERROOT . '/tests/data/lorem-copy.txt'], |
|
227 | + [3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'], |
|
228 | + ]; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Tests recursive folder deletion with rmdirr() |
|
233 | + */ |
|
234 | + public function testRecursiveFolderDeletion(): void { |
|
235 | + $baseDir = \OC::$server->getTempManager()->getTemporaryFolder() . '/'; |
|
236 | + mkdir($baseDir . 'a/b/c/d/e', 0777, true); |
|
237 | + mkdir($baseDir . 'a/b/c1/d/e', 0777, true); |
|
238 | + mkdir($baseDir . 'a/b/c2/d/e', 0777, true); |
|
239 | + mkdir($baseDir . 'a/b1/c1/d/e', 0777, true); |
|
240 | + mkdir($baseDir . 'a/b2/c1/d/e', 0777, true); |
|
241 | + mkdir($baseDir . 'a/b3/c1/d/e', 0777, true); |
|
242 | + mkdir($baseDir . 'a1/b', 0777, true); |
|
243 | + mkdir($baseDir . 'a1/c', 0777, true); |
|
244 | + file_put_contents($baseDir . 'a/test.txt', 'Hello file!'); |
|
245 | + file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!'); |
|
246 | + file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!'); |
|
247 | + \OC_Helper::rmdirr($baseDir . 'a'); |
|
248 | + |
|
249 | + $this->assertFalse(file_exists($baseDir . 'a')); |
|
250 | + $this->assertTrue(file_exists($baseDir . 'a1')); |
|
251 | + |
|
252 | + \OC_Helper::rmdirr($baseDir); |
|
253 | + $this->assertFalse(file_exists($baseDir)); |
|
254 | + } |
|
255 | 255 | } |
@@ -222,9 +222,9 @@ discard block |
||
222 | 222 | public static function streamCopyDataProvider(): array { |
223 | 223 | return [ |
224 | 224 | [0, false, false, false], |
225 | - [0, false, \OC::$SERVERROOT . '/tests/data/lorem.txt', false], |
|
226 | - [filesize(\OC::$SERVERROOT . '/tests/data/lorem.txt'), true, \OC::$SERVERROOT . '/tests/data/lorem.txt', \OC::$SERVERROOT . '/tests/data/lorem-copy.txt'], |
|
227 | - [3670, true, \OC::$SERVERROOT . '/tests/data/testimage.png', \OC::$SERVERROOT . '/tests/data/testimage-copy.png'], |
|
225 | + [0, false, \OC::$SERVERROOT.'/tests/data/lorem.txt', false], |
|
226 | + [filesize(\OC::$SERVERROOT.'/tests/data/lorem.txt'), true, \OC::$SERVERROOT.'/tests/data/lorem.txt', \OC::$SERVERROOT.'/tests/data/lorem-copy.txt'], |
|
227 | + [3670, true, \OC::$SERVERROOT.'/tests/data/testimage.png', \OC::$SERVERROOT.'/tests/data/testimage-copy.png'], |
|
228 | 228 | ]; |
229 | 229 | } |
230 | 230 | |
@@ -232,22 +232,22 @@ discard block |
||
232 | 232 | * Tests recursive folder deletion with rmdirr() |
233 | 233 | */ |
234 | 234 | public function testRecursiveFolderDeletion(): void { |
235 | - $baseDir = \OC::$server->getTempManager()->getTemporaryFolder() . '/'; |
|
236 | - mkdir($baseDir . 'a/b/c/d/e', 0777, true); |
|
237 | - mkdir($baseDir . 'a/b/c1/d/e', 0777, true); |
|
238 | - mkdir($baseDir . 'a/b/c2/d/e', 0777, true); |
|
239 | - mkdir($baseDir . 'a/b1/c1/d/e', 0777, true); |
|
240 | - mkdir($baseDir . 'a/b2/c1/d/e', 0777, true); |
|
241 | - mkdir($baseDir . 'a/b3/c1/d/e', 0777, true); |
|
242 | - mkdir($baseDir . 'a1/b', 0777, true); |
|
243 | - mkdir($baseDir . 'a1/c', 0777, true); |
|
244 | - file_put_contents($baseDir . 'a/test.txt', 'Hello file!'); |
|
245 | - file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!'); |
|
246 | - file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!'); |
|
247 | - \OC_Helper::rmdirr($baseDir . 'a'); |
|
248 | - |
|
249 | - $this->assertFalse(file_exists($baseDir . 'a')); |
|
250 | - $this->assertTrue(file_exists($baseDir . 'a1')); |
|
235 | + $baseDir = \OC::$server->getTempManager()->getTemporaryFolder().'/'; |
|
236 | + mkdir($baseDir.'a/b/c/d/e', 0777, true); |
|
237 | + mkdir($baseDir.'a/b/c1/d/e', 0777, true); |
|
238 | + mkdir($baseDir.'a/b/c2/d/e', 0777, true); |
|
239 | + mkdir($baseDir.'a/b1/c1/d/e', 0777, true); |
|
240 | + mkdir($baseDir.'a/b2/c1/d/e', 0777, true); |
|
241 | + mkdir($baseDir.'a/b3/c1/d/e', 0777, true); |
|
242 | + mkdir($baseDir.'a1/b', 0777, true); |
|
243 | + mkdir($baseDir.'a1/c', 0777, true); |
|
244 | + file_put_contents($baseDir.'a/test.txt', 'Hello file!'); |
|
245 | + file_put_contents($baseDir.'a/b1/c1/test one.txt', 'Hello file one!'); |
|
246 | + file_put_contents($baseDir.'a1/b/test two.txt', 'Hello file two!'); |
|
247 | + \OC_Helper::rmdirr($baseDir.'a'); |
|
248 | + |
|
249 | + $this->assertFalse(file_exists($baseDir.'a')); |
|
250 | + $this->assertTrue(file_exists($baseDir.'a1')); |
|
251 | 251 | |
252 | 252 | \OC_Helper::rmdirr($baseDir); |
253 | 253 | $this->assertFalse(file_exists($baseDir)); |
@@ -28,252 +28,252 @@ |
||
28 | 28 | * Class AvatarManagerTest |
29 | 29 | */ |
30 | 30 | class AvatarManagerTest extends \Test\TestCase { |
31 | - /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ |
|
32 | - private $userSession; |
|
33 | - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */ |
|
34 | - private $userManager; |
|
35 | - /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */ |
|
36 | - private $appData; |
|
37 | - /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ |
|
38 | - private $l10n; |
|
39 | - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ |
|
40 | - private $logger; |
|
41 | - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
42 | - private $config; |
|
43 | - /** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
44 | - private $accountManager; |
|
45 | - /** @var AvatarManager | \PHPUnit\Framework\MockObject\MockObject */ |
|
46 | - private $avatarManager; |
|
47 | - /** @var KnownUserService | \PHPUnit\Framework\MockObject\MockObject */ |
|
48 | - private $knownUserService; |
|
49 | - |
|
50 | - protected function setUp(): void { |
|
51 | - parent::setUp(); |
|
52 | - |
|
53 | - $this->userSession = $this->createMock(IUserSession::class); |
|
54 | - $this->userManager = $this->createMock(Manager::class); |
|
55 | - $this->appData = $this->createMock(IAppData::class); |
|
56 | - $this->l10n = $this->createMock(IL10N::class); |
|
57 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
58 | - $this->config = $this->createMock(IConfig::class); |
|
59 | - $this->accountManager = $this->createMock(IAccountManager::class); |
|
60 | - $this->knownUserService = $this->createMock(KnownUserService::class); |
|
61 | - |
|
62 | - $this->avatarManager = new AvatarManager( |
|
63 | - $this->userSession, |
|
64 | - $this->userManager, |
|
65 | - $this->appData, |
|
66 | - $this->l10n, |
|
67 | - $this->logger, |
|
68 | - $this->config, |
|
69 | - $this->accountManager, |
|
70 | - $this->knownUserService |
|
71 | - ); |
|
72 | - } |
|
73 | - |
|
74 | - public function testGetAvatarInvalidUser(): void { |
|
75 | - $this->expectException(\Exception::class); |
|
76 | - $this->expectExceptionMessage('user does not exist'); |
|
77 | - |
|
78 | - $this->userManager |
|
79 | - ->expects($this->once()) |
|
80 | - ->method('get') |
|
81 | - ->with('invalidUser') |
|
82 | - ->willReturn(null); |
|
83 | - |
|
84 | - $this->avatarManager->getAvatar('invalidUser'); |
|
85 | - } |
|
86 | - |
|
87 | - public function testGetAvatarForSelf(): void { |
|
88 | - $user = $this->createMock(User::class); |
|
89 | - $user |
|
90 | - ->expects($this->any()) |
|
91 | - ->method('getUID') |
|
92 | - ->willReturn('valid-user'); |
|
93 | - |
|
94 | - $user |
|
95 | - ->expects($this->any()) |
|
96 | - ->method('isEnabled') |
|
97 | - ->willReturn(true); |
|
98 | - |
|
99 | - // requesting user |
|
100 | - $this->userSession->expects($this->once()) |
|
101 | - ->method('getUser') |
|
102 | - ->willReturn($user); |
|
103 | - |
|
104 | - $this->userManager |
|
105 | - ->expects($this->once()) |
|
106 | - ->method('get') |
|
107 | - ->with('valid-user') |
|
108 | - ->willReturn($user); |
|
109 | - |
|
110 | - $account = $this->createMock(IAccount::class); |
|
111 | - $this->accountManager->expects($this->once()) |
|
112 | - ->method('getAccount') |
|
113 | - ->with($user) |
|
114 | - ->willReturn($account); |
|
115 | - |
|
116 | - $property = $this->createMock(IAccountProperty::class); |
|
117 | - $account->expects($this->once()) |
|
118 | - ->method('getProperty') |
|
119 | - ->with(IAccountManager::PROPERTY_AVATAR) |
|
120 | - ->willReturn($property); |
|
121 | - |
|
122 | - $property->expects($this->once()) |
|
123 | - ->method('getScope') |
|
124 | - ->willReturn(IAccountManager::SCOPE_PRIVATE); |
|
125 | - |
|
126 | - $this->knownUserService->expects($this->any()) |
|
127 | - ->method('isKnownToUser') |
|
128 | - ->with('valid-user', 'valid-user') |
|
129 | - ->willReturn(true); |
|
130 | - |
|
131 | - $folder = $this->createMock(ISimpleFolder::class); |
|
132 | - $this->appData |
|
133 | - ->expects($this->once()) |
|
134 | - ->method('getFolder') |
|
135 | - ->with('valid-user') |
|
136 | - ->willReturn($folder); |
|
137 | - |
|
138 | - $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config); |
|
139 | - $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user')); |
|
140 | - } |
|
141 | - |
|
142 | - public function testGetAvatarValidUserDifferentCasing(): void { |
|
143 | - $user = $this->createMock(User::class); |
|
144 | - $this->userManager->expects($this->once()) |
|
145 | - ->method('get') |
|
146 | - ->with('vaLid-USER') |
|
147 | - ->willReturn($user); |
|
148 | - |
|
149 | - $user->expects($this->once()) |
|
150 | - ->method('getUID') |
|
151 | - ->willReturn('valid-user'); |
|
152 | - |
|
153 | - $user |
|
154 | - ->expects($this->any()) |
|
155 | - ->method('isEnabled') |
|
156 | - ->willReturn(true); |
|
157 | - |
|
158 | - $this->userSession->expects($this->once()) |
|
159 | - ->method('getUser') |
|
160 | - ->willReturn($user); |
|
161 | - |
|
162 | - $folder = $this->createMock(ISimpleFolder::class); |
|
163 | - $this->appData |
|
164 | - ->expects($this->once()) |
|
165 | - ->method('getFolder') |
|
166 | - ->with('valid-user') |
|
167 | - ->willReturn($folder); |
|
168 | - |
|
169 | - $account = $this->createMock(IAccount::class); |
|
170 | - $this->accountManager->expects($this->once()) |
|
171 | - ->method('getAccount') |
|
172 | - ->with($user) |
|
173 | - ->willReturn($account); |
|
174 | - |
|
175 | - $property = $this->createMock(IAccountProperty::class); |
|
176 | - $account->expects($this->once()) |
|
177 | - ->method('getProperty') |
|
178 | - ->with(IAccountManager::PROPERTY_AVATAR) |
|
179 | - ->willReturn($property); |
|
180 | - |
|
181 | - $property->expects($this->once()) |
|
182 | - ->method('getScope') |
|
183 | - ->willReturn(IAccountManager::SCOPE_FEDERATED); |
|
184 | - |
|
185 | - $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config); |
|
186 | - $this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER')); |
|
187 | - } |
|
188 | - |
|
189 | - public static function dataGetAvatarScopes(): array { |
|
190 | - return [ |
|
191 | - // public access cannot see real avatar |
|
192 | - [IAccountManager::SCOPE_PRIVATE, true, false, true], |
|
193 | - // unknown users cannot see real avatar |
|
194 | - [IAccountManager::SCOPE_PRIVATE, false, false, true], |
|
195 | - // known users can see real avatar |
|
196 | - [IAccountManager::SCOPE_PRIVATE, false, true, false], |
|
197 | - [IAccountManager::SCOPE_LOCAL, false, false, false], |
|
198 | - [IAccountManager::SCOPE_LOCAL, true, false, false], |
|
199 | - [IAccountManager::SCOPE_FEDERATED, false, false, false], |
|
200 | - [IAccountManager::SCOPE_FEDERATED, true, false, false], |
|
201 | - [IAccountManager::SCOPE_PUBLISHED, false, false, false], |
|
202 | - [IAccountManager::SCOPE_PUBLISHED, true, false, false], |
|
203 | - ]; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @dataProvider dataGetAvatarScopes |
|
208 | - */ |
|
209 | - public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $expectedPlaceholder): void { |
|
210 | - if ($isPublicCall) { |
|
211 | - $requestingUser = null; |
|
212 | - } else { |
|
213 | - $requestingUser = $this->createMock(IUser::class); |
|
214 | - $requestingUser->method('getUID')->willReturn('requesting-user'); |
|
215 | - } |
|
216 | - |
|
217 | - // requesting user |
|
218 | - $this->userSession->expects($this->once()) |
|
219 | - ->method('getUser') |
|
220 | - ->willReturn($requestingUser); |
|
221 | - |
|
222 | - $user = $this->createMock(User::class); |
|
223 | - $user |
|
224 | - ->expects($this->once()) |
|
225 | - ->method('getUID') |
|
226 | - ->willReturn('valid-user'); |
|
227 | - |
|
228 | - $user |
|
229 | - ->expects($this->any()) |
|
230 | - ->method('isEnabled') |
|
231 | - ->willReturn(true); |
|
232 | - |
|
233 | - $this->userManager |
|
234 | - ->expects($this->once()) |
|
235 | - ->method('get') |
|
236 | - ->with('valid-user') |
|
237 | - ->willReturn($user); |
|
238 | - |
|
239 | - $account = $this->createMock(IAccount::class); |
|
240 | - $this->accountManager->expects($this->once()) |
|
241 | - ->method('getAccount') |
|
242 | - ->with($user) |
|
243 | - ->willReturn($account); |
|
244 | - |
|
245 | - $property = $this->createMock(IAccountProperty::class); |
|
246 | - $account->expects($this->once()) |
|
247 | - ->method('getProperty') |
|
248 | - ->with(IAccountManager::PROPERTY_AVATAR) |
|
249 | - ->willReturn($property); |
|
250 | - |
|
251 | - $property->expects($this->once()) |
|
252 | - ->method('getScope') |
|
253 | - ->willReturn($avatarScope); |
|
254 | - |
|
255 | - $folder = $this->createMock(ISimpleFolder::class); |
|
256 | - $this->appData |
|
257 | - ->expects($this->once()) |
|
258 | - ->method('getFolder') |
|
259 | - ->with('valid-user') |
|
260 | - ->willReturn($folder); |
|
261 | - |
|
262 | - if (!$isPublicCall) { |
|
263 | - $this->knownUserService->expects($this->any()) |
|
264 | - ->method('isKnownToUser') |
|
265 | - ->with('requesting-user', 'valid-user') |
|
266 | - ->willReturn($isKnownUser); |
|
267 | - } else { |
|
268 | - $this->knownUserService->expects($this->never()) |
|
269 | - ->method('isKnownToUser'); |
|
270 | - } |
|
271 | - |
|
272 | - if ($expectedPlaceholder) { |
|
273 | - $expected = new PlaceholderAvatar($folder, $user, $this->createMock(LoggerInterface::class)); |
|
274 | - } else { |
|
275 | - $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config); |
|
276 | - } |
|
277 | - $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user')); |
|
278 | - } |
|
31 | + /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ |
|
32 | + private $userSession; |
|
33 | + /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */ |
|
34 | + private $userManager; |
|
35 | + /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */ |
|
36 | + private $appData; |
|
37 | + /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ |
|
38 | + private $l10n; |
|
39 | + /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ |
|
40 | + private $logger; |
|
41 | + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
42 | + private $config; |
|
43 | + /** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */ |
|
44 | + private $accountManager; |
|
45 | + /** @var AvatarManager | \PHPUnit\Framework\MockObject\MockObject */ |
|
46 | + private $avatarManager; |
|
47 | + /** @var KnownUserService | \PHPUnit\Framework\MockObject\MockObject */ |
|
48 | + private $knownUserService; |
|
49 | + |
|
50 | + protected function setUp(): void { |
|
51 | + parent::setUp(); |
|
52 | + |
|
53 | + $this->userSession = $this->createMock(IUserSession::class); |
|
54 | + $this->userManager = $this->createMock(Manager::class); |
|
55 | + $this->appData = $this->createMock(IAppData::class); |
|
56 | + $this->l10n = $this->createMock(IL10N::class); |
|
57 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
58 | + $this->config = $this->createMock(IConfig::class); |
|
59 | + $this->accountManager = $this->createMock(IAccountManager::class); |
|
60 | + $this->knownUserService = $this->createMock(KnownUserService::class); |
|
61 | + |
|
62 | + $this->avatarManager = new AvatarManager( |
|
63 | + $this->userSession, |
|
64 | + $this->userManager, |
|
65 | + $this->appData, |
|
66 | + $this->l10n, |
|
67 | + $this->logger, |
|
68 | + $this->config, |
|
69 | + $this->accountManager, |
|
70 | + $this->knownUserService |
|
71 | + ); |
|
72 | + } |
|
73 | + |
|
74 | + public function testGetAvatarInvalidUser(): void { |
|
75 | + $this->expectException(\Exception::class); |
|
76 | + $this->expectExceptionMessage('user does not exist'); |
|
77 | + |
|
78 | + $this->userManager |
|
79 | + ->expects($this->once()) |
|
80 | + ->method('get') |
|
81 | + ->with('invalidUser') |
|
82 | + ->willReturn(null); |
|
83 | + |
|
84 | + $this->avatarManager->getAvatar('invalidUser'); |
|
85 | + } |
|
86 | + |
|
87 | + public function testGetAvatarForSelf(): void { |
|
88 | + $user = $this->createMock(User::class); |
|
89 | + $user |
|
90 | + ->expects($this->any()) |
|
91 | + ->method('getUID') |
|
92 | + ->willReturn('valid-user'); |
|
93 | + |
|
94 | + $user |
|
95 | + ->expects($this->any()) |
|
96 | + ->method('isEnabled') |
|
97 | + ->willReturn(true); |
|
98 | + |
|
99 | + // requesting user |
|
100 | + $this->userSession->expects($this->once()) |
|
101 | + ->method('getUser') |
|
102 | + ->willReturn($user); |
|
103 | + |
|
104 | + $this->userManager |
|
105 | + ->expects($this->once()) |
|
106 | + ->method('get') |
|
107 | + ->with('valid-user') |
|
108 | + ->willReturn($user); |
|
109 | + |
|
110 | + $account = $this->createMock(IAccount::class); |
|
111 | + $this->accountManager->expects($this->once()) |
|
112 | + ->method('getAccount') |
|
113 | + ->with($user) |
|
114 | + ->willReturn($account); |
|
115 | + |
|
116 | + $property = $this->createMock(IAccountProperty::class); |
|
117 | + $account->expects($this->once()) |
|
118 | + ->method('getProperty') |
|
119 | + ->with(IAccountManager::PROPERTY_AVATAR) |
|
120 | + ->willReturn($property); |
|
121 | + |
|
122 | + $property->expects($this->once()) |
|
123 | + ->method('getScope') |
|
124 | + ->willReturn(IAccountManager::SCOPE_PRIVATE); |
|
125 | + |
|
126 | + $this->knownUserService->expects($this->any()) |
|
127 | + ->method('isKnownToUser') |
|
128 | + ->with('valid-user', 'valid-user') |
|
129 | + ->willReturn(true); |
|
130 | + |
|
131 | + $folder = $this->createMock(ISimpleFolder::class); |
|
132 | + $this->appData |
|
133 | + ->expects($this->once()) |
|
134 | + ->method('getFolder') |
|
135 | + ->with('valid-user') |
|
136 | + ->willReturn($folder); |
|
137 | + |
|
138 | + $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config); |
|
139 | + $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user')); |
|
140 | + } |
|
141 | + |
|
142 | + public function testGetAvatarValidUserDifferentCasing(): void { |
|
143 | + $user = $this->createMock(User::class); |
|
144 | + $this->userManager->expects($this->once()) |
|
145 | + ->method('get') |
|
146 | + ->with('vaLid-USER') |
|
147 | + ->willReturn($user); |
|
148 | + |
|
149 | + $user->expects($this->once()) |
|
150 | + ->method('getUID') |
|
151 | + ->willReturn('valid-user'); |
|
152 | + |
|
153 | + $user |
|
154 | + ->expects($this->any()) |
|
155 | + ->method('isEnabled') |
|
156 | + ->willReturn(true); |
|
157 | + |
|
158 | + $this->userSession->expects($this->once()) |
|
159 | + ->method('getUser') |
|
160 | + ->willReturn($user); |
|
161 | + |
|
162 | + $folder = $this->createMock(ISimpleFolder::class); |
|
163 | + $this->appData |
|
164 | + ->expects($this->once()) |
|
165 | + ->method('getFolder') |
|
166 | + ->with('valid-user') |
|
167 | + ->willReturn($folder); |
|
168 | + |
|
169 | + $account = $this->createMock(IAccount::class); |
|
170 | + $this->accountManager->expects($this->once()) |
|
171 | + ->method('getAccount') |
|
172 | + ->with($user) |
|
173 | + ->willReturn($account); |
|
174 | + |
|
175 | + $property = $this->createMock(IAccountProperty::class); |
|
176 | + $account->expects($this->once()) |
|
177 | + ->method('getProperty') |
|
178 | + ->with(IAccountManager::PROPERTY_AVATAR) |
|
179 | + ->willReturn($property); |
|
180 | + |
|
181 | + $property->expects($this->once()) |
|
182 | + ->method('getScope') |
|
183 | + ->willReturn(IAccountManager::SCOPE_FEDERATED); |
|
184 | + |
|
185 | + $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config); |
|
186 | + $this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER')); |
|
187 | + } |
|
188 | + |
|
189 | + public static function dataGetAvatarScopes(): array { |
|
190 | + return [ |
|
191 | + // public access cannot see real avatar |
|
192 | + [IAccountManager::SCOPE_PRIVATE, true, false, true], |
|
193 | + // unknown users cannot see real avatar |
|
194 | + [IAccountManager::SCOPE_PRIVATE, false, false, true], |
|
195 | + // known users can see real avatar |
|
196 | + [IAccountManager::SCOPE_PRIVATE, false, true, false], |
|
197 | + [IAccountManager::SCOPE_LOCAL, false, false, false], |
|
198 | + [IAccountManager::SCOPE_LOCAL, true, false, false], |
|
199 | + [IAccountManager::SCOPE_FEDERATED, false, false, false], |
|
200 | + [IAccountManager::SCOPE_FEDERATED, true, false, false], |
|
201 | + [IAccountManager::SCOPE_PUBLISHED, false, false, false], |
|
202 | + [IAccountManager::SCOPE_PUBLISHED, true, false, false], |
|
203 | + ]; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @dataProvider dataGetAvatarScopes |
|
208 | + */ |
|
209 | + public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $expectedPlaceholder): void { |
|
210 | + if ($isPublicCall) { |
|
211 | + $requestingUser = null; |
|
212 | + } else { |
|
213 | + $requestingUser = $this->createMock(IUser::class); |
|
214 | + $requestingUser->method('getUID')->willReturn('requesting-user'); |
|
215 | + } |
|
216 | + |
|
217 | + // requesting user |
|
218 | + $this->userSession->expects($this->once()) |
|
219 | + ->method('getUser') |
|
220 | + ->willReturn($requestingUser); |
|
221 | + |
|
222 | + $user = $this->createMock(User::class); |
|
223 | + $user |
|
224 | + ->expects($this->once()) |
|
225 | + ->method('getUID') |
|
226 | + ->willReturn('valid-user'); |
|
227 | + |
|
228 | + $user |
|
229 | + ->expects($this->any()) |
|
230 | + ->method('isEnabled') |
|
231 | + ->willReturn(true); |
|
232 | + |
|
233 | + $this->userManager |
|
234 | + ->expects($this->once()) |
|
235 | + ->method('get') |
|
236 | + ->with('valid-user') |
|
237 | + ->willReturn($user); |
|
238 | + |
|
239 | + $account = $this->createMock(IAccount::class); |
|
240 | + $this->accountManager->expects($this->once()) |
|
241 | + ->method('getAccount') |
|
242 | + ->with($user) |
|
243 | + ->willReturn($account); |
|
244 | + |
|
245 | + $property = $this->createMock(IAccountProperty::class); |
|
246 | + $account->expects($this->once()) |
|
247 | + ->method('getProperty') |
|
248 | + ->with(IAccountManager::PROPERTY_AVATAR) |
|
249 | + ->willReturn($property); |
|
250 | + |
|
251 | + $property->expects($this->once()) |
|
252 | + ->method('getScope') |
|
253 | + ->willReturn($avatarScope); |
|
254 | + |
|
255 | + $folder = $this->createMock(ISimpleFolder::class); |
|
256 | + $this->appData |
|
257 | + ->expects($this->once()) |
|
258 | + ->method('getFolder') |
|
259 | + ->with('valid-user') |
|
260 | + ->willReturn($folder); |
|
261 | + |
|
262 | + if (!$isPublicCall) { |
|
263 | + $this->knownUserService->expects($this->any()) |
|
264 | + ->method('isKnownToUser') |
|
265 | + ->with('requesting-user', 'valid-user') |
|
266 | + ->willReturn($isKnownUser); |
|
267 | + } else { |
|
268 | + $this->knownUserService->expects($this->never()) |
|
269 | + ->method('isKnownToUser'); |
|
270 | + } |
|
271 | + |
|
272 | + if ($expectedPlaceholder) { |
|
273 | + $expected = new PlaceholderAvatar($folder, $user, $this->createMock(LoggerInterface::class)); |
|
274 | + } else { |
|
275 | + $expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config); |
|
276 | + } |
|
277 | + $this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user')); |
|
278 | + } |
|
279 | 279 | } |
@@ -18,282 +18,282 @@ |
||
18 | 18 | use Psr\Log\LoggerInterface; |
19 | 19 | |
20 | 20 | class UserAvatarTest extends \Test\TestCase { |
21 | - /** @var SimpleFolder | \PHPUnit\Framework\MockObject\MockObject */ |
|
22 | - private $folder; |
|
23 | - |
|
24 | - /** @var \OC\Avatar\UserAvatar */ |
|
25 | - private $avatar; |
|
26 | - |
|
27 | - /** @var \OC\User\User | \PHPUnit\Framework\MockObject\MockObject $user */ |
|
28 | - private $user; |
|
29 | - |
|
30 | - /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
31 | - private $config; |
|
32 | - |
|
33 | - protected function setUp(): void { |
|
34 | - parent::setUp(); |
|
35 | - |
|
36 | - $this->folder = $this->createMock(SimpleFolder::class); |
|
37 | - // abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9 |
|
38 | - $this->user = $this->getUserWithDisplayName('abcdefghi'); |
|
39 | - $this->config = $this->createMock(IConfig::class); |
|
40 | - |
|
41 | - $this->avatar = $this->getUserAvatar($this->user); |
|
42 | - } |
|
43 | - |
|
44 | - public static function avatarTextData(): array { |
|
45 | - return [ |
|
46 | - ['', '?'], |
|
47 | - ['matchish', 'M'], |
|
48 | - ['Firstname Lastname', 'FL'], |
|
49 | - ['Firstname Lastname Rest', 'FL'], |
|
50 | - ]; |
|
51 | - } |
|
52 | - |
|
53 | - public function testGetNoAvatar(): void { |
|
54 | - $file = $this->createMock(ISimpleFile::class); |
|
55 | - $this->folder->method('newFile') |
|
56 | - ->willReturn($file); |
|
57 | - |
|
58 | - $this->folder->method('getFile') |
|
59 | - ->willReturnCallback(function (string $path) { |
|
60 | - if ($path === 'avatar.64.png') { |
|
61 | - throw new NotFoundException(); |
|
62 | - } |
|
63 | - }); |
|
64 | - $this->folder->method('fileExists') |
|
65 | - ->willReturnCallback(function ($path) { |
|
66 | - if ($path === 'generated') { |
|
67 | - return true; |
|
68 | - } |
|
69 | - return false; |
|
70 | - }); |
|
71 | - |
|
72 | - $data = null; |
|
73 | - $file->method('putContent') |
|
74 | - ->with($this->callback(function ($d) use (&$data) { |
|
75 | - $data = $d; |
|
76 | - return true; |
|
77 | - })); |
|
78 | - |
|
79 | - $file->method('getContent') |
|
80 | - ->willReturnCallback(function () use (&$data) { |
|
81 | - return $data; |
|
82 | - }); |
|
83 | - |
|
84 | - $result = $this->avatar->get(); |
|
85 | - $this->assertTrue($result->valid()); |
|
86 | - } |
|
87 | - |
|
88 | - public function testGetAvatarSizeMatch(): void { |
|
89 | - $this->folder->method('fileExists') |
|
90 | - ->willReturnMap([ |
|
91 | - ['avatar.jpg', true], |
|
92 | - ['avatar.128.jpg', true], |
|
93 | - ['generated', false], |
|
94 | - ]); |
|
95 | - |
|
96 | - $expected = new Image(); |
|
97 | - $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
98 | - |
|
99 | - $file = $this->createMock(ISimpleFile::class); |
|
100 | - $file->method('getContent')->willReturn($expected->data()); |
|
101 | - $this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file); |
|
102 | - |
|
103 | - $this->assertEquals($expected->data(), $this->avatar->get(128)->data()); |
|
104 | - } |
|
105 | - |
|
106 | - public function testGetAvatarSizeMinusOne(): void { |
|
107 | - $this->folder->method('fileExists') |
|
108 | - ->willReturnMap([ |
|
109 | - ['avatar.jpg', true], |
|
110 | - ['generated', false], |
|
111 | - ]); |
|
112 | - |
|
113 | - $expected = new Image(); |
|
114 | - $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
115 | - |
|
116 | - $file = $this->createMock(ISimpleFile::class); |
|
117 | - $file->method('getContent')->willReturn($expected->data()); |
|
118 | - $this->folder->method('getFile')->with('avatar.jpg')->willReturn($file); |
|
119 | - |
|
120 | - $this->assertEquals($expected->data(), $this->avatar->get(-1)->data()); |
|
121 | - } |
|
122 | - |
|
123 | - public function testGetAvatarNoSizeMatch(): void { |
|
124 | - $this->folder->method('fileExists') |
|
125 | - ->willReturnMap([ |
|
126 | - ['avatar.jpg', false], |
|
127 | - ['avatar.png', true], |
|
128 | - ['avatar.32.png', false], |
|
129 | - ['generated', false], |
|
130 | - ]); |
|
131 | - |
|
132 | - $expected = new Image(); |
|
133 | - $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
134 | - $expected2 = new Image(); |
|
135 | - $expected2->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
136 | - $expected2->resize(32); |
|
137 | - |
|
138 | - $file = $this->createMock(ISimpleFile::class); |
|
139 | - $file->method('getContent')->willReturn($expected->data()); |
|
140 | - |
|
141 | - $this->folder->method('getFile') |
|
142 | - ->willReturnCallback( |
|
143 | - function ($path) use ($file) { |
|
144 | - if ($path === 'avatar.png') { |
|
145 | - return $file; |
|
146 | - } else { |
|
147 | - throw new \OCP\Files\NotFoundException; |
|
148 | - } |
|
149 | - } |
|
150 | - ); |
|
151 | - |
|
152 | - $newFile = $this->createMock(ISimpleFile::class); |
|
153 | - $newFile->expects($this->once()) |
|
154 | - ->method('putContent') |
|
155 | - ->with($expected2->data()); |
|
156 | - $newFile->expects($this->once()) |
|
157 | - ->method('getContent') |
|
158 | - ->willReturn($expected2->data()); |
|
159 | - $this->folder->expects($this->once()) |
|
160 | - ->method('newFile') |
|
161 | - ->with('avatar.32.png') |
|
162 | - ->willReturn($newFile); |
|
163 | - |
|
164 | - $this->assertEquals($expected2->data(), $this->avatar->get(32)->data()); |
|
165 | - } |
|
166 | - |
|
167 | - public function testExistsNo(): void { |
|
168 | - $this->assertFalse($this->avatar->exists()); |
|
169 | - } |
|
170 | - |
|
171 | - public function testExiststJPG(): void { |
|
172 | - $this->folder->method('fileExists') |
|
173 | - ->willReturnMap([ |
|
174 | - ['avatar.jpg', true], |
|
175 | - ['avatar.png', false], |
|
176 | - ]); |
|
177 | - $this->assertTrue($this->avatar->exists()); |
|
178 | - } |
|
179 | - |
|
180 | - public function testExistsPNG(): void { |
|
181 | - $this->folder->method('fileExists') |
|
182 | - ->willReturnMap([ |
|
183 | - ['avatar.jpg', false], |
|
184 | - ['avatar.png', true], |
|
185 | - ]); |
|
186 | - $this->assertTrue($this->avatar->exists()); |
|
187 | - } |
|
188 | - |
|
189 | - public function testSetAvatar(): void { |
|
190 | - $avatarFileJPG = $this->createMock(File::class); |
|
191 | - $avatarFileJPG->method('getName') |
|
192 | - ->willReturn('avatar.jpg'); |
|
193 | - $avatarFileJPG->expects($this->once())->method('delete'); |
|
194 | - |
|
195 | - $avatarFilePNG = $this->createMock(File::class); |
|
196 | - $avatarFilePNG->method('getName') |
|
197 | - ->willReturn('avatar.png'); |
|
198 | - $avatarFilePNG->expects($this->once())->method('delete'); |
|
199 | - |
|
200 | - $resizedAvatarFile = $this->createMock(File::class); |
|
201 | - $resizedAvatarFile->method('getName') |
|
202 | - ->willReturn('avatar.32.jpg'); |
|
203 | - $resizedAvatarFile->expects($this->once())->method('delete'); |
|
204 | - |
|
205 | - $this->folder->method('getDirectoryListing') |
|
206 | - ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile]); |
|
207 | - |
|
208 | - $generated = $this->createMock(ISimpleFile::class); |
|
209 | - $this->folder->method('getFile') |
|
210 | - ->with('generated') |
|
211 | - ->willReturn($generated); |
|
212 | - |
|
213 | - $newFile = $this->createMock(ISimpleFile::class); |
|
214 | - $this->folder->expects($this->once()) |
|
215 | - ->method('newFile') |
|
216 | - ->with('avatar.png') |
|
217 | - ->willReturn($newFile); |
|
218 | - |
|
219 | - $image = new Image(); |
|
220 | - $image->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
221 | - $newFile->expects($this->once()) |
|
222 | - ->method('putContent') |
|
223 | - ->with($image->data()); |
|
224 | - |
|
225 | - $this->config->expects($this->exactly(3)) |
|
226 | - ->method('setUserValue'); |
|
227 | - $this->config->expects($this->once()) |
|
228 | - ->method('getUserValue'); |
|
229 | - |
|
230 | - $this->user->expects($this->exactly(1))->method('triggerChange'); |
|
231 | - |
|
232 | - $this->avatar->set($image->data()); |
|
233 | - } |
|
234 | - |
|
235 | - public function testGenerateSvgAvatar(): void { |
|
236 | - $avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64, false]); |
|
237 | - |
|
238 | - $svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
|
21 | + /** @var SimpleFolder | \PHPUnit\Framework\MockObject\MockObject */ |
|
22 | + private $folder; |
|
23 | + |
|
24 | + /** @var \OC\Avatar\UserAvatar */ |
|
25 | + private $avatar; |
|
26 | + |
|
27 | + /** @var \OC\User\User | \PHPUnit\Framework\MockObject\MockObject $user */ |
|
28 | + private $user; |
|
29 | + |
|
30 | + /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */ |
|
31 | + private $config; |
|
32 | + |
|
33 | + protected function setUp(): void { |
|
34 | + parent::setUp(); |
|
35 | + |
|
36 | + $this->folder = $this->createMock(SimpleFolder::class); |
|
37 | + // abcdefghi is a convenient name that our algorithm convert to our nextcloud blue 0082c9 |
|
38 | + $this->user = $this->getUserWithDisplayName('abcdefghi'); |
|
39 | + $this->config = $this->createMock(IConfig::class); |
|
40 | + |
|
41 | + $this->avatar = $this->getUserAvatar($this->user); |
|
42 | + } |
|
43 | + |
|
44 | + public static function avatarTextData(): array { |
|
45 | + return [ |
|
46 | + ['', '?'], |
|
47 | + ['matchish', 'M'], |
|
48 | + ['Firstname Lastname', 'FL'], |
|
49 | + ['Firstname Lastname Rest', 'FL'], |
|
50 | + ]; |
|
51 | + } |
|
52 | + |
|
53 | + public function testGetNoAvatar(): void { |
|
54 | + $file = $this->createMock(ISimpleFile::class); |
|
55 | + $this->folder->method('newFile') |
|
56 | + ->willReturn($file); |
|
57 | + |
|
58 | + $this->folder->method('getFile') |
|
59 | + ->willReturnCallback(function (string $path) { |
|
60 | + if ($path === 'avatar.64.png') { |
|
61 | + throw new NotFoundException(); |
|
62 | + } |
|
63 | + }); |
|
64 | + $this->folder->method('fileExists') |
|
65 | + ->willReturnCallback(function ($path) { |
|
66 | + if ($path === 'generated') { |
|
67 | + return true; |
|
68 | + } |
|
69 | + return false; |
|
70 | + }); |
|
71 | + |
|
72 | + $data = null; |
|
73 | + $file->method('putContent') |
|
74 | + ->with($this->callback(function ($d) use (&$data) { |
|
75 | + $data = $d; |
|
76 | + return true; |
|
77 | + })); |
|
78 | + |
|
79 | + $file->method('getContent') |
|
80 | + ->willReturnCallback(function () use (&$data) { |
|
81 | + return $data; |
|
82 | + }); |
|
83 | + |
|
84 | + $result = $this->avatar->get(); |
|
85 | + $this->assertTrue($result->valid()); |
|
86 | + } |
|
87 | + |
|
88 | + public function testGetAvatarSizeMatch(): void { |
|
89 | + $this->folder->method('fileExists') |
|
90 | + ->willReturnMap([ |
|
91 | + ['avatar.jpg', true], |
|
92 | + ['avatar.128.jpg', true], |
|
93 | + ['generated', false], |
|
94 | + ]); |
|
95 | + |
|
96 | + $expected = new Image(); |
|
97 | + $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
98 | + |
|
99 | + $file = $this->createMock(ISimpleFile::class); |
|
100 | + $file->method('getContent')->willReturn($expected->data()); |
|
101 | + $this->folder->method('getFile')->with('avatar.128.jpg')->willReturn($file); |
|
102 | + |
|
103 | + $this->assertEquals($expected->data(), $this->avatar->get(128)->data()); |
|
104 | + } |
|
105 | + |
|
106 | + public function testGetAvatarSizeMinusOne(): void { |
|
107 | + $this->folder->method('fileExists') |
|
108 | + ->willReturnMap([ |
|
109 | + ['avatar.jpg', true], |
|
110 | + ['generated', false], |
|
111 | + ]); |
|
112 | + |
|
113 | + $expected = new Image(); |
|
114 | + $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
115 | + |
|
116 | + $file = $this->createMock(ISimpleFile::class); |
|
117 | + $file->method('getContent')->willReturn($expected->data()); |
|
118 | + $this->folder->method('getFile')->with('avatar.jpg')->willReturn($file); |
|
119 | + |
|
120 | + $this->assertEquals($expected->data(), $this->avatar->get(-1)->data()); |
|
121 | + } |
|
122 | + |
|
123 | + public function testGetAvatarNoSizeMatch(): void { |
|
124 | + $this->folder->method('fileExists') |
|
125 | + ->willReturnMap([ |
|
126 | + ['avatar.jpg', false], |
|
127 | + ['avatar.png', true], |
|
128 | + ['avatar.32.png', false], |
|
129 | + ['generated', false], |
|
130 | + ]); |
|
131 | + |
|
132 | + $expected = new Image(); |
|
133 | + $expected->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
134 | + $expected2 = new Image(); |
|
135 | + $expected2->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
136 | + $expected2->resize(32); |
|
137 | + |
|
138 | + $file = $this->createMock(ISimpleFile::class); |
|
139 | + $file->method('getContent')->willReturn($expected->data()); |
|
140 | + |
|
141 | + $this->folder->method('getFile') |
|
142 | + ->willReturnCallback( |
|
143 | + function ($path) use ($file) { |
|
144 | + if ($path === 'avatar.png') { |
|
145 | + return $file; |
|
146 | + } else { |
|
147 | + throw new \OCP\Files\NotFoundException; |
|
148 | + } |
|
149 | + } |
|
150 | + ); |
|
151 | + |
|
152 | + $newFile = $this->createMock(ISimpleFile::class); |
|
153 | + $newFile->expects($this->once()) |
|
154 | + ->method('putContent') |
|
155 | + ->with($expected2->data()); |
|
156 | + $newFile->expects($this->once()) |
|
157 | + ->method('getContent') |
|
158 | + ->willReturn($expected2->data()); |
|
159 | + $this->folder->expects($this->once()) |
|
160 | + ->method('newFile') |
|
161 | + ->with('avatar.32.png') |
|
162 | + ->willReturn($newFile); |
|
163 | + |
|
164 | + $this->assertEquals($expected2->data(), $this->avatar->get(32)->data()); |
|
165 | + } |
|
166 | + |
|
167 | + public function testExistsNo(): void { |
|
168 | + $this->assertFalse($this->avatar->exists()); |
|
169 | + } |
|
170 | + |
|
171 | + public function testExiststJPG(): void { |
|
172 | + $this->folder->method('fileExists') |
|
173 | + ->willReturnMap([ |
|
174 | + ['avatar.jpg', true], |
|
175 | + ['avatar.png', false], |
|
176 | + ]); |
|
177 | + $this->assertTrue($this->avatar->exists()); |
|
178 | + } |
|
179 | + |
|
180 | + public function testExistsPNG(): void { |
|
181 | + $this->folder->method('fileExists') |
|
182 | + ->willReturnMap([ |
|
183 | + ['avatar.jpg', false], |
|
184 | + ['avatar.png', true], |
|
185 | + ]); |
|
186 | + $this->assertTrue($this->avatar->exists()); |
|
187 | + } |
|
188 | + |
|
189 | + public function testSetAvatar(): void { |
|
190 | + $avatarFileJPG = $this->createMock(File::class); |
|
191 | + $avatarFileJPG->method('getName') |
|
192 | + ->willReturn('avatar.jpg'); |
|
193 | + $avatarFileJPG->expects($this->once())->method('delete'); |
|
194 | + |
|
195 | + $avatarFilePNG = $this->createMock(File::class); |
|
196 | + $avatarFilePNG->method('getName') |
|
197 | + ->willReturn('avatar.png'); |
|
198 | + $avatarFilePNG->expects($this->once())->method('delete'); |
|
199 | + |
|
200 | + $resizedAvatarFile = $this->createMock(File::class); |
|
201 | + $resizedAvatarFile->method('getName') |
|
202 | + ->willReturn('avatar.32.jpg'); |
|
203 | + $resizedAvatarFile->expects($this->once())->method('delete'); |
|
204 | + |
|
205 | + $this->folder->method('getDirectoryListing') |
|
206 | + ->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile]); |
|
207 | + |
|
208 | + $generated = $this->createMock(ISimpleFile::class); |
|
209 | + $this->folder->method('getFile') |
|
210 | + ->with('generated') |
|
211 | + ->willReturn($generated); |
|
212 | + |
|
213 | + $newFile = $this->createMock(ISimpleFile::class); |
|
214 | + $this->folder->expects($this->once()) |
|
215 | + ->method('newFile') |
|
216 | + ->with('avatar.png') |
|
217 | + ->willReturn($newFile); |
|
218 | + |
|
219 | + $image = new Image(); |
|
220 | + $image->loadFromFile(\OC::$SERVERROOT . '/tests/data/testavatar.png'); |
|
221 | + $newFile->expects($this->once()) |
|
222 | + ->method('putContent') |
|
223 | + ->with($image->data()); |
|
224 | + |
|
225 | + $this->config->expects($this->exactly(3)) |
|
226 | + ->method('setUserValue'); |
|
227 | + $this->config->expects($this->once()) |
|
228 | + ->method('getUserValue'); |
|
229 | + |
|
230 | + $this->user->expects($this->exactly(1))->method('triggerChange'); |
|
231 | + |
|
232 | + $this->avatar->set($image->data()); |
|
233 | + } |
|
234 | + |
|
235 | + public function testGenerateSvgAvatar(): void { |
|
236 | + $avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64, false]); |
|
237 | + |
|
238 | + $svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
|
239 | 239 | <svg width="64" height="64" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg"> |
240 | 240 | <rect width="100%" height="100%" fill="#e5f2f9"></rect> |
241 | 241 | <text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#0082c9">A</text> |
242 | 242 | </svg>'; |
243 | - $this->assertEquals($avatar, $svg); |
|
244 | - } |
|
245 | - |
|
246 | - |
|
247 | - /** |
|
248 | - * @dataProvider avatarTextData |
|
249 | - */ |
|
250 | - public function testGetAvatarText($displayName, $expectedAvatarText): void { |
|
251 | - $user = $this->getUserWithDisplayName($displayName); |
|
252 | - $avatar = $this->getUserAvatar($user); |
|
253 | - |
|
254 | - $avatarText = $this->invokePrivate($avatar, 'getAvatarText'); |
|
255 | - $this->assertEquals($expectedAvatarText, $avatarText); |
|
256 | - } |
|
257 | - |
|
258 | - public function testHashToInt(): void { |
|
259 | - $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); |
|
260 | - $this->assertTrue(gettype($hashToInt) === 'integer'); |
|
261 | - } |
|
262 | - |
|
263 | - public function testMixPalette(): void { |
|
264 | - $colorFrom = new \OCP\Color(0, 0, 0); |
|
265 | - $colorTo = new \OCP\Color(6, 12, 18); |
|
266 | - $steps = 6; |
|
267 | - $palette = \OCP\Color::mixPalette($steps, $colorFrom, $colorTo); |
|
268 | - foreach ($palette as $j => $color) { |
|
269 | - // calc increment |
|
270 | - $incR = $colorTo->red() / $steps * $j; |
|
271 | - $incG = $colorTo->green() / $steps * $j; |
|
272 | - $incB = $colorTo->blue() / $steps * $j; |
|
273 | - // ensure everything is equal |
|
274 | - $this->assertEquals($color, new \OCP\Color($incR, $incG, $incB)); |
|
275 | - } |
|
276 | - $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); |
|
277 | - $this->assertTrue(gettype($hashToInt) === 'integer'); |
|
278 | - } |
|
279 | - |
|
280 | - private function getUserWithDisplayName($name) { |
|
281 | - $user = $this->createMock(User::class); |
|
282 | - $user->method('getDisplayName')->willReturn($name); |
|
283 | - return $user; |
|
284 | - } |
|
285 | - |
|
286 | - private function getUserAvatar($user) { |
|
287 | - /** @var \OCP\IL10N | \PHPUnit\Framework\MockObject\MockObject $l */ |
|
288 | - $l = $this->createMock(IL10N::class); |
|
289 | - $l->method('t')->willReturnArgument(0); |
|
290 | - |
|
291 | - return new \OC\Avatar\UserAvatar( |
|
292 | - $this->folder, |
|
293 | - $l, |
|
294 | - $user, |
|
295 | - $this->createMock(LoggerInterface::class), |
|
296 | - $this->config |
|
297 | - ); |
|
298 | - } |
|
243 | + $this->assertEquals($avatar, $svg); |
|
244 | + } |
|
245 | + |
|
246 | + |
|
247 | + /** |
|
248 | + * @dataProvider avatarTextData |
|
249 | + */ |
|
250 | + public function testGetAvatarText($displayName, $expectedAvatarText): void { |
|
251 | + $user = $this->getUserWithDisplayName($displayName); |
|
252 | + $avatar = $this->getUserAvatar($user); |
|
253 | + |
|
254 | + $avatarText = $this->invokePrivate($avatar, 'getAvatarText'); |
|
255 | + $this->assertEquals($expectedAvatarText, $avatarText); |
|
256 | + } |
|
257 | + |
|
258 | + public function testHashToInt(): void { |
|
259 | + $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); |
|
260 | + $this->assertTrue(gettype($hashToInt) === 'integer'); |
|
261 | + } |
|
262 | + |
|
263 | + public function testMixPalette(): void { |
|
264 | + $colorFrom = new \OCP\Color(0, 0, 0); |
|
265 | + $colorTo = new \OCP\Color(6, 12, 18); |
|
266 | + $steps = 6; |
|
267 | + $palette = \OCP\Color::mixPalette($steps, $colorFrom, $colorTo); |
|
268 | + foreach ($palette as $j => $color) { |
|
269 | + // calc increment |
|
270 | + $incR = $colorTo->red() / $steps * $j; |
|
271 | + $incG = $colorTo->green() / $steps * $j; |
|
272 | + $incB = $colorTo->blue() / $steps * $j; |
|
273 | + // ensure everything is equal |
|
274 | + $this->assertEquals($color, new \OCP\Color($incR, $incG, $incB)); |
|
275 | + } |
|
276 | + $hashToInt = $this->invokePrivate($this->avatar, 'hashToInt', ['abcdef', 18]); |
|
277 | + $this->assertTrue(gettype($hashToInt) === 'integer'); |
|
278 | + } |
|
279 | + |
|
280 | + private function getUserWithDisplayName($name) { |
|
281 | + $user = $this->createMock(User::class); |
|
282 | + $user->method('getDisplayName')->willReturn($name); |
|
283 | + return $user; |
|
284 | + } |
|
285 | + |
|
286 | + private function getUserAvatar($user) { |
|
287 | + /** @var \OCP\IL10N | \PHPUnit\Framework\MockObject\MockObject $l */ |
|
288 | + $l = $this->createMock(IL10N::class); |
|
289 | + $l->method('t')->willReturnArgument(0); |
|
290 | + |
|
291 | + return new \OC\Avatar\UserAvatar( |
|
292 | + $this->folder, |
|
293 | + $l, |
|
294 | + $user, |
|
295 | + $this->createMock(LoggerInterface::class), |
|
296 | + $this->config |
|
297 | + ); |
|
298 | + } |
|
299 | 299 | } |
@@ -18,71 +18,71 @@ |
||
18 | 18 | use Test\TestCase; |
19 | 19 | |
20 | 20 | class PsrLoggerAdapterTest extends TestCase { |
21 | - protected Log&MockObject $logger; |
|
22 | - protected PsrLoggerAdapter $loggerAdapter; |
|
21 | + protected Log&MockObject $logger; |
|
22 | + protected PsrLoggerAdapter $loggerAdapter; |
|
23 | 23 | |
24 | - protected function setUp(): void { |
|
25 | - parent::setUp(); |
|
24 | + protected function setUp(): void { |
|
25 | + parent::setUp(); |
|
26 | 26 | |
27 | - $this->logger = $this->createMock(Log::class); |
|
28 | - $this->loggerAdapter = new PsrLoggerAdapter($this->logger); |
|
29 | - } |
|
27 | + $this->logger = $this->createMock(Log::class); |
|
28 | + $this->loggerAdapter = new PsrLoggerAdapter($this->logger); |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @dataProvider dataPsrLoggingLevels |
|
33 | - */ |
|
34 | - public function testLoggingWithPsrLogLevels(string $level, int $expectedLevel): void { |
|
35 | - $this->logger->expects(self::once()) |
|
36 | - ->method('log') |
|
37 | - ->with($expectedLevel, 'test message', ['app' => 'test']); |
|
38 | - $this->loggerAdapter->log($level, 'test message', ['app' => 'test']); |
|
39 | - } |
|
31 | + /** |
|
32 | + * @dataProvider dataPsrLoggingLevels |
|
33 | + */ |
|
34 | + public function testLoggingWithPsrLogLevels(string $level, int $expectedLevel): void { |
|
35 | + $this->logger->expects(self::once()) |
|
36 | + ->method('log') |
|
37 | + ->with($expectedLevel, 'test message', ['app' => 'test']); |
|
38 | + $this->loggerAdapter->log($level, 'test message', ['app' => 'test']); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * @dataProvider dataPsrLoggingLevels |
|
43 | - */ |
|
44 | - public function testLogLevelToInt(string $level, int $expectedLevel): void { |
|
45 | - $this->assertEquals($expectedLevel, PsrLoggerAdapter::logLevelToInt($level)); |
|
46 | - } |
|
41 | + /** |
|
42 | + * @dataProvider dataPsrLoggingLevels |
|
43 | + */ |
|
44 | + public function testLogLevelToInt(string $level, int $expectedLevel): void { |
|
45 | + $this->assertEquals($expectedLevel, PsrLoggerAdapter::logLevelToInt($level)); |
|
46 | + } |
|
47 | 47 | |
48 | - public static function dataPsrLoggingLevels(): array { |
|
49 | - return [ |
|
50 | - [LogLevel::ALERT, ILogger::ERROR], |
|
51 | - [LogLevel::CRITICAL, ILogger::ERROR], |
|
52 | - [LogLevel::DEBUG, ILogger::DEBUG], |
|
53 | - [LogLevel::EMERGENCY, ILogger::FATAL], |
|
54 | - [LogLevel::ERROR, ILogger::ERROR], |
|
55 | - [LogLevel::INFO, ILogger::INFO], |
|
56 | - [LogLevel::NOTICE, ILogger::INFO], |
|
57 | - [LogLevel::WARNING, ILogger::WARN], |
|
58 | - ]; |
|
59 | - } |
|
48 | + public static function dataPsrLoggingLevels(): array { |
|
49 | + return [ |
|
50 | + [LogLevel::ALERT, ILogger::ERROR], |
|
51 | + [LogLevel::CRITICAL, ILogger::ERROR], |
|
52 | + [LogLevel::DEBUG, ILogger::DEBUG], |
|
53 | + [LogLevel::EMERGENCY, ILogger::FATAL], |
|
54 | + [LogLevel::ERROR, ILogger::ERROR], |
|
55 | + [LogLevel::INFO, ILogger::INFO], |
|
56 | + [LogLevel::NOTICE, ILogger::INFO], |
|
57 | + [LogLevel::WARNING, ILogger::WARN], |
|
58 | + ]; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @dataProvider dataInvalidLoggingLevel |
|
63 | - */ |
|
64 | - public function testInvalidLoggingLevel($level): void { |
|
65 | - $this->logger->expects(self::never()) |
|
66 | - ->method('log'); |
|
67 | - $this->expectException(InvalidArgumentException::class); |
|
61 | + /** |
|
62 | + * @dataProvider dataInvalidLoggingLevel |
|
63 | + */ |
|
64 | + public function testInvalidLoggingLevel($level): void { |
|
65 | + $this->logger->expects(self::never()) |
|
66 | + ->method('log'); |
|
67 | + $this->expectException(InvalidArgumentException::class); |
|
68 | 68 | |
69 | - $this->loggerAdapter->log($level, 'valid message'); |
|
70 | - } |
|
69 | + $this->loggerAdapter->log($level, 'valid message'); |
|
70 | + } |
|
71 | 71 | |
72 | - public static function dataInvalidLoggingLevel(): array { |
|
73 | - return [ |
|
74 | - // invalid string |
|
75 | - ['this is not a level'], |
|
76 | - // int out of range |
|
77 | - [ILogger::DEBUG - 1], |
|
78 | - [ILogger::FATAL + 1], |
|
79 | - // float is not allowed |
|
80 | - [1.2345], |
|
81 | - // boolean is not a level |
|
82 | - [true], |
|
83 | - [false], |
|
84 | - // |
|
85 | - [null], |
|
86 | - ]; |
|
87 | - } |
|
72 | + public static function dataInvalidLoggingLevel(): array { |
|
73 | + return [ |
|
74 | + // invalid string |
|
75 | + ['this is not a level'], |
|
76 | + // int out of range |
|
77 | + [ILogger::DEBUG - 1], |
|
78 | + [ILogger::FATAL + 1], |
|
79 | + // float is not allowed |
|
80 | + [1.2345], |
|
81 | + // boolean is not a level |
|
82 | + [true], |
|
83 | + [false], |
|
84 | + // |
|
85 | + [null], |
|
86 | + ]; |
|
87 | + } |
|
88 | 88 | } |
@@ -18,231 +18,231 @@ |
||
18 | 18 | * @group DB |
19 | 19 | */ |
20 | 20 | class HelperStorageTest extends \Test\TestCase { |
21 | - use UserTrait; |
|
22 | - |
|
23 | - /** @var string */ |
|
24 | - private $user; |
|
25 | - /** @var \OC\Files\Storage\Storage */ |
|
26 | - private $storageMock; |
|
27 | - /** @var \OC\Files\Storage\Storage */ |
|
28 | - private $storage; |
|
29 | - private bool $savedQuotaIncludeExternalStorage; |
|
30 | - |
|
31 | - protected function setUp(): void { |
|
32 | - parent::setUp(); |
|
33 | - |
|
34 | - $this->user = $this->getUniqueID('user_'); |
|
35 | - $this->createUser($this->user, $this->user); |
|
36 | - $this->savedQuotaIncludeExternalStorage = $this->getIncludeExternalStorage(); |
|
37 | - |
|
38 | - \OC\Files\Filesystem::tearDown(); |
|
39 | - \OC_User::setUserId($this->user); |
|
40 | - \OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files'); |
|
41 | - |
|
42 | - /** @var IMountManager $manager */ |
|
43 | - $manager = \OC::$server->get(IMountManager::class); |
|
44 | - $manager->removeMount('/' . $this->user); |
|
45 | - |
|
46 | - $this->storageMock = null; |
|
47 | - } |
|
48 | - |
|
49 | - protected function tearDown(): void { |
|
50 | - $this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage); |
|
51 | - $this->user = null; |
|
52 | - |
|
53 | - if ($this->storageMock) { |
|
54 | - $this->storageMock->getCache()->clear(); |
|
55 | - $this->storageMock = null; |
|
56 | - } |
|
57 | - \OC\Files\Filesystem::tearDown(); |
|
58 | - |
|
59 | - \OC_User::setUserId(''); |
|
60 | - \OC::$server->getConfig()->deleteAllUserValues($this->user); |
|
61 | - |
|
62 | - parent::tearDown(); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Returns a storage mock that returns the given value as |
|
67 | - * free space |
|
68 | - * |
|
69 | - * @param int $freeSpace free space value |
|
70 | - * @return \OC\Files\Storage\Storage |
|
71 | - */ |
|
72 | - private function getStorageMock($freeSpace = 12) { |
|
73 | - $this->storageMock = $this->getMockBuilder(Temporary::class) |
|
74 | - ->onlyMethods(['free_space']) |
|
75 | - ->setConstructorArgs([[]]) |
|
76 | - ->getMock(); |
|
77 | - |
|
78 | - $this->storageMock->expects($this->once()) |
|
79 | - ->method('free_space') |
|
80 | - ->willReturn($freeSpace); |
|
81 | - return $this->storageMock; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Test getting the storage info |
|
86 | - */ |
|
87 | - public function testGetStorageInfo(): void { |
|
88 | - $homeStorage = $this->getStorageMock(12); |
|
89 | - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
90 | - $homeStorage->file_put_contents('test.txt', '01234'); |
|
91 | - |
|
92 | - $storageInfo = \OC_Helper::getStorageInfo(''); |
|
93 | - $this->assertEquals(12, $storageInfo['free']); |
|
94 | - $this->assertEquals(5, $storageInfo['used']); |
|
95 | - $this->assertEquals(17, $storageInfo['total']); |
|
96 | - } |
|
97 | - private function getIncludeExternalStorage(): bool { |
|
98 | - $class = new \ReflectionClass(\OC_Helper::class); |
|
99 | - $prop = $class->getProperty('quotaIncludeExternalStorage'); |
|
100 | - $prop->setAccessible(true); |
|
101 | - return $prop->getValue(null) ?? false; |
|
102 | - } |
|
103 | - |
|
104 | - private function setIncludeExternalStorage(bool $include) { |
|
105 | - $class = new \ReflectionClass(\OC_Helper::class); |
|
106 | - $prop = $class->getProperty('quotaIncludeExternalStorage'); |
|
107 | - $prop->setAccessible(true); |
|
108 | - $prop->setValue(null, $include); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Test getting the storage info, ignoring extra mount points |
|
113 | - */ |
|
114 | - public function testGetStorageInfoExcludingExtStorage(): void { |
|
115 | - $homeStorage = $this->getStorageMock(12); |
|
116 | - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
117 | - $homeStorage->file_put_contents('test.txt', '01234'); |
|
118 | - |
|
119 | - $extStorage = new \OC\Files\Storage\Temporary([]); |
|
120 | - $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); |
|
121 | - $extStorage->getScanner()->scan(''); // update root size |
|
122 | - |
|
123 | - $this->setIncludeExternalStorage(false); |
|
124 | - |
|
125 | - \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); |
|
126 | - |
|
127 | - $storageInfo = \OC_Helper::getStorageInfo(''); |
|
128 | - $this->assertEquals(12, $storageInfo['free']); |
|
129 | - $this->assertEquals(5, $storageInfo['used']); |
|
130 | - $this->assertEquals(17, $storageInfo['total']); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Test getting the storage info, including extra mount points |
|
135 | - */ |
|
136 | - public function testGetStorageInfoIncludingExtStorage(): void { |
|
137 | - $homeStorage = new \OC\Files\Storage\Temporary([]); |
|
138 | - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
139 | - $homeStorage->file_put_contents('test.txt', '01234'); |
|
140 | - |
|
141 | - $extStorage = new \OC\Files\Storage\Temporary([]); |
|
142 | - $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); |
|
143 | - $extStorage->getScanner()->scan(''); // update root size |
|
144 | - |
|
145 | - \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); |
|
146 | - |
|
147 | - $this->setIncludeExternalStorage(true); |
|
148 | - |
|
149 | - $config = \OC::$server->get(IConfig::class); |
|
150 | - $config->setUserValue($this->user, 'files', 'quota', '25'); |
|
151 | - |
|
152 | - $storageInfo = \OC_Helper::getStorageInfo(''); |
|
153 | - $this->assertEquals(3, $storageInfo['free']); |
|
154 | - $this->assertEquals(22, $storageInfo['used']); |
|
155 | - $this->assertEquals(25, $storageInfo['total']); |
|
156 | - |
|
157 | - $config->setUserValue($this->user, 'files', 'quota', 'default'); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Test getting the storage info excluding extra mount points |
|
162 | - * when user has no quota set, even when quota ext storage option |
|
163 | - * was set |
|
164 | - */ |
|
165 | - public function testGetStorageInfoIncludingExtStorageWithNoUserQuota(): void { |
|
166 | - $homeStorage = $this->getStorageMock(12); |
|
167 | - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
168 | - $homeStorage->file_put_contents('test.txt', '01234'); |
|
169 | - |
|
170 | - $extStorage = new \OC\Files\Storage\Temporary([]); |
|
171 | - $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); |
|
172 | - $extStorage->getScanner()->scan(''); // update root size |
|
173 | - |
|
174 | - \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); |
|
175 | - |
|
176 | - $config = \OC::$server->getConfig(); |
|
177 | - $this->setIncludeExternalStorage(true); |
|
178 | - |
|
179 | - $storageInfo = \OC_Helper::getStorageInfo(''); |
|
180 | - $this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage'); |
|
181 | - $this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used'); |
|
182 | - $this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage'); |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * Test getting the storage info with quota enabled |
|
188 | - */ |
|
189 | - public function testGetStorageInfoWithQuota(): void { |
|
190 | - $homeStorage = $this->getStorageMock(12); |
|
191 | - $homeStorage->file_put_contents('test.txt', '01234'); |
|
192 | - $homeStorage = new \OC\Files\Storage\Wrapper\Quota( |
|
193 | - [ |
|
194 | - 'storage' => $homeStorage, |
|
195 | - 'quota' => 7 |
|
196 | - ] |
|
197 | - ); |
|
198 | - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
199 | - |
|
200 | - $storageInfo = \OC_Helper::getStorageInfo(''); |
|
201 | - $this->assertEquals(2, $storageInfo['free']); |
|
202 | - $this->assertEquals(5, $storageInfo['used']); |
|
203 | - $this->assertEquals(7, $storageInfo['total']); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Test getting the storage info when data exceeds quota |
|
208 | - */ |
|
209 | - public function testGetStorageInfoWhenSizeExceedsQuota(): void { |
|
210 | - $homeStorage = $this->getStorageMock(12); |
|
211 | - $homeStorage->file_put_contents('test.txt', '0123456789'); |
|
212 | - $homeStorage = new \OC\Files\Storage\Wrapper\Quota( |
|
213 | - [ |
|
214 | - 'storage' => $homeStorage, |
|
215 | - 'quota' => 7 |
|
216 | - ] |
|
217 | - ); |
|
218 | - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
219 | - |
|
220 | - $storageInfo = \OC_Helper::getStorageInfo(''); |
|
221 | - $this->assertEquals(0, $storageInfo['free']); |
|
222 | - $this->assertEquals(10, $storageInfo['used']); |
|
223 | - // total = quota |
|
224 | - $this->assertEquals(7, $storageInfo['total']); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Test getting the storage info when the remaining |
|
229 | - * free storage space is less than the quota |
|
230 | - */ |
|
231 | - public function testGetStorageInfoWhenFreeSpaceLessThanQuota(): void { |
|
232 | - $homeStorage = $this->getStorageMock(12); |
|
233 | - $homeStorage->file_put_contents('test.txt', '01234'); |
|
234 | - $homeStorage = new \OC\Files\Storage\Wrapper\Quota( |
|
235 | - [ |
|
236 | - 'storage' => $homeStorage, |
|
237 | - 'quota' => 18 |
|
238 | - ] |
|
239 | - ); |
|
240 | - \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
241 | - |
|
242 | - $storageInfo = \OC_Helper::getStorageInfo(''); |
|
243 | - $this->assertEquals(12, $storageInfo['free']); |
|
244 | - $this->assertEquals(5, $storageInfo['used']); |
|
245 | - // total = free + used (because quota > total) |
|
246 | - $this->assertEquals(17, $storageInfo['total']); |
|
247 | - } |
|
21 | + use UserTrait; |
|
22 | + |
|
23 | + /** @var string */ |
|
24 | + private $user; |
|
25 | + /** @var \OC\Files\Storage\Storage */ |
|
26 | + private $storageMock; |
|
27 | + /** @var \OC\Files\Storage\Storage */ |
|
28 | + private $storage; |
|
29 | + private bool $savedQuotaIncludeExternalStorage; |
|
30 | + |
|
31 | + protected function setUp(): void { |
|
32 | + parent::setUp(); |
|
33 | + |
|
34 | + $this->user = $this->getUniqueID('user_'); |
|
35 | + $this->createUser($this->user, $this->user); |
|
36 | + $this->savedQuotaIncludeExternalStorage = $this->getIncludeExternalStorage(); |
|
37 | + |
|
38 | + \OC\Files\Filesystem::tearDown(); |
|
39 | + \OC_User::setUserId($this->user); |
|
40 | + \OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files'); |
|
41 | + |
|
42 | + /** @var IMountManager $manager */ |
|
43 | + $manager = \OC::$server->get(IMountManager::class); |
|
44 | + $manager->removeMount('/' . $this->user); |
|
45 | + |
|
46 | + $this->storageMock = null; |
|
47 | + } |
|
48 | + |
|
49 | + protected function tearDown(): void { |
|
50 | + $this->setIncludeExternalStorage($this->savedQuotaIncludeExternalStorage); |
|
51 | + $this->user = null; |
|
52 | + |
|
53 | + if ($this->storageMock) { |
|
54 | + $this->storageMock->getCache()->clear(); |
|
55 | + $this->storageMock = null; |
|
56 | + } |
|
57 | + \OC\Files\Filesystem::tearDown(); |
|
58 | + |
|
59 | + \OC_User::setUserId(''); |
|
60 | + \OC::$server->getConfig()->deleteAllUserValues($this->user); |
|
61 | + |
|
62 | + parent::tearDown(); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Returns a storage mock that returns the given value as |
|
67 | + * free space |
|
68 | + * |
|
69 | + * @param int $freeSpace free space value |
|
70 | + * @return \OC\Files\Storage\Storage |
|
71 | + */ |
|
72 | + private function getStorageMock($freeSpace = 12) { |
|
73 | + $this->storageMock = $this->getMockBuilder(Temporary::class) |
|
74 | + ->onlyMethods(['free_space']) |
|
75 | + ->setConstructorArgs([[]]) |
|
76 | + ->getMock(); |
|
77 | + |
|
78 | + $this->storageMock->expects($this->once()) |
|
79 | + ->method('free_space') |
|
80 | + ->willReturn($freeSpace); |
|
81 | + return $this->storageMock; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Test getting the storage info |
|
86 | + */ |
|
87 | + public function testGetStorageInfo(): void { |
|
88 | + $homeStorage = $this->getStorageMock(12); |
|
89 | + \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
90 | + $homeStorage->file_put_contents('test.txt', '01234'); |
|
91 | + |
|
92 | + $storageInfo = \OC_Helper::getStorageInfo(''); |
|
93 | + $this->assertEquals(12, $storageInfo['free']); |
|
94 | + $this->assertEquals(5, $storageInfo['used']); |
|
95 | + $this->assertEquals(17, $storageInfo['total']); |
|
96 | + } |
|
97 | + private function getIncludeExternalStorage(): bool { |
|
98 | + $class = new \ReflectionClass(\OC_Helper::class); |
|
99 | + $prop = $class->getProperty('quotaIncludeExternalStorage'); |
|
100 | + $prop->setAccessible(true); |
|
101 | + return $prop->getValue(null) ?? false; |
|
102 | + } |
|
103 | + |
|
104 | + private function setIncludeExternalStorage(bool $include) { |
|
105 | + $class = new \ReflectionClass(\OC_Helper::class); |
|
106 | + $prop = $class->getProperty('quotaIncludeExternalStorage'); |
|
107 | + $prop->setAccessible(true); |
|
108 | + $prop->setValue(null, $include); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Test getting the storage info, ignoring extra mount points |
|
113 | + */ |
|
114 | + public function testGetStorageInfoExcludingExtStorage(): void { |
|
115 | + $homeStorage = $this->getStorageMock(12); |
|
116 | + \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
117 | + $homeStorage->file_put_contents('test.txt', '01234'); |
|
118 | + |
|
119 | + $extStorage = new \OC\Files\Storage\Temporary([]); |
|
120 | + $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); |
|
121 | + $extStorage->getScanner()->scan(''); // update root size |
|
122 | + |
|
123 | + $this->setIncludeExternalStorage(false); |
|
124 | + |
|
125 | + \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); |
|
126 | + |
|
127 | + $storageInfo = \OC_Helper::getStorageInfo(''); |
|
128 | + $this->assertEquals(12, $storageInfo['free']); |
|
129 | + $this->assertEquals(5, $storageInfo['used']); |
|
130 | + $this->assertEquals(17, $storageInfo['total']); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Test getting the storage info, including extra mount points |
|
135 | + */ |
|
136 | + public function testGetStorageInfoIncludingExtStorage(): void { |
|
137 | + $homeStorage = new \OC\Files\Storage\Temporary([]); |
|
138 | + \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
139 | + $homeStorage->file_put_contents('test.txt', '01234'); |
|
140 | + |
|
141 | + $extStorage = new \OC\Files\Storage\Temporary([]); |
|
142 | + $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); |
|
143 | + $extStorage->getScanner()->scan(''); // update root size |
|
144 | + |
|
145 | + \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); |
|
146 | + |
|
147 | + $this->setIncludeExternalStorage(true); |
|
148 | + |
|
149 | + $config = \OC::$server->get(IConfig::class); |
|
150 | + $config->setUserValue($this->user, 'files', 'quota', '25'); |
|
151 | + |
|
152 | + $storageInfo = \OC_Helper::getStorageInfo(''); |
|
153 | + $this->assertEquals(3, $storageInfo['free']); |
|
154 | + $this->assertEquals(22, $storageInfo['used']); |
|
155 | + $this->assertEquals(25, $storageInfo['total']); |
|
156 | + |
|
157 | + $config->setUserValue($this->user, 'files', 'quota', 'default'); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Test getting the storage info excluding extra mount points |
|
162 | + * when user has no quota set, even when quota ext storage option |
|
163 | + * was set |
|
164 | + */ |
|
165 | + public function testGetStorageInfoIncludingExtStorageWithNoUserQuota(): void { |
|
166 | + $homeStorage = $this->getStorageMock(12); |
|
167 | + \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
168 | + $homeStorage->file_put_contents('test.txt', '01234'); |
|
169 | + |
|
170 | + $extStorage = new \OC\Files\Storage\Temporary([]); |
|
171 | + $extStorage->file_put_contents('extfile.txt', 'abcdefghijklmnopq'); |
|
172 | + $extStorage->getScanner()->scan(''); // update root size |
|
173 | + |
|
174 | + \OC\Files\Filesystem::mount($extStorage, [], '/' . $this->user . '/files/ext'); |
|
175 | + |
|
176 | + $config = \OC::$server->getConfig(); |
|
177 | + $this->setIncludeExternalStorage(true); |
|
178 | + |
|
179 | + $storageInfo = \OC_Helper::getStorageInfo(''); |
|
180 | + $this->assertEquals(12, $storageInfo['free'], '12 bytes free in home storage'); |
|
181 | + $this->assertEquals(22, $storageInfo['used'], '5 bytes of home storage and 17 bytes of the temporary storage are used'); |
|
182 | + $this->assertEquals(34, $storageInfo['total'], '5 bytes used and 12 bytes free in home storage as well as 17 bytes used in temporary storage'); |
|
183 | + } |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * Test getting the storage info with quota enabled |
|
188 | + */ |
|
189 | + public function testGetStorageInfoWithQuota(): void { |
|
190 | + $homeStorage = $this->getStorageMock(12); |
|
191 | + $homeStorage->file_put_contents('test.txt', '01234'); |
|
192 | + $homeStorage = new \OC\Files\Storage\Wrapper\Quota( |
|
193 | + [ |
|
194 | + 'storage' => $homeStorage, |
|
195 | + 'quota' => 7 |
|
196 | + ] |
|
197 | + ); |
|
198 | + \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
199 | + |
|
200 | + $storageInfo = \OC_Helper::getStorageInfo(''); |
|
201 | + $this->assertEquals(2, $storageInfo['free']); |
|
202 | + $this->assertEquals(5, $storageInfo['used']); |
|
203 | + $this->assertEquals(7, $storageInfo['total']); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Test getting the storage info when data exceeds quota |
|
208 | + */ |
|
209 | + public function testGetStorageInfoWhenSizeExceedsQuota(): void { |
|
210 | + $homeStorage = $this->getStorageMock(12); |
|
211 | + $homeStorage->file_put_contents('test.txt', '0123456789'); |
|
212 | + $homeStorage = new \OC\Files\Storage\Wrapper\Quota( |
|
213 | + [ |
|
214 | + 'storage' => $homeStorage, |
|
215 | + 'quota' => 7 |
|
216 | + ] |
|
217 | + ); |
|
218 | + \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
219 | + |
|
220 | + $storageInfo = \OC_Helper::getStorageInfo(''); |
|
221 | + $this->assertEquals(0, $storageInfo['free']); |
|
222 | + $this->assertEquals(10, $storageInfo['used']); |
|
223 | + // total = quota |
|
224 | + $this->assertEquals(7, $storageInfo['total']); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Test getting the storage info when the remaining |
|
229 | + * free storage space is less than the quota |
|
230 | + */ |
|
231 | + public function testGetStorageInfoWhenFreeSpaceLessThanQuota(): void { |
|
232 | + $homeStorage = $this->getStorageMock(12); |
|
233 | + $homeStorage->file_put_contents('test.txt', '01234'); |
|
234 | + $homeStorage = new \OC\Files\Storage\Wrapper\Quota( |
|
235 | + [ |
|
236 | + 'storage' => $homeStorage, |
|
237 | + 'quota' => 18 |
|
238 | + ] |
|
239 | + ); |
|
240 | + \OC\Files\Filesystem::mount($homeStorage, [], '/' . $this->user . '/files'); |
|
241 | + |
|
242 | + $storageInfo = \OC_Helper::getStorageInfo(''); |
|
243 | + $this->assertEquals(12, $storageInfo['free']); |
|
244 | + $this->assertEquals(5, $storageInfo['used']); |
|
245 | + // total = free + used (because quota > total) |
|
246 | + $this->assertEquals(17, $storageInfo['total']); |
|
247 | + } |
|
248 | 248 | } |