From fe2781093e8f3c0861661b9c55737d615c8741b9 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Fri, 18 Nov 2022 13:47:26 +0000
Subject: [PATCH] fix tests for consultations, fixes #1794

Closes #1794

Merge request studip/studip!1175
---
 tests/jsonapi/ConsultationHelper.php          | 25 +++++++++++++++----
 ...sultationsBookingCreateBySlotIndexTest.php |  4 +--
 .../ConsultationsBookingCreateTest.php        |  4 +--
 3 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/tests/jsonapi/ConsultationHelper.php b/tests/jsonapi/ConsultationHelper.php
index 0fdd40b1b63..10d50502221 100644
--- a/tests/jsonapi/ConsultationHelper.php
+++ b/tests/jsonapi/ConsultationHelper.php
@@ -41,17 +41,32 @@ trait ConsultationHelper
         return User::find($credentials['id']);
     }
 
-    protected function createBlockWithSlotsForRange(Range $range, array $additional_data = []): ConsultationBlock
+    protected function createBlockWithSlotsForRange(Range $range, bool $lock_blocks = false): ConsultationBlock
     {
-        $hour = date('H');
-        $begin = strtotime("today {$hour}:00:00");
-        $end = strtotime('+2 hours', $begin);
+        // Generate start and end time. Assures that the day is not a holiday.
+        $now = time();
 
+        do {
+            $begin = strtotime('next monday 8:00:00', $now);
+            $end = strtotime('+2 hours', $begin);
+
+            $now = strtotime('+1 week', $now);
+
+            $temp = holiday($begin);
+        } while (is_array($temp) && $temp['col'] === 3);
+
+        // Lock blocks?
+        $additional_data = [];
+        if ($lock_blocks) {
+            $additional_data['lock_time'] = ceil(($begin - time()) / 3600);
+        }
+
+        // Generate blocks
         $blocks = ConsultationBlock::generateBlocks(
             $range,
             $begin,
             $end,
-            date('w'),
+            date('w', $begin),
             1
         );
         $blocks = iterator_to_array($blocks);
diff --git a/tests/jsonapi/ConsultationsBookingCreateBySlotIndexTest.php b/tests/jsonapi/ConsultationsBookingCreateBySlotIndexTest.php
index 56ef1838ca7..590343ddd59 100644
--- a/tests/jsonapi/ConsultationsBookingCreateBySlotIndexTest.php
+++ b/tests/jsonapi/ConsultationsBookingCreateBySlotIndexTest.php
@@ -27,12 +27,12 @@ class ConsultationsBookingCreateBySlotIndexTest extends Codeception\Test\Unit
         );
     }
 
-    public function testAutorMayCreateNotCreateBookingDueToLock(): void
+    public function testAutorMayNotCreateBookingDueToLock(): void
     {
         $credentials = $this->tester->getCredentialsForTestDozent();
         $range = User::find($credentials['id']);
 
-        $block = $this->createBlockWithSlotsForRange($range, ['lock_time' => 2]);
+        $block = $this->createBlockWithSlotsForRange($range, true);
         $slot = $this->getSlotFromBlock($block);
 
         $response = $this->createBooking(
diff --git a/tests/jsonapi/ConsultationsBookingCreateTest.php b/tests/jsonapi/ConsultationsBookingCreateTest.php
index 54ad2b4865b..d0af9dad823 100644
--- a/tests/jsonapi/ConsultationsBookingCreateTest.php
+++ b/tests/jsonapi/ConsultationsBookingCreateTest.php
@@ -28,12 +28,12 @@ class ConsultationsBookingCreateTest extends Codeception\Test\Unit
         );
     }
 
-    public function testAutorMayCreateNotCreateBookingDueToLock(): void
+    public function testAutorMayNotCreateBookingDueToLock(): void
     {
         $credentials = $this->tester->getCredentialsForTestDozent();
         $range = User::find($credentials['id']);
 
-        $block = $this->createBlockWithSlotsForRange($range, ['lock_time' => 2]);
+        $block = $this->createBlockWithSlotsForRange($range, true);
         $slot = $this->getSlotFromBlock($block);
 
         $response = $this->createBooking(
-- 
GitLab