diff --git a/app/code/Magento/Cron/Model/Config/Backend/Product/Alert.php b/app/code/Magento/Cron/Model/Config/Backend/Product/Alert.php index eeef291fb6ad1..0039e629b735c 100644 --- a/app/code/Magento/Cron/Model/Config/Backend/Product/Alert.php +++ b/app/code/Magento/Cron/Model/Config/Backend/Product/Alert.php @@ -17,12 +17,12 @@ class Alert extends \Magento\Framework\App\Config\Value { /** - * Cron string path + * Cron expression config path */ const CRON_STRING_PATH = 'crontab/default/jobs/catalog_product_alert/schedule/cron_expr'; /** - * Cron model path + * Cron model config path */ const CRON_MODEL_PATH = 'crontab/default/jobs/catalog_product_alert/run/model'; @@ -71,8 +71,16 @@ public function __construct( */ public function afterSave() { - $time = $this->getData('groups/productalert_cron/fields/time/value'); - $frequency = $this->getData('groups/productalert_cron/fields/frequency/value'); + $time = $this->getData('groups/productalert_cron/fields/time/value') ?: + explode( + ',', + $this->_config->getValue( + 'catalog/productalert_cron/time', + $this->getScope(), + $this->getScopeId() + ) ?: '0,0,0' + ); + $frequency = $this->getValue(); $cronExprArray = [ (int)$time[1], //Minute @@ -102,6 +110,7 @@ public function afterSave() self::CRON_MODEL_PATH )->save(); } catch (\Exception $e) { + // phpcs:ignore Magento2.Exceptions.DirectThrow throw new \Exception(__('We can\'t save the cron expression.')); } diff --git a/dev/tests/integration/testsuite/Magento/Cron/Model/Config/Backend/Product/AlertTest.php b/dev/tests/integration/testsuite/Magento/Cron/Model/Config/Backend/Product/AlertTest.php new file mode 100644 index 0000000000000..31d3506c1e6d7 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Cron/Model/Config/Backend/Product/AlertTest.php @@ -0,0 +1,98 @@ +objectManager = Bootstrap::getObjectManager(); + } + + /** + * @dataProvider frequencyDataProvider + * @param string $frequency + * @param string $expectedCronExpr + */ + public function testDirectSave(string $frequency, string $expectedCronExpr): void + { + $preparedValueFactory = $this->objectManager->get(PreparedValueFactory::class); + /** @var Alert $sitemapValue */ + $alertValue = $preparedValueFactory->create('catalog/productalert_cron/frequency', $frequency, 'default', 0); + $alertValue->save(); + + self::assertEquals($expectedCronExpr, $this->getCronExpression()); + } + + /** + * @dataProvider frequencyDataProvider + * @param string $frequency + * @param string $expectedCronExpr + */ + public function testSaveFromAdmin(string $frequency, string $expectedCronExpr): void + { + $config = $this->objectManager->create(ConfigModel::class); + $config->setSection('catalog'); + $config->setGroups( + [ + 'productalert_cron' => [ + 'fields' => [ + 'time' => ['value' => ['00', '00', '00']], + 'frequency' => ['value' => $frequency], + ], + ], + ] + ); + $config->save(); + + self::assertEquals($expectedCronExpr, $this->getCronExpression()); + } + + /** + * @return array + */ + public function frequencyDataProvider(): array + { + return [ + 'daily' => [Frequency::CRON_DAILY, '0 0 * * *'], + 'weekly' => [Frequency::CRON_WEEKLY, '0 0 * * 1'], + 'monthly' => [Frequency::CRON_MONTHLY, '0 0 1 * *'], + ]; + } + + /** + * @return string + */ + private function getCronExpression(): string + { + $valueFactory = $this->objectManager->get(ValueFactory::class); + $cronExprValue = $valueFactory->create() + ->load(Alert::CRON_STRING_PATH, 'path'); + + return $cronExprValue->getValue(); + } +}