Skip to content

Commit a1d4a1d

Browse files
committed
MAGETWO-58269: [GitHub] Error in 'useForShipping'-parameter when saving billing address via API #6557
1 parent 6af30d6 commit a1d4a1d

File tree

6 files changed

+227
-5
lines changed

6 files changed

+227
-5
lines changed

app/code/Magento/Quote/Api/GuestBillingAddressManagementInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ interface GuestBillingAddressManagementInterface
1616
*
1717
* @param string $cartId The cart ID.
1818
* @param \Magento\Quote\Api\Data\AddressInterface $address Billing address data.
19+
* @param bool $useForShipping
1920
* @return int Address ID.
2021
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist.
2122
* @throws \Magento\Framework\Exception\InputException The specified cart ID or address data is not valid.
2223
*/
23-
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address);
24+
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address, $useForShipping = false);
2425

2526
/**
2627
* Return the billing address for a specified quote.

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class BillingAddressManagement implements BillingAddressManagementInterface
4040
*/
4141
protected $addressRepository;
4242

43+
/**
44+
* @var \Magento\Quote\Model\ShippingAddressAssignment
45+
*/
46+
private $shippingAddressAssignment;
47+
4348
/**
4449
* Constructs a quote billing address service object.
4550
*
@@ -71,6 +76,7 @@ public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $addres
7176
$quote->removeAddress($quote->getBillingAddress()->getId());
7277
$quote->setBillingAddress($address);
7378
try {
79+
$this->getShippingAddressAssignment()->setAddress($quote, $address, $useForShipping);
7480
$quote->setDataChanges(true);
7581
$this->quoteRepository->save($quote);
7682
} catch (\Exception $e) {
@@ -88,4 +94,17 @@ public function get($cartId)
8894
$cart = $this->quoteRepository->getActive($cartId);
8995
return $cart->getBillingAddress();
9096
}
97+
98+
/**
99+
* @return \Magento\Quote\Model\ShippingAddressAssignment
100+
* @deprecated
101+
*/
102+
private function getShippingAddressAssignment()
103+
{
104+
if (!$this->shippingAddressAssignment) {
105+
$this->shippingAddressAssignment = ObjectManager::getInstance()
106+
->get(\Magento\Quote\Model\ShippingAddressAssignment::class);
107+
}
108+
return $this->shippingAddressAssignment;
109+
}
91110
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public function __construct(
4242
/**
4343
* {@inheritDoc}
4444
*/
45-
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
45+
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address, $useForShipping = false)
4646
{
4747
/** @var $quoteIdMask QuoteIdMask */
4848
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
49-
return $this->billingAddressManagement->assign($quoteIdMask->getQuoteId(), $address);
49+
return $this->billingAddressManagement->assign($quoteIdMask->getQuoteId(), $address, $useForShipping);
5050
}
5151

