Skip to content

Commit 83130a0

Browse files
committed
ACP2E-341::Product is not getting added in wishlist from product list page and product view page when customer confirms account from confirmation email - Fixed Automation test & Modify Back URL
1 parent c956f29 commit 83130a0

File tree

3 files changed

+94
-90
lines changed

3 files changed

+94
-90
lines changed

app/code/Magento/Wishlist/Model/DataSerializer.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,21 @@ public function serialize(array $data): string
105105
*/
106106
public function unserialize(string $token): array
107107
{
108-
if (strlen($token) !== self::CACHE_ID_LENGTH) {
109-
$this->logger->error("Invalid Token '$token' supplied.");
110-
}
111-
112108
$result = [];
113-
$cacheKey = self::CACHE_KEY_PREFIX.$token;
114-
$json = $this->cache->load($cacheKey);
115-
if ($json) {
116-
$result = $this->serializer->unserialize($json);
117-
}
118-
119-
try {
120-
$this->cache->remove($token);
121-
} catch (Throwable $exception) {
122-
$this->logger->error('Unable to remove cache: '.$exception);
109+
if (strlen($token) === self::CACHE_ID_LENGTH) {
110+
$cacheKey = self::CACHE_KEY_PREFIX.$token;
111+
$json = $this->cache->load($cacheKey);
112+
if ($json) {
113+
$result = $this->serializer->unserialize($json);
114+
}
115+
116+
try {
117+
$this->cache->remove($token);
118+
} catch (Throwable $exception) {
119+
$this->logger->error('Unable to remove cache: '.$exception);
120+
}
121+
} else {
122+
$this->logger->error("Invalid Token '$token' supplied.");
123123
}
124124

125125
return $result;

app/code/Magento/Wishlist/Plugin/SaveWishlistDataAndAddReferenceKeyToBackUrl.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,19 @@ public function beforeNewAccount(
7272
$storeId = null,
7373
$sendemailStoreId = null
7474
): array {
75-
if ($this->customerSession->getBeforeWishlistRequest() != null
76-
&& $customer->getConfirmation() != null
75+
if (($this->customerSession->getBeforeWishlistRequest() != null)
76+
&& ($customer->getConfirmation() != null)
77+
&& (strpos($backUrl, 'wishlist/index/add') !== false)
7778
) {
7879
$token = $this->dataSerializer->serialize($this->customerSession->getBeforeWishlistRequest());
79-
$backUrl = $this->urlBuilder->getUrl('wishlist/index/add', ['_query' => ['token' => $token]]);
80+
$query = [];
81+
if (strpos($backUrl, '?') !== false) {
82+
$queryString = explode('?', $backUrl);
83+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
84+
parse_str($queryString[1], $query);
85+
}
86+
$query['token'] = $token;
87+
$backUrl = $this->urlBuilder->getUrl('wishlist/index/add', ['_query' => $query]);
8088
}
8189

8290
return [$customer, $type, $backUrl, $storeId, $sendemailStoreId];

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

Lines changed: 69 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -172,48 +172,6 @@ public function testAddToNotExistingWishList(): void
172172
$this->assert404NotFound();
173173
}
174174

175-
/**
176-
* Perform request add item to wish list.
177-
*
178-
* @param array $params
179-
* @return void
180-
*/
181-
private function performAddToWishListRequest(array $params): void
182-
{
183-
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
184-
$this->dispatch('wishlist/index/add');
185-
}
186-
187-
/**
188-
* Assert success response and items count.
189-
*
190-
* @param int $customerId
191-
* @param int $itemsCount
192-
* @param string $productName
193-
* @return void
194-
*/
195-
private function assertSuccess(int $customerId, int $itemsCount, string $productName): void
196-
{
197-
$expectedMessage = sprintf("\n%s has been added to your Wish List.", $productName)
198-
. " Click <a href=\"http://localhost/test\">here</a> to continue shopping.";
199-
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_SUCCESS);
200-
$wishlist = $this->getWishlistByCustomerId->execute($customerId);
201-
$this->assertCount($itemsCount, $wishlist->getItemCollection());
202-
$this->assertRedirect($this->stringContains('wishlist/index/index/wishlist_id/' . $wishlist->getId()));
203-
}
204-
205-
/**
206-
* Prepare referer to test.
207-
*
208-
* @return void
209-
*/
210-
private function prepareReferer(): void
211-
{
212-
$parameters = $this->_objectManager->create(Parameters::class);
213-
$parameters->set('HTTP_REFERER', 'http://localhost/test');
214-
$this->getRequest()->setServer($parameters);
215-
}
216-
217175
/**
218176
* Add Product to Wishlist Before Login, Create Customer & Send Confirmation Email
219177
*
@@ -229,8 +187,8 @@ public function testCreateCustomerWithEmailConfirmationAfterAddToWishlist(): voi
229187
$data = [];
230188
$data['product'] = (int) $product->getId();
231189
$this->customerSession->setBeforeWishlistRequest($data);
232-
$email = 'test_example_new@email.com';
233-
$this->fillRequestWithAccountData($email);
190+
$email = 'test_example_new31@email.com';
191+
$this->fillRequestWithCustomerData($email);
234192
$this->dispatch('customer/account/createPost');
235193
$this->assertRedirect($this->stringContains('customer/account/index'));
236194
$message = 'You must confirm your account.'
@@ -261,25 +219,8 @@ public function testCreateCustomerWithEmailConfirmationAfterAddToWishlist(): voi
261219
}
262220

263221
/**
264-
* Fills request with customer data.
265-
*
266-
* @param string $email
267-
* @return void
268-
*/
269-
private function fillRequestWithAccountData(string $email): void
270-
{
271-
$this->getRequest()
272-
->setMethod(HttpRequest::METHOD_POST)
273-
->setParam(CustomerInterface::FIRSTNAME, 'firstname1')
274-
->setParam(CustomerInterface::LASTNAME, 'lastname1')
275-
->setParam(CustomerInterface::EMAIL, $email)
276-
->setParam('password', '_Password1')
277-
->setParam('password_confirmation', '_Password1');
278-
}
279-
280-
/**
281-
* Save Wishlist Product Data into Cache, Prepare Customer Email Confirmation Link
282-
* Also Confirm the Customer & Add Product To Wishlist based on the data retrieved from Cache
222+
* Save Wishlist Product Data into Cache.
223+
* Also Add Product To Wishlist based on the data retrieved from Cache Token
283224
*
284225
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
285226
* @magentoDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
@@ -290,21 +231,76 @@ private function fillRequestWithAccountData(string $email): void
290231
*/
291232
public function testAddToWishlistOnCustomerConfirmation(): void
292233
{
234+
$this->prepareReferer();
293235
$product = $this->productRepository->get('simple');
294236
$data = [];
295237
$data['product'] = (int) $product->getId();
296-
$token = $this->dataSerializer->serialize($data);
297-
238+
$token = $this->dataSerializer->serialize($data);//Save into Cache
298239
$customer = $this->customerRepository->get('unconfirmedcustomer@example.com');
240+
$customer->setConfirmation(null);
241+
$this->customerRepository->save($customer);
242+
$this->assertEquals(null, $customer->getConfirmation());
243+
$this->customerSession->setCustomerId((int) $customer->getId());
244+
$this->performAddToWishListRequest(['token' => $token]);
245+
$this->assertSuccess((int) $customer->getId(), 1, $product->getName());
246+
}
299247

300-
$this->getRequest()
301-
->setParam('back_url', $this->urlBuilder->getUrl(
302-
'wishlist/index/add', ['_query' => ['token' => $token]]
303-
))
304-
->setParam('id', (int) $customer->getId())
305-
->setParam('key', $customer->getConfirmation());
248+
/**
249+
* Perform request add item to wish list.
250+
*
251+
* @param array $params
252+
* @return void
253+
*/
254+
private function performAddToWishListRequest(array $params): void
255+
{
256+
$this->getRequest()->setParams($params)->setMethod(HttpRequest::METHOD_POST);
257+
$this->dispatch('wishlist/index/add');
258+
}
306259

307-
$this->dispatch('customer/account/confirm');
308-
$this->assertSuccess((int) $customer->getId(), 1, $product->getName());
260+
/**
261+
* Assert success response and items count.
262+
*
263+
* @param int $customerId
264+
* @param int $itemsCount
265+
* @param string $productName
266+
* @return void
267+
*/
268+
private function assertSuccess(int $customerId, int $itemsCount, string $productName): void
269+
{
270+
$expectedMessage = sprintf("\n%s has been added to your Wish List.", $productName)
271+
. " Click <a href=\"http://localhost/test\">here</a> to continue shopping.";
272+
$this->assertSessionMessages($this->equalTo([(string)__($expectedMessage)]), MessageInterface::TYPE_SUCCESS);
273+
$wishlist = $this->getWishlistByCustomerId->execute($customerId);
274+
$this->assertCount($itemsCount, $wishlist->getItemCollection());
275+
$this->assertRedirect($this->stringContains('wishlist/index/index/wishlist_id/' . $wishlist->getId()));
276+
}
277+
278+
/**
279+
* Prepare referer to test.
280+
*
281+
* @return void
282+
*/
283+
private function prepareReferer(): void
284+
{
285+
$parameters = $this->_objectManager->create(Parameters::class);
286+
$parameters->set('HTTP_REFERER', 'http://localhost/test');
287+
$this->getRequest()->setServer($parameters);
288+
}
289+
290+
/**
291+
* Fills request with customer data.
292+
*
293+
* @param string $email
294+
* @return void
295+
*/
296+
private function fillRequestWithCustomerData(string $email): void
297+
{
298+
$this->getRequest()
299+
->setMethod(HttpRequest::METHOD_POST)
300+
->setParam(CustomerInterface::FIRSTNAME, 'firstname1')
301+
->setParam(CustomerInterface::LASTNAME, 'lastname1')
302+
->setParam(CustomerInterface::EMAIL, $email)
303+
->setParam('password', '_Password1')
304+
->setParam('password_confirmation', '_Password1');
309305
}
310306
}

0 commit comments

Comments
 (0)