Skip to content

Commit 869e156

Browse files
authored
Merge pull request #910 from magento-tsg/MC-37666
Incorrect Customer TAX Class saved with Quote when VAT Validation used on Guest orders
2 parents d31cd74 + ac843d3 commit 869e156

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Quote\Model;
99

1010
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Customer\Api\Data\GroupInterface;
1112
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\Event\ManagerInterface as EventManager;
1314
use Magento\Framework\Exception\CouldNotSaveException;
@@ -396,7 +397,8 @@ public function placeOrder($cartId, PaymentInterface $paymentMethod = null)
396397
}
397398
}
398399
$quote->setCustomerIsGuest(true);
399-
$quote->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);
400+
$groupId = $quote->getCustomer()->getGroupId() ?: GroupInterface::NOT_LOGGED_IN_ID;
401+
$quote->setCustomerGroupId($groupId);
400402
}
401403

402404
$remoteAddress = $this->remoteAddress->getRemoteAddress();

app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ protected function setUp(): void
247247
'getPayment',
248248
'setCheckoutMethod',
249249
'setCustomerIsGuest',
250+
'getCustomer',
250251
'getId'
251252
]
252253
)
@@ -799,6 +800,12 @@ public function testPlaceOrderIfCustomerIsGuest()
799800
$this->quoteMock->expects($this->once())
800801
->method('getCheckoutMethod')
801802
->willReturn(Onepage::METHOD_GUEST);
803+
$customerMock = $this->getMockBuilder(Customer::class)
804+
->disableOriginalConstructor()
805+
->getMock();
806+
$this->quoteMock->expects($this->once())
807+
->method('getCustomer')
808+
->willReturn($customerMock);
802809
$this->quoteMock->expects($this->once())->method('setCustomerId')->with(null)->willReturnSelf();
803810
$this->quoteMock->expects($this->once())->method('setCustomerEmail')->with($email)->willReturnSelf();
804811

@@ -866,6 +873,9 @@ public function testPlaceOrderIfCustomerIsGuest()
866873
$this->assertEquals($orderId, $service->placeOrder($cartId));
867874
}
868875

876+
/**
877+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
878+
*/
869879
public function testPlaceOrder()
870880
{
871881
$cartId = 323;

dev/tests/integration/testsuite/Magento/Quote/Model/QuoteManagementTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@
1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Product\Type;
1212
use Magento\Customer\Api\CustomerRepositoryInterface;
13+
use Magento\Customer\Model\Vat;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\DataObject;
1316
use Magento\Framework\Exception\LocalizedException;
1417
use Magento\Framework\Exception\StateException;
1518
use Magento\Framework\ObjectManagerInterface;
1619
use Magento\Quote\Api\CartManagementInterface;
20+
use Magento\Quote\Observer\Frontend\Quote\Address\CollectTotalsObserver;
21+
use Magento\Quote\Observer\Frontend\Quote\Address\VatValidator;
1722
use Magento\Sales\Api\OrderManagementInterface;
1823
use Magento\Sales\Api\OrderRepositoryInterface;
1924
use Magento\Store\Model\StoreManagerInterface;
2025
use Magento\TestFramework\Helper\Bootstrap;
2126
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
2227
use PHPUnit\Framework\ExpectationFailedException;
2328
use PHPUnit\Framework\TestCase;
29+
use Psr\Log\LoggerInterface;
2430

2531
/**
2632
* Class for testing QuoteManagement model
@@ -106,6 +112,28 @@ public function testSubmit(): void
106112
}
107113
}
108114

115+
/**
116+
* Verify guest customer place order with auto-group assigment.
117+
*
118+
* @magentoDataFixture Magento/Sales/_files/guest_quote_with_addresses.php
119+
*
120+
* @magentoConfigFixture default_store customer/create_account/auto_group_assign 1
121+
* @magentoConfigFixture default_store customer/create_account/tax_calculation_address_type shipping
122+
* @magentoConfigFixture default_store customer/create_account/viv_intra_union_group 2
123+
* @magentoConfigFixture default_store customer/create_account/viv_on_each_transaction 1
124+
*
125+
* @return void
126+
*/
127+
public function testSubmitGuestCustomer(): void
128+
{
129+
$this->mockVatValidation();
130+
$quote = $this->getQuoteByReservedOrderId->execute('guest_quote');
131+
$this->cartManagement->placeOrder($quote->getId());
132+
$quoteAfterOrderPlaced = $this->getQuoteByReservedOrderId->execute('guest_quote');
133+
self::assertEquals(2, $quoteAfterOrderPlaced->getCustomerGroupId());
134+
self::assertEquals(3, $quoteAfterOrderPlaced->getCustomerTaxClassId());
135+
}
136+
109137
/**
110138
* Tries to create order with product that has child items and one of them was deleted.
111139
*
@@ -231,4 +259,33 @@ private function makeProductOutOfStock(string $sku): void
231259
$stockItem->setIsInStock(false);
232260
$this->productRepository->save($product);
233261
}
262+
263+
/**
264+
* Makes customer vat validator 'check vat number' response successful.
265+
*
266+
* @return void
267+
*/
268+
private function mockVatValidation(): void
269+
{
270+
$vatMock = $this->getMockBuilder(Vat::class)
271+
->setConstructorArgs(
272+
[
273+
'scopeConfig' => $this->objectManager->get(ScopeConfigInterface::class),
274+
'logger' => $this->objectManager->get(LoggerInterface::class),
275+
]
276+
)
277+
->onlyMethods(['checkVatNumber'])
278+
->getMock();
279+
$gatewayResponse = new DataObject([
280+
'is_valid' => true,
281+
'request_date' => 'testData',
282+
'request_identifier' => 'testRequestIdentifier',
283+
'request_success' => true,
284+
]);
285+
$vatMock->method('checkVatNumber')->willReturn($gatewayResponse);
286+
$this->objectManager->removeSharedInstance(CollectTotalsObserver::class);
287+
$this->objectManager->removeSharedInstance(VatValidator::class);
288+
$this->objectManager->removeSharedInstance(Vat::class);
289+
$this->objectManager->addSharedInstance($vatMock, Vat::class);
290+
}
234291
}

dev/tests/integration/testsuite/Magento/Sales/_files/guest_quote_with_addresses.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
$addressData = [
4141
'telephone' => 3234676,
4242
'postcode' => 47676,
43-
'country_id' => 'US',
43+
'country_id' => 'DE',
4444
'city' => 'CityX',
4545
'street' => ['Black str, 48'],
4646
'lastname' => 'Smith',
4747
'firstname' => 'John',
48+
'vat_id' => 12345,
4849
'address_type' => 'shipping',
4950
'email' => 'some_email@mail.com',
50-
'region_id' => 1,
5151
];
5252

5353
$billingAddress = $objectManager->create(
@@ -66,6 +66,7 @@
6666
$quote->setCustomerIsGuest(true)
6767
->setStoreId($store->getId())
6868
->setReservedOrderId('guest_quote')
69+
->setCheckoutMethod('guest')
6970
->setBillingAddress($billingAddress)
7071
->setShippingAddress($shippingAddress)
7172
->addProduct($product);

0 commit comments

Comments
 (0)