Skip to content

Commit 99366e7

Browse files
committed
MC-37351: Cart contents lost after switching to different store with different domain
- Remove customer_id from context interface
1 parent f7fbeb9 commit 99366e7

File tree

12 files changed

+50
-108
lines changed

12 files changed

+50
-108
lines changed

app/code/Magento/Customer/Model/StoreSwitcher/RedirectDataPostprocessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public function process(ContextInterface $context, array $data): void
6565
}
6666
} catch (NoSuchEntityException $e) {
6767
$this->logger->error($e);
68-
throw new LocalizedException(__('Failed to sign into the customer account.'), $e);
68+
throw new LocalizedException(__('Something went wrong.'), $e);
6969
} catch (LocalizedException $e) {
7070
$this->logger->error($e);
71-
throw new LocalizedException(__('Failed to sign into the customer account.'), $e);
71+
throw new LocalizedException(__('Something went wrong.'), $e);
7272
}
7373
}
7474
}

app/code/Magento/Customer/Model/StoreSwitcher/RedirectDataPreprocessor.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@
77

88
namespace Magento\Customer\Model\StoreSwitcher;
99

10-
use Magento\Authorization\Model\UserContextInterface;
1110
use Magento\Customer\Model\CustomerRegistry;
11+
use Magento\Customer\Model\Session;
1212
use Magento\Store\Model\StoreSwitcher\ContextInterface;
1313
use Magento\Store\Model\StoreSwitcher\RedirectDataPreprocessorInterface;
1414
use Psr\Log\LoggerInterface;
1515
use Throwable;
1616

