EventComparisonServiceTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Daniel Kesselberg <mail@danielkesselberg.de>
  5. * @copyright 2024 Robert C. Schaller <gtbc_robert.schaller@rsxc.de>
  6. *
  7. * @author 2023 Daniel Kesselberg <mail@danielkesselberg.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\CalDAV;
  26. use OCA\DAV\CalDAV\EventComparisonService;
  27. use Sabre\VObject\Component\VCalendar;
  28. use Test\TestCase;
  29. class EventComparisonServiceTest extends TestCase {
  30. /** @var EventComparisonService */
  31. private $eventComparisonService;
  32. protected function setUp(): void {
  33. $this->eventComparisonService = new EventComparisonService();
  34. }
  35. public function testNoModifiedEvent(): void {
  36. $vCalendarOld = new VCalendar();
  37. $vCalendarNew = new VCalendar();
  38. $vEventOld = $vCalendarOld->add('VEVENT', [
  39. 'UID' => 'uid-1234',
  40. 'LAST-MODIFIED' => 123456,
  41. 'SEQUENCE' => 2,
  42. 'SUMMARY' => 'Fellowship meeting',
  43. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  44. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  45. ]);
  46. $vEventOld->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  47. $vEventOld->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  48. $vEventNew = $vCalendarNew->add('VEVENT', [
  49. 'UID' => 'uid-1234',
  50. 'LAST-MODIFIED' => 123456,
  51. 'SEQUENCE' => 2,
  52. 'SUMMARY' => 'Fellowship meeting',
  53. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  54. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  55. ]);
  56. $vEventNew->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  57. $vEventNew->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  58. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  59. $this->assertEmpty($result['old']);
  60. $this->assertEmpty($result['new']);
  61. }
  62. public function testNewEvent(): void {
  63. $vCalendarOld = null;
  64. $vCalendarNew = new VCalendar();
  65. $vEventNew = $vCalendarNew->add('VEVENT', [
  66. 'UID' => 'uid-1234',
  67. 'LAST-MODIFIED' => 123456,
  68. 'SEQUENCE' => 2,
  69. 'SUMMARY' => 'Fellowship meeting',
  70. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  71. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  72. ]);
  73. $vEventNew->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  74. $vEventNew->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  75. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  76. $this->assertNull($result['old']);
  77. $this->assertEquals([$vEventNew], $result['new']);
  78. }
  79. public function testModifiedUnmodifiedEvent(): void {
  80. $vCalendarOld = new VCalendar();
  81. $vCalendarNew = new VCalendar();
  82. $vEventOld1 = $vCalendarOld->add('VEVENT', [
  83. 'UID' => 'uid-1234',
  84. 'LAST-MODIFIED' => 123456,
  85. 'SEQUENCE' => 2,
  86. 'SUMMARY' => 'Fellowship meeting',
  87. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  88. ]);
  89. $vEventOld1->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  90. $vEventOld1->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  91. $vEventOld2 = $vCalendarOld->add('VEVENT', [
  92. 'UID' => 'uid-1235',
  93. 'LAST-MODIFIED' => 123456,
  94. 'SEQUENCE' => 2,
  95. 'SUMMARY' => 'Fellowship meeting',
  96. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  97. ]);
  98. $vEventOld2->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  99. $vEventOld2->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  100. $vEventNew1 = $vCalendarNew->add('VEVENT', [
  101. 'UID' => 'uid-1234',
  102. 'LAST-MODIFIED' => 123456,
  103. 'SEQUENCE' => 2,
  104. 'SUMMARY' => 'Fellowship meeting',
  105. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  106. ]);
  107. $vEventNew1->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  108. $vEventNew1->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  109. $vEventNew2 = $vCalendarNew->add('VEVENT', [
  110. 'UID' => 'uid-1235',
  111. 'LAST-MODIFIED' => 123457,
  112. 'SEQUENCE' => 3,
  113. 'SUMMARY' => 'Fellowship meeting 2',
  114. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  115. ]);
  116. $vEventNew2->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  117. $vEventNew2->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  118. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  119. $this->assertEquals([$vEventOld2], $result['old']);
  120. $this->assertEquals([$vEventNew2], $result['new']);
  121. }
  122. // First test to certify fix for issue nextcloud/server#41084
  123. public function testSequenceNumberIncrementDetectedForFirstModificationToEventWithoutZeroInit(): void {
  124. $vCalendarOld = new VCalendar();
  125. $vCalendarNew = new VCalendar();
  126. $vEventOld = $vCalendarOld->add('VEVENT', [
  127. 'UID' => 'uid-1234',
  128. 'LAST-MODIFIED' => 123456,
  129. // 'SEQUENCE' => 0, // sequence number may not be set to zero during event creation and instead fully omitted
  130. 'SUMMARY' => 'Fellowship meeting',
  131. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  132. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  133. ]);
  134. $vEventOld->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  135. $vEventOld->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  136. $vEventNew = $vCalendarNew->add('VEVENT', [
  137. 'UID' => 'uid-1234',
  138. 'LAST-MODIFIED' => 123456,
  139. 'SEQUENCE' => 1,
  140. 'SUMMARY' => 'Fellowship meeting',
  141. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  142. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  143. ]);
  144. $vEventNew->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  145. $vEventNew->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  146. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  147. $this->assertEquals([$vEventOld], $result['old']);
  148. $this->assertEquals([$vEventNew], $result['new']);
  149. }
  150. // Second test to certify fix for issue nextcloud/server#41084
  151. public function testSequenceNumberIncrementDetectedForFirstModificationToEventWithZeroInit(): void {
  152. $vCalendarOld = new VCalendar();
  153. $vCalendarNew = new VCalendar();
  154. $vEventOld = $vCalendarOld->add('VEVENT', [
  155. 'UID' => 'uid-1234',
  156. 'LAST-MODIFIED' => 123456,
  157. 'SEQUENCE' => 0,
  158. 'SUMMARY' => 'Fellowship meeting',
  159. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  160. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  161. ]);
  162. $vEventOld->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  163. $vEventOld->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  164. $vEventNew = $vCalendarNew->add('VEVENT', [
  165. 'UID' => 'uid-1234',
  166. 'LAST-MODIFIED' => 123456,
  167. 'SEQUENCE' => 1,
  168. 'SUMMARY' => 'Fellowship meeting',
  169. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  170. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  171. ]);
  172. $vEventNew->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  173. $vEventNew->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  174. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  175. $this->assertEquals([$vEventOld], $result['old']);
  176. $this->assertEquals([$vEventNew], $result['new']);
  177. }
  178. }