Skip to content

Commit b648b96

Browse files
author
Mark Berube
committed
MC-11035: Create install script for MC-4239
- Modified install script to additionally migrate data from websites scope - Modified unit tests with new scope migration in mind
1 parent edbf845 commit b648b96

File tree

2 files changed

+112
-18
lines changed

2 files changed

+112
-18
lines changed

app/code/Magento/AuthorizenetAcceptjs/Setup/Patch/Data/CopyCurrentConfig.php

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
1111
use Magento\Framework\App\Config\ScopeConfigInterface;
1212
use Magento\Framework\Encryption\EncryptorInterface;
13-
use Magento\Framework\Setup\InstallDataInterface;
14-
use Magento\Framework\Setup\ModuleContextInterface;
1513
use Magento\Framework\Setup\ModuleDataSetupInterface;
1614
use Magento\Framework\Setup\Patch\DataPatchInterface;
17-
use Magento\Framework\Setup\Patch\PatchInterface;
1815
use Magento\Store\Model\ScopeInterface;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
use Magento\Store\Api\Data\WebsiteInterface;
1918

2019
/**
2120
* Copies the Authorize.net DirectPost configuration values to the new Accept.js module.
@@ -46,6 +45,11 @@ class CopyCurrentConfig implements DataPatchInterface
4645
*/
4746
private $moduleDataSetup;
4847

48+
/**
49+
* @var StoreManagerInterface
50+
*/
51+
private $storeManager;
52+
4953
/**
5054
* @var array
5155
*/
@@ -76,26 +80,40 @@ class CopyCurrentConfig implements DataPatchInterface
7680
* @param ScopeConfigInterface $scopeConfig
7781
* @param ConfigInterface $resourceConfig
7882
* @param EncryptorInterface $encryptor
83+
* @param StoreManagerInterface $storeManager
7984
*/
8085
public function __construct(
8186
ModuleDataSetupInterface $moduleDataSetup,
8287
ScopeConfigInterface $scopeConfig,
8388
ConfigInterface $resourceConfig,
84-
EncryptorInterface $encryptor
89+
EncryptorInterface $encryptor,
90+
StoreManagerInterface $storeManager
8591
) {
8692
$this->scopeConfig = $scopeConfig;
8793
$this->resourceConfig = $resourceConfig;
8894
$this->encryptor = $encryptor;
8995
$this->moduleDataSetup = $moduleDataSetup;
96+
$this->storeManager = $storeManager;
9097
}
9198

9299
/**
93100
* @inheritdoc
94101
*/
95-
public function apply()
102+
public function apply(): void
96103
{
97104
$this->moduleDataSetup->startSetup();
105+
$this->migrateDefaultValues();
106+
$this->migrateWebsiteValues();
107+
$this->moduleDataSetup->endSetup();
108+
}
98109

110+
/**
111+
* Migrate configuration values from DirectPost to Accept.js on default scope
112+
*
113+
* @return void
114+
*/
115+
private function migrateDefaultValues(): void
116+
{
99117
foreach ($this->configFieldsToMigrate as $field) {
100118
$configValue = $this->getOldConfigValue($field);
101119

@@ -108,24 +126,62 @@ public function apply()
108126
$configValue = $this->getOldConfigValue($field);
109127

110128
if (!empty($configValue)) {
111-
$this->saveNewConfigValue($field, $configValue, true);
129+
$this->saveNewConfigValue(
130+
$field,
131+
$configValue,
132+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
133+
0,
134+
true
135+
);
112136
}
113137
}
138+
}
114139

115-
$this->moduleDataSetup->endSetup();
140+
/**
141+
* Migrate configuration values from DirectPost to Accept.js on all website scopes
142+
*
143+
* @return void
144+
*/
145+
private function migrateWebsiteValues(): void
146+
{
147+
foreach ($this->storeManager->getWebsites() as $website) {
148+
$websiteID = (int) $website->getId();
149+
150+
foreach ($this->configFieldsToMigrate as $field) {
151+
$configValue = $this->getOldConfigValue($field, ScopeInterface::SCOPE_WEBSITES, $websiteID);
152+
153+
if (!empty($configValue)) {
154+
$this->saveNewConfigValue($field, $configValue, ScopeInterface::SCOPE_WEBSITES, $websiteID);
155+
}
156+
}
157+
158+
foreach ($this->encryptedConfigFieldsToMigrate as $field) {
159+
$configValue = $this->getOldConfigValue($field, ScopeInterface::SCOPE_WEBSITES, $websiteID);
160+
161+
if (!empty($configValue)) {
162+
$this->saveNewConfigValue($field, $configValue, ScopeInterface::SCOPE_WEBSITES, $websiteID, true);
163+
}
164+
}
165+
}
116166
}
117167