1717
/**
1818
* Collect customer data to be redirected to target store
19+
*
20+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1921
*/
2022
class RedirectDataPreprocessor implements RedirectDataPreprocessorInterface
2123
{
2224
/**
23-
* @var UserContextInterface
25+
* @var Session
2426
*/
25-
private $userContext;
27+
private $session;
2628
/**
2729
* @var LoggerInterface
2830
*/
@@ -34,16 +36,16 @@ class RedirectDataPreprocessor implements RedirectDataPreprocessorInterface
3436

3537
/**
3638
* @param CustomerRegistry $customerRegistry
37-
* @param UserContextInterface $userContext
39+
* @param Session $session
3840
* @param LoggerInterface $logger
3941
*/
4042
public function __construct(
4143
CustomerRegistry $customerRegistry,
42-
UserContextInterface $userContext,
44+
Session $session,
4345
LoggerInterface $logger
4446
) {
4547
$this->customerRegistry = $customerRegistry;
46-
$this->userContext = $userContext;
48+
$this->session = $session;
4749
$this->logger = $logger;
4850
}
4951

@@ -52,11 +54,9 @@ public function __construct(
5254
*/
5355
public function process(ContextInterface $context, array $data): array
5456
{
55-
if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER
56-
&& $this->userContext->getUserId()
57-
) {
57+
if ($this->session->isLoggedIn()) {
5858
try {
59-
$customer = $this->customerRegistry->retrieve($this->userContext->getUserId());
59+
$customer = $this->customerRegistry->retrieve($this->session->getCustomerId());
6060
if (in_array($context->getTargetStore()->getId(), $customer->getSharedStoreIds())) {
6161
$data['customer_id'] = (int) $customer->getId();
6262
}

app/code/Magento/Customer/Test/Unit/Model/StoreSwitcher/RedirectDataPostprocessorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testProcessShouldNotLoginCustomerIfNotRegisteredInTargetStore():
118118

119119
public function testProcessShouldThrowExceptionIfCustomerDoesNotExist(): void
120120
{
121-
$this->expectErrorMessage('Failed to sign into the customer account.');
121+
$this->expectErrorMessage('Something went wrong.');
122122
$data = ['customer_id' => 1];
123123
$this->session->expects($this->never())
124124
->method('setCustomerDataAsLoggedIn');
@@ -127,7 +127,7 @@ public function testProcessShouldThrowExceptionIfCustomerDoesNotExist(): void
127127

128128
public function testProcessShouldThrowExceptionIfAnErrorOccur(): void
129129
{
130-
$this->expectErrorMessage('Failed to sign into the customer account.');
130+
$this->expectErrorMessage('Something went wrong.');
131131
$data = ['customer_id' => 2];
132132
$this->session->expects($this->never())
133133
->method('setCustomerDataAsLoggedIn');

app/code/Magento/Customer/Test/Unit/Model/StoreSwitcher/RedirectDataPreprocessorTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Authorization\Model\UserContextInterface;
1111
use Magento\Customer\Model\Customer;
1212
use Magento\Customer\Model\CustomerRegistry;
13+
use Magento\Customer\Model\Session;
1314
use Magento\Customer\Model\StoreSwitcher\RedirectDataPreprocessor;
1415
use Magento\Framework\Exception\LocalizedException;
1516
use Magento\Framework\Exception\NoSuchEntityException;
@@ -26,13 +27,13 @@ class RedirectDataPreprocessorTest extends TestCase
2627
*/
2728
private $model;
2829
/**
29-
* @var ContextInterface|MockObject
30+
* @var Session|MockObject
3031
*/
3132
private $context;
3233
/**
3334
* @var UserContextInterface|MockObject
3435
*/
35-
private $userContext;
36+
private $session;
3637

3738
/**
3839
* @inheritDoc
@@ -42,16 +43,13 @@ protected function setUp(): void
4243
parent::setUp();
4344
$customerRegistry = $this->createMock(CustomerRegistry::class);
4445
$logger = $this->createMock(LoggerInterface::class);
45-
$this->userContext = $this->createMock(UserContextInterface::class);
46+
$this->session = $this->createMock(Session::class);
4647
$this->model = new RedirectDataPreprocessor(
4748
$customerRegistry,
48-
$this->userContext,
49+
$this->session,
4950
$logger
5051
);
5152

52-
$this->userContext->method('getUserType')
53-
->willReturn(UserContextInterface::USER_TYPE_CUSTOMER);
54-
5553
$store1 = $this->createConfiguredMock(
5654
StoreInterface::class,
5755
[
@@ -101,7 +99,9 @@ function ($id) {
10199
*/
102100
public function testProcess(?int $customerId, array $data): void
103101
{
104-
$this->userContext->method('getUserId')
102+
$this->session->method('isLoggedIn')
103+
->willReturn(true);
104+
$this->session->method('getCustomerId')
105105
->willReturn($customerId);
106106
$this->assertEquals($data, $this->model->process($this->context, []));
107107
}

app/code/Magento/Store/Controller/Store/Redirect.php

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

88
namespace Magento\Store\Controller\Store;
99

10-
use Magento\Authorization\Model\UserContextInterface;
1110
use Magento\Framework\App\Action\Action;
1211
use Magento\Framework\App\Action\Context;
1312
use Magento\Framework\App\Action\HttpGetActionInterface;
@@ -60,10 +59,6 @@ class Redirect extends Action implements HttpGetActionInterface, HttpPostActionI
6059
* @var ContextInterfaceFactory|null
6160
*/
6261
private $contextFactory;
63-
/**
64-
* @var UserContextInterface|null
65-
*/
66-
private $userContext;
6762

6863
/**
6964
* @param Context $context
@@ -75,7 +70,6 @@ class Redirect extends Action implements HttpGetActionInterface, HttpPostActionI
7570
* @param StoreManagerInterface|null $storeManager
7671
* @param RedirectDataGenerator|null $redirectDataGenerator
7772
* @param ContextInterfaceFactory|null $contextFactory
78-
* @param UserContextInterface|null $userContext
7973
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8074
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8175
*/
@@ -88,8 +82,7 @@ public function __construct(
8882
HashGenerator $hashGenerator,
8983
StoreManagerInterface $storeManager = null,
9084
?RedirectDataGenerator $redirectDataGenerator = null,
91-
?ContextInterfaceFactory $contextFactory = null,
92-
?UserContextInterface $userContext = null
85+
?ContextInterfaceFactory $contextFactory = null
9386
) {
9487
parent::__construct($context);
9588
$this->storeRepository = $storeRepository;
@@ -100,8 +93,6 @@ public function __construct(
10093
?: ObjectManager::getInstance()->get(RedirectDataGenerator::class);
10194
$this->contextFactory = $contextFactory
10295
?: ObjectManager::getInstance()->get(ContextInterfaceFactory::class);
103-
$this->userContext = $userContext
104-
?: ObjectManager::getInstance()->get(UserContextInterface::class);
10596
}
10697

10798
/**
@@ -127,17 +118,12 @@ public function execute()
127118
$targetStore = $this->storeRepository->get($targetStoreCode);
128119
$this->storeManager->setCurrentStore($targetStore);
129120
$encodedUrl = $this->_request->getParam(ActionInterface::PARAM_NAME_URL_ENCODED);
130-
$customerId = $this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER
131-
&& $this->userContext->getUserId()
132-
? (int) $this->userContext->getUserId()
133-
: null;
134121
$redirectData = $this->redirectDataGenerator->generate(
135122
$this->contextFactory->create(
136123
[
137124
'fromStore' => $fromStore,
138125
'targetStore' => $targetStore,
139-
'redirectUrl' => $this->_redirect->getRedirectUrl(),
140-
'customerId' => $customerId
126+
'redirectUrl' => $this->_redirect->getRedirectUrl()
141127
]
142128
)
143129
);

app/code/Magento/Store/Model/StoreSwitcher/Context.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,20 @@ class Context implements ContextInterface
2626
* @var string
2727
*/
2828
private $redirectUrl;
29-
/**
30-
* @var int|null
31-
*/
32-
private $customerId;
3329

3430
/**
3531
* @param StoreInterface $fromStore
3632
* @param StoreInterface $targetStore
3733
* @param string $redirectUrl
38-
* @param int|null $customerId
3934
*/
4035
public function __construct(
4136
StoreInterface $fromStore,
4237
StoreInterface $targetStore,
43-
string $redirectUrl,
44-
?int $customerId = null
38+
string $redirectUrl
4539
) {
4640
$this->fromStore = $fromStore;
4741
$this->targetStore = $targetStore;
4842
$this->redirectUrl = $redirectUrl;
49-
$this->customerId = $customerId;
5043
}
5144

5245
/**
@@ -72,12 +65,4 @@ public function getRedirectUrl(): string
7265
{
7366
return $this->redirectUrl;
7467
}
75-
76-
/**
77-
* @inheritDoc
78-
*/
79-
public function getCustomerId(): ?int
80-
{
81-
return $this->customerId;
82-
}
8368
}

app/code/Magento/Store/Model/StoreSwitcher/ContextInterface.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,4 @@ public function getTargetStore(): StoreInterface;
3434
* @return string
3535
*/
3636
public function getRedirectUrl(): string;
37-
38-
/**
39-
* The logged in customer ID
40-
*
41-
* @return int
42-
*/
43-
public function getCustomerId(): ?int;
4437
}

app/code/Magento/Store/Model/StoreSwitcher/HashProcessor.php

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

88
namespace Magento\Store\Model\StoreSwitcher;
99

10-
use Magento\Authorization\Model\UserContextInterface;
1110
use Magento\Framework\App\RequestInterface;
1211
use Magento\Framework\Exception\LocalizedException;
1312
use Magento\Framework\Message\ManagerInterface;
@@ -55,10 +54,6 @@ class HashProcessor implements StoreSwitcherInterface
5554
* @var LoggerInterface
5655
*/
5756
private $logger;
58-
/**
59-
* @var UserContextInterface
60-
*/
61-
private $userContext;
6257

6358
/**
6459
* @param RequestInterface $request
@@ -69,7 +64,6 @@ class HashProcessor implements StoreSwitcherInterface
6964
* @param RedirectDataInterfaceFactory $dataFactory
7065
* @param RedirectDataValidator $dataValidator
7166
* @param LoggerInterface $logger
72-
* @param UserContextInterface $userContext
7367
*/
7468
public function __construct(
7569
RequestInterface $request,
@@ -79,8 +73,7 @@ public function __construct(
7973
ContextInterfaceFactory $contextFactory,
8074
RedirectDataInterfaceFactory $dataFactory,
8175
RedirectDataValidator $dataValidator,
82-
LoggerInterface $logger,
83-
UserContextInterface $userContext
76+
LoggerInterface $logger
8477
) {
8578
$this->request = $request;
8679
$this->postprocessor = $postprocessor;
@@ -90,7 +83,6 @@ public function __construct(
9083
$this->dataFactory = $dataFactory;
9184
$this->dataValidator = $dataValidator;
9285
$this->logger = $logger;
93-
$this->userContext = $userContext;
9486
}
9587

9688
/**
@@ -107,16 +99,11 @@ public function switch(StoreInterface $fromStore, StoreInterface $targetStore, s
10799
$timestamp = (int) $this->request->getParam('time_stamp');
108100
$signature = (string) $this->request->getParam('signature');
109101
$data = (string) $this->request->getParam('data');
110-
$customerId = $this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER
111-
&& $this->userContext->getUserId()
112-
? (int) $this->userContext->getUserId()
113-
: null;
114102
$context = $this->contextFactory->create(
115103
[
116104
'fromStore' => $fromStore,
117105
'targetStore' => $targetStore,
118-
'redirectUrl' => $redirectUrl,
119-
'customerId' => $customerId
106+
'redirectUrl' => $redirectUrl
120107
]
121108
);
122109
$redirectDataObject = $this->dataFactory->create(
@@ -140,7 +127,7 @@ public function switch(StoreInterface $fromStore, StoreInterface $targetStore, s
140127
} catch (\Throwable $exception) {
141128
$this->logger->error($exception);
142129
$this->messageManager->addErrorMessage(
143-
__('Something went wrong while switching to the store.')
130+
__('Something went wrong.')
144131
);
145132
}
146133

app/code/Magento/Store/Model/StoreSwitcher/RedirectDataCacheSerializer.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Framework\App\CacheInterface;
1212
use Magento\Framework\Math\Random;
1313
use Magento\Framework\Serialize\Serializer\Json;
14+
use Psr\Log\LoggerInterface;
15+
use Throwable;
1416

1517
/**
1618
* Store switcher redirect data cache serializer
@@ -33,20 +35,27 @@ class RedirectDataCacheSerializer implements RedirectDataSerializerInterface
3335
* @var Random
3436
*/
3537
private $random;
38+
/**
39+
* @var LoggerInterface
40+
*/
41+
private $logger;
3642

3743
/**
3844
* @param Json $json
3945
* @param Random $random
4046
* @param CacheInterface $cache
47+
* @param LoggerInterface $logger
4148
*/
4249
public function __construct(
4350
Json $json,
4451
Random $random,
45-
CacheInterface $cache
52+
CacheInterface $cache,
53+
LoggerInterface $logger
4654
) {
4755
$this->cache = $cache;
4856
$this->json = $json;
4957
$this->random = $random;
58+
$this->logger = $logger;
5059
}
5160

5261
/**
@@ -76,7 +85,11 @@ public function unserialize(string $data): array
7685
throw new InvalidArgumentException('Couldn\'t retrieve data from cache.');
7786
}
7887
$result = $this->json->unserialize($json);
79-
$this->cache->remove($cacheKey);
88+
try {
89+
$this->cache->remove($cacheKey);
90+
} catch (Throwable $exception) {
91+
$this->logger->error($exception);
92+
}
8093

8194
return $result;
8295
}

0 commit comments

Comments
 (0)