Skip to content

Commit 84f00ae

Browse files
authored
Fix invalid timezone value (#180)
* Add pint for formatting * Add tests * Add fix
1 parent 8258a49 commit 84f00ae

7 files changed

Lines changed: 66 additions & 6 deletions

File tree

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"require-dev": {
2121
"laravel/pao": "^1.1",
22+
"laravel/pint": "^1.30",
2223
"orchestra/testbench": "^9.2 || ^10.0",
2324
"pestphp/pest": "^4.0",
2425
"spatie/laravel-ray": "^1.35",

‎src/Types/Event.php‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use RRule\RRuleInterface;
1212
use Spatie\IcalendarGenerator\Components\Event as ICalendarEvent;
1313
use Statamic\Entries\Entry;
14+
use TransformStudios\Events\Events;
1415

1516
abstract class Event
1617
{
@@ -87,14 +88,14 @@ public function startTime(): string
8788
public function start(): CarbonImmutable
8889
{
8990
return CarbonImmutable::parse($this->start_date)
90-
->shiftTimezone($this->timezone['name'])
91+
->shiftTimezone($this->timezoneName())
9192
->setTimeFromTimeString($this->startTime());
9293
}
9394

9495
public function end(): CarbonImmutable
9596
{
9697
return CarbonImmutable::parse($this->start_date)
97-
->shiftTimezone($this->timezone['name'])
98+
->shiftTimezone($this->timezoneName())
9899
->setTimeFromTimeString($this->endTime());
99100
}
100101

@@ -165,14 +166,19 @@ protected function toCarbonImmutable(string|CarbonInterface $date): CarbonImmuta
165166
{
166167
$carbon = is_string($date) ? CarbonImmutable::parse($date) : $date;
167168

168-
return $carbon->shiftTimezone($this->timezone['name']);
169+
return $carbon->shiftTimezone($this->timezoneName());
170+
}
171+
172+
protected function timezoneName(): string
173+
{
174+
return Arr::get($this->timezone->data(), 'name') ?? Events::defaultTimezone();
169175
}
170176

171177
private function collect(array $dates): Collection
172178
{
173179
return collect($dates)
174180
->map(fn (DateTimeInterface $date) => $this->supplement(
175-
date: CarbonImmutable::parse($date, $this->timezone['name'])
181+
date: CarbonImmutable::parse($date, $this->timezoneName())
176182
))->filter();
177183
}
178184
}

‎src/Types/MultiDayEvent.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(Entry $event, private bool $collapseMultiDays)
2525
->sortBy('date')
2626
->map(fn (Values $day) => new Day(
2727
$day->all(),
28-
$this->timezone['name'],
28+
$this->timezoneName(),
2929
$day->all_day || $this->isAllDay(),
3030
));
3131
}

‎src/Types/RecurringEvent.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function rule(bool $useEnd = false): RRuleInterface
6161
];
6262

6363
if ($end = $this->end_date) {
64-
$rule['until'] = CarbonImmutable::parse($end)->shiftTimezone($this->timezone['name'])->endOfDay();
64+
$rule['until'] = CarbonImmutable::parse($end)->shiftTimezone($this->timezoneName())->endOfDay();
6565
}
6666

6767
if (! empty($days = $this->onSpecificDays())) {

‎tests/Types/MultiDayEventsTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace TransformStudios\Events\Tests\Types;
44

55
use Carbon\Carbon;
6+
use Carbon\CarbonTimeZone;
67
use Statamic\Facades\Entry;
78
use TransformStudios\Events\EventFactory;
89
use TransformStudios\Events\Types\MultiDayEvent;
@@ -117,3 +118,23 @@
117118

118119
expect($days[0]->isAllDay())->toBeTrue();
119120
});
121+
122+
test('falls back to default timezone when entry has invalid timezone value', function () {
123+
$entry = Entry::make()
124+
->collection('events')
125+
->data([
126+
'recurrence' => 'multi_day',
127+
'days' => [
128+
[
129+
'date' => '2019-11-23',
130+
'start_time' => '19:00',
131+
'end_time' => '21:00',
132+
],
133+
],
134+
'timezone' => 'est',
135+
]);
136+
137+
$event = EventFactory::createFromEntry($entry);
138+
139+
expect($event->start()->timezone)->toEqual(new CarbonTimeZone('UTC'));
140+
});

‎tests/Types/RecurringEventsTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99
use TransformStudios\Events\Types\RecurringEvent;
1010
use TransformStudios\Events\Types\SingleDayEvent;
1111

12+
test('falls back to default timezone when entry has invalid timezone value', function () {
13+
$recurringEntry = Entry::make()
14+
->collection('events')
15+
->data([
16+
'start_date' => Carbon::now()->toDateString(),
17+
'start_time' => '11:00',
18+
'recurrence' => 'daily',
19+
'end_date' => Carbon::now()->addDays(2)->toDateString(),
20+
'timezone' => 'est',
21+
]);
22+
23+
$event = EventFactory::createFromEntry($recurringEntry);
24+
25+
expect($event->nextOccurrences())->not->toBeEmpty();
26+
});
27+
1228
test('can create recurring event', function () {
1329
$recurringEntry = Entry::make()
1430
->collection('events')

‎tests/Types/SingleDayEventsTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@
3030
expect($event->end()->timezone)->toEqual(new CarbonTimeZone('America/Vancouver'));
3131
});
3232

33+
test('falls back to default timezone when entry has invalid timezone value', function () {
34+
$entry = Entry::make()
35+
->collection('events')
36+
->data([
37+
'start_date' => Carbon::now()->toDateString(),
38+
'start_time' => '11:00',
39+
'end_time' => '12:00',
40+
'timezone' => 'est',
41+
]);
42+
43+
$event = EventFactory::createFromEntry($entry);
44+
45+
expect($event->start()->timezone)->toEqual(new CarbonTimeZone('UTC'));
46+
expect($event->end()->timezone)->toEqual(new CarbonTimeZone('UTC'));
47+
});
48+
3349
test('can create single all day event', function () {
3450
$entry = Entry::make()
3551
->collection('events')

0 commit comments

Comments
 (0)