From bb997b14041c9ff9fb7b8dbebe675fa791c6e4d2 Mon Sep 17 00:00:00 2001 From: Sviatoslav Mankivskyi <smankivskyi@ebay.com> Date: Mon, 6 Jul 2015 16:06:29 +0300 Subject: [PATCH] MAGETWO-39108: Shopping Cart becomes empty if Customer updates the item to be the same as already existent in Shopping Cart --- .../Quote/Model/Quote/Item/Processor.php | 2 +- .../Unit/Model/Quote/Item/ProcessorTest.php | 161 ++++++++++++++++-- 2 files changed, 150 insertions(+), 13 deletions(-) diff --git a/app/code/Magento/Quote/Model/Quote/Item/Processor.php b/app/code/Magento/Quote/Model/Quote/Item/Processor.php index 471e7ef9a36..ad17f5a8ea5 100644 --- a/app/code/Magento/Quote/Model/Quote/Item/Processor.php +++ b/app/code/Magento/Quote/Model/Quote/Item/Processor.php @@ -94,7 +94,7 @@ class Processor /** * We specify qty after we know about parent (for stock) */ - if ($request->getResetCount() && !$candidate->getStickWithinParent() && $item->getId() === $request->getId()) { + if ($request->getResetCount() && !$candidate->getStickWithinParent() && $item->getId() == $request->getId()) { $item->setData(CartItemInterface::KEY_QTY, 0); } $item->addQty($candidate->getCartQty()); diff --git a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/ProcessorTest.php b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/ProcessorTest.php index b27d2ca465a..84864eab629 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/ProcessorTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/Quote/Item/ProcessorTest.php @@ -72,7 +72,16 @@ class ProcessorTest extends \PHPUnit_Framework_TestCase $this->itemMock = $this->getMock( 'Magento\Quote\Model\Quote\Item', - ['getId', 'setOptions', '__wakeup', 'setProduct', 'addQty', 'setCustomPrice', 'setOriginalCustomPrice'], + [ + 'getId', + 'setOptions', + '__wakeup', + 'setProduct', + 'addQty', + 'setCustomPrice', + 'setOriginalCustomPrice', + 'setData' + ], [], '', false @@ -109,7 +118,7 @@ class ProcessorTest extends \PHPUnit_Framework_TestCase $this->productMock = $this->getMock( 'Magento\Catalog\Model\Product', - ['getCustomOptions', '__wakeup', 'getParentProductId', 'getCartQty'], + ['getCustomOptions', '__wakeup', 'getParentProductId', 'getCartQty', 'getStickWithinParent'], [], '', false @@ -148,8 +157,12 @@ class ProcessorTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue($itemId)); $this->itemMock->expects($this->any()) ->method('setData') - ->with($this->equalTo('qty'), $this->equalTo(0)); - + ->willReturnMap( + [ + ['store_id', $storeId], + ['qty', 0], + ] + ); $this->storeMock->expects($this->any()) ->method('getId') @@ -177,7 +190,6 @@ class ProcessorTest extends \PHPUnit_Framework_TestCase ->method('getParentProductId') ->will($this->returnValue(true)); - $this->itemMock->expects($this->never())->method('setOptions'); $this->itemMock->expects($this->never())->method('setProduct'); @@ -185,7 +197,13 @@ class ProcessorTest extends \PHPUnit_Framework_TestCase ->method('getId') ->will($this->returnValue($itemId)); - $this->itemMock->expects($this->never())->method('setData'); + $this->itemMock->expects($this->any()) + ->method('setData') + ->willReturnMap( + [ + ['store_id', $storeId], + ] + ); $this->storeMock->expects($this->any()) ->method('getId') @@ -222,7 +240,13 @@ class ProcessorTest extends \PHPUnit_Framework_TestCase ->method('getId') ->will($this->returnValue($itemId)); - $this->itemMock->expects($this->never())->method('setData'); + $this->itemMock->expects($this->any()) + ->method('setData') + ->willReturnMap( + [ + ['store_id', $storeId], + ] + ); $this->storeMock->expects($this->any()) ->method('getId') @@ -238,58 +262,171 @@ class ProcessorTest extends \PHPUnit_Framework_TestCase { $qty = 3000000000; $customPrice = 400000000; + $itemId = 1; + $requestItemId = 1; $this->productMock->expects($this->any()) ->method('getCartQty') ->will($this->returnValue($qty)); + $this->productMock->expects($this->any()) + ->method('getStickWithinParent') + ->will($this->returnValue(false)); - $this->itemMock->expects($this->any()) + $this->itemMock->expects($this->once()) ->method('addQty') ->with($qty); + $this->itemMock->expects($this->any()) + ->method('getId') + ->will($this->returnValue($itemId)); + $this->itemMock->expects($this->never()) + ->method('setData'); $this->objectMock->expects($this->any()) ->method('getCustomPrice') ->will($this->returnValue($customPrice)); + $this->objectMock->expects($this->any()) + ->method('getResetCount') + ->will($this->returnValue(false)); + $this->objectMock->expects($this->any()) + ->method('getId') + ->will($this->returnValue($requestItemId)); + + $this->itemMock->expects($this->once()) + ->method('setCustomPrice') + ->will($this->returnValue($customPrice)); + $this->itemMock->expects($this->once()) + ->method('setOriginalCustomPrice') + ->will($this->returnValue($customPrice)); + $this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock); + } + + public function testPrepareWithResetCountAndStick() + { + $qty = 3000000000; + $customPrice = 400000000; + $itemId = 1; + $requestItemId = 1; + + $this->productMock->expects($this->any()) + ->method('getCartQty') + ->will($this->returnValue($qty)); + $this->productMock->expects($this->any()) + ->method('getStickWithinParent') + ->will($this->returnValue(true)); + + $this->itemMock->expects($this->once()) + ->method('addQty') + ->with($qty); $this->itemMock->expects($this->any()) + ->method('getId') + ->will($this->returnValue($itemId)); + $this->itemMock->expects($this->never()) + ->method('setData'); + + $this->objectMock->expects($this->any()) + ->method('getCustomPrice') + ->will($this->returnValue($customPrice)); + $this->objectMock->expects($this->any()) + ->method('getResetCount') + ->will($this->returnValue(true)); + $this->objectMock->expects($this->any()) + ->method('getId') + ->will($this->returnValue($requestItemId)); + + $this->itemMock->expects($this->once()) ->method('setCustomPrice') ->will($this->returnValue($customPrice)); + $this->itemMock->expects($this->once()) + ->method('setOriginalCustomPrice') + ->will($this->returnValue($customPrice)); + + $this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock); + } + + public function testPrepareWithResetCountAndNotStickAndOtherItemId() + { + $qty = 3000000000; + $customPrice = 400000000; + $itemId = 1; + $requestItemId = 2; + + $this->productMock->expects($this->any()) + ->method('getCartQty') + ->will($this->returnValue($qty)); + $this->productMock->expects($this->any()) + ->method('getStickWithinParent') + ->will($this->returnValue(false)); + + $this->itemMock->expects($this->once()) + ->method('addQty') + ->with($qty); $this->itemMock->expects($this->any()) + ->method('getId') + ->will($this->returnValue($itemId)); + $this->itemMock->expects($this->never()) + ->method('setData'); + + $this->objectMock->expects($this->any()) + ->method('getCustomPrice') + ->will($this->returnValue($customPrice)); + $this->objectMock->expects($this->any()) + ->method('getResetCount') + ->will($this->returnValue(true)); + $this->objectMock->expects($this->any()) + ->method('getId') + ->will($this->returnValue($requestItemId)); + + $this->itemMock->expects($this->once()) + ->method('setCustomPrice') + ->will($this->returnValue($customPrice)); + $this->itemMock->expects($this->once()) ->method('setOriginalCustomPrice') ->will($this->returnValue($customPrice)); $this->processor->prepare($this->itemMock, $this->objectMock, $this->productMock); } - public function testPrepareResetCount() + public function testPrepareWithResetCountAndNotStickAndSameItemId() { $qty = 3000000000; $customPrice = 400000000; + $itemId = 1; + $requestItemId = 1; $this->objectMock->expects($this->any()) ->method('getResetCount') ->will($this->returnValue(true)); $this->itemMock->expects($this->any()) + ->method('getId') + ->will($this->returnValue($itemId)); + $this->itemMock->expects($this->once()) ->method('setData') ->with(CartItemInterface::KEY_QTY, 0); $this->productMock->expects($this->any()) ->method('getCartQty') ->will($this->returnValue($qty)); + $this->productMock->expects($this->any()) + ->method('getStickWithinParent') + ->will($this->returnValue(false)); - $this->itemMock->expects($this->any()) + $this->itemMock->expects($this->once()) ->method('addQty') ->with($qty); $this->objectMock->expects($this->any()) ->method('getCustomPrice') ->will($this->returnValue($customPrice)); + $this->objectMock->expects($this->any()) + ->method('getId') + ->will($this->returnValue($requestItemId)); - $this->itemMock->expects($this->any()) + $this->itemMock->expects($this->once()) ->method('setCustomPrice') ->will($this->returnValue($customPrice)); - $this->itemMock->expects($this->any()) + $this->itemMock->expects($this->once()) ->method('setOriginalCustomPrice') ->will($this->returnValue($customPrice)); -- GitLab