Skip to content

Commit 7eb4cea

Browse files
committed
MC-35618: Unexpected results of editing a Grouped Product in the Wish List by a Customer
1 parent 1586e8d commit 7eb4cea

File tree

6 files changed

+54
-29
lines changed

6 files changed

+54
-29
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ public function execute()
112112

113113
try {
114114
$buyRequest = new \Magento\Framework\DataObject($requestParams);
115-
$buyRequest->setData('action', 'add');
116115

117116
$result = $wishlist->addNewItem($product, $buyRequest);
118117
if (is_string($result)) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ public function execute()
104104
}
105105

106106
$buyRequest = new \Magento\Framework\DataObject($this->getRequest()->getParams());
107-
$buyRequest->setData('action', 'updateItem');
108107

109108
$wishlist->updateItem($id, $buyRequest)->save();
110109

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ public function addNewItem($product, $buyRequest = null, $forciblySetQty = false
492492
} else {
493493
$_buyRequest = new DataObject();
494494
}
495+
if ($_buyRequest->getData('action') !== 'updateItem') {
496+
$_buyRequest->setData('action', 'add');
497+
}
495498

496499
/* @var $product Product */
497500
$cartCandidates = $product->getTypeInstance()->processConfiguration($_buyRequest, clone $product);
@@ -750,15 +753,16 @@ public function updateItem($itemId, $buyRequest, $params = null)
750753
}
751754
$params->setCurrentConfig($item->getBuyRequest());
752755
$buyRequest = $this->_catalogProduct->addParamsToBuyRequest($buyRequest, $params);
756+
$buyRequest->setData('action', 'updateItem');
753757

