Skip to content

Commit 3d282f2

Browse files
author
Bohdan Korablov
committed
MAGETWO-65422: Write default configs to shared configuration file by app:config:dump
1 parent 0b89455 commit 3d282f2

File tree

4 files changed

+210
-11
lines changed

4 files changed

+210
-11
lines changed

app/code/Magento/Config/Model/PreparedValueFactory.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Framework\App\Config\ValueInterface;
1313
use Magento\Framework\App\Config\Value;
1414
use Magento\Framework\App\ScopeInterface;
15-
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
15+
use Magento\Store\Model\ScopeTypeNormalizer;
1616
use Magento\Framework\App\ScopeResolverPool;
1717
use Magento\Framework\Exception\RuntimeException;
1818

@@ -52,22 +52,32 @@ class PreparedValueFactory
5252
*/
5353
private $config;
5454

55+
/**
56+
* The scope type normalizer.
57+
*
58+
* @var ScopeTypeNormalizer
59+
*/
60+
private $scopeTypeNormalizer;
61+
5562
/**
5663
* @param ScopeResolverPool $scopeResolverPool The scope resolver pool
5764
* @param StructureFactory $structureFactory The manager for system configuration structure
5865
* @param BackendFactory $valueFactory The factory for configuration value objects
5966
* @param ScopeConfigInterface $config The scope configuration
67+
* @param ScopeTypeNormalizer $scopeTypeNormalizer The scope type normalizer
6068
*/
6169
public function __construct(
6270
ScopeResolverPool $scopeResolverPool,
6371
StructureFactory $structureFactory,
6472
BackendFactory $valueFactory,
65-
ScopeConfigInterface $config
73+
ScopeConfigInterface $config,
74+
ScopeTypeNormalizer $scopeTypeNormalizer
6675
) {
6776
$this->scopeResolverPool = $scopeResolverPool;
6877
$this->structureFactory = $structureFactory;
6978
$this->valueFactory = $valueFactory;
7079
$this->config = $config;
80+
$this->scopeTypeNormalizer = $scopeTypeNormalizer;
7181
}
7282

7383
/**
@@ -106,20 +116,26 @@ public function create($path, $value, $scope, $scopeCode = null)
106116
if ($backendModel instanceof Value) {
107117
$scopeId = 0;
108118

109-
if (in_array($scope, [StoreScopeInterface::SCOPE_WEBSITE, StoreScopeInterface::SCOPE_WEBSITES])) {
110-
$scope = StoreScopeInterface::SCOPE_WEBSITES;
111-
} elseif (in_array($scope, [StoreScopeInterface::SCOPE_STORE, StoreScopeInterface::SCOPE_STORES])) {
112-
$scope = StoreScopeInterface::SCOPE_STORES;
113-
}
119+
$scope = $this->scopeTypeNormalizer->normalize($scope);
114120

115121
if ($scope !== ScopeInterface::SCOPE_DEFAULT) {
116-
$scopeResolver = $this->scopeResolverPool->get($scope);
117-
$scopeId = $scopeResolver->getScope($scopeCode)->getId();
122+
$scopeId = $this->scopeResolverPool->get($scope)
123+
->getScope($scopeCode)
124+
->getId();
125+
}
126+
127+
if ($field instanceof Structure\Element\Field) {
128+
$groupPath = $field->getGroupPath();
129+
$group = $structure->getElement($groupPath);
130+
$backendModel->setField($field->getId());
131+
$backendModel->setGroupId($group->getId());
132+
$backendModel->setFieldConfig($field->getData());
118133
}
119134

120135
$backendModel->setPath($configPath);
121136
$backendModel->setScope($scope);
122137
$backendModel->setScopeId($scopeId);
138+
$backendModel->setScopeCode($scopeCode);
123139
$backendModel->setValue($value);
124140
}
125141

app/code/Magento/Config/Test/Unit/Model/PreparedValueFactoryTest.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
use Magento\Framework\App\Config\Value;
1313
use Magento\Config\Model\Config\Structure;
1414
use Magento\Config\Model\Config\Structure\Element\Field;
15+
use Magento\Config\Model\Config\Structure\Element\Group;
1516
use Magento\Framework\App\ScopeInterface;
1617
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
1718
use Magento\Framework\App\ScopeResolver;
1819
use Magento\Framework\App\ScopeResolverPool;
20+
use Magento\Store\Model\ScopeTypeNormalizer;
1921
use PHPUnit_Framework_MockObject_MockObject as Mock;
2022

2123
/**
2224
* @inheritdoc
2325
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2427
*/
2528
class PreparedValueFactoryTest extends \PHPUnit_Framework_TestCase
2629
{
@@ -69,6 +72,11 @@ class PreparedValueFactoryTest extends \PHPUnit_Framework_TestCase
6972
*/
7073
private $scopeMock;
7174

75+
/**
76+
* @var ScopeTypeNormalizer|Mock
77+
*/
78+
private $scopeTypeNormalizer;
79+
7280
/**
7381
* @var PreparedValueFactory
7482
*/
@@ -91,11 +99,15 @@ protected function setUp()
9199
->disableOriginalConstructor()
92100
->getMock();
93101
$this->fieldMock = $this->getMockBuilder(Field::class)
102+
//->setMethods(['getData', 'getConfigPath', 'getId', 'hasBackendModel'])
94103
->disableOriginalConstructor()
95104
->getMock();
96105
$this->valueMock = $this->getMockBuilder(Value::class)
97106
->disableOriginalConstructor()
98-
->setMethods(['setPath', 'setScope', 'setScopeId', 'setValue'])
107+
->setMethods([
108+
'setPath', 'setScope', 'setScopeId', 'setValue', 'setField',
109+
'setGroupId', 'setFieldConfig', 'setScopeCode'
110+
])
99111
->getMock();
100112
$this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
101113
->getMockForAbstractClass();
@@ -107,12 +119,16 @@ protected function setUp()
107119
->getMock();
108120
$this->scopeMock = $this->getMockBuilder(ScopeInterface::class)
109121
->getMockForAbstractClass();
122+
$this->scopeTypeNormalizer = $this->getMockBuilder(ScopeTypeNormalizer::class)
123+
->disableOriginalConstructor()
124+
->getMock();
110125

111126
$this->preparedValueFactory = new PreparedValueFactory(
112127
$this->scopeResolverPoolMock,
113128
$this->structureFactoryMock,
114129
$this->valueFactoryMock,
115-
$this->configMock
130+
$this->configMock,
131+
$this->scopeTypeNormalizer
116132
);
117133
}
118134

