1
0

TimezoneServiceTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. declare(strict_types=1);
  7. /**
  8. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  9. * SPDX-License-Identifier: AGPL-3.0-or-later
  10. */
  11. namespace OCA\DAV\Tests\unit\CalDAV;
  12. use DateTimeZone;
  13. use OCA\DAV\CalDAV\CalendarImpl;
  14. use OCA\DAV\CalDAV\TimezoneService;
  15. use OCA\DAV\Db\Property;
  16. use OCA\DAV\Db\PropertyMapper;
  17. use OCP\Calendar\ICalendar;
  18. use OCP\Calendar\IManager;
  19. use OCP\IConfig;
  20. use PHPUnit\Framework\MockObject\MockObject;
  21. use Sabre\VObject\Component\VTimeZone;
  22. use Test\TestCase;
  23. class TimezoneServiceTest extends TestCase {
  24. private IConfig|MockObject $config;
  25. private PropertyMapper|MockObject $propertyMapper;
  26. private IManager|MockObject $calendarManager;
  27. private TimezoneService $service;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->config = $this->createMock(IConfig::class);
  31. $this->propertyMapper = $this->createMock(PropertyMapper::class);
  32. $this->calendarManager = $this->createMock(IManager::class);
  33. $this->service = new TimezoneService(
  34. $this->config,
  35. $this->propertyMapper,
  36. $this->calendarManager,
  37. );
  38. }
  39. public function testGetUserTimezoneFromSettings(): void {
  40. $this->config->expects(self::once())
  41. ->method('getUserValue')
  42. ->with('test123', 'core', 'timezone', '')
  43. ->willReturn('Europe/Warsaw');
  44. $timezone = $this->service->getUserTimezone('test123');
  45. self::assertSame('Europe/Warsaw', $timezone);
  46. }
  47. public function testGetUserTimezoneFromAvailability(): void {
  48. $this->config->expects(self::once())
  49. ->method('getUserValue')
  50. ->with('test123', 'core', 'timezone', '')
  51. ->willReturn('');
  52. $property = new Property();
  53. $property->setPropertyvalue('BEGIN:VCALENDAR
  54. PRODID:Nextcloud DAV app
  55. BEGIN:VTIMEZONE
  56. TZID:Europe/Vienna
  57. END:VTIMEZONE
  58. END:VCALENDAR');
  59. $this->propertyMapper->expects(self::once())
  60. ->method('findPropertyByPathAndName')
  61. ->willReturn([
  62. $property,
  63. ]);
  64. $timezone = $this->service->getUserTimezone('test123');
  65. self::assertNotNull($timezone);
  66. self::assertEquals('Europe/Vienna', $timezone);
  67. }
  68. public function testGetUserTimezoneFromPersonalCalendar(): void {
  69. $this->config->expects(self::exactly(2))
  70. ->method('getUserValue')
  71. ->willReturnMap([
  72. ['test123', 'core', 'timezone', '', ''],
  73. ['test123', 'dav', 'defaultCalendar', '', 'personal-1'],
  74. ]);
  75. $other = $this->createMock(ICalendar::class);
  76. $other->method('getUri')->willReturn('other');
  77. $personal = $this->createMock(CalendarImpl::class);
  78. $personal->method('getUri')->willReturn('personal-1');
  79. $tz = new DateTimeZone('Europe/Berlin');
  80. $vtz = $this->createMock(VTimeZone::class);
  81. $vtz->method('getTimeZone')->willReturn($tz);
  82. $personal->method('getSchedulingTimezone')->willReturn($vtz);
  83. $this->calendarManager->expects(self::once())
  84. ->method('getCalendarsForPrincipal')
  85. ->with('principals/users/test123')
  86. ->willReturn([
  87. $other,
  88. $personal,
  89. ]);
  90. $timezone = $this->service->getUserTimezone('test123');
  91. self::assertNotNull($timezone);
  92. self::assertEquals('Europe/Berlin', $timezone);
  93. }
  94. public function testGetUserTimezoneFromAny(): void {
  95. $this->config->expects(self::exactly(2))
  96. ->method('getUserValue')
  97. ->willReturnMap([
  98. ['test123', 'core', 'timezone', '', ''],
  99. ['test123', 'dav', 'defaultCalendar', '', 'personal-1'],
  100. ]);
  101. $other = $this->createMock(ICalendar::class);
  102. $other->method('getUri')->willReturn('other');
  103. $personal = $this->createMock(CalendarImpl::class);
  104. $personal->method('getUri')->willReturn('personal-2');
  105. $tz = new DateTimeZone('Europe/Prague');
  106. $vtz = $this->createMock(VTimeZone::class);
  107. $vtz->method('getTimeZone')->willReturn($tz);
  108. $personal->method('getSchedulingTimezone')->willReturn($vtz);
  109. $this->calendarManager->expects(self::once())
  110. ->method('getCalendarsForPrincipal')
  111. ->with('principals/users/test123')
  112. ->willReturn([
  113. $other,
  114. $personal,
  115. ]);
  116. $timezone = $this->service->getUserTimezone('test123');
  117. self::assertNotNull($timezone);
  118. self::assertEquals('Europe/Prague', $timezone);
  119. }
  120. public function testGetUserTimezoneNoneFound(): void {
  121. $timezone = $this->service->getUserTimezone('test123');
  122. self::assertNull($timezone);
  123. }
  124. }