Skip to content

Commit c561c8f

Browse files
author
Sergey Nosov
committed
Merge remote-tracking branch 'origin/MDVA-280' into 2.0.6_backlog
2 parents b5c2446 + c852db6 commit c561c8f

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Magento\Sales\Api\OrderManagementInterface as OrderManagement;
2222
use Magento\Store\Model\StoreManagerInterface;
2323
use Magento\Quote\Model\Quote\Address;
24+
use Magento\Framework\App\ObjectManager;
25+
use Magento\Quote\Model\QuoteIdMaskFactory;
2426

2527
/**
2628
* Class QuoteManagement
@@ -130,6 +132,11 @@ class QuoteManagement implements \Magento\Quote\Api\CartManagementInterface
130132
*/
131133
protected $quoteFactory;
132134

135+
/**
136+
* @var QuoteIdMaskFactory
137+
*/
138+
private $quoteIdMaskFactory;
139+
133140
/**
134141
* @param EventManager $eventManager
135142
* @param QuoteValidator $quoteValidator
@@ -262,6 +269,12 @@ public function assignCustomer($cartId, $customerId, $storeId)
262269

263270
$quote->setCustomer($customer);
264271
$quote->setCustomerIsGuest(0);
272+
$quoteIdMaskFactory = $this->getQuoteIdMaskFactory();
273+
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
274+
$quoteIdMask = $quoteIdMaskFactory->create()->load($cartId, 'quote_id');
275+
if ($quoteIdMask->getId()) {
276+
$quoteIdMask->delete();
277+
}
265278
$this->quoteRepository->save($quote);
266279
return true;
267280

@@ -547,4 +560,16 @@ protected function _prepareCustomerQuote($quote)
547560
$shipping->setIsDefaultBilling(true);
548561
}
549562
}
563+
564+
/**
565+
* @return QuoteIdMaskFactory
566+
* @deprecated
567+
*/
568+
private function getQuoteIdMaskFactory()
569+
{
570+
if (!$this->quoteIdMaskFactory) {
571+
$this->quoteIdMaskFactory = ObjectManager::getInstance()->get(QuoteIdMaskFactory::class);
572+
}
573+
return $this->quoteIdMaskFactory;
574+
}
550575
}

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ class QuoteManagementTest extends \PHPUnit_Framework_TestCase
126126
*/
127127
protected $quoteFactoryMock;
128128

129+
/**
130+
* @var \PHPUnit_Framework_MockObject_MockObject
131+
*/
132+
private $quoteIdMock;
133+
129134
/**
130135
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
131136
*/
@@ -238,7 +243,6 @@ protected function setUp()
238243
);
239244

240245
$this->quoteFactoryMock = $this->getMock('\Magento\Quote\Model\QuoteFactory', ['create'], [], '', false);
241-
242246
$this->model = $objectManager->getObject(
243247
'\Magento\Quote\Model\QuoteManagement',
244248
[
@@ -264,6 +268,12 @@ protected function setUp()
264268
'quoteFactory' => $this->quoteFactoryMock
265269
]
266270
);
271+
272+
// Set the new dependency
273+
$this->quoteIdMock = $this->getMock('Magento\Quote\Model\QuoteIdMask', [], [], '', false);
274+
$quoteIdFactoryMock = $this->getMock(\Magento\Quote\Model\QuoteIdMaskFactory::class, ['create'], [], '', false);
275+
$this->setPropertyValue($this->model, 'quoteIdMaskFactory', $quoteIdFactoryMock);
276+
267277
}
268278

269279
public function testCreateEmptyCartAnonymous()
@@ -508,6 +518,13 @@ public function testAssignCustomer()
508518
$customerId = 455;
509519
$storeId = 5;
510520

521+
$this->getPropertyValue($this->model, 'quoteIdMaskFactory')
522+
->expects($this->once())
523+
->method('create')
524+
->willReturn($this->quoteIdMock);
525+
$this->quoteIdMock->expects($this->once())->method('load')->with($cartId, 'quote_id')->willReturnSelf();
526+
$this->quoteIdMock->expects($this->once())->method('getId')->willReturn(10);
527+
$this->quoteIdMock->expects($this->once())->method('delete');
511528
$quoteMock = $this->getMock(
512529
'\Magento\Quote\Model\Quote',
513530
['getCustomerId', 'setCustomer', 'setCustomerIsGuest'],
@@ -979,4 +996,37 @@ public function testGetCartForCustomer()
979996
->willReturn($cartMock);
980997
$this->assertEquals($cartMock, $this->model->getCartForCustomer($customerId));
981998
}
999+
1000+
/**
1001+
* Get any object property value.
1002+
*
1003+
* @param $object
1004+
* @param $property
1005+
* @return mixed
1006+
*/
1007+
protected function getPropertyValue($object, $property)
1008+
{
1009+
$reflection = new \ReflectionClass(get_class($object));
1010+
$reflectionProperty = $reflection->getProperty($property);
1011+
$reflectionProperty->setAccessible(true);
1012+
1013+
return $reflectionProperty->getValue($object);
1014+
}
1015+
1016+
/**
1017+
* Set object property value.
1018+
*
1019+
* @param $object
1020+
* @param $property
1021+
* @param $value
1022+
*/
1023+
protected function setPropertyValue(&$object, $property, $value)
1024+
{
1025+
$reflection = new \ReflectionClass(get_class($object));
1026+
$reflectionProperty = $reflection->getProperty($property);
1027+
$reflectionProperty->setAccessible(true);
1028+
$reflectionProperty->setValue($object, $value);
1029+
1030+
return $object;
1031+
}
9821032
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public function testAssignCustomer()
7070
$quote = $this->objectManager->create('Magento\Quote\Model\Quote')->load('test01', 'reserved_order_id');
7171
$cartId = $quote->getId();
7272
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
73-
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
74-
->create('Magento\Quote\Model\QuoteIdMaskFactory')
75-
->create();
73+
$quoteIdMaskFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
74+
->create('Magento\Quote\Model\QuoteIdMaskFactory');
75+
$quoteIdMask = $quoteIdMaskFactory->create();
7676
$quoteIdMask->load($cartId, 'quote_id');
7777
//Use masked cart Id
7878
$cartId = $quoteIdMask->getMaskedId();
@@ -110,6 +110,7 @@ public function testAssignCustomer()
110110
$this->assertEquals($customer->getId(), $quote->getCustomerId());
111111
$this->assertEquals($customer->getFirstname(), $quote->getCustomerFirstname());
112112
$this->assertEquals($customer->getLastname(), $quote->getCustomerLastname());
113+
$this->assertNull($quoteIdMaskFactory->create()->load($cartId, 'masked_id')->getId());
113114
}
114115

115116
/**

0 commit comments

Comments
 (0)