Skip to content

Commit b01eadf

Browse files
committed
ACP2E-973: Product is not getting added in wishlist from product list page and product view page when customer confirms account from confirmation email
1 parent cd826aa commit b01eadf

File tree

4 files changed

+55
-53
lines changed

4 files changed

+55
-53
lines changed

app/code/Magento/Wishlist/Controller/Index/Add.php

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Catalog\Api\ProductRepositoryInterface;
99
use Magento\Customer\Model\Session;
1010
use Magento\Framework\App\Action\Context;
11+
use Magento\Framework\App\Action\HttpGetActionInterface;
1112
use Magento\Framework\App\Action\HttpPostActionInterface;
1213
use Magento\Framework\Controller\Result\Redirect;
1314
use Magento\Framework\Data\Form\FormKey\Validator;
@@ -20,9 +21,6 @@
2021
use Magento\Framework\App\Response\RedirectInterface;
2122
use Magento\Framework\Controller\ResultInterface;
2223
use Magento\Wishlist\Controller\WishlistProviderInterface;
23-
use Magento\Framework\App\Action\HttpGetActionInterface;
24-
use Magento\Wishlist\Model\DataSerializer;
25-
use Magento\Framework\Data\Form\FormKey;
2624

2725
/**
2826
* Wish list Add controller
@@ -61,16 +59,6 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPost
6159
*/
6260
private $urlBuilder;
6361

