TimezoneServiceTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. * @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. declare(strict_types=1);
  23. /*
  24. * @copyright 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
  25. *
  26. * @author 2023 Christoph Wurst <christoph@winzerhof-wurst.at>
  27. *
  28. * @license GNU AGPL version 3 or any later version
  29. *
  30. * This program is free software: you can redistribute it and/or modify
  31. * it under the terms of the GNU Affero General Public License as
  32. * published by the Free Software Foundation, either version 3 of the
  33. * License, or (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU Affero General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU Affero General Public License
  41. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  42. */
  43. namespace OCA\DAV\Tests\unit\CalDAV;
  44. use DateTimeZone;
  45. use OCA\DAV\CalDAV\CalendarImpl;
  46. use OCA\DAV\CalDAV\TimezoneService;
  47. use OCA\DAV\Db\Property;
  48. use OCA\DAV\Db\PropertyMapper;
  49. use OCP\Calendar\ICalendar;
  50. use OCP\Calendar\IManager;
  51. use OCP\IConfig;
  52. use PHPUnit\Framework\MockObject\MockObject;
  53. use Sabre\VObject\Component\VTimeZone;
  54. use Test\TestCase;
  55. class TimezoneServiceTest extends TestCase {
  56. private IConfig|MockObject $config;
  57. private PropertyMapper|MockObject $propertyMapper;
  58. private IManager|MockObject $calendarManager;
  59. private TimezoneService $service;
  60. protected function setUp(): void {
  61. parent::setUp();
  62. $this->config = $this->createMock(IConfig::class);
  63. $this->propertyMapper = $this->createMock(PropertyMapper::class);
  64. $this->calendarManager = $this->createMock(IManager::class);
  65. $this->service = new TimezoneService(
  66. $this->config,
  67. $this->propertyMapper,
  68. $this->calendarManager,
  69. );
  70. }
  71. public function testGetUserTimezoneFromSettings(): void {
  72. $this->config->expects(self::once())
  73. ->method('getUserValue')
  74. ->with('test123', 'core', 'timezone', '')
  75. ->willReturn('Europe/Warsaw');
  76. $timezone = $this->service->getUserTimezone('test123');
  77. self::assertSame('Europe/Warsaw', $timezone);
  78. }
  79. public function testGetUserTimezoneFromAvailability(): void {
  80. $this->config->expects(self::once())
  81. ->method('getUserValue')
  82. ->with('test123', 'core', 'timezone', '')
  83. ->willReturn('');
  84. $property = new Property();
  85. $property->setPropertyvalue('BEGIN:VCALENDAR
  86. PRODID:Nextcloud DAV app
  87. BEGIN:VTIMEZONE
  88. TZID:Europe/Vienna
  89. END:VTIMEZONE
  90. END:VCALENDAR');
  91. $this->propertyMapper->expects(self::once())
  92. ->method('findPropertyByPathAndName')
  93. ->willReturn([
  94. $property,
  95. ]);
  96. $timezone = $this->service->getUserTimezone('test123');
  97. self::assertNotNull($timezone);
  98. self::assertEquals('Europe/Vienna', $timezone);
  99. }
  100. public function testGetUserTimezoneFromPersonalCalendar(): void {
  101. $this->config->expects(self::exactly(2))
  102. ->method('getUserValue')
  103. ->willReturnMap([
  104. ['test123', 'core', 'timezone', '', ''],
  105. ['test123', 'dav', 'defaultCalendar', '', 'personal-1'],
  106. ]);
  107. $other = $this->createMock(ICalendar::class);
  108. $other->method('getUri')->willReturn('other');
  109. $personal = $this->createMock(CalendarImpl::class);
  110. $personal->method('getUri')->willReturn('personal-1');
  111. $tz = new DateTimeZone('Europe/Berlin');
  112. $vtz = $this->createMock(VTimeZone::class);
  113. $vtz->method('getTimeZone')->willReturn($tz);
  114. $personal->method('getSchedulingTimezone')->willReturn($vtz);
  115. $this->calendarManager->expects(self::once())
  116. ->method('getCalendarsForPrincipal')
  117. ->with('principals/users/test123')
  118. ->willReturn([
  119. $other,
  120. $personal,
  121. ]);
  122. $timezone = $this->service->getUserTimezone('test123');
  123. self::assertNotNull($timezone);
  124. self::assertEquals('Europe/Berlin', $timezone);
  125. }
  126. public function testGetUserTimezoneFromAny(): void {
  127. $this->config->expects(self::exactly(2))
  128. ->method('getUserValue')
  129. ->willReturnMap([
  130. ['test123', 'core', 'timezone', '', ''],
  131. ['test123', 'dav', 'defaultCalendar', '', 'personal-1'],
  132. ]);
  133. $other = $this->createMock(ICalendar::class);
  134. $other->method('getUri')->willReturn('other');
  135. $personal = $this->createMock(CalendarImpl::class);
  136. $personal->method('getUri')->willReturn('personal-2');
  137. $tz = new DateTimeZone('Europe/Prague');
  138. $vtz = $this->createMock(VTimeZone::class);
  139. $vtz->method('getTimeZone')->willReturn($tz);
  140. $personal->method('getSchedulingTimezone')->willReturn($vtz);
  141. $this->calendarManager->expects(self::once())
  142. ->method('getCalendarsForPrincipal')
  143. ->with('principals/users/test123')
  144. ->willReturn([
  145. $other,
  146. $personal,
  147. ]);
  148. $timezone = $this->service->getUserTimezone('test123');
  149. self::assertNotNull($timezone);
  150. self::assertEquals('Europe/Prague', $timezone);
  151. }
  152. public function testGetUserTimezoneNoneFound(): void {
  153. $timezone = $this->service->getUserTimezone('test123');
  154. self::assertNull($timezone);
  155. }
  156. }