1
0

EventComparisonServiceTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\unit\CalDAV;
  8. use OCA\DAV\CalDAV\EventComparisonService;
  9. use Sabre\VObject\Component\VCalendar;
  10. use Test\TestCase;
  11. class EventComparisonServiceTest extends TestCase {
  12. /** @var EventComparisonService */
  13. private $eventComparisonService;
  14. protected function setUp(): void {
  15. $this->eventComparisonService = new EventComparisonService();
  16. }
  17. public function testNoModifiedEvent(): void {
  18. $vCalendarOld = new VCalendar();
  19. $vCalendarNew = new VCalendar();
  20. $vEventOld = $vCalendarOld->add('VEVENT', [
  21. 'UID' => 'uid-1234',
  22. 'LAST-MODIFIED' => 123456,
  23. 'SEQUENCE' => 2,
  24. 'SUMMARY' => 'Fellowship meeting',
  25. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  26. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  27. ]);
  28. $vEventOld->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  29. $vEventOld->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  30. $vEventNew = $vCalendarNew->add('VEVENT', [
  31. 'UID' => 'uid-1234',
  32. 'LAST-MODIFIED' => 123456,
  33. 'SEQUENCE' => 2,
  34. 'SUMMARY' => 'Fellowship meeting',
  35. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  36. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  37. ]);
  38. $vEventNew->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  39. $vEventNew->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  40. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  41. $this->assertEmpty($result['old']);
  42. $this->assertEmpty($result['new']);
  43. }
  44. public function testNewEvent(): void {
  45. $vCalendarOld = null;
  46. $vCalendarNew = new VCalendar();
  47. $vEventNew = $vCalendarNew->add('VEVENT', [
  48. 'UID' => 'uid-1234',
  49. 'LAST-MODIFIED' => 123456,
  50. 'SEQUENCE' => 2,
  51. 'SUMMARY' => 'Fellowship meeting',
  52. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  53. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  54. ]);
  55. $vEventNew->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  56. $vEventNew->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  57. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  58. $this->assertNull($result['old']);
  59. $this->assertEquals([$vEventNew], $result['new']);
  60. }
  61. public function testModifiedUnmodifiedEvent(): void {
  62. $vCalendarOld = new VCalendar();
  63. $vCalendarNew = new VCalendar();
  64. $vEventOld1 = $vCalendarOld->add('VEVENT', [
  65. 'UID' => 'uid-1234',
  66. 'LAST-MODIFIED' => 123456,
  67. 'SEQUENCE' => 2,
  68. 'SUMMARY' => 'Fellowship meeting',
  69. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  70. ]);
  71. $vEventOld1->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  72. $vEventOld1->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  73. $vEventOld2 = $vCalendarOld->add('VEVENT', [
  74. 'UID' => 'uid-1235',
  75. 'LAST-MODIFIED' => 123456,
  76. 'SEQUENCE' => 2,
  77. 'SUMMARY' => 'Fellowship meeting',
  78. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  79. ]);
  80. $vEventOld2->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  81. $vEventOld2->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  82. $vEventNew1 = $vCalendarNew->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. $vEventNew1->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  90. $vEventNew1->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  91. $vEventNew2 = $vCalendarNew->add('VEVENT', [
  92. 'UID' => 'uid-1235',
  93. 'LAST-MODIFIED' => 123457,
  94. 'SEQUENCE' => 3,
  95. 'SUMMARY' => 'Fellowship meeting 2',
  96. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  97. ]);
  98. $vEventNew2->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  99. $vEventNew2->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  100. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  101. $this->assertEquals([$vEventOld2], $result['old']);
  102. $this->assertEquals([$vEventNew2], $result['new']);
  103. }
  104. // First test to certify fix for issue nextcloud/server#41084
  105. public function testSequenceNumberIncrementDetectedForFirstModificationToEventWithoutZeroInit(): void {
  106. $vCalendarOld = new VCalendar();
  107. $vCalendarNew = new VCalendar();
  108. $vEventOld = $vCalendarOld->add('VEVENT', [
  109. 'UID' => 'uid-1234',
  110. 'LAST-MODIFIED' => 123456,
  111. // 'SEQUENCE' => 0, // sequence number may not be set to zero during event creation and instead fully omitted
  112. 'SUMMARY' => 'Fellowship meeting',
  113. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  114. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  115. ]);
  116. $vEventOld->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  117. $vEventOld->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  118. $vEventNew = $vCalendarNew->add('VEVENT', [
  119. 'UID' => 'uid-1234',
  120. 'LAST-MODIFIED' => 123456,
  121. 'SEQUENCE' => 1,
  122. 'SUMMARY' => 'Fellowship meeting',
  123. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  124. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  125. ]);
  126. $vEventNew->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  127. $vEventNew->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  128. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  129. $this->assertEquals([$vEventOld], $result['old']);
  130. $this->assertEquals([$vEventNew], $result['new']);
  131. }
  132. // Second test to certify fix for issue nextcloud/server#41084
  133. public function testSequenceNumberIncrementDetectedForFirstModificationToEventWithZeroInit(): void {
  134. $vCalendarOld = new VCalendar();
  135. $vCalendarNew = new VCalendar();
  136. $vEventOld = $vCalendarOld->add('VEVENT', [
  137. 'UID' => 'uid-1234',
  138. 'LAST-MODIFIED' => 123456,
  139. 'SEQUENCE' => 0,
  140. 'SUMMARY' => 'Fellowship meeting',
  141. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  142. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  143. ]);
  144. $vEventOld->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  145. $vEventOld->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  146. $vEventNew = $vCalendarNew->add('VEVENT', [
  147. 'UID' => 'uid-1234',
  148. 'LAST-MODIFIED' => 123456,
  149. 'SEQUENCE' => 1,
  150. 'SUMMARY' => 'Fellowship meeting',
  151. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  152. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  153. ]);
  154. $vEventNew->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  155. $vEventNew->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  156. $result = $this->eventComparisonService->findModified($vCalendarNew, $vCalendarOld);
  157. $this->assertEquals([$vEventOld], $result['old']);
  158. $this->assertEquals([$vEventNew], $result['new']);
  159. }
  160. }