diff --git a/app/code/Magento/Backend/Model/Session/Quote.php b/app/code/Magento/Backend/Model/Session/Quote.php index cad251a9a1d13..1ecbde15f840e 100644 --- a/app/code/Magento/Backend/Model/Session/Quote.php +++ b/app/code/Magento/Backend/Model/Session/Quote.php @@ -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 @@ -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 @@ -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()); @@ -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 * diff --git a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php index 7d01c47cccaef..d02422ebdc93d 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php @@ -92,6 +92,11 @@ class QuoteTest extends \PHPUnit_Framework_TestCase */ protected $quoteFactoryMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $cartManagementMock; + /** * Set 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, @@ -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] + ]); } /** @@ -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)); @@ -271,7 +287,6 @@ public function testGetQuoteWithoutQuoteId() 'setStoreId', 'setCustomerGroupId', 'setIsActive', - 'getId', 'assignCustomer', 'setIgnoreOldQty', 'setIsSuperMode', @@ -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) @@ -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); @@ -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()); } @@ -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) @@ -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); + } } diff --git a/app/code/Magento/Quote/Model/QuoteAddressValidator.php b/app/code/Magento/Quote/Model/QuoteAddressValidator.php index e2b20a52378a4..99451105ff290 100644 --- a/app/code/Magento/Quote/Model/QuoteAddressValidator.php +++ b/app/code/Magento/Quote/Model/QuoteAddressValidator.php @@ -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()) ); } } diff --git a/app/code/Magento/Quote/Model/QuoteRepository/SaveHandler.php b/app/code/Magento/Quote/Model/QuoteRepository/SaveHandler.php index f09195d0a34d6..1bece9a0887ca 100644 --- a/app/code/Magento/Quote/Model/QuoteRepository/SaveHandler.php +++ b/app/code/Magento/Quote/Model/QuoteRepository/SaveHandler.php @@ -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()) { @@ -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); } diff --git a/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/SaveHandlerTest.php b/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/SaveHandlerTest.php index da6154bc9c1a6..33091fc00ca52 100644 --- a/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/SaveHandlerTest.php +++ b/app/code/Magento/Quote/Test/Unit/Model/QuoteRepository/SaveHandlerTest.php @@ -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') diff --git a/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/Create/Shipping/Method.php b/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/Create/Shipping/Method.php index a8cb5b244154f..3d9673e29805d 100644 --- a/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/Create/Shipping/Method.php +++ b/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/Create/Shipping/Method.php @@ -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'], diff --git a/dev/tests/integration/testsuite/Magento/Customer/_files/customer_address.php b/dev/tests/integration/testsuite/Magento/Customer/_files/customer_address.php index 95225c3bc6b31..4d9a12a6e1f10 100644 --- a/dev/tests/integration/testsuite/Magento/Customer/_files/customer_address.php +++ b/dev/tests/integration/testsuite/Magento/Customer/_files/customer_address.php @@ -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( [ @@ -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()); diff --git a/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteManagementTest.php b/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteManagementTest.php index 65bba35bfa986..a52cd44302284 100644 --- a/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteManagementTest.php +++ b/dev/tests/integration/testsuite/Magento/Quote/Model/QuoteManagementTest.php @@ -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