754758
$product->setWishlistStoreId($item->getStoreId());
755759
$items = $this->getItemCollection();
756760
$isForceSetQuantity = true;
757-
foreach ($items as $_item) {
758-
/* @var $_item Item */
759-
if ($_item->getProductId() == $product->getId()
760-
&& $_item->getId() != $item->getId()
761-
&& $_item->representProduct($product)
761+
foreach ($items as $wishlistItem) {
762+
/* @var $wishlistItem Item */
763+
if ($wishlistItem->getProductId() == $product->getId()
764+
&& $wishlistItem->getId() != $item->getId()
765+
&& $wishlistItem->representProduct($product)
762766
) {
763767
// We do not add new wishlist item, but updating the existing one
764768
$isForceSetQuantity = false;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function testExecuteAddSuccessException()
380380
$wishlist
381381
->expects($this->once())
382382
->method('updateItem')
383-
->with(3, new DataObject(['action' => 'updateItem']))
383+
->with(3, new DataObject([]))
384384
->willReturnSelf();
385385
$wishlist
386386
->expects($this->once())
@@ -509,7 +509,7 @@ public function testExecuteAddSuccessCriticalException()
509509
$wishlist
510510
->expects($this->once())
511511
->method('updateItem')
512-
->with(3, new DataObject(['action' => 'updateItem']))
512+
->with(3, new DataObject([]))
513513
->willReturnSelf();
514514
$wishlist
515515
->expects($this->once())

app/code/Magento/Wishlist/Test/Unit/Model/WishlistTest.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -243,34 +243,22 @@ public function testLoadByCustomerId()
243243

244244
/**
245245
* @param int|Item|MockObject $itemId
246-
* @param DataObject $buyRequest
246+
* @param DataObject|MockObject $buyRequest
247247
* @param null|array|DataObject $param
248248
* @throws LocalizedException
249249
*
250250
* @dataProvider updateItemDataProvider
251251
*/
252-
public function testUpdateItem($itemId, $buyRequest, $param)
252+
public function testUpdateItem($itemId, $buyRequest, $param): void
253253
{
254254
$storeId = 1;
255255
$productId = 1;
256256
$stores = [(new DataObject())->setId($storeId)];
257257

258-
$newItem = $this->getMockBuilder(Item::class)
259-
->setMethods(
260-
['setProductId', 'setWishlistId', 'setStoreId', 'setOptions', 'setProduct', 'setQty', 'getItem', 'save']
261-
)
262-
->disableOriginalConstructor()
263-
->getMock();
264-
$newItem->expects($this->any())->method('setProductId')->willReturnSelf();
265-
$newItem->expects($this->any())->method('setWishlistId')->willReturnSelf();
266-
$newItem->expects($this->any())->method('setStoreId')->willReturnSelf();
267-
$newItem->expects($this->any())->method('setOptions')->willReturnSelf();
268-
$newItem->expects($this->any())->method('setProduct')->willReturnSelf();
269-
$newItem->expects($this->any())->method('setQty')->willReturnSelf();
270-
$newItem->expects($this->any())->method('getItem')->willReturn(2);
271-
$newItem->expects($this->any())->method('save')->willReturnSelf();
258+
$newItem = $this->prepareWishlistItem();
272259

273260
$this->itemFactory->expects($this->once())->method('create')->willReturn($newItem);
261+
$this->productHelper->expects($this->once())->method('addParamsToBuyRequest')->willReturn($buyRequest);
274262

275263
$this->storeManager->expects($this->any())->method('getStores')->willReturn($stores);
276264
$this->storeManager->expects($this->any())->method('getStore')->willReturn($stores[0]);
@@ -355,13 +343,48 @@ public function testUpdateItem($itemId, $buyRequest, $param)
355343
);
356344
}
357345

346+
/**
347+
* Prepare wishlist item mock.
348+
*
349+
* @return MockObject
350+
*/
351+
private function prepareWishlistItem(): MockObject
352+
{
353+
$newItem = $this->getMockBuilder(Item::class)
354+
->setMethods(
355+
['setProductId', 'setWishlistId', 'setStoreId', 'setOptions', 'setProduct', 'setQty', 'getItem', 'save']
356+
)
357+
->disableOriginalConstructor()
358+
->getMock();
359+
$newItem->expects($this->any())->method('setProductId')->willReturnSelf();
360+
$newItem->expects($this->any())->method('setWishlistId')->willReturnSelf();
361+
$newItem->expects($this->any())->method('setStoreId')->willReturnSelf();
362+
$newItem->expects($this->any())->method('setOptions')->willReturnSelf();
363+
$newItem->expects($this->any())->method('setProduct')->willReturnSelf();
364+
$newItem->expects($this->any())->method('setQty')->willReturnSelf();
365+
$newItem->expects($this->any())->method('getItem')->willReturn(2);
366+
$newItem->expects($this->any())->method('save')->willReturnSelf();
367+
368+
return $newItem;
369+
}
370+
358371
/**
359372
* @return array
360373
*/
361-
public function updateItemDataProvider()
374+
public function updateItemDataProvider(): array
362375
{
376+
$dataObjectMock = $this->createMock(DataObject::class);
377+
$dataObjectMock->expects($this->once())
378+
->method('setData')
379+
->with('action', 'updateItem')
380+
->willReturnSelf();
381+
$dataObjectMock->expects($this->once())
382+
->method('getData')
383+
->with('action')
384+
->willReturn('updateItem');
385+
363386
return [
364-
'0' => [1, new DataObject(), null]
387+
'0' => [1, $dataObjectMock, null]
365388
];
366389
}
367390

dev/tests/integration/testsuite/Magento/Wishlist/_files/wishlist_with_grouped_product.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
$objectManager = Bootstrap::getObjectManager();
2121
/** @var CustomerRegistry $customerRegistry */
22-
$customerRegistry = $objectManager->create(CustomerRegistry::class);
22+
$customerRegistry = $objectManager->get(CustomerRegistry::class);
2323
$customer = $customerRegistry->retrieve(1);
2424
$wishlistFactory = $objectManager->get(WishlistFactory::class);
2525
$wishlist = $wishlistFactory->create();
2626
$wishlist->loadByCustomerId($customer->getId(), true);
2727
/** @var ProductRepositoryInterface $productRepository */
28-
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
28+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
2929
$product = $productRepository->get('grouped');
3030
$simple1 = $productRepository->get('simple_11');
3131
$simple2 = $productRepository->get('simple_22');

0 commit comments

Comments
 (0)