From b087098793369c10b8c094fe413a8220548fc563 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Mon, 16 Sep 2024 12:29:46 +0200 Subject: [PATCH] Fix broken month view if last week of December is in next year (#9) The cause was using the wrong character in formatting the year. We now switch from `Y` to `o` which will work based on the week instead of date. This is necessary as we provide this year to the week, and therefore need the year of the week, not day. Resolves: #11388 --- Classes/Domain/Model/Month.php | 2 +- Classes/Domain/Model/Week.php | 4 ++-- Documentation/Changelog/1.1.1.rst | 7 ++++++- Tests/Unit/Domain/Model/MonthTest.php | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Classes/Domain/Model/Month.php b/Classes/Domain/Model/Month.php index 1bc5102..04a6143 100644 --- a/Classes/Domain/Model/Month.php +++ b/Classes/Domain/Model/Month.php @@ -78,7 +78,7 @@ class Month while ($currentDay <= $lastDay) { $this->weeks[] = new Week( (int) $currentDay->format('W'), - (int) $currentDay->format('Y') + (int) $currentDay->format('o') ); $currentDay = $currentDay->modify('+7 days'); diff --git a/Classes/Domain/Model/Week.php b/Classes/Domain/Model/Week.php index f02ab53..81e1c65 100644 --- a/Classes/Domain/Model/Week.php +++ b/Classes/Domain/Model/Week.php @@ -80,7 +80,7 @@ class Week return new self( (int) $newDay->format('W'), - (int) $newDay->format('Y') + (int) $newDay->format('o') ); } @@ -90,7 +90,7 @@ class Week return new self( (int) $newDay->format('W'), - (int) $newDay->format('Y') + (int) $newDay->format('o') ); } diff --git a/Documentation/Changelog/1.1.1.rst b/Documentation/Changelog/1.1.1.rst index b68b614..0f6411e 100644 --- a/Documentation/Changelog/1.1.1.rst +++ b/Documentation/Changelog/1.1.1.rst @@ -14,7 +14,12 @@ Nothing Fixes ----- -Nothing +* Fix broken month view if last week of December is in next year. + + The cause was using the wrong character in formatting the year. + We now switch from `Y` to `o` which will work based on the week instead of date. + This is necessary as we provide this year to the week, + and therefore need the year of the week, not day. Tasks ----- diff --git a/Tests/Unit/Domain/Model/MonthTest.php b/Tests/Unit/Domain/Model/MonthTest.php index e604516..f214f63 100644 --- a/Tests/Unit/Domain/Model/MonthTest.php +++ b/Tests/Unit/Domain/Model/MonthTest.php @@ -138,6 +138,28 @@ class MonthTest extends TestCase self::assertSame('2021-02-28', $result[27]->getDateTimeInstance()->format('Y-m-d')); } + /** + * @test + */ + public function returnsWeeksIfLastDecemberWeekIsInNextYear(): void + { + $subject = new Month(12, 2024); + + $result = $subject->getWeeks(); + + self::assertCount(6, $result); + + $week = array_pop($result); + $days = $week->getDays(); + self::assertSame('2024-12-30', $days[0]->getDateTimeInstance()->format('Y-m-d')); + self::assertSame('2025-01-05', $days[6]->getDateTimeInstance()->format('Y-m-d')); + + $week = array_pop($result); + $days = $week->getDays(); + self::assertSame('2024-12-23', $days[0]->getDateTimeInstance()->format('Y-m-d')); + self::assertSame('2024-12-29', $days[6]->getDateTimeInstance()->format('Y-m-d')); + } + /** * @test */