5252
/**
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Quote\Model;
8+
9+
class ShippingAddressAssignment
10+
{
11+
/**
12+
* @var \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor
13+
*/
14+
private $shippingAssignmentProcessor;
15+
16+
/**
17+
* @var \Magento\Quote\Api\Data\CartExtensionFactory
18+
*/
19+
private $cartExtensionFactory;
20+
21+
/**
22+
* ShippingAddressAssignment constructor.
23+
* @param \Magento\Quote\Api\Data\CartExtensionFactory $cartExtensionFactory
24+
* @param Quote\ShippingAssignment\ShippingAssignmentProcessor $shippingAssignmentProcessor
25+
*/
26+
public function __construct(
27+
\Magento\Quote\Api\Data\CartExtensionFactory $cartExtensionFactory,
28+
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor $shippingAssignmentProcessor
29+
) {
30+
$this->shippingAssignmentProcessor = $shippingAssignmentProcessor;;
31+
$this->cartExtensionFactory = $cartExtensionFactory;
32+
}
33+
34+
/**
35+
* @param \Magento\Quote\Api\Data\CartInterface $quote
36+
* @param \Magento\Quote\Api\Data\AddressInterface $address
37+
* @param bool $useForShipping
38+
* @return void
39+
*/
40+
public function setAddress(
41+
\Magento\Quote\Api\Data\CartInterface $quote,
42+
\Magento\Quote\Api\Data\AddressInterface $address,
43+
$useForShipping = false
44+
) {
45+
if ($useForShipping) {
46+
$quote->removeAddress($quote->getShippingAddress()->getId());
47+
$address->setSameAsBilling(1);
48+
$address->setCollectShippingRates(true);
49+
} else {
50+
$address = $quote->getShippingAddress()->setSameAsBilling(0);
51+
}
52+
53+
$quote->setShippingAddress($address);
54+
$cartExtension = $quote->getExtensionAttributes();
55+
if ($cartExtension === null) {
56+
$cartExtension = $this->cartExtensionFactory->create();
57+
}
58+
/** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
59+
$shippingAssignment = $this->shippingAssignmentProcessor->create($quote);
60+
$cartExtension->setShippingAssignments([$shippingAssignment]);
61+
$quote->setExtensionAttributes($cartExtension);
62+
}
63+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Quote\Test\Unit\Model;
8+
9+
class ShippingAddressAssignmentTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var \Magento\Quote\Model\ShippingAddressAssignment
13+
*/
14+
private $model;
15+
16+
/**
17+
* @var \PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
private $shippingAssignmentProcessorMock;
20+
21+
/**
22+
* @var \PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $cartExtensionFactoryMock;
25+
26+
/**
27+
* @var \PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $quoteMock;
30+
31+
/**
32+
* @var \PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
private $addressMock;
35+
36+
/**
37+
* @var \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $extensionAttributeMock;
40+
41+
/**
42+
* @var \PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
private $shippingAssignmentMock;
45+
46+
public function setUp()
47+
{
48+
$this->cartExtensionFactoryMock = $this->getMock(
49+
\Magento\Quote\Api\Data\CartExtensionFactory::class,
50+
['create'],
51+
[],
52+
'',
53+
false
54+
);
55+
$this->shippingAssignmentProcessorMock = $this->getMock(
56+
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor::class,
57+
[],
58+
[],
59+
'',
60+
false
61+
);
62+
$this->quoteMock = $this->getMock(\Magento\Quote\Model\Quote::class, [], [], '', false);
63+
$this->addressMock = $this->getMock(\Magento\Quote\Model\Quote\Address::class, [], [], '', false);
64+
$this->extensionAttributeMock = $this->getMock(
65+
\Magento\Quote\Api\Data\CartExtension::class,
66+
['setShippingAssignments'],
67+
[],
68+
'',
69+
false
70+
);
71+
72+
$this->shippingAssignmentMock = $this->getMock(\Magento\Quote\Api\Data\ShippingAssignmentInterface::class);
73+
//shipping assignment processing
74+
$this->quoteMock->expects($this->once())->method('getExtensionAttributes')->willReturn(null);
75+
$this->cartExtensionFactoryMock
76+
->expects($this->once())
77+
->method('create')
78+
->willReturn($this->extensionAttributeMock);
79+
$this->shippingAssignmentProcessorMock
80+
->expects($this->once())
81+
->method('create')
82+
->willReturn($this->shippingAssignmentMock);
83+
$this->extensionAttributeMock
84+
->expects($this->once())
85+
->method('setShippingAssignments')
86+
->with([$this->shippingAssignmentMock])
87+
->willReturnSelf();
88+
$this->quoteMock->expects($this->once())->method('setExtensionAttributes')->with($this->extensionAttributeMock);
89+
$this->model = new \Magento\Quote\Model\ShippingAddressAssignment(
90+
$this->cartExtensionFactoryMock,
91+
$this->shippingAssignmentProcessorMock
92+
);
93+
}
94+
95+
public function testSetAddressUseForShippingTrue()
96+
{
97+
$addressId = 1;
98+
$addressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
99+
$this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($addressMock);
100+
$addressMock->expects($this->once())->method('getId')->willReturn($addressId);
101+
$this->addressMock->expects($this->once())->method('setSameAsBilling')->with(1);
102+
$this->quoteMock->expects($this->once())->method('removeAddress')->with($addressId);
103+
$this->quoteMock->expects($this->once())->method('setShippingAddress')->with($this->addressMock);
104+
$this->model->setAddress($this->quoteMock, $this->addressMock, true);
105+
}
106+
107+
public function testSetAddressUseForShippingFalse()
108+
{
109+
$addressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
110+
$this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($addressMock);
111+
$addressMock->expects($this->once())->method('setSameAsBilling')->with(0)->willReturnSelf();
112+
$this->quoteMock->expects($this->once())->method('setShippingAddress')->with($addressMock);
113+
$this->model->setAddress($this->quoteMock, $this->addressMock, false);
114+
}
115+
}

dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestBillingAddressManagementTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public function testGetAddress()
8888

8989
/**
9090
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
91+
* @dataProvider setAddressDataProvider
9192
*/
92-
public function testSetAddress()
93+
public function testSetAddress($useForShipping)
9394
{
9495
/** @var \Magento\Quote\Model\Quote $quote */
9596
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
@@ -125,8 +126,9 @@ public function testSetAddress()
125126
'fax' => '44332255',
126127
];
127128
$requestData = [
128-
"cartId" => $cartId,
129+
'cartId' => $cartId,
129130
'address' => $addressData,
131+
'useForShipping' => $useForShipping
130132
];
131133

132134
$addressId = $this->_webApiCall($serviceInfo, $requestData);
@@ -149,5 +151,27 @@ public function testSetAddress()
149151
foreach ($addressData as $key => $value) {
150152
$this->assertEquals($value, $savedData[$key]);
151153
}
154+
$address = $quote->getShippingAddress();
155+
$address->getRegionCode();
156+
$savedData = $address->getData();
157+
if ($useForShipping) {
158+
//check that shipping address set
159+
$this->assertEquals('shipping', $savedData['address_type']);
160+
$this->assertEquals(1, $savedData['same_as_billing']);
161+
//check the rest of fields
162+
foreach ($addressData as $key => $value) {
163+
$this->assertEquals($value, $savedData[$key]);
164+
}
165+
} else {
166+
$this->assertEquals(0, $savedData['same_as_billing']);
167+
}
152168
}
169+
170+
public function setAddressDataProvider()
171+
{
172+
return [
173+
[true],
174+
[false]
175+
];
176+
}
153177
}

0 commit comments

Comments
 (0)