Skip to content

Commit beb52b6

Browse files
committed
7691: address with saveInAddressBook 0 are still being added to the address book for new customers
1 parent cf4be3d commit beb52b6

File tree

8 files changed

+222
-40
lines changed

8 files changed

+222
-40
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
466466
'email' => $quote->getCustomerEmail()
467467
]
468468
);
469+
$shippingAddress->setData('quote_address_id', $quote->getShippingAddress()->getId());
469470
$addresses[] = $shippingAddress;
470471
$order->setShippingAddress($shippingAddress);
471472
$order->setShippingMethod($quote->getShippingAddress()->getShippingMethod());
@@ -477,6 +478,7 @@ protected function submitQuote(QuoteEntity $quote, $orderData = [])
477478
'email' => $quote->getCustomerEmail()
478479
]
479480
);
481+
$billingAddress->setData('quote_address_id', $quote->getBillingAddress()->getId());
480482
$addresses[] = $billingAddress;
481483
$order->setBillingAddress($billingAddress);
482484
$order->setAddresses($addresses);

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

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Exception\NoSuchEntityException;
1010

1111
use Magento\Quote\Model\CustomerManagement;
12+
use Magento\Sales\Api\Data\OrderAddressInterface;
1213

1314
/**
1415
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -131,6 +132,11 @@ class QuoteManagementTest extends \PHPUnit\Framework\TestCase
131132
*/
132133
private $addressRepositoryMock;
133134

135+
/**
136+
* @var \PHPUnit_Framework_MockObject_MockObject
137+
*/
138+
private $quoteFactoryMock;
139+
134140
/**
135141
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
136142
*/
@@ -530,12 +536,12 @@ public function testSubmit()
530536
$shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
531537
$payment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
532538
$baseOrder = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
533-
$convertedBillingAddress = $this->createMock(\Magento\Sales\Api\Data\OrderAddressInterface::class);
534-
$convertedShippingAddress = $this->createMock(\Magento\Sales\Api\Data\OrderAddressInterface::class);
539+
$convertedBilling = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
540+
$convertedShipping = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
535541
$convertedPayment = $this->createMock(\Magento\Sales\Api\Data\OrderPaymentInterface::class);
536542
$convertedQuoteItem = $this->createMock(\Magento\Sales\Api\Data\OrderItemInterface::class);
537543

538-
$addresses = [$convertedShippingAddress, $convertedBillingAddress];
544+
$addresses = [$convertedShipping, $convertedBilling];
539545
$quoteItems = [$quoteItem];
540546
$convertedItems = [$convertedQuoteItem];
541547

@@ -564,7 +570,7 @@ public function testSubmit()
564570
'email' => 'customer@example.com'
565571
]
566572
)
567-
->willReturn($convertedShippingAddress);
573+
->willReturn($convertedShipping);
568574
$this->quoteAddressToOrderAddress->expects($this->at(1))
569575
->method('convert')
570576
->with(
@@ -574,22 +580,26 @@ public function testSubmit()
574580
'email' => 'customer@example.com'
575581
]
576582
)
577-
->willReturn($convertedBillingAddress);
583+
->willReturn($convertedBilling);
584+
$billingAddress->expects($this->once())->method('getId')->willReturn(4);
585+
$convertedBilling->expects($this->once())->method('setData')->with('quote_address_id', 4);
586+
578587
$this->quoteItemToOrderItem->expects($this->once())->method('convert')
579588
->with($quoteItem, ['parent_item' => null])
580589
->willReturn($convertedQuoteItem);
581590
$this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)
582591
->willReturn($convertedPayment);
583592
$shippingAddress->expects($this->once())->method('getShippingMethod')->willReturn('free');
584-
593+
$shippingAddress->expects($this->once())->method('getId')->willReturn(5);
594+
$convertedShipping->expects($this->once())->method('setData')->with('quote_address_id', 5);
585595
$order = $this->prepareOrderFactory(
586596
$baseOrder,
587-
$convertedBillingAddress,
597+
$convertedBilling,
588598
$addresses,
589599
$convertedPayment,
590600
$convertedItems,
591601
$quoteId,
592-
$convertedShippingAddress
602+
$convertedShipping
593603
);
594604

