diff --git a/core/log.class.inc.php b/core/log.class.inc.php index 36314ceca..5c40a1bb8 100644 --- a/core/log.class.inc.php +++ b/core/log.class.inc.php @@ -200,7 +200,7 @@ abstract class RotatingLogFileNameBuilder implements iLogFileNameBuilder * @uses static::$oLogFileLastModified * @uses GetFileSuffix */ - protected function GetRotatedFileName($oLogFileLastModified) + public function GetRotatedFileName($oLogFileLastModified) { $aPathParts = pathinfo($this->sLogFileFullPath); $this->sFilePath = $aPathParts['dirname']; diff --git a/test/core/LogFileNameBuilderTest.php b/test/core/LogFileNameBuilderTest.php index 00bcef7a7..6dc1d91a3 100644 --- a/test/core/LogFileNameBuilderTest.php +++ b/test/core/LogFileNameBuilderTest.php @@ -17,6 +17,18 @@ use DateTime; */ class LogFileNameBuilderTest extends ItopTestCase { + /** + * @param $sLogFile + * @param \DateTime $oNewModificationDate + * + * @uses \clearstatcache() if not called, the next filemtime() call will return the PHP cached date instead of the real one + */ + private function ChangeFileModificationDate($sLogFile, DateTime $oNewModificationDate) + { + touch($sLogFile, $oNewModificationDate->getTimestamp()); + clearstatcache(true, $sLogFile); + } + protected function setUp() { parent::setUp(); @@ -24,6 +36,51 @@ class LogFileNameBuilderTest extends ItopTestCase require_once APPROOT.'core/log.class.inc.php'; } + public function testCheckAndRotateLogFile() + { + $sLogFile = __DIR__.DIRECTORY_SEPARATOR.'fileNameBuilder.test.log'; + $oFileBuilder = new DailyRotatingLogFileNameBuilder($sLogFile); + + if (file_exists($sLogFile)) + { + unlink($sLogFile); + } + + $bIsFileExists = $oFileBuilder->IsLogFileExists(); + $this->assertFalse($bIsFileExists, 'Test log file does not exist'); + + $hLogFile = fopen($sLogFile, 'a'); + $sDate = date('Y-m-d H:i:s'); + $sTestClassName = self::class; + fwrite($hLogFile, "$sDate | This is a line generated by $sTestClassName\n"); + fclose($hLogFile); + $iLogDateLastModifiedTimeStamp = filemtime($sLogFile); + $oLogFileLastModified = DateTime::createFromFormat('U', $iLogDateLastModifiedTimeStamp); + + $sRotatedLogFile = $oFileBuilder->GetRotatedFileName($oLogFileLastModified); + $oFileBuilder->CheckAndRotateLogFile(); + $this->assertFileExists($sLogFile, 'Test log file modification date is today, original file still exists after rotation call'); + $this->assertFileNotExists($sRotatedLogFile, 'No rotation occurred yet'); + + // changing modification date, but do not reset cached filebuilder date => no change + $oTimeYesterday = new DateTime('yesterday'); + touch($sLogFile, $oTimeYesterday->getTimestamp()); + $sRotatedLogFile = $oFileBuilder->GetRotatedFileName($oTimeYesterday); + $oFileBuilder->CheckAndRotateLogFile(); + $this->assertFileExists($sLogFile, 'Test log file modification date is yesterday but filebuilder use its cache, original file still exists after rotation call'); + $this->assertFileNotExists($sRotatedLogFile, 'No rotation occurred yet'); + + // changing modification date AND resetting filebuilder date cache + $oTimeYesterday = new DateTime('yesterday'); + $this->ChangeFileModificationDate($sLogFile, $oTimeYesterday); + $oFileBuilder->ResetLastModifiedDateForFile(); + $sRotatedLogFile = $oFileBuilder->GetRotatedFileName($oTimeYesterday); + $oFileBuilder->CheckAndRotateLogFile(); + $this->assertFileNotExists($sLogFile, 'Test log file modification date is yesterday, file rotated !'); + $this->assertFileExists($sRotatedLogFile, 'Rotation was done'); + unlink($sRotatedLogFile); + } + public function ShouldRotateProvider() { return array(