Skip to content

Commit

Permalink
handle bad legacy data
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell committed Feb 20, 2024
1 parent f0f7b30 commit de0bc8a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TransformStudios\Events;

use Carbon\CarbonInterface;
use Exception;
use Illuminate\Support\Traits\Conditionable;
use Statamic\Entries\Entry;
use Statamic\Entries\EntryCollection;
Expand Down Expand Up @@ -165,7 +166,8 @@ private function entries(): self
private function occurrences(callable $generator): EntryCollection
{
return $this->entries
// take each event and generate the occurences
// take each event and generate the occurrences
->filter(fn (Entry $occurrence) => $this->hasStartDate($occurrence))
->flatMap(callback: $generator)
->reject(fn (Entry $occurrence) => collect($occurrence->exclude_dates)
->filter(fn (Values $dateRow) => $dateRow->date)
Expand All @@ -174,6 +176,19 @@ private function occurrences(callable $generator): EntryCollection
->values();
}

private function hasStartDate(Entry $occurrence): bool
{
if ($occurrence->multi_day || $occurrence->get('recurrence') === 'multi_day') {
try {
return collect($occurrence->days)->every(fn (Values $day) => $day->date);
} catch (Exception $e) {
return false;
}
}

return $occurrence->has('start_date');
}

private function paginate(EntryCollection $occurrences): LengthAwarePaginator
{
return new LengthAwarePaginator(
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,34 @@ public function canHandleEmptyExcludeDates()

$this->assertCount(4, $occurrences);
}

/** @test */
public function canFilterOurEventsWithNoStartDate()
{
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

Entry::make()
->collection('events')
->slug('single-event')
->data([
'title' => 'Single Event',
'start_time' => '11:00',
'end_time' => '12:00',
])->save();
Entry::make()
->collection('events')
->slug('legacy-multi-day-event')
->data([
'title' => 'Legacy Multi-day Event',
'multi_day' => true,
'days' => [
['date' => 'bad-date'],
],
])->save();

$occurrences = Events::fromCollection(handle: 'events')
->upcoming(5);

$this->assertEmpty($occurrences);
}
}

0 comments on commit de0bc8a

Please sign in to comment.