Skip to content

Commit 8b3a1c1

Browse files
committed
MAGETWO-69107: [Backport] - [Magento Cloud] Switching stores not working properly - for 2.1
1 parent a91df24 commit 8b3a1c1

File tree

5 files changed

+194
-23
lines changed

5 files changed

+194
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public function getTargetStorePostData(\Magento\Store\Model\Store $store, $data
221221
{
222222
$data[\Magento\Store\Api\StoreResolverInterface::PARAM_NAME] = $store->getCode();
223223
return $this->_postDataHelper->getPostData(
224-
$this->getUrl('stores/store/switch'),
224+
$store->getCurrentUrl(false),
225225
$data
226226
);
227227
}

app/code/Magento/Store/Model/Plugin/StoreCookie.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Store\Model\StoreIsInactiveException;
1313
use Magento\Framework\Exception\NoSuchEntityException;
1414
use \InvalidArgumentException;
15+
use Magento\Store\Api\StoreResolverInterface;
16+
use Magento\Framework\App\ObjectManager;
1517

1618
/**
1719
* Class StoreCookie
@@ -33,19 +35,27 @@ class StoreCookie
3335
*/
3436
protected $storeRepository;
3537

38+
/**
39+
* @var StoreResolverInterface
40+
*/
41+
private $storeResolver;
42+
3643
/**
3744
* @param StoreManagerInterface $storeManager
3845
* @param StoreCookieManagerInterface $storeCookieManager
3946
* @param StoreRepositoryInterface $storeRepository
47+
* @param StoreResolverInterface $storeResolver
4048
*/
4149
public function __construct(
4250
StoreManagerInterface $storeManager,
4351
StoreCookieManagerInterface $storeCookieManager,
44-
StoreRepositoryInterface $storeRepository
52+
StoreRepositoryInterface $storeRepository,
53+
StoreResolverInterface $storeResolver = null
4554
) {
4655
$this->storeManager = $storeManager;
4756
$this->storeCookieManager = $storeCookieManager;
4857
$this->storeRepository = $storeRepository;
58+
$this->storeResolver = $storeResolver ?: ObjectManager::getInstance()->get(StoreResolverInterface::class);
4959
}
5060

5161
/**
@@ -72,5 +82,13 @@ public function beforeDispatch(
7282
$this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView());
7383
}
7484
}
85+
if (
86+
$this->storeCookieManager->getStoreCodeFromCookie() === null
87+
|| $request->getParam(StoreResolverInterface::PARAM_NAME) !== null
88+
) {
89+
$storeId = $this->storeResolver->getCurrentStoreId();
90+
$store = $this->storeRepository->getActiveStoreById($storeId);
91+
$this->storeCookieManager->setStoreCookie($store);
92+
}
7593
}
7694
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,19 @@ protected function setUp()
4444

4545
public function testGetTargetStorePostData()
4646
{
47-
$store = $this->getMockBuilder('Magento\Store\Model\Store')->disableOriginalConstructor()->getMock();
48-
$store->expects($this->any())->method('getCode')->will($this->returnValue('new-store'));
49-
$storeSwitchUrl = 'stores/store/switch';
50-
$this->urlBuilder->expects($this->any())->method('getUrl')->with($storeSwitchUrl)->willReturnArgument(0);
51-
$this->corePostDataHelper->expects($this->any())->method('getPostData')
47+
$store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
48+
->disableOriginalConstructor()
49+
->getMock();
50+
$store->expects($this->any())
51+
->method('getCode')
52+
->willReturn('new-store');
53+
$storeSwitchUrl = 'http://domain.com/stores/store/switch';
54+
$store->expects($this->atLeastOnce())
55+
->method('getCurrentUrl')
56+
->with(false)
57+
->willReturn($storeSwitchUrl);
58+
$this->corePostDataHelper->expects($this->any())
59+
->method('getPostData')
5260
->with($storeSwitchUrl, ['___store' => 'new-store']);
5361

5462
$this->switcher->getTargetStorePostData($store);

app/code/Magento/Store/Test/Unit/Model/Plugin/StoreCookieTest.php

Lines changed: 115 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
namespace Magento\Store\Test\Unit\Model\Plugin;
88

99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10-
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\Store\Api\StoreResolverInterface;
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Store\Model\StoreIsInactiveException;
1313
use \InvalidArgumentException;
1414

1515
/**
16-
* Class StoreCookieTest
16+
* Unit tests for \Magento\Store\Model\Plugin\StoreCookie class.
17+
*
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1719
*/
1820
class StoreCookieTest extends \PHPUnit_Framework_TestCase
1921
{
@@ -52,6 +54,11 @@ class StoreCookieTest extends \PHPUnit_Framework_TestCase
5254
*/
5355
protected $storeRepositoryMock;
5456

57+
/**
58+
* @var \Magento\Store\Api\StoreResolverInterface|\PHPUnit_Framework_MockObject_MockObject
59+
*/
60+
protected $storeResolverMock;
61+
5562
/**
5663
* Set up
5764
*/
@@ -87,59 +94,151 @@ protected function setUp()
8794
->setMethods([])
8895
->getMock();
8996

97+
$this->storeResolverMock = $this->getMockBuilder(\Magento\Store\Api\StoreResolverInterface::class)
98+
->disableOriginalConstructor()
99+
->setMethods([])
100+
->getMock();
101+
90102
$this->plugin = (new ObjectManager($this))->getObject(
91103
'Magento\Store\Model\Plugin\StoreCookie',
92104
[
93105
'storeManager' => $this->storeManagerMock,
94106
'storeCookieManager' => $this->storeCookieManagerMock,
95-
'storeRepository' => $this->storeRepositoryMock
107+
'storeRepository' => $this->storeRepositoryMock,
108+
'storeResolver' => $this->storeResolverMock
96109
]
97110
);
98111
}
99112

100113
public function testBeforeDispatchNoSuchEntity()
101114
{
102115
$storeCode = 'store';
103-
$this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock);
104-
$this->storeCookieManagerMock->expects($this->once())->method('getStoreCodeFromCookie')->willReturn($storeCode);
116+
$this->storeManagerMock->expects($this->once())
117+
->method('getDefaultStoreView')
118+
->willReturn($this->storeMock);
119+
$this->storeCookieManagerMock->expects($this->atLeastOnce())
120+
->method('getStoreCodeFromCookie')
121+
->willReturn($storeCode);
105122
$this->storeRepositoryMock->expects($this->once())
106123
->method('getActiveStoreByCode')
107124
->willThrowException(new NoSuchEntityException);
108-
$this->storeCookieManagerMock->expects($this->once())->method('deleteStoreCookie')->with($this->storeMock);
125+
$this->storeCookieManagerMock->expects($this->once())
126+
->method('deleteStoreCookie')
127+
->with($this->storeMock);
128+
$this->requestMock->expects($this->atLeastOnce())
129+
->method('getParam')
130+
->with(StoreResolverInterface::PARAM_NAME)
131+
->willReturn(null);
132+
109133
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
110134
}
111135

