Skip to content

Commit

Permalink
MAGETWO-58269: [GitHub] Error in 'useForShipping'-parameter when savi…
Browse files Browse the repository at this point in the history
…ng billing address via API #6557
  • Loading branch information
irenelagno committed Sep 16, 2016
1 parent 6af30d6 commit a1d4a1d
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ interface GuestBillingAddressManagementInterface
*
* @param string $cartId The cart ID.
* @param \Magento\Quote\Api\Data\AddressInterface $address Billing address data.
* @param bool $useForShipping
* @return int Address ID.
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist.
* @throws \Magento\Framework\Exception\InputException The specified cart ID or address data is not valid.
*/
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address);
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address, $useForShipping = false);

/**
* Return the billing address for a specified quote.
Expand Down
19 changes: 19 additions & 0 deletions app/code/Magento/Quote/Model/BillingAddressManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class BillingAddressManagement implements BillingAddressManagementInterface
*/
protected $addressRepository;

/**
* @var \Magento\Quote\Model\ShippingAddressAssignment
*/
private $shippingAddressAssignment;

/**
* Constructs a quote billing address service object.
*
Expand Down Expand Up @@ -71,6 +76,7 @@ public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $addres
$quote->removeAddress($quote->getBillingAddress()->getId());
$quote->setBillingAddress($address);
try {
$this->getShippingAddressAssignment()->setAddress($quote, $address, $useForShipping);
$quote->setDataChanges(true);
$this->quoteRepository->save($quote);
} catch (\Exception $e) {
Expand All @@ -88,4 +94,17 @@ public function get($cartId)
$cart = $this->quoteRepository->getActive($cartId);
return $cart->getBillingAddress();
}

/**
* @return \Magento\Quote\Model\ShippingAddressAssignment
* @deprecated
*/
private function getShippingAddressAssignment()
{
if (!$this->shippingAddressAssignment) {
$this->shippingAddressAssignment = ObjectManager::getInstance()
->get(\Magento\Quote\Model\ShippingAddressAssignment::class);
}
return $this->shippingAddressAssignment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function __construct(
/**
* {@inheritDoc}
*/
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address)
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address, $useForShipping = false)
{
/** @var $quoteIdMask QuoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
return $this->billingAddressManagement->assign($quoteIdMask->getQuoteId(), $address);
return $this->billingAddressManagement->assign($quoteIdMask->getQuoteId(), $address, $useForShipping);
}

/**
Expand Down
63 changes: 63 additions & 0 deletions app/code/Magento/Quote/Model/ShippingAddressAssignment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Quote\Model;

class ShippingAddressAssignment
{
/**
* @var \Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor
*/
private $shippingAssignmentProcessor;

/**
* @var \Magento\Quote\Api\Data\CartExtensionFactory
*/
private $cartExtensionFactory;

/**
* ShippingAddressAssignment constructor.
* @param \Magento\Quote\Api\Data\CartExtensionFactory $cartExtensionFactory
* @param Quote\ShippingAssignment\ShippingAssignmentProcessor $shippingAssignmentProcessor
*/
public function __construct(
\Magento\Quote\Api\Data\CartExtensionFactory $cartExtensionFactory,
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor $shippingAssignmentProcessor
) {
$this->shippingAssignmentProcessor = $shippingAssignmentProcessor;;
$this->cartExtensionFactory = $cartExtensionFactory;
}

/**
* @param \Magento\Quote\Api\Data\CartInterface $quote
* @param \Magento\Quote\Api\Data\AddressInterface $address
* @param bool $useForShipping
* @return void
*/
public function setAddress(
\Magento\Quote\Api\Data\CartInterface $quote,
\Magento\Quote\Api\Data\AddressInterface $address,
$useForShipping = false
) {
if ($useForShipping) {
$quote->removeAddress($quote->getShippingAddress()->getId());
$address->setSameAsBilling(1);
$address->setCollectShippingRates(true);
} else {
$address = $quote->getShippingAddress()->setSameAsBilling(0);
}

$quote->setShippingAddress($address);
$cartExtension = $quote->getExtensionAttributes();
if ($cartExtension === null) {
$cartExtension = $this->cartExtensionFactory->create();
}
/** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */
$shippingAssignment = $this->shippingAssignmentProcessor->create($quote);
$cartExtension->setShippingAssignments([$shippingAssignment]);
$quote->setExtensionAttributes($cartExtension);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Quote\Test\Unit\Model;

class ShippingAddressAssignmentTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Quote\Model\ShippingAddressAssignment
*/
private $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $shippingAssignmentProcessorMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $cartExtensionFactoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $quoteMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $addressMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $extensionAttributeMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $shippingAssignmentMock;

public function setUp()
{
$this->cartExtensionFactoryMock = $this->getMock(
\Magento\Quote\Api\Data\CartExtensionFactory::class,
['create'],
[],
'',
false
);
$this->shippingAssignmentProcessorMock = $this->getMock(
\Magento\Quote\Model\Quote\ShippingAssignment\ShippingAssignmentProcessor::class,
[],
[],
'',
false
);
$this->quoteMock = $this->getMock(\Magento\Quote\Model\Quote::class, [], [], '', false);
$this->addressMock = $this->getMock(\Magento\Quote\Model\Quote\Address::class, [], [], '', false);
$this->extensionAttributeMock = $this->getMock(
\Magento\Quote\Api\Data\CartExtension::class,
['setShippingAssignments'],
[],
'',
false
);

$this->shippingAssignmentMock = $this->getMock(\Magento\Quote\Api\Data\ShippingAssignmentInterface::class);
//shipping assignment processing
$this->quoteMock->expects($this->once())->method('getExtensionAttributes')->willReturn(null);
$this->cartExtensionFactoryMock
->expects($this->once())
->method('create')
->willReturn($this->extensionAttributeMock);
$this->shippingAssignmentProcessorMock
->expects($this->once())
->method('create')
->willReturn($this->shippingAssignmentMock);
$this->extensionAttributeMock
->expects($this->once())
->method('setShippingAssignments')
->with([$this->shippingAssignmentMock])
->willReturnSelf();
$this->quoteMock->expects($this->once())->method('setExtensionAttributes')->with($this->extensionAttributeMock);
$this->model = new \Magento\Quote\Model\ShippingAddressAssignment(
$this->cartExtensionFactoryMock,
$this->shippingAssignmentProcessorMock
);
}

public function testSetAddressUseForShippingTrue()
{
$addressId = 1;
$addressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
$this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($addressMock);
$addressMock->expects($this->once())->method('getId')->willReturn($addressId);
$this->addressMock->expects($this->once())->method('setSameAsBilling')->with(1);
$this->quoteMock->expects($this->once())->method('removeAddress')->with($addressId);
$this->quoteMock->expects($this->once())->method('setShippingAddress')->with($this->addressMock);
$this->model->setAddress($this->quoteMock, $this->addressMock, true);
}

public function testSetAddressUseForShippingFalse()
{
$addressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class);
$this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($addressMock);
$addressMock->expects($this->once())->method('setSameAsBilling')->with(0)->willReturnSelf();
$this->quoteMock->expects($this->once())->method('setShippingAddress')->with($addressMock);
$this->model->setAddress($this->quoteMock, $this->addressMock, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public function testGetAddress()

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @dataProvider setAddressDataProvider
*/
public function testSetAddress()
public function testSetAddress($useForShipping)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
Expand Down Expand Up @@ -125,8 +126,9 @@ public function testSetAddress()
'fax' => '44332255',
];
$requestData = [
"cartId" => $cartId,
'cartId' => $cartId,
'address' => $addressData,
'useForShipping' => $useForShipping
];

$addressId = $this->_webApiCall($serviceInfo, $requestData);
Expand All @@ -149,5 +151,27 @@ public function testSetAddress()
foreach ($addressData as $key => $value) {
$this->assertEquals($value, $savedData[$key]);
}
$address = $quote->getShippingAddress();
$address->getRegionCode();
$savedData = $address->getData();
if ($useForShipping) {
//check that shipping address set
$this->assertEquals('shipping', $savedData['address_type']);
$this->assertEquals(1, $savedData['same_as_billing']);
//check the rest of fields
foreach ($addressData as $key => $value) {
$this->assertEquals($value, $savedData[$key]);
}
} else {
$this->assertEquals(0, $savedData['same_as_billing']);
}
}

public function setAddressDataProvider()
{
return [
[true],
[false]
];
}
}

0 comments on commit a1d4a1d

Please sign in to comment.