-
-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathLinkTest.php
More file actions
459 lines (387 loc) · 16.2 KB
/
Copy pathLinkTest.php
File metadata and controls
459 lines (387 loc) · 16.2 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
declare(strict_types=1);
namespace Spatie\CalendarLinks\Tests;
use DateTime;
use DateTimeZone;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use Spatie\CalendarLinks\Exceptions\InvalidLink;
use Spatie\CalendarLinks\Generators\Ics;
use Spatie\CalendarLinks\Link;
final class LinkTest extends TestCase
{
#[Test]
public function it_is_initializable(): void
{
$this->assertInstanceOf(Link::class, $this->createShortEventLink());
}
#[Test]
public function it_will_throw_an_exception_when_to_comes_after_from(): void
{
$this->expectException(InvalidLink::class);
new Link(
'Birthday',
DateTime::createFromFormat('Y-m-d H:i', '2018-02-01 18:00'),
DateTime::createFromFormat('Y-m-d H:i', '2018-02-01 09:00')
);
}
#[Test]
public function it_has_a_title(): void
{
$this->assertEquals('Birthday', $this->createShortEventLink()->title);
}
#[Test]
public function it_has_a_mutable_from_date(): void
{
$this->assertEquals(new DateTime('20180201T090000 UTC'), $this->createShortEventLink()->from);
}
#[Test]
public function it_has_a_mutable_to_date(): void
{
$this->assertEquals(new DateTime('20180201T180000 UTC'), $this->createShortEventLink()->to);
}
#[Test]
public function it_has_an_immutable_from_date(): void
{
$this->assertEquals(new DateTime('20180201T090000 UTC'), $this->createShortEventLink()->from);
}
#[Test]
public function it_has_an_immutable_to_date(): void
{
$this->assertEquals(new \DateTimeImmutable('20180201T180000 UTC'), $this->createShortEventLink()->to);
}
#[Test]
public function it_can_have_a_description(): void
{
$link = $this->createShortEventLink();
$correctDescription = 'With balloons, clowns and stuff
Bring a dog, bring a frog';
$this->assertEquals($correctDescription, $link->description);
}
#[Test]
public function it_can_have_an_address(): void
{
$link = $this->createShortEventLink();
$this->assertEquals('Party Lane 1A, 1337 Funtown', $link->address);
}
#[Test]
public function it_keeps_both_timezones_of_a_cross_timezone_event(): void
{
$flight = $this->createFlightWithDistinctTimezonesLink();
$this->assertSame('Asia/Tokyo', $flight->fromTimezone->getName());
$this->assertSame('America/Los_Angeles', $flight->toTimezone->getName());
$this->assertTrue($flight->hasDistinctTimezones());
}
#[Test]
public function it_still_normalises_the_end_date_into_the_start_timezone(): void
{
$flight = $this->createFlightWithDistinctTimezonesLink();
// Recording the zones must not change $from or $to themselves.
$this->assertSame('Asia/Tokyo', $flight->to->getTimezone()->getName());
$this->assertSame('2027-03-16T01:30:00+09:00', $flight->to->format('c'));
$this->assertSame('2027-03-15T09:30:00-07:00', $flight->to->setTimezone($flight->toTimezone)->format('c'));
}
/** @param non-empty-string $timezone */
#[Test]
#[TestWith(['UTC'])]
#[TestWith(['Asia/Tokyo'])]
#[TestWith(['Europe/Brussels'])]
// A backward name is an alias of a region, whether or not it is spelled with a slash.
#[TestWith(['US/Pacific'])]
#[TestWith(['Japan'])]
#[TestWith(['GB'])]
public function it_reports_a_name_that_stands_for_a_place_as_resolvable(string $timezone): void
{
$this->assertTrue($this->createEventLinkInTimezone($timezone)->hasResolvableTimezones());
}
/** @param non-empty-string $timezone */
#[Test]
#[TestWith(['+02:00'])]
#[TestWith(['-05:00'])]
#[TestWith(['CEST'])]
// The TZDB entries that stand for no place: a POSIX rule set, a spelling of UTC, the placeholder.
#[TestWith(['EST'])]
#[TestWith(['MST7MDT'])]
#[TestWith(['GMT'])]
#[TestWith(['Universal'])]
#[TestWith(['Etc/GMT+5'])]
#[TestWith(['Etc/UTC'])]
#[TestWith(['Factory'])]
public function it_does_not_report_a_placeless_name_as_resolvable(string $timezone): void
{
$this->assertFalse($this->createEventLinkInTimezone($timezone)->hasResolvableTimezones());
}
#[Test]
public function it_still_names_the_start_zone_when_the_other_end_is_a_refused_spelling(): void
{
// `UTC` to `Etc/UTC` collapses to a single zone, so the end zone is a label the generators
// discard. Refusing that spelling must not drag the event into the UTC fallback and cost the
// start zone the name it was going to be written under.
$link = $this->createEventAcrossUtcAliasesLink();
$this->assertFalse($link->hasDistinctTimezones());
$this->assertTrue($link->hasResolvableTimezones());
$this->assertStringContainsString('&ctz=UTC', $link->google());
}
#[Test]
public function it_does_not_report_resolvable_timezones_when_only_one_end_names_a_place(): void
{
// Naming one end and not the other would leave the pair inconsistent, so both go to UTC.
$this->assertFalse($this->createFlightWithUnresolvableEndTimezoneLink()->hasResolvableTimezones());
$this->assertTrue($this->createFlightWithDistinctTimezonesLink()->hasResolvableTimezones());
}
#[Test]
public function it_does_not_report_distinct_timezones_for_a_single_zone_event(): void
{
$this->assertFalse($this->createShortEventLink()->hasDistinctTimezones());
}
#[Test]
public function it_does_not_report_distinct_timezones_for_an_all_day_event(): void
{
// An all-day event has no clock time to place in a zone, whatever it was given.
$this->assertFalse($this->createEventMultipleDaysViaStartEndWithDiffTimezoneLink()->hasDistinctTimezones());
}
#[Test]
public function it_does_not_report_distinct_timezones_for_two_spellings_of_utc(): void
{
$this->assertFalse($this->createEventAcrossUtcAliasesLink()->hasDistinctTimezones());
}
#[Test]
public function it_does_not_report_distinct_timezones_for_a_z_suffixed_start(): void
{
$link = $this->createEventWithZSuffixedStartLink();
$this->assertSame('Z', $link->fromTimezone->getName());
$this->assertFalse($link->hasDistinctTimezones());
}
#[Test]
public function it_does_not_report_distinct_timezones_for_an_offset_matching_the_start_zone(): void
{
// A bare offset names no place, so it adds nothing to the zone it is paired with.
$link = Link::create(
'Meeting',
new DateTime('2026-01-01 10:00', new DateTimeZone('UTC')),
new DateTime('2026-01-01T11:00:00+00:00'),
);
$this->assertFalse($link->hasDistinctTimezones());
}
#[Test]
public function it_reports_distinct_timezones_for_two_places_sharing_an_offset(): void
{
// Equal offsets are not enough to merge two real places: the destination is still worth naming.
$flight = $this->createFlightBetweenPlacesSharingAnOffsetLink();
$this->assertSame(0, $flight->fromTimezone->getOffset($flight->from));
$this->assertSame(0, $flight->toTimezone->getOffset($flight->to));
$this->assertTrue($flight->hasDistinctTimezones());
}
#[Test]
public function it_reports_distinct_timezones_for_a_legacy_alias_naming_a_place(): void
{
$flight = $this->createFlightBookedWithALegacyZoneAliasLink();
$this->assertSame(32400, $flight->fromTimezone->getOffset($flight->from));
$this->assertSame(32400, $flight->toTimezone->getOffset($flight->to));
$this->assertTrue($flight->hasDistinctTimezones());
}
#[Test]
public function it_reports_distinct_timezones_for_every_legacy_alias_sharing_an_offset(): void
{
// The IANA backward names are ordinary places wearing an old label, and the timezone
// database gives them the same `??` country as the UTC family, so they need naming outright.
$pairs = [
['Singapore', 'Asia/Kuala_Lumpur'],
['Poland', 'Europe/Berlin'],
['Hongkong', 'Asia/Shanghai'],
['US/Eastern', 'America/Toronto'],
['GB', 'Europe/Lisbon'],
];
foreach ($pairs as [$from, $to]) {
$link = Link::create(
'Flight',
new DateTime('2026-01-15 09:00', new DateTimeZone($from)),
new DateTime('2026-01-15 11:45', new DateTimeZone($to)),
);
$this->assertSame(
$link->fromTimezone->getOffset($link->from),
$link->toTimezone->getOffset($link->to),
"$from and $to should share an offset, otherwise this proves nothing",
);
$this->assertTrue($link->hasDistinctTimezones(), "$from to $to should keep both zones");
}
}
#[Test]
public function it_reports_distinct_timezones_for_a_posix_rule_zone(): void
{
// EST5EDT names no place, but it carries daylight saving rules rather than one fixed offset,
// so it counts as a zone of its own. Folding it away would drop America/New_York instead.
$link = Link::create(
'Meeting',
new DateTime('2026-01-15 09:00', new DateTimeZone('EST5EDT')),
new DateTime('2026-01-15 11:00', new DateTimeZone('America/New_York')),
);
$this->assertTrue($link->hasDistinctTimezones());
}
#[Test]
public function it_does_not_report_distinct_timezones_when_only_one_side_names_a_place(): void
{
// Etc/GMT+5 is a fixed offset with no place of its own, so at a matching offset it adds
// nothing to the zone opposite it.
$link = Link::create(
'Meeting',
new DateTime('2026-01-15 09:00', new DateTimeZone('America/New_York')),
new DateTime('2026-01-15 11:00', new DateTimeZone('Etc/GMT+5')),
);
$this->assertFalse($link->hasDistinctTimezones());
}
#[Test]
public function it_does_not_report_distinct_timezones_across_a_daylight_saving_change_in_one_zone(): void
{
// Both endpoints are named `Europe/London`, so the zones match even though the offsets do not.
$link = Link::create(
'Clocks going forward',
new DateTime('2026-03-29 00:30', new DateTimeZone('Europe/London')),
new DateTime('2026-03-29 02:30', new DateTimeZone('Europe/London')),
);
$this->assertNotSame($link->fromTimezone->getOffset($link->from), $link->toTimezone->getOffset($link->to));
$this->assertFalse($link->hasDistinctTimezones());
}
#[Test]
public function it_does_not_report_distinct_timezones_for_an_all_day_event_across_utc_spellings(): void
{
$link = new Link(
'New Year break',
new DateTime('2026-01-01', new DateTimeZone('UTC')),
new DateTime('2026-01-02', new DateTimeZone('Etc/UTC')),
true,
);
$this->assertFalse($link->hasDistinctTimezones());
$this->assertStringContainsString('dates=20260101/20260103', $link->google());
}
#[Test]
public function it_keeps_the_inclusive_end_date_of_a_cross_timezone_all_day_event(): void
{
// New Year's Day and the day after, written by a caller whose two dates carry different zones.
$link = new Link(
'New Year break',
new DateTime('2026-01-01', new DateTimeZone('America/New_York')),
new DateTime('2026-01-02', new DateTimeZone('Europe/London')),
true,
);
$this->assertStringContainsString('dates=20260101/20260103', $link->google());
$this->assertStringContainsString('DURATION:P2D', $link->ics([], ['format' => Ics::FORMAT_FILE]));
}
#[Test]
public function it_leaves_a_single_timezone_all_day_event_alone(): void
{
// The control for the test above: same dates, one zone, so nothing is reinterpreted.
$link = new Link(
'New Year break',
new DateTime('2026-01-01', new DateTimeZone('America/New_York')),
new DateTime('2026-01-02', new DateTimeZone('America/New_York')),
true,
);
$this->assertStringContainsString('dates=20260101/20260103', $link->google());
$this->assertStringContainsString('DURATION:P2D', $link->ics([], ['format' => Ics::FORMAT_FILE]));
}
#[Test]
public function it_still_rejects_a_negative_range_across_timezones_when_all_day(): void
{
$this->expectException(InvalidLink::class);
new Link(
'New Year break',
new DateTime('2026-01-02', new DateTimeZone('America/New_York')),
new DateTime('2026-01-01', new DateTimeZone('Europe/London')),
true,
);
}
#[Test]
public function it_can_have_required_and_optional_guests(): void
{
$link = $this->createShortEventLink()
->guest('santa@example.com')
->guest('krampus@example.com', optional: true);
$this->assertSame([
['email' => 'santa@example.com', 'optional' => false],
['email' => 'krampus@example.com', 'optional' => true],
], $link->guests);
}
#[Test]
public function it_can_have_several_guests_added_at_once(): void
{
$link = $this->createShortEventLink()
->guests(['santa@example.com', 'krampus@example.com'], optional: true);
$this->assertSame([
['email' => 'santa@example.com', 'optional' => true],
['email' => 'krampus@example.com', 'optional' => true],
], $link->guests);
}
#[Test]
public function it_ignores_a_guest_that_is_already_on_the_list(): void
{
$link = $this->createShortEventLink()
->guest('santa@example.com')
->guest('santa@example.com');
$this->assertSame([
['email' => 'santa@example.com', 'optional' => false],
], $link->guests);
}
#[Test]
public function it_ignores_a_guest_whose_address_differs_only_in_case(): void
{
$link = $this->createShortEventLink()
->guest('santa@example.com')
->guest('Santa@Example.COM');
// The first spelling is the one that survives.
$this->assertSame([
['email' => 'santa@example.com', 'optional' => false],
], $link->guests);
}
#[Test]
public function it_keeps_the_role_a_duplicated_guest_was_first_added_with(): void
{
$link = $this->createShortEventLink()
->guest('santa@example.com')
->guest('santa@example.com', optional: true);
$this->assertSame([
['email' => 'santa@example.com', 'optional' => false],
], $link->guests);
}
#[Test]
public function it_ignores_a_duplicate_inside_a_bulk_of_guests(): void
{
$link = $this->createShortEventLink()
->guest('santa@example.com')
->guests(['krampus@example.com', 'KRAMPUS@example.com', 'santa@example.com']);
$this->assertSame([
['email' => 'santa@example.com', 'optional' => false],
['email' => 'krampus@example.com', 'optional' => false],
], $link->guests);
}
#[Test]
public function it_will_throw_an_exception_for_an_invalid_guest_email(): void
{
$this->expectException(InvalidLink::class);
$this->createShortEventLink()->guest('not-an-email');
}
#[Test]
public function it_will_throw_an_exception_for_a_guest_email_with_a_display_name(): void
{
$this->expectException(InvalidLink::class);
$this->createShortEventLink()->guest('Santa <santa@example.com>');
}
#[Test]
public function it_will_throw_an_exception_for_a_guest_email_with_a_quoted_local_part(): void
{
$this->expectException(InvalidLink::class);
// Passes FILTER_VALIDATE_EMAIL, but its comma would be read as an address separator by Google and Yahoo.
$this->createShortEventLink()->guest('"santa,claus"@example.com');
}
#[Test]
public function it_adds_no_guest_at_all_when_one_address_of_a_bulk_is_invalid(): void
{
$link = $this->createShortEventLink();
try {
$link->guests(['santa@example.com', 'not-an-email']);
} catch (InvalidLink) {
// Expected.
}
$this->assertSame([], $link->guests);
}
}