Skip to content

Commit

Permalink
Merge pull request #626 from magento-folks/bugfix
Browse files Browse the repository at this point in the history
[Folks] Bugfix
  • Loading branch information
slavvka committed May 16, 2016
2 parents 1121148 + ca7305e commit fc9cb7e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 35 deletions.
31 changes: 25 additions & 6 deletions app/code/Magento/Backend/Model/Session/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\GroupManagementInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Quote\Api\CartManagementInterface;

/**
* Adminhtml quote session
Expand Down Expand Up @@ -79,6 +81,11 @@ class Quote extends \Magento\Framework\Session\SessionManager
*/
protected $quoteFactory;

/**
* @var \Magento\Quote\Api\CartManagementInterface;
*/
private $cartManagement;

/**
* @param \Magento\Framework\App\Request\Http $request
* @param \Magento\Framework\Session\SidResolverInterface $sidResolver
Expand Down Expand Up @@ -143,15 +150,15 @@ public function __construct(
*/
public function getQuote()
{
$cartManagement = $this->getCartManagement();

if ($this->_quote === null) {
$this->_quote = $this->quoteFactory->create();
if ($this->getStoreId()) {
if (!$this->getQuoteId()) {
$this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId())
->setIsActive(false)
->setStoreId($this->getStoreId());
$this->quoteRepository->save($this->_quote);
$this->setQuoteId($this->_quote->getId());
$this->setQuoteId($cartManagement->createEmptyCart());
$this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
$this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId());
$this->_quote->setIsActive(false);
} else {
$this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
$this->_quote->setStoreId($this->getStoreId());
Expand All @@ -169,6 +176,18 @@ public function getQuote()
return $this->_quote;
}

/**
* @return CartManagementInterface
* @deprecated
*/
private function getCartManagement()
{
if ($this->cartManagement === null) {
$this->cartManagement = ObjectManager::getInstance()->get(CartManagementInterface::class);
}
return $this->cartManagement;
}

/**
* Retrieve store model object
*
Expand Down
57 changes: 36 additions & 21 deletions app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ class QuoteTest extends \PHPUnit_Framework_TestCase
*/
protected $quoteFactoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $cartManagementMock;

/**
* Set up
*
Expand Down Expand Up @@ -197,9 +202,16 @@ protected function setUp()
);

$this->quoteFactoryMock = $this->getMock('\Magento\Quote\Model\QuoteFactory', ['create'], [], '', false);
$this->cartManagementMock = $this->getMock(
\Magento\Quote\Api\CartManagementInterface::class,
[],
[],
'',
false
);

$this->quote = $this->getMock(
'Magento\Backend\Model\Session\Quote',
\Magento\Backend\Model\Session\Quote::class,
['getStoreId', 'getQuoteId', 'setQuoteId', 'hasCustomerId', 'getCustomerId'],
[
'request' => $this->requestMock,
Expand All @@ -217,10 +229,12 @@ protected function setUp()
'storeManager' => $this->storeManagerMock,
'groupManagement' => $this->groupManagementMock,
'quoteFactory' => $this->quoteFactoryMock
],
'',
true
]
);

$this->prepareObjectManager([
[\Magento\Quote\Api\CartManagementInterface::class, $this->cartManagementMock]
]);
}

/**
Expand All @@ -235,6 +249,8 @@ public function testGetQuoteWithoutQuoteId()
$customerId = 66;
$customerGroupId = 77;

$this->cartManagementMock->expects($this->once())->method('createEmptyCart')->willReturn($quoteId);

$this->quote->expects($this->any())
->method('getQuoteId')
->will($this->returnValue(null));
Expand Down Expand Up @@ -271,7 +287,6 @@ public function testGetQuoteWithoutQuoteId()
'setStoreId',
'setCustomerGroupId',
'setIsActive',
'getId',
'assignCustomer',
'setIgnoreOldQty',
'setIsSuperMode',
Expand All @@ -281,9 +296,7 @@ public function testGetQuoteWithoutQuoteId()
'',
false
);
$quoteMock->expects($this->once())
->method('setStoreId')
->with($storeId);
$this->quoteRepositoryMock->expects($this->once())->method('get')->willReturn($quoteMock);
$quoteMock->expects($this->once())
->method('setCustomerGroupId')
->with($customerGroupId)
Expand All @@ -292,9 +305,6 @@ public function testGetQuoteWithoutQuoteId()
->method('setIsActive')
->with(false)
->will($this->returnSelf());
$quoteMock->expects($this->once())
->method('getId')
->will($this->returnValue($quoteId));
$quoteMock->expects($this->once())
->method('assignCustomer')
->with($dataCustomerMock);
Expand All @@ -305,13 +315,6 @@ public function testGetQuoteWithoutQuoteId()
->method('setIsSuperMode')
->with(true);

$this->quoteFactoryMock->expects($this->once())
->method('create')
->will($this->returnValue($quoteMock));
$this->quoteRepositoryMock->expects($this->once())
->method('save')
->with($quoteMock);

$this->assertEquals($quoteMock, $this->quote->getQuote());
}

Expand Down Expand Up @@ -380,9 +383,6 @@ public function testGetQuoteWithQuoteId($customerId, $quoteCustomerId, $expected
->method('getCustomerId')
->will($this->returnValue($quoteCustomerId));

$this->quoteFactoryMock->expects($this->once())
->method('create')
->will($this->returnValue($quoteMock));
$this->quoteRepositoryMock->expects($this->once())
->method('get')
->with($quoteId)
Expand All @@ -401,4 +401,19 @@ public function getQuoteDataProvider()
'customer ids same' => [66, 66, 'never'],
];
}

/**
* @param array $map
* @deprecated
*/
private function prepareObjectManager($map)
{
$objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface');
$objectManagerMock->expects($this->any())->method('getInstance')->willReturnSelf();
$objectManagerMock->expects($this->any())->method('get')->will($this->returnValueMap($map));
$reflectionClass = new \ReflectionClass('Magento\Framework\App\ObjectManager');
$reflectionProperty = $reflectionClass->getProperty('_instance');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($objectManagerMock);
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Quote/Model/QuoteAddressValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function validate(\Magento\Quote\Api\Data\AddressInterface $addressData)
}, $this->customerRepository->getById($addressData->getCustomerId())->getAddresses());
if (!in_array($addressData->getCustomerAddressId(), $applicableAddressIds)) {
throw new \Magento\Framework\Exception\NoSuchEntityException(
__('Invalid address id %1', $addressData->getCustomerAddressId())
__('Invalid customer address id %1', $addressData->getCustomerAddressId())
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Quote/Model/QuoteRepository/SaveHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function save(CartInterface $quote)
/** @var \Magento\Quote\Model\Quote $quote */
// Quote Item processing
$items = $quote->getItems();
if ($items && $quote->getIsActive()) {
if ($items) {
foreach ($items as $item) {
/** @var \Magento\Quote\Model\Quote\Item $item */
if (!$item->isDeleted()) {
Expand All @@ -73,7 +73,7 @@ public function save(CartInterface $quote)

// Billing Address processing
$billingAddress = $quote->getBillingAddress();
if ($billingAddress && $quote->getIsActive()) {
if ($billingAddress) {
$this->billingAddressPersister->save($quote, $billingAddress);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public function testSaveForVirtualQuote()
->willReturn($this->itemMock);
$this->quoteMock->expects($this->once())->method('setLastAddedItem')->with($this->itemMock);
$this->quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($this->billingAddressMock);
$this->quoteMock->expects($this->exactly(2))->method('getIsActive')->willReturn(true);
$this->billingAddressPersister
->expects($this->once())
->method('save')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class Method extends Block
*/
public function selectShippingMethod(array $shippingMethod)
{
$this->_rootElement->find($this->shippingMethodsLink)->click();
if ($this->_rootElement->find($this->shippingMethodsLink)->isVisible()) {
$this->_rootElement->find($this->shippingMethodsLink)->click();
}
$selector = sprintf(
$this->shippingMethod,
$shippingMethod['shipping_service'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Customer\Model\Address $customerAddress */
$customerAddress = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create('Magento\Customer\Model\Address');
$customerAddress = $objectManager->create(\Magento\Customer\Model\Address::class);
$customerAddress->isObjectNew(true);
$customerAddress->setData(
[
Expand All @@ -24,5 +24,15 @@
'parent_id' => 1,
'region_id' => 1,
]
)->setCustomerId(1);
);
$customerAddress->save();

/** @var \Magento\Customer\Api\AddressRepositoryInterface $addressRepository */
$addressRepository = $objectManager->get(\Magento\Customer\Api\AddressRepositoryInterface::class);
$customerAddress = $addressRepository->getById(1);
$customerAddress->setCustomerId(1);
$customerAddress = $addressRepository->save($customerAddress);

/** @var \Magento\Customer\Model\AddressRegistry $addressRegistry */
$addressRegistry = $objectManager->get(\Magento\Customer\Model\AddressRegistry::class);
$addressRegistry->remove($customerAddress->getId());
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase
*/
public function testSubmit()
{
$this->markTestSkipped('MAGETWO-50989');
/**
* Preconditions:
* Load quote with Bundle product that has at least to child products
Expand Down

0 comments on commit fc9cb7e

Please sign in to comment.