From 452de88e18713026b0e2717d5f7b7e6664154e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Par=C3=A9-Simard?= Date: Thu, 31 Aug 2023 07:38:58 -0400 Subject: [PATCH] change isWeekend() handling to use work-hour configuration (#4261) --- src/Twig/LocaleFormatExtensions.php | 23 +++------- templates/contract/status.html.twig | 2 +- templates/reporting/report_by_user.html.twig | 8 ++-- .../reporting/report_user_list.html.twig | 4 +- tests/Twig/LocaleFormatExtensionsTest.php | 46 +++++++++++-------- 5 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/Twig/LocaleFormatExtensions.php b/src/Twig/LocaleFormatExtensions.php index 48a95ae4..15e8d0d8 100644 --- a/src/Twig/LocaleFormatExtensions.php +++ b/src/Twig/LocaleFormatExtensions.php @@ -26,7 +26,6 @@ use Twig\TwigTest; final class LocaleFormatExtensions extends AbstractExtension implements LocaleAwareInterface { - private ?bool $fdowSunday = null; private ?LocaleFormatter $formatter = null; private ?string $locale = null; @@ -116,29 +115,21 @@ final class LocaleFormatExtensions extends AbstractExtension implements LocaleAw return $this->locale; } - public function isWeekend(\DateTimeInterface|string|null $dateTime): bool + public function isWeekend(\DateTimeInterface|string|null $dateTime, ?User $user = null): bool { if (!$dateTime instanceof \DateTimeInterface) { return false; } - $day = (int) $dateTime->format('w'); + $day = (int) $dateTime->format('N'); - if ($this->fdowSunday === null) { - /** @var User|null $user */ - $user = $this->security->getUser(); - if ($user !== null) { - $this->fdowSunday = $user->isFirstDayOfWeekSunday(); - } else { - $this->fdowSunday = false; - } + /** @var User|null $tmp */ + $tmp = $user ?? $this->security->getUser(); + if ($tmp !== null && $tmp->hasWorkHourConfiguration()) { + return !$tmp->isWorkDay($dateTime); } - if ($this->fdowSunday) { - return ($day === 5 || $day === 6); - } - - return ($day === 0 || $day === 6); + return ($day === 6 || $day === 7); } public function dateShort(\DateTimeInterface|string|null $date): string diff --git a/templates/contract/status.html.twig b/templates/contract/status.html.twig index 8d47eef0..000a75d9 100644 --- a/templates/contract/status.html.twig +++ b/templates/contract/status.html.twig @@ -213,7 +213,7 @@ {% set dayCount = 0 %} {% for day in month.days %} {% set class = 'text-end contractDay text-nowrap' %} - {% if day.day is weekend %} + {% if day.day is weekend(user) %} {% set class = class ~ ' weekend' %} {% endif %} {% if day.workingTime.expectedTime > 0 and day.workingTime.actualTime == 0 and now > day.day %} diff --git a/templates/reporting/report_by_user.html.twig b/templates/reporting/report_by_user.html.twig index 59cd80cd..9231ca08 100644 --- a/templates/reporting/report_by_user.html.twig +++ b/templates/reporting/report_by_user.html.twig @@ -3,18 +3,18 @@ {% block report_content %} {% embed 'reporting/report_by_user_data.html.twig' %} {% block period_name %} - + {{ column|date_weekday }} {% endblock %} {% block column_classes_project -%} - {% if column.date is weekend %} weekend{% endif %}{% if column.date is today %} today{% endif %} + {% if column.date is weekend(user) %} weekend{% endif %}{% if column.date is today %} today{% endif %} {%- endblock %} {% block column_classes_activity -%} - {% if column.date is weekend %} weekend{% endif %}{% if column.date is today %} today{% endif %} + {% if column.date is weekend(user) %} weekend{% endif %}{% if column.date is today %} today{% endif %} {%- endblock %} {% block column_classes_total -%} - {% if column is weekend %} weekend{% endif %} + {% if column is weekend(user) %} weekend{% endif %} {%- endblock %} {% endembed %} {% endblock %} diff --git a/templates/reporting/report_user_list.html.twig b/templates/reporting/report_user_list.html.twig index 2d35118a..3576715f 100644 --- a/templates/reporting/report_user_list.html.twig +++ b/templates/reporting/report_user_list.html.twig @@ -3,7 +3,7 @@ {% block report_content %} {% embed 'reporting/user_list_period_data.html.twig' with {stats: stats, dataType: dataType, period_attribute: period_attribute, subReportRoute: subReportRoute, subReportDate: subReportDate, decimal: decimal} only %} {% block period_name %} - + {{ column|date_weekday }} {% endblock %} @@ -25,6 +25,6 @@ {% block duration %} {{ period.totalDuration|duration(decimal) }} {% endblock %} - {% block period_cell_class %}{% if period.date is weekend %} weekend{% endif %}{% if period.date is today %} today{% endif %}{% endblock %} + {% block period_cell_class %}{% if period.date is weekend(userPeriod.user) %} weekend{% endif %}{% if period.date is today %} today{% endif %}{% endblock %} {% endembed %} {% endblock %} diff --git a/tests/Twig/LocaleFormatExtensionsTest.php b/tests/Twig/LocaleFormatExtensionsTest.php index 8ba1a20c..c77b11e3 100644 --- a/tests/Twig/LocaleFormatExtensionsTest.php +++ b/tests/Twig/LocaleFormatExtensionsTest.php @@ -12,7 +12,6 @@ namespace App\Tests\Twig; use App\Configuration\LocaleService; use App\Entity\Timesheet; use App\Entity\User; -use App\Entity\UserPreference; use App\Twig\LocaleFormatExtensions; use PHPUnit\Framework\TestCase; use Symfony\Bundle\SecurityBundle\Security; @@ -51,10 +50,9 @@ class LocaleFormatExtensionsTest extends TestCase /** * @param string|array $locale * @param array|string $dateSettings - * @param bool $fdowSunday * @return LocaleFormatExtensions */ - protected function getSut($locale, $dateSettings, $fdowSunday = false) + protected function getSut($locale, $dateSettings, ?User $user = null) { $language = $locale; if (\is_array($locale)) { @@ -62,9 +60,10 @@ class LocaleFormatExtensionsTest extends TestCase $dateSettings = $locale; } - $user = new User(); - $user->setPreferenceValue(UserPreference::FIRST_WEEKDAY, ($fdowSunday ? 'sunday' : 'monday')); - $user->setTimezone('Europe/Vienna'); + if ($user === null) { + $user = new User(); + $user->setTimezone('Europe/Vienna'); + } $security = $this->createMock(Security::class); $security->expects($this->any())->method('getUser')->willReturn($user); @@ -553,7 +552,11 @@ class LocaleFormatExtensionsTest extends TestCase public function testIsWeekend(): void { - $sut = $this->getSut('en', $this->localeEn, false); + $sut = $this->getSut('en', $this->localeEn); + self::assertFalse($sut->isWeekend(null)); + self::assertFalse($sut->isWeekend('2022-01-01')); + + // default case with default user (saturday and sunday is weekend) self::assertFalse($sut->isWeekend(new \DateTime('first monday this month'))); self::assertFalse($sut->isWeekend(new \DateTime('first tuesday this month'))); self::assertFalse($sut->isWeekend(new \DateTime('first wednesday this month'))); @@ -561,17 +564,22 @@ class LocaleFormatExtensionsTest extends TestCase self::assertFalse($sut->isWeekend(new \DateTime('first friday this month'))); self::assertTrue($sut->isWeekend(new \DateTime('first saturday this month'))); self::assertTrue($sut->isWeekend(new \DateTime('first sunday this month'))); - self::assertFalse($sut->isWeekend(null)); - $sut = $this->getSut('en', $this->localeEn, true); - self::assertFalse($sut->isWeekend(new \DateTime('first monday this month'))); - self::assertFalse($sut->isWeekend(new \DateTime('first tuesday this month'))); - self::assertFalse($sut->isWeekend(new \DateTime('first wednesday this month'))); - self::assertFalse($sut->isWeekend(new \DateTime('first thursday this month'))); - self::assertTrue($sut->isWeekend(new \DateTime('first friday this month'))); - self::assertTrue($sut->isWeekend(new \DateTime('first saturday this month'))); - self::assertFalse($sut->isWeekend(new \DateTime('first sunday this month'))); - self::assertFalse($sut->isWeekend(null)); + // seconds case: a user with work-hour configuration + $user = new User(); + $user->setWorkHoursTuesday(1); + $user->setWorkHoursThursday(1); + $user->setWorkHoursSaturday(1); + $user->setWorkHoursSunday(1); + + $sut = $this->getSut('en', $this->localeEn); + self::assertTrue($sut->isWeekend(new \DateTime('first monday this month'), $user)); + self::assertFalse($sut->isWeekend(new \DateTime('first tuesday this month'), $user)); + self::assertTrue($sut->isWeekend(new \DateTime('first wednesday this month'), $user)); + self::assertFalse($sut->isWeekend(new \DateTime('first thursday this month'), $user)); + self::assertTrue($sut->isWeekend(new \DateTime('first friday this month'), $user)); + self::assertFalse($sut->isWeekend(new \DateTime('first saturday this month'), $user)); + self::assertFalse($sut->isWeekend(new \DateTime('first sunday this month'), $user)); } protected function getTimesheet($seconds): Timesheet @@ -589,7 +597,7 @@ class LocaleFormatExtensionsTest extends TestCase public function testChartMoney(): void { - $sut = $this->getSut('en', $this->localeEn, false); + $sut = $this->getSut('en', $this->localeEn); $this->assertEquals('-123456.78', $sut->moneyChart(-123456.78)); $this->assertEquals('123456.78', $sut->moneyChart(123456.78)); $this->assertEquals('123456.00', $sut->moneyChart(123456)); @@ -598,7 +606,7 @@ class LocaleFormatExtensionsTest extends TestCase public function testChartDuration(): void { - $sut = $this->getSut('en', $this->localeEn, false); + $sut = $this->getSut('en', $this->localeEn); $this->assertEquals('34.29', $sut->durationChart(123456)); $this->assertEquals('-34.29', $sut->durationChart(-123456)); }