118168
/**
119169
* Get old configuration value from the DirectPost module's configuration on the store scope
120170
*
121171
* @param string $field
172+
* @param string $scope
173+
* @param int $scopeID
122174
* @return mixed
123175
*/
124-
private function getOldConfigValue(string $field)
125-
{
176+
private function getOldConfigValue(
177+
string $field,
178+
string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
179+
int $scopeID = null
180+
) {
126181
return $this->scopeConfig->getValue(
127182
sprintf(self::PAYMENT_PATH_FORMAT, self::DIRECTPOST_PATH, $field),
128-
ScopeInterface::SCOPE_STORE
183+
$scope,
184+
$scopeID
129185
);
130186
}
131187

@@ -134,15 +190,25 @@ private function getOldConfigValue(string $field)
134190
*
135191
* @param string $field
136192
* @param mixed $value
193+
* @param string $scope
194+
* @param int $scopeID
137195
* @param bool $isEncrypted
196+
* @return void
138197
*/
139-
private function saveNewConfigValue(string $field, $value, $isEncrypted = false): void
140-
{
198+
private function saveNewConfigValue(
199+
string $field,
200+
$value,
201+
string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
202+
int $scopeID = 0,
203+
bool $isEncrypted = false
204+
): void {
141205
$value = $isEncrypted ? $this->encryptor->encrypt($value) : $value;
206+
142207
$this->resourceConfig->saveConfig(
143208
sprintf(self::PAYMENT_PATH_FORMAT, self::ACCEPTJS_PATH, $field),
144209
$value,
145-
ScopeInterface::SCOPE_STORE
210+
$scope,
211+
$scopeID
146212
);
147213
}
148214

app/code/Magento/AuthorizenetAcceptjs/Test/Unit/Setup/Patch/Data/CopyCurrentConfigTest.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1616
use Magento\Setup\Module\DataSetup;
1717
use Magento\Setup\Model\ModuleContext;
18+
use Magento\Store\Model\StoreManagerInterface;
19+
use Magento\Store\Model\Website;
1820
use PHPUnit\Framework\TestCase;
1921

2022
class CopyCurrentConfigTest extends TestCase
@@ -44,6 +46,16 @@ class CopyCurrentConfigTest extends TestCase
4446
*/
4547
private $context;
4648

49+
/**
50+
* @var \Magento\Store\Model\StoreManager
51+
*/
52+
private $storeManager;
53+
54+
/**
55+
* @var \Magento\Store\Model\Website
56+
*/
57+
private $website;
58+
4759
protected function setUp(): void
4860
{
4961
$this->scopeConfig = $this->createMock(Config::class);
@@ -60,22 +72,32 @@ protected function setUp(): void
6072
->willReturn(null);
6173

6274
$this->context = $this->createMock(ModuleContext::class);
75+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
76+
$this->website = $this->createMock(Website::class);
6377
}
6478

6579
public function testMigrateData(): void
6680
{
67-
$this->scopeConfig->expects($this->exactly(13))
81+
$this->scopeConfig->expects($this->exactly(26))
6882
->method('getValue')
6983
->willReturn('TestValue');
7084

71-
$this->resourceConfig->expects($this->exactly(13))
85+
$this->resourceConfig->expects($this->exactly(26))
7286
->method('saveConfig')
7387
->willReturn(null);
7488

75-
$this->encryptor->expects($this->exactly(3))
89+
$this->encryptor->expects($this->exactly(6))
7690
->method('encrypt')
7791
->willReturn('TestValue');
7892

93+
$this->website->expects($this->once())
94+
->method('getId')
95+
->willReturn(1);
96+
97+
$this->storeManager->expects($this->once())
98+
->method('getWebsites')
99+
->willReturn([$this->website]);
100+
79101
$objectManager = new ObjectManager($this);
80102

81103
$installer = $objectManager->getObject(
@@ -84,7 +106,8 @@ public function testMigrateData(): void
84106
'moduleDataSetup' => $this->setup,
85107
'scopeConfig' => $this->scopeConfig,
86108
'resourceConfig' => $this->resourceConfig,
87-
'encryptor' => $this->encryptor
109+
'encryptor' => $this->encryptor,
110+
'storeManager' => $this->storeManager
88111
]
89112
);
90113

@@ -104,6 +127,10 @@ public function testMigrateDataNullFields(): void
104127
$this->encryptor->expects($this->never())
105128
->method('encrypt');
106129

130+
$this->storeManager->expects($this->once())
131+
->method('getWebsites')
132+
->willReturn([]);
133+
107134
$objectManager = new ObjectManager($this);
108135

109136
$installer = $objectManager->getObject(
@@ -112,7 +139,8 @@ public function testMigrateDataNullFields(): void
112139
'moduleDataSetup' => $this->setup,
113140
'scopeConfig' => $this->scopeConfig,
114141
'resourceConfig' => $this->resourceConfig,
115-
'encryptor' => $this->encryptor
142+
'encryptor' => $this->encryptor,
143+
'storeManager' => $this->storeManager
116144
]
117145
);
118146

0 commit comments

Comments
 (0)