From 4ad83853867482c5a14626674e525a21f8b49ada Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 18 Feb 2024 02:18:31 +0100 Subject: [PATCH] isNightShift: Sync with sql query --- includes/model/User_model.php | 7 ++++--- src/Helpers/Shifts.php | 6 ++++-- tests/Unit/Helpers/ShiftsTest.php | 18 ++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 560ab171..e1da6add 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -123,13 +123,14 @@ function User_get_shifts_sum_query() return 'COALESCE(SUM(UNIX_TIMESTAMP(shifts.end) - UNIX_TIMESTAMP(shifts.start)), 0)'; } + /* @see \Engelsystem\Helpers\Shifts::isNightShift to keep it in sync */ return sprintf( ' COALESCE(SUM( (1 + ( - (HOUR(shifts.end) > %1$d AND HOUR(shifts.end) < %2$d) - OR (HOUR(shifts.start) > %1$d AND HOUR(shifts.start) < %2$d) - OR (HOUR(shifts.start) <= %1$d AND HOUR(shifts.end) >= %2$d) + HOUR(shifts.start) > %1$d AND HOUR(shifts.start) < %2$d + OR HOUR(shifts.end) > %1$d AND HOUR(shifts.end) < %2$d + OR HOUR(shifts.start) <= %1$d AND HOUR(shifts.end) >= %2$d )) * (UNIX_TIMESTAMP(shifts.end) - UNIX_TIMESTAMP(shifts.start)) * (1 - (%3$d + 1) * `shift_entries`.`freeloaded`) diff --git a/src/Helpers/Shifts.php b/src/Helpers/Shifts.php index fef2bdab..c75611e1 100644 --- a/src/Helpers/Shifts.php +++ b/src/Helpers/Shifts.php @@ -16,9 +16,11 @@ class Shifts { $config = config('night_shifts'); + /** @see User_get_shifts_sum_query to keep it in sync */ return $config['enabled'] && ( - $start->hour >= $config['start'] && $start->hour < $config['end'] - || $end->hour >= $config['start'] && $end->hour < $config['end'] + $start->hour > $config['start'] && $start->hour < $config['end'] + || $end->hour > $config['start'] && $end->hour < $config['end'] + || $start->hour <= $config['start'] && $end->hour >= $config['end'] ); } diff --git a/tests/Unit/Helpers/ShiftsTest.php b/tests/Unit/Helpers/ShiftsTest.php index f6c00c91..0b4f6756 100644 --- a/tests/Unit/Helpers/ShiftsTest.php +++ b/tests/Unit/Helpers/ShiftsTest.php @@ -32,22 +32,24 @@ class ShiftsTest extends TestCase } /** - * @return array{0: Carbon, 1: Carbon, 2: boolean}[] + * @return array{0: string, 1: string, 2: boolean}[] */ public function nightShiftData(): array { // $start, $end, $isNightShift return [ // Is night shift - [new Carbon('2042-01-01 04:00'), new Carbon('2042-01-01 05:00'), true], + ['2042-01-01 04:00', '2042-01-01 05:00', true], // Starts as night shift - [new Carbon('2042-01-01 05:45'), new Carbon('2042-01-01 07:00'), true], + ['2042-01-01 05:45', '2042-01-01 07:00', true], // Ends as night shift - [new Carbon('2042-01-01 00:00'), new Carbon('2042-01-01 02:15'), true], + ['2042-01-01 00:00', '2042-01-01 03:00', true], + // Contains night shift + ['2042-01-01 01:00', '2042-01-01 09:00', true], // Too early - [new Carbon('2042-01-01 00:00'), new Carbon('2042-01-01 01:59'), false], + ['2042-01-01 00:00', '2042-01-01 02:00', false], // Too late - [new Carbon('2042-01-01 06:00'), new Carbon('2042-01-01 09:59'), false], + ['2042-01-01 06:00', '2042-01-01 10:00', false], ]; } @@ -55,7 +57,7 @@ class ShiftsTest extends TestCase * @covers \Engelsystem\Helpers\Shifts::isNightShift * @dataProvider nightShiftData */ - public function testIsNightShiftEnabled(Carbon $start, Carbon $end, bool $isNightShift): void + public function testIsNightShiftEnabled(string $start, string $end, bool $isNightShift): void { $config = new Config(['night_shifts' => [ 'enabled' => true, @@ -65,7 +67,7 @@ class ShiftsTest extends TestCase ]]); $this->app->instance('config', $config); - $this->assertEquals($isNightShift, Shifts::isNightShift($start, $end)); + $this->assertEquals($isNightShift, Shifts::isNightShift(new Carbon($start), new Carbon($end))); } /**