Skip to content

Commit 3c6ea77

Browse files
author
olysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-72611' into forwardports
2 parents b9b5311 + a506526 commit 3c6ea77

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

app/code/Magento/Sales/Controller/Adminhtml/Order/Create/Save.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function execute()
6363
}
6464
$resultRedirect->setPath('sales/*/');
6565
} catch (\Magento\Framework\Exception\LocalizedException $e) {
66+
// customer can be created before place order flow is completed and should be stored in current session
67+
$this->_getSession()->setCustomerId($this->_getSession()->getQuote()->getCustomerId());
6668
$message = $e->getMessage();
6769
if (!empty($message)) {
6870
$this->messageManager->addError($message);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Controller\Adminhtml\Order\Create;
7+
8+
use Magento\Backend\Model\Session\Quote;
9+
use Magento\Customer\Api\CustomerRepositoryInterface;
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Message\MessageInterface;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Sales\Model\Service\OrderService;
15+
use Magento\TestFramework\TestCase\AbstractBackendController;
16+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
17+
18+
class SaveTest extends AbstractBackendController
19+
{
20+
/**
21+
* Checks a case when order creation is failed on payment method processing but new customer already created
22+
* in the database and after new controller dispatching the customer should be already loaded in session
23+
* to prevent invalid validation.
24+
*
25+
* @magentoAppArea adminhtml
26+
* @magentoDataFixture Magento/Sales/_files/quote_with_new_customer.php
27+
*/
28+
public function testExecuteWithPaymentOperation()
29+
{
30+
$quote = $this->getQuote('2000000001');
31+
$session = $this->_objectManager->get(Quote::class);
32+
$session->setQuoteId($quote->getId());
33+
$session->setCustomerId(0);
34+
35+
$email = 'john.doe001@test.com';
36+
$data = [
37+
'account' => [
38+
'email' => $email
39+
]
40+
];
41+
$this->getRequest()->setPostValue(['order' => $data]);
42+
43+
/** @var OrderService|MockObject $orderService */
44+
$orderService = $this->getMockBuilder(OrderService::class)
45+
->disableOriginalConstructor()
46+
->getMock();
47+
$orderService->method('place')
48+
->willThrowException(new LocalizedException(__('Transaction has been declined.')));
49+
$this->_objectManager->addSharedInstance($orderService, OrderService::class);
50+
51+
$this->dispatch('backend/sales/order_create/save');
52+
$this->assertSessionMessages(
53+
self::equalTo(['Transaction has been declined.']),
54+
MessageInterface::TYPE_ERROR
55+
);
56+
57+
/** @var CustomerRepositoryInterface $customerRepository */
58+
$customerRepository = $this->_objectManager->get(CustomerRepositoryInterface::class);
59+
$customer = $customerRepository->get($email);
60+
61+
self::assertNotEmpty($session->getCustomerId());
62+
self::assertEquals($customer->getId(), $session->getCustomerId());
63+
64+
$this->_objectManager->removeSharedInstance(OrderService::class);
65+
}
66+
67+
/**
68+
* Gets quote by reserved order id.
69+
*
70+
* @param string $reservedOrderId
71+
* @return \Magento\Quote\Api\Data\CartInterface
72+
*/
73+
private function getQuote($reservedOrderId)
74+
{
75+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
76+
$searchCriteriaBuilder = $this->_objectManager->get(SearchCriteriaBuilder::class);
77+
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
78+
->create();
79+
80+
/** @var CartRepositoryInterface $quoteRepository */
81+
$quoteRepository = $this->_objectManager->get(CartRepositoryInterface::class);
82+
$items = $quoteRepository->getList($searchCriteria)->getItems();
83+
return array_pop($items);
84+
}
85+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Api\ProductRepositoryInterface;
8+
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
10+
use Magento\Catalog\Model\Product\Visibility;
11+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
12+
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Model\Quote;
14+
use Magento\Quote\Model\Quote\Address;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
18+
/** @var \Magento\TestFramework\ObjectManager $objectManager */
19+
$objectManager = Bootstrap::getObjectManager();
20+
21+
/** @var Product $product */
22+
$product = $objectManager->create(Product::class);
23+
$product->setTypeId('simple')
24+
->setAttributeSetId(4)
25+
->setName('Simple Product')
26+
->setSku('simple001')
27+
->setPrice(10)
28+
->setQty(100)
29+
->setVisibility(Visibility::VISIBILITY_BOTH)
30+
->setStatus(Status::STATUS_ENABLED);
31+
32+
/** @var StockItemInterface $stockItem */
33+
$stockItem = $objectManager->create(StockItemInterface::class);
34+
$stockItem->setQty(100)
35+
->setIsInStock(true);
36+
$extensionAttributes = $product->getExtensionAttributes();
37+
$extensionAttributes->setStockItem($stockItem);
38+
39+
/** @var ProductRepositoryInterface $productRepository */
40+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
41+
$product = $productRepository->save($product);
42+
43+
$addressData = include __DIR__ . '/address_data.php';
44+
$billingAddress = $objectManager->create(Address::class, ['data' => $addressData]);
45+
$billingAddress->setAddressType('billing');
46+
47+
$shippingAddress = clone $billingAddress;
48+
$shippingAddress->setId(null)
49+
->setAddressType('shipping');
50+
51+
/** @var \Magento\Store\Api\Data\StoreInterface $store */
52+
$store = $objectManager->get(StoreManagerInterface::class)
53+
->getStore();
54+
55+
/** @var Quote $quote */
56+
$quote = $objectManager->create(Quote::class);
57+
$quote->setCustomerIsGuest(false)
58+
->setCustomerEmail('john.doe001@test.com')
59+
->setStoreId($store->getId())
60+
->setReservedOrderId('2000000001')
61+
->setBillingAddress($billingAddress)
62+
->setShippingAddress($shippingAddress)
63+
->addProduct($product);
64+
65+
$quote->getPayment()
66+
->setMethod('checkmo');
67+
$quote->getShippingAddress()
68+
->setShippingMethod('flatrate_flatrate')
69+
->setCollectShippingRates(true);
70+
$quote->collectTotals();
71+
72+
/** @var CartRepositoryInterface $quoteRepository */
73+
$quoteRepository = $objectManager->get(CartRepositoryInterface::class);
74+
$quoteRepository->save($quote);

0 commit comments

Comments
 (0)