From ab83cdbd9bf4896eb5f3acbd8388397c5a0fdd4b Mon Sep 17 00:00:00 2001 From: Nikolay Sumrak Date: Thu, 23 May 2019 14:42:31 +0300 Subject: [PATCH 1/3] Fixed apply discount coupons for bundle product Fixed apply discount coupons for bundle product with both virtual and simple product and "Ship Bundle Items" = "Separately" --- app/code/Magento/Quote/Model/Quote/Item.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Quote/Model/Quote/Item.php b/app/code/Magento/Quote/Model/Quote/Item.php index fe6d712500bcd..249d3690112a1 100644 --- a/app/code/Magento/Quote/Model/Quote/Item.php +++ b/app/code/Magento/Quote/Model/Quote/Item.php @@ -270,7 +270,7 @@ public function beforeSave() */ public function getAddress() { - if ($this->getQuote()->getItemsQty() == $this->getQuote()->getVirtualItemsQty()) { + if ($this->getQuote()->isVirtual()) { $address = $this->getQuote()->getBillingAddress(); } else { $address = $this->getQuote()->getShippingAddress(); From 072d9f72d4df2995f5cd0dadfcc7986ad942f7aa Mon Sep 17 00:00:00 2001 From: Yurii Sapiha Date: Thu, 20 Jun 2019 12:39:58 +0300 Subject: [PATCH 2/3] magento/magento2#22987 add integration test --- .../Magento/Quote/Model/QuoteTest.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteTest.php b/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteTest.php index 3667f4cc37fab..be3f647b822d8 100644 --- a/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteTest.php +++ b/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteTest.php @@ -14,6 +14,7 @@ use Magento\Customer\Api\Data\CustomerInterfaceFactory; use Magento\Customer\Model\Data\Customer; use Magento\Framework\Exception\LocalizedException; +use Magento\Quote\Api\Data\AddressInterface; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\ObjectManager; use Magento\Framework\Api\SearchCriteriaBuilder; @@ -72,6 +73,46 @@ public function testCollectTotalsWithVirtual(): void $this->assertEquals(20, $quote->getBaseGrandTotal()); } + /** + * @magentoDataFixture Magento/Catalog/_files/product_virtual.php + * @magentoDataFixture Magento/Quote/_files/empty_quote.php + * @return void + */ + public function testGetAddressWithVirtualProduct() + { + /** @var Quote $quote */ + $quote = $this->objectManager->create(Quote::class); + $quote->load('reserved_order_id_1', 'reserved_order_id'); + $billingAddress = $this->objectManager->create(AddressInterface::class); + $billingAddress->setFirstname('Joe') + ->setLastname('Doe') + ->setCountryId('US') + ->setRegion('TX') + ->setCity('Austin') + ->setStreet('1000 West Parmer Line') + ->setPostcode('11501') + ->setTelephone('123456789'); + $quote->setBillingAddress($billingAddress); + $shippingAddress = $this->objectManager->create(AddressInterface::class); + $shippingAddress->setFirstname('Joe') + ->setLastname('Doe') + ->setCountryId('US') + ->setRegion('NJ') + ->setCity('Newark') + ->setStreet('2775 Granville Lane') + ->setPostcode('07102') + ->setTelephone('9734685221'); + $quote->setShippingAddress($shippingAddress); + $productRepository = $this->objectManager->create( + ProductRepositoryInterface::class + ); + $product = $productRepository->get('virtual-product', false, null, true); + $quote->addProduct($product); + $quote->save(); + $expectedAddress = $quote->getBillingAddress(); + $this->assertEquals($expectedAddress, $quote->getAllItems()[0]->getAddress()); + } + /** * @return void */ From a2a4e2c473abae32d55cb2a07278e3583bbeabe8 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Thu, 27 Jun 2019 12:33:34 +0300 Subject: [PATCH 3/3] magento/magento2#22987: Static and unit tests fix. --- .../Model/Quote/Address/FreeShippingTest.php | 30 +++++++------ app/code/Magento/Quote/Model/Quote/Item.php | 39 +++++++++-------- .../Quote/Test/Unit/Model/Quote/ItemTest.php | 43 +++++++++---------- 3 files changed, 60 insertions(+), 52 deletions(-) diff --git a/app/code/Magento/OfflineShipping/Test/Unit/Model/Quote/Address/FreeShippingTest.php b/app/code/Magento/OfflineShipping/Test/Unit/Model/Quote/Address/FreeShippingTest.php index 8266a39bbbb6d..1f4fa5245c48a 100644 --- a/app/code/Magento/OfflineShipping/Test/Unit/Model/Quote/Address/FreeShippingTest.php +++ b/app/code/Magento/OfflineShipping/Test/Unit/Model/Quote/Address/FreeShippingTest.php @@ -16,6 +16,9 @@ use Magento\Store\Model\StoreManagerInterface; use PHPUnit_Framework_MockObject_MockObject as MockObject; +/** + * Test for Magento\OfflineShipping\Model\Quote\Address\FreeShipping class. + */ class FreeShippingTest extends \PHPUnit\Framework\TestCase { private static $websiteId = 1; @@ -77,12 +80,14 @@ public function testIsFreeShipping(int $addressFree, int $fItemFree, int $sItemF [$fItem], [$sItem] ) - ->willReturnCallback(function () use ($fItem, $sItem, $addressFree, $fItemFree, $sItemFree) { - // emulate behavior of cart rule calculator - $fItem->getAddress()->setFreeShipping($addressFree); - $fItem->setFreeShipping($fItemFree); - $sItem->setFreeShipping($sItemFree); - }); + ->willReturnCallback( + function () use ($fItem, $sItem, $addressFree, $fItemFree, $sItemFree) { + // emulate behavior of cart rule calculator + $fItem->getAddress()->setFreeShipping($addressFree); + $fItem->setFreeShipping($fItemFree); + $sItem->setFreeShipping($sItemFree); + } + ); $actual = $this->model->isFreeShipping($quote, $items); self::assertEquals($expected, $actual); @@ -133,8 +138,11 @@ private function getQuote(Address $address): Quote ->disableOriginalConstructor() ->setMethods( [ - 'getCouponCode', 'getCustomerGroupId', 'getShippingAddress', 'getStoreId', 'getItemsQty', - 'getVirtualItemsQty' + 'getCouponCode', + 'getCustomerGroupId', + 'getShippingAddress', + 'getStoreId', + 'isVirtual' ] ) ->getMock(); @@ -147,10 +155,8 @@ private function getQuote(Address $address): Quote ->willReturn(self::$couponCode); $quote->method('getShippingAddress') ->willReturn($address); - $quote->method('getItemsQty') - ->willReturn(2); - $quote->method('getVirtualItemsQty') - ->willReturn(0); + $quote->method('isVirtual') + ->willReturn(false); return $quote; } diff --git a/app/code/Magento/Quote/Model/Quote/Item.php b/app/code/Magento/Quote/Model/Quote/Item.php index 249d3690112a1..2d6ff67018160 100644 --- a/app/code/Magento/Quote/Model/Quote/Item.php +++ b/app/code/Magento/Quote/Model/Quote/Item.php @@ -282,7 +282,7 @@ public function getAddress() /** * Declare quote model object * - * @param \Magento\Quote\Model\Quote $quote + * @param \Magento\Quote\Model\Quote $quote * @return $this */ public function setQuote(\Magento\Quote\Model\Quote $quote) @@ -719,6 +719,7 @@ public function getOptionByCode($code) /** * Checks that item model has data changes. + * * Call save item options if model isn't need to save in DB * * @return boolean @@ -813,8 +814,9 @@ public function __clone() } /** - * Returns formatted buy request - object, holding request received from - * product view page with keys and options for configured product + * Get formatted buy request. + * + * Returns object, holding request received from product view page with keys and options for configured product. * * @return \Magento\Framework\DataObject */ @@ -863,6 +865,7 @@ public function setHasError($flag) /** * Clears list of errors, associated with this quote item. + * * Also automatically removes error-flag from oneself. * * @return $this @@ -876,6 +879,7 @@ protected function _clearErrorInfo() /** * Adds error information to the quote item. + * * Automatically sets error flag. * * @param string|null $origin Usually a name of module, that embeds error @@ -930,9 +934,8 @@ public function removeErrorInfosByParams($params) } /** + * @inheritdoc * @codeCoverageIgnoreStart - * - * {@inheritdoc} */ public function getItemId() { @@ -940,7 +943,7 @@ public function getItemId() } /** - * {@inheritdoc} + * @inheritdoc */ public function setItemId($itemID) { @@ -948,7 +951,7 @@ public function setItemId($itemID) } /** - * {@inheritdoc} + * @inheritdoc */ public function getSku() { @@ -956,7 +959,7 @@ public function getSku() } /** - * {@inheritdoc} + * @inheritdoc */ public function setSku($sku) { @@ -964,7 +967,7 @@ public function setSku($sku) } /** - * {@inheritdoc} + * @inheritdoc */ public function getQty() { @@ -972,7 +975,7 @@ public function getQty() } /** - * {@inheritdoc} + * @inheritdoc */ public function getName() { @@ -980,7 +983,7 @@ public function getName() } /** - * {@inheritdoc} + * @inheritdoc */ public function setName($name) { @@ -988,7 +991,7 @@ public function setName($name) } /** - * {@inheritdoc} + * @inheritdoc */ public function getPrice() { @@ -996,7 +999,7 @@ public function getPrice() } /** - * {@inheritdoc} + * @inheritdoc */ public function setPrice($price) { @@ -1004,7 +1007,7 @@ public function setPrice($price) } /** - * {@inheritdoc} + * @inheritdoc */ public function setProductType($productType) { @@ -1012,7 +1015,7 @@ public function setProductType($productType) } /** - * {@inheritdoc} + * @inheritdoc */ public function getQuoteId() { @@ -1020,7 +1023,7 @@ public function getQuoteId() } /** - * {@inheritdoc} + * @inheritdoc */ public function setQuoteId($quoteId) { @@ -1051,7 +1054,7 @@ public function setProductOption(\Magento\Quote\Api\Data\ProductOptionInterface //@codeCoverageIgnoreEnd /** - * {@inheritdoc} + * @inheritdoc * * @return \Magento\Quote\Api\Data\CartItemExtensionInterface|null */ @@ -1061,7 +1064,7 @@ public function getExtensionAttributes() } /** - * {@inheritdoc} + * @inheritdoc * * @param \Magento\Quote\Api\Data\CartItemExtensionInterface $extensionAttributes * @return $this diff --git a/app/code/Magento/Quote/Test/Unit/Model/Quote/ItemTest.php b/app/code/Magento/Quote/Test/Unit/Model/Quote/ItemTest.php index ef529f643d6d4..4c4f40da5f78f 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/Quote/ItemTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/Quote/ItemTest.php @@ -135,27 +135,24 @@ protected function setUp() public function testGetAddress() { $quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class) - ->setMethods(['getShippingAddress', 'getBillingAddress', 'getStoreId', '__wakeup']) + ->setMethods(['getShippingAddress', 'getBillingAddress', 'getStoreId', '__wakeup', 'isVirtual']) ->disableOriginalConstructor() ->getMock(); $quote->expects($this->once()) ->method('getShippingAddress') - ->will($this->returnValue('shipping')); + ->willReturn('shipping'); $quote->expects($this->once()) ->method('getBillingAddress') - ->will($this->returnValue('billing')); + ->willReturn('billing'); $quote->expects($this->any()) ->method('getStoreId') - ->will($this->returnValue(1)); + ->willReturn(1); + $quote->expects($this->exactly(2)) + ->method('isVirtual') + ->willReturnOnConsecutiveCalls(false, true); $this->model->setQuote($quote); - - $quote->setItemsQty(2); - $quote->setVirtualItemsQty(1); $this->assertEquals('shipping', $this->model->getAddress(), 'Wrong shipping address'); - - $quote->setItemsQty(2); - $quote->setVirtualItemsQty(2); $this->assertEquals('billing', $this->model->getAddress(), 'Wrong billing address'); } @@ -855,18 +852,20 @@ public function testSetOptionsWithNull() private function createOptionMock($optionCode, $optionData = []) { $optionMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class) - ->setMethods([ - 'setData', - 'setItem', - 'getItem', - 'getCode', - '__wakeup', - 'isDeleted', - 'delete', - 'getValue', - 'getProduct', - 'save' - ]) + ->setMethods( + [ + 'setData', + 'setItem', + 'getItem', + 'getCode', + '__wakeup', + 'isDeleted', + 'delete', + 'getValue', + 'getProduct', + 'save' + ] + ) ->disableOriginalConstructor() ->getMock(); $optionMock->expects($this->any())