From a774f597646d67652e731c2e815acd1e1588adb6 Mon Sep 17 00:00:00 2001
From: Andreas Kienast <a.fernandez@scripting-base.de>
Date: Sat, 2 Mar 2024 11:09:08 +0100
Subject: [PATCH] [BUGFIX] Use correct `DateInterval` objects in
 `RotatingFileWriterTest`

The `RotatingFileWriterTest` previously used a timestamp-based
calculation to mock file names. This approach conflicts with the
`DateInterval`-based calculation and might lead to false-negative test
results. This commit updates the test cases by using the interval-based
calculation now.

Resolves: #103243
Releases: main
Change-Id: I9b08b797377eb504af7707acf520fae896b6cbb1
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83177
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../Log/Writer/RotatingFileWriterTest.php     | 56 ++++++++-----------
 1 file changed, 23 insertions(+), 33 deletions(-)

diff --git a/typo3/sysext/core/Tests/Unit/Log/Writer/RotatingFileWriterTest.php b/typo3/sysext/core/Tests/Unit/Log/Writer/RotatingFileWriterTest.php
index 3bf43230c612..4b583076f7d4 100644
--- a/typo3/sysext/core/Tests/Unit/Log/Writer/RotatingFileWriterTest.php
+++ b/typo3/sysext/core/Tests/Unit/Log/Writer/RotatingFileWriterTest.php
@@ -93,11 +93,24 @@ final class RotatingFileWriterTest extends UnitTestCase
         self::assertCount(1, $rotatedFiles);
     }
 
-    #[DataProvider('writingLogWithLatestRotationInTimeFrameDoesNotRotateDataProvider')]
+    public static function intervalDataProvider(): array
+    {
+        return [
+            [Interval::DAILY],
+            [Interval::WEEKLY],
+            [Interval::MONTHLY],
+            [Interval::YEARLY],
+        ];
+    }
+
+    #[DataProvider('intervalDataProvider')]
     #[Test]
-    public function writingLogWithLatestRotationInTimeFrameDoesNotRotate(Interval $interval, int $rotationTimestamp): void
+    public function writingLogWithLatestRotationInTimeFrameDoesNotRotate(Interval $interval): void
     {
-        $rotationDate = (new \DateTimeImmutable('@' . $rotationTimestamp))->format('YmdHis');
+        $testingTolerance = 100;
+        $rotationDate = (new \DateTime('@' . (time() + $testingTolerance)))
+            ->sub(new \DateInterval($interval->getDateInterval()))
+            ->format('YmdHis');
         $logFileName = $this->getDefaultFileName();
 
         file_put_contents($logFileName, 'fooo');
@@ -114,24 +127,15 @@ final class RotatingFileWriterTest extends UnitTestCase
         self::assertCount(1, $rotatedFiles);
     }
 
-    public static function writingLogWithLatestRotationInTimeFrameDoesNotRotateDataProvider(): array
-    {
-        $secondsOfADay = 86400;
-        $tolerance = 100;
-
-        return [
-            [Interval::DAILY, time() - $secondsOfADay + $tolerance],
-            [Interval::WEEKLY, time() - $secondsOfADay * 7 + $tolerance],
-            [Interval::MONTHLY, time() - $secondsOfADay * 30 + $tolerance],
-            [Interval::YEARLY, time() - $secondsOfADay * 365 + $tolerance],
-        ];
-    }
-
-    #[DataProvider('writingLogWithExpiredLatestRotationInTimeFrameRotatesDataProvider')]
+    #[DataProvider('intervalDataProvider')]
     #[Test]
-    public function writingLogWithExpiredLatestRotationInTimeFrameRotates(Interval $interval, int $rotationTimestamp): void
+    public function writingLogWithExpiredLatestRotationInTimeFrameRotates(Interval $interval): void
     {
-        $rotationDate = (new \DateTimeImmutable('@' . $rotationTimestamp))->format('YmdHis');
+        // Helper variable to ensure the next rotation interval kicks in
+        $boost = 100;
+        $rotationDate = (new \DateTime('@' . (time() - $boost)))
+            ->sub(new \DateInterval($interval->getDateInterval()))
+            ->format('YmdHis');
         $logFileName = $this->getDefaultFileName();
 
         file_put_contents($logFileName, 'fooo');
@@ -148,20 +152,6 @@ final class RotatingFileWriterTest extends UnitTestCase
         self::assertCount(2, $rotatedFiles);
     }
 
-    public static function writingLogWithExpiredLatestRotationInTimeFrameRotatesDataProvider(): array
-    {
-        $secondsOfADay = 86400;
-        // Helper variable to ensure the next rotation interval kicks in
-        $boost = 100;
-
-        return [
-            [Interval::DAILY, time() - $secondsOfADay - $boost],
-            [Interval::WEEKLY, time() - $secondsOfADay * 7 - $boost],
-            [Interval::MONTHLY, time() - $secondsOfADay * 31 - $boost], // dumb test, always expect months with 31 days
-            [Interval::YEARLY, time() - $secondsOfADay * 366 - $boost], // dumb test, always expect years with 366 days
-        ];
-    }
-
     #[Test]
     public function rotationRespectsMaxAmountOfFiles(): void
     {
-- 
GitLab