112136
public function testBeforeDispatchStoreIsInactive()
113137
{
114138
$storeCode = 'store';
115-
$this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock);
116-
$this->storeCookieManagerMock->expects($this->once())->method('getStoreCodeFromCookie')->willReturn($storeCode);
139+
$this->storeManagerMock->expects($this->once())
140+
->method('getDefaultStoreView')
141+
->willReturn($this->storeMock);
142+
$this->storeCookieManagerMock->expects($this->atLeastOnce())
143+
->method('getStoreCodeFromCookie')
144+
->willReturn($storeCode);
117145
$this->storeRepositoryMock->expects($this->once())
118146
->method('getActiveStoreByCode')
119147
->willThrowException(new StoreIsInactiveException);
120-
$this->storeCookieManagerMock->expects($this->once())->method('deleteStoreCookie')->with($this->storeMock);
148+
$this->storeCookieManagerMock->expects($this->once())
149+
->method('deleteStoreCookie')
150+
->with($this->storeMock);
151+
$this->requestMock->expects($this->atLeastOnce())
152+
->method('getParam')
153+
->with(StoreResolverInterface::PARAM_NAME)
154+
->willReturn(null);
155+
121156
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
122157
}
123158

124159
public function testBeforeDispatchInvalidArgument()
125160
{
126161
$storeCode = 'store';
127-
$this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock);
128-
$this->storeCookieManagerMock->expects($this->once())->method('getStoreCodeFromCookie')->willReturn($storeCode);
162+
$this->storeManagerMock->expects($this->once())
163+
->method('getDefaultStoreView')
164+
->willReturn($this->storeMock);
165+
$this->storeCookieManagerMock->expects($this->atLeastOnce())
166+
->method('getStoreCodeFromCookie')
167+
->willReturn($storeCode);
129168
$this->storeRepositoryMock->expects($this->once())
130169
->method('getActiveStoreByCode')
131170
->willThrowException(new InvalidArgumentException);
132-
$this->storeCookieManagerMock->expects($this->once())->method('deleteStoreCookie')->with($this->storeMock);
171+
$this->storeCookieManagerMock->expects($this->once())
172+
->method('deleteStoreCookie')
173+
->with($this->storeMock);
174+
$this->requestMock->expects($this->atLeastOnce())
175+
->method('getParam')
176+
->with(StoreResolverInterface::PARAM_NAME)
177+
->willReturn(null);
178+
133179
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
134180
}
135181

