Skip to content

Commit 2dc2fa7

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-97423' into 2.3-develop-pr20
2 parents 93fc029 + f2faf15 commit 2dc2fa7

File tree

5 files changed

+250
-41
lines changed

5 files changed

+250
-41
lines changed

app/code/Magento/Tax/Model/Sales/Total/Quote/CommonTaxCollector.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public function mapItems(
348348
$useBaseCurrency
349349
) {
350350
$items = $shippingAssignment->getItems();
351-
if (!count($items)) {
351+
if (empty($items)) {
352352
return [];
353353
}
354354

@@ -478,7 +478,7 @@ protected function prepareQuoteDetails(ShippingAssignmentInterface $shippingAssi
478478
{
479479
$items = $shippingAssignment->getItems();
480480
$address = $shippingAssignment->getShipping()->getAddress();
481-
if (!count($items)) {
481+
if (empty($items)) {
482482
return $this->quoteDetailsDataObjectFactory->create();
483483
}
484484

@@ -688,6 +688,9 @@ public function updateItemTaxInfo($quoteItem, $itemTaxDetails, $baseItemTaxDetai
688688
{
689689
//The price should be base price
690690
$quoteItem->setPrice($baseItemTaxDetails->getPrice());
691+
if ($quoteItem->getCustomPrice() && $this->taxHelper->applyTaxOnCustomPrice()) {
692+
$quoteItem->setCustomPrice($baseItemTaxDetails->getPrice());
693+
}
691694
$quoteItem->setConvertedPrice($itemTaxDetails->getPrice());
692695
$quoteItem->setPriceInclTax($itemTaxDetails->getPriceInclTax());
693696
$quoteItem->setRowTotal($itemTaxDetails->getRowTotal());

app/code/Magento/Tax/Test/Unit/Model/Sales/Total/Quote/CommonTaxCollectorTest.php

Lines changed: 101 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,106 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Tax\Test\Unit\Model\Sales\Total\Quote;
89

9-
/**
10-
* Test class for \Magento\Tax\Model\Sales\Total\Quote\Tax
11-
*/
1210
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Tax\Helper\Data as TaxHelper;
12+
use Magento\Tax\Api\Data\TaxDetailsItemInterface;
13+
use Magento\Quote\Model\Quote\Item as QuoteItem;
14+
use Magento\Store\Model\Store;
15+
use Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector;
16+
use Magento\Tax\Model\Config;
17+
use Magento\Quote\Model\Quote\Address as QuoteAddress;
18+
use Magento\Quote\Model\Quote;
19+
use Magento\Tax\Api\Data\QuoteDetailsItemInterface;
20+
use Magento\Tax\Api\Data\TaxClassKeyInterface;
21+
use Magento\Tax\Model\Sales\Quote\ItemDetails;
22+
use Magento\Tax\Model\TaxClass\Key as TaxClassKey;
23+
use Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory;
24+
use Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory;
25+
use Magento\Quote\Api\Data\ShippingAssignmentInterface;
26+
use Magento\Quote\Api\Data\ShippingInterface;
27+
use Magento\Quote\Model\Quote\Address\Total as QuoteAddressTotal;
28+
use PHPUnit\Framework\MockObject\MockObject;
29+
use PHPUnit\Framework\TestCase;
1330

1431
/**
32+
* Common tax collector test
33+
*
1534
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1635
*/
17-
class CommonTaxCollectorTest extends \PHPUnit\Framework\TestCase
36+
class CommonTaxCollectorTest extends TestCase
1837
{
1938
/**
20-
* @var \Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector
39+
* @var CommonTaxCollector
2140
*/
2241
private $commonTaxCollector;
2342

2443
/**
25-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Tax\Model\Config
44+
* @var MockObject|Config
2645
*/
2746
private $taxConfig;
2847

2948
/**
30-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\Quote\Address
49+
* @var MockObject|QuoteAddress
3150
*/
3251
private $address;
3352

3453
/**
35-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\Quote
54+
* @var MockObject|Quote
3655
*/
3756
private $quote;
3857

3958
/**
40-
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
59+
* @var MockObject|Store
4160
*/
4261
private $store;
4362

4463
/**
45-
* @var \PHPUnit_Framework_MockObject_MockObject|
64+
* @var MockObject
4665
*/
4766
protected $taxClassKeyDataObjectFactoryMock;
4867

4968
/**
50-
* @var \PHPUnit_Framework_MockObject_MockObject|
69+
* @var MockObject
5170
*/
5271
protected $quoteDetailsItemDataObjectFactoryMock;
5372

5473
/**
55-
* @var \Magento\Tax\Api\Data\QuoteDetailsItemInterface
74+
* @var QuoteDetailsItemInterface
5675
*/
5776
protected $quoteDetailsItemDataObject;
5877

5978
/**
60-
* @var \Magento\Tax\Api\Data\TaxClassKeyInterface
79+
* @var TaxClassKeyInterface
6180
*/
6281
protected $taxClassKeyDataObject;
6382

83+
/**
84+
* @var TaxHelper
85+
*/
86+
private $taxHelper;
87+
88+
/**
89+
* {@inheritdoc}
90+
*/
6491
protected function setUp()
6592
{
6693
$objectManager = new ObjectManager($this);
6794

68-
$this->taxConfig = $this->getMockBuilder(\Magento\Tax\Model\Config::class)
95+
$this->taxConfig = $this->getMockBuilder(Config::class)
6996
->disableOriginalConstructor()
70-
->setMethods(['getShippingTaxClass', 'shippingPriceIncludesTax'])
97+
->setMethods(['getShippingTaxClass', 'shippingPriceIncludesTax', 'discountTax'])
7198
->getMock();
7299

73-
$this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
100+
$this->store = $this->getMockBuilder(Store::class)
74101
->disableOriginalConstructor()
75102
->setMethods(['__wakeup'])
76103
->getMock();
77104

78-
$this->quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
105+
$this->quote = $this->getMockBuilder(Quote::class)
79106
->disableOriginalConstructor()
80107
->setMethods(['__wakeup', 'getStore'])
81108
->getMock();
@@ -84,52 +111,58 @@ protected function setUp()
84111
->method('getStore')
85112
->will($this->returnValue($this->store));
86113

87-
$this->address = $this->getMockBuilder(\Magento\Quote\Model\Quote\Address::class)
114+
$this->address = $this->getMockBuilder(QuoteAddress::class)
88115
->disableOriginalConstructor()
89116
->getMock();
90117

91118
$this->address->expects($this->any())
92119
->method('getQuote')
93120
->will($this->returnValue($this->quote));
94121
$methods = ['create'];
95-
$this->quoteDetailsItemDataObject = $objectManager->getObject(
96-
\Magento\Tax\Model\Sales\Quote\ItemDetails::class
97-
);
98-
$this->taxClassKeyDataObject = $objectManager->getObject(\Magento\Tax\Model\TaxClass\Key::class);
122+
$this->quoteDetailsItemDataObject = $objectManager->getObject(ItemDetails::class);
123+
$this->taxClassKeyDataObject = $objectManager->getObject(TaxClassKey::class);
99124
$this->quoteDetailsItemDataObjectFactoryMock
100-
= $this->createPartialMock(\Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory::class, $methods);
125+
= $this->createPartialMock(QuoteDetailsItemInterfaceFactory::class, $methods);
101126
$this->quoteDetailsItemDataObjectFactoryMock->expects($this->any())
102127
->method('create')
103128
->willReturn($this->quoteDetailsItemDataObject);
104129
$this->taxClassKeyDataObjectFactoryMock =
105-
$this->createPartialMock(\Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory::class, $methods);
130+
$this->createPartialMock(TaxClassKeyInterfaceFactory::class, $methods);
106131
$this->taxClassKeyDataObjectFactoryMock->expects($this->any())
107132
->method('create')
108133
->willReturn($this->taxClassKeyDataObject);
134+
$this->taxHelper = $this->getMockBuilder(TaxHelper::class)
135+
->disableOriginalConstructor()
136+
->getMock();
109137
$this->commonTaxCollector = $objectManager->getObject(
110-
\Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector::class,
138+
CommonTaxCollector::class,
111139
[
112140
'taxConfig' => $this->taxConfig,
113141
'quoteDetailsItemDataObjectFactory' => $this->quoteDetailsItemDataObjectFactoryMock,
114-
'taxClassKeyDataObjectFactory' => $this->taxClassKeyDataObjectFactoryMock
142+
'taxClassKeyDataObjectFactory' => $this->taxClassKeyDataObjectFactoryMock,
143+
'taxHelper' => $this->taxHelper,
115144
]
116145
);
117146
}
118147

119148
/**
149+
* Test for GetShippingDataObject
150+
*
120151
* @param array $addressData
121152
* @param bool $useBaseCurrency
122153
* @param string $shippingTaxClass
123154
* @param bool $shippingPriceInclTax
155+
*
156+
* @return void
124157
* @dataProvider getShippingDataObjectDataProvider
125158
*/
126159
public function testGetShippingDataObject(
127160
array $addressData,
128161
$useBaseCurrency,
129162
$shippingTaxClass,
130163
$shippingPriceInclTax
131-
) {
132-
$shippingAssignmentMock = $this->createMock(\Magento\Quote\Api\Data\ShippingAssignmentInterface::class);
164+
): void {
165+
$shippingAssignmentMock = $this->createMock(ShippingAssignmentInterface::class);
133166
$methods = [
134167
'getShippingDiscountAmount',
135168
'getShippingTaxCalculationAmount',
@@ -139,8 +172,10 @@ public function testGetShippingDataObject(
139172
'getBaseShippingAmount',
140173
'getBaseShippingDiscountAmount'
141174
];
142-
$totalsMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address\Total::class, $methods);
143-
$shippingMock = $this->createMock(\Magento\Quote\Api\Data\ShippingInterface::class);
175+
/** @var MockObject|QuoteAddressTotal $totalsMock */
176+
$totalsMock = $this->createPartialMock(QuoteAddressTotal::class, $methods);
177+
$shippingMock = $this->createMock(ShippingInterface::class);
178+
/** @var MockObject|ShippingAssignmentInterface $shippingAssignmentMock */
144179
$shippingAssignmentMock->expects($this->once())->method('getShipping')->willReturn($shippingMock);
145180
$shippingMock->expects($this->once())->method('getAddress')->willReturn($this->address);
146181
$baseShippingAmount = $addressData['base_shipping_amount'];
@@ -184,9 +219,44 @@ public function testGetShippingDataObject(
184219
}
185220

186221
/**
222+
* Update item tax info
223+
*
224+
* @return void
225+
*/
226+
public function testUpdateItemTaxInfo(): void
227+
{
228+
/** @var MockObject|QuoteItem $quoteItem */
229+
$quoteItem = $this->getMockBuilder(QuoteItem::class)
230+
->disableOriginalConstructor()
231+
->setMethods(['getPrice', 'setPrice', 'getCustomPrice', 'setCustomPrice'])
232+
->getMock();
233+
$this->taxHelper->method('applyTaxOnCustomPrice')->willReturn(true);
234+
$quoteItem->method('getCustomPrice')->willReturn(true);
235+
/** @var MockObject|TaxDetailsItemInterface $itemTaxDetails */
236+
$itemTaxDetails = $this->getMockBuilder(TaxDetailsItemInterface::class)
237+
->disableOriginalConstructor()
238+
->getMock();
239+
/** @var MockObject|TaxDetailsItemInterface $baseItemTaxDetails */
240+
$baseItemTaxDetails = $this->getMockBuilder(TaxDetailsItemInterface::class)
241+
->disableOriginalConstructor()
242+
->getMock();
243+
244+
$quoteItem->expects($this->once())->method('setCustomPrice');
245+
246+
$this->commonTaxCollector->updateItemTaxInfo(
247+
$quoteItem,
248+
$itemTaxDetails,
249+
$baseItemTaxDetails,
250+
$this->store
251+
);
252+
}
253+
254+
/**
255+
* Data for testGetShippingDataObject
256+
*
187257
* @return array
188258
*/
189-
public function getShippingDataObjectDataProvider()
259+
public function getShippingDataObjectDataProvider(): array
190260
{
191261
$data = [
192262
'free_shipping' => [

dev/tests/integration/testsuite/Magento/Tax/Model/Sales/Total/Quote/SetupUtil.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@
77
namespace Magento\Tax\Model\Sales\Total\Quote;
88

99
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Quote\Model\Quote;
1011
use Magento\Tax\Model\Config;
1112
use Magento\Tax\Model\Calculation;
13+
use Magento\Quote\Model\Quote\Item\Updater;
14+
use Magento\Catalog\Api\ProductRepositoryInterface;
15+
use Magento\Catalog\Api\Data\ProductInterface;
16+
use Magento\Framework\Api\Filter;
17+
use Magento\Framework\Api\Search\FilterGroup;
18+
use Magento\Framework\Api\SearchCriteriaInterface;
1219

1320
/**
21+
* Setup utility for quote
22+
*
1423
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1524
*/
1625
class SetupUtil
@@ -594,7 +603,7 @@ protected function createCartRule($ruleDataOverride)
594603
*
595604
* @param array $quoteData
596605
* @param \Magento\Customer\Api\Data\CustomerInterface $customer
597-
* @return \Magento\Quote\Model\Quote
606+
* @return Quote
598607
*/
599608
protected function createQuote($quoteData, $customer)
600609
{
@@ -619,8 +628,8 @@ protected function createQuote($quoteData, $customer)
619628
$quoteBillingAddress = $this->objectManager->create(\Magento\Quote\Model\Quote\Address::class);
620629
$quoteBillingAddress->importCustomerAddressData($addressService->getById($billingAddress->getId()));
621630

622-
/** @var \Magento\Quote\Model\Quote $quote */
623-
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
631+
/** @var Quote $quote */
632+
$quote = $this->objectManager->create(Quote::class);
624633
$quote->setStoreId(1)
625634
->setIsActive(true)
626635
->setIsMultiShipping(false)
@@ -634,7 +643,7 @@ protected function createQuote($quoteData, $customer)
634643
/**
635644
* Add products to quote
636645
*
637-
* @param \Magento\Quote\Model\Quote $quote
646+
* @param Quote $quote
638647
* @param array $itemsData
639648
* @return $this
640649
*/
@@ -657,7 +666,8 @@ protected function addProductToQuote($quote, $itemsData)
657666
* Create a quote based on given data
658667
*
659668
* @param array $quoteData
660-
* @return \Magento\Quote\Model\Quote
669+
*
670+
* @return Quote
661671
*/
662672
public function setupQuote($quoteData)
663673
{
@@ -666,7 +676,9 @@ public function setupQuote($quoteData)
666676
$quote = $this->createQuote($quoteData, $customer);
667677

668678
$this->addProductToQuote($quote, $quoteData['items']);
669-
679+
if (isset($quoteData['update_items'])) {
680+
$this->updateItems($quote, $quoteData['update_items']);
681+
}
670682
//Set shipping amount
671683
if (isset($quoteData['shipping_method'])) {
672684
$quote->getShippingAddress()->setShippingMethod($quoteData['shipping_method']);
@@ -683,4 +695,33 @@ public function setupQuote($quoteData)
683695

684696
return $quote;
685697
}
698+
699+
/**
700+
* Update quote items
701+
*
702+
* @param Quote $quote
703+
* @param array $items
704+
*
705+
* @return void
706+
*/
707+
private function updateItems(Quote $quote, array $items): void
708+
{
709+
$updater = $this->objectManager->get(Updater::class);
710+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
711+
$filter = $this->objectManager->create(Filter::class);
712+
$filter->setField('sku')->setValue(array_keys($items));
713+
$filterGroup = $this->objectManager->create(FilterGroup::class);
714+
$filterGroup->setFilters([$filter]);
715+
$searchCriteria = $this->objectManager->create(SearchCriteriaInterface::class);
716+
$searchCriteria->setFilterGroups([$filterGroup]);
717+
$products = $productRepository->getList($searchCriteria)->getItems();
718+
/** @var ProductInterface $product */
719+
foreach ($products as $product) {
720+
$quoteItem = $quote->getItemByProduct($product);
721+
$updater->update(
722+
$quoteItem,
723+
$items[$product->getSku()]
724+
);
725+
}
726+
}
686727
}

0 commit comments

Comments
 (0)