Skip to content

Commit 663d23a

Browse files
committed
MC-37351: Cart contents lost after switching to different store with different domain
- Add integration test
1 parent f529c59 commit 663d23a

File tree

2 files changed

+175
-5
lines changed

2 files changed

+175
-5
lines changed

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
namespace Magento\Store\Controller\Store;
99

10+
use Magento\Framework\App\CacheInterface;
1011
use Magento\Framework\Session\SidResolverInterface;
1112
use Magento\Store\Model\StoreResolver;
13+
use Magento\Store\Model\StoreSwitcher\RedirectDataPreprocessorInterface;
1214
use Magento\TestFramework\TestCase\AbstractController;
15+
use PHPUnit\Framework\MockObject\MockObject;
1316

1417
/**
1518
* Test Redirect controller.
@@ -18,6 +21,68 @@
1821
*/
1922
class RedirectTest extends AbstractController
2023
{
24+
/**
25+
* @var RedirectDataPreprocessorInterface
26+
*/
27+
private $preprocessor;
28+
/**
29+
* @var MockObject
30+
*/
31+
private $preprocessorMock;
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
protected function setUp(): void
37+
{
38+
parent::setUp();
39+
$this->preprocessor = $this->_objectManager->get(RedirectDataPreprocessorInterface::class);
40+
$this->preprocessorMock = $this->createMock(RedirectDataPreprocessorInterface::class);
41+
$this->_objectManager->addSharedInstance($this->preprocessorMock, get_class($this->preprocessor));
42+
}
43+
44+
/**
45+
* @inheritDoc
46+
*/
47+
protected function tearDown(): void
48+
{
49+
if ($this->preprocessor) {
50+
$this->_objectManager->addSharedInstance($this->preprocessor, get_class($this->preprocessor));
51+
}
52+
parent::tearDown();
53+
}
54+
55+
/**
56+
* @magentoDataFixture Magento/Store/_files/second_store.php
57+
* @magentoConfigFixture web/url/use_store 0
58+
* @magentoConfigFixture fixture_second_store_store web/unsecure/base_url http://second_store.test/
59+
* @magentoConfigFixture fixture_second_store_store web/unsecure/base_link_url http://second_store.test/
60+
* @magentoConfigFixture fixture_second_store_store web/secure/base_url http://second_store.test/
61+
* @magentoConfigFixture fixture_second_store_store web/secure/base_link_url http://second_store.test/
62+
*/
63+
public function testRedirectToSecondStoreOnAnotherUrl(): void
64+
{
65+
$this->preprocessorMock->method('process')
66+
->willReturn(['key1' => 'value1', 'key2' => 1]);
67+
$this->getRequest()->setParam(StoreResolver::PARAM_NAME, 'fixture_second_store');
68+
$this->getRequest()->setParam('___from_store', 'default');
69+
$this->dispatch('/stores/store/redirect');
70+
$header = $this->getResponse()->getHeader('Location');
71+
$this->assertNotEmpty($header);
72+
$result = $header->getFieldValue();
73+
$this->assertStringStartsWith('http://second_store.test/', $result);
74+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
75+
$urlParts = parse_url($result);
76+
$this->assertStringEndsWith('stores/store/switch/', $urlParts['path']);
77+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
78+
parse_str($urlParts['query'], $params);
79+
$this->assertTrue(!empty($params['time_stamp']));
80+
$this->assertTrue(!empty($params['signature']));
81+
$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']));
84+
}
85+
2186
/**
2287
* Check that there's no SID in redirect URL.
2388
*
@@ -35,6 +100,6 @@ public function testNoSid(): void
35100

36101
$result = (string)$this->getResponse()->getHeader('location');
37102
$this->assertNotEmpty($result);
38-
$this->assertStringNotContainsString(SidResolverInterface::SESSION_ID_QUERY_PARAM .'=', $result);
103+
$this->assertStringNotContainsString(SidResolverInterface::SESSION_ID_QUERY_PARAM . '=', $result);
39104
}
40105
}

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

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,116 @@
55
*/
66
namespace Magento\Store\Controller\Store;
77

8+
use Magento\Framework\App\ActionInterface;
9+
use Magento\Framework\Encryption\UrlCoder;
10+
use Magento\Store\Api\StoreResolverInterface;
11+
use Magento\Store\Model\Store;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Store\Model\StoreSwitcher\ContextInterface;
14+
use Magento\Store\Model\StoreSwitcher\ContextInterfaceFactory;
15+
use Magento\Store\Model\StoreSwitcher\RedirectDataGenerator;
16+
use Magento\Store\Model\StoreSwitcher\RedirectDataPostprocessorInterface;
17+
use Magento\Store\Model\StoreSwitcher\RedirectDataPreprocessorInterface;
18+
use Magento\TestFramework\TestCase\AbstractController;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
821
/**
922
* Test for store switch controller.
1023
*/
11-
class SwitchActionTest extends \Magento\TestFramework\TestCase\AbstractController
24+
class SwitchActionTest extends AbstractController
1225
{
26+
/**
27+
* @var RedirectDataPreprocessorInterface
28+
*/
29+
private $preprocessor;
30+
/**
31+
* @var MockObject
32+
*/
33+
private $preprocessorMock;
34+
/**
35+
* @var RedirectDataPostprocessorInterface
36+
*/
37+
private $postprocessor;
38+
/**
39+
* @var MockObject
40+
*/
41+
private $postprocessorMock;
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
protected function setUp(): void
47+
{
48+
parent::setUp();
49+
$this->preprocessor = $this->_objectManager->get(RedirectDataPreprocessorInterface::class);
50+
$this->preprocessorMock = $this->createMock(RedirectDataPreprocessorInterface::class);
51+
$this->_objectManager->addSharedInstance($this->preprocessorMock, get_class($this->preprocessor));
52+
53+
$this->postprocessor = $this->_objectManager->get(RedirectDataPostprocessorInterface::class);
54+
$this->postprocessorMock = $this->createMock(RedirectDataPostprocessorInterface::class);
55+
$this->_objectManager->addSharedInstance($this->postprocessorMock, get_class($this->postprocessor));
56+
}
57+
58+
/**
59+
* @inheritDoc
60+
*/
61+
protected function tearDown(): void
62+
{
63+
if ($this->preprocessor) {
64+
$this->_objectManager->addSharedInstance($this->preprocessor, get_class($this->preprocessor));
65+
}
66+
if ($this->postprocessor) {
67+
$this->_objectManager->addSharedInstance($this->postprocessor, get_class($this->postprocessor));
68+
}
69+
parent::tearDown();
70+
}
71+
72+
/**
73+
* @magentoDataFixture Magento/Store/_files/second_store.php
74+
* @magentoConfigFixture web/url/use_store 0
75+
* @magentoConfigFixture fixture_second_store_store web/unsecure/base_url http://second_store.test/
76+
* @magentoConfigFixture fixture_second_store_store web/unsecure/base_link_url http://second_store.test/
77+
* @magentoConfigFixture fixture_second_store_store web/secure/base_url http://second_store.test/
78+
* @magentoConfigFixture fixture_second_store_store web/secure/base_link_url http://second_store.test/
79+
*/
80+
public function testSwitch()
81+
{
82+
$data = ['key1' => 'value1', 'key2' => 1];
83+
$this->preprocessorMock->method('process')
84+
->willReturn($data);
85+
$this->postprocessorMock->expects($this->once())
86+
->method('process')
87+
->with($this->isInstanceOf(ContextInterface::class), $data);
88+
89+
$redirectDataGenerator = $this->_objectManager->get(RedirectDataGenerator::class);
90+
$contextFactory = $this->_objectManager->get(ContextInterfaceFactory::class);
91+
$storeManager = $this->_objectManager->get(StoreManagerInterface::class);
92+
$urlEncoder = $this->_objectManager->get(UrlCoder::class);
93+
$fromStore = $storeManager->getStore('fixture_second_store');
94+
$targetStore = $storeManager->getStore('default');
95+
$redirectData = $redirectDataGenerator->generate(
96+
$contextFactory->create(
97+
[
98+
'fromStore' => $fromStore,
99+
'targetStore' => $targetStore,
100+
'redirectUrl' => '/',
101+
]
102+
)
103+
);
104+
$this->getRequest()->setParams(
105+
[
106+
'___from_store' => $fromStore->getCode(),
107+
StoreResolverInterface::PARAM_NAME => $targetStore->getCode(),
108+
ActionInterface::PARAM_NAME_URL_ENCODED => $urlEncoder->encode('/'),
109+
'data' => $redirectData->getData(),
110+
'time_stamp' => $redirectData->getTimestamp(),
111+
'signature' => $redirectData->getSignature(),
112+
]
113+
);
114+
$this->dispatch('stores/store/switch');
115+
$this->assertRedirect($this->equalTo('http://localhost/index.php/'));
116+
}
117+
13118
/**
14119
* Ensure that proper default store code is calculated.
15120
*
@@ -41,10 +146,10 @@ public function testExecuteWithCustomDefaultStore()
41146
* @param string $from
42147
* @param string $to
43148
*/
44-
protected function changeStoreCode($from, $to)
149+
private function changeStoreCode($from, $to)
45150
{
46-
/** @var \Magento\Store\Model\Store $store */
47-
$store = $this->_objectManager->create(\Magento\Store\Model\Store::class);
151+
/** @var Store $store */
152+
$store = $this->_objectManager->create(Store::class);
48153
$store->load($from, 'code');
49154
$store->setCode($to);
50155
$store->save();

0 commit comments

Comments
 (0)