diff --git a/core/log.class.inc.php b/core/log.class.inc.php index a3d00390c..df92d2ae0 100644 --- a/core/log.class.inc.php +++ b/core/log.class.inc.php @@ -854,10 +854,32 @@ class DeprecatedCallsLog extends LogAPI /** @var \FileLog we want our own instance ! */ protected static $m_oFileLog = null; + /** + * Indirection to {@see \LogAPI::IsLogLevelEnabled()} that is handling possible {@see ConfigException} + * + * @param string $sLevel + * @param string $sChannel + * + * @return bool if exception occurs, then returns false + * + * @uses \LogAPI::IsLogLevelEnabled() + */ + protected static function IsLogLevelEnabledSafe($sLevel, $sChannel): bool + { + try { + $bIsLogLevelEnabled = static::IsLogLevelEnabled(self::LEVEL_WARNING, self::ENUM_CHANNEL_PHP_LIBMETHOD); + } + catch (ConfigException $e) { + $bIsLogLevelEnabled = false; + } + + return $bIsLogLevelEnabled; + } + /** * @param string|null $sTargetFile * - *@uses \set_error_handler() to catch deprecated notices + * @uses \set_error_handler() to catch deprecated notices * * @since 3.0.0 N°3002 logs deprecated notices in called code */ @@ -868,13 +890,7 @@ class DeprecatedCallsLog extends LogAPI } parent::Enable($sTargetFile); - try { - $bIsLogLevelEnabled = static::IsLogLevelEnabled(self::LEVEL_WARNING, self::ENUM_CHANNEL_PHP_LIBMETHOD); - } - catch (ConfigException $e) { - $bIsLogLevelEnabled = false; - } - if ($bIsLogLevelEnabled) { + if (static::IsLogLevelEnabledSafe(self::LEVEL_WARNING, self::ENUM_CHANNEL_PHP_LIBMETHOD)) { set_error_handler([static::class, 'DeprecatedNoticesErrorHandler']); } } @@ -901,6 +917,11 @@ class DeprecatedCallsLog extends LogAPI return false; } + if (false === static::IsLogLevelEnabledSafe(self::LEVEL_WARNING, self::ENUM_CHANNEL_PHP_LIBMETHOD)) { + // returns true so that nothing is throwned ! + return true; + } + $aStack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4); $iStackDeprecatedMethodLevel = 2; // level 0 = current method, level 1 = @trigger_error, level 2 = method containing the `trigger_error` call $sDeprecatedObject = $aStack[$iStackDeprecatedMethodLevel]['class']; diff --git a/test/core/Log/ExceptionLogTest.php b/test/core/Log/ExceptionLogTest.php index b481f4b16..dc1c5e242 100644 --- a/test/core/Log/ExceptionLogTest.php +++ b/test/core/Log/ExceptionLogTest.php @@ -16,19 +16,34 @@ namespace Combodo\iTop\Test\UnitTest\Core\Log; use Combodo\iTop\Test\UnitTest\ItopDataTestCase; +use DeprecatedCallsLog; use ExceptionLog; +use LogAPI; +use MetaModel; -require_once (__DIR__.'/ExceptionLogTest/Exceptions.php'); +require_once(__DIR__.'/ExceptionLogTest/Exceptions.php'); + class ExceptionLogTest extends ItopDataTestCase { protected function setUp() { - require_once (__DIR__.'/ExceptionLogTest/Exceptions.php'); + require_once(__DIR__.'/ExceptionLogTest/Exceptions.php'); parent::setUp(); - $oConfig = \MetaModel::GetConfig(); - $oConfig->Set('developer_mode.enabled', false); + // We are using PHPUnit\Framework\MockObject\Generator::generateMock that is throwing notice ! + // Changing the log config so that those won't be caught by \DeprecatedCallsLog::DeprecatedNoticesErrorHandler + $oConfig = MetaModel::GetConfig(); + $mLogLevelMin = $oConfig->Get('log_level_min'); + if (is_string($mLogLevelMin)) { + $aLogLevelMin[''] = $mLogLevelMin; + } else if (is_array($mLogLevelMin)) { + $aLogLevelMin = $mLogLevelMin; + } else { + $aLogLevelMin = []; + } + $aLogLevelMin[DeprecatedCallsLog::ENUM_CHANNEL_PHP_LIBMETHOD] = LogAPI::LEVEL_ERROR; + $oConfig->Set('log_level_min', $aLogLevelMin); } /**