Skip to content

Commit 5b25350

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-51081' into BUGS
2 parents c29f3e7 + 01e4fe5 commit 5b25350

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
class CollectTotalsObserver implements ObserverInterface
1111
{
12+
/**
13+
* @var \Magento\Customer\Api\AddressRepositoryInterface
14+
*/
15+
private $addressRepository;
16+
17+
/**
18+
* @var \Magento\Customer\Model\Session
19+
*/
20+
private $customerSession;
21+
1222
/**
1323
* @var \Magento\Customer\Helper\Address
1424
*/
@@ -50,13 +60,17 @@ public function __construct(
5060
\Magento\Customer\Model\Vat $customerVat,
5161
VatValidator $vatValidator,
5262
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
53-
\Magento\Customer\Api\GroupManagementInterface $groupManagement
63+
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
64+
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
65+
\Magento\Customer\Model\Session $customerSession
5466
) {
5567
$this->customerVat = $customerVat;
5668
$this->customerAddressHelper = $customerAddressHelper;
5769
$this->vatValidator = $vatValidator;
5870
$this->customerDataFactory = $customerDataFactory;
5971
$this->groupManagement = $groupManagement;
72+
$this->addressRepository = $addressRepository;
73+
$this->customerSession = $customerSession;
6074
}
6175

6276
/**
@@ -84,6 +98,15 @@ public function execute(\Magento\Framework\Event\Observer $observer)
8498
}
8599
$customerCountryCode = $address->getCountryId();
86100
$customerVatNumber = $address->getVatId();
101+
102+
/** try to get data from customer if quote address needed data is empty */
103+
if (empty($customerCountryCode) && empty($customerVatNumber) && $customer->getDefaultShipping()) {
104+
$customerAddress = $this->addressRepository->getById($customer->getDefaultShipping());
105+
106+
$customerCountryCode = $customerAddress->getCountryId();
107+
$customerVatNumber = $customerAddress->getVatId();
108+
}
109+
87110
$groupId = null;
88111
if (empty($customerVatNumber) || false == $this->customerVat->isCountryInEU($customerCountryCode)) {
89112
$groupId = $customer->getId() ? $this->groupManagement->getDefaultGroup(
@@ -101,6 +124,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
101124
if ($groupId) {
102125
$address->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId());
103126
$quote->setCustomerGroupId($groupId);
127+
$this->customerSession->setCustomerGroupId($groupId);
104128
$customer->setGroupId($groupId);
105129
$quote->setCustomer($customer);
106130
}

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)