Skip to content

Commit

Permalink
Merge remote-tracking branch 'magento-l3/ACP2E-215' into L3_PR_21-09-30
Browse files Browse the repository at this point in the history
  • Loading branch information
veloraven committed Oct 12, 2021
2 parents 8b1152a + 70848ea commit 7719519
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/code/Magento/Cron/Model/Config/Backend/Product/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.'));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Cron\Model\Config\Backend\Product;

use Magento\Config\Model\Config as ConfigModel;
use Magento\Config\Model\PreparedValueFactory;
use Magento\Cron\Model\Config\Source\Frequency;
use Magento\Framework\App\Config\ValueFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* @magentoAppArea adminhtml
*/
class AlertTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->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();
}
}

0 comments on commit 7719519

Please sign in to comment.