From 1a0101772f2d98c62e40365b570b7a1521da02b7 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 1 Aug 2024 21:08:20 +0100 Subject: [PATCH] Only past talks are counted Talks at an event happening "today" haven't happened yet, so shouldn't be counted. A talk should only counted if it was given at a past event - i.e. yesterday or earlier. Previously a talk with today's date was returning a time of 00:00 and was being counted in the past count if the site was generated on the same day. --- .../TwigExtension/OpdaviesTwigExtension.php | 2 +- .../OpdaviesTwigExtensionTest.php | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Opdavies/TwigExtension/OpdaviesTwigExtension.php b/src/Opdavies/TwigExtension/OpdaviesTwigExtension.php index 9c9fc41d..6d664998 100644 --- a/src/Opdavies/TwigExtension/OpdaviesTwigExtension.php +++ b/src/Opdavies/TwigExtension/OpdaviesTwigExtension.php @@ -23,7 +23,7 @@ class OpdaviesTwigExtension extends AbstractExtension public function getPastTalkCount(array $talks): int { - $today = (new \DateTime())->getTimestamp(); + $today = (new \DateTime('today'))->getTimestamp(); return collect($talks) ->flatMap(fn (ProxySourceItem $talk) => $talk->data()->get('events')) diff --git a/tests/Opdavies/TwigExtension/OpdaviesTwigExtensionTest.php b/tests/Opdavies/TwigExtension/OpdaviesTwigExtensionTest.php index 42852d50..ebd03d46 100644 --- a/tests/Opdavies/TwigExtension/OpdaviesTwigExtensionTest.php +++ b/tests/Opdavies/TwigExtension/OpdaviesTwigExtensionTest.php @@ -90,6 +90,30 @@ class OpdaviesTwigExtensionTest extends TestCase $this->assertTalkCount(expectedCount: 2, talks: [$talkA, $talkB]); } + public function testTheCurrentDayIsNotCounted(): void + { + $talkA = $this->createTalk( + events: [ + ['date' => (new \DateTime('yesterday'))->getTimestamp()], + ['date' => (new \DateTime('today'))->getTimestamp()], + ], + ); + + $talkB = $this->createTalk( + events: [ + ['date' => (new \DateTime('today'))->getTimestamp()], + ], + ); + + $talkC = $this->createTalk( + events: [ + ['date' => (new \DateTime('yesterday'))->getTimestamp()], + ], + ); + + $this->assertTalkCount(expectedCount: 2, talks: [$talkA, $talkB, $talkC]); + } + /** * Assert the extension uses the correct number of talks. */