Skip to content

Commit 63830b4

Browse files
committed
Merge remote-tracking branch 'folks/MAGETWO-70280' into PRS
2 parents 41669ce + e1f961d commit 63830b4

File tree

16 files changed

+421
-21
lines changed

16 files changed

+421
-21
lines changed

app/code/Magento/CatalogRule/Setup/UpgradeSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
4040
}
4141
}
4242

43-
if (version_compare($context->getVersion(), '2.2.0', '<')) {
43+
if (version_compare($context->getVersion(), '2.1.0', '<')) {
4444
$connection = $setup->getConnection();
4545
$connection->dropForeignKey(
4646
$setup->getTable('catalogrule_group_website'),

app/code/Magento/CatalogRule/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_CatalogRule" setup_version="2.0.3">
9+
<module name="Magento_CatalogRule" setup_version="2.1.0">
1010
<sequence>
1111
<module name="Magento_Rule"/>
1212
<module name="Magento_Catalog"/>

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Customer\Api\AccountManagementInterface as AccountManagement;
1111
use Magento\Customer\Api\AddressRepositoryInterface as CustomerAddressRepository;
1212
use Magento\Quote\Model\Quote as QuoteEntity;
13+
use Magento\Framework\App\ObjectManager;
1314

1415
/**
1516
* Class Customer
@@ -32,18 +33,37 @@ class CustomerManagement
3233
protected $accountManagement;
3334

3435
/**
36+
* @var \Magento\Framework\Validator\Factory
37+
*/
38+
private $validatorFactory;
39+
40+
/**
41+
* @var \Magento\Customer\Model\AddressFactory
42+
*/
43+
private $addressFactory;
44+
45+
/**
46+
* CustomerManagement constructor.
3547
* @param CustomerRepository $customerRepository
3648
* @param CustomerAddressRepository $customerAddressRepository
3749
* @param AccountManagement $accountManagement
50+
* @param \Magento\Framework\Validator\Factory|null $validatorFactory
51+
* @param \Magento\Customer\Model\AddressFactory|null $addressFactory
3852
*/
3953
public function __construct(
4054
CustomerRepository $customerRepository,
4155
CustomerAddressRepository $customerAddressRepository,
42-
AccountManagement $accountManagement
56+
AccountManagement $accountManagement,
57+
\Magento\Framework\Validator\Factory $validatorFactory = null,
58+
\Magento\Customer\Model\AddressFactory $addressFactory = null
4359
) {
4460
$this->customerRepository = $customerRepository;
4561
$this->customerAddressRepository = $customerAddressRepository;
4662
$this->accountManagement = $accountManagement;
63+
$this->validatorFactory = $validatorFactory ?: ObjectManager::getInstance()
64+
->get(\Magento\Framework\Validator\Factory::class);
65+
$this->addressFactory = $addressFactory ?: ObjectManager::getInstance()
66+
->get(\Magento\Customer\Model\AddressFactory::class);
4767
}
4868

4969
/**
@@ -79,4 +99,40 @@ public function populateCustomerInfo(QuoteEntity $quote)
7999
$quote->getShippingAddress()->setCustomerAddressId($customer->getDefaultShipping());
80100
}
81101
}
102+
103+
/**
104+
* Validate Quote Addresses
105+
*
106+
* @param Quote $quote
107+
* @throws \Magento\Framework\Validator\Exception
108+
* @return void
109+
*/
110+
public function validateAddresses(QuoteEntity $quote)
111+
{
112+
$addresses = [];
113+
if ($quote->getBillingAddress()->getCustomerAddressId()) {
114+
$addresses[] = $this->customerAddressRepository->getById(
115+
$quote->getBillingAddress()->getCustomerAddressId()
116+
);
117+
}
118+
if ($quote->getShippingAddress()->getCustomerAddressId()) {
119+
$addresses[] = $this->customerAddressRepository->getById(
120+
$quote->getShippingAddress()->getCustomerAddressId()
121+
);
122+
}
123+
if (!empty($addresses)) {
124+
foreach ($addresses as $address) {
125+
$validator = $this->validatorFactory->createValidator('customer_address', 'save');
126+
$addressModel = $this->addressFactory->create();
127+
$addressModel->updateData($address);
128+
if (!$validator->isValid($addressModel)) {
129+
throw new \Magento\Framework\Validator\Exception(
130+
null,
131+
null,
132+
$validator->getMessages()
133+
);
134+
}
135+
}
136+
}
137+
}
82138
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ public function addCustomerAddress(\Magento\Customer\Api\Data\AddressInterface $
10321032
$addresses = (array)$this->getCustomer()->getAddresses();
10331033
$addresses[] = $address;
10341034
$this->getCustomer()->setAddresses($addresses);
1035-
$this->updateCustomerData($this->customerRepository->save($this->getCustomer()));
1035+
$this->updateCustomerData($this->getCustomer());
10361036
return $this;
10371037
}
10381038

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Magento\Framework\Exception\LocalizedException;
1414
use Magento\Framework\Exception\StateException;
1515
use Magento\Quote\Api\Data\PaymentInterface;
16-
use Magento\Quote\Model\Quote\Address;
1716
use Magento\Quote\Model\Quote\Address\ToOrder as ToOrderConverter;
1817
use Magento\Quote\Model\Quote\Address\ToOrderAddress as ToOrderAddressConverter;
1918
use Magento\Quote\Model\Quote as QuoteEntity;
@@ -136,6 +135,16 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
136135
*/
137136
private $quoteIdMaskFactory;
138137

138+
/**
139+
* @var \Magento\Customer\Api\AddressRepositoryInterface
140+
*/
141+
private $addressRepository;
142+
143+
/**
144+
* @var array
145+
*/
146+
private $addressesToSync = [];
147+
139148
/**
140149
* @param EventManager $eventManager
141150
* @param QuoteValidator $quoteValidator
@@ -158,6 +167,7 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
158167
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
159168
* @param QuoteFactory $quoteFactory
160169
* @param \Magento\Quote\Model\QuoteIdMaskFactory|null $quoteIdMaskFactory
170+
* @param \Magento\Customer\Api\AddressRepositoryInterface|null $addressRepository
161171
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
162172
*/
163173
public function __construct(
@@ -181,7 +191,8 @@ public function __construct(
181191
\Magento\Customer\Model\Session $customerSession,
182192
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
183193
\Magento\Quote\Model\QuoteFactory $quoteFactory,
184-
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory = null
194+
\Magento\Quote\Model\QuoteIdMaskFactory $quoteIdMaskFactory = null,
195+
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository = null
185196
) {
186197
$this->eventManager = $eventManager;
187198
$this->quoteValidator = $quoteValidator;
@@ -205,6 +216,8 @@ public function __construct(
205216
$this->quoteFactory = $quoteFactory;
206217
$this->quoteIdMaskFactory = $quoteIdMaskFactory ?: ObjectManager::getInstance()
207218
->get(\Magento\Quote\Model\QuoteIdMaskFactory::class);
219+
$this->addressRepository = $addressRepository ?: ObjectManager::getInstance()
220+
->get(\Magento\Customer\Api\AddressRepositoryInterface::class);
208221
}
209222

210223
/**
@@ -428,6 +441,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
428441
if (!$quote->getCustomerIsGuest()) {
429442
if ($quote->getCustomerId()) {
430443
$this->_prepareCustomerQuote($quote);
444+
$this->customerManagement->validateAddresses($quote);
431445
}
432446
$this->customerManagement->populateCustomerInfo($quote);
433447
}
@@ -496,6 +510,11 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
496510
);
497511
$this->quoteRepository->save($quote);
498512
} catch (\Exception $e) {
513+
if (!empty($this->addressesToSync)) {
514+
foreach ($this->addressesToSync as $addressId) {
515+
$this->addressRepository->deleteById($addressId);
516+
}
517+
}
499518
$this->eventManager->dispatch(
500519
'sales_model_service_quote_submit_failure',
501520
[
@@ -536,8 +555,12 @@ protected function _prepareCustomerQuote($quote)
536555
$shippingAddress->setIsDefaultShipping(true);
537556
$hasDefaultShipping = true;
538557
}
558+
//save here new customer address
559+
$shippingAddress->setCustomerId($quote->getCustomerId());
560+
$this->addressRepository->save($shippingAddress);
539561
$quote->addCustomerAddress($shippingAddress);
540562
$shipping->setCustomerAddressData($shippingAddress);
563+
$this->addressesToSync[] = $shippingAddress->getId();
541564
$shipping->setCustomerAddressId($shippingAddress->getId());
542565
}
543566

@@ -551,8 +574,11 @@ protected function _prepareCustomerQuote($quote)
551574
}
552575
$billingAddress->setIsDefaultBilling(true);
553576
}
577+
$billingAddress->setCustomerId($quote->getCustomerId());
578+
$this->addressRepository->save($billingAddress);
554579
$quote->addCustomerAddress($billingAddress);
555580
$billing->setCustomerAddressData($billingAddress);
581+
$this->addressesToSync[] = $billingAddress->getId();
556582
$billing->setCustomerAddressId($billingAddress->getId());
557583
}
558584
if ($shipping && !$shipping->getCustomerId() && !$hasDefaultBilling) {

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/**
99
* Class CustomerManagementTest
10+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1011
*/
1112
class CustomerManagementTest extends \PHPUnit_Framework_TestCase
1213
{
@@ -50,6 +51,16 @@ class CustomerManagementTest extends \PHPUnit_Framework_TestCase
5051
*/
5152
protected $customerAddressMock;
5253

54+
/**
55+
* @var \PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
private $validatorFactoryMock;
58+
59+
/**
60+
* @var \PHPUnit_Framework_MockObject_MockObject
61+
*/
62+
private $addressFactoryMock;
63+
5364
protected function setUp()
5465
{
5566
$this->customerRepositoryMock = $this->getMockForAbstractClass(
@@ -111,10 +122,19 @@ protected function setUp()
111122
true,
112123
[]
113124
);
125+
$this->addressFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\AddressFactory::class)
126+
->disableOriginalConstructor()
127+
->setMethods(['create'])
128+
->getMock();
129+
$this->validatorFactoryMock = $this->getMockBuilder(\Magento\Framework\Validator\Factory::class)
130+
->disableOriginalConstructor()
131+
->getMock();
114132
$this->customerManagement = new \Magento\Quote\Model\CustomerManagement(
115133
$this->customerRepositoryMock,
116134
$this->customerAddressRepositoryMock,
117-
$this->accountManagementMock
135+
$this->accountManagementMock,
136+
$this->validatorFactoryMock,
137+
$this->addressFactoryMock
118138
);
119139
}
120140

@@ -188,4 +208,53 @@ public function testPopulateCustomerInfoForExistingCustomer()
188208
->willReturnSelf();
189209
$this->customerManagement->populateCustomerInfo($this->quoteMock);
190210
}
211+
212+
public function testValidateAddresses()
213+
{
214+
$this->quoteMock
215+
->expects($this->exactly(2))
216+
->method('getBillingAddress')
217+
->willReturn($this->quoteAddressMock);
218+
$this->quoteMock
219+
->expects($this->exactly(2))
220+
->method('getShippingAddress')
221+
->willReturn($this->quoteAddressMock);
222+
$this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(2);
223+
$this->customerAddressRepositoryMock
224+
->expects($this->any())
225+
->method('getById')
226+
->willReturn($this->customerAddressMock);
227+
$validatorMock = $this->getMockBuilder(\Magento\Framework\Validator::class)
228+
->disableOriginalConstructor()
229+
->getMock();
230+
$addressMock = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
231+
->disableOriginalConstructor()
232+
->getMock();
233+
$this->addressFactoryMock->expects($this->exactly(2))->method('create')->willReturn($addressMock);
234+
$this->validatorFactoryMock
235+
->expects($this->exactly(2))
236+
->method('createValidator')
237+
->with('customer_address', 'save', null)
238+
->willReturn($validatorMock);
239+
$validatorMock->expects($this->exactly(2))->method('isValid')->with($addressMock)->willReturn(true);
240+
$this->customerManagement->validateAddresses($this->quoteMock);
241+
}
242+
243+
public function testValidateAddressesNotSavedInAddressBook()
244+
{
245+
$this->quoteMock
246+
->expects($this->once())
247+
->method('getBillingAddress')
248+
->willReturn($this->quoteAddressMock);
249+
$this->quoteMock
250+
->expects($this->once())
251+
->method('getShippingAddress')
252+
->willReturn($this->quoteAddressMock);
253+
$this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(null);
254+
$this->validatorFactoryMock
255+
->expects($this->never())
256+
->method('createValidator')
257+
->with('customer_address', 'save', null);
258+
$this->customerManagement->validateAddresses($this->quoteMock);
259+
}
191260
}

0 commit comments

Comments
 (0)