Skip to content

Commit f7fbeb9

Browse files
committed
MC-37351: Cart contents lost after switching to different store with different domain
- Fix static tests
1 parent 663d23a commit f7fbeb9

File tree

2 files changed

+79
-12
lines changed

2 files changed

+79
-12
lines changed

dev/tests/integration/testsuite/Magento/Store/Controller/Store/RedirectTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77

88
namespace Magento\Store\Controller\Store;
99

10-
use Magento\Framework\App\CacheInterface;
10+
use Magento\Framework\Interception\InterceptorInterface;
1111
use Magento\Framework\Session\SidResolverInterface;
1212
use Magento\Store\Model\StoreResolver;
1313
use Magento\Store\Model\StoreSwitcher\RedirectDataPreprocessorInterface;
14+
use Magento\Store\Model\StoreSwitcher\RedirectDataSerializerInterface;
1415
use Magento\TestFramework\TestCase\AbstractController;
1516
use PHPUnit\Framework\MockObject\MockObject;
1617

1718
/**
1819
* Test Redirect controller.
1920
*
21+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2022
* @magentoAppArea frontend
2123
*/
2224
class RedirectTest extends AbstractController
@@ -38,7 +40,7 @@ protected function setUp(): void
3840
parent::setUp();
3941
$this->preprocessor = $this->_objectManager->get(RedirectDataPreprocessorInterface::class);
4042
$this->preprocessorMock = $this->createMock(RedirectDataPreprocessorInterface::class);
41-
$this->_objectManager->addSharedInstance($this->preprocessorMock, get_class($this->preprocessor));
43+
$this->_objectManager->addSharedInstance($this->preprocessorMock, $this->getClassName($this->preprocessor));
4244
}
4345

4446
/**
@@ -47,7 +49,7 @@ protected function setUp(): void
4749
protected function tearDown(): void
4850
{
4951
if ($this->preprocessor) {
50-
$this->_objectManager->addSharedInstance($this->preprocessor, get_class($this->preprocessor));
52+
$this->_objectManager->addSharedInstance($this->preprocessor, $this->getClassName($this->preprocessor));
5153
}
5254
parent::tearDown();
5355
}
@@ -62,8 +64,9 @@ protected function tearDown(): void
6264
*/
6365
public function testRedirectToSecondStoreOnAnotherUrl(): void
6466
{
67+
$data = ['key1' => 'value1', 'key2' => 1];
6568
$this->preprocessorMock->method('process')
66-
->willReturn(['key1' => 'value1', 'key2' => 1]);
69+
->willReturn($data);
6770
$this->getRequest()->setParam(StoreResolver::PARAM_NAME, 'fixture_second_store');
6871
$this->getRequest()->setParam('___from_store', 'default');
6972
$this->dispatch('/stores/store/redirect');
@@ -79,8 +82,23 @@ public function testRedirectToSecondStoreOnAnotherUrl(): void
7982
$this->assertTrue(!empty($params['time_stamp']));
8083
$this->assertTrue(!empty($params['signature']));
8184
$this->assertTrue(!empty($params['data']));
82-
$cache = $this->_objectManager->get(CacheInterface::class);
83-
$this->assertEquals('{"key1":"value1","key2":1}', $cache->load('store_switch_' . $params['data']));
85+
$serializer = $this->_objectManager->get(RedirectDataSerializerInterface::class);
86+
$this->assertEquals($data, $serializer->unserialize($params['data']));
87+
}
88+
89+
/**
90+
* Return class name of the given object
91+
*
92+
* @param mixed $instance
93+
*/
94+
private function getClassName($instance): string
95+
{
96+
if ($instance instanceof InterceptorInterface) {
97+
$actionClass = get_parent_class($instance);
98+
} else {
99+
$actionClass = get_class($instance);
100+
}
101+
return $actionClass;
84102
}
85103

