Skip to content

Commit 33de4e7

Browse files
committed
AC-6588: Error during app:config:import -- Import failed
1 parent e0d650c commit 33de4e7

File tree

2 files changed

+93
-16
lines changed

2 files changed

+93
-16
lines changed

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

Lines changed: 23 additions & 5 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
*
@@ -18,7 +20,7 @@
1820
class Cron extends \Magento\Framework\App\Config\Value
1921
{
2022
const CRON_STRING_PATH = 'crontab/default/jobs/currency_rates_update/schedule/cron_expr';
21-
23+
const CONFIG_FIELD = 'path';
2224
/**
2325
* @var \Magento\Framework\App\Config\ValueFactory
2426
*/
@@ -52,13 +54,29 @@ public function __construct(
5254
* After save handler
5355
*
5456
* @return $this
55-
* @throws \Exception
57+
* @throws LocalizedException
5658
*/
5759
public function afterSave()
5860
{
5961
$time = $this->getData('groups/import/fields/time/value');
60-
$frequency = $this->getData('groups/import/fields/frequency/value');
62+
if (empty($time)) {
6163

64+
$time = explode(
65+
',',
66+
$this->_config->getValue(
67+
'currency/import/time',
68+
$this->getScope(),
69+
$this->getScopeId()
70+
) ?: '0,0,0'
71+
);
72+
$frequency = $this->_config->getValue(
73+
'currency/import/frequency',
74+
$this->getScope(),
75+
$this->getScopeId()
76+
);
77+
} else {
78+
$frequency = $this->getData('groups/import/fields/frequency/value');
79+
}
6280
$frequencyWeekly = \Magento\Cron\Model\Config\Source\Frequency::CRON_WEEKLY;
6381
$frequencyMonthly = \Magento\Cron\Model\Config\Source\Frequency::CRON_MONTHLY;
6482

@@ -75,10 +93,10 @@ public function afterSave()
7593
try {
7694
/** @var $configValue \Magento\Framework\App\Config\ValueInterface */
7795
$configValue = $this->_configValueFactory->create();
78-
$configValue->load(self::CRON_STRING_PATH, 'path');
96+
$configValue->load(self::CRON_STRING_PATH, self::CONFIG_FIELD);
7997
$configValue->setValue($cronExprString)->setPath(self::CRON_STRING_PATH)->save();
8098
} catch (\Exception $e) {
81-
throw new \Exception(__('We can\'t save the Cron expression.'));
99+
throw new LocalizedException(__('We can\'t save the Cron expression.'));
82100
}
83101
return parent::afterSave();
84102
}

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

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
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)
@@ -160,7 +161,7 @@ public function testImportStores()
160161
$command = $this->objectManager->create(ConfigImportCommand::class);
161162
$commandTester = new CommandTester($command);
162163

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

165166
/** @var StoreFactory $storeFactory */
166167
$storeFactory = $this->objectManager->get(StoreFactory::class);
@@ -189,7 +190,7 @@ public function testImportStores()
189190
require __DIR__ . '/../../../_files/scopes/config_with_changed_stores.php'
190191
);
191192

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

194195
$store = $storeFactory->create();
195196
$store->getResource()->load($store, 'test', 'code');
@@ -212,7 +213,7 @@ public function testImportStores()
212213
require __DIR__ . '/../../../_files/scopes/config_with_removed_stores.php'
213214
);
214215

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

217218
$group = $groupFactory->create();
218219
$group->getResource()->load($group, 'test_website_store', 'code');
@@ -341,6 +342,64 @@ public function testImportConfig()
341342
$this->assertSame(Cli::RETURN_SUCCESS, $commandTester->getStatusCode());
342343
}
343344

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

0 commit comments

Comments
 (0)