forked from symfony/ux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIcsBuilderTest.php
More file actions
218 lines (179 loc) · 8.12 KB
/
Copy pathIcsBuilderTest.php
File metadata and controls
218 lines (179 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\UX\CalendarLink\Tests\Ics;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Factory\MockUuidFactory;
use Symfony\UX\CalendarLink\CalendarEvent;
use Symfony\UX\CalendarLink\CalendarRecurrence;
use Symfony\UX\CalendarLink\CalendarReminder;
use Symfony\UX\CalendarLink\Ics\IcsBuilder;
final class IcsBuilderTest extends TestCase
{
private IcsBuilder $builder;
protected function setUp(): void
{
$this->builder = new IcsBuilder(new MockUuidFactory(['0192a5d2-7c6f-7000-8000-000000000000']));
}
public function testUidIsUniquePerBuild()
{
$builder = new IcsBuilder();
$event = new CalendarEvent(
title: 'Demo',
start: new \DateTimeImmutable('2026-05-14 09:00', new \DateTimeZone('UTC')),
end: new \DateTimeImmutable('2026-05-14 10:00', new \DateTimeZone('UTC')),
);
preg_match('/^UID:(.+)$/m', $builder->build($event), $first);
preg_match('/^UID:(.+)$/m', $builder->build($event), $second);
$this->assertNotSame($first[1], $second[1]);
}
public function testMinimalTimedEventStructure()
{
$event = new CalendarEvent(
title: 'Demo',
start: new \DateTimeImmutable('2026-05-14 09:00', new \DateTimeZone('UTC')),
end: new \DateTimeImmutable('2026-05-14 10:00', new \DateTimeZone('UTC')),
);
$ics = $this->builder->build($event);
$this->assertStringContainsString(
"DTSTART:20260514T090000Z\r\n"
."DTEND:20260514T100000Z\r\n"
."SUMMARY:Demo\r\n"
."END:VEVENT\r\n"
."END:VCALENDAR\r\n",
$ics,
);
}
public function testAllDayEventUsesValueDateAndIncrementsEnd()
{
$event = new CalendarEvent(
title: 'Conf',
start: new \DateTimeImmutable('2026-05-14', new \DateTimeZone('UTC')),
end: new \DateTimeImmutable('2026-05-15', new \DateTimeZone('UTC')),
allDay: true,
);
$ics = $this->builder->build($event);
$this->assertStringContainsString("DTSTART;VALUE=DATE:20260514\r\n", $ics);
$this->assertStringContainsString("DTEND;VALUE=DATE:20260516\r\n", $ics);
}
public function testTimedEventInNamedZoneUsesTzidInsteadOfUtc()
{
$event = new CalendarEvent(
title: 'Standup',
start: new \DateTimeImmutable('2026-07-01 09:00', new \DateTimeZone('Europe/Paris')),
end: new \DateTimeImmutable('2026-07-01 09:30', new \DateTimeZone('Europe/Paris')),
);
$ics = $this->builder->build($event);
$this->assertStringContainsString("DTSTART;TZID=Europe/Paris:20260701T090000\r\n", $ics);
$this->assertStringContainsString("DTEND;TZID=Europe/Paris:20260701T093000\r\n", $ics);
}
public function testNamedZoneEmitsVtimezoneWithDstRules()
{
$event = new CalendarEvent(
title: 'Standup',
start: new \DateTimeImmutable('2026-07-01 09:00', new \DateTimeZone('Europe/Paris')),
end: new \DateTimeImmutable('2026-07-01 09:30', new \DateTimeZone('Europe/Paris')),
recurrence: CalendarRecurrence::weekly(),
);
$ics = $this->builder->build($event);
$this->assertStringContainsString("BEGIN:VTIMEZONE\r\nTZID:Europe/Paris\r\n", $ics);
$this->assertStringContainsString("BEGIN:DAYLIGHT\r\n", $ics);
$this->assertStringContainsString("TZOFFSETTO:+0200\r\n", $ics);
$this->assertStringContainsString("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\n", $ics);
$this->assertStringContainsString("BEGIN:STANDARD\r\n", $ics);
$this->assertStringContainsString("TZOFFSETTO:+0100\r\n", $ics);
$this->assertStringContainsString("RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\n", $ics);
}
public function testUtcEventDoesNotEmitVtimezone()
{
$event = new CalendarEvent(
title: 'Demo',
start: new \DateTimeImmutable('2026-05-14 09:00', new \DateTimeZone('UTC')),
end: new \DateTimeImmutable('2026-05-14 10:00', new \DateTimeZone('UTC')),
);
$ics = $this->builder->build($event);
$this->assertStringNotContainsString('BEGIN:VTIMEZONE', $ics);
$this->assertStringContainsString("DTSTART:20260514T090000Z\r\n", $ics);
}
public function testTextEscaping()
{
$event = new CalendarEvent(
title: 'Symfony, UX; test',
start: new \DateTimeImmutable('2026-05-14 09:00', new \DateTimeZone('UTC')),
end: new \DateTimeImmutable('2026-05-14 10:00', new \DateTimeZone('UTC')),
description: "Line 1\nLine 2",
location: 'A\\B',
);
$ics = $this->builder->build($event);
$this->assertStringContainsString("SUMMARY:Symfony\\, UX\\; test\r\n", $ics);
$this->assertStringContainsString('DESCRIPTION:Line 1\nLine 2', $ics);
$this->assertStringContainsString('LOCATION:A\\\\B', $ics);
}
public function testLineFoldingAtSeventyFiveOctets()
{
$event = new CalendarEvent(
title: 'Demo',
start: new \DateTimeImmutable('2026-05-14 09:00', new \DateTimeZone('UTC')),
end: new \DateTimeImmutable('2026-05-14 10:00', new \DateTimeZone('UTC')),
description: str_repeat('a', 300),
);
$ics = $this->builder->build($event);
foreach (explode("\r\n", $ics) as $line) {
$this->assertLessThanOrEqual(75, \strlen($line), \sprintf('Line "%s" exceeds 75 octets.', $line));
}
}
/**
* @return iterable<string, array{CalendarReminder, string}>
*/
public static function triggerFormatProvider(): iterable
{
yield 'minutes' => [CalendarReminder::before(minutes: 15), '-PT15M'];
yield 'exact hours' => [CalendarReminder::before(hours: 2), '-PT2H'];
yield 'exact days' => [CalendarReminder::before(days: 1), '-P1D'];
yield 'exact weeks' => [CalendarReminder::before(weeks: 1), '-P1W'];
yield 'mixed not divisible by 60' => [CalendarReminder::before(hours: 23, minutes: 88), '-PT1468M'];
yield 'mixed hours and minutes' => [CalendarReminder::before(hours: 1, minutes: 30), '-PT90M'];
}
#[DataProvider('triggerFormatProvider')]
public function testTriggerFormat(CalendarReminder $reminder, string $expectedTrigger)
{
$event = new CalendarEvent(
title: 'Demo',
start: new \DateTimeImmutable('2026-05-14 09:00', new \DateTimeZone('UTC')),
end: new \DateTimeImmutable('2026-05-14 10:00', new \DateTimeZone('UTC')),
reminders: [$reminder],
);
$this->assertStringContainsString("TRIGGER:$expectedTrigger\r\n", $this->builder->build($event));
}
public function testValarmBlockFromReminders()
{
$event = new CalendarEvent(
title: 'Demo',
start: new \DateTimeImmutable('2026-05-14 09:00', new \DateTimeZone('UTC')),
end: new \DateTimeImmutable('2026-05-14 10:00', new \DateTimeZone('UTC')),
reminders: [CalendarReminder::before(minutes: 15, description: 'Stand-up')],
);
$ics = $this->builder->build($event);
$this->assertStringContainsString("BEGIN:VALARM\r\n", $ics);
$this->assertStringContainsString("TRIGGER:-PT15M\r\n", $ics);
$this->assertStringContainsString("END:VALARM\r\n", $ics);
}
public function testRrulePassthrough()
{
$event = new CalendarEvent(
title: 'Weekly',
start: new \DateTimeImmutable('2026-05-14 09:00', new \DateTimeZone('UTC')),
end: new \DateTimeImmutable('2026-05-14 10:00', new \DateTimeZone('UTC')),
recurrence: CalendarRecurrence::weekly(count: 10),
);
$ics = $this->builder->build($event);
$this->assertStringContainsString("RRULE:FREQ=WEEKLY;COUNT=10\r\n", $ics);
}
}