Skip to content

Commit 63e2850

Browse files
committed
MAGETWO-97247: Impossible to enable shared catalog through cli command
1 parent 069fa71 commit 63e2850

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

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

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\App\ScopeInterface;
1313
use Magento\Framework\App\ScopeResolverPool;
1414
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
15+
use Magento\Store\Model\ScopeTypeNormalizer;
1516

1617
/**
1718
* Backend config model
@@ -108,6 +109,11 @@ class Config extends \Magento\Framework\DataObject
108109
*/
109110
private $scopeResolverPool;
110111

112+
/**
113+
* @var ScopeTypeNormalizer
114+
*/
115+
private $scopeTypeNormalizer;
116+
111117
/**
112118
* @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
113119
* @param \Magento\Framework\Event\ManagerInterface $eventManager
@@ -119,6 +125,7 @@ class Config extends \Magento\Framework\DataObject
119125
* @param Config\Reader\Source\Deployed\SettingChecker|null $settingChecker
120126
* @param array $data
121127
* @param ScopeResolverPool|null $scopeResolverPool
128+
* @param ScopeTypeNormalizer|null $scopeTypeNormalizer
122129
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
123130
*/
124131
public function __construct(
@@ -131,7 +138,8 @@ public function __construct(
131138
\Magento\Store\Model\StoreManagerInterface $storeManager,
132139
SettingChecker $settingChecker = null,
133140
array $data = [],
134-
ScopeResolverPool $scopeResolverPool = null
141+
ScopeResolverPool $scopeResolverPool = null,
142+
ScopeTypeNormalizer $scopeTypeNormalizer = null
135143
) {
136144
parent::__construct($data);
137145
$this->_eventManager = $eventManager;
@@ -141,8 +149,12 @@ public function __construct(
141149
$this->_configLoader = $configLoader;
142150
$this->_configValueFactory = $configValueFactory;
143151
$this->_storeManager = $storeManager;
144-
$this->settingChecker = $settingChecker ?? ObjectManager::getInstance()->get(SettingChecker::class);
145-
$this->scopeResolverPool = $scopeResolverPool ?? ObjectManager::getInstance()->get(ScopeResolverPool::class);
152+
$this->settingChecker = $settingChecker
153+
?? ObjectManager::getInstance()->get(SettingChecker::class);
154+
$this->scopeResolverPool = $scopeResolverPool
155+
?? ObjectManager::getInstance()->get(ScopeResolverPool::class);
156+
$this->scopeTypeNormalizer = $scopeTypeNormalizer
157+
?? ObjectManager::getInstance()->get(ScopeTypeNormalizer::class);
146158
}
147159

148160
/**
@@ -539,53 +551,34 @@ private function initScope()
539551
if ($this->getSection() === null) {
540552
$this->setSection('');
541553
}
542-
if ($this->getWebsite() === null) {
543-
$this->setWebsite('');
544-
}
545-
if ($this->getStore() === null) {
546-
$this->setStore('');
547-
}
548554

549-
if ($this->getScope()) {
550-
$scope = $this->scopeResolverPool->get($this->getScope())
551-
->getScope($this->getScopeCode());
552-
if (StoreScopeInterface::SCOPE_WEBSITE === $scope->getScopeType()) {
553-
$this->setWebsite($scope->getId());
554-
} elseif (StoreScopeInterface::SCOPE_STORE === $scope->getScopeType()) {
555-
$this->setStore($scope->getId());
555+
if (!$this->getScope()) {
556+
switch (true) {
557+
case $this->getStore():
558+
$this->setScope(StoreScopeInterface::SCOPE_STORES);
559+
$this->setScopeCode($this->getStore());
560+
break;
561+
case $this->getWebsite():
562+
$this->setScope(StoreScopeInterface::SCOPE_WEBSITES);
563+
$this->setScopeCode($this->getWebsite());
564+
break;
565+
default:
566+
$this->setScope(ScopeInterface::SCOPE_DEFAULT);
567+
$this->setScopeCode('');
568+
break;
556569
}
557-
$this->setScopeId($scope->getId());
558-
} else {
559-
$scope = $this->resolveScope();
560-
$this->setScope($scope->getScopeType());
561-
$this->setScopeId($scope->getId());
562-
$this->setScopeCode($scope->getCode());
563570
}
564-
}
565-
566-
/**
567-
* Resolve scope
568-
*
569-
* @return ScopeInterface
570-
*/
571-
private function resolveScope(): ScopeInterface
572-
{
573-
switch (true) {
574-
case $this->getStore():
575-
$scope = $this->scopeResolverPool->get(StoreScopeInterface::SCOPE_STORE)
576-
->getScope($this->getStore());
577-
break;
578-
case $this->getWebsite():
579-
$scope = $this->scopeResolverPool->get(StoreScopeInterface::SCOPE_WEBSITE)
580-
->getScope($this->getWebsite());
581-
break;
582-
default:
583-
$scope = $this->scopeResolverPool->get(ScopeInterface::SCOPE_DEFAULT)
584-
->getScope(0);
585-
break;
571+
$scope = $this->scopeResolverPool->get($this->getScope())
572+
->getScope($this->getScopeCode());
573+
$this->setScope($this->scopeTypeNormalizer->normalize($scope->getScopeType()));
574+
$this->setScopeCode($scope->getCode());
575+
$this->setScopeId($scope->getId());
576+
if ($this->getWebsite() === null) {
577+
$this->setWebsite(StoreScopeInterface::SCOPE_WEBSITES === $this->getScope() ? $scope->getId() : '');
578+
}
579+
if ($this->getStore() === null) {
580+
$this->setStore(StoreScopeInterface::SCOPE_STORES === $this->getScope() ? $scope->getId() : '');
586581
}
587-
588-
return $scope;
589582
}
590583

591584
/**

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ class ConfigTest extends \PHPUnit\Framework\TestCase
7777
*/
7878
private $scope;
7979

80+
/**
81+
* @var \Magento\Store\Model\ScopeTypeNormalizer|MockObject
82+
*/
83+
private $scopeTypeNormalizer;
84+
8085
protected function setUp()
8186
{
8287
$this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
@@ -118,6 +123,8 @@ protected function setUp()
118123
$this->scopeResolver->method('getScope')
119124
->willReturn($this->scope);
120125

126+
$this->scopeTypeNormalizer = $this->createMock(\Magento\Store\Model\ScopeTypeNormalizer::class);
127+
121128
$this->model = new \Magento\Config\Model\Config(
122129
$this->appConfigMock,
123130
$this->eventManagerMock,
@@ -128,7 +135,8 @@ protected function setUp()
128135
$this->storeManager,
129136
$this->settingsChecker,
130137
[],
131-
$this->scopeResolverPool
138+
$this->scopeResolverPool,
139+
$this->scopeTypeNormalizer
132140
);
133141
}
134142

@@ -284,6 +292,10 @@ public function testSaveToCheckScopeDataSet()
284292
$this->scope->expects($this->atLeastOnce())
285293
->method('getCode')
286294
->willReturn('website_code');
295+
$this->scopeTypeNormalizer->expects($this->atLeastOnce())
296+
->method('normalize')
297+
->with('website')
298+
->willReturn('websites');
287299
$website = $this->createMock(\Magento\Store\Model\Website::class);
288300
$this->storeManager->expects($this->any())->method('getWebsites')->will($this->returnValue([$website]));
289301
$this->storeManager->expects($this->any())->method('isSingleStoreMode')->will($this->returnValue(true));
@@ -302,7 +314,7 @@ public function testSaveToCheckScopeDataSet()
302314
'field' => 'key',
303315
'groups' => [1 => ['fields' => ['key' => ['data']]]],
304316
'group_id' => null,
305-
'scope' => 'website',
317+
'scope' => 'websites',
306318
'scope_id' => 1,
307319
'scope_code' => 'website_code',
308320
'field_config' => null,

0 commit comments

Comments
 (0)