Skip to content

Commit a343105

Browse files
author
Sergey Shvets
committed
Merge remote-tracking branch 'origin/MAGETWO-71051' into chaika-pr1
2 parents 78e72eb + eec3eed commit a343105

File tree

2 files changed

+149
-41
lines changed
  • app/code/Magento/Sales/Model/AdminOrder
  • dev/tests/integration/testsuite/Magento/Sales/Model/AdminOrder

2 files changed

+149
-41
lines changed

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,9 @@ public function applyCoupon($code)
15761576
public function setAccountData($accountData)
15771577
{
15781578
$customer = $this->getQuote()->getCustomer();
1579+
if (empty($accountData['email'])) {
1580+
$accountData['email'] = $customer->getEmail();
1581+
}
15791582
$form = $this->_createCustomerForm($customer);
15801583

15811584
// emulate request
@@ -1670,8 +1673,7 @@ public function importPostData($data)
16701673
*/
16711674
protected function _customerIsInStore($store)
16721675
{
1673-
$customerId = (int)$this->getSession()->getCustomerId();
1674-
$customer = $this->customerRepository->getById($customerId);
1676+
$customer = $this->getQuote()->getCustomer();
16751677

16761678
return $customer->getWebsiteId() == $store->getWebsiteId()
16771679
|| $this->accountManagement->isCustomerInStore($customer->getWebsiteId(), $store->getId());

dev/tests/integration/testsuite/Magento/Sales/Model/AdminOrder/CreateTest.php

Lines changed: 145 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@
55
*/
66
namespace Magento\Sales\Model\AdminOrder;
77

8+
use Magento\Sales\Api\OrderManagementInterface;
89
use Magento\TestFramework\Helper\Bootstrap;
910
use Magento\Sales\Model\Order;
1011
use Magento\Framework\Registry;
12+
use Magento\Framework\Message\ManagerInterface;
13+
use Magento\Backend\Model\Session\Quote;
14+
use Magento\Wishlist\Model\Wishlist;
15+
use Magento\Quote\Model\Quote\Address;
16+
use Magento\Customer\Model\Customer;
17+
use Magento\Catalog\Model\Product;
18+
use Magento\Quote\Model\Quote\Address\Rate;
19+
use Magento\Customer\Api\CustomerRepositoryInterface;
20+
use Magento\Customer\Api\AddressRepositoryInterface;
1121

1222
/**
1323
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -26,9 +36,9 @@ class CreateTest extends \PHPUnit_Framework_TestCase
2636
protected function setUp()
2737
{
2838
parent::setUp();
29-
$this->_messageManager = Bootstrap::getObjectManager()->get('Magento\Framework\Message\ManagerInterface');
39+
$this->_messageManager = Bootstrap::getObjectManager()->get(ManagerInterface::class);
3040
$this->_model = Bootstrap::getObjectManager()->create(
31-
'Magento\Sales\Model\AdminOrder\Create',
41+
Create::class,
3242
['messageManager' => $this->_messageManager]
3343
);
3444
}
@@ -40,13 +50,13 @@ protected function setUp()
4050
public function testInitFromOrderShippingAddressSameAsBillingWhenEmpty()
4151
{
4252
/** @var $order \Magento\Sales\Model\Order */
43-
$order = Bootstrap::getObjectManager()->create('Magento\Sales\Model\Order');
53+
$order = Bootstrap::getObjectManager()->create(Order::class);
4454
$order->loadByIncrementId('100000001');
4555
$this->assertNull($order->getShippingAddress());
4656

4757
/** @var $objectManager \Magento\TestFramework\ObjectManager */
4858
$objectManager = Bootstrap::getObjectManager();
49-
$objectManager->get('Magento\Framework\Registry')->unregister('rule_data');
59+
$objectManager->get(Registry::class)->unregister('rule_data');
5060
$this->_model->initFromOrder($order);
5161

5262
$this->assertNull($order->getShippingAddress());
@@ -60,14 +70,14 @@ public function testInitFromOrderShippingAddressSameAsBillingWhenEmpty()
6070
public function testInitFromOrderShippingAddressSameAsBillingWhenSame()
6171
{
6272
/** @var $order \Magento\Sales\Model\Order */
63-
$order = Bootstrap::getObjectManager()->create('Magento\Sales\Model\Order');
73+
$order = Bootstrap::getObjectManager()->create(Order::class);
6474
$order->loadByIncrementId('100000001');
6575

6676
$this->assertNull($order->getShippingAddress()->getSameAsBilling());
6777

6878
/** @var $objectManager \Magento\TestFramework\ObjectManager */
6979
$objectManager = Bootstrap::getObjectManager();
70-
$objectManager->get('Magento\Framework\Registry')->unregister('rule_data');
80+
$objectManager->get(Registry::class)->unregister('rule_data');
7181
$this->_model->initFromOrder($order);
7282

7383
$this->assertTrue($order->getShippingAddress()->getSameAsBilling());
@@ -84,12 +94,12 @@ public function testInitFromOrderShippingAddressSameAsBillingWhenDifferent()
8494
$objectManager = Bootstrap::getObjectManager();
8595

8696
/** @var $order \Magento\Sales\Model\Order */
87-
$order = $objectManager->create('Magento\Sales\Model\Order');
97+
$order = $objectManager->create(Order::class);
8898
$order->loadByIncrementId('100000002');
8999

90100
$this->assertNull($order->getShippingAddress()->getSameAsBilling());
91101

92-
$objectManager->get('Magento\Framework\Registry')->unregister('rule_data');
102+
$objectManager->get(Registry::class)->unregister('rule_data');
93103
$this->_model->initFromOrder($order);
94104

95105
$this->assertFalse($order->getShippingAddress()->getSameAsBilling());
@@ -104,7 +114,7 @@ public function testInitFromOrderCcInformationDeleted()
104114
$objectManager = Bootstrap::getObjectManager();
105115

106116
/** @var $order \Magento\Sales\Model\Order */
107-
$order = $objectManager->create('Magento\Sales\Model\Order');
117+
$order = $objectManager->create(Order::class);
108118
$order->loadByIncrementId('100000001');
109119

110120
$payment = $order->getPayment();
@@ -113,7 +123,7 @@ public function testInitFromOrderCcInformationDeleted()
113123
$this->assertEquals('AE', $payment->getCcType());
114124
$this->assertEquals('0005', $payment->getCcLast4());
115125

116-
$objectManager->get('Magento\Framework\Registry')->unregister('rule_data');
126+
$objectManager->get(Registry::class)->unregister('rule_data');
117127
$payment = $this->_model->initFromOrder($order)->getQuote()->getPayment();
118128

119129
$this->assertNull($payment->getCcExpMonth());
@@ -153,8 +163,8 @@ public function testInitFromOrderWithEmptyPaymentDetails()
153163
*/
154164
public function testGetCustomerWishlistNoCustomerId()
155165
{
156-
/** @var \Magento\Backend\Model\Session\Quote $session */
157-
$session = Bootstrap::getObjectManager()->create('Magento\Backend\Model\Session\Quote');
166+
/** @var Quote $session */
167+
$session = Bootstrap::getObjectManager()->create(Quote::class);
158168
$session->setCustomerId(null);
159169
$this->assertFalse(
160170
$this->_model->getCustomerWishlist(true),
@@ -172,15 +182,15 @@ public function testGetCustomerWishlist()
172182
{
173183
$customerIdFromFixture = 1;
174184
$productIdFromFixture = 1;
175-
/** @var \Magento\Backend\Model\Session\Quote $session */
176-
$session = Bootstrap::getObjectManager()->create('Magento\Backend\Model\Session\Quote');
185+
/** @var Quote $session */
186+
$session = Bootstrap::getObjectManager()->create(Quote::class);
177187
$session->setCustomerId($customerIdFromFixture);
178188

179189
/** Test new wishlist creation for the customer specified above */
180-
/** @var \Magento\Wishlist\Model\Wishlist $wishlist */
190+
/** @var Wishlist $wishlist */
181191
$wishlist = $this->_model->getCustomerWishlist(true);
182192
$this->assertInstanceOf(
183-
'Magento\Wishlist\Model\Wishlist',
193+
Wishlist::class,
184194
$wishlist,
185195
'New Wish List is expected to be created if existing Customer does not have one yet.'
186196
);
@@ -218,7 +228,7 @@ public function testSetBillingAddress()
218228
/** Validate data before creating address object */
219229
$this->_model->setIsValidate(true)->setBillingAddress($addressData);
220230
$this->assertInstanceOf(
221-
'Magento\Quote\Model\Quote\Address',
231+
Address::class,
222232
$this->_model->getBillingAddress(),
223233
'Billing address object was not created.'
224234
);
@@ -250,8 +260,8 @@ public function testSetBillingAddress()
250260
public function testSetBillingAddressValidationErrors()
251261
{
252262
$customerIdFromFixture = 1;
253-
/** @var \Magento\Backend\Model\Session\Quote $session */
254-
$session = Bootstrap::getObjectManager()->create('Magento\Backend\Model\Session\Quote');
263+
/** @var Quote $session */
264+
$session = Bootstrap::getObjectManager()->create(Quote::class);
255265
$session->setCustomerId($customerIdFromFixture);
256266
$invalidAddressData = array_merge($this->_getValidAddressData(), ['firstname' => '', 'lastname' => '']);
257267
/**
@@ -319,8 +329,8 @@ public function testCreateOrderNewCustomerDifferentAddresses()
319329
);
320330
$order = $this->_model->createOrder();
321331
$this->_verifyCreatedOrder($order, $shippingMethod);
322-
/** @var \Magento\Customer\Model\Customer $customer */
323-
$customer = Bootstrap::getObjectManager()->create('Magento\Customer\Model\Customer');
332+
/** @var Customer $customer */
333+
$customer = Bootstrap::getObjectManager()->create(Customer::class);
324334
$customer->load($order->getCustomerId());
325335
$this->assertEquals(
326336
$firstNameForShippingAddress,
@@ -450,6 +460,102 @@ public function testCreateOrderExistingCustomer()
450460
$this->_verifyCreatedOrder($order, $shippingMethod);
451461
}
452462

463+
/**
464+
* Tests order creation with new customer after failed first place order action.
465+
*
466+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
467+
* @magentoDbIsolation enabled
468+
* @magentoAppIsolation enabled
469+
* @dataProvider createOrderNewCustomerWithFailedFirstPlaceOrderActionDataProvider
470+
* @param string $customerEmailFirstAttempt
471+
* @param string $customerEmailSecondAttempt
472+
*/
473+
public function testCreateOrderNewCustomerWithFailedFirstPlaceOrderAction(
474+
$customerEmailFirstAttempt,
475+
$customerEmailSecondAttempt
476+
) {
477+
$productIdFromFixture = 1;
478+
$shippingMethod = 'freeshipping_freeshipping';
479+
$paymentMethod = 'checkmo';
480+
$shippingAddressAsBilling = 1;
481+
$customerEmail = $customerEmailFirstAttempt;
482+
$orderData = [
483+
'currency' => 'USD',
484+
'account' => [
485+
'group_id' => '1',
486+
'email' => $customerEmail
487+
],
488+
'billing_address' => array_merge(
489+
$this->_getValidAddressData(),
490+
['save_in_address_book' => '1']
491+
),
492+
'shipping_method' => $shippingMethod,
493+
'comment' => [
494+
'customer_note' => ''
495+
],
496+
'send_confirmation' => false,
497+
];
498+
$paymentData = ['method' => $paymentMethod];
499+
500+
$this->_preparePreconditionsForCreateOrder(
501+
$productIdFromFixture,
502+
$customerEmail,
503+
$shippingMethod,
504+
$shippingAddressAsBilling,
505+
$paymentData,
506+
$orderData,
507+
$paymentMethod
508+
);
509+
510+
// Emulates failing place order action
511+
$orderManagement = $this->getMockForAbstractClass(OrderManagementInterface::class);
512+
$orderManagement->method('place')
513+
->willThrowException(new \Exception('Can\'t place order'));
514+
Bootstrap::getObjectManager()->addSharedInstance($orderManagement, OrderManagementInterface::class);
515+
try {
516+
$this->_model->createOrder();
517+
} catch (\Exception $e) {
518+
Bootstrap::getObjectManager()->removeSharedInstance(OrderManagementInterface::class);
519+
}
520+
521+
$customerEmail = $customerEmailSecondAttempt ?: $this->_model->getQuote()->getCustomer()->getEmail();
522+
$orderData['account']['email'] = $customerEmailSecondAttempt;
523+
524+
$this->_preparePreconditionsForCreateOrder(
525+
$productIdFromFixture,
526+
$customerEmail,
527+
$shippingMethod,
528+
$shippingAddressAsBilling,
529+
$paymentData,
530+
$orderData,
531+
$paymentMethod
532+
);
533+
534+
$order = $this->_model->createOrder();
535+
$this->_verifyCreatedOrder($order, $shippingMethod);
536+
}
537+
538+
/**
539+
* Email before and after failed first place order action.
540+
*
541+
* @case #1 Is the same.
542+
* @case #2 Is empty.
543+
* @case #3 Filled after failed first place order action.
544+
* @case #4 Empty after failed first place order action.
545+
* @case #5 Changed after failed first place order action.
546+
* @return array
547+
*/
548+
public function createOrderNewCustomerWithFailedFirstPlaceOrderActionDataProvider()
549+
{
550+
return [
551+
1 => ['customer@email.com', 'customer@email.com'],
552+
2 => ['', ''],
553+
3 => ['', 'customer@email.com'],
554+
4 => ['customer@email.com', ''],
555+
5 => ['customer@email.com', 'changed_customer@email.com'],
556+
];
557+
}
558+
453559
/**
454560
* @magentoAppIsolation enabled
455561
* @magentoDataFixture Magento/Sales/_files/quote.php
@@ -460,11 +566,11 @@ public function testGetCustomerCartExistingCart()
460566
$fixtureCustomerId = 1;
461567

462568
/** Preconditions */
463-
/** @var \Magento\Backend\Model\Session\Quote $session */
464-
$session = Bootstrap::getObjectManager()->create('Magento\Backend\Model\Session\Quote');
569+
/** @var Quote $session */
570+
$session = Bootstrap::getObjectManager()->create(Quote::class);
465571
$session->setCustomerId($fixtureCustomerId);
466572
/** @var $quoteFixture \Magento\Quote\Model\Quote */
467-
$quoteFixture = Bootstrap::getObjectManager()->create('Magento\Quote\Model\Quote');
573+
$quoteFixture = Bootstrap::getObjectManager()->create(\Magento\Quote\Model\Quote::class);
468574
$quoteFixture->load('test01', 'reserved_order_id');
469575
$quoteFixture->setCustomerIsGuest(false)->setCustomerId($fixtureCustomerId)->save();
470576

@@ -488,8 +594,8 @@ public function testGetCustomerCartNewCart()
488594
$customerEmailFromFixture = 'customer@example.com';
489595

490596
/** Preconditions */
491-
/** @var \Magento\Backend\Model\Session\Quote $session */
492-
$session = Bootstrap::getObjectManager()->create('Magento\Backend\Model\Session\Quote');
597+
/** @var Quote $session */
598+
$session = Bootstrap::getObjectManager()->create(Quote::class);
493599
$session->setCustomerId($customerIdFromFixture);
494600

495601
/** SUT execution */
@@ -525,19 +631,19 @@ protected function _preparePreconditionsForCreateOrder(
525631
$customerIdFromFixture = null
526632
) {
527633
/** Disable product options */
528-
/** @var \Magento\Catalog\Model\Product $product */
529-
$product = Bootstrap::getObjectManager()->create('Magento\Catalog\Model\Product');
634+
/** @var Product $product */
635+
$product = Bootstrap::getObjectManager()->create(Product::class);
530636
$product->load($productIdFromFixture)->setHasOptions(false)->save();
531637

532638
/** Set current customer */
533-
/** @var \Magento\Backend\Model\Session\Quote $session */
534-
$session = Bootstrap::getObjectManager()->get('Magento\Backend\Model\Session\Quote');
639+
/** @var Quote $session */
640+
$session = Bootstrap::getObjectManager()->get(Quote::class);
535641
if ($customerIdFromFixture !== null) {
536642
$session->setCustomerId($customerIdFromFixture);
537643

538644
/** Unset fake IDs for default billing and shipping customer addresses */
539-
/** @var \Magento\Customer\Model\Customer $customer */
540-
$customer = Bootstrap::getObjectManager()->create('Magento\Customer\Model\Customer');
645+
/** @var Customer $customer */
646+
$customer = Bootstrap::getObjectManager()->create(Customer::class);
541647
$customer->load($customerIdFromFixture)->setDefaultBilling(null)->setDefaultShipping(null)->save();
542648
} else {
543649
/**
@@ -548,8 +654,8 @@ protected function _preparePreconditionsForCreateOrder(
548654
}
549655

550656
/** Emulate availability of shipping method (all are disabled by default) */
551-
/** @var $rate \Magento\Quote\Model\Quote\Address\Rate */
552-
$rate = Bootstrap::getObjectManager()->create('Magento\Quote\Model\Quote\Address\Rate');
657+
/** @var $rate Rate */
658+
$rate = Bootstrap::getObjectManager()->create(Rate::class);
553659
$rate->setCode($shippingMethod);
554660
$this->_model->getQuote()->getShippingAddress()->addShippingRate($rate);
555661

@@ -657,11 +763,11 @@ private function getCustomerById($id)
657763
}
658764

659765
/**
660-
* @return \Magento\Customer\Api\CustomerRepositoryInterface
766+
* @return CustomerRepositoryInterface
661767
*/
662768
private function getCustomerRepository()
663769
{
664-
return Bootstrap::getObjectManager()->create('Magento\Customer\Api\CustomerRepositoryInterface');
770+
return Bootstrap::getObjectManager()->create(CustomerRepositoryInterface::class);
665771
}
666772

667773
/**
@@ -674,11 +780,11 @@ private function getAddressById($id)
674780
}
675781

676782
/**
677-
* @return \Magento\Customer\Api\AddressRepositoryInterface
783+
* @return AddressRepositoryInterface
678784
*/
679785
private function getAddressRepository()
680786
{
681-
/** @var \Magento\Customer\Api\AddressRepositoryInterface $addressRepository */
682-
return Bootstrap::getObjectManager()->create('Magento\Customer\Api\AddressRepositoryInterface');
787+
/** @var AddressRepositoryInterface $addressRepository */
788+
return Bootstrap::getObjectManager()->create(AddressRepositoryInterface::class);
683789
}
684790
}

0 commit comments

Comments
 (0)