Skip to content

Commit bb52428

Browse files
authored
Merge pull request #3666 from codeeu/fix/online-activities-upcoming-only
Show only upcoming activities on the online activities page
2 parents 8d03774 + 81ae6f7 commit bb52428

2 files changed

Lines changed: 49 additions & 52 deletions

File tree

‎app/Livewire/OnlineCalendar.php‎

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function mount()
3333
->orderBy('start_date')
3434
->get(['start_date'])
3535
->groupBy(function ($event) {
36-
return $this->effectiveStart($event->start_date)->format('n/Y');
36+
return Carbon::parse($event->start_date)->format('n/Y');
3737
})
3838
->map(function ($group, $id) {
3939
[$month, $year] = explode('/', $id);
@@ -68,20 +68,10 @@ public function render()
6868

6969
$monthStart = Carbon::createFromDate($this->selectedYear, $this->selectedMonth, 1)->startOfMonth();
7070

71-
$query->where(function ($monthQuery) use ($monthStart) {
72-
$monthQuery->whereBetween('start_date', [$monthStart, $monthStart->copy()->endOfMonth()]);
73-
74-
if ($monthStart->isSameMonth(Carbon::now())) {
75-
$monthQuery->orWhere('start_date', '<', $monthStart);
76-
}
77-
});
71+
$query->whereBetween('start_date', [$monthStart, $monthStart->copy()->endOfMonth()]);
7872
}
7973

80-
$events = $query->get()
81-
->sortBy(function ($event) {
82-
return $this->effectiveStart($event->start_date)->getTimestamp();
83-
})
84-
->values();
74+
$events = $query->get();
8575

8676
$events->each(function ($event) {
8777
$event->title = str_limit($event->title, 50);
@@ -133,24 +123,12 @@ public function render()
133123

134124
private function baseQuery()
135125
{
136-
// Only the end date gates the list: an activity that began earlier but has not
137-
// finished is still open to participants.
126+
// The start date alone decides what is upcoming. Gating on the end date instead
127+
// surfaced activities starting in 2020-2022 that carry an end date years away.
138128
return Event::where([
139129
'activity_type' => 'open-online',
140130
'status' => 'APPROVED',
141-
])->where('end_date', '>=', Carbon::now());
142-
}
143-
144-
/**
145-
* Activities already under way are listed under the current month rather than the
146-
* month they originally started in, which may be long past.
147-
*/
148-
private function effectiveStart($startDate): Carbon
149-
{
150-
$start = Carbon::parse($startDate);
151-
$currentMonth = Carbon::now()->firstOfMonth();
152-
153-
return $start->lessThan($currentMonth) ? $currentMonth : $start;
131+
])->where('start_date', '>=', Carbon::today());
154132
}
155133

156134
private function eventMatchesLanguage($event, string $selectedLanguage): bool

‎tests/Feature/OnlineEventsWorkflowTest.php‎

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -321,67 +321,86 @@ public function online_activities_month_filter_only_shows_events_for_selected_mo
321321
}
322322

