Skip to content

Commit d1e7899

Browse files
committed
Merge remote-tracking branch 'falcon/MAGETWO-65422' into falcons-pr-bugfixes
2 parents 3993417 + d0deb08 commit d1e7899

File tree

9 files changed

+270
-13
lines changed

9 files changed

+270
-13
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

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

56+
/**
57+
* The scope type normalizer.
58+
*
59+
* @var ScopeTypeNormalizer
60+
*/
61+
private $scopeTypeNormalizer;
62+
5663
/**
5764
* @param ScopeResolverPool $scopeResolverPool The scope resolver pool
5865
* @param StructureFactory $structureFactory The manager for system configuration structure
5966
* @param BackendFactory $valueFactory The factory for configuration value objects
6067
* @param ScopeConfigInterface $config The scope configuration
68+
* @param ScopeTypeNormalizer $scopeTypeNormalizer The scope type normalizer
6169
*/
6270
public function __construct(
6371
ScopeResolverPool $scopeResolverPool,
6472
StructureFactory $structureFactory,
6573
BackendFactory $valueFactory,
66-
ScopeConfigInterface $config
74+
ScopeConfigInterface $config,
75+
ScopeTypeNormalizer $scopeTypeNormalizer
6776
) {
6877
$this->scopeResolverPool = $scopeResolverPool;
6978
$this->structureFactory = $structureFactory;
7079
$this->valueFactory = $valueFactory;
7180
$this->config = $config;
81+
$this->scopeTypeNormalizer = $scopeTypeNormalizer;
7282
}
7383

7484
/**
@@ -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: 62 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
*/
@@ -95,7 +103,10 @@ protected function setUp()
95103
->getMock();
96104
$this->valueMock = $this->getMockBuilder(Value::class)
97105
->disableOriginalConstructor()
98-
->setMethods(['setPath', 'setScope', 'setScopeId', 'setValue'])
106+
->setMethods([
107+
'setPath', 'setScope', 'setScopeId', 'setValue', 'setField',
108+
'setGroupId', 'setFieldConfig', 'setScopeCode'
109+
])
99110
->getMock();
100111
$this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
101112
->getMockForAbstractClass();
@@ -107,12 +118,16 @@ protected function setUp()
107118
->getMock();
108119
$this->scopeMock = $this->getMockBuilder(ScopeInterface::class)
109120
->getMockForAbstractClass();
121+
$this->scopeTypeNormalizer = $this->getMockBuilder(ScopeTypeNormalizer::class)
122+
->disableOriginalConstructor()
123+
->getMock();
110124

111125
$this->preparedValueFactory = new PreparedValueFactory(
112126
$this->scopeResolverPoolMock,
113127
$this->structureFactoryMock,
114128
$this->valueFactoryMock,
115-
$this->configMock
129+
$this->configMock,
130+
$this->scopeTypeNormalizer
116131
);
117132
}
118133

