Skip to content

Commit 8bfe610

Browse files
committed
Merge remote-tracking branch 'origin/AC-6588' into Hammer_PlatForm_Health_246_Scope_04102022
2 parents dc8047e + 2f94256 commit 8bfe610

File tree

2 files changed

+93
-17
lines changed

2 files changed

+93
-17
lines changed

app/code/Magento/Config/Model/Config/Backend/Currency/Cron.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace Magento\Config\Model\Config\Backend\Currency;
1111

12+
use Magento\Framework\Exception\LocalizedException;
13+
1214
/**
1315
* Cron job configuration for currency
1416
*
@@ -17,8 +19,7 @@
1719
*/
1820
class Cron extends \Magento\Framework\App\Config\Value
1921
{
20-
const CRON_STRING_PATH = 'crontab/default/jobs/currency_rates_update/schedule/cron_expr';
21-
22+
public const CRON_STRING_PATH = 'crontab/default/jobs/currency_rates_update/schedule/cron_expr';
2223
/**
2324
* @var \Magento\Framework\App\Config\ValueFactory
2425
*/
@@ -52,13 +53,28 @@ public function __construct(
5253
* After save handler
5354
*
5455
* @return $this
55-
* @throws \Exception
56+
* @throws LocalizedException
5657
*/
5758
public function afterSave()
5859
{
5960
$time = $this->getData('groups/import/fields/time/value');
60-
$frequency = $this->getData('groups/import/fields/frequency/value');
61-
61+
if (empty($time)) {
62+
$time = explode(
63+
',',
64+
$this->_config->getValue(
65+
'currency/import/time',
66+
$this->getScope(),
67+
$this->getScopeId()
68+
) ?: '0,0,0'
69+
);
70+
$frequency = $this->_config->getValue(
71+
'currency/import/frequency',
72+
$this->getScope(),
73+
$this->getScopeId()
74+
);
75+
} else {
76+
$frequency = $this->getData('groups/import/fields/frequency/value');
77+
}
6278
$frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY;
6379
$frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY;
6480

@@ -78,7 +94,7 @@ public function afterSave()
7894
$configValue->load(self::CRON_STRING_PATH, 'path');
7995
$configValue->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save();
8096
} catch (\Exception $e) {
81-
throw new \Exception(__('We can\'t save the Cron expression.'));
97+
throw new LocalizedException(__('We can\'t save the Cron expression.'));
8298
}
8399
return parent::afterSave();
84100
}

dev/tests/integration/testsuite/Magento/Deploy/Console/Command/App/ConfigImportCommandTest.php

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@
55
*/
66
namespace Magento\Deploy\Console\Command\App;
77

8+
use Magento\Config\Model\Config\Backend\Currency\Cron;
9+
use Magento\Deploy\Model\DeploymentConfig\Hash;
810
use Magento\Framework\App\CacheInterface;
11+
use Magento\Framework\App\Config\ReinitableConfigInterface;
912
use Magento\Framework\App\DeploymentConfig;
13+
use Magento\Framework\App\Filesystem\DirectoryList;
1014
use Magento\Framework\Config\File\ConfigFilePool;
15+
use Magento\Framework\Console\Cli;
16+
use Magento\Framework\Filesystem;
1117
use Magento\Framework\Flag;
1218
use Magento\Framework\FlagFactory;
19+
use Magento\Framework\ObjectManagerInterface;
1320
use Magento\Store\Model\GroupFactory;
1421
use Magento\Store\Model\StoreFactory;
1522
use Magento\Store\Model\WebsiteFactory;
1623
use Magento\TestFramework\Helper\Bootstrap;
17-
use Magento\Framework\ObjectManagerInterface;
18-
use Magento\Framework\Console\Cli;
19-
use Magento\Framework\Filesystem;
20-
use Magento\Framework\App\Filesystem\DirectoryList;
2124
use Symfony\Component\Console\Tester\CommandTester;
22-
use Magento\Deploy\Model\DeploymentConfig\Hash;
23-
use Magento\Framework\App\Config\ReinitableConfigInterface;
2425

2526
/**
2627
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28+
* phpcs:disable
2729
*/
2830
class ConfigImportCommandTest extends \PHPUnit\Framework\TestCase
2931
{
@@ -160,7 +162,7 @@ public function testImportStores()
160162
$command = $this->objectManager->create(ConfigImportCommand::class);
161163
$commandTester = new CommandTester($command);
162164

163-
$this->runConfigImportCommand($commandTester);
165+
$this->runConfigImportCommand($commandTester, 'Stores were processed');
164166

165167
/** @var StoreFactory $storeFactory */
166168
$storeFactory = $this->objectManager->get(StoreFactory::class);
@@ -189,7 +191,7 @@ public function testImportStores()
189191
require __DIR__ . '/../../../_files/scopes/config_with_changed_stores.php'
190192
);
191193

192-
$this->runConfigImportCommand($commandTester);
194+
$this->runConfigImportCommand($commandTester, 'Stores were processed');
193195

194196
$store = $storeFactory->create();
195197
$store->getResource()->load($store, 'test', 'code');
@@ -212,7 +214,7 @@ public function testImportStores()
212214
require __DIR__ . '/../../../_files/scopes/config_with_removed_stores.php'
213215
);
214216

