Skip to content

Commit 179f984

Browse files
committed
MAGETWO-9007: Get MAGETWO-52856 into Magento 2.1.x
1 parent 13d3888 commit 179f984

File tree

5 files changed

+114
-27
lines changed

5 files changed

+114
-27
lines changed

app/code/Magento/Checkout/Model/PaymentInformationManagement.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
1414
{
1515
/**
1616
* @var \Magento\Quote\Api\BillingAddressManagementInterface
17+
* @deprecated This call was substituted to eliminate extra quote::save call.
1718
*/
1819
protected $billingAddressManagement;
1920

@@ -44,6 +45,13 @@ class PaymentInformationManagement implements \Magento\Checkout\Api\PaymentInfor
4445
*/
4546
private $logger;
4647

48+
/**
49+
* Provide active quote.
50+
*
51+
* @var \Magento\Quote\Api\CartRepositoryInterface
52+
*/
53+
private $cartRepository;
54+
4755
/**
4856
* @param \Magento\Quote\Api\BillingAddressManagementInterface $billingAddressManagement
4957
* @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
@@ -102,7 +110,19 @@ public function savePaymentInformation(
102110
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
103111
) {
104112
if ($billingAddress) {
105-
$this->billingAddressManagement->assign($cartId, $billingAddress);
113+
/** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */
114+
$quoteRepository = $this->getCartRepository();
115+
/** @var \Magento\Quote\Model\Quote $quote */
116+
$quote = $quoteRepository->getActive($cartId);
117+
$quote->removeAddress($quote->getBillingAddress()->getId());
118+
$quote->setBillingAddress($billingAddress);
119+
$quote->setDataChanges(true);
120+
$shippingAddress = $quote->getShippingAddress();
121+
if ($shippingAddress && $shippingAddress->getShippingMethod()) {
122+
$shippingDataArray = explode('_', $shippingAddress->getShippingMethod());
123+
$shippingCarrier = array_shift($shippingDataArray);
124+
$shippingAddress->setLimitCarrier($shippingCarrier);
125+
}
106126
}
107127
$this->paymentMethodManagement->set($cartId, $paymentMethod);
108128

@@ -133,7 +153,21 @@ private function getLogger()
133153
if (!$this->logger) {
134154
$this->logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
135155
}
136-
137156
return $this->logger;
138157
}
158+
159+
/**
160+
* Get Cart repository instance.
161+
*
162+
* @return \Magento\Quote\Api\CartRepositoryInterface
163+
* @deprecated
164+
*/
165+
private function getCartRepository()
166+
{
167+
if (!$this->cartRepository) {
168+
$this->cartRepository = \Magento\Framework\App\ObjectManager::getInstance()
169+
->get(\Magento\Quote\Api\CartRepositoryInterface::class);
170+
}
171+
return $this->cartRepository;
172+
}
139173
}