86104
/**

dev/tests/integration/testsuite/Magento/Store/Controller/Store/SwitchActionTest.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
namespace Magento\Store\Controller\Store;
77

8+
use Magento\Authorization\Model\UserContextInterface;
89
use Magento\Framework\App\ActionInterface;
910
use Magento\Framework\Encryption\UrlCoder;
11+
use Magento\Framework\Interception\InterceptorInterface;
1012
use Magento\Store\Api\StoreResolverInterface;
1113
use Magento\Store\Model\Store;
1214
use Magento\Store\Model\StoreManagerInterface;
@@ -20,6 +22,9 @@
2022

2123
/**
2224
* Test for store switch controller.
25+
*
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
* @magentoAppArea frontend
2328
*/
2429
class SwitchActionTest extends AbstractController
2530
{
@@ -39,6 +44,14 @@ class SwitchActionTest extends AbstractController
3944
* @var MockObject
4045
*/
4146
private $postprocessorMock;
47+
/**
48+
* @var UserContextInterface
49+
*/
50+
private $userContext;
51+
/**
52+
* @var MockObject
53+
*/
54+
private $userContextMock;
4255

4356
/**
4457
* @inheritDoc
@@ -48,11 +61,15 @@ protected function setUp(): void
4861
parent::setUp();
4962
$this->preprocessor = $this->_objectManager->get(RedirectDataPreprocessorInterface::class);
5063
$this->preprocessorMock = $this->createMock(RedirectDataPreprocessorInterface::class);
51-
$this->_objectManager->addSharedInstance($this->preprocessorMock, get_class($this->preprocessor));
64+
$this->_objectManager->addSharedInstance($this->preprocessorMock, $this->getClassName($this->preprocessor));
5265

5366
$this->postprocessor = $this->_objectManager->get(RedirectDataPostprocessorInterface::class);
5467
$this->postprocessorMock = $this->createMock(RedirectDataPostprocessorInterface::class);
55-
$this->_objectManager->addSharedInstance($this->postprocessorMock, get_class($this->postprocessor));
68+
$this->_objectManager->addSharedInstance($this->postprocessorMock, $this->getClassName($this->postprocessor));
69+
70+
$this->userContext = $this->_objectManager->get(UserContextInterface::class);
71+
$this->userContextMock = $this->createMock(UserContextInterface::class);
72+
$this->_objectManager->addSharedInstance($this->userContextMock, $this->getClassName($this->userContext));
5673
}
5774

5875
/**
@@ -61,15 +78,19 @@ protected function setUp(): void
6178
protected function tearDown(): void
6279
{
6380
if ($this->preprocessor) {
64-
$this->_objectManager->addSharedInstance($this->preprocessor, get_class($this->preprocessor));
81+
$this->_objectManager->addSharedInstance($this->preprocessor, $this->getClassName($this->preprocessor));
6582
}
6683
if ($this->postprocessor) {
67-
$this->_objectManager->addSharedInstance($this->postprocessor, get_class($this->postprocessor));
84+
$this->_objectManager->addSharedInstance($this->postprocessor, $this->getClassName($this->postprocessor));
85+
}
86+
if ($this->userContext) {
87+
$this->_objectManager->addSharedInstance($this->userContext, $this->getClassName($this->userContext));
6888
}
6989
parent::tearDown();
7090
}
7191

7292
/**
93+
* @magentoDataFixture Magento/Customer/_files/customer.php
7394
* @magentoDataFixture Magento/Store/_files/second_store.php
7495
* @magentoConfigFixture web/url/use_store 0
7596
* @magentoConfigFixture fixture_second_store_store web/unsecure/base_url http://second_store.test/
@@ -82,10 +103,23 @@ public function testSwitch()
82103
$data = ['key1' => 'value1', 'key2' => 1];
83104
$this->preprocessorMock->method('process')
84105
->willReturn($data);
106+
$this->userContextMock->method('getUserType')
107+
->willReturn(UserContextInterface::USER_TYPE_CUSTOMER);
108+
$this->userContextMock->method('getUserId')
109+
->willReturn(1);
85110
$this->postprocessorMock->expects($this->once())
86111
->method('process')
87-
->with($this->isInstanceOf(ContextInterface::class), $data);
88-
112+
->with(
113+
$this->callback(
114+
function (ContextInterface $context) {
115+
return $context->getFromStore()->getCode() === 'fixture_second_store'
116+
&& $context->getTargetStore()->getCode() === 'default'
117+
&& $context->getRedirectUrl() === 'http://localhost/index.php/'
118+
&& $context->getCustomerId() === 1;
119+
}
120+
),
121+
$data
122+
);
89123
$redirectDataGenerator = $this->_objectManager->get(RedirectDataGenerator::class);
90124
$contextFactory = $this->_objectManager->get(ContextInterfaceFactory::class);
91125
$storeManager = $this->_objectManager->get(StoreManagerInterface::class);
@@ -115,6 +149,21 @@ public function testSwitch()
115149
$this->assertRedirect($this->equalTo('http://localhost/index.php/'));
116150
}
117151

152+
/**
153+
* Return class name of the given object
154+
*
155+
* @param mixed $instance
156+
*/
157+
private function getClassName($instance): string
158+
{
159+
if ($instance instanceof InterceptorInterface) {
160+
$actionClass = get_parent_class($instance);
161+
} else {
162+
$actionClass = get_class($instance);
163+
}
164+
return $actionClass;
165+
}
166+
118167
/**
119168
* Ensure that proper default store code is calculated.
120169
*

0 commit comments

Comments
 (0)