Skip to content

Commit 834fa8e

Browse files
author
Mike Weis
committed
MAGETWO-31933: Can't change shipping address when creating order in backend
- provide simpler fix - re-flow original unit test
1 parent 1670359 commit 834fa8e

File tree

2 files changed

+77
-177
lines changed

2 files changed

+77
-177
lines changed

app/code/Magento/Backend/Model/Session/Quote.php

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,9 @@ public function getQuote()
147147
$this->_quote->setStoreId($this->getStoreId());
148148
}
149149

150-
if ($this->getCustomerId()) {
151-
// on a new quote, this will return an empty array
152-
$addresses = $this->_quote->getAllAddresses();
153-
$billingAddress = $this->findAddressByType(
154-
$addresses,
155-
\Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_BILLING
156-
);
157-
$shippingAddress = $this->findAddressByType(
158-
$addresses,
159-
\Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING
160-
);
150+
if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) {
161151
$customer = $this->customerRepository->getById($this->getCustomerId());
162-
$this->_quote->assignCustomerWithAddressChange($customer, $billingAddress, $shippingAddress);
152+
$this->_quote->assignCustomer($customer);
163153
}
164154
}
165155
$this->_quote->setIgnoreOldQty(true);
@@ -169,25 +159,6 @@ public function getQuote()
169159
return $this->_quote;
170160
}
171161