app/code/Magento/Checkout/Model/ShippingInformationManagement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public function saveAddressInformation(
144144

145145
/** @var \Magento\Quote\Model\Quote $quote */
146146
$quote = $this->quoteRepository->getActive($cartId);
147+
$address->setLimitCarrier($carrierCode);
147148
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
148149
$this->validateQuote($quote);
149150
$quote->setIsMultiShipping(false);

app/code/Magento/Checkout/Test/Unit/Model/PaymentInformationManagementTest.php

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class PaymentInformationManagementTest extends \PHPUnit_Framework_TestCase
3333
*/
3434
private $loggerMock;
3535

36+
/**
37+
* @var \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $cartRepositoryMock;
40+
3641
protected function setUp()
3742
{
3843
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -45,7 +50,7 @@ protected function setUp()
4550
$this->cartManagementMock = $this->getMock(\Magento\Quote\Api\CartManagementInterface::class);
4651

4752
$this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class);
48-
53+
$this->cartRepositoryMock = $this->getMockBuilder(\Magento\Quote\Api\CartRepositoryInterface::class)->getMock();
4954
$this->model = $objectManager->getObject(
5055
\Magento\Checkout\Model\PaymentInformationManagement::class,
5156
[
@@ -55,6 +60,7 @@ protected function setUp()
5560
]
5661
);
5762
$objectManager->setBackwardCompatibleProperty($this->model, 'logger', $this->loggerMock);
63+
$objectManager->setBackwardCompatibleProperty($this->model, 'cartRepository', $this->cartRepositoryMock);
5864
}
5965

6066
public function testSavePaymentInformationAndPlaceOrder()
@@ -64,9 +70,7 @@ public function testSavePaymentInformationAndPlaceOrder()
6470
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
6571
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
6672

67-
$this->billingAddressManagementMock->expects($this->once())
68-
->method('assign')
69-
->with($cartId, $billingAddressMock);
73+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
7074
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
7175
$this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
7276

@@ -86,9 +90,7 @@ public function testSavePaymentInformationAndPlaceOrderException()
8690
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
8791
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
8892

89-
$this->billingAddressManagementMock->expects($this->once())
90-
->method('assign')
91-
->with($cartId, $billingAddressMock);
93+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
9294
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
9395
$exception = new \Exception(__('DB exception'));
9496
$this->loggerMock->expects($this->once())->method('critical');
@@ -103,7 +105,6 @@ public function testSavePaymentInformationAndPlaceOrderIfBillingAddressNotExist(
103105
$orderId = 200;
104106
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
105107

106-
$this->billingAddressManagementMock->expects($this->never())->method('assign');
107108
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
108109
$this->cartManagementMock->expects($this->once())->method('placeOrder')->with($cartId)->willReturn($orderId);
109110

@@ -119,9 +120,7 @@ public function testSavePaymentInformation()
119120
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
120121
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
121122

122-
$this->billingAddressManagementMock->expects($this->once())
123-
->method('assign')
124-
->with($cartId, $billingAddressMock);
123+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
125124
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
126125

127126
$this->assertTrue($this->model->savePaymentInformation($cartId, $paymentMock, $billingAddressMock));
@@ -132,7 +131,6 @@ public function testSavePaymentInformationWithoutBillingAddress()
132131
$cartId = 100;
133132
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
134133

135-
$this->billingAddressManagementMock->expects($this->never())->method('assign');
136134
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
137135

138136
$this->assertTrue($this->model->savePaymentInformation($cartId, $paymentMock));
@@ -148,9 +146,8 @@ public function testSavePaymentInformationAndPlaceOrderWithLocalizedException()
148146
$paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class);
149147
$billingAddressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
150148

151-
$this->billingAddressManagementMock->expects($this->once())
152-
->method('assign')
153-
->with($cartId, $billingAddressMock);
149+
$this->getMockForAssignBillingAddress($cartId, $billingAddressMock);
150+
154151
$this->paymentMethodManagementMock->expects($this->once())->method('set')->with($cartId, $paymentMock);
155152
$phrase = new \Magento\Framework\Phrase(__('DB exception'));
156153
$exception = new \Magento\Framework\Exception\LocalizedException($phrase);
@@ -159,4 +156,31 @@ public function testSavePaymentInformationAndPlaceOrderWithLocalizedException()
159156

160157
$this->model->savePaymentInformationAndPlaceOrder($cartId, $paymentMock, $billingAddressMock);
161158
}
159+
160+
/**
161+
* @param int $cartId
162+
* @param \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
163+
*/
164+
private function getMockForAssignBillingAddress($cartId, $billingAddressMock)
165+
{
166+
$billingAddressId = 1;
167+
$quoteMock = $this->getMock(\Magento\Quote\Model\Quote::class, [], [], '', false);
168+
$quoteBillingAddress = $this->getMock(\Magento\Quote\Model\Quote\Address::class, [], [], '', false);
169+
$quoteShippingAddress = $this->getMock(
170+
\Magento\Quote\Model\Quote\Address::class,
171+
['setLimitCarrier', 'getShippingMethod'],
172+
[],
173+
'',
174+
false
175+
);
176+
$this->cartRepositoryMock->expects($this->any())->method('getActive')->with($cartId)->willReturn($quoteMock);
177+
$quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($quoteBillingAddress);
178+
$quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($quoteShippingAddress);
179+
$quoteBillingAddress->expects($this->once())->method('getId')->willReturn($billingAddressId);
180+
$quoteMock->expects($this->once())->method('removeAddress')->with($billingAddressId);
181+
$quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddressMock);
182+
$quoteMock->expects($this->once())->method('setDataChanges')->willReturnSelf();
183+
$quoteShippingAddress->expects($this->any())->method('getShippingMethod')->willReturn('flatrate_flatrate');
184+
$quoteShippingAddress->expects($this->once())->method('setLimitCarrier')->with('flatrate')->willReturnSelf();
185+
}
162186
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(
4747
}
4848

