Skip to content

Commit 2f5d529

Browse files
committed
MC-41949: Hierarchy scope rolls back to Default after save
- Initial commit and adding layout overrider
1 parent 32fda1c commit 2f5d529

File tree

2 files changed

+178
-6
lines changed

2 files changed

+178
-6
lines changed

app/code/Magento/Backend/Block/Store/Switcher.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Backend\Block\Store;
99

10+
use Magento\Framework\Exception\LocalizedException;
11+
1012
/**
1113
* Store switcher block
1214
*
@@ -471,9 +473,17 @@ public function getCurrentSelectionName()
471473
*/
472474
public function getCurrentWebsiteName()
473475
{
474-
if ($this->getWebsiteId() !== null) {
476+
$websiteId = $this->getWebsiteId();
477+
if ($websiteId !== null) {
478+
if ($this->hasData('get_data_from_request')) {
479+
$requestedWebsite = $this->getRequest()->getParams('website');
480+
if (!empty($requestedWebsite)
481+
&& array_key_exists('website', $requestedWebsite)) {
482+
$websiteId = $requestedWebsite['website'];
483+
}
484+
}
475485
$website = $this->_websiteFactory->create();
476-
$website->load($this->getWebsiteId());
486+
$website->load($websiteId);
477487
if ($website->getId()) {
478488
return $website->getName();
479489
}
@@ -504,12 +514,21 @@ public function getCurrentStoreGroupName()
504514
* Get current store view name
505515
*
506516
* @return string
517+
* @throws LocalizedException
507518
*/
508519
public function getCurrentStoreName()
509520
{
510-
if ($this->getStoreId() !== null) {
521+
$storeId = $this->getStoreId();
522+
if ($storeId !== null) {
523+
if ($this->hasData('get_data_from_request')) {
524+
$requestedStore = $this->getRequest()->getParams('store');
525+
if (!empty($requestedStore)
526+
&& array_key_exists('store', $requestedStore)) {
527+
$storeId = $requestedStore['store'];
528+
}
529+
}
511530
$store = $this->_storeFactory->create();
512-
$store->load($this->getStoreId());
531+
$store->load($storeId);
513532
if ($store->getId()) {
514533
return $store->getName();
515534
}

app/code/Magento/Backend/Test/Unit/Block/Store/SwitcherTest.php

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
namespace Magento\Backend\Test\Unit\Block\Store;
99

1010
use Magento\Backend\Block\Store\Switcher;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Store\Model\StoreFactory;
14+
use Magento\Store\Model\Store;
15+
use Magento\Store\Model\WebsiteFactory;
16+
use Magento\Store\Model\Website;
1117
use Magento\Backend\Block\Template\Context;
1218
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1319
use Magento\Store\Model\StoreManagerInterface;
14-
use Magento\Store\Model\Website;
1520
use PHPUnit\Framework\TestCase;
1621

1722
class SwitcherTest extends TestCase
@@ -23,20 +28,81 @@ class SwitcherTest extends TestCase
2328

2429
private $storeManagerMock;
2530

31+
/**
32+
* @var RequestInterface|MockObject
33+
*/
34+
private $requestMock;
35+
36+
/**
37+
* @var WebsiteFactory|MockObject
38+
*/
39+
private $websiteFactoryMock;
40+
41+
/**
42+
* @var StoreFactory|MockObject
43+
*/
44+
private $storeFactoryMock;
45+
46+
/**
47+
* @var Website|MockObject
48+
*/
49+
private $websiteMock;
50+
51+
/**
52+
* @var Store|MockObject
53+
*/
54+
private $storeMock;
55+
2656
protected function setUp(): void
2757
{
2858
$this->storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
2959
$objectHelper = new ObjectManager($this);
60+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
61+
->getMockForAbstractClass();
62+
$this->websiteFactoryMock = $this->getMockBuilder(WebsiteFactory::class)
63+
->disableOriginalConstructor()
64+
->setMethods(['create'])
65+
->getMock();
66+
$this->storeFactoryMock = $this->getMockBuilder(StoreFactory::class)
67+
->disableOriginalConstructor()
68+
->setMethods(['create'])
69+
->getMock();
70+
$this->websiteMock = $this->getMockBuilder(Website::class)
71+
->disableOriginalConstructor()
72+
->setMethods(['load', 'getId', 'getName'])
73+
->getMock();
74+
$this->storeMock = $this->getMockBuilder(Store::class)
75+
->disableOriginalConstructor()
76+
->setMethods(['load', 'getId', 'getName'])
77+
->getMock();
78+
$this->websiteFactoryMock->expects($this->any())
79+
->method('create')
80+
->willReturn($this->websiteMock);
81+
$this->storeFactoryMock->expects($this->any())
82+
->method('create')
83+
->willReturn($this->storeMock);
84+
$this->websiteMock->expects($this->any())
85+
->method('load')
86+
->willReturnSelf();
87+
$this->storeMock->expects($this->any())
88+
->method('load')
89+
->willReturnSelf();
3090
$context = $objectHelper->getObject(
3191
Context::class,
3292
[
3393
'storeManager' => $this->storeManagerMock,
94+
'request' => $this->requestMock
3495
]
3596
);
3697

3798
$this->switcherBlock = $objectHelper->getObject(
3899
Switcher::class,
39-
['context' => $context]
100+
[
101+
'context' => $context,
102+
'data' => ['get_data_from_request' => 1],
103+
'websiteFactory' => $this->websiteFactoryMock,
104+
'storeFactory' => $this->storeFactoryMock
105+
]
40106
);
41107
}
42108

@@ -58,4 +124,91 @@ public function testGetWebsitesIfSetWebsiteIds()
58124
$expected = [1 => $websiteMock];
59125
$this->assertEquals($expected, $this->switcherBlock->getWebsites());
60126
}
127+
128+
/**
129+
* Test case for after current store name plugin
130+
*
131+
* @param array $requestedStore
132+
* @param string $expectedResult
133+
* @return void
134+
* @dataProvider getStoreNameDataProvider
135+
* @throws LocalizedException
136+
*/
137+
public function testAfterGetCurrentStoreName(array $requestedStore, string $expectedResult): void
138+
{
139+
$this->requestMock->expects($this->any())
140+
->method('getParams')
141+
->willReturn($requestedStore);
142+
$this->storeMock->expects($this->any())
143+
->method('getId')
144+
->willReturn($requestedStore);
145+
$this->storeMock->expects($this->any())
146+
->method('getName')
147+
->willReturn($expectedResult);
148+
$this->assertSame($expectedResult, $this->switcherBlock->getCurrentStoreName());
149+
}
150+
151+
/**
152+
* Data provider for getStoreName plugin
153+
*
154+
* @return array
155+
*/
156+
public function getStoreNameDataProvider(): array
157+
{
158+
return [
159+
'test storeName with valid requested store' =>
160+
[
161+
['store' => 'test store'],
162+
'base store'
163+
],
164+
'test storeName with invalid requested store' =>
165+
[
166+
['store' => 'test store'],
167+
'test store'
168+
]
169+
];
170+
}
171+
172+
/**
173+
* Test case for get current website name
174+
*
175+
* @param array $requestedWebsite
176+
* @param string $expectedResult
177+
* @return void
178+
* @dataProvider getWebsiteNameDataProvider
179+
*/
180+
public function testGetCurrentWebsiteName(array $requestedWebsite, string $expectedResult): void
181+
{
182+
$this->requestMock->expects($this->any())
183+
->method('getParams')
184+
->willReturn($requestedWebsite);
185+
$this->websiteMock->expects($this->any())
186+
->method('getId')
187+
->willReturn($requestedWebsite);
188+
$this->websiteMock->expects($this->any())
189+
->method('getName')
190+
->willReturn($expectedResult);
191+
$this->assertSame($expectedResult, $this->switcherBlock->getCurrentWebsiteName());
192+
}
193+
194+
/**
195+
* Data provider for getWebsiteName plugin
196+
*
197+
* @return array
198+
*/
199+
public function getWebsiteNameDataProvider(): array
200+
{
201+
return [
202+
'test websiteName with valid requested website' =>
203+
[
204+
['website' => 'test website'],
205+
'base website'
206+
],
207+
'test websiteName with invalid requested website' =>
208+
[
209+
['website' => 'test website'],
210+
'test website'
211+
]
212+
];
213+
}
61214
}

0 commit comments

Comments
 (0)