136182
public function testBeforeDispatchNoStoreCookie()
137183
{
138184
$storeCode = null;
139-
$this->storeCookieManagerMock->expects($this->once())->method('getStoreCodeFromCookie')->willReturn($storeCode);
140-
$this->storeManagerMock->expects($this->never())->method('getDefaultStoreView')->willReturn($this->storeMock);
141-
$this->storeRepositoryMock->expects($this->never())->method('getActiveStoreByCode');
142-
$this->storeCookieManagerMock->expects($this->never())->method('deleteStoreCookie')->with($this->storeMock);
185+
$this->storeCookieManagerMock->expects($this->atLeastOnce())
186+
->method('getStoreCodeFromCookie')
187+
->willReturn($storeCode);
188+
$this->storeManagerMock->expects($this->never())
189+
->method('getDefaultStoreView')
190+
->willReturn($this->storeMock);
191+
$this->storeRepositoryMock->expects($this->never())
192+
->method('getActiveStoreByCode');
193+
$this->storeCookieManagerMock->expects($this->never())
194+
->method('deleteStoreCookie')
195+
->with($this->storeMock);
196+
197+
$this->storeResolverMock->expects($this->atLeastOnce())
198+
->method('getCurrentStoreId')
199+
->willReturn(1);
200+
201+
$this->storeRepositoryMock->expects($this->atLeastOnce())
202+
->method('getActiveStoreById')
203+
->willReturn($this->storeMock);
204+
205+
$this->storeCookieManagerMock->expects($this->atLeastOnce())
206+
->method('setStoreCookie')
207+
->with($this->storeMock);
208+
209+
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
210+
}
211+
212+
public function testBeforeDispatchWithStoreRequestParam()
213+
{
214+
$storeCode = 'store';
215+
$this->storeCookieManagerMock->expects($this->atLeastOnce())
216+
->method('getStoreCodeFromCookie')
217+
->willReturn($storeCode);
218+
$this->storeRepositoryMock->expects($this->atLeastOnce())
219+
->method('getActiveStoreByCode')
220+
->willReturn($this->storeMock);
221+
$this->storeCookieManagerMock->expects($this->never())
222+
->method('deleteStoreCookie')
223+
->with($this->storeMock);
224+
225+
$this->requestMock->expects($this->atLeastOnce())
226+
->method('getParam')
227+
->with(StoreResolverInterface::PARAM_NAME)
228+
->willReturn($storeCode);
229+
230+
$this->storeResolverMock->expects($this->atLeastOnce())
231+
->method('getCurrentStoreId')
232+
->willReturn(1);
233+
234+
$this->storeRepositoryMock->expects($this->atLeastOnce())
235+
->method('getActiveStoreById')
236+
->willReturn($this->storeMock);
237+
238+
$this->storeCookieManagerMock->expects($this->atLeastOnce())
239+
->method('setStoreCookie')
240+
->with($this->storeMock);
241+
143242
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
144243
}
145244
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Store\Block;
7+
8+
/**
9+
* Integration tests for \Magento\Store\Block\Switcher block.
10+
*/
11+
class SwitcherTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var \Magento\TestFramework\ObjectManager
15+
*/
16+
private $_objectManager;
17+
18+
/**
19+
* Set up.
20+
*
21+
* @return void
22+
*/
23+
protected function setUp()
24+
{
25+
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
26+
}
27+
28+
/**
29+
* Test that GetTargetStorePostData() method return correct store URL.
30+
*
31+
* @magentoDataFixture Magento/Store/_files/store.php
32+
* @return void
33+
*/
34+
public function testGetTargetStorePostData()
35+
{
36+
$storeCode = 'test';
37+
/** @var \Magento\Store\Block\Switcher $block */
38+
$block = $this->_objectManager->create(\Magento\Store\Block\Switcher::class);
39+
/** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
40+
$storeRepository = $this->_objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
41+
$store = $storeRepository->get($storeCode);
42+
$result = json_decode($block->getTargetStorePostData($store), true);
43+
44+
$this->assertContains($storeCode, $result['action']);
45+
}
46+
}

0 commit comments

Comments
 (0)