From 2705543efdd7bd445cde029255a8717bbebf7ae2 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Fri, 12 Jun 2020 18:19:14 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B02997=20new=20test=20for=20AbstractWeekly?= =?UTF-8?q?ScheduledProcess=20Document=20the=20way=20GetNextOccurrence=20w?= =?UTF-8?q?orks=20O:)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/backgroundprocess.inc.php | 81 ++++++++++++------- .../core/WeeklyScheduledProcessMockConfig.php | 36 +++++++++ test/core/WeeklyScheduledProcessTest.php | 67 +++++++++++++++ 3 files changed, 153 insertions(+), 31 deletions(-) create mode 100644 test/core/WeeklyScheduledProcessMockConfig.php create mode 100644 test/core/WeeklyScheduledProcessTest.php diff --git a/core/backgroundprocess.inc.php b/core/backgroundprocess.inc.php index 8b25c70b3..05ec4572e 100644 --- a/core/backgroundprocess.inc.php +++ b/core/backgroundprocess.inc.php @@ -1,29 +1,23 @@ - - /** - * interface iProcess - * Something that can be executed + * Copyright (C) 2010-2020 Combodo SARL * - * @copyright Copyright (C) 2010-2012 Combodo SARL - * @license http://opensource.org/licenses/AGPL-3.0 + * This file is part of iTop. + * + * iTop is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * iTop is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License */ + + interface iProcess { /** @@ -94,6 +88,11 @@ abstract class AbstractWeeklyScheduledProcess implements iScheduledProcess const DEFAULT_MODULE_SETTING_WEEKDAYS = 'monday, tuesday, wednesday, thursday, friday, saturday, sunday'; const MODULE_SETTING_TIME = 'time'; + /** + * @var Config can be used to mock config for tests + */ + protected $oConfig; + /** * Module must be declared in each implementation * @@ -107,6 +106,20 @@ abstract class AbstractWeeklyScheduledProcess implements iScheduledProcess */ abstract protected function GetDefaultModuleSettingTime(); + /** + * @return \Config + */ + public function getOConfig() + { + if (!isset($this->oConfig)) + { + $this->oConfig = MetaModel::GetConfig(); + } + + return $this->oConfig; + } + + /** * Interpret current setting for the week days * @@ -125,7 +138,7 @@ abstract class AbstractWeeklyScheduledProcess implements iScheduledProcess 'sunday' => 7, ); $aDays = array(); - $sWeekDays = MetaModel::GetConfig()->GetModuleSetting( + $sWeekDays = $this->getOConfig()->GetModuleSetting( $this->GetModuleName(), static::MODULE_SETTING_WEEKDAYS, static::DEFAULT_MODULE_SETTING_WEEKDAYS @@ -158,21 +171,26 @@ abstract class AbstractWeeklyScheduledProcess implements iScheduledProcess } /** - * Gives the exact time at which the process must be run next time + * @param string $sCurrentTime Date string to extract time dependency + * this parameter is not present in the interface but as it is optional it's ok * - * @return DateTime - * @throws Exception + * @return DateTime the exact time at which the process must be run next time + * @throws \ProcessInvalidConfigException */ - public function GetNextOccurrence() + public function GetNextOccurrence($sCurrentTime = 'now') { - $bEnabled = MetaModel::GetConfig()->GetModuleSetting( + $bEnabled = $this->getOConfig()->GetModuleSetting( $this->GetModuleName(), static::MODULE_SETTING_ENABLED, static::DEFAULT_MODULE_SETTING_ENABLED ); + + $sItopTimeZone = $this->getOConfig()->Get('timezone'); + $timezone = new DateTimeZone($sItopTimeZone); + if (!$bEnabled) { - return new DateTime('3000-01-01'); + return new DateTime('3000-01-01', $timezone); } // 1st - Interpret the list of days as ordered numbers (monday = 1) @@ -181,7 +199,7 @@ abstract class AbstractWeeklyScheduledProcess implements iScheduledProcess // 2nd - Find the next active week day // - $sProcessTime = MetaModel::GetConfig()->GetModuleSetting( + $sProcessTime = $this->getOConfig()->GetModuleSetting( $this->GetModuleName(), static::MODULE_SETTING_TIME, static::GetDefaultModuleSettingTime() @@ -190,7 +208,8 @@ abstract class AbstractWeeklyScheduledProcess implements iScheduledProcess { throw new ProcessInvalidConfigException($this->GetModuleName().": wrong format for setting '".static::MODULE_SETTING_TIME."' (found '$sProcessTime')"); } - $oNow = new DateTime(); + + $oNow = new DateTime($sCurrentTime, $timezone); $iNextPos = false; $sDay = $oNow->format('N'); for ($iDay = (int) $sDay; $iDay <= 7; $iDay++) diff --git a/test/core/WeeklyScheduledProcessMockConfig.php b/test/core/WeeklyScheduledProcessMockConfig.php new file mode 100644 index 000000000..236f83797 --- /dev/null +++ b/test/core/WeeklyScheduledProcessMockConfig.php @@ -0,0 +1,36 @@ +oConfig = new Config(); + $this->oConfig->SetModuleSetting(self::MODULE_NAME, self::MODULE_SETTING_ENABLED, $bEnabledValue); + $this->oConfig->SetModuleSetting(self::MODULE_NAME, self::MODULE_SETTING_TIME, $sTimeValue); + $this->oConfig->SetModuleSetting(self::MODULE_NAME, self::MODULE_SETTING_WEEKDAYS, 'monday, tuesday, wednesday, thursday, friday'); + } + + protected function GetModuleName() + { + return self::MODULE_NAME; + } + + protected function GetDefaultModuleSettingTime() + { + return null; // config mock injected in the constructor + } + + public function Process($iUnixTimeLimit) + { + // nothing to do here (not tested) + } +} \ No newline at end of file diff --git a/test/core/WeeklyScheduledProcessTest.php b/test/core/WeeklyScheduledProcessTest.php new file mode 100644 index 000000000..ffd879b64 --- /dev/null +++ b/test/core/WeeklyScheduledProcessTest.php @@ -0,0 +1,67 @@ +getOConfig()->Get('timezone'); + $timezone = new \DateTimeZone($sItopTimeZone); + $oExpectedDateTime = new DateTime($sExpectedTime, $timezone); + + $this->assertEquals($oExpectedDateTime, $oWeeklyImpl->GetNextOccurrence($sCurrentTime)); + } + + public function GetNextOccurrenceProvider() + { + return array( + 'Disabled process' => array(false, 'now', null, '3000-01-01'), + 'working day same day, prog noon and current before noon' => array(true, '2020-05-11 11:00', '12:00', '2020-05-11 12:00'), + 'working day same day, prog noon and current is noon' => array(true, '2020-05-11 12:00', '12:00', '2020-05-12 12:00'), + 'working day same day, prog noon and current after noon' => array(true, '2020-05-11 13:00', '12:00', '2020-05-12 12:00'), + 'saturday, prog noon and current before noon' => array(true, '2020-05-09 11:00', '12:00', '2020-05-11 12:00'), + 'saturday, prog noon and current is noon' => array(true, '2020-05-09 12:00', '12:00', '2020-05-11 12:00'), + 'saturday, prog noon and current after noon' => array(true, '2020-05-09 13:00', '12:00', '2020-05-11 12:00'), + 'sunday, prog noon and current before noon' => array(true, '2020-05-10 11:00', '12:00', '2020-05-11 12:00'), + 'sunday, prog noon and current is noon' => array(true, '2020-05-10 12:00', '12:00', '2020-05-11 12:00'), + 'sunday, prog noon and current after noon' => array(true, '2020-05-10 13:00', '12:00', '2020-05-11 12:00'), + 'working day, day before, prog noon and current before midnight' => array(true, '2020-05-11 23:59', '00:00', '2020-05-12 00:00'), + 'working day same day, prog noon and current is midnight' => array(true, '2020-05-12 00:00', '00:00', '2020-05-13 00:00'), + 'working day same day, prog noon and current after midnight' => array(true, '2020-05-12 00:01', '00:00', '2020-05-13 00:00'), + ); + } +} +