595605
$this->orderManagement->expects($this->once())
@@ -857,22 +867,22 @@ protected function getQuote(
857867

858868
/**
859869
* @param \Magento\Sales\Api\Data\OrderInterface $baseOrder
860-
* @param \Magento\Sales\Api\Data\OrderAddressInterface $billingAddress
870+
* @param OrderAddressInterface $billingAddress
861871
* @param array $addresses
862872
* @param $payment
863873
* @param array $items
864874
* @param $quoteId
865-
* @param \Magento\Sales\Api\Data\OrderAddressInterface $shippingAddress
875+
* @param OrderAddressInterface $shippingAddress
866876
* @return \PHPUnit_Framework_MockObject_MockObject
867877
*/
868878
protected function prepareOrderFactory(
869879
\Magento\Sales\Api\Data\OrderInterface $baseOrder,
870-
\Magento\Sales\Api\Data\OrderAddressInterface $billingAddress,
880+
OrderAddressInterface $billingAddress,
871881
array $addresses,
872882
$payment,
873883
array $items,
874884
$quoteId,
875-
\Magento\Sales\Api\Data\OrderAddressInterface $shippingAddress = null,
885+
OrderAddressInterface $shippingAddress = null,
876886
$customerId = null
877887
) {
878888
$order = $this->createPartialMock(
@@ -963,9 +973,6 @@ protected function setPropertyValue(&$object, $property, $value)
963973
return $object;
964974
}
965975

966-
/**
967-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
968-
*/
969976
public function testSubmitForCustomer()
970977
{
971978
$orderData = [];
@@ -978,16 +985,12 @@ public function testSubmitForCustomer()
978985
$shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
979986
$payment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
980987
$baseOrder = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
981-
$convertedBillingAddress = $this->createMock(
982-
\Magento\Sales\Api\Data\OrderAddressInterface::class
983-
);
984-
$convertedShippingAddress = $this->createMock(
985-
\Magento\Sales\Api\Data\OrderAddressInterface::class
986-
);
988+
$convertedBilling = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
989+
$convertedShipping = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
987990
$convertedPayment = $this->createMock(\Magento\Sales\Api\Data\OrderPaymentInterface::class);
988991
$convertedQuoteItem = $this->createMock(\Magento\Sales\Api\Data\OrderItemInterface::class);
989992

990-
$addresses = [$convertedShippingAddress, $convertedBillingAddress];
993+
$addresses = [$convertedShipping, $convertedBilling];
991994
$quoteItems = [$quoteItem];
992995
$convertedItems = [$convertedQuoteItem];
993996

@@ -1016,7 +1019,7 @@ public function testSubmitForCustomer()
10161019
'email' => 'customer@example.com'
10171020
]
10181021
)
1019-
->willReturn($convertedShippingAddress);
1022+
->willReturn($convertedShipping);
10201023
$this->quoteAddressToOrderAddress->expects($this->at(1))
10211024
->method('convert')
10221025
->with(
@@ -1026,22 +1029,23 @@ public function testSubmitForCustomer()
10261029
'email' => 'customer@example.com'
10271030
]
10281031
)
1029-
->willReturn($convertedBillingAddress);
1032+
->willReturn($convertedBilling);
10301033
$this->quoteItemToOrderItem->expects($this->once())->method('convert')
10311034
->with($quoteItem, ['parent_item' => null])
10321035
->willReturn($convertedQuoteItem);
10331036
$this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)
10341037
->willReturn($convertedPayment);
10351038
$shippingAddress->expects($this->once())->method('getShippingMethod')->willReturn('free');
1036-
1039+
$shippingAddress->expects($this->once())->method('getId')->willReturn(5);
1040+
$convertedShipping->expects($this->once())->method('setData')->with('quote_address_id', 5);
10371041
$order = $this->prepareOrderFactory(
10381042
$baseOrder,
1039-
$convertedBillingAddress,
1043+
$convertedBilling,
10401044
$addresses,
10411045
$convertedPayment,
10421046
$convertedItems,
10431047
$quoteId,
1044-
$convertedShippingAddress
1048+
$convertedShipping
10451049
);
10461050
$customerAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
10471051
->getMockForAbstractClass();
@@ -1050,6 +1054,8 @@ public function testSubmitForCustomer()
10501054
$quote->expects($this->any())->method('addCustomerAddress')->with($customerAddressMock);
10511055
$billingAddress->expects($this->once())->method('getCustomerId')->willReturn(2);
10521056
$billingAddress->expects($this->once())->method('getSaveInAddressBook')->willReturn(false);
1057+
$billingAddress->expects($this->once())->method('getId')->willReturn(4);
1058+
$convertedBilling->expects($this->once())->method('setData')->with('quote_address_id', 4);
10531059
$this->orderManagement->expects($this->once())
10541060
->method('place')
10551061
->with($order)
@@ -1063,4 +1069,25 @@ public function testSubmitForCustomer()
10631069
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
10641070
$this->assertEquals($order, $this->model->submit($quote, $orderData));
10651071
}
1072+
1073+
/**
1074+
* Get mock for abstract class with methods.
1075+
*
1076+
* @param string $className
1077+
* @param array $methods
1078+
*
1079+
* @return \PHPUnit_Framework_MockObject_MockObject
1080+
*/
1081+
private function createPartialMockForAbstractClass($className, $methods = [])
1082+
{
1083+
return $this->getMockForAbstractClass(
1084+
$className,
1085+
[],
1086+
'',
1087+
true,
1088+
true,
1089+
true,
1090+
$methods
1091+
);
1092+
}
10661093
}