@@ -133,6 +148,11 @@ public function testCreate(
133148
$scopeCode,
134149
$scopeId
135150
) {
151+
$groupPath = 'some/group';
152+
$groupId = 'some_group';
153+
$fieldId = 'some_field';
154+
$fieldData = ['backend_model' => 'some_model'];
155+
136156
if (ScopeInterface::SCOPE_DEFAULT !== $scope) {
137157
$this->scopeResolverPoolMock->expects($this->once())
138158
->method('get')
@@ -146,19 +166,43 @@ public function testCreate(
146166
->method('getId')
147167
->willReturn($scopeId);
148168
}
169+
/** @var Group|Mock $groupMock */
170+
$groupMock = $this->getMockBuilder(Group::class)
171+
->disableOriginalConstructor()
172+
->getMock();
173+
$groupMock->expects($this->once())
174+
->method('getId')
175+
->willReturn($groupId);
149176

177+
$this->scopeTypeNormalizer->expects($this->once())
178+
->method('normalize')
179+
->with($scope, true)
180+
->willReturnArgument(0);
150181
$this->structureFactoryMock->expects($this->once())
151182
->method('create')
152183
->willReturn($this->structureMock);
153184
$this->structureMock->expects($this->once())
154185
->method('getElementByConfigPath')
155186
->willReturn($this->fieldMock);
187+
$this->structureMock->expects($this->once())
188+
->method('getElement')
189+
->with($groupPath)
190+
->willReturn($groupMock);
156191
$this->fieldMock->expects($this->once())
157192
->method('hasBackendModel')
158193
->willReturn(true);
159194
$this->fieldMock
160195
->method('getConfigPath')
161196
->willReturn($configPath);
197+
$this->fieldMock
198+
->method('getId')
199+
->willReturn($fieldId);
200+
$this->fieldMock
201+
->method('getData')
202+
->willReturn($fieldData);
203+
$this->fieldMock->expects($this->once())
204+
->method('getGroupPath')
205+
->willReturn($groupPath);
162206
$this->valueFactoryMock->expects($this->once())
163207
->method('create')
164208
->willReturn($this->valueMock);
@@ -174,10 +218,26 @@ public function testCreate(
174218
->method('setScopeId')
175219
->with($scopeId)
176220
->willReturnSelf();
221+
$this->valueMock->expects($this->once())
222+
->method('setScopeCode')
223+
->with($scopeCode)
224+
->willReturnSelf();
177225
$this->valueMock->expects($this->once())
178226
->method('setValue')
179227
->with($value)
180228
->willReturnSelf();
229+
$this->valueMock->expects($this->once())
230+
->method('setField')
231+
->with($fieldId)
232+
->willReturnSelf();
233+
$this->valueMock->expects($this->once())
234+
->method('setGroupId')
235+
->with($groupId)
236+
->willReturnSelf();
237+
$this->valueMock->expects($this->once())
238+
->method('setFieldConfig')
239+
->with($fieldData)
240+
->willReturnSelf();
181241

182242
$this->assertInstanceOf(
183243
Value::class,

app/code/Magento/Config/etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@
195195
<virtualType name="appDumpSystemSource" type="Magento\Config\App\Config\Source\DumpConfigSourceAggregated">
196196
<arguments>
197197
<argument name="sources" xsi:type="array">
198+
<item name="modular" xsi:type="array">
199+
<item name="source" xsi:type="object">Magento\Config\App\Config\Source\ModularConfigSource</item>
200+
<item name="sortOrder" xsi:type="string">10</item>
201+
</item>
198202
<item name="dynamic" xsi:type="array">
199203
<item name="source" xsi:type="object">Magento\Config\App\Config\Source\RuntimeConfigSource</item>
200204
<item name="sortOrder" xsi:type="string">100</item>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © 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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © 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+
60+
/**
61+
* @param string $scopeType
62+
* @param string $expectedResult
63+
* @dataProvider normalizeDefaultDataProvider
64+
*/
65+
public function testNormalizeDefault($scopeType, $expectedResult)
66+
{
67+
$this->assertEquals($expectedResult, $this->scopeTypeNormalizer->normalize($scopeType));
68+
}
69+
70+
/**
71+
* @return array
72+
*/
73+
public function normalizeDefaultDataProvider()
74+
{
75+
return [
76+
[ScopeInterface::SCOPE_WEBSITE, ScopeInterface::SCOPE_WEBSITES],
77+
[ScopeInterface::SCOPE_WEBSITES, ScopeInterface::SCOPE_WEBSITES],
78+
[ScopeInterface::SCOPE_GROUP, ScopeInterface::SCOPE_GROUPS],
79+
[ScopeInterface::SCOPE_GROUPS, ScopeInterface::SCOPE_GROUPS],
80+
[ScopeInterface::SCOPE_STORE, ScopeInterface::SCOPE_STORES],
81+
[ScopeInterface::SCOPE_STORES, ScopeInterface::SCOPE_STORES],
82+
['default', 'default'],
83+
];
84+
}
85+
}

dev/tests/integration/testsuite/Magento/Config/Console/Command/ConfigSetCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ function (Mock $output) {
492492
$output->expects($this->once())
493493
->method('writeln')
494494
->with(
495-
'<error>Invalid value. Value must be a URL or one of placeholders: {{base_url}}</error>'
495+
'<error>Invalid Base URL. Value must be a URL or one of placeholders: {{base_url}}</error>'
496496
);
497497
},
498498
'web/unsecure/base_url',

0 commit comments

Comments
 (0)