|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TransformStudios\Events\Tests; |
| 4 | + |
| 5 | +use Illuminate\Support\Carbon; |
| 6 | +use Statamic\Facades\Entry; |
| 7 | +use TransformStudios\Events\Events; |
| 8 | + |
| 9 | +beforeEach(function () { |
| 10 | + Carbon::setTestNow(now()->setTimeFromTimeString('10:00')); |
| 11 | +}); |
| 12 | + |
| 13 | +test('keeps unbounded recurring event that started years ago', function () { |
| 14 | + Entry::make() |
| 15 | + ->collection('events') |
| 16 | + ->slug('weekly-halaqa') |
| 17 | + ->data([ |
| 18 | + 'title' => 'Weekly Halaqa', |
| 19 | + 'start_date' => now()->subYears(2)->toDateString(), |
| 20 | + 'start_time' => '19:00', |
| 21 | + 'end_time' => '20:00', |
| 22 | + 'recurrence' => 'weekly', |
| 23 | + ])->save(); |
| 24 | + |
| 25 | + expect(Events::fromCollection('events')->upcoming(1))->toHaveCount(1); |
| 26 | +}); |
| 27 | + |
| 28 | +test('keeps annually recurring event that started years ago', function () { |
| 29 | + Entry::make() |
| 30 | + ->collection('events') |
| 31 | + ->slug('annual-fundraiser') |
| 32 | + ->data([ |
| 33 | + 'title' => 'Annual Fundraiser', |
| 34 | + 'start_date' => now()->subYears(3)->toDateString(), |
| 35 | + 'start_time' => '18:00', |
| 36 | + 'end_time' => '21:00', |
| 37 | + 'recurrence' => 'every', |
| 38 | + 'interval' => 1, |
| 39 | + 'period' => 'years', |
| 40 | + ])->save(); |
| 41 | + |
| 42 | + expect(Events::fromCollection('events')->upcoming(1))->toHaveCount(1); |
| 43 | +}); |
| 44 | + |
| 45 | +test('keeps recurring event whose end date is still in the future', function () { |
| 46 | + Entry::make() |
| 47 | + ->collection('events') |
| 48 | + ->slug('term-class') |
| 49 | + ->data([ |
| 50 | + 'title' => 'Term Class', |
| 51 | + 'start_date' => now()->subYear()->toDateString(), |
| 52 | + 'start_time' => '09:00', |
| 53 | + 'end_time' => '10:00', |
| 54 | + 'recurrence' => 'weekly', |
| 55 | + 'end_date' => now()->addMonths(2)->toDateString(), |
| 56 | + ])->save(); |
| 57 | + |
| 58 | + expect(Events::fromCollection('events')->upcoming(1))->toHaveCount(1); |
| 59 | +}); |
| 60 | + |
| 61 | +test('drops recurring event whose end date has passed', function () { |
| 62 | + Entry::make() |
| 63 | + ->collection('events') |
| 64 | + ->slug('finished-class') |
| 65 | + ->data([ |
| 66 | + 'title' => 'Finished Class', |
| 67 | + 'start_date' => now()->subYears(2)->toDateString(), |
| 68 | + 'start_time' => '09:00', |
| 69 | + 'end_time' => '10:00', |
| 70 | + 'recurrence' => 'weekly', |
| 71 | + 'end_date' => now()->subYear()->toDateString(), |
| 72 | + ])->save(); |
| 73 | + |
| 74 | + expect(Events::fromCollection('events')->upcoming(1))->toBeEmpty(); |
| 75 | +}); |
| 76 | + |
| 77 | +test('drops single day event in the past but keeps one in the future', function () { |
| 78 | + Entry::make() |
| 79 | + ->collection('events') |
| 80 | + ->slug('past-lecture') |
| 81 | + ->data([ |
| 82 | + 'title' => 'Past Lecture', |
| 83 | + 'start_date' => now()->subMonth()->toDateString(), |
| 84 | + 'start_time' => '19:00', |
| 85 | + ])->save(); |
| 86 | + |
| 87 | + Entry::make() |
| 88 | + ->collection('events') |
| 89 | + ->slug('future-lecture') |
| 90 | + ->data([ |
| 91 | + 'title' => 'Future Lecture', |
| 92 | + 'start_date' => now()->addMonth()->toDateString(), |
| 93 | + 'start_time' => '19:00', |
| 94 | + ])->save(); |
| 95 | + |
| 96 | + $occurrences = Events::fromCollection('events')->upcoming(1); |
| 97 | + |
| 98 | + expect($occurrences)->toHaveCount(1); |
| 99 | + expect($occurrences->first()->title)->toBe('Future Lecture'); |
| 100 | +}); |
| 101 | + |
| 102 | +test('drops multi day event whose days have all passed but keeps one with a day still to come', function () { |
| 103 | + Entry::make() |
| 104 | + ->collection('events') |
| 105 | + ->slug('past-conference') |
| 106 | + ->data([ |
| 107 | + 'title' => 'Past Conference', |
| 108 | + 'recurrence' => 'multi_day', |
| 109 | + 'days' => [ |
| 110 | + ['date' => now()->subMonth()->toDateString(), 'start_time' => '09:00', 'end_time' => '17:00'], |
| 111 | + ['date' => now()->subMonth()->addDay()->toDateString(), 'start_time' => '09:00', 'end_time' => '17:00'], |
| 112 | + ], |
| 113 | + ])->save(); |
| 114 | + |
| 115 | + Entry::make() |
| 116 | + ->collection('events') |
| 117 | + ->slug('upcoming-conference') |
| 118 | + ->data([ |
| 119 | + 'title' => 'Upcoming Conference', |
| 120 | + 'recurrence' => 'multi_day', |
| 121 | + 'days' => [ |
| 122 | + ['date' => now()->addMonth()->toDateString(), 'start_time' => '09:00', 'end_time' => '17:00'], |
| 123 | + ['date' => now()->addMonth()->addDay()->toDateString(), 'start_time' => '09:00', 'end_time' => '17:00'], |
| 124 | + ], |
| 125 | + ])->save(); |
| 126 | + |
| 127 | + $occurrences = Events::fromCollection('events')->upcoming(5); |
| 128 | + |
| 129 | + expect($occurrences)->not->toBeEmpty(); |
| 130 | + expect($occurrences->pluck('title')->unique()->all())->toBe(['Upcoming Conference']); |
| 131 | +}); |
| 132 | + |
| 133 | +test('keeps a multi day event that is partway through', function () { |
| 134 | + Entry::make() |
| 135 | + ->collection('events') |
| 136 | + ->slug('current-conference') |
| 137 | + ->data([ |
| 138 | + 'title' => 'Current Conference', |
| 139 | + 'recurrence' => 'multi_day', |
| 140 | + 'days' => [ |
| 141 | + ['date' => now()->subDay()->toDateString(), 'start_time' => '09:00', 'end_time' => '17:00'], |
| 142 | + ['date' => now()->addDay()->toDateString(), 'start_time' => '09:00', 'end_time' => '17:00'], |
| 143 | + ], |
| 144 | + ])->save(); |
| 145 | + |
| 146 | + expect(Events::fromCollection('events')->upcoming(5))->not->toBeEmpty(); |
| 147 | +}); |
| 148 | + |
| 149 | +test('past events are still returned when explicitly querying a past range', function () { |
| 150 | + Entry::make() |
| 151 | + ->collection('events') |
| 152 | + ->slug('last-years-lecture') |
| 153 | + ->data([ |
| 154 | + 'title' => 'Last Years Lecture', |
| 155 | + 'start_date' => now()->subYear()->toDateString(), |
| 156 | + 'start_time' => '19:00', |
| 157 | + 'end_time' => '20:00', |
| 158 | + ])->save(); |
| 159 | + |
| 160 | + $occurrences = Events::fromCollection('events')->between( |
| 161 | + now()->subYear()->startOfDay(), |
| 162 | + now()->subYear()->endOfDay(), |
| 163 | + ); |
| 164 | + |
| 165 | + expect($occurrences)->toHaveCount(1); |
| 166 | +}); |
0 commit comments