diff --git a/core/log.class.inc.php b/core/log.class.inc.php index be2b198f0..f34c507f6 100644 --- a/core/log.class.inc.php +++ b/core/log.class.inc.php @@ -331,6 +331,58 @@ class WeeklyRotatingLogFileNameBuilder extends RotatingLogFileNameBuilder } } +/** + * @since 2.7.0 N°2820 + */ +class MonthlyRotatingLogFileNameBuilder extends RotatingLogFileNameBuilder +{ + /** + * @inheritDoc + */ + public function ShouldRotate($oLogFileLastModified, $oNow) + { + $iLogYear = $oLogFileLastModified->format('Y'); + $iLogMonth = $oLogFileLastModified->format('n'); + $iNowYear = $oNow->format('Y'); + $iNowMonth = $oNow->format('n'); + + if ($iLogYear !== $iNowYear) + { + return true; + } + + if ($iLogMonth !== $iNowMonth) + { + return true; + } + + return false; + } + + /** + * @inheritDoc + */ + protected function GetFileSuffix($oDate) + { + $sWeekYear = $oDate->format('o'); + $sWeekNumber = $oDate->format('m'); + + return $sWeekYear.'-month'.$sWeekNumber; + } + + /** + * @inheritDoc + */ + public function GetCronProcessNextOccurrence(DateTime $oNow) + { + $oOccurrence = clone $oNow; + $oOccurrence->modify('first day of next month'); + $oOccurrence->setTime(0, 0, 0); + + return $oOccurrence; + } +} + /** * @since 2.7.0 N°2518 */ diff --git a/test/core/LogFileNameBuilderTest.php b/test/core/LogFileNameBuilderTest.php index d69baa968..00bcef7a7 100644 --- a/test/core/LogFileNameBuilderTest.php +++ b/test/core/LogFileNameBuilderTest.php @@ -6,14 +6,30 @@ namespace Combodo\iTop\Test\UnitTest\Core; use Combodo\iTop\Test\UnitTest\ItopTestCase; use DailyRotatingLogFileNameBuilder; -use DateTime; use WeeklyRotatingLogFileNameBuilder; +use MonthlyRotatingLogFileNameBuilder; +use DateTime; +/** + * @runTestsInSeparateProcesses + * @preserveGlobalState disabled + * @backupGlobals disabled + */ class LogFileNameBuilderTest extends ItopTestCase { + protected function setUp() + { + parent::setUp(); + + require_once APPROOT.'core/log.class.inc.php'; + } + public function ShouldRotateProvider() { return array( + 'DAILY Same day' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-01 15:42', false), + 'DAILY Same week, different day' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-02 00:00', true), + 'DAILY 1 week diff' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-08 00:00', true), 'WEEKLY Same week' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-01 00:00', false), 'WEEKLY 1 week diff, same month' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-08 00:00', true), 'WEEKLY 2 weeks diff, same month' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-15 00:00', true), @@ -21,9 +37,8 @@ class LogFileNameBuilderTest extends ItopTestCase 'WEEKLY same week, different month' => array('WeeklyRotatingLogFileNameBuilder', '2020-01-27 00:00', '2020-02-02 00:00', false), 'WEEKLY 1 week diff, different year' => array('WeeklyRotatingLogFileNameBuilder', '2019-12-30 00:00', '2020-01-06 00:00', true), 'WEEKLY same week, different year' => array('WeeklyRotatingLogFileNameBuilder', '2019-12-30 00:00', '2020-01-05 00:00', true), - 'DAILY Same day' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-01 15:42', false), - 'DAILY Same week, different day' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-02 00:00', true), - 'DAILY 1 week diff' => array('DailyRotatingLogFileNameBuilder', '2020-02-01 00:00', '2020-02-08 00:00', true), + 'MONTHLY same month' => array('MonthlyRotatingLogFileNameBuilder', '2020-02-10 00:00', '2020-02-14 00:00', false), + 'MONTHLY on first day which is a sunday' => array('MonthlyRotatingLogFileNameBuilder', '2020-01-30 00:00', '2020-02-01 00:00', true), ); } @@ -56,6 +71,7 @@ class LogFileNameBuilderTest extends ItopTestCase 'WEEKLY monday 00:00' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-03 00:00', '2020-02-10 00:00'), 'WEEKLY tuesday 12:42' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-04 12:42', '2020-02-10 00:00'), 'WEEKLY sunday 12:42' => array('WeeklyRotatingLogFileNameBuilder', '2020-02-02 12:42', '2020-02-03 00:00'), + 'MONTHLY 12/02 12:42' => array('MonthlyRotatingLogFileNameBuilder', '2020-02-12 12:42', '2020-03-01 00:00'), ); }