Skip to content

Commit c77cc80

Browse files
authored
Merge pull request #33906 from nextcloud/fix/fix-calendar-tests-getTimestamp
Fix Calendar tests mocking a non-existant method.
2 parents 5555500 + e3a52ce commit c77cc80

2 files changed

Lines changed: 53 additions & 35 deletions

File tree

lib/private/Calendar/Manager.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,27 @@ public function handleIMipCancel(string $principalUri, string $sender, ?string $
330330
// to the email address in the ORGANIZER.
331331
// We don't want to accept a CANCEL request from just anyone
332332
$organizer = substr($vEvent->{'ORGANIZER'}->getValue(), 7);
333-
if (strcasecmp($sender, $organizer) !== 0 && strcasecmp($replyTo, $organizer) !== 0) {
333+
$isNotOrganizer = ($replyTo !== null) ? (strcasecmp($sender, $organizer) !== 0 && strcasecmp($replyTo, $organizer) !== 0) : (strcasecmp($sender, $organizer) !== 0);
334+
if ($isNotOrganizer) {
334335
$this->logger->warning('Sender must be the ORGANIZER of this event');
335336
return false;
336337
}
337338

339+
//check if the event is in the future
340+
/** @var DateTime $eventTime */
341+
$eventTime = $vEvent->{'DTSTART'};
342+
if ($eventTime->getDateTime()->getTimeStamp() < $this->timeFactory->getTime()) { // this might cause issues with recurrences
343+
$this->logger->warning('Only events in the future are processed');
344+
return false;
345+
}
346+
347+
// Check if we have a calendar to work with
338348
$calendars = $this->getCalendarsForPrincipal($principalUri);
349+
if (empty($calendars)) {
350+
$this->logger->warning('Could not find any calendars for principal ' . $principalUri);
351+
return false;
352+
}
353+
339354
$found = null;
340355
// if the attendee has been found in at least one calendar event with the UID of the iMIP event
341356
// we process it.

tests/lib/Calendar/ManagerTest.php

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function testHandleImipReplyWrongMethod(): void {
251251
$this->logger->expects(self::once())
252252
->method('warning');
253253
$this->time->expects(self::never())
254-
->method('getTimestamp');
254+
->method('getTime');
255255

256256
$result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
257257
$this->assertFalse($result);
@@ -266,7 +266,7 @@ public function testHandleImipReplyOrganizerNotRecipient(): void {
266266
$this->logger->expects(self::once())
267267
->method('warning');
268268
$this->time->expects(self::never())
269-
->method('getTimestamp');
269+
->method('getTime');
270270

271271
$result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
272272
$this->assertFalse($result);
@@ -280,7 +280,7 @@ public function testHandleImipReplyDateInThePast(): void {
280280
$calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past
281281

282282
$this->time->expects(self::once())
283-
->method('getTimestamp')
283+
->method('getTime')
284284
->willReturn(time());
285285

286286
$this->logger->expects(self::once())
@@ -301,15 +301,16 @@ public function testHandleImipReplyNoCalendars(): void {
301301
])
302302
->setMethods([
303303
'getCalendarsForPrincipal'
304-
]);
304+
])
305+
->getMock();
305306
$principalUri = 'principals/user/linus';
306307
$sender = 'pierre@general-store.com';
307308
$recipient = 'linus@stardew-tent-living.com';
308309
$calendarData = $this->getVCalendarReply();
309310

310311
$this->time->expects(self::once())
311-
->method('getTimestamp')
312-
->willReturn(202208219);
312+
->method('getTime')
313+
->willReturn(1628374233);
313314
$manager->expects(self::once())
314315
->method('getCalendarsForPrincipal')
315316
->willReturn([]);
@@ -331,16 +332,17 @@ public function testHandleImipReplyEventNotFound(): void {
331332
])
332333
->setMethods([
333334
'getCalendarsForPrincipal'
334-
]);
335+
])
336+
->getMock();
335337
$calendar = $this->createMock(ICreateFromString::class);
336338
$principalUri = 'principals/user/linus';
337339
$sender = 'pierre@general-store.com';
338340
$recipient = 'linus@stardew-tent-living.com';
339341
$calendarData = $this->getVCalendarReply();
340342

341343
$this->time->expects(self::once())
342-
->method('getTimestamp')
343-
->willReturn(202208219);
344+
->method('getTime')
345+
->willReturn(1628374233);
344346
$manager->expects(self::once())
345347
->method('getCalendarsForPrincipal')
346348
->willReturn([$calendar]);
@@ -367,16 +369,17 @@ public function testHandleImipReply(): void {
367369
])
368370
->setMethods([
369371
'getCalendarsForPrincipal'
370-
]);
372+
])
373+
->getMock();
371374
$calendar = $this->createMock(ICreateFromString::class);
372375
$principalUri = 'principals/user/linus';
373376
$sender = 'pierre@general-store.com';
374377
$recipient = 'linus@stardew-tent-living.com';
375378
$calendarData = $this->getVCalendarReply();
376379

