UpcomingEventsServiceTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\Unit\DAV\Service;
  8. use DateTimeImmutable;
  9. use OCA\DAV\CalDAV\UpcomingEventsService;
  10. use OCP\App\IAppManager;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\Calendar\ICalendarQuery;
  13. use OCP\Calendar\IManager;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserManager;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use PHPUnit\Framework\TestCase;
  18. class UpcomingEventsServiceTest extends TestCase {
  19. private MockObject|IManager $calendarManager;
  20. private ITimeFactory|MockObject $timeFactory;
  21. private IUserManager|MockObject $userManager;
  22. private IAppManager|MockObject $appManager;
  23. private IURLGenerator|MockObject $urlGenerator;
  24. private UpcomingEventsService $service;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->calendarManager = $this->createMock(IManager::class);
  28. $this->timeFactory = $this->createMock(ITimeFactory::class);
  29. $this->userManager = $this->createMock(IUserManager::class);
  30. $this->appManager = $this->createMock(IAppManager::class);
  31. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  32. $this->service = new UpcomingEventsService(
  33. $this->calendarManager,
  34. $this->timeFactory,
  35. $this->userManager,
  36. $this->appManager,
  37. $this->urlGenerator,
  38. );
  39. }
  40. public function testGetEventsByLocation(): void {
  41. $now = new DateTimeImmutable('2024-07-08T18:20:20Z');
  42. $this->timeFactory->method('now')
  43. ->willReturn($now);
  44. $query = $this->createMock(ICalendarQuery::class);
  45. $this->appManager->method('isEnabledForUser')->willReturn(false);
  46. $this->calendarManager->method('newQuery')
  47. ->with('principals/users/user1')
  48. ->willReturn($query);
  49. $query->expects(self::once())
  50. ->method('addSearchProperty')
  51. ->with('LOCATION');
  52. $query->expects(self::once())
  53. ->method('setSearchPattern')
  54. ->with('https://cloud.example.com/call/123');
  55. $this->calendarManager->expects(self::once())
  56. ->method('searchForPrincipal')
  57. ->with($query)
  58. ->willReturn([
  59. [
  60. 'uri' => 'ev1',
  61. 'calendar-key' => '1',
  62. 'calendar-uri' => 'personal',
  63. 'objects' => [
  64. 0 => [
  65. 'DTSTART' => [
  66. new DateTimeImmutable('now'),
  67. ],
  68. ],
  69. ],
  70. ],
  71. ]);
  72. $events = $this->service->getEvents('user1', 'https://cloud.example.com/call/123');
  73. self::assertCount(1, $events);
  74. $event1 = $events[0];
  75. self::assertEquals('ev1', $event1->getUri());
  76. }
  77. }