app/code/Magento/Sales/Model/Order/CustomerManagement.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Sales\Model\Order;
78

9+
use Magento\Framework\App\ObjectManager;
810
use Magento\Framework\Exception\AlreadyExistsException;
911
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1012

@@ -43,28 +45,38 @@ class CustomerManagement implements \Magento\Sales\Api\OrderCustomerManagementIn
4345
*/
4446
protected $objectCopyService;
4547

48+
/**
49+
* @var \Magento\Quote\Model\Quote\AddressFactory
50+
*/
51+
private $quoteAddressFactory;
52+
4653
/**
4754
* @param \Magento\Framework\DataObject\Copy $objectCopyService
4855
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
4956
* @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory
5057
* @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory
5158
* @param \Magento\Customer\Api\Data\RegionInterfaceFactory $regionFactory
5259
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
60+
* @param \Magento\Quote\Model\Quote\AddressFactory|null $quoteAddressFactory
5361
*/
5462
public function __construct(
5563
\Magento\Framework\DataObject\Copy $objectCopyService,
5664
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
5765
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory,
5866
\Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory,
5967
\Magento\Customer\Api\Data\RegionInterfaceFactory $regionFactory,
60-
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
68+
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
69+
\Magento\Quote\Model\Quote\AddressFactory $quoteAddressFactory = null
6170
) {
6271
$this->objectCopyService = $objectCopyService;
6372
$this->accountManagement = $accountManagement;
6473
$this->orderRepository = $orderRepository;
6574
$this->customerFactory = $customerFactory;
6675
$this->addressFactory = $addressFactory;
6776
$this->regionFactory = $regionFactory;
77+
$this->quoteAddressFactory = $quoteAddressFactory ?: ObjectManager::getInstance()->get(
78+
\Magento\Quote\Model\Quote\AddressFactory::class
79+
);
6880
}
6981

7082
/**
@@ -84,6 +96,9 @@ public function create($orderId)
8496
);
8597
$addresses = $order->getAddresses();
8698
foreach ($addresses as $address) {
99+
if (!$this->isNeededToSaveAddress($address->getData('quote_address_id'))) {
100+
continue;
101+
}
87102
$addressData = $this->objectCopyService->copyFieldsetToTarget(
88103
'order_address',
89104
'to_customer_address',
@@ -117,6 +132,26 @@ public function create($orderId)
117132
$account = $this->accountManagement->createAccount($customer);
118133
$order->setCustomerId($account->getId());
119134
$this->orderRepository->save($order);
135+
120136
return $account;
121137
}
138+
139+
/**
140+
* Check if we need to save address in address book.
141+
*
142+
* @param int $quoteAddressId
143+
*
144+
* @return bool
145+
*/
146+
private function isNeededToSaveAddress($quoteAddressId)
147+
{
148+
$saveInAddressBook = true;
149+
150+
$quoteAddress = $this->quoteAddressFactory->create()->load($quoteAddressId);
151+
if ($quoteAddress && $quoteAddress->getId()) {
152+
$saveInAddressBook = (int)$quoteAddress->getData('save_in_address_book');
153+
}
154+
155+
return $saveInAddressBook;
156+
}
122157
}

0 commit comments

Comments
 (0)