215-
$this->runConfigImportCommand($commandTester);
217+
$this->runConfigImportCommand($commandTester, 'Stores were processed');
216218

217219
$group = $groupFactory->create();
218220
$group->getResource()->load($group, 'test_website_store', 'code');
@@ -341,6 +343,64 @@ public function testImportConfig()
341343
$this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode());
342344
}
343345

346+
/**
347+
* @magentoDbIsolation disabled
348+
*/
349+
public function testImportCurrencyImportSchedule()
350+
{
351+
$this->assertEmpty($this->hash->get());
352+
353+
$dumpCommand = $this->objectManager->create(ApplicationDumpCommand::class);
354+
$dumpCommandTester = new CommandTester($dumpCommand);
355+
$dumpCommandTester->execute([]);
356+
$dumpedData = $this->reader->load(ConfigFilePool::APP_CONFIG);
357+
358+
$command = $this->objectManager->create(ConfigImportCommand::class);
359+
$commandTester = new CommandTester($command);
360+
361+
/** @var Cron $currencyImportSettings */
362+
$currencyImportSettings = $this->objectManager->get(Cron::class);
363+
/** @var $valueFactory \Magento\Framework\App\Config\ValueFactory */
364+
$reflectionProperty = new \ReflectionProperty(
365+
$currencyImportSettings,
366+
'_configValueFactory'
367+
);
368+
$reflectionProperty->setAccessible(true);
369+
$valueFactory = $reflectionProperty->getValue($currencyImportSettings);
370+
/** @var $configValue \Magento\Framework\App\Config\ValueInterface */
371+
$configValue = $valueFactory->create();
372+
373+
$hour = rand(0, 23);
374+
$min = rand(0, 59);
375+
$data = [
376+
'system' => [
377+
'default' => [
378+
'currency' => [
379+
'import' => [
380+
'enabled' => '1',
381+
'service' => 'fixerio',
382+
'time' => sprintf("%02d", $hour) . ',' . sprintf("%02d", $min) . ',00',
383+
'frequency' => 'D',
384+
'error_email_identity' => 'general',
385+
'error_email_template' => 'currency_import_error_email_template',
386+
],
387+
],
388+
],
389+
]
390+
];
391+
392+
$this->writeConfig(
393+
$dumpedData,
394+
$data
395+
);
396+
$this->runConfigImportCommand($commandTester, 'System config was processed');
397+
398+
$configValue->load(Cron::CRON_STRING_PATH, 'path');
399+
$configValue->getFieldsetDataValue('path');
400+
$time = $configValue->getValue();
401+
$this->assertSame($time, "$min $hour * * *");
402+
}
403+
344404
/**
345405
* Saves new data.
346406
*
@@ -393,7 +453,7 @@ private function loadEnvConfig()
393453
*
394454
* @param $commandTester
395455
*/
396-
private function runConfigImportCommand($commandTester)
456+
private function runConfigImportCommand($commandTester, $assertPartialString)
397457
{
398458
$this->appConfig->reinit();
399459
$commandTester->execute([], ['interactive' => false]);
@@ -402,7 +462,7 @@ private function runConfigImportCommand($commandTester)
402462
'Processing configurations data from configuration file...',
403463
$commandTester->getDisplay()
404464
);
405-
$this->assertStringContainsString('Stores were processed', $commandTester->getDisplay());
465+
$this->assertStringContainsString($assertPartialString, $commandTester->getDisplay());
406466
$this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode());
407467
}
408468
}

0 commit comments

Comments
 (0)