172-
/**
173-
* Returns the type of address requested, or null if no type of address is found
174-
*
175-
* @param \Magento\Quote\Model\Quote\Address[] $addresses
176-
* @param string $type ex: 'billing' or 'shipping'
177-
* @return \Magento\Quote\Model\Quote\Address|null
178-
*/
179-
protected function findAddressByType($addresses, $type)
180-
{
181-
$theAddress = null;
182-
foreach ($addresses as $address) {
183-
if ($address->getAddressType() == $type) {
184-
$theAddress = $address;
185-
break;
186-
}
187-
}
188-
return $theAddress;
189-
}
190-
191162
/**
192163
* Retrieve store model object
193164
*

app/code/Magento/Backend/Test/Unit/Model/Session/QuoteTest.php

Lines changed: 75 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
/**
99
* Class QuoteTest
10-
*
1110
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12-
* @SuppressWarnings(PHPMD.TooManyFields)
1311
*/
1412
class QuoteTest extends \PHPUnit_Framework_TestCase
1513
{
@@ -88,16 +86,6 @@ class QuoteTest extends \PHPUnit_Framework_TestCase
8886
*/
8987
protected $groupManagementMock;
9088

91-
/**
92-
* @var \Magento\Quote\Model\Quote\Address|\PHPUnit_Framework_MockObject_MockObject
93-
*/
94-
protected $billingAddressMock;
95-
96-
/**
97-
* @var \Magento\Quote\Model\Quote\Address|\PHPUnit_Framework_MockObject_MockObject
98-
*/
99-
protected $shippingAddressMock;
100-
10189
/**
10290
* Set up
10391
*
@@ -227,18 +215,44 @@ protected function setUp()
227215
/**
228216
* Run test getQuote method
229217
*
230-
* @param \Magento\Quote\Model\Quote\Address[] $allAddresses
231-
* @param \Magento\Quote\Model\Quote\Address|null $expectedBillingAddress
232-
* @param \Magento\Quote\Model\Quote\Address|null $expectedShippingAddress
233218
* @return void
234-
* @dataProvider allAddressesDataProvider
235219
*/
236-
public function testGetQuoteWithoutQuoteId($allAddresses, $expectedBillingAddress, $expectedShippingAddress)
220+
public function testGetQuoteWithoutQuoteId()
237221
{
238-
$storeId = 10;
239222
$quoteId = 22;
240-
$customerGroupId = 77;
223+
$storeId = 10;
241224
$customerId = 66;
225+
$customerGroupId = 77;
226+
227+
$this->quote->expects($this->any())
228+
->method('getQuoteId')
229+
->will($this->returnValue(null));
230+
$this->quote->expects($this->any())
231+
->method('setQuoteId')
232+
->with($quoteId);
233+
$this->quote->expects($this->any())
234+
->method('getStoreId')
235+
->will($this->returnValue($storeId));
236+
$this->quote->expects($this->any())
237+
->method('getCustomerId')
238+
->will($this->returnValue($customerId));
239+
240+
$defaultGroup = $this->getMockBuilder('Magento\Customer\Api\Data\GroupInterface')
241+
->getMock();
242+
$defaultGroup->expects($this->any())
243+
->method('getId')
244+
->will($this->returnValue($customerGroupId));
245+
$this->groupManagementMock->expects($this->any())
246+
->method('getDefaultGroup')
247+
->will($this->returnValue($defaultGroup));
248+
249+
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
250+
->disableOriginalConstructor()
251+
->getMock();
252+
$this->customerRepositoryMock->expects($this->once())
253+
->method('getById')
254+
->with($customerId)
255+
->willReturn($dataCustomerMock);
242256

243257
$quoteMock = $this->getMock(
244258
'Magento\Quote\Model\Quote',
@@ -247,38 +261,18 @@ public function testGetQuoteWithoutQuoteId($allAddresses, $expectedBillingAddres
247261
'setCustomerGroupId',
248262
'setIsActive',
249263
'getId',
250-
'assignCustomerWithAddressChange',
264+
'assignCustomer',
251265
'setIgnoreOldQty',
252266
'setIsSuperMode',
253-
'getAllAddresses',
254267
'__wakeup'
255268
],
256269
[],
257270
'',
258271
false
259272
);
260-
261-
$defaultGroup = $this->getMockBuilder('Magento\Customer\Api\Data\GroupInterface')
262-
->getMock();
263-
$defaultGroup->expects($this->any())
264-
->method('getId')
265-
->will($this->returnValue($customerGroupId));
266-
$this->groupManagementMock->expects($this->any())
267-
->method('getDefaultGroup')
268-
->will($this->returnValue($defaultGroup));
269-
270-
$this->quoteRepositoryMock->expects($this->once())
271-
->method('create')
272-
->will($this->returnValue($quoteMock));
273-
$this->quote->expects($this->any())
274-
->method('getStoreId')
275-
->will($this->returnValue($storeId));
276273
$quoteMock->expects($this->once())
277274
->method('setStoreId')
278275
->with($storeId);
279-
$this->quote->expects($this->any())
280-
->method('getQuoteId')
281-
->will($this->returnValue(null));
282276
$quoteMock->expects($this->once())
283277
->method('setCustomerGroupId')
284278
->with($customerGroupId)
@@ -287,90 +281,27 @@ public function testGetQuoteWithoutQuoteId($allAddresses, $expectedBillingAddres
287281
->method('setIsActive')
288282
->with(false)
289283
->will($this->returnSelf());
290-
$this->quoteRepositoryMock->expects($this->once())
291-
->method('save')
292-
->with($quoteMock);
293284
$quoteMock->expects($this->once())
294285
->method('getId')
295286
->will($this->returnValue($quoteId));
296-
$this->quote->expects($this->any())
297-
->method('setQuoteId')
298-
->with($quoteId);
299-
$this->quote->expects($this->any())
300-
->method('getCustomerId')
301-
->will($this->returnValue($customerId));
302-
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
303-
->disableOriginalConstructor()
304-
->getMock();
305-
$this->customerRepositoryMock->expects($this->once())
306-
->method('getById')
307-
->with($customerId)
308-
->willReturn($dataCustomerMock);
309287
$quoteMock->expects($this->once())
310-
->method('assignCustomerWithAddressChange')
311-
->with($dataCustomerMock, $expectedBillingAddress, $expectedShippingAddress);
288+
->method('assignCustomer')
289+
->with($dataCustomerMock);
312290
$quoteMock->expects($this->once())
313291
->method('setIgnoreOldQty')
314292
->with(true);
315293
$quoteMock->expects($this->once())
316294
->method('setIsSuperMode')
317295
->with(true);
318-
$quoteMock->expects($this->any())
319-
->method('getAllAddresses')
320-
->will($this->returnValue($allAddresses));
321296

322-
$this->assertEquals($quoteMock, $this->quote->getQuote());
323-
}
324-
325-
/**
326-
* @return array
327-
*/
328-
public function allAddressesDataProvider()
329-
{
330-
// since setup() is called after the dataProvider, ensure we have valid addresses
331-
$this->buildAddressMocks();
332-
333-
return [
334-
'empty addresses' => [
335-
[],
336-
null,
337-
null
338-
],
339-
'use typical addresses' => [
340-
[$this->billingAddressMock, $this->shippingAddressMock],
341-
$this->billingAddressMock,
342-
$this->shippingAddressMock
343-
],
344-
];
345-
}
297+
$this->quoteRepositoryMock->expects($this->once())
298+
->method('create')
299+
->will($this->returnValue($quoteMock));
300+
$this->quoteRepositoryMock->expects($this->once())
301+
->method('save')
302+
->with($quoteMock);
346303

347-
protected function buildAddressMocks()
348-
{
349-
if ($this->billingAddressMock == null) {
350-
$this->billingAddressMock = $this->getMock(
351-
'Magento\Quote\Model\Quote\Address',
352-
['getAddressType'],
353-
[],
354-
'',
355-
false
356-
);
357-
$this->billingAddressMock->expects($this->any())
358-
->method('getAddressType')
359-
->will($this->returnValue(\Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_BILLING));
360-
}
361-
362-
if ($this->shippingAddressMock == null) {
363-
$this->shippingAddressMock = $this->getMock(
364-
'Magento\Quote\Model\Quote\Address',
365-
['getAddressType'],
366-
[],
367-
'',
368-
false
369-
);
370-
$this->shippingAddressMock->expects($this->any())
371-
->method('getAddressType')
372-
->will($this->returnValue(\Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING));
373-
}
304+
$this->assertEquals($quoteMock, $this->quote->getQuote());
374305
}
375306

376307
/**
@@ -380,69 +311,67 @@ protected function buildAddressMocks()
380311
*/
381312
public function testGetQuoteWithQuoteId()
382313
{
383-
$storeId = 10;
384314
$quoteId = 22;
315+
$storeId = 10;
385316
$customerId = 66;
386317

318+
$this->quote->expects($this->any())
319+
->method('getQuoteId')
320+
->will($this->returnValue($quoteId));
321+
$this->quote->expects($this->any())
322+
->method('setQuoteId')
323+
->with($quoteId);
324+
$this->quote->expects($this->any())
325+
->method('getStoreId')
326+
->will($this->returnValue($storeId));
327+
$this->quote->expects($this->any())
328+
->method('getCustomerId')
329+
->will($this->returnValue($customerId));
330+
331+
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
332+
->disableOriginalConstructor()
333+
->getMock();
334+
$this->customerRepositoryMock->expects($this->once())
335+
->method('getById')
336+
->with($customerId)
337+
->willReturn($dataCustomerMock);
338+
387339
$quoteMock = $this->getMock(
388340
'Magento\Quote\Model\Quote',
389341
[
390342
'setStoreId',
391343
'setCustomerGroupId',
392344
'setIsActive',
393345
'getId',
394-
'assignCustomerWithAddressChange',
346+
'assignCustomer',
395347
'setIgnoreOldQty',
396348
'setIsSuperMode',
397-
'getAllAddresses',
398349
'__wakeup'
399350
],
400351
[],
401352
'',
402353
false
403354
);
404-
405-
$this->quoteRepositoryMock->expects($this->once())
406-
->method('create')
407-
->will($this->returnValue($quoteMock));
408-
$this->quote->expects($this->any())
409-
->method('getStoreId')
410-
->will($this->returnValue($storeId));
411355
$quoteMock->expects($this->once())
412356
->method('setStoreId')
413357
->with($storeId);
414-
$this->quote->expects($this->any())
415-
->method('getQuoteId')
416-
->will($this->returnValue($quoteId));
417-
$this->quoteRepositoryMock->expects($this->once())
418-
->method('get')
419-
->with($quoteId)
420-
->willReturn($quoteMock);
421-
$this->quote->expects($this->any())
422-
->method('setQuoteId')
423-
->with($quoteId);
424-
$this->quote->expects($this->any())
425-
->method('getCustomerId')
426-
->will($this->returnValue($customerId));
427-
$dataCustomerMock = $this->getMockBuilder('Magento\Customer\Api\Data\CustomerInterface')
428-
->disableOriginalConstructor()
429-
->getMock();
430-
$this->customerRepositoryMock->expects($this->once())
431-
->method('getById')
432-
->with($customerId)
433-
->willReturn($dataCustomerMock);
434358
$quoteMock->expects($this->once())
435-
->method('assignCustomerWithAddressChange')
359+
->method('assignCustomer')
436360
->with($dataCustomerMock);
437361
$quoteMock->expects($this->once())
438362
->method('setIgnoreOldQty')
439363
->with(true);
440364
$quoteMock->expects($this->once())
441365
->method('setIsSuperMode')
442366
->with(true);
443-
$quoteMock->expects($this->any())
444-
->method('getAllAddresses')
445-
->will($this->returnValue([]));
367+
368+
$this->quoteRepositoryMock->expects($this->once())
369+
->method('create')
370+
->will($this->returnValue($quoteMock));
371+
$this->quoteRepositoryMock->expects($this->once())
372+
->method('get')
373+
->with($quoteId)
374+
->willReturn($quoteMock);
446375

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

0 commit comments

Comments
 (0)