323323
#[Test]
324-
public function online_activities_page_lists_activities_already_under_way(): void
324+
public function online_activities_page_excludes_activities_that_already_started(): void
325325
{
326326
$this->seed('RolesAndPermissionsSeeder');
327327

328-
$ongoing = \App\Event::factory()->create([
329-
'start_date' => Carbon::now()->subMonths(4),
330-
'end_date' => Carbon::now()->addMonths(6),
328+
// Reported by the community: activities dated 2020-2022 were on the page because
329+
// they carry an end date years in the future.
330+
$staleStartDate = \App\Event::factory()->create([
331+
'start_date' => Carbon::now()->subYears(5),
332+
'end_date' => Carbon::now()->addYear(),
331333
'status' => 'APPROVED',
332334
'activity_type' => 'open-online',
333335
'highlighted_status' => 'NONE',
334336
'language' => ['en'],
335-
'title' => 'Still Running Open Online Activity',
337+
'title' => 'Started Years Ago Ends Far Away',
336338
]);
337339

338-
$finished = \App\Event::factory()->create([
339-
'start_date' => Carbon::now()->subMonths(4),
340-
'end_date' => Carbon::now()->subMonth(),
340+
$startedThisMonth = \App\Event::factory()->create([
341+
'start_date' => Carbon::now()->startOfMonth(),
342+
'end_date' => Carbon::now()->addMonths(2),
341343
'status' => 'APPROVED',
342344
'activity_type' => 'open-online',
343345
'highlighted_status' => 'NONE',
344346
'language' => ['en'],
345-
'title' => 'Already Finished Open Online Activity',
347+
'title' => 'Started Earlier This Month',
348+
]);
349+
350+
$upcoming = \App\Event::factory()->create([
351+
'start_date' => Carbon::now()->addWeek(),
352+
'end_date' => Carbon::now()->addWeek()->addDay(),
353+
'status' => 'APPROVED',
354+
'activity_type' => 'open-online',
355+
'highlighted_status' => 'NONE',
356+
'language' => ['en'],
357+
'title' => 'Genuinely Upcoming Activity',
346358
]);
347359

348360
$this->get('/online-activities')
349361
->assertStatus(200)
350-
->assertSee($ongoing->title)
351-
->assertDontSee($finished->title);
362+
->assertSee($upcoming->title)
363+
->assertDontSee($staleStartDate->title)
364+
->assertDontSee($startedThisMonth->title);
352365
}
353366

354367
#[Test]
355-
public function activities_already_under_way_are_listed_under_the_current_month(): void
368+
public function the_month_filter_only_offers_months_that_are_still_to_come(): void
356369
{
357370
$this->seed('RolesAndPermissionsSeeder');
358371

359-
$startedFourMonthsAgo = Carbon::now()->subMonths(4);
372+
$pastStart = Carbon::now()->subYears(5);
360373

361-
$ongoing = \App\Event::factory()->create([
362-
'start_date' => $startedFourMonthsAgo,
363-
'end_date' => Carbon::now()->addMonths(6),
374+
\App\Event::factory()->create([
375+
'start_date' => $pastStart,
376+
'end_date' => Carbon::now()->addYear(),
364377
'status' => 'APPROVED',
365378
'activity_type' => 'open-online',
366379
'highlighted_status' => 'NONE',
367380
'language' => ['en'],
368-
'title' => 'Ongoing Grouped Under Current Month',
381+
'title' => 'Stale Start Date Activity',
369382
]);
370383

371-
$now = Carbon::now();
384+
$nextMonth = Carbon::now()->addMonthNoOverflow()->startOfMonth();
372385

373-
\Livewire\Livewire::test(\App\Livewire\OnlineCalendar::class)
374-
->set('selectedDate', $now->month.'/'.$now->year)
375-
->assertSee($ongoing->title);
386+
\App\Event::factory()->create([
387+
'start_date' => $nextMonth->copy()->addDays(3),
388+
'end_date' => $nextMonth->copy()->addDays(4),
389+
'status' => 'APPROVED',
390+
'activity_type' => 'open-online',
391+
'highlighted_status' => 'NONE',
392+
'language' => ['en'],
393+
'title' => 'Next Month Activity',
394+
]);
376395

377396
$monthIds = collect(\Livewire\Livewire::test(\App\Livewire\OnlineCalendar::class)->get('months'))
378397
->pluck('id');
379398

380-
$this->assertContains($now->month.'/'.$now->year, $monthIds);
399+
$this->assertContains($nextMonth->month.'/'.$nextMonth->year, $monthIds);
381400
$this->assertNotContains(
382-
$startedFourMonthsAgo->month.'/'.$startedFourMonthsAgo->year,
401+
$pastStart->month.'/'.$pastStart->year,
383402
$monthIds,
384-
'The month filter should not offer a past month just because an ongoing activity started then.'
403+
'A past month must not appear in the filter just because an activity carries a far-off end date.'
385404
);
386405
}
387406

0 commit comments

Comments
 (0)