64-
/**
65-
* @var DataSerializer
66-
*/
67-
private $dataSerializer;
68-
69-
/**
70-
* @var FormKey
71-
*/
72-
private $formKey;
73-
7462
/**
7563
* @param Context $context
7664
* @param Session $customerSession
@@ -79,8 +67,6 @@ class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPost
7967
* @param Validator $formKeyValidator
8068
* @param RedirectInterface|null $redirect
8169
* @param UrlInterface|null $urlBuilder
82-
* @param DataSerializer|null $dataSerializer
83-
* @param FormKey|null $formKey
8470
*/
8571
public function __construct(
8672
Context $context,
@@ -89,18 +75,14 @@ public function __construct(
8975
ProductRepositoryInterface $productRepository,
9076
Validator $formKeyValidator,
9177
?RedirectInterface $redirect = null,
92-
?UrlInterface $urlBuilder = null,
93-
?DataSerializer $dataSerializer = null,
94-
?FormKey $formKey = null
78+
?UrlInterface $urlBuilder = null
9579
) {
9680
$this->_customerSession = $customerSession;
9781
$this->wishlistProvider = $wishlistProvider;
9882
$this->productRepository = $productRepository;
9983
$this->formKeyValidator = $formKeyValidator;
10084
$this->redirect = $redirect ?: ObjectManager::getInstance()->get(RedirectInterface::class);
10185
$this->urlBuilder = $urlBuilder ?: ObjectManager::getInstance()->get(UrlInterface::class);
102-
$this->dataSerializer = $dataSerializer ?: ObjectManager::getInstance()->get(DataSerializer::class);
103-
$this->formKey = $formKey ?: ObjectManager::getInstance()->get(FormKey::class);
10486
parent::__construct($context);
10587
}
10688

@@ -120,20 +102,6 @@ public function execute()
120102
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
121103
$session = $this->_customerSession;
122104
$requestParams = $this->getRequest()->getParams();
123-
124-
if ($session->getBeforeWishlistRequest()) {
125-
$requestParams = $session->getBeforeWishlistRequest();
126-
$session->unsBeforeWishlistRequest();
127-
$this->getRequest()->setParam('form_key', $requestParams['form_key']);
128-
}
129-
130-
if (isset($requestParams['token'])) {
131-
$wishlistRequestBeforeLogin = $this->dataSerializer->unserialize($requestParams['token']);
132-
$requestParams['product'] = isset($wishlistRequestBeforeLogin['product']) ?
133-
(int)$wishlistRequestBeforeLogin['product'] : null;
134-
$this->getRequest()->setParam('form_key', $this->formKey->getFormKey());
135-
}
136-
137105
if (!$this->formKeyValidator->validate($this->getRequest())) {
138106
return $resultRedirect->setPath('*/');
139107
}

app/code/Magento/Wishlist/Controller/Index/Plugin.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
namespace Magento\Wishlist\Controller\Index;
77

88
use Magento\Customer\Model\Session as CustomerSession;
9+
use Magento\Framework\Data\Form\FormKey;
910
use Magento\Framework\Exception\NotFoundException;
1011
use Magento\Framework\App\Config\ScopeConfigInterface;
1112
use Magento\Framework\App\RequestInterface;
1213
use Magento\Framework\App\Response\RedirectInterface;
1314
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Wishlist\Model\DataSerializer;
1416

1517
/**
1618
* Wishlist plugin before dispatch
@@ -42,25 +44,41 @@ class Plugin
4244
*/
4345
private $messageManager;
4446

47+
/**
48+
* @var DataSerializer
49+
*/
50+
private $dataSerializer;
51+
52+
/**
53+
* @var FormKey
54+
*/
55+
private $formKey;
56+
4557
/**
4658
* @param CustomerSession $customerSession
4759
* @param \Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState
4860
* @param ScopeConfigInterface $config
4961
* @param RedirectInterface $redirector
5062
* @param \Magento\Framework\Message\ManagerInterface $messageManager
63+
* @param DataSerializer $dataSerializer
64+
* @param FormKey $formKey
5165
*/
5266
public function __construct(
5367
CustomerSession $customerSession,
5468
\Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState,
5569
ScopeConfigInterface $config,
5670
RedirectInterface $redirector,
57-
\Magento\Framework\Message\ManagerInterface $messageManager
71+
\Magento\Framework\Message\ManagerInterface $messageManager,
72+
DataSerializer $dataSerializer,
73+
FormKey $formKey
5874
) {
5975
$this->customerSession = $customerSession;
6076
$this->authenticationState = $authenticationState;
6177
$this->config = $config;
6278
$this->redirector = $redirector;
6379
$this->messageManager = $messageManager;
80+
$this->dataSerializer = $dataSerializer;
81+
$this->formKey = $formKey;
6482
}
6583

6684
/**
@@ -70,6 +88,7 @@ public function __construct(
7088
* @param RequestInterface $request
7189
* @return void
7290
* @throws \Magento\Framework\Exception\NotFoundException
91+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
7392
*/
7493
public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject, RequestInterface $request)
7594
{
@@ -86,9 +105,22 @@ public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject,
86105
$this->customerSession->setBeforeControllerName('index');
87106
$this->customerSession->setBeforeAction('add');
88107

89-
if ($request->getActionName() == 'add') {
108+
if ($request->getActionName() === 'add') {
90109
$this->messageManager->addErrorMessage(__('You must login or register to add items to your wishlist.'));
91110
}
111+
} elseif ($this->customerSession->authenticate()) {
112+
if ($this->customerSession->getBeforeWishlistRequest()) {
113+
$request->setParams($this->customerSession->getBeforeWishlistRequest());
114+
$this->customerSession->unsBeforeWishlistRequest();
115+
} elseif ($request->getParam('token')) {
116+
// check if the token is valid and retrieve the data
117+
$data = $this->dataSerializer->unserialize($request->getParam('token'));
118+
// Bypass CSRF validation if the token is valid
119+
if ($data) {
120+
$data['form_key'] = $this->formKey->getFormKey();
121+
$request->setParams($data);
122+
}
123+
}
92124
}
93125
if (!$this->config->isSetFlag('wishlist/general/active', ScopeInterface::SCOPE_STORES)) {
94126
throw new NotFoundException(__('Page not found.'));

app/code/Magento/Wishlist/Test/Unit/Controller/Index/PluginTest.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
use Magento\Framework\App\Config\ScopeConfigInterface;
1515
use Magento\Framework\App\Request\Http;
1616
use Magento\Framework\App\Response\RedirectInterface;
17+
use Magento\Framework\Data\Form\FormKey;
1718
use Magento\Framework\Message\ManagerInterface;
1819
use Magento\Store\App\Response\Redirect;
1920
use Magento\Store\Model\ScopeInterface;
2021
use Magento\Wishlist\Controller\Index\Index;
2122
use Magento\Wishlist\Controller\Index\Plugin;
2223
use Magento\Wishlist\Model\AuthenticationState;
2324
use Magento\Wishlist\Model\AuthenticationStateInterface;
25+
use Magento\Wishlist\Model\DataSerializer;
2426
use PHPUnit\Framework\MockObject\MockObject;
2527
use PHPUnit\Framework\TestCase;
2628

@@ -61,6 +63,16 @@ class PluginTest extends TestCase
6163
*/
6264
protected $request;
6365

66+
/**
67+
* @var DataSerializer|MockObject
68+
*/
69+
private $dataSerializer;
70+
71+
/**
72+
* @var FormKey|MockObject
73+
*/
74+
private $formKey;
75+
6476
/**
6577
* @inheritdoc
6678
*/
@@ -87,21 +99,8 @@ protected function setUp(): void
8799
$this->redirector = $this->createMock(Redirect::class);
88100
$this->messageManager = $this->getMockForAbstractClass(ManagerInterface::class);
89101
$this->request = $this->createMock(Http::class);
90-
}
91-
92-
/**
93-
* @inheritdoc
94-
*/
95-
protected function tearDown(): void
96-
{
97-
unset(
98-
$this->customerSession,
99-
$this->authenticationState,
100-
$this->config,
101-
$this->redirector,
102-
$this->messageManager,
103-
$this->request
104-
);
102+
$this->dataSerializer = $this->createMock(DataSerializer::class);
103+
$this->formKey = $this->createMock(FormKey::class);
105104
}
106105

107106
/**
@@ -114,7 +113,9 @@ protected function getPlugin()
114113
$this->authenticationState,
115114
$this->config,
116115
$this->redirector,
117-
$this->messageManager
116+
$this->messageManager,
117+
$this->dataSerializer,
118+
$this->formKey
118119
);
119120
}
120121

dev/tests/integration/testsuite/Magento/Wishlist/Controller/Index/AddTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ public function testAddToWishlistOnCustomerConfirmation(): void
242242
$this->customerRepository->save($customer);
243243
$this->assertEquals(null, $customer->getConfirmation());
244244
$this->customerSession->setCustomerId((int)$customer->getId());
245-
$this->performAddToWishListRequest(['token' => $token]);
245+
$this->getRequest()->setParams(['token' => $token])->setMethod(HttpRequest::METHOD_GET);
246+
$this->dispatch('wishlist/index/add');
246247
$this->assertSuccess((int)$customer->getId(), 1, $product->getName());
247248
}
248249

0 commit comments

Comments
 (0)