377380
$this->time->expects(self::once())
378-
->method('getTimestamp')
379-
->willReturn(202208219);
381+
->method('getTime')
382+
->willReturn(1628374233);
380383
$manager->expects(self::once())
381384
->method('getCalendarsForPrincipal')
382385
->willReturn([$calendar]);
@@ -397,12 +400,12 @@ public function testHandleImipCancelWrongMethod(): void {
397400
$recipient = 'pierre@general-store.com';
398401
$replyTo = null;
399402
$calendarData = $this->getVCalendarCancel();
400-
$calendarData->VEVENT->METHOD = 'REQUEST';
403+
$calendarData->METHOD = 'REQUEST';
401404

402405
$this->logger->expects(self::once())
403406
->method('warning');
404407
$this->time->expects(self::never())
405-
->method('getTimestamp');
408+
->method('getTime');
406409

407410
$result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
408411
$this->assertFalse($result);
@@ -414,12 +417,11 @@ public function testHandleImipCancelAttendeeNotRecipient(): void {
414417
$recipient = 'leah@general-store.com';
415418
$replyTo = null;
416419
$calendarData = $this->getVCalendarCancel();
417-
$calendarData->VEVENT->METHOD = 'CANCEL';
418420

419421
$this->logger->expects(self::once())
420422
->method('warning');
421423
$this->time->expects(self::never())
422-
->method('getTimestamp');
424+
->method('getTime');
423425

424426
$result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
425427
$this->assertFalse($result);
@@ -434,7 +436,7 @@ public function testHandleImipCancelDateInThePast(): void {
434436
$calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past
435437

436438
$this->time->expects(self::once())
437-
->method('getTimestamp')
439+
->method('getTime')
438440
->willReturn(time());
439441
$this->logger->expects(self::once())
440442
->method('warning');
@@ -454,25 +456,26 @@ public function testHandleImipCancelNoCalendars(): void {
454456
])
455457
->setMethods([
456458
'getCalendarsForPrincipal'
457-
]);
459+
])
460+
->getMock();
458461
$principalUri = 'principals/user/pierre';
459462
$sender = 'linus@stardew-tent-living.com';
460463
$recipient = 'pierre@general-store.com';
461464
$replyTo = null;
462465
$calendarData = $this->getVCalendarCancel();
463466

464467
$this->time->expects(self::once())
465-
->method('getTimestamp')
466-
->willReturn(202208219);
468+
->method('getTime')
469+
->willReturn(1628374233);
467470
$manager->expects(self::once())
468471
->method('getCalendarsForPrincipal')
469472
->with($principalUri)
470473
->willReturn([]);
471474
$this->logger->expects(self::once())
472475
->method('warning');
473476

474-
$result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
475-
$this->assertTrue($result);
477+
$result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
478+
$this->assertFalse($result);
476479
}
477480

478481
public function testHandleImipCancelOrganiserInReplyTo(): void {
@@ -486,18 +489,18 @@ public function testHandleImipCancelOrganiserInReplyTo(): void {
486489
])
487490
->setMethods([
488491
'getCalendarsForPrincipal'
489-
]);
492+
])
493+
->getMock();
490494
$principalUri = 'principals/user/pierre';
491495
$sender = 'clint@stardew-blacksmiths.com';
492496
$recipient = 'pierre@general-store.com';
493497
$replyTo = 'linus@stardew-tent-living.com';
494498
$calendar = $this->createMock(ICreateFromString::class);
495499
$calendarData = $this->getVCalendarCancel();
496-
$calendarData->VEVENT->METHOD = 'CANCEL';
497500

498501
$this->time->expects(self::once())
499-
->method('getTimestamp')
500-
->willReturn(202208219);
502+
->method('getTime')
503+
->willReturn(1628374233);
501504
$manager->expects(self::once())
502505
->method('getCalendarsForPrincipal')
503506
->with($principalUri)
@@ -508,8 +511,8 @@ public function testHandleImipCancelOrganiserInReplyTo(): void {
508511
$calendar->expects(self::once())
509512
->method('handleIMipMessage')
510513
->with('testname.ics', $calendarData->serialize());
511-
$result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
512-
$this->assertFalse($result);
514+
$result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
515+
$this->assertTrue($result);
513516
}
514517

515518
public function testHandleImipCancel(): void {
@@ -523,18 +526,18 @@ public function testHandleImipCancel(): void {
523526
])
524527
->setMethods([
525528
'getCalendarsForPrincipal'
526-
]);
529+
])
530+
->getMock();
527531
$principalUri = 'principals/user/pierre';
528532
$sender = 'linus@stardew-tent-living.com';
529533
$recipient = 'pierre@general-store.com';
530534
$replyTo = null;
531535
$calendar = $this->createMock(ICreateFromString::class);
532536
$calendarData = $this->getVCalendarCancel();
533-
$calendarData->VEVENT->METHOD = 'CANCEL';
534537

535538
$this->time->expects(self::once())
536-
->method('getTimestamp')
537-
->willReturn(202208219);
539+
->method('getTime')
540+
->willReturn(1628374233);
538541
$manager->expects(self::once())
539542
->method('getCalendarsForPrincipal')
540543
->with($principalUri)
@@ -545,8 +548,8 @@ public function testHandleImipCancel(): void {
545548
$calendar->expects(self::once())
546549
->method('handleIMipMessage')
547550
->with('testname.ics', $calendarData->serialize());
548-
$result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
549-
$this->assertFalse($result);
551+
$result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
552+
$this->assertTrue($result);
550553
}
551554

552555
private function getVCalendarReply(): Document {

0 commit comments

Comments
 (0)