change isWeekend() handling to use work-hour configuration (#4261)

This commit is contained in:
Bruno Paré-Simard
2023-08-31 07:38:58 -04:00
committed by GitHub
parent 18d953e79b
commit 452de88e18
5 changed files with 41 additions and 42 deletions

View File

@@ -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

View File

@@ -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 %}

View File

@@ -3,18 +3,18 @@
{% block report_content %}
{% embed 'reporting/report_by_user_data.html.twig' %}
{% block period_name %}
<th class="text-center text-nowrap{% if column is weekend %} weekend{% endif %}{% if column is today %} today{% endif %}">
<th class="text-center text-nowrap{% if column is weekend(user) %} weekend{% endif %}{% if column is today %} today{% endif %}">
{{ column|date_weekday }}
</th>
{% 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 %}

View File

@@ -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 %}
<th class="text-center text-nowrap{% if column is weekend %} weekend{% endif %}{% if column is today %} today{% endif %}">
<th class="text-center text-nowrap{% if column is today %} today{% endif %}">
{{ column|date_weekday }}
</th>
{% 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 %}

View File

@@ -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));
}