@@ -133,6 +149,11 @@ public function testCreate(
133149
$scopeCode,
134150
$scopeId
135151
) {
152+
$groupPath = 'some/group';
153+
$groupId = 'some_group';
154+
$fieldId = 'some_field';
155+
$fieldData = ['backend_model' => 'some_model'];
156+
136157
if (ScopeInterface::SCOPE_DEFAULT !== $scope) {
137158
$this->scopeResolverPoolMock->expects($this->once())
138159
->method('get')
@@ -146,19 +167,43 @@ public function testCreate(
146167
->method('getId')
147168
->willReturn($scopeId);
148169
}
170+
/** @var Group|Mock $groupMock */
171+
$groupMock = $this->getMockBuilder(Group::class)
172+
->disableOriginalConstructor()
173+
->getMock();
174+
$groupMock->expects($this->once())
175+
->method('getId')
176+
->willReturn($groupId);
149177

178+
$this->scopeTypeNormalizer->expects($this->once())
179+
->method('normalize')
180+
->with($scope, true)
181+
->willReturnArgument(0);
150182
$this->structureFactoryMock->expects($this->once())
151183
->method('create')
152184
->willReturn($this->structureMock);
153185
$this->structureMock->expects($this->once())
154186
->method('getElementByConfigPath')
155187
->willReturn($this->fieldMock);
188+
$this->structureMock->expects($this->once())
189+
->method('getElement')
190+
->with($groupPath)
191+
->willReturn($groupMock);
156192
$this->fieldMock->expects($this->once())
157193
->method('hasBackendModel')
158194
->willReturn(true);
159195
$this->fieldMock
160196
->method('getConfigPath')
161197
->willReturn($configPath);
198+
$this->fieldMock
199+
->method('getId')
200+
->willReturn($fieldId);
201+
$this->fieldMock
202+
->method('getData')
203+
->willReturn($fieldData);
204+
$this->fieldMock->expects($this->once())
205+
->method('getGroupPath')
206+
->willReturn($groupPath);
162207
$this->valueFactoryMock->expects($this->once())
163208
->method('create')
164209
->willReturn($this->valueMock);
@@ -174,10 +219,26 @@ public function testCreate(
174219
->method('setScopeId')
175220
->with($scopeId)
176221
->willReturnSelf();
222+
$this->valueMock->expects($this->once())
223+
->method('setScopeCode')
224+
->with($scopeCode)
225+
->willReturnSelf();
177226
$this->valueMock->expects($this->once())
178227
->method('setValue')
179228
->with($value)
180229
->willReturnSelf();
230+
$this->valueMock->expects($this->once())
231+
->method('setField')
232+
->with($fieldId)
233+
->willReturnSelf();
234+
$this->valueMock->expects($this->once())
235+
->method('setGroupId')
236+
->with($groupId)
237+
->willReturnSelf();
238+
$this->valueMock->expects($this->once())
239+
->method('setFieldConfig')
240+
->with($fieldData)
241+
->willReturnSelf();
181242

182243
$this->assertInstanceOf(
183244
Value::class,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Store\Model;
7+
8+
/**
9+
* Normalizes scope types to the chosen form, plural or singular.
10+
*/
11+
class ScopeTypeNormalizer
12+
{
13+
/**
14+
* Normalizes scope types.
15+
*
16+
* We have a few forms of scope types in the plural and singular:
17+
* websites, website, groups, group, stores, store
18+
*
19+
* This method returns scope type in the chosen form (plural or singular).
20+
*
21+
* For example, the next calls of this method returns 'website':
22+
* ```php
23+
* $this->normalize('websites', false);
24+
* $this->normalize('website', false);
25+
* ```
26+
*
27+
* This calls of this method returns 'websites':
28+
* ```php
29+
* $this->normalize('website', false);
30+
* $this->normalize('websites', false);
31+
* $this->normalize('website');
32+
* $this->normalize('websites');
33+
* ```
34+
*
35+
* If there is not scope in the list (websites, website, groups, group, stores, store)
36+
* then it will be returned without changes.
37+
*
38+
* The next calls of this method returns 'default':
39+
* ```php
40+
* $this->normalize('default', false);
41+
* $this->normalize('default', true);
42+
* $this->normalize('default');
43+
* ```
44+
*
45+
* @param string $scopeType The type of scope
46+
* @param bool $plural The flag for choosing returned form of scope, in the plural or not. Used plural by default.
47+
* @return string
48+
*/
49+
public function normalize($scopeType, $plural = true)
50+
{
51+
$replaces = [
52+
ScopeInterface::SCOPE_WEBSITE => ScopeInterface::SCOPE_WEBSITES,
53+
ScopeInterface::SCOPE_GROUP => ScopeInterface::SCOPE_GROUPS,
54+
ScopeInterface::SCOPE_STORE => ScopeInterface::SCOPE_STORES,
55+
];
56+
57+
if (!$plural) {
58+
$replaces = array_flip($replaces);
59+
}
60+
61+
return $replaces[$scopeType] ?? $scopeType;
62+
}
63+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Store\Test\Unit\Model;
7+
8+
use Magento\Store\Model\ScopeTypeNormalizer;
9+
use Magento\Store\Model\ScopeInterface;
10+
11+
class ScopeTypeNormalizerTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var ScopeTypeNormalizer
15+
*/
16+
private $scopeTypeNormalizer;
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
protected function setUp()
22+
{
23+
$this->scopeTypeNormalizer = new ScopeTypeNormalizer();
24+
}
25+
26+
/**
27+
* @param string $scopeType
28+
* @param bool $plural
29+
* @param string $expectedResult
30+
* @dataProvider normalizeDataProvider
31+
*/
32+
public function testNormalize($scopeType, $plural, $expectedResult)
33+
{
34+
$this->assertEquals($expectedResult, $this->scopeTypeNormalizer->normalize($scopeType, $plural));
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function normalizeDataProvider()
41+
{
42+
return [
43+
[ScopeInterface::SCOPE_WEBSITE, true, ScopeInterface::SCOPE_WEBSITES],
44+
[ScopeInterface::SCOPE_WEBSITES, true, ScopeInterface::SCOPE_WEBSITES],
45+
[ScopeInterface::SCOPE_WEBSITE, false, ScopeInterface::SCOPE_WEBSITE],
46+
[ScopeInterface::SCOPE_WEBSITES, false, ScopeInterface::SCOPE_WEBSITE],
47+
[ScopeInterface::SCOPE_GROUP, true, ScopeInterface::SCOPE_GROUPS],
48+
[ScopeInterface::SCOPE_GROUPS, true, ScopeInterface::SCOPE_GROUPS],
49+
[ScopeInterface::SCOPE_GROUP, false, ScopeInterface::SCOPE_GROUP],
50+
[ScopeInterface::SCOPE_GROUPS, false, ScopeInterface::SCOPE_GROUP],
51+
[ScopeInterface::SCOPE_STORE, true, ScopeInterface::SCOPE_STORES],
52+
[ScopeInterface::SCOPE_STORES, true, ScopeInterface::SCOPE_STORES],
53+
[ScopeInterface::SCOPE_STORE, false, ScopeInterface::SCOPE_STORE],
54+
[ScopeInterface::SCOPE_STORES, false, ScopeInterface::SCOPE_STORE],
55+
['default', true, 'default'],
56+
['default', false, 'default'],
57+
];
58+
}
59+
}

0 commit comments

Comments
 (0)