Skip to content

Commit 950fb1c

Browse files
isxamnikshostko
authored andcommitted
MAGETWO-64885: Wrong cookies set for store views with multidomain
1 parent 508f1ef commit 950fb1c

File tree

2 files changed

+128
-21
lines changed

2 files changed

+128
-21
lines changed

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::class)->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::class,
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
}

0 commit comments

Comments
 (0)