4949
/**
50-
* Populate customer model
50+
* Populate customer model.
5151
*
5252
* @param Quote $quote
5353
* @return void
@@ -62,8 +62,6 @@ public function populateCustomerInfo(QuoteEntity $quote)
6262
$quote->getPasswordHash()
6363
);
6464
$quote->setCustomer($customer);
65-
} else {
66-
$this->customerRepository->save($customer);
6765
}
6866
if (!$quote->getBillingAddress()->getId() && $customer->getDefaultBilling()) {
6967
$quote->getBillingAddress()->importCustomerAddressData(

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class CustomerManagementTest extends \PHPUnit_Framework_TestCase
5353
protected function setUp()
5454
{
5555
$this->customerRepositoryMock = $this->getMockForAbstractClass(
56-
'Magento\Customer\Api\CustomerRepositoryInterface',
56+
\Magento\Customer\Api\CustomerRepositoryInterface::class,
5757
[],
5858
'',
5959
false,
@@ -62,7 +62,7 @@ protected function setUp()
6262
['getById']
6363
);
6464
$this->customerAddressRepositoryMock = $this->getMockForAbstractClass(
65-
'Magento\Customer\Api\AddressRepositoryInterface',
65+
\Magento\Customer\Api\AddressRepositoryInterface::class,
6666
[],
6767
'',
6868
false,
@@ -71,7 +71,7 @@ protected function setUp()
7171
['getById']
7272
);
7373
$this->accountManagementMock = $this->getMockForAbstractClass(
74-
'Magento\Customer\Api\AccountManagementInterface',
74+
\Magento\Customer\Api\AccountManagementInterface::class,
7575
[],
7676
'',
7777
false,
@@ -80,21 +80,21 @@ protected function setUp()
8080
[]
8181
);
8282
$this->quoteMock = $this->getMock(
83-
'Magento\Quote\Model\Quote',
83+
\Magento\Quote\Model\Quote::class,
8484
['getId', 'getCustomer', 'getBillingAddress', 'getShippingAddress', 'setCustomer', 'getPasswordHash'],
8585
[],
8686
'',
8787
false
8888
);
8989
$this->quoteAddressMock = $this->getMock(
90-
'Magento\Quote\Model\Quote\Address',
90+
\Magento\Quote\Model\Quote\Address::class,
9191
[],
9292
[],
9393
'',
9494
false
9595
);
9696
$this->customerMock = $this->getMockForAbstractClass(
97-
'Magento\Customer\Api\Data\CustomerInterface',
97+
\Magento\Customer\Api\Data\CustomerInterface::class,
9898
[],
9999
'',
100100
false,
@@ -103,7 +103,7 @@ protected function setUp()
103103
['getId', 'getDefaultBilling']
104104
);
105105
$this->customerAddressMock = $this->getMockForAbstractClass(
106-
'Magento\Customer\Api\Data\AddressInterface',
106+
\Magento\Customer\Api\Data\AddressInterface::class,
107107
[],
108108
'',
109109
false,
@@ -158,4 +158,34 @@ public function testPopulateCustomerInfo()
158158
->willReturn($this->customerMock);
159159
$this->customerManagement->populateCustomerInfo($this->quoteMock);
160160
}
161+
162+
public function testPopulateCustomerInfoForExistingCustomer()
163+
{
164+
$this->quoteMock->expects($this->once())
165+
->method('getCustomer')
166+
->willReturn($this->customerMock);
167+
$this->customerMock->expects($this->atLeastOnce())
168+
->method('getId')
169+
->willReturn(1);
170+
$this->customerMock->expects($this->atLeastOnce())
171+
->method('getDefaultBilling')
172+
->willReturn(100500);
173+
$this->quoteMock->expects($this->atLeastOnce())
174+
->method('getBillingAddress')
175+
->willReturn($this->quoteAddressMock);
176+
$this->quoteMock->expects($this->atLeastOnce())
177+
->method('getShippingAddress')
178+
->willReturn($this->quoteAddressMock);
179+
$this->quoteAddressMock->expects($this->atLeastOnce())
180+
->method('getId')
181+
->willReturn(null);
182+
$this->customerAddressRepositoryMock->expects($this->atLeastOnce())
183+
->method('getById')
184+
->with(100500)
185+
->willReturn($this->customerAddressMock);
186+
$this->quoteAddressMock->expects($this->atLeastOnce())
187+
->method('importCustomerAddressData')
188+
->willReturnSelf();
189+
$this->customerManagement->populateCustomerInfo($this->quoteMock);
190+
}
161191
}

0 commit comments

Comments
 (0)