Skip to content

Commit 01e4fe5

Browse files
author
Sergii Kovalenko
committed
MAGETWO-51081: Incorrect tier price calculation with enabled Automatic Assignment for Vat Id
1 parent a0b3eab commit 01e4fe5

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

app/code/Magento/Quote/Observer/Frontend/Quote/Address/CollectTotalsObserver.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Quote\Observer\Frontend\Quote\Address;
77

8-
use Magento\Framework\App\ObjectManager;
98
use Magento\Framework\Event\ObserverInterface;
109

1110
class CollectTotalsObserver implements ObserverInterface
@@ -15,6 +14,11 @@ class CollectTotalsObserver implements ObserverInterface
1514
*/
1615
private $addressRepository;
1716

17+
/**
18+
* @var \Magento\Customer\Model\Session
19+
*/
20+
private $customerSession;
21+
1822
/**
1923
* @var \Magento\Customer\Helper\Address
2024
*/
@@ -57,14 +61,16 @@ public function __construct(
5761
VatValidator $vatValidator,
5862
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
5963
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
60-
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository
64+
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
65+
\Magento\Customer\Model\Session $customerSession
6166
) {
6267
$this->customerVat = $customerVat;
6368
$this->customerAddressHelper = $customerAddressHelper;
6469
$this->vatValidator = $vatValidator;
6570
$this->customerDataFactory = $customerDataFactory;
6671
$this->groupManagement = $groupManagement;
6772
$this->addressRepository = $addressRepository;
73+
$this->customerSession = $customerSession;
6874
}
6975

7076
/**
@@ -118,6 +124,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
118124
if ($groupId) {
119125
$address->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
120126
$quote->setCustomerGroupId($groupId);
127+
$this->customerSession->setCustomerGroupId($groupId);
121128
$customer->setGroupId($groupId);
122129
$quote->setCustomer($customer);
123130
}

app/code/Magento/Quote/Test/Unit/Observer/Frontend/Quote/Address/CollectTotalsObserverTest.php

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class CollectTotalsObserverTest extends \PHPUnit_Framework_TestCase
1515
{
1616
/**
17-
* @var \Magento\Quote\Observer\Frontend\Quote\Address\CollectTotalsObserver;
17+
* @var \Magento\Quote\Observer\Frontend\Quote\Address\CollectTotalsObserver
1818
*/
1919
protected $model;
2020

@@ -23,11 +23,21 @@ class CollectTotalsObserverTest extends \PHPUnit_Framework_TestCase
2323
*/
2424
protected $customerAddressMock;
2525

26+
/**
27+
* @var \PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $customerSession;
30+
2631
/**
2732
* @var \PHPUnit_Framework_MockObject_MockObject
2833
*/
2934
protected $customerVatMock;
3035

36+
/**
37+
* @var \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $addressRepository;
40+
3141
/**
3242
* @var \PHPUnit_Framework_MockObject_MockObject
3343
*/
@@ -172,14 +182,21 @@ protected function setUp()
172182
->method('getCustomer')
173183
->will($this->returnValue($this->customerMock));
174184

185+
$this->addressRepository = $this->getMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
186+
$this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
187+
->disableOriginalConstructor()
188+
->getMock();
189+
175190
$this->customerMock->expects($this->any())->method('getStoreId')->will($this->returnValue($this->storeId));
176191

177192
$this->model = new \Magento\Quote\Observer\Frontend\Quote\Address\CollectTotalsObserver(
178193
$this->customerAddressMock,
179194
$this->customerVatMock,
180195
$this->vatValidatorMock,
181196
$this->customerDataFactoryMock,
182-
$this->groupManagementMock
197+
$this->groupManagementMock,
198+
$this->addressRepository,
199+
$this->customerSession
183200
);
184201
}
185202

@@ -319,4 +336,75 @@ public function testDispatchWithCustomerCountryInEU()
319336
->willReturn($this->customerMock);
320337
$this->model->execute($this->observerMock);
321338
}
339+
340+
public function testDispatchWithEmptyShippingAddress()
341+
{
342+
$customerCountryCode = "DE";
343+
$customerVat = "123123123";
344+
$defaultShipping = 1;
345+
346+
$customerAddress = $this->getMock(\Magento\Customer\Api\Data\AddressInterface::class);
347+
$customerAddress->expects($this->once())
348+
->method("getCountryId")
349+
->willReturn($customerCountryCode);
350+
351+
$customerAddress->expects($this->once())
352+
->method("getVatId")
353+
->willReturn($customerVat);
354+
$this->addressRepository->expects($this->once())
355+
->method("getById")
356+
->with($defaultShipping)
357+
->willReturn($customerAddress);
358+
359+
$this->customerMock->expects($this->atLeastOnce())
360+
->method("getDefaultShipping")
361+
->willReturn($defaultShipping);
362+
363+
$this->vatValidatorMock->expects($this->once())
364+
->method('isEnabled')
365+
->with($this->quoteAddressMock, $this->storeId)
366+
->will($this->returnValue(true));
367+
368+
$this->quoteAddressMock->expects($this->once())
369+
->method('getCountryId')
370+
->will($this->returnValue(null));
371+
$this->quoteAddressMock->expects($this->once())
372+
->method('getVatId')
373+
->will($this->returnValue(null));
374+
375+
$this->customerVatMock->expects($this->once())
376+
->method('isCountryInEU')
377+
->with($customerCountryCode)
378+
->willReturn(true);
379+
380+
$this->quoteMock->expects($this->once())
381+
->method('getCustomerGroupId')
382+
->will($this->returnValue('customerGroupId'));
383+
$validationResult = ['some' => 'result'];
384+
$this->customerVatMock->expects($this->once())
385+
->method('getCustomerGroupIdBasedOnVatNumber')
386+
->with($customerCountryCode, $validationResult, $this->storeId)
387+
->will($this->returnValue('customerGroupId'));
388+
$this->customerSession->expects($this->once())
389+
->method("setCustomerGroupId")
390+
->with('customerGroupId');
391+
392+
$this->vatValidatorMock->expects($this->once())
393+
->method('validate')
394+
->with($this->quoteAddressMock, $this->storeId)
395+
->will($this->returnValue($validationResult));
396+
397+
/** Assertions */
398+
$this->quoteAddressMock->expects($this->once())
399+
->method('setPrevQuoteCustomerGroupId')
400+
->with('customerGroupId');
401+
402+
$this->quoteMock->expects($this->once())->method('setCustomerGroupId')->with('customerGroupId');
403+
$this->quoteMock->expects($this->once())->method('setCustomer')->with($this->customerMock);
404+
$this->customerDataFactoryMock->expects($this->any())
405+
->method('create')
406+
->willReturn($this->customerMock);
407+
$this->model->execute($this->observerMock);
408+
409+
}
322410
}

0